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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
package cgeo.geocaching.apps.cache.navi;
import cgeo.geocaching.SearchResult;
import cgeo.geocaching.Settings;
import cgeo.geocaching.cgCache;
import cgeo.geocaching.cgGeo;
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.content.res.Resources;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
public final class NavigationAppFactory extends AbstractAppFactory {
private static NavigationApp[] apps = new NavigationApp[] {};
private static NavigationApp[] getNavigationApps(Resources res) {
if (ArrayUtils.isEmpty(apps)) {
apps = new NavigationApp[] {
new CompassApp(res),
new RadarApp(res),
new InternalMap(res),
new StaticMapApp(res),
new LocusApp(res),
new RMapsApp(res),
new GoogleMapsApp(res),
new GoogleNavigationApp(res),
new StreetviewApp(res),
new OruxMapsApp(res) };
}
return apps;
}
public static void addMenuItems(final Menu menu, final Activity activity,
final Resources res) {
addMenuItems(menu, activity, res, true, false);
}
public static void addMenuItems(final Menu menu, final Activity activity,
final Resources res, final boolean showInternalMap, final boolean showDefaultNavigation) {
int defaultNavigationTool = Settings.getDefaultNavigationTool();
for (NavigationApp app : getInstalledNavigationApps(activity, res)) {
if ((showInternalMap || !(app instanceof InternalMap)) &&
(showDefaultNavigation || defaultNavigationTool != app.getId())) {
menu.add(0, app.getId(), 0, app.getName());
}
}
}
public static List<NavigationApp> getInstalledNavigationApps(final Activity activity, final Resources res) {
List<NavigationApp> installedNavigationApps = new ArrayList<NavigationApp>();
for (NavigationApp app : getNavigationApps(res)) {
if (app.isInstalled(activity)) {
installedNavigationApps.add(app);
}
}
return installedNavigationApps;
}
public static int getOrdinalFromId(final Activity activity, final Resources res, final int id) {
int ordinal = 0;
for (NavigationApp app : getInstalledNavigationApps(activity, res)) {
if (app.getId() == id) {
return ordinal;
}
ordinal++;
}
return 0;
}
public static boolean onMenuItemSelected(final MenuItem item,
final cgGeo geo, Activity activity, Resources res,
cgCache cache,
final SearchResult search, cgWaypoint waypoint, final Geopoint destination) {
NavigationApp app = (NavigationApp) getAppFromMenuItem(item, apps);
if (app != null) {
try {
return app.invoke(geo, activity, res, cache,
search, waypoint, destination);
} catch (Exception e) {
Log.e(Settings.tag, "NavigationAppFactory.onMenuItemSelected: " + e.toString());
}
}
return false;
}
public static void startDefaultNavigationApplication(final cgGeo geo, Activity activity, Resources res, cgCache cache,
final SearchResult search, cgWaypoint waypoint, final Geopoint destination) {
final int defaultNavigationTool = Settings.getDefaultNavigationTool();
NavigationApp app = null;
List<NavigationApp> installedNavigationApps = getInstalledNavigationApps(activity, res);
if (defaultNavigationTool == 0) {
// assume that 0 is the compass-app
app = installedNavigationApps.get(0);
} else {
for (NavigationApp navigationApp : installedNavigationApps) {
if (navigationApp.getId() == defaultNavigationTool) {
app = navigationApp;
break;
}
}
}
if (app != null) {
try {
app.invoke(geo, activity, res, cache, search, waypoint, destination);
} catch (Exception e) {
Log.e(Settings.tag, "NavigationAppFactory.startDefaultNavigationApplication: " + e.toString());
}
}
}
}
|