diff options
author | Bananeweizen <Bananeweizen@gmx.de> | 2012-01-24 11:59:31 -0800 |
---|---|---|
committer | Bananeweizen <Bananeweizen@gmx.de> | 2012-01-24 11:59:31 -0800 |
commit | a5c6a6dd79ed24080026568b469a68867f333284 (patch) | |
tree | 61bb94609250e7974b97184af49340f35d426f48 /main/src | |
parent | ca3cdc6d21ec7bf07b715074969110b7e9d544b6 (diff) | |
parent | cd561bd585be3d2951a57d72fa68cff84a1bb2e9 (diff) | |
download | cgeo-a5c6a6dd79ed24080026568b469a68867f333284.zip cgeo-a5c6a6dd79ed24080026568b469a68867f333284.tar.gz cgeo-a5c6a6dd79ed24080026568b469a68867f333284.tar.bz2 |
Merge pull request #1027 from RoadRunner-/navid
Implementation for issue #1012: ID may change
Diffstat (limited to 'main/src')
-rw-r--r-- | main/src/cgeo/geocaching/apps/cache/navi/NavigationAppFactory.java | 104 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgeoinit.java | 42 |
2 files changed, 84 insertions, 62 deletions
diff --git a/main/src/cgeo/geocaching/apps/cache/navi/NavigationAppFactory.java b/main/src/cgeo/geocaching/apps/cache/navi/NavigationAppFactory.java index 529bc47..ac1cd8a 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/NavigationAppFactory.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/NavigationAppFactory.java @@ -9,8 +9,6 @@ import cgeo.geocaching.cgWaypoint; import cgeo.geocaching.apps.AbstractAppFactory; import cgeo.geocaching.geopoint.Geopoint; -import org.apache.commons.lang3.ArrayUtils; - import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; @@ -22,24 +20,33 @@ import java.util.ArrayList; import java.util.List; public final class NavigationAppFactory extends AbstractAppFactory { - private static NavigationApp[] apps = new NavigationApp[] {}; - - private static NavigationApp[] getNavigationApps() { - if (ArrayUtils.isEmpty(apps)) { - apps = new NavigationApp[] { - new CompassApp(), // MUST always be the first entry in this array - new RadarApp(), - new InternalMap(), - new StaticMapApp(), - new LocusApp(), - new RMapsApp(), - new GoogleMapsApp(), - new GoogleNavigationApp(), - new StreetviewApp(), - new OruxMapsApp(), - new NavigonApp() }; + + public enum NavigationAppsEnum { + COMPASS(new CompassApp(), 0), + RADAR(new RadarApp(), 1), + INTERNAL_MAP(new InternalMap(), 2), + STATIC_MAP(new StaticMapApp(), 3), + LOCUS(new LocusApp(), 4), + RMAPS(new RMapsApp(), 5), + GOOGLE_MAPS(new GoogleMapsApp(), 6), + GOOGLE_NAVIGATION(new GoogleNavigationApp(), 7), + GOOGLE_STREETVIEW(new StreetviewApp(), 8), + ORUX_MAPS(new OruxMapsApp(), 9), + NAVIGON(new NavigonApp(), 10); + + NavigationAppsEnum(NavigationApp app, int id) { + this.app = app; + this.id = id; } - return apps; + + /** + * The app instance to use + */ + public final NavigationApp app; + /** + * The id - used in c:geo settings + */ + public final int id; } public static void addMenuItems(final Menu menu, final Activity activity) { @@ -49,10 +56,10 @@ public final class NavigationAppFactory extends AbstractAppFactory { public static void addMenuItems(final Menu menu, final Activity activity, final boolean showInternalMap, final boolean showDefaultNavigation) { final int defaultNavigationTool = Settings.getDefaultNavigationTool(); - for (NavigationApp app : getInstalledNavigationApps(activity)) { - if ((showInternalMap || !(app instanceof InternalMap)) && - (showDefaultNavigation || defaultNavigationTool != app.getId())) { - menu.add(0, app.getId(), 0, app.getName()); + for (NavigationAppsEnum navApp : getInstalledNavigationApps(activity)) { + if ((showInternalMap || !(navApp.app instanceof InternalMap)) && + (showDefaultNavigation || defaultNavigationTool != navApp.id)) { + menu.add(0, navApp.id, 0, navApp.app.getName()); } } } @@ -61,14 +68,14 @@ public final class NavigationAppFactory extends AbstractAppFactory { AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setTitle(R.string.cache_menu_navigate); builder.setIcon(android.R.drawable.ic_menu_mapmode); - final List<NavigationApp> installed = getInstalledNavigationApps(activity); + final List<NavigationAppsEnum> installed = getInstalledNavigationApps(activity); String[] items = new String[installed.size()]; for (int i = 0; i < installed.size(); i++) { - items[i] = installed.get(i).getName(); + items[i] = installed.get(i).app.getName(); } builder.setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { - installed.get(item).invoke(geo, activity, cache, search, null, null); + installed.get(item).app.invoke(geo, activity, cache, search, null, null); } }); AlertDialog alert = builder.create(); @@ -76,27 +83,16 @@ public final class NavigationAppFactory extends AbstractAppFactory { } - public static List<NavigationApp> getInstalledNavigationApps(final Activity activity) { - final List<NavigationApp> installedNavigationApps = new ArrayList<NavigationApp>(); - for (NavigationApp app : getNavigationApps()) { - if (app.isInstalled(activity)) { - installedNavigationApps.add(app); + public static List<NavigationAppsEnum> getInstalledNavigationApps(final Activity activity) { + final List<NavigationAppsEnum> installedNavigationApps = new ArrayList<NavigationAppsEnum>(); + for (NavigationAppsEnum appEnum : NavigationAppsEnum.values()) { + if (appEnum.app.isInstalled(activity)) { + installedNavigationApps.add(appEnum); } } return installedNavigationApps; } - public static int getOrdinalFromId(final Activity activity, final int id) { - int ordinal = 0; - for (NavigationApp app : getInstalledNavigationApps(activity)) { - if (app.getId() == id) { - return ordinal; - } - ordinal++; - } - return 0; - } - public static boolean onMenuItemSelected(final MenuItem item, final cgGeo geo, Activity activity, cgCache cache, final SearchResult search, cgWaypoint waypoint, final Geopoint destination) { @@ -104,7 +100,7 @@ public final class NavigationAppFactory extends AbstractAppFactory { return false; } - final NavigationApp app = (NavigationApp) getAppFromMenuItem(item, apps); + final NavigationApp app = getAppFromMenuItem(item); if (app != null) { try { return app.invoke(geo, activity, cache, @@ -116,6 +112,16 @@ public final class NavigationAppFactory extends AbstractAppFactory { return false; } + public static NavigationApp getAppFromMenuItem(MenuItem item) { + final int id = item.getItemId(); + for (NavigationAppsEnum navApp : NavigationAppsEnum.values()) { + if (navApp.id == id) { + return navApp.app; + } + } + return null; + } + public static void startDefaultNavigationApplication(final cgGeo geo, Activity activity, cgCache cache, final SearchResult search, cgWaypoint waypoint, final Geopoint destination) { NavigationApp app = getDefaultNavigationApplication(activity); @@ -131,7 +137,7 @@ public final class NavigationAppFactory extends AbstractAppFactory { /** * Returns the default navigation tool if correctly set and installed or the compass app as default fallback - * + * * @param activity * @return never <code>null</code> */ @@ -139,18 +145,18 @@ public final class NavigationAppFactory extends AbstractAppFactory { final int defaultNavigationTool = Settings.getDefaultNavigationTool(); NavigationApp app = null; - final List<NavigationApp> installedNavigationApps = getInstalledNavigationApps(activity); + final List<NavigationAppsEnum> installedNavigationApps = getInstalledNavigationApps(activity); - for (NavigationApp navigationApp : installedNavigationApps) { - if (navigationApp.getId() == defaultNavigationTool) { - app = navigationApp; + for (NavigationAppsEnum navigationApp : installedNavigationApps) { + if (navigationApp.id == defaultNavigationTool) { + app = navigationApp.app; break; } } // default navigation tool wasn't set already or couldn't be found (not installed any more for example) if (app == null) { // assume that 0 is the compass-app - app = installedNavigationApps.get(0); + app = installedNavigationApps.get(0).app; } return app; } diff --git a/main/src/cgeo/geocaching/cgeoinit.java b/main/src/cgeo/geocaching/cgeoinit.java index be6782f..58db9bd 100644 --- a/main/src/cgeo/geocaching/cgeoinit.java +++ b/main/src/cgeo/geocaching/cgeoinit.java @@ -1,8 +1,8 @@ package cgeo.geocaching; import cgeo.geocaching.activity.AbstractActivity; -import cgeo.geocaching.apps.cache.navi.NavigationApp; import cgeo.geocaching.apps.cache.navi.NavigationAppFactory; +import cgeo.geocaching.apps.cache.navi.NavigationAppFactory.NavigationAppsEnum; import cgeo.geocaching.compatibility.Compatibility; import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.enumerations.StatusCode; @@ -30,6 +30,7 @@ import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; @@ -40,7 +41,6 @@ import android.widget.Spinner; import android.widget.TextView; import java.io.File; -import java.util.ArrayList; import java.util.List; import java.util.SortedMap; import java.util.concurrent.atomic.AtomicReference; @@ -587,23 +587,39 @@ public class cgeoinit extends AbstractActivity { }); // Default navigation tool settings - final List<NavigationApp> apps = NavigationAppFactory.getInstalledNavigationApps(this); - final List<String> appNames = new ArrayList<String>(); - for (NavigationApp navigationApp : apps) { - appNames.add(navigationApp.getName()); - } Spinner defaultNavigationToolSelector = (Spinner) findViewById(R.id.default_navigation_tool); - ArrayAdapter<String> naviAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, appNames.toArray(new String[appNames.size()])); + final List<NavigationAppsEnum> apps = NavigationAppFactory.getInstalledNavigationApps(this); + ArrayAdapter<NavigationAppsEnum> naviAdapter = new ArrayAdapter<NavigationAppsEnum>(this, android.R.layout.simple_spinner_item, apps.toArray(new NavigationAppsEnum[apps.size()])) { + @Override + public View getView(int position, View convertView, ViewGroup parent) { + TextView textView = (TextView) super.getView(position, convertView, parent); + textView.setText(getItem(position).app.getName()); + return textView; + } + @Override + public View getDropDownView(int position, View convertView, ViewGroup parent) { + TextView textView = (TextView) super.getDropDownView(position, convertView, parent); + textView.setText(getItem(position).app.getName()); + return textView; + } + }; naviAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); defaultNavigationToolSelector.setAdapter(naviAdapter); int defaultNavigationTool = Settings.getDefaultNavigationTool(); - defaultNavigationToolSelector.setSelection(NavigationAppFactory.getOrdinalFromId(this, defaultNavigationTool)); + int ordinal = 0; + for (int i = 0; i < apps.size(); i++) { + if (apps.get(i).id == defaultNavigationTool) { + ordinal = i; + break; + } + } + defaultNavigationToolSelector.setSelection(ordinal); defaultNavigationToolSelector.setOnItemSelectedListener(new OnItemSelectedListener() { @Override - public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { - NavigationApp selectedApp = apps.get(arg2); - if (selectedApp != null) { - Settings.setDefaultNavigationTool(selectedApp.getId()); + public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { + NavigationAppsEnum item = (NavigationAppsEnum) parent.getItemAtPosition(position); + if (item != null) { + Settings.setDefaultNavigationTool(item.id); } } |