diff options
author | Bananeweizen <Bananeweizen@gmx.de> | 2012-05-24 22:22:02 +0200 |
---|---|---|
committer | Bananeweizen <Bananeweizen@gmx.de> | 2012-05-29 21:26:16 +0200 |
commit | b8b1121891602f55f445d293fd69b07ebbbcad21 (patch) | |
tree | 234b75934e357ba7a67fd3670fa0ee5c888684af /main/src/cgeo/geocaching/apps/cache | |
parent | ee725717349df3c119dfbea15578ac023ce9bf45 (diff) | |
download | cgeo-b8b1121891602f55f445d293fd69b07ebbbcad21.zip cgeo-b8b1121891602f55f445d293fd69b07ebbbcad21.tar.gz cgeo-b8b1121891602f55f445d293fd69b07ebbbcad21.tar.bz2 |
refactoring: use interfaces for navigation apps
Diffstat (limited to 'main/src/cgeo/geocaching/apps/cache')
18 files changed, 254 insertions, 231 deletions
diff --git a/main/src/cgeo/geocaching/apps/cache/navi/AbstractPointNavigationApp.java b/main/src/cgeo/geocaching/apps/cache/navi/AbstractPointNavigationApp.java index 5817763..ca8c7db 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/AbstractPointNavigationApp.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/AbstractPointNavigationApp.java @@ -2,14 +2,14 @@ package cgeo.geocaching.apps.cache.navi; import cgeo.geocaching.cgCache; import cgeo.geocaching.cgWaypoint; -import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.apps.AbstractApp; import android.app.Activity; /** * navigation app for simple point navigation (no differentiation between cache/waypoint/point) */ -abstract class AbstractPointNavigationApp extends AbstractNavigationApp { +abstract class AbstractPointNavigationApp extends AbstractApp implements CacheNavigationApp, WaypointNavigationApp, GeopointNavigationApp { protected AbstractPointNavigationApp(String name, String intent) { super(name, intent); @@ -20,46 +20,22 @@ abstract class AbstractPointNavigationApp extends AbstractNavigationApp { } @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; + public void navigate(Activity activity, cgCache cache) { + navigate(activity, cache.getCoords()); } - 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(); - } + @Override + public void navigate(Activity activity, cgWaypoint waypoint) { + navigate(activity, waypoint.getCoords()); + } - if (waypoint != null && waypoint.getCoords() != null) { - return waypoint.getCoords(); - } + @Override + public boolean isEnabled(cgCache cache) { + return cache.getCoords() != null; + } - return coords; + @Override + public boolean isEnabled(cgWaypoint waypoint) { + return waypoint.getCoords() != null; } -}
\ No newline at end of file +} diff --git a/main/src/cgeo/geocaching/apps/cache/navi/AbstractStaticMapsApp.java b/main/src/cgeo/geocaching/apps/cache/navi/AbstractStaticMapsApp.java index ff61404..8e5d7d1 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/AbstractStaticMapsApp.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/AbstractStaticMapsApp.java @@ -8,13 +8,13 @@ import cgeo.geocaching.cgCache; import cgeo.geocaching.cgWaypoint; import cgeo.geocaching.cgeoapplication; import cgeo.geocaching.activity.ActivityMixin; -import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.apps.AbstractApp; import org.apache.commons.lang3.StringUtils; import android.app.Activity; -abstract class AbstractStaticMapsApp extends AbstractNavigationApp { +abstract class AbstractStaticMapsApp extends AbstractApp implements CacheNavigationApp, WaypointNavigationApp { public AbstractStaticMapsApp(String name) { super(name, null); } @@ -25,27 +25,23 @@ abstract class AbstractStaticMapsApp extends AbstractNavigationApp { } protected static boolean hasStaticMap(cgCache cache) { - if (cache != null) { - String geocode = cache.getGeocode(); - if (StringUtils.isNotEmpty(geocode) && cgeoapplication.getInstance().isOffline(geocode, null)) { - return StaticMapsProvider.doesExistStaticMapForCache(geocode); - } + String geocode = cache.getGeocode(); + if (StringUtils.isNotEmpty(geocode) && cgeoapplication.getInstance().isOffline(geocode, null)) { + return StaticMapsProvider.doesExistStaticMapForCache(geocode); } return false; } protected static boolean hasStaticMap(cgWaypoint waypoint) { - if (waypoint != null) { - String geocode = waypoint.getGeocode(); - int id = waypoint.getId(); - if (StringUtils.isNotEmpty(geocode) && cgeoapplication.getInstance().isOffline(geocode, null)) { - return StaticMapsProvider.doesExistStaticMapForWaypoint(geocode, id); - } + String geocode = waypoint.getGeocode(); + int id = waypoint.getId(); + if (StringUtils.isNotEmpty(geocode) && cgeoapplication.getInstance().isOffline(geocode, null)) { + return StaticMapsProvider.doesExistStaticMapForWaypoint(geocode, id); } return false; } - protected static boolean invoke(final Activity activity, final cgCache cache, final cgWaypoint waypoint, final boolean download) { + protected static boolean invokeStaticMaps(final Activity activity, final cgCache cache, final cgWaypoint waypoint, final boolean download) { final ILogable logable = cache != null && cache.getListId() != 0 ? cache : waypoint; final String geocode = logable.getGeocode().toUpperCase(); if (geocode == null) { @@ -56,11 +52,4 @@ abstract class AbstractStaticMapsApp extends AbstractNavigationApp { StaticMapsActivity.startActivity(activity, geocode, download, waypoint); return true; } - - - @Override - public boolean isEnabled(Geopoint geopoint) { - return false; - } - } diff --git a/main/src/cgeo/geocaching/apps/cache/navi/CacheNavigationApp.java b/main/src/cgeo/geocaching/apps/cache/navi/CacheNavigationApp.java new file mode 100644 index 0000000..e47150f --- /dev/null +++ b/main/src/cgeo/geocaching/apps/cache/navi/CacheNavigationApp.java @@ -0,0 +1,17 @@ +package cgeo.geocaching.apps.cache.navi; + +import cgeo.geocaching.cgCache; +import cgeo.geocaching.apps.App; + +import android.app.Activity; + +/** + * interface for navigation to a cache + * + */ +public interface CacheNavigationApp extends App { + void navigate(final Activity activity, final cgCache cache); + + @Override + boolean isEnabled(final cgCache cache); +}
\ No newline at end of file diff --git a/main/src/cgeo/geocaching/apps/cache/navi/CompassApp.java b/main/src/cgeo/geocaching/apps/cache/navi/CompassApp.java index 1576617..da3d5f9 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/CompassApp.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/CompassApp.java @@ -4,11 +4,12 @@ import cgeo.geocaching.R; import cgeo.geocaching.cgCache; import cgeo.geocaching.cgWaypoint; import cgeo.geocaching.cgeonavigate; +import cgeo.geocaching.apps.AbstractApp; import cgeo.geocaching.geopoint.Geopoint; import android.app.Activity; -class CompassApp extends AbstractNavigationApp { +class CompassApp extends AbstractApp implements CacheNavigationApp, WaypointNavigationApp, GeopointNavigationApp { CompassApp() { super(getString(R.string.compass_title), null); @@ -20,22 +21,28 @@ class CompassApp extends AbstractNavigationApp { } @Override - public boolean invoke(Activity activity, cgCache cache, cgWaypoint waypoint, final Geopoint coords) { - - if (cache != null && cache.getGeocode() != null) { - cgeonavigate.startActivity(activity, cache.getGeocode(), cache.getName(), cache.getCoords(), null); - return true; - } - if (waypoint != null && waypoint.getCoords() != null) { - cgeonavigate.startActivity(activity, waypoint.getPrefix() + "/" + waypoint.getLookup(), waypoint.getName(), waypoint.getCoords(), null); - return true; - } - if (coords != null) { - cgeonavigate.startActivity(activity, getString(R.string.navigation_direct_navigation), getString(R.string.navigation_target), coords, null); - return true; - } - // search is not handled here - return false; + public void navigate(Activity activity, Geopoint coords) { + cgeonavigate.startActivity(activity, getString(R.string.navigation_direct_navigation), getString(R.string.navigation_target), coords, null); + } + + @Override + public void navigate(Activity activity, cgWaypoint waypoint) { + cgeonavigate.startActivity(activity, waypoint.getPrefix() + "/" + waypoint.getLookup(), waypoint.getName(), waypoint.getCoords(), null); + } + + @Override + public boolean isEnabled(cgWaypoint waypoint) { + return waypoint.getCoords() != null; + } + + @Override + public void navigate(Activity activity, cgCache cache) { + cgeonavigate.startActivity(activity, cache.getGeocode(), cache.getName(), cache.getCoords(), null); + } + + @Override + public boolean isEnabled(cgCache cache) { + return cache.getGeocode() != null; } }
\ No newline at end of file diff --git a/main/src/cgeo/geocaching/apps/cache/navi/DownloadStaticMapsApp.java b/main/src/cgeo/geocaching/apps/cache/navi/DownloadStaticMapsApp.java index 1625ef7..f093937 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/DownloadStaticMapsApp.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/DownloadStaticMapsApp.java @@ -3,7 +3,6 @@ package cgeo.geocaching.apps.cache.navi; import cgeo.geocaching.R; import cgeo.geocaching.cgCache; import cgeo.geocaching.cgWaypoint; -import cgeo.geocaching.geopoint.Geopoint; import android.app.Activity; @@ -14,23 +13,22 @@ class DownloadStaticMapsApp extends AbstractStaticMapsApp { } @Override - public boolean invoke(Activity activity, cgCache cache, cgWaypoint waypoint, final Geopoint coords) { - return invoke(activity, cache, waypoint, true); - } - - @Override public boolean isEnabled(cgCache cache) { - if (cache == null) { - return false; - } return !hasStaticMap(cache); } @Override public boolean isEnabled(cgWaypoint waypoint) { - if (waypoint == null) { - return false; - } return !hasStaticMap(waypoint); } + + @Override + public void navigate(Activity activity, cgCache cache) { + invokeStaticMaps(activity, cache, null, true); + } + + @Override + public void navigate(Activity activity, cgWaypoint waypoint) { + invokeStaticMaps(activity, null, waypoint, true); + } } diff --git a/main/src/cgeo/geocaching/apps/cache/navi/GeopointNavigationApp.java b/main/src/cgeo/geocaching/apps/cache/navi/GeopointNavigationApp.java new file mode 100644 index 0000000..fe4fd5d --- /dev/null +++ b/main/src/cgeo/geocaching/apps/cache/navi/GeopointNavigationApp.java @@ -0,0 +1,13 @@ +package cgeo.geocaching.apps.cache.navi; + +import cgeo.geocaching.geopoint.Geopoint; + +import android.app.Activity; + +/** + * interface for navigation to a coordinate. This one cannot be enabled/disabled. + * + */ +public interface GeopointNavigationApp { + void navigate(final Activity activity, final Geopoint coords); +} diff --git a/main/src/cgeo/geocaching/apps/cache/navi/GoogleMapsApp.java b/main/src/cgeo/geocaching/apps/cache/navi/GoogleMapsApp.java index 4e6e62b..eac33cc 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/GoogleMapsApp.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/GoogleMapsApp.java @@ -21,7 +21,7 @@ class GoogleMapsApp extends AbstractPointNavigationApp { } @Override - protected void navigate(Activity activity, Geopoint point) { + public void navigate(Activity activity, Geopoint point) { // INFO: q parameter works with Google Maps, but breaks cooperation with all other apps try { activity.startActivity(new Intent(Intent.ACTION_VIEW, diff --git a/main/src/cgeo/geocaching/apps/cache/navi/GoogleNavigationApp.java b/main/src/cgeo/geocaching/apps/cache/navi/GoogleNavigationApp.java index 31d99e9..7258e11 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/GoogleNavigationApp.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/GoogleNavigationApp.java @@ -3,8 +3,6 @@ package cgeo.geocaching.apps.cache.navi; import cgeo.geocaching.IGeoData; import cgeo.geocaching.R; import cgeo.geocaching.Settings; -import cgeo.geocaching.cgCache; -import cgeo.geocaching.cgWaypoint; import cgeo.geocaching.cgeoapplication; import cgeo.geocaching.activity.ActivityMixin; import cgeo.geocaching.geopoint.Geopoint; @@ -14,7 +12,7 @@ import android.app.Activity; import android.content.Intent; import android.net.Uri; -class GoogleNavigationApp extends AbstractNavigationApp { +class GoogleNavigationApp extends AbstractPointNavigationApp { GoogleNavigationApp() { super(getString(R.string.cache_menu_tbt), null); @@ -25,33 +23,8 @@ class GoogleNavigationApp extends AbstractNavigationApp { return true; } - @Override - public boolean invoke(final Activity activity, final cgCache cache, final cgWaypoint waypoint, final Geopoint coords) { - if (activity == null) { - return false; - } - + private static boolean navigateToCoordinates(Activity activity, final Geopoint coords) { IGeoData geo = cgeoapplication.getInstance().currentGeo(); - boolean navigationResult = false; - if (coords != null) { - navigationResult = navigateToCoordinates(geo, activity, coords); - } - else if (waypoint != null) { - navigationResult = navigateToCoordinates(geo, activity, waypoint.getCoords()); - } - else if (cache != null) { - navigationResult = navigateToCoordinates(geo, activity, cache.getCoords()); - } - - if (!navigationResult) { - ActivityMixin.showToast(activity, getString(R.string.err_navigation_no)); - return false; - } - - return true; - } - - private static boolean navigateToCoordinates(IGeoData geo, Activity activity, final Geopoint coords) { final Geopoint coordsNow = geo == null ? null : geo.getCoords(); // Google Navigation @@ -89,4 +62,10 @@ class GoogleNavigationApp extends AbstractNavigationApp { return false; } + @Override + public void navigate(Activity activity, Geopoint coords) { + if (!navigateToCoordinates(activity, coords)) { + ActivityMixin.showToast(activity, getString(R.string.err_navigation_no)); + } + } }
\ No newline at end of file diff --git a/main/src/cgeo/geocaching/apps/cache/navi/InternalMap.java b/main/src/cgeo/geocaching/apps/cache/navi/InternalMap.java index f1e03ed..8185f40 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/InternalMap.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/InternalMap.java @@ -3,37 +3,46 @@ package cgeo.geocaching.apps.cache.navi; import cgeo.geocaching.R; import cgeo.geocaching.cgCache; import cgeo.geocaching.cgWaypoint; +import cgeo.geocaching.apps.AbstractApp; import cgeo.geocaching.enumerations.WaypointType; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.maps.CGeoMap; import android.app.Activity; -class InternalMap extends AbstractNavigationApp { +class InternalMap extends AbstractApp implements CacheNavigationApp, WaypointNavigationApp, GeopointNavigationApp { InternalMap() { super(getString(R.string.cache_menu_map), null); } @Override - public boolean invoke(Activity activity, cgCache cache, cgWaypoint waypoint, final Geopoint coords) { - if (cache != null) { - CGeoMap.startActivityGeoCode(activity, cache.getGeocode()); - // may need some code from CGeoMap.startActivitySearch(activity, search, cache != null ? cache.getGeocode() : null, true); - } - else if (waypoint != null) { - CGeoMap.startActivityCoords(activity, waypoint.getCoords(), waypoint.getWaypointType(), waypoint.getName()); - } - else if (coords != null) { - CGeoMap.startActivityCoords(activity, coords, WaypointType.WAYPOINT, null); - } - + public boolean isInstalled() { return true; } @Override - public boolean isInstalled() { - return true; + public void navigate(Activity activity, Geopoint coords) { + CGeoMap.startActivityCoords(activity, coords, WaypointType.WAYPOINT, null); } + @Override + public void navigate(Activity activity, cgWaypoint waypoint) { + CGeoMap.startActivityCoords(activity, waypoint.getCoords(), waypoint.getWaypointType(), waypoint.getName()); + } + + @Override + public boolean isEnabled(cgWaypoint waypoint) { + return waypoint.getCoords() != null; + } + + @Override + public void navigate(Activity activity, cgCache cache) { + CGeoMap.startActivityGeoCode(activity, cache.getGeocode()); + } + + @Override + public boolean isEnabled(cgCache cache) { + return cache.getCoords() != null; + } } diff --git a/main/src/cgeo/geocaching/apps/cache/navi/LocusApp.java b/main/src/cgeo/geocaching/apps/cache/navi/LocusApp.java index d71a474..a20f2ce 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/LocusApp.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/LocusApp.java @@ -3,45 +3,30 @@ package cgeo.geocaching.apps.cache.navi; import cgeo.geocaching.cgCache; import cgeo.geocaching.cgWaypoint; import cgeo.geocaching.apps.AbstractLocusApp; -import cgeo.geocaching.geopoint.Geopoint; import android.app.Activity; -import java.util.ArrayList; +import java.util.Collections; -class LocusApp extends AbstractLocusApp implements NavigationApp { +class LocusApp extends AbstractLocusApp implements CacheNavigationApp, WaypointNavigationApp { + + @Override + public boolean isEnabled(cgWaypoint waypoint) { + return waypoint.getCoords() != null; + } /** * Show a single cache with waypoints or a single waypoint in Locus. * This method constructs a list of cache and waypoints only. * - * @see AbstractLocusApp#showInLocus */ @Override - public boolean invoke(Activity activity, cgCache cache, cgWaypoint waypoint, final Geopoint coords) { - - final ArrayList<Object> points = new ArrayList<Object>(); - - // add cache if present - if (cache != null && cache.getCoords() != null) { - points.add(cache); - } - - // add waypoint if present - if (waypoint != null && waypoint.getCoords() != null) { - points.add(waypoint); - } - - return showInLocus(points, true, false, activity); - } - - @Override - public boolean isEnabled(cgWaypoint waypoint) { - return waypoint != null; + public void navigate(Activity activity, cgWaypoint waypoint) { + showInLocus(Collections.singletonList(waypoint), true, false, activity); } @Override - public boolean isEnabled(Geopoint geopoint) { - return geopoint != null; + public void navigate(Activity activity, cgCache cache) { + showInLocus(Collections.singletonList(cache), true, false, activity); } } diff --git a/main/src/cgeo/geocaching/apps/cache/navi/NavigationAppFactory.java b/main/src/cgeo/geocaching/apps/cache/navi/NavigationAppFactory.java index 95f6408..8219320 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/NavigationAppFactory.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/NavigationAppFactory.java @@ -7,8 +7,8 @@ import cgeo.geocaching.cgWaypoint; import cgeo.geocaching.cgeoapplication; import cgeo.geocaching.activity.ActivityMixin; import cgeo.geocaching.apps.AbstractAppFactory; +import cgeo.geocaching.apps.App; import cgeo.geocaching.geopoint.Geopoint; -import cgeo.geocaching.utils.Log; import android.app.Activity; import android.app.AlertDialog; @@ -48,7 +48,7 @@ public final class NavigationAppFactory extends AbstractAppFactory { /** The external navigon app */ NAVIGON(new NavigonApp(), 10); - NavigationAppsEnum(NavigationApp app, int id) { + NavigationAppsEnum(App app, int id) { this.app = app; this.id = id; } @@ -56,7 +56,7 @@ public final class NavigationAppFactory extends AbstractAppFactory { /** * The app instance to use */ - public final NavigationApp app; + public final App app; /** * The id - used in c:geo settings */ @@ -119,7 +119,17 @@ public final class NavigationAppFactory extends AbstractAppFactory { for (NavigationAppsEnum navApp : getInstalledNavigationApps()) { if ((showInternalMap || !(navApp.app instanceof InternalMap)) && (showDefaultNavigation || defaultNavigationTool != navApp.id)) { - if (navApp.app.isEnabled(cache) || navApp.app.isEnabled(waypoint) || navApp.app.isEnabled(destination)) { + boolean add = false; + if (cache != null && navApp.app instanceof CacheNavigationApp && ((CacheNavigationApp) navApp.app).isEnabled(cache)) { + add = true; + } + if (waypoint != null && navApp.app instanceof WaypointNavigationApp && ((WaypointNavigationApp) navApp.app).isEnabled(waypoint)) { + add = true; + } + if (destination != null && navApp.app instanceof GeopointNavigationApp) { + add = true; + } + if (add) { items.add(navApp); } } @@ -134,7 +144,18 @@ public final class NavigationAppFactory extends AbstractAppFactory { @Override public void onClick(DialogInterface dialog, int item) { NavigationAppsEnum selectedItem = adapter.getItem(item); - selectedItem.app.invoke(activity, cache, waypoint, destination); + if (cache != null) { + CacheNavigationApp cacheApp = (CacheNavigationApp) selectedItem.app; + cacheApp.navigate(activity, cache); + } + else if (waypoint != null) { + WaypointNavigationApp waypointApp = (WaypointNavigationApp) selectedItem.app; + waypointApp.navigate(activity, waypoint); + } + else { + GeopointNavigationApp geopointApp = (GeopointNavigationApp) selectedItem.app; + geopointApp.navigate(activity, destination); + } } }); final AlertDialog alert = builder.create(); @@ -174,16 +195,22 @@ public final class NavigationAppFactory extends AbstractAppFactory { */ public static void addMenuItems(final Menu menu, final cgCache cache) { for (NavigationAppsEnum navApp : getInstalledNavigationApps()) { - if (navApp.app.isEnabled(cache)) { - menu.add(0, MENU_ITEM_OFFSET + navApp.id, 0, navApp.app.getName()); + if (navApp.app instanceof CacheNavigationApp) { + CacheNavigationApp cacheApp = (CacheNavigationApp) navApp.app; + if (cacheApp.isEnabled(cache)) { + menu.add(0, MENU_ITEM_OFFSET + navApp.id, 0, navApp.app.getName()); + } } } } public static void addMenuItems(final Menu menu, final cgWaypoint waypoint) { for (NavigationAppsEnum navApp : getInstalledNavigationApps()) { - if (navApp.app.isEnabled(waypoint)) { - menu.add(0, MENU_ITEM_OFFSET + navApp.id, 0, navApp.app.getName()); + if (navApp.app instanceof WaypointNavigationApp) { + WaypointNavigationApp waypointApp = (WaypointNavigationApp) navApp.app; + if (waypointApp.isEnabled(waypoint)) { + menu.add(0, MENU_ITEM_OFFSET + navApp.id, 0, navApp.app.getName()); + } } } } @@ -197,29 +224,37 @@ public final class NavigationAppFactory extends AbstractAppFactory { * @return */ public static boolean onMenuItemSelected(final MenuItem item, Activity activity, cgCache cache) { - return invokeApp(activity, cache, null, null, getAppFromMenuItem(item)); + navigateCache(activity, cache, getAppFromMenuItem(item)); + return true; + } + + private static void navigateCache(Activity activity, cgCache cache, App app) { + if (app instanceof CacheNavigationApp) { + CacheNavigationApp cacheApp = (CacheNavigationApp) app; + cacheApp.navigate(activity, cache); + } } public static boolean onMenuItemSelected(final MenuItem item, Activity activity, cgWaypoint waypoint) { - return invokeApp(activity, null, waypoint, null, getAppFromMenuItem(item)); + navigateWaypoint(activity, waypoint, getAppFromMenuItem(item)); + return true; } - private static boolean invokeApp(Activity activity, cgCache cache, cgWaypoint waypoint, final Geopoint destination, final NavigationApp app) { - if (app == null) { - return false; + private static void navigateWaypoint(Activity activity, cgWaypoint waypoint, App app) { + if (app instanceof WaypointNavigationApp) { + WaypointNavigationApp waypointApp = (WaypointNavigationApp) app; + waypointApp.navigate(activity, waypoint); } - if (cache == null && waypoint == null && destination == null) { - return false; - } - try { - return app.invoke(activity, cache, waypoint, destination); - } catch (Exception e) { - Log.e("NavigationAppFactory.onMenuItemSelected: " + e.toString()); + } + + private static void navigateGeopoint(Activity activity, Geopoint destination, App app) { + if (app instanceof GeopointNavigationApp) { + GeopointNavigationApp geopointApp = (GeopointNavigationApp) app; + geopointApp.navigate(activity, destination); } - return false; } - private static NavigationApp getAppFromMenuItem(MenuItem item) { + private static App getAppFromMenuItem(MenuItem item) { final int id = item.getItemId(); for (NavigationAppsEnum navApp : NavigationAppsEnum.values()) { if (MENU_ITEM_OFFSET + navApp.id == id) { @@ -243,10 +278,10 @@ public final class NavigationAppFactory extends AbstractAppFactory { return; } - invokeApp(activity, cache, null, null, getDefaultNavigationApplication(defaultNavigation)); + navigateCache(activity, cache, getDefaultNavigationApplication(defaultNavigation)); } - private static NavigationApp getDefaultNavigationApplication(int defaultNavigation) { + private static App getDefaultNavigationApplication(int defaultNavigation) { if (defaultNavigation == 2) { return getNavigationAppFromSetting(Settings.getDefaultNavigationTool2()); } @@ -264,7 +299,7 @@ public final class NavigationAppFactory extends AbstractAppFactory { ActivityMixin.showToast(activity, cgeoapplication.getInstance().getString(R.string.err_location_unknown)); return; } - invokeApp(activity, null, waypoint, null, getDefaultNavigationApplication(defaultNavigation)); + navigateWaypoint(activity, waypoint, getDefaultNavigationApplication(defaultNavigation)); } /** @@ -279,7 +314,7 @@ public final class NavigationAppFactory extends AbstractAppFactory { return; } - invokeApp(activity, null, null, destination, getDefaultNavigationApplication(defaultNavigation)); + navigateGeopoint(activity, destination, getDefaultNavigationApplication(defaultNavigation)); } /** @@ -287,11 +322,11 @@ public final class NavigationAppFactory extends AbstractAppFactory { * * @return never <code>null</code> */ - public static NavigationApp getDefaultNavigationApplication() { + public static App getDefaultNavigationApplication() { return getNavigationAppFromSetting(Settings.getDefaultNavigationTool()); } - private static NavigationApp getNavigationAppFromSetting(final int defaultNavigationTool) { + private static App getNavigationAppFromSetting(final int defaultNavigationTool) { final List<NavigationAppsEnum> installedNavigationApps = getInstalledNavigationApps(); for (NavigationAppsEnum navigationApp : installedNavigationApps) { diff --git a/main/src/cgeo/geocaching/apps/cache/navi/NavigonApp.java b/main/src/cgeo/geocaching/apps/cache/navi/NavigonApp.java index 69f63dc..7ea86fb 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/NavigonApp.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/NavigonApp.java @@ -16,7 +16,7 @@ class NavigonApp extends AbstractPointNavigationApp { } @Override - protected void navigate(Activity activity, Geopoint point) { + public void navigate(Activity activity, Geopoint point) { final Intent intent = new Intent(INTENT); /* diff --git a/main/src/cgeo/geocaching/apps/cache/navi/OruxMapsApp.java b/main/src/cgeo/geocaching/apps/cache/navi/OruxMapsApp.java index 27ec2fa..24ef81b 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/OruxMapsApp.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/OruxMapsApp.java @@ -15,7 +15,7 @@ class OruxMapsApp extends AbstractPointNavigationApp { } @Override - protected void navigate(Activity activity, Geopoint point) { + public void navigate(Activity activity, Geopoint point) { final Intent intent = new Intent(INTENT); intent.putExtra("latitude", point.getLatitude());//latitude, wgs84 datum intent.putExtra("longitude", point.getLongitude());//longitude, wgs84 datum diff --git a/main/src/cgeo/geocaching/apps/cache/navi/RMapsApp.java b/main/src/cgeo/geocaching/apps/cache/navi/RMapsApp.java index 8044cc6..a481813 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/RMapsApp.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/RMapsApp.java @@ -3,6 +3,7 @@ package cgeo.geocaching.apps.cache.navi; import cgeo.geocaching.R; import cgeo.geocaching.cgCache; import cgeo.geocaching.cgWaypoint; +import cgeo.geocaching.apps.AbstractApp; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.GeopointFormatter.Format; @@ -11,7 +12,7 @@ import android.content.Intent; import java.util.ArrayList; -class RMapsApp extends AbstractNavigationApp { +class RMapsApp extends AbstractApp implements CacheNavigationApp, WaypointNavigationApp, GeopointNavigationApp { private static final String INTENT = "com.robert.maps.action.SHOW_POINTS"; @@ -20,29 +21,30 @@ class RMapsApp extends AbstractNavigationApp { } @Override - public boolean invoke(Activity activity, cgCache cache, cgWaypoint waypoint, final Geopoint coords) { - try { - final ArrayList<String> locations = new ArrayList<String>(); - if (cache != null && cache.getCoords() != null) { - locations.add(cache.getCoords().format(Format.LAT_LON_DECDEGREE_COMMA) - + ";" + cache.getGeocode() - + ";" + cache.getName()); - } else if (waypoint != null && waypoint.getCoords() != null) { - locations.add(waypoint.getCoords().format(Format.LAT_LON_DECDEGREE_COMMA) - + ";" + waypoint.getLookup() - + ";" + waypoint.getName()); - } - - if (!locations.isEmpty()) { - final Intent intent = new Intent(INTENT); - intent.putStringArrayListExtra("locations", locations); - activity.startActivity(intent); - return true; - } - } catch (Exception e) { - // nothing - } - - return false; + public void navigate(Activity activity, cgWaypoint waypoint) { + navigate(activity, waypoint.getCoords(), waypoint.getLookup(), waypoint.getName()); + } + + private static void navigate(Activity activity, Geopoint coords, String code, String name) { + final ArrayList<String> locations = new ArrayList<String>(); + locations.add(coords.format(Format.LAT_LON_DECDEGREE_COMMA) + ";" + code + ";" + name); + final Intent intent = new Intent(INTENT); + intent.putStringArrayListExtra("locations", locations); + activity.startActivity(intent); + } + + @Override + public boolean isEnabled(cgWaypoint waypoint) { + return waypoint.getCoords() != null; + } + + @Override + public void navigate(Activity activity, cgCache cache) { + navigate(activity, cache.getCoords(), cache.getGeocode(), cache.getName()); + } + + @Override + public void navigate(Activity activity, Geopoint coords) { + navigate(activity, coords, "", ""); } } diff --git a/main/src/cgeo/geocaching/apps/cache/navi/RadarApp.java b/main/src/cgeo/geocaching/apps/cache/navi/RadarApp.java index 3374691..b01539c 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/RadarApp.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/RadarApp.java @@ -16,7 +16,7 @@ class RadarApp extends AbstractPointNavigationApp { } @Override - protected void navigate(Activity activity, Geopoint point) { + public void navigate(Activity activity, Geopoint point) { final Intent radarIntent = new Intent(INTENT); radarIntent.putExtra("latitude", (float) point.getLatitude()); radarIntent.putExtra("longitude", (float) point.getLongitude()); diff --git a/main/src/cgeo/geocaching/apps/cache/navi/StaticMapApp.java b/main/src/cgeo/geocaching/apps/cache/navi/StaticMapApp.java index d805656..6be027f 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/StaticMapApp.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/StaticMapApp.java @@ -3,7 +3,6 @@ package cgeo.geocaching.apps.cache.navi; import cgeo.geocaching.R; import cgeo.geocaching.cgCache; import cgeo.geocaching.cgWaypoint; -import cgeo.geocaching.geopoint.Geopoint; import android.app.Activity; @@ -14,23 +13,22 @@ class StaticMapApp extends AbstractStaticMapsApp { } @Override - public boolean invoke(Activity activity, cgCache cache, cgWaypoint waypoint, final Geopoint coords) { - return invoke(activity, cache, waypoint, false); - } - - @Override public boolean isEnabled(cgCache cache) { - if (cache == null) { - return false; - } return hasStaticMap(cache); } @Override public boolean isEnabled(cgWaypoint waypoint) { - if (waypoint == null) { - return false; - } return hasStaticMap(waypoint); } + + @Override + public void navigate(Activity activity, cgCache cache) { + invokeStaticMaps(activity, cache, null, false); + } + + @Override + public void navigate(Activity activity, cgWaypoint waypoint) { + invokeStaticMaps(activity, null, waypoint, false); + } } diff --git a/main/src/cgeo/geocaching/apps/cache/navi/StreetviewApp.java b/main/src/cgeo/geocaching/apps/cache/navi/StreetviewApp.java index 41f958d..012b94f 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/StreetviewApp.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/StreetviewApp.java @@ -22,7 +22,7 @@ class StreetviewApp extends AbstractPointNavigationApp { } @Override - protected void navigate(Activity activity, Geopoint point) { + public void navigate(Activity activity, Geopoint point) { try { activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("google.streetview:cbll=" + point.getLatitude() + "," + point.getLongitude()))); diff --git a/main/src/cgeo/geocaching/apps/cache/navi/WaypointNavigationApp.java b/main/src/cgeo/geocaching/apps/cache/navi/WaypointNavigationApp.java new file mode 100644 index 0000000..7d3a706 --- /dev/null +++ b/main/src/cgeo/geocaching/apps/cache/navi/WaypointNavigationApp.java @@ -0,0 +1,15 @@ +package cgeo.geocaching.apps.cache.navi; + +import cgeo.geocaching.cgWaypoint; + +import android.app.Activity; + +/** + * interface for navigation to a waypoint + * + */ +public interface WaypointNavigationApp { + void navigate(final Activity activity, final cgWaypoint waypoint); + + boolean isEnabled(final cgWaypoint waypoint); +} |