diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2012-04-16 09:44:07 +0200 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2012-04-16 12:15:52 +0200 |
| commit | 978dc30c1ab355bc334c041bb1db165f305b8624 (patch) | |
| tree | 6322ed02a958d65b6cfaaf874b326dc43595ddc0 | |
| parent | 7633c242d468a069419f091dec95cb4f70be5034 (diff) | |
| download | cgeo-978dc30c1ab355bc334c041bb1db165f305b8624.zip cgeo-978dc30c1ab355bc334c041bb1db165f305b8624.tar.gz cgeo-978dc30c1ab355bc334c041bb1db165f305b8624.tar.bz2 | |
Fix #1388: switch map view on resume if needed
The map to display may have changed when we come back to the current
activity. In this case, change the map view or, if the map provider must
be changed as well, restart the activity.
4 files changed, 108 insertions, 54 deletions
diff --git a/main/src/cgeo/geocaching/maps/AbstractMap.java b/main/src/cgeo/geocaching/maps/AbstractMap.java index 4fc99a5..ae5b57b 100644 --- a/main/src/cgeo/geocaching/maps/AbstractMap.java +++ b/main/src/cgeo/geocaching/maps/AbstractMap.java @@ -68,4 +68,6 @@ public abstract class AbstractMap { public abstract void goManual(View view); + public abstract void onSaveInstanceState(final Bundle outState); + } diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java index 6853c3b..81fe5a9 100644 --- a/main/src/cgeo/geocaching/maps/CGeoMap.java +++ b/main/src/cgeo/geocaching/maps/CGeoMap.java @@ -115,6 +115,8 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto private static final String EXTRAS_MAP_TITLE = "mapTitle"; + private static final String BUNDLE_MAP_SOURCE = "mapSource"; + private Resources res = null; private MapProvider mapProvider = null; private Activity activity = null; @@ -329,6 +331,9 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto */ private String mapTitle; + /* Current source id */ + private int currentSourceId; + public CGeoMap(MapActivityImpl activity) { super(activity); } @@ -356,6 +361,11 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } @Override + public void onSaveInstanceState(final Bundle outState) { + outState.putInt(BUNDLE_MAP_SOURCE, currentSourceId); + } + + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -365,6 +375,39 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto app = (cgeoapplication) activity.getApplication(); mapProvider = Settings.getMapProvider(); + // Restore previously saved map source if any + if (savedInstanceState != null) { + currentSourceId = savedInstanceState.getInt(BUNDLE_MAP_SOURCE, Settings.getMapSource()); + } else { + currentSourceId = Settings.getMapSource(); + } + + // get parameters + Bundle extras = activity.getIntent().getExtras(); + if (extras != null) { + searchIntent = (SearchResult) extras.getParcelable(EXTRAS_SEARCH); + geocodeIntent = extras.getString(EXTRAS_GEOCODE); + final double latitudeIntent = extras.getDouble(EXTRAS_LATITUDE); + final double longitudeIntent = extras.getDouble(EXTRAS_LONGITUDE); + coordsIntent = new Geopoint(latitudeIntent, longitudeIntent); + waypointTypeIntent = WaypointType.findById(extras.getString(EXTRAS_WPTTYPE)); + mapStateIntent = extras.getIntArray(EXTRAS_MAPSTATE); + mapTitle = extras.getString(EXTRAS_MAP_TITLE); + + if (coordsIntent.getLatitude() == 0.0 || coordsIntent.getLongitude() == 0.0) { + coordsIntent = null; + } + } + + if (StringUtils.isBlank(mapTitle)) { + mapTitle = res.getString(R.string.map_map); + } + + // If recreating from an obsolete map source, we may need a restart + if (changeMapSource(Settings.getMapSource())) { + return; + } + // reset status noMapTokenShowed = false; @@ -422,26 +465,6 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto dirUpdate.updateDirection(dir); } - // get parameters - Bundle extras = activity.getIntent().getExtras(); - if (extras != null) { - searchIntent = (SearchResult) extras.getParcelable(EXTRAS_SEARCH); - geocodeIntent = extras.getString(EXTRAS_GEOCODE); - final double latitudeIntent = extras.getDouble(EXTRAS_LATITUDE); - final double longitudeIntent = extras.getDouble(EXTRAS_LONGITUDE); - coordsIntent = new Geopoint(latitudeIntent, longitudeIntent); - waypointTypeIntent = WaypointType.findById(extras.getString(EXTRAS_WPTTYPE)); - mapStateIntent = extras.getIntArray(EXTRAS_MAPSTATE); - mapTitle = extras.getString(EXTRAS_MAP_TITLE); - - if (coordsIntent.getLatitude() == 0.0 || coordsIntent.getLongitude() == 0.0) { - coordsIntent = null; - } - } - - if (StringUtils.isBlank(mapTitle)) { - mapTitle = res.getString(R.string.map_map); - } // live map, if no arguments are given live = (searchIntent == null && geocodeIntent == null && coordsIntent == null); @@ -486,6 +509,10 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto public void onResume() { super.onResume(); + if (changeMapSource(Settings.getMapSource())) { + return; + } + app.setAction(StringUtils.defaultIfBlank(geocodeIntent, null)); if (geo == null) { geo = app.startGeo(geoUpdate); @@ -809,35 +836,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto item.setChecked(true); int mapSource = MapProviderFactory.getMapSourceFromMenuId(id); - boolean mapRestartRequired = switchMapSource(mapSource); - - if (mapRestartRequired) { - // close old mapview - activity.finish(); - - // prepare information to restart a similar view - Intent mapIntent = new Intent(activity, Settings.getMapProvider().getMapClass()); - - mapIntent.putExtra(EXTRAS_SEARCH, searchIntent); - mapIntent.putExtra(EXTRAS_GEOCODE, geocodeIntent); - if (coordsIntent != null) { - mapIntent.putExtra(EXTRAS_LATITUDE, coordsIntent.getLatitude()); - mapIntent.putExtra(EXTRAS_LONGITUDE, coordsIntent.getLongitude()); - } - mapIntent.putExtra(EXTRAS_WPTTYPE, waypointTypeIntent != null ? waypointTypeIntent.id : null); - int[] mapState = new int[4]; - GeoPointImpl mapCenter = mapView.getMapViewCenter(); - mapState[0] = mapCenter.getLatitudeE6(); - mapState[1] = mapCenter.getLongitudeE6(); - mapState[2] = mapView.getMapZoomLevel(); - mapState[3] = followMyLocation ? 1 : 0; - mapIntent.putExtra(EXTRAS_MAPSTATE, mapState); - mapIntent.putExtra(EXTRAS_MAP_TITLE, mapTitle); - - // start the new map - activity.startActivity(mapIntent); - - } + changeMapSource(mapSource); return true; } @@ -845,16 +844,59 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto return false; } - private boolean switchMapSource(int sourceId) { - boolean mapRestartRequired = !MapProviderFactory.isSameProvider(Settings.getMapSource(), sourceId); + /** + * Restart the current activity if the map provider has changed, or change the map source if needed. + * + * @param mapSource + * the new map source, which can be the same as the current one + * @return true if a restart is needed, false otherwise + */ + private boolean changeMapSource(final int mapSource) { + final boolean restartRequired = !MapProviderFactory.isSameProvider(currentSourceId, mapSource); - Settings.setMapSource(sourceId); + Settings.setMapSource(mapSource); + currentSourceId = mapSource; - if (!mapRestartRequired) { + if (restartRequired) { + mapRestart(); + } else if (mapView != null) { mapView.setMapSource(); } - return mapRestartRequired; + return restartRequired; + } + + /** + * Restart the current activity with the default map source. + */ + private void mapRestart() { + // close old mapview + activity.finish(); + + // prepare information to restart a similar view + Intent mapIntent = new Intent(activity, Settings.getMapProvider().getMapClass()); + + mapIntent.putExtra(EXTRAS_SEARCH, searchIntent); + mapIntent.putExtra(EXTRAS_GEOCODE, geocodeIntent); + if (coordsIntent != null) { + mapIntent.putExtra(EXTRAS_LATITUDE, coordsIntent.getLatitude()); + mapIntent.putExtra(EXTRAS_LONGITUDE, coordsIntent.getLongitude()); + } + mapIntent.putExtra(EXTRAS_WPTTYPE, waypointTypeIntent != null ? waypointTypeIntent.id : null); + mapIntent.putExtra(EXTRAS_MAP_TITLE, mapTitle); + + if (mapView != null) { + int[] mapState = new int[4]; + GeoPointImpl mapCenter = mapView.getMapViewCenter(); + mapState[0] = mapCenter.getLatitudeE6(); + mapState[1] = mapCenter.getLongitudeE6(); + mapState[2] = mapView.getMapZoomLevel(); + mapState[3] = followMyLocation ? 1 : 0; + mapIntent.putExtra(EXTRAS_MAPSTATE, mapState); + } + + // start the new map + activity.startActivity(mapIntent); } private void savePrefs() { diff --git a/main/src/cgeo/geocaching/maps/google/GoogleMapActivity.java b/main/src/cgeo/geocaching/maps/google/GoogleMapActivity.java index ba53003..6cbe9ec 100644 --- a/main/src/cgeo/geocaching/maps/google/GoogleMapActivity.java +++ b/main/src/cgeo/geocaching/maps/google/GoogleMapActivity.java @@ -36,6 +36,11 @@ public class GoogleMapActivity extends MapActivity implements MapActivityImpl { } @Override + protected void onSaveInstanceState(final Bundle outState) { + mapBase.onSaveInstanceState(outState); + } + + @Override protected void onDestroy() { mapBase.onDestroy(); } diff --git a/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapActivity.java b/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapActivity.java index 6ca34d6..06d018c 100644 --- a/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapActivity.java +++ b/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapActivity.java @@ -31,6 +31,11 @@ public class MapsforgeMapActivity extends MapActivity implements MapActivityImpl } @Override + protected void onSaveInstanceState(final Bundle outState) { + mapBase.onSaveInstanceState(outState); + } + + @Override protected void onDestroy() { mapBase.onDestroy(); } |
