blob: a640847be9964f74e11335f9aa896e53bce2234d (
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
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
|
package cgeo.geocaching.maps;
import cgeo.geocaching.CgeoApplication;
import cgeo.geocaching.R;
import cgeo.geocaching.maps.google.v1.GoogleMapProvider;
import cgeo.geocaching.maps.interfaces.MapProvider;
import cgeo.geocaching.maps.interfaces.MapSource;
import cgeo.geocaching.maps.mapsforge.MapsforgeMapProvider;
import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.utils.Log;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import android.view.Menu;
import android.view.SubMenu;
import java.util.ArrayList;
import java.util.List;
public class MapProviderFactory {
private final static ArrayList<MapSource> mapSources = new ArrayList<>();
static {
// add GoogleMapProvider only if google api is available in order to support x86 android emulator
if (isGoogleMapsInstalled()) {
GoogleMapProvider.getInstance();
}
MapsforgeMapProvider.getInstance();
}
public static boolean isGoogleMapsInstalled() {
// Check if API key is available
final String mapsKey = CgeoApplication.getInstance().getString(R.string.maps_api_key);
if (StringUtils.length(mapsKey) < 30 || StringUtils.contains(mapsKey, "key")) {
Log.w("No Google API key available.");
return false;
}
// Check if API is available
try {
Class.forName("com.google.android.maps.MapActivity");
} catch (final ClassNotFoundException ignored) {
return false;
}
// Assume that Google Maps is available and working
return true;
}
public static List<MapSource> getMapSources() {
return mapSources;
}
public static boolean isSameActivity(@NonNull final MapSource source1, @NonNull final MapSource source2) {
final MapProvider provider1 = source1.getMapProvider();
final MapProvider provider2 = source2.getMapProvider();
return provider1 == provider2 && provider1.isSameActivity(source1, source2);
}
public static void addMapviewMenuItems(final Menu menu) {
final SubMenu parentMenu = menu.findItem(R.id.menu_select_mapview).getSubMenu();
final int currentSource = Settings.getMapSource().getNumericalId();
for (int i = 0; i < mapSources.size(); i++) {
final MapSource mapSource = mapSources.get(i);
final int id = mapSource.getNumericalId();
parentMenu.add(R.id.menu_group_map_sources, id, i, mapSource.getName()).setCheckable(true).setChecked(id == currentSource);
}
parentMenu.setGroupCheckable(R.id.menu_group_map_sources, true, true);
}
/**
* Return a map source by id.
*
* @param id the map source id
* @return the map source, or <tt>null</tt> if <tt>id</tt> does not correspond to a registered map source
*/
@Nullable
public static MapSource getMapSource(final int id) {
for (final MapSource mapSource : mapSources) {
if (mapSource.getNumericalId() == id) {
return mapSource;
}
}
return null;
}
/**
* Return a map source if there is at least one.
*
* @return the first map source in the collection, or <tt>null</tt> if there are none registered
*/
public static MapSource getAnyMapSource() {
return mapSources.isEmpty() ? null : mapSources.get(0);
}
public static void registerMapSource(final MapSource mapSource) {
mapSources.add(mapSource);
}
public static MapSource getDefaultSource() {
return mapSources.get(0);
}
/**
* remove offline map sources after changes of the settings
*/
public static void deleteOfflineMapSources() {
final ArrayList<MapSource> deletion = new ArrayList<>();
for (final MapSource mapSource : mapSources) {
if (mapSource instanceof MapsforgeMapProvider.OfflineMapSource) {
deletion.add(mapSource);
}
}
mapSources.removeAll(deletion);
}
}
|