aboutsummaryrefslogtreecommitdiffstats
path: root/src/cgeo/geocaching/apps/cache/navi/NavigationAppFactory.java
blob: b8872e33b06d4581e2f55aefea6a969298b587c9 (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
66
67
68
69
package cgeo.geocaching.apps.cache.navi;

import java.util.List;
import java.util.UUID;

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 cgeo.geocaching.cgCache;
import cgeo.geocaching.cgGeo;
import cgeo.geocaching.cgSettings;
import cgeo.geocaching.cgWaypoint;
import cgeo.geocaching.apps.AbstractAppFactory;

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[] {
					// compass
					new RadarApp(res), 
					new InternalMap(res), 
					new StaticMapApp(res),
					new LocusApp(res),
					new RMapsApp(res),
					new GoogleMapsApp(res),
					new GoogleNavigationApp(res),
					new StreetviewApp(res)};
		}
		return apps;
	}

	public static void addMenuItems(Menu menu, Activity activity,
			Resources res) {
		for (NavigationApp app : getNavigationApps(res)) {
			if (app.isInstalled(activity)) {
				menu.add(0, app.getId(), 0, app.getName());
			}
		}
	}

	public static boolean onMenuItemSelected(final MenuItem item,
			final cgGeo geo, Activity activity, Resources res,
			cgCache cache,
			final UUID searchId, cgWaypoint waypoint, List<Double> destination) {
		NavigationApp app = (NavigationApp) getAppFromMenuItem(item, apps);
		if (app != null) {
			Double latitude = null;
			Double longitude = null;
			if (destination != null && destination.size() >= 2) {
				latitude = destination.get(0);
				longitude = destination.get(1);
			}
			try {
				return app.invoke(geo, activity, res, cache,
						searchId, waypoint, latitude, longitude);
			} catch (Exception e) {
				Log.e(cgSettings.tag, "NavigationAppFactory.onMenuItemSelected: " + e.toString());
			}
		}
		return false;
	}

}