diff options
author | Bananeweizen <Bananeweizen@gmx.de> | 2011-11-15 21:51:38 -0800 |
---|---|---|
committer | Bananeweizen <Bananeweizen@gmx.de> | 2011-11-15 21:51:38 -0800 |
commit | 12fccdf3f0d0dff7410102e23eb5863aedae7911 (patch) | |
tree | 4fcd9eb92dfeaebb8a24b334a4c87833a96b65c0 /main/src/cgeo | |
parent | e23e570af398c33eb0f278deee164536026daa74 (diff) | |
parent | 082cde2c609dedaef1b970f3e2af71395ef2c543 (diff) | |
download | cgeo-12fccdf3f0d0dff7410102e23eb5863aedae7911.zip cgeo-12fccdf3f0d0dff7410102e23eb5863aedae7911.tar.gz cgeo-12fccdf3f0d0dff7410102e23eb5863aedae7911.tar.bz2 |
Merge pull request #796 from rsudev/refactor_mapsources
Implementing #179, refactor maps to single source, last part
Diffstat (limited to 'main/src/cgeo')
15 files changed, 384 insertions, 242 deletions
diff --git a/main/src/cgeo/geocaching/Settings.java b/main/src/cgeo/geocaching/Settings.java index 2007493..822bd04 100644 --- a/main/src/cgeo/geocaching/Settings.java +++ b/main/src/cgeo/geocaching/Settings.java @@ -2,9 +2,8 @@ package cgeo.geocaching; import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.geopoint.Geopoint; -import cgeo.geocaching.maps.google.GoogleMapFactory; -import cgeo.geocaching.maps.interfaces.MapFactory; -import cgeo.geocaching.maps.mapsforge.MapsforgeMapFactory; +import cgeo.geocaching.maps.MapProviderFactory; +import cgeo.geocaching.maps.interfaces.MapProvider; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; @@ -81,32 +80,6 @@ public final class Settings { void edit(final Editor edit); } - public enum mapSourceEnum { - googleMap, - googleSat, - mapsforgeMapnik, - mapsforgeOsmarender, - mapsforgeCycle, - mapsforgeOffline; - - static mapSourceEnum fromInt(int id) { - final mapSourceEnum[] values = mapSourceEnum.values(); - if (id >= 0 && id < values.length) { - return values[id]; - } else { - return googleMap; - } - } - - public boolean isGoogleMapSource() { - if (googleMap == this || googleSat == this) { - return true; - } - - return false; - } - } - public enum coordInputFormatEnum { Plain, Deg, @@ -134,7 +107,7 @@ public final class Settings { private static String password = null; // maps - private static MapFactory mapFactory = null; + private static MapProvider mapProvider = null; private Settings() { // this class is not to be instantiated; @@ -328,16 +301,11 @@ public final class Settings { }); } - public static MapFactory getMapFactory() { - if (mapFactory == null) { - if (getMapSource().isGoogleMapSource()) { - mapFactory = new GoogleMapFactory(); - } - else { - mapFactory = new MapsforgeMapFactory(); - } + public static MapProvider getMapProvider() { + if (mapProvider == null) { + mapProvider = MapProviderFactory.getMapProvider(getMapSource()); } - return mapFactory; + return mapProvider; } public static String getMapFile() { @@ -705,19 +673,19 @@ public final class Settings { }); } - public static mapSourceEnum getMapSource() { - return mapSourceEnum.fromInt(sharedPrefs.getInt(KEY_MAP_SOURCE, 0)); + public static int getMapSource() { + return sharedPrefs.getInt(KEY_MAP_SOURCE, 0); } - public static void setMapSource(final mapSourceEnum newMapSource) { - if (getMapSource().isGoogleMapSource() != newMapSource.isGoogleMapSource()) { - mapFactory = null; + public static void setMapSource(final int newMapSource) { + if (!MapProviderFactory.IsSameProvider(getMapSource(), newMapSource)) { + mapProvider = null; } editSharedSettings(new PrefRunnable() { @Override public void edit(Editor edit) { - edit.putInt(KEY_MAP_SOURCE, newMapSource.ordinal()); + edit.putInt(KEY_MAP_SOURCE, newMapSource); } }); } diff --git a/main/src/cgeo/geocaching/cgeoinit.java b/main/src/cgeo/geocaching/cgeoinit.java index 13a358c..9248876 100644 --- a/main/src/cgeo/geocaching/cgeoinit.java +++ b/main/src/cgeo/geocaching/cgeoinit.java @@ -1,10 +1,10 @@ package cgeo.geocaching; import cgeo.geocaching.LogTemplateProvider.LogTemplate; -import cgeo.geocaching.Settings.mapSourceEnum; import cgeo.geocaching.activity.AbstractActivity; import cgeo.geocaching.compatibility.Compatibility; import cgeo.geocaching.enumerations.StatusCode; +import cgeo.geocaching.maps.MapProviderFactory; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpResponse; @@ -33,6 +33,7 @@ import android.widget.Spinner; import android.widget.TextView; import java.io.File; +import java.util.SortedMap; import java.util.concurrent.atomic.AtomicReference; public class cgeoinit extends AbstractActivity { @@ -518,13 +519,13 @@ public class cgeoinit extends AbstractActivity { webAuth.setOnClickListener(new webAuth()); // Map source settings + SortedMap<Integer, String> mapSources = MapProviderFactory.getMapSources(); Spinner mapSourceSelector = (Spinner) findViewById(R.id.mapsource); - ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( - this, R.array.map_sources, android.R.layout.simple_spinner_item); + ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, mapSources.values().toArray(new String[] {})); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mapSourceSelector.setAdapter(adapter); int mapsource = prefs.getInt("mapsource", 0); - mapSourceSelector.setSelection(mapsource); + mapSourceSelector.setSelection(MapProviderFactory.getSourceOrdinalFromId(mapsource)); mapSourceSelector.setOnItemSelectedListener(new cgeoChangeMapSource()); initMapfileEdittext(false); @@ -657,12 +658,12 @@ public class cgeoinit extends AbstractActivity { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { - Settings.setMapSource(mapSourceEnum.fromInt(arg2)); + Settings.setMapSource(MapProviderFactory.getSourceIdFromOrdinal(arg2)); } @Override public void onNothingSelected(AdapterView<?> arg0) { - arg0.setSelection(Settings.getMapSource().ordinal()); + arg0.setSelection(MapProviderFactory.getSourceIdFromOrdinal(Settings.getMapSource())); } } diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java index f59ddf0..bbbb913 100644 --- a/main/src/cgeo/geocaching/maps/CGeoMap.java +++ b/main/src/cgeo/geocaching/maps/CGeoMap.java @@ -2,7 +2,6 @@ package cgeo.geocaching.maps; import cgeo.geocaching.R; import cgeo.geocaching.Settings; -import cgeo.geocaching.Settings.mapSourceEnum; import cgeo.geocaching.cgBase; import cgeo.geocaching.cgCache; import cgeo.geocaching.cgCoord; @@ -26,7 +25,7 @@ import cgeo.geocaching.maps.interfaces.CachesOverlayItemImpl; import cgeo.geocaching.maps.interfaces.GeoPointImpl; import cgeo.geocaching.maps.interfaces.MapActivityImpl; import cgeo.geocaching.maps.interfaces.MapControllerImpl; -import cgeo.geocaching.maps.interfaces.MapFactory; +import cgeo.geocaching.maps.interfaces.MapProvider; import cgeo.geocaching.maps.interfaces.MapViewImpl; import cgeo.geocaching.maps.interfaces.OnDragListener; import cgeo.geocaching.maps.interfaces.OtherCachersOverlayItemImpl; @@ -92,15 +91,10 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory private static final int MENU_AS_LIST = 6; private static final int MENU_NAVIGATE = 7; - private static final int SUBMENU_VIEW_GOOGLE_MAP = 10; - private static final int SUBMENU_VIEW_GOOGLE_SAT = 11; - private static final int SUBMENU_VIEW_MF_MAPNIK = 13; - private static final int SUBMENU_VIEW_MF_OSMARENDER = 14; - private static final int SUBMENU_VIEW_MF_CYCLEMAP = 15; - private static final int SUBMENU_VIEW_MF_OFFLINE = 16; private static final String EXTRAS_MAP_TITLE = "mapTitle"; private Resources res = null; + private MapProvider mapProvider = null; private Activity activity = null; private MapViewImpl mapView = null; private MapControllerImpl mapController = null; @@ -303,7 +297,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory activity = this.getActivity(); app = (cgeoapplication) activity.getApplication(); base = cgBase.getInstance(app); - MapFactory mapFactory = Settings.getMapFactory(); + mapProvider = Settings.getMapProvider(); // reset status noMapTokenShowed = false; @@ -313,7 +307,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory // set layout ActivityMixin.setTheme(activity); - activity.setContentView(Settings.getMapFactory().getMapLayoutId()); + activity.setContentView(mapProvider.getMapLayoutId()); ActivityMixin.setTitle(activity, res.getString(R.string.map_map)); if (geo == null) { @@ -324,7 +318,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory } // initialize map - mapView = (MapViewImpl) activity.findViewById(mapFactory.getMapViewId()); + mapView = (MapViewImpl) activity.findViewById(mapProvider.getMapViewId()); mapView.setMapSource(); mapView.setBuiltInZoomControls(true); mapView.displayZoomControls(true); @@ -538,15 +532,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory } private void addMapViewMenuItems(final Menu menu) { - String[] mapViews = res.getStringArray(R.array.map_sources); - mapSourceEnum mapSource = Settings.getMapSource(); - - menu.add(1, SUBMENU_VIEW_GOOGLE_MAP, 0, mapViews[0]).setCheckable(true).setChecked(mapSource == mapSourceEnum.googleMap); - menu.add(1, SUBMENU_VIEW_GOOGLE_SAT, 0, mapViews[1]).setCheckable(true).setChecked(mapSource == mapSourceEnum.googleSat); - menu.add(1, SUBMENU_VIEW_MF_MAPNIK, 0, mapViews[2]).setCheckable(true).setChecked(mapSource == mapSourceEnum.mapsforgeMapnik); - menu.add(1, SUBMENU_VIEW_MF_OSMARENDER, 0, mapViews[3]).setCheckable(true).setChecked(mapSource == mapSourceEnum.mapsforgeOsmarender); - menu.add(1, SUBMENU_VIEW_MF_CYCLEMAP, 0, mapViews[4]).setCheckable(true).setChecked(mapSource == mapSourceEnum.mapsforgeCycle); - menu.add(1, SUBMENU_VIEW_MF_OFFLINE, 0, mapViews[5]).setCheckable(true).setChecked(mapSource == mapSourceEnum.mapsforgeOffline); + MapProviderFactory.addMapviewMenuItems(menu, 1, Settings.getMapSource()); menu.setGroupCheckable(1, true, true); } @@ -584,8 +570,6 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory item.setTitle(res.getString(R.string.map_circles_show)); } - menu.findItem(SUBMENU_VIEW_MF_OFFLINE).setEnabled(Settings.isValidMapFile()); - item = menu.findItem(MENU_AS_LIST); item.setVisible(live); item.setEnabled(CollectionUtils.isNotEmpty(caches)); @@ -707,9 +691,9 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory return true; } default: - if (SUBMENU_VIEW_GOOGLE_MAP <= id && SUBMENU_VIEW_MF_OFFLINE >= id) { + if (MapProviderFactory.IsValidSourceId(MapProviderFactory.getMapSourceFromMenuId(id))) { item.setChecked(true); - mapSourceEnum mapSource = getMapSourceFromMenuId(id); + int mapSource = MapProviderFactory.getMapSourceFromMenuId(id); boolean mapRestartRequired = switchMapSource(mapSource); @@ -718,7 +702,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory activity.finish(); // prepare information to restart a similar view - Intent mapIntent = new Intent(activity, Settings.getMapFactory().getMapClass()); + Intent mapIntent = new Intent(activity, Settings.getMapProvider().getMapClass()); mapIntent.putExtra(EXTRAS_DETAIL, fromDetailIntent); mapIntent.putExtra(EXTRAS_SEARCH, searchIntent); @@ -750,32 +734,10 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory return false; } - private static mapSourceEnum getMapSourceFromMenuId(int menuItemId) { - - switch (menuItemId) { - case SUBMENU_VIEW_GOOGLE_MAP: - return mapSourceEnum.googleMap; - case SUBMENU_VIEW_GOOGLE_SAT: - return mapSourceEnum.googleSat; - case SUBMENU_VIEW_MF_OSMARENDER: - return mapSourceEnum.mapsforgeOsmarender; - case SUBMENU_VIEW_MF_MAPNIK: - return mapSourceEnum.mapsforgeMapnik; - case SUBMENU_VIEW_MF_CYCLEMAP: - return mapSourceEnum.mapsforgeCycle; - case SUBMENU_VIEW_MF_OFFLINE: - return mapSourceEnum.mapsforgeOffline; - default: - return mapSourceEnum.googleMap; - } - } - - private boolean switchMapSource(mapSourceEnum mapSource) { - boolean oldIsGoogle = Settings.getMapSource().isGoogleMapSource(); - - Settings.setMapSource(mapSource); + private boolean switchMapSource(int sourceId) { + boolean mapRestartRequired = !MapProviderFactory.IsSameProvider(Settings.getMapSource(), sourceId); - boolean mapRestartRequired = mapSource.isGoogleMapSource() != oldIsGoogle; + Settings.setMapSource(sourceId); if (!mapRestartRequired) { mapView.setMapSource(); @@ -1465,7 +1427,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory */ private CachesOverlayItemImpl getItem(cgCoord cgCoord, int icon, final CacheType cacheType) { coordinates.add(cgCoord); - CachesOverlayItemImpl item = Settings.getMapFactory().getCachesOverlayItem(cgCoord, cacheType); + CachesOverlayItemImpl item = mapProvider.getCachesOverlayItem(cgCoord, cacheType); Drawable pin = null; if (iconsCache.containsKey(icon)) { @@ -1562,7 +1524,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory continue; } - item = Settings.getMapFactory().getOtherCachersOverlayItemBase(activity, userOne); + item = mapProvider.getOtherCachersOverlayItemBase(activity, userOne); items.add(item); counter++; @@ -1597,7 +1559,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory coord.setName("some place"); coordinates.add(coord); - final CachesOverlayItemImpl item = Settings.getMapFactory().getCachesOverlayItem(coord, null); + final CachesOverlayItemImpl item = mapProvider.getCachesOverlayItem(coord, null); final int icon; if (waypointTypeIntent != null) { @@ -1773,7 +1735,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory if (!centered && mapState != null) { try { - mapController.setCenter(Settings.getMapFactory().getGeoPointBase(new Geopoint(mapState[0] / 1.0e6, mapState[1] / 1.0e6))); + mapController.setCenter(mapProvider.getGeoPointBase(new Geopoint(mapState[0] / 1.0e6, mapState[1] / 1.0e6))); mapController.setZoom(mapState[2]); } catch (Exception e) { // nothing at all @@ -1833,7 +1795,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory } if (cnt > 0) { - mapController.setCenter(Settings.getMapFactory().getGeoPointBase(new Geopoint(centerLat, centerLon))); + mapController.setCenter(mapProvider.getGeoPointBase(new Geopoint(centerLat, centerLon))); if (Math.abs(maxLat - minLat) != 0 && Math.abs(maxLon - minLon) != 0) { mapController.zoomToSpan(Math.abs(maxLat - minLat), Math.abs(maxLon - minLon)); } @@ -1883,8 +1845,8 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory } // make geopoint - private static GeoPointImpl makeGeoPoint(final Geopoint coords) { - return Settings.getMapFactory().getGeoPointBase(coords); + private GeoPointImpl makeGeoPoint(final Geopoint coords) { + return mapProvider.getGeoPointBase(coords); } // close activity and open homescreen @@ -1908,7 +1870,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory } public static void startActivitySearch(final Activity fromActivity, final cgSearch search, final String title, boolean detail) { - Intent mapIntent = new Intent(fromActivity, Settings.getMapFactory().getMapClass()); + Intent mapIntent = new Intent(fromActivity, Settings.getMapProvider().getMapClass()); mapIntent.putExtra(EXTRAS_DETAIL, detail); mapIntent.putExtra(EXTRAS_SEARCH, search); if (StringUtils.isNotBlank(title)) { @@ -1918,11 +1880,11 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory } public static void startActivityLiveMap(final Context context) { - context.startActivity(new Intent(context, Settings.getMapFactory().getMapClass())); + context.startActivity(new Intent(context, Settings.getMapProvider().getMapClass())); } public static void startActivityCoords(final Context context, final Geopoint coords, final WaypointType type, final String title) { - Intent mapIntent = new Intent(context, Settings.getMapFactory().getMapClass()); + Intent mapIntent = new Intent(context, Settings.getMapProvider().getMapClass()); mapIntent.putExtra(EXTRAS_DETAIL, false); mapIntent.putExtra(EXTRAS_LATITUDE, coords.getLatitude()); mapIntent.putExtra(EXTRAS_LONGITUDE, coords.getLongitude()); @@ -1936,7 +1898,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory } public static void startActivityGeoCode(final Context context, final String geocode) { - Intent mapIntent = new Intent(context, Settings.getMapFactory().getMapClass()); + Intent mapIntent = new Intent(context, Settings.getMapProvider().getMapClass()); mapIntent.putExtra(EXTRAS_DETAIL, false); mapIntent.putExtra(EXTRAS_GEOCODE, geocode); mapIntent.putExtra(EXTRAS_MAP_TITLE, geocode); diff --git a/main/src/cgeo/geocaching/maps/CachesOverlay.java b/main/src/cgeo/geocaching/maps/CachesOverlay.java index f6e3660..52800b9 100644 --- a/main/src/cgeo/geocaching/maps/CachesOverlay.java +++ b/main/src/cgeo/geocaching/maps/CachesOverlay.java @@ -13,8 +13,8 @@ import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.maps.interfaces.CachesOverlayItemImpl; import cgeo.geocaching.maps.interfaces.GeoPointImpl; import cgeo.geocaching.maps.interfaces.ItemizedOverlayImpl; -import cgeo.geocaching.maps.interfaces.MapFactory; import cgeo.geocaching.maps.interfaces.MapProjectionImpl; +import cgeo.geocaching.maps.interfaces.MapProvider; import cgeo.geocaching.maps.interfaces.MapViewImpl; import org.apache.commons.lang3.StringUtils; @@ -50,7 +50,7 @@ public class CachesOverlay extends AbstractItemizedOverlay { private Paint blockedCircle = null; private PaintFlagsDrawFilter setfil = null; private PaintFlagsDrawFilter remfil = null; - private MapFactory mapFactory = null; + private MapProvider mapProvider = null; public CachesOverlay(ItemizedOverlayImpl ovlImpl, Context contextIn, boolean fromDetailIn) { super(ovlImpl); @@ -60,7 +60,7 @@ public class CachesOverlay extends AbstractItemizedOverlay { context = contextIn; fromDetail = fromDetailIn; - mapFactory = Settings.getMapFactory(); + mapProvider = Settings.getMapProvider(); } public void updateItems(CachesOverlayItemImpl item) { @@ -147,11 +147,11 @@ public class CachesOverlay extends AbstractItemizedOverlay { itemCoord.getCoords().getLatitude(), itemCoord.getCoords().getLongitude() + 1, result); final float longitudeLineDistance = result[0]; - GeoPointImpl itemGeo = mapFactory.getGeoPointBase(itemCoord.getCoords()); + GeoPointImpl itemGeo = mapProvider.getGeoPointBase(itemCoord.getCoords()); final Geopoint leftCoords = new Geopoint(itemCoord.getCoords().getLatitude(), itemCoord.getCoords().getLongitude() - 161 / longitudeLineDistance); - GeoPointImpl leftGeo = mapFactory.getGeoPointBase(leftCoords); + GeoPointImpl leftGeo = mapProvider.getGeoPointBase(leftCoords); projection.toPixels(itemGeo, center); projection.toPixels(leftGeo, left); diff --git a/main/src/cgeo/geocaching/maps/MapProviderFactory.java b/main/src/cgeo/geocaching/maps/MapProviderFactory.java new file mode 100644 index 0000000..95be0f3 --- /dev/null +++ b/main/src/cgeo/geocaching/maps/MapProviderFactory.java @@ -0,0 +1,104 @@ +package cgeo.geocaching.maps; + +import cgeo.geocaching.maps.google.GoogleMapProvider; +import cgeo.geocaching.maps.interfaces.MapProvider; +import cgeo.geocaching.maps.mapsforge.MapsforgeMapProvider; + +import android.view.Menu; + +import java.util.SortedMap; +import java.util.TreeMap; + +public class MapProviderFactory { + + private final static int GOOGLEMAP_BASEID = 30; + private final static int MFMAP_BASEID = 40; + + private static MapProviderFactory instance = null; + + private MapProvider[] mapProviders; + private SortedMap<Integer, String> mapSources; + + private MapProviderFactory() { + mapProviders = new MapProvider[] { new GoogleMapProvider(GOOGLEMAP_BASEID), new MapsforgeMapProvider(MFMAP_BASEID) }; + mapSources = new TreeMap<Integer, String>(); + for (MapProvider mp : mapProviders) { + mapSources.putAll(mp.getMapSources()); + } + } + + private static synchronized void initInstance() { + if (null == instance) { + instance = new MapProviderFactory(); + } + } + + private static MapProviderFactory getInstance() { + if (null == instance) { + initInstance(); + } + return instance; + } + + public static SortedMap<Integer, String> getMapSources() { + return getInstance().mapSources; + } + + public static boolean IsValidSourceId(int sourceId) { + return getInstance().mapSources.containsKey(sourceId); + } + + public static boolean IsSameProvider(int sourceId1, int sourceId2) { + for (MapProvider mp : getInstance().mapProviders) { + if (mp.IsMySource(sourceId1) && mp.IsMySource(sourceId2)) { + return true; + } + } + return false; + } + + public static MapProvider getMapProvider(int sourceId) { + for (MapProvider mp : getInstance().mapProviders) { + if (mp.IsMySource(sourceId)) { + return mp; + } + } + return getInstance().mapProviders[0]; + } + + public static int getSourceOrdinalFromId(int sourceId) { + + int sourceOrdinal = 0; + for (int key : getInstance().mapSources.keySet()) { + if (sourceId == key) { + return sourceOrdinal; + } + sourceOrdinal++; + } + return 0; + } + + public static int getSourceIdFromOrdinal(int sourceOrdinal) { + int count = 0; + for (int key : getInstance().mapSources.keySet()) { + if (sourceOrdinal == count) { + return key; + } + count++; + } + return getInstance().mapSources.firstKey(); + } + + public static void addMapviewMenuItems(Menu parentMenu, int groupId, int currentSource) { + + SortedMap<Integer, String> mapSources = getInstance().mapSources; + + for (int key : mapSources.keySet()) { + parentMenu.add(groupId, key, 0, mapSources.get(key)).setCheckable(true).setChecked(key == currentSource); + } + } + + public static int getMapSourceFromMenuId(int menuId) { + return menuId; + } +} diff --git a/main/src/cgeo/geocaching/maps/PositionOverlay.java b/main/src/cgeo/geocaching/maps/PositionOverlay.java index 18aafbe..a09b6de 100644 --- a/main/src/cgeo/geocaching/maps/PositionOverlay.java +++ b/main/src/cgeo/geocaching/maps/PositionOverlay.java @@ -5,8 +5,8 @@ import cgeo.geocaching.Settings; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.maps.interfaces.GeneralOverlay; import cgeo.geocaching.maps.interfaces.GeoPointImpl; -import cgeo.geocaching.maps.interfaces.MapFactory; import cgeo.geocaching.maps.interfaces.MapProjectionImpl; +import cgeo.geocaching.maps.interfaces.MapProvider; import cgeo.geocaching.maps.interfaces.MapViewImpl; import cgeo.geocaching.maps.interfaces.OverlayImpl; @@ -43,18 +43,18 @@ public class PositionOverlay implements GeneralOverlay { private Point historyPointN = new Point(); private Point historyPointP = new Point(); private Activity activity; - private MapFactory mapFactory = null; + private MapProvider mapProvider = null; private OverlayImpl ovlImpl = null; public PositionOverlay(Activity activity, OverlayImpl ovlImpl) { this.activity = activity; - this.mapFactory = Settings.getMapFactory(); + this.mapProvider = Settings.getMapProvider(); this.ovlImpl = ovlImpl; } public void setCoordinates(Location coordinatesIn) { coordinates = coordinatesIn; - location = Settings.getMapFactory().getGeoPointBase(new Geopoint(coordinates)); + location = mapProvider.getGeoPointBase(new Geopoint(coordinates)); } public void setHeading(Float bearingNow) { @@ -119,7 +119,7 @@ public class PositionOverlay implements GeneralOverlay { float longitudeLineDistance = result[0]; final Geopoint leftCoords = new Geopoint(latitude, longitude - accuracy / longitudeLineDistance); - GeoPointImpl leftGeo = mapFactory.getGeoPointBase(leftCoords); + GeoPointImpl leftGeo = mapProvider.getGeoPointBase(leftCoords); projection.toPixels(leftGeo, left); projection.toPixels(location, center); int radius = center.x - left.x; @@ -161,8 +161,8 @@ public class PositionOverlay implements GeneralOverlay { Location now = history.get(cnt); if (prev != null && now != null) { - projection.toPixels(mapFactory.getGeoPointBase(new Geopoint(prev)), historyPointP); - projection.toPixels(mapFactory.getGeoPointBase(new Geopoint(now)), historyPointN); + projection.toPixels(mapProvider.getGeoPointBase(new Geopoint(prev)), historyPointP); + projection.toPixels(mapProvider.getGeoPointBase(new Geopoint(now)), historyPointN); if ((alphaCnt - cnt) > 0) { alpha = 255 / (alphaCnt - cnt); @@ -185,8 +185,8 @@ public class PositionOverlay implements GeneralOverlay { Location now = coordinates; if (prev != null && now != null) { - projection.toPixels(mapFactory.getGeoPointBase(new Geopoint(prev)), historyPointP); - projection.toPixels(mapFactory.getGeoPointBase(new Geopoint(now)), historyPointN); + projection.toPixels(mapProvider.getGeoPointBase(new Geopoint(prev)), historyPointP); + projection.toPixels(mapProvider.getGeoPointBase(new Geopoint(now)), historyPointN); historyLineShadow.setAlpha(255); historyLine.setAlpha(255); diff --git a/main/src/cgeo/geocaching/maps/ScaleOverlay.java b/main/src/cgeo/geocaching/maps/ScaleOverlay.java index ebf0854..e4d4880 100644 --- a/main/src/cgeo/geocaching/maps/ScaleOverlay.java +++ b/main/src/cgeo/geocaching/maps/ScaleOverlay.java @@ -1,7 +1,6 @@ package cgeo.geocaching.maps; import cgeo.geocaching.Settings; -import cgeo.geocaching.Settings.mapSourceEnum; import cgeo.geocaching.cgBase; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.maps.interfaces.GeneralOverlay; @@ -131,7 +130,7 @@ public class ScaleOverlay implements GeneralOverlay { scale.setTypeface(Typeface.DEFAULT_BOLD); } - if (mapSourceEnum.googleSat == Settings.getMapSource()) { + if (mapView.NeedsInvertedColors()) { scaleShadow.setColor(0xFF000000); scale.setColor(0xFFFFFFFF); } else { diff --git a/main/src/cgeo/geocaching/maps/google/GoogleMapFactory.java b/main/src/cgeo/geocaching/maps/google/GoogleMapFactory.java deleted file mode 100644 index d4ac68b..0000000 --- a/main/src/cgeo/geocaching/maps/google/GoogleMapFactory.java +++ /dev/null @@ -1,51 +0,0 @@ -package cgeo.geocaching.maps.google; - -import cgeo.geocaching.R; -import cgeo.geocaching.cgCoord; -import cgeo.geocaching.enumerations.CacheType; -import cgeo.geocaching.geopoint.Geopoint; -import cgeo.geocaching.go4cache.Go4CacheUser; -import cgeo.geocaching.maps.interfaces.CachesOverlayItemImpl; -import cgeo.geocaching.maps.interfaces.GeoPointImpl; -import cgeo.geocaching.maps.interfaces.MapFactory; -import cgeo.geocaching.maps.interfaces.OtherCachersOverlayItemImpl; - -import com.google.android.maps.MapActivity; - -import android.content.Context; - -public class GoogleMapFactory implements MapFactory { - - @Override - public Class<? extends MapActivity> getMapClass() { - return GoogleMapActivity.class; - } - - @Override - public int getMapViewId() { - return R.id.map; - } - - @Override - public int getMapLayoutId() { - return R.layout.map_google; - } - - @Override - public GeoPointImpl getGeoPointBase(final Geopoint coords) { - return new GoogleGeoPoint(coords.getLatitudeE6(), coords.getLongitudeE6()); - } - - @Override - public CachesOverlayItemImpl getCachesOverlayItem(final cgCoord coordinate, final CacheType type) { - GoogleCacheOverlayItem baseItem = new GoogleCacheOverlayItem(coordinate, type); - return baseItem; - } - - @Override - public OtherCachersOverlayItemImpl getOtherCachersOverlayItemBase(Context context, Go4CacheUser userOne) { - GoogleOtherCachersOverlayItem baseItem = new GoogleOtherCachersOverlayItem(context, userOne); - return baseItem; - } - -} diff --git a/main/src/cgeo/geocaching/maps/google/GoogleMapProvider.java b/main/src/cgeo/geocaching/maps/google/GoogleMapProvider.java new file mode 100644 index 0000000..13373f1 --- /dev/null +++ b/main/src/cgeo/geocaching/maps/google/GoogleMapProvider.java @@ -0,0 +1,95 @@ +package cgeo.geocaching.maps.google; + +import cgeo.geocaching.R; +import cgeo.geocaching.cgCoord; +import cgeo.geocaching.cgeoapplication; +import cgeo.geocaching.enumerations.CacheType; +import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.go4cache.Go4CacheUser; +import cgeo.geocaching.maps.MapProviderFactory; +import cgeo.geocaching.maps.interfaces.CachesOverlayItemImpl; +import cgeo.geocaching.maps.interfaces.GeoPointImpl; +import cgeo.geocaching.maps.interfaces.MapProvider; +import cgeo.geocaching.maps.interfaces.OtherCachersOverlayItemImpl; + +import com.google.android.maps.MapActivity; + +import android.content.Context; +import android.content.res.Resources; + +import java.util.HashMap; +import java.util.Map; + +public class GoogleMapProvider implements MapProvider { + + public final static int MAP = 1; + public final static int SATELLITE = 2; + + private final Map<Integer, String> mapSources; + + private int baseId; + + public GoogleMapProvider(int _baseId) { + baseId = _baseId; + final Resources resources = cgeoapplication.getInstance().getResources(); + + mapSources = new HashMap<Integer, String>(); + mapSources.put(baseId + MAP, resources.getString(R.string.map_source_google_map)); + mapSources.put(baseId + SATELLITE, resources.getString(R.string.map_source_google_satellite)); + } + + @Override + public Map<Integer, String> getMapSources() { + + return mapSources; + } + + @Override + public boolean IsMySource(int sourceId) { + return sourceId >= baseId + MAP && sourceId <= baseId + SATELLITE; + } + + public static boolean IsSatelliteSource(int sourceId) { + MapProvider mp = MapProviderFactory.getMapProvider(sourceId); + if (mp instanceof GoogleMapProvider) { + GoogleMapProvider gp = (GoogleMapProvider) mp; + if (gp.baseId + SATELLITE == sourceId) { + return true; + } + } + return false; + } + + @Override + public Class<? extends MapActivity> getMapClass() { + return GoogleMapActivity.class; + } + + @Override + public int getMapViewId() { + return R.id.map; + } + + @Override + public int getMapLayoutId() { + return R.layout.map_google; + } + + @Override + public GeoPointImpl getGeoPointBase(final Geopoint coords) { + return new GoogleGeoPoint(coords.getLatitudeE6(), coords.getLongitudeE6()); + } + + @Override + public CachesOverlayItemImpl getCachesOverlayItem(final cgCoord coordinate, final CacheType type) { + GoogleCacheOverlayItem baseItem = new GoogleCacheOverlayItem(coordinate, type); + return baseItem; + } + + @Override + public OtherCachersOverlayItemImpl getOtherCachersOverlayItemBase(Context context, Go4CacheUser userOne) { + GoogleOtherCachersOverlayItem baseItem = new GoogleOtherCachersOverlayItem(context, userOne); + return baseItem; + } + +} diff --git a/main/src/cgeo/geocaching/maps/google/GoogleMapView.java b/main/src/cgeo/geocaching/maps/google/GoogleMapView.java index 2b87b15..0cf135e 100644 --- a/main/src/cgeo/geocaching/maps/google/GoogleMapView.java +++ b/main/src/cgeo/geocaching/maps/google/GoogleMapView.java @@ -142,14 +142,7 @@ public class GoogleMapView extends MapView implements MapViewImpl { @Override public void setMapSource() { - - switch (Settings.getMapSource()) { - case googleSat: - setSatellite(true); - break; - default: - setSatellite(false); - } + setSatellite(GoogleMapProvider.IsSatelliteSource(Settings.getMapSource())); } @Override @@ -187,4 +180,9 @@ public class GoogleMapView extends MapView implements MapViewImpl { return super.onScroll(e1, e2, distanceX, distanceY); } } + + @Override + public boolean NeedsInvertedColors() { + return false; + } } diff --git a/main/src/cgeo/geocaching/maps/interfaces/MapFactory.java b/main/src/cgeo/geocaching/maps/interfaces/MapProvider.java index 3bc6b8f..96e725b 100644 --- a/main/src/cgeo/geocaching/maps/interfaces/MapFactory.java +++ b/main/src/cgeo/geocaching/maps/interfaces/MapProvider.java @@ -8,6 +8,8 @@ import cgeo.geocaching.go4cache.Go4CacheUser; import android.app.Activity; import android.content.Context; +import java.util.Map; + /** * Defines functions of a factory class to get implementation specific objects * (GeoPoints, OverlayItems, ...) @@ -15,7 +17,11 @@ import android.content.Context; * @author rsudev * */ -public interface MapFactory { +public interface MapProvider { + + public Map<Integer, String> getMapSources(); + + public boolean IsMySource(int sourceId); public Class<? extends Activity> getMapClass(); diff --git a/main/src/cgeo/geocaching/maps/interfaces/MapViewImpl.java b/main/src/cgeo/geocaching/maps/interfaces/MapViewImpl.java index 1f4c304..13d9c65 100644 --- a/main/src/cgeo/geocaching/maps/interfaces/MapViewImpl.java +++ b/main/src/cgeo/geocaching/maps/interfaces/MapViewImpl.java @@ -70,4 +70,13 @@ public interface MapViewImpl { void repaintRequired(GeneralOverlay overlay); void setOnDragListener(OnDragListener onDragListener); + + /** + * Indicates if overlay text or line colours should be dark (normal case) + * or light (inverted case) + * + * @return true - text/draw in light colors, false text/draw in dark colors + */ + boolean NeedsInvertedColors(); + } diff --git a/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapFactory.java b/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapFactory.java deleted file mode 100644 index 3499c1d..0000000 --- a/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapFactory.java +++ /dev/null @@ -1,50 +0,0 @@ -package cgeo.geocaching.maps.mapsforge; - -import cgeo.geocaching.R; -import cgeo.geocaching.cgCoord; -import cgeo.geocaching.enumerations.CacheType; -import cgeo.geocaching.geopoint.Geopoint; -import cgeo.geocaching.go4cache.Go4CacheUser; -import cgeo.geocaching.maps.interfaces.CachesOverlayItemImpl; -import cgeo.geocaching.maps.interfaces.GeoPointImpl; -import cgeo.geocaching.maps.interfaces.MapFactory; -import cgeo.geocaching.maps.interfaces.OtherCachersOverlayItemImpl; - -import android.app.Activity; -import android.content.Context; - -public class MapsforgeMapFactory implements MapFactory { - - @Override - public Class<? extends Activity> getMapClass() { - return MapsforgeMapActivity.class; - } - - @Override - public int getMapViewId() { - return R.id.mfmap; - } - - @Override - public int getMapLayoutId() { - return R.layout.map_mapsforge; - } - - @Override - public GeoPointImpl getGeoPointBase(final Geopoint coords) { - return new MapsforgeGeoPoint(coords.getLatitudeE6(), coords.getLongitudeE6()); - } - - @Override - public CachesOverlayItemImpl getCachesOverlayItem(final cgCoord coordinate, final CacheType type) { - MapsforgeCacheOverlayItem baseItem = new MapsforgeCacheOverlayItem(coordinate, type); - return baseItem; - } - - @Override - public OtherCachersOverlayItemImpl getOtherCachersOverlayItemBase(Context context, Go4CacheUser userOne) { - MapsforgeOtherCachersOverlayItem baseItem = new MapsforgeOtherCachersOverlayItem(context, userOne); - return baseItem; - } - -} diff --git a/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapProvider.java b/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapProvider.java new file mode 100644 index 0000000..b72f4ec --- /dev/null +++ b/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapProvider.java @@ -0,0 +1,96 @@ +package cgeo.geocaching.maps.mapsforge; + +import cgeo.geocaching.R; +import cgeo.geocaching.cgCoord; +import cgeo.geocaching.cgeoapplication; +import cgeo.geocaching.enumerations.CacheType; +import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.go4cache.Go4CacheUser; +import cgeo.geocaching.maps.MapProviderFactory; +import cgeo.geocaching.maps.interfaces.CachesOverlayItemImpl; +import cgeo.geocaching.maps.interfaces.GeoPointImpl; +import cgeo.geocaching.maps.interfaces.MapProvider; +import cgeo.geocaching.maps.interfaces.OtherCachersOverlayItemImpl; + +import android.app.Activity; +import android.content.Context; +import android.content.res.Resources; + +import java.util.HashMap; +import java.util.Map; + +public class MapsforgeMapProvider implements MapProvider { + + public final static int MAPNIK = 1; + public final static int OSMARENDER = 2; + public final static int CYCLEMAP = 3; + public final static int OFFLINE = 4; + + private final Map<Integer, String> mapSources; + + private final int baseId; + + public MapsforgeMapProvider(int _baseId) { + baseId = _baseId; + final Resources resources = cgeoapplication.getInstance().getResources(); + + mapSources = new HashMap<Integer, String>(); + mapSources.put(baseId + MAPNIK, resources.getString(R.string.map_source_osm_mapnik)); + mapSources.put(baseId + OSMARENDER, resources.getString(R.string.map_source_osm_osmarender)); + mapSources.put(baseId + CYCLEMAP, resources.getString(R.string.map_source_osm_cyclemap)); + mapSources.put(baseId + OFFLINE, resources.getString(R.string.map_source_osm_offline)); + } + + @Override + public Map<Integer, String> getMapSources() { + + return mapSources; + } + + @Override + public boolean IsMySource(int sourceId) { + return sourceId >= baseId + MAPNIK && sourceId <= baseId + OFFLINE; + } + + public static int getMapsforgeSource(int sourceId) { + MapProvider mp = MapProviderFactory.getMapProvider(sourceId); + if (mp instanceof MapsforgeMapProvider) { + MapsforgeMapProvider mfp = (MapsforgeMapProvider) mp; + return sourceId - mfp.baseId; + } + return 0; + } + + @Override + public Class<? extends Activity> getMapClass() { + return MapsforgeMapActivity.class; + } + + @Override + public int getMapViewId() { + return R.id.mfmap; + } + + @Override + public int getMapLayoutId() { + return R.layout.map_mapsforge; + } + + @Override + public GeoPointImpl getGeoPointBase(final Geopoint coords) { + return new MapsforgeGeoPoint(coords.getLatitudeE6(), coords.getLongitudeE6()); + } + + @Override + public CachesOverlayItemImpl getCachesOverlayItem(final cgCoord coordinate, final CacheType type) { + MapsforgeCacheOverlayItem baseItem = new MapsforgeCacheOverlayItem(coordinate, type); + return baseItem; + } + + @Override + public OtherCachersOverlayItemImpl getOtherCachersOverlayItemBase(Context context, Go4CacheUser userOne) { + MapsforgeOtherCachersOverlayItem baseItem = new MapsforgeOtherCachersOverlayItem(context, userOne); + return baseItem; + } + +} diff --git a/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapView.java b/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapView.java index cfcdfb6..5da5612 100644 --- a/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapView.java +++ b/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapView.java @@ -165,14 +165,14 @@ public class MapsforgeMapView extends MapView implements MapViewImpl { @Override public void setMapSource() { - switch (Settings.getMapSource()) { - case mapsforgeOsmarender: + switch (MapsforgeMapProvider.getMapsforgeSource(Settings.getMapSource())) { + case MapsforgeMapProvider.OSMARENDER: setMapViewMode(MapViewMode.OSMARENDER_TILE_DOWNLOAD); break; - case mapsforgeCycle: + case MapsforgeMapProvider.CYCLEMAP: setMapViewMode(MapViewMode.OPENCYCLEMAP_TILE_DOWNLOAD); break; - case mapsforgeOffline: + case MapsforgeMapProvider.OFFLINE: if (MapDatabase.isValidMapFile(Settings.getMapFile())) { setMapViewMode(MapViewMode.CANVAS_RENDERER); super.setMapFile(Settings.getMapFile()); @@ -233,4 +233,9 @@ public class MapsforgeMapView extends MapView implements MapViewImpl { return super.onScroll(e1, e2, distanceX, distanceY); } } + + @Override + public boolean NeedsInvertedColors() { + return false; + } } |