aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/apps/cache/navi/AbstractPointNavigationApp.java
blob: 581776381b9aadda7d7f735ae3beb6005fcbbbfc (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
59
60
61
62
63
64
65
package cgeo.geocaching.apps.cache.navi;

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)
 */
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(Activity activity, cgCache cache, cgWaypoint waypoint, Geopoint coords) {
        if (cache == null && waypoint == null && coords == null) {
            return false;
        }

        try {
            if (isInstalled()) {
                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);

    /**
     * Return the first of the cache coordinates, the waypoint coordinates or the extra coordinates. <code>null</code>
     * entities are skipped.
     *
     * @param cache a cache
     * @param waypoint a waypoint
     * @param coords extra coordinates
     * @return the first non-null coordinates, or null if none are set
     */
    private static Geopoint getCoordinates(final cgCache cache, final cgWaypoint waypoint, final Geopoint coords) {
        if (cache != null && cache.getCoords() != null) {
            return cache.getCoords();
        }

        if (waypoint != null && waypoint.getCoords() != null) {
            return waypoint.getCoords();
        }

        return coords;
    }
}