aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/apps/cache/navi/AbstractPointNavigationApp.java
blob: c5abf7fb173b4819c8b389abbb56104eb74f7841 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package cgeo.geocaching.apps.cache.navi;

import cgeo.geocaching.IGeoData;
import cgeo.geocaching.cgCache;
import cgeo.geocaching.cgWaypoint;
import cgeo.geocaching.geopoint.Geopoint;

import android.app.Activity;

/**
 * navigation app for simple point navigation (no differentiation between cache/waypoint/point)
 *
 * @author bananeweizen
 *
 */
abstract class AbstractPointNavigationApp extends AbstractNavigationApp {

    protected AbstractPointNavigationApp(String name, String intent) {
        super(name, intent);
    }

    protected AbstractPointNavigationApp(String name, String intent, String packageName) {
        super(name, intent, packageName);
    }

    @Override
    public final boolean invoke(IGeoData geo, Activity activity, cgCache cache, cgWaypoint waypoint, Geopoint coords) {
        if (cache == null && waypoint == null && coords == null) {
            return false;
        }

        try {
            if (isInstalled(activity)) {
                final Geopoint point = getCoordinates(cache, waypoint, coords);
                if (point != null) {
                    navigate(activity, point);
                    return true;
                }
            }
        } catch (Exception e) {
            // nothing
        }

        return false;
    }

    protected abstract void navigate(Activity activity, Geopoint point);

    private static Geopoint getCoordinates(cgCache cache, cgWaypoint waypoint, Geopoint coords) {
        if (cache != null && cache.getCoords() != null) {
            return cache.getCoords();
        }
        else if (waypoint != null && waypoint.getCoords() != null) {
            return waypoint.getCoords();
        }
        return coords;
    }
}