From cf7752c80c1dab6a818fb5093bee5cd964990525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Wed, 5 Sep 2018 13:38:12 +0200 Subject: [android] save location state --- .../java/com/mapbox/mapboxsdk/maps/MapboxMap.java | 2 + .../locationlayer/LocationLayerConstants.java | 10 +++- .../plugins/locationlayer/LocationLayerPlugin.java | 56 ++++++++++++++++++++-- .../locationlayer/PluginAnimatorCoordinatorTest.kt | 20 ++++---- 4 files changed, 71 insertions(+), 17 deletions(-) (limited to 'platform/android/MapboxGLAndroidSDK') diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java index 588a327839..7dddcae993 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java @@ -132,6 +132,7 @@ public final class MapboxMap { outState.putBoolean(MapboxConstants.STATE_DEBUG_ACTIVE, nativeMapView.getDebug()); outState.putString(MapboxConstants.STATE_STYLE_URL, nativeMapView.getStyleUrl()); uiSettings.onSaveInstanceState(outState); + locationLayerPlugin.onSaveInstanceState(outState); } /** @@ -156,6 +157,7 @@ public final class MapboxMap { if (!TextUtils.isEmpty(styleUrl)) { nativeMapView.setStyleUrl(savedInstanceState.getString(MapboxConstants.STATE_STYLE_URL)); } + locationLayerPlugin.onRestoreInstanceState(savedInstanceState); } /** diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerConstants.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerConstants.java index b6e4f568af..6e5f9bf4c6 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerConstants.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerConstants.java @@ -5,6 +5,12 @@ package com.mapbox.mapboxsdk.plugins.locationlayer; */ final class LocationLayerConstants { + static final String STATE_LOCATION_ENABLED = "mapbox_location_locationEnabled"; + static final String STATE_LOCATION_OPTIONS = "mapbox_location_options"; + static final String STATE_LOCATION_LAST_LOCATION = "mapbox_location_lastLocation"; + static final String STATE_LOCATION_RENDER_MODE = "mapbox_location_renderMode"; + static final String STATE_LOCATION_CAMERA_MODE = "mapbox_location_cameraMode"; + // Controls the compass update rate in milliseconds static final int COMPASS_UPDATE_RATE_MS = 500; @@ -18,10 +24,10 @@ final class LocationLayerConstants { static final long ACCURACY_RADIUS_ANIMATION_DURATION = 250; // Default animation duration for zooming while tracking. - static final long DEFAULT_TRACKING_ZOOM_ANIMATION_DURATION = 750; + static final long DEFAULT_TRACKING_ZOOM_ANIM_DURATION = 750; // Default animation duration for tilting while tracking. - static final long DEFAULT_TRACKING_TILT_ANIMATION_DURATION = 1250; + static final long DEFAULT_TRACKING_TILT_ANIM_DURATION = 1250; // Sources static final String LOCATION_SOURCE = "mapbox-location-source"; diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java index 1fcdaa7c76..c98ee9c4ce 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java @@ -4,6 +4,7 @@ import android.annotation.SuppressLint; import android.content.Context; import android.hardware.SensorManager; import android.location.Location; +import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.RequiresPermission; @@ -31,8 +32,13 @@ import java.util.concurrent.CopyOnWriteArrayList; import static android.Manifest.permission.ACCESS_COARSE_LOCATION; import static android.Manifest.permission.ACCESS_FINE_LOCATION; -import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.DEFAULT_TRACKING_TILT_ANIMATION_DURATION; -import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.DEFAULT_TRACKING_ZOOM_ANIMATION_DURATION; +import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.DEFAULT_TRACKING_TILT_ANIM_DURATION; +import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.DEFAULT_TRACKING_ZOOM_ANIM_DURATION; +import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.STATE_LOCATION_CAMERA_MODE; +import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.STATE_LOCATION_ENABLED; +import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.STATE_LOCATION_LAST_LOCATION; +import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.STATE_LOCATION_OPTIONS; +import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.STATE_LOCATION_RENDER_MODE; /** * The Location layer plugin provides location awareness to your mobile application. Enabling this @@ -59,6 +65,16 @@ import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants. * When instantiating the plugin for the first time, the map's max/min zoom levels will be set to * {@link LocationLayerOptions#MAX_ZOOM_DEFAULT} and {@link LocationLayerOptions#MIN_ZOOM_DEFAULT} respectively. * You can adjust the zoom range with {@link LocationLayerOptions#maxZoom()} and {@link LocationLayerOptions#minZoom()}. + *

+ * When an activity, or a fragment, that contains the plugin is destroyed and recreated, + * the plugin will restore its state, which is: + *
+ * - If the plugin was enabled, last location will be displayed. + * You still need to activate the plugin, or just provide the {@link LocationEngine}. + *
+ * - {@link CameraMode} and {@link RenderMode} will be restored. + *
+ * - {@link LocationLayerOptions} will be restored. */ public final class LocationLayerPlugin { private static final String TAG = "Mbgl-LocationLayerPlugin"; @@ -265,9 +281,9 @@ public final class LocationLayerPlugin { * @param cameraMode one of the modes found in {@link CameraMode} */ public void setCameraMode(@CameraMode.Mode int cameraMode) { + locationLayerCamera.setCameraMode(cameraMode); boolean isGpsNorth = cameraMode == CameraMode.TRACKING_GPS_NORTH; pluginAnimatorCoordinator.resetAllCameraAnimations(mapboxMap.getCameraPosition(), isGpsNorth); - locationLayerCamera.setCameraMode(cameraMode); } /** @@ -387,7 +403,7 @@ public final class LocationLayerPlugin { * @param zoomLevel The desired zoom level. */ public void zoomWhileTracking(double zoomLevel) { - zoomWhileTracking(zoomLevel, DEFAULT_TRACKING_ZOOM_ANIMATION_DURATION, null); + zoomWhileTracking(zoomLevel, DEFAULT_TRACKING_ZOOM_ANIM_DURATION, null); } /** @@ -445,7 +461,7 @@ public final class LocationLayerPlugin { * @param tilt The desired camera tilt. */ public void tiltWhileTracking(double tilt) { - tiltWhileTracking(tilt, DEFAULT_TRACKING_TILT_ANIMATION_DURATION, null); + tiltWhileTracking(tilt, DEFAULT_TRACKING_TILT_ANIM_DURATION, null); } /** @@ -665,6 +681,28 @@ public final class LocationLayerPlugin { isPluginStarted = false; } + /** + * Internal use. + */ + public void onSaveInstanceState(@NonNull Bundle outState) { + outState.putBoolean(STATE_LOCATION_ENABLED, isEnabled); + outState.putParcelable(STATE_LOCATION_OPTIONS, options); + outState.putInt(STATE_LOCATION_RENDER_MODE, locationLayer.getRenderMode()); + outState.putInt(STATE_LOCATION_CAMERA_MODE, locationLayerCamera.getCameraMode()); + outState.putParcelable(STATE_LOCATION_LAST_LOCATION, lastLocation); + } + + /** + * Internal use. + */ + public void onRestoreInstanceState(@NonNull Bundle savedInstanceState) { + updateLocation(savedInstanceState.getParcelable(STATE_LOCATION_LAST_LOCATION), true); + setCameraMode(savedInstanceState.getInt(STATE_LOCATION_CAMERA_MODE)); + setRenderMode(savedInstanceState.getInt(STATE_LOCATION_RENDER_MODE)); + applyStyle(savedInstanceState.getParcelable(STATE_LOCATION_OPTIONS)); + setLocationLayerEnabled(savedInstanceState.getBoolean(STATE_LOCATION_ENABLED)); + } + /** * Internal use. */ @@ -772,6 +810,14 @@ public final class LocationLayerPlugin { } private void initializeLocationEngine(@NonNull Context context) { + if (this.locationEngine != null) { + if (usingInternalLocationEngine) { + this.locationEngine.removeLocationUpdates(); + this.locationEngine.deactivate(); + } + this.locationEngine.removeLocationEngineListener(locationEngineListener); + } + usingInternalLocationEngine = true; locationEngine = new LocationEngineProvider(context).obtainBestLocationEngineAvailable(); locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY); diff --git a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/plugins/locationlayer/PluginAnimatorCoordinatorTest.kt b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/plugins/locationlayer/PluginAnimatorCoordinatorTest.kt index 738b7b7a20..2d7dc5b1a2 100644 --- a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/plugins/locationlayer/PluginAnimatorCoordinatorTest.kt +++ b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/plugins/locationlayer/PluginAnimatorCoordinatorTest.kt @@ -3,8 +3,8 @@ package com.mapbox.mapboxsdk.plugins.locationlayer import android.location.Location import com.mapbox.mapboxsdk.camera.CameraPosition import com.mapbox.mapboxsdk.geometry.LatLng -import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.DEFAULT_TRACKING_TILT_ANIMATION_DURATION -import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.DEFAULT_TRACKING_ZOOM_ANIMATION_DURATION +import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.DEFAULT_TRACKING_TILT_ANIM_DURATION +import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.DEFAULT_TRACKING_ZOOM_ANIM_DURATION import com.mapbox.mapboxsdk.plugins.locationlayer.PluginAnimator.* import junit.framework.Assert.assertEquals import junit.framework.Assert.assertTrue @@ -154,7 +154,7 @@ class PluginAnimatorCoordinatorTest { pluginAnimatorCoordinator.feedNewZoomLevel( 15.0, cameraPosition, - DEFAULT_TRACKING_ZOOM_ANIMATION_DURATION, + DEFAULT_TRACKING_ZOOM_ANIM_DURATION, null ) @@ -167,12 +167,12 @@ class PluginAnimatorCoordinatorTest { pluginAnimatorCoordinator.feedNewZoomLevel( zoom.toDouble(), cameraPosition, - DEFAULT_TRACKING_ZOOM_ANIMATION_DURATION, + DEFAULT_TRACKING_ZOOM_ANIM_DURATION, null ) val animationDuration = pluginAnimatorCoordinator.animatorMap[ANIMATOR_ZOOM]?.duration as Long - assertEquals(DEFAULT_TRACKING_ZOOM_ANIMATION_DURATION, animationDuration) + assertEquals(DEFAULT_TRACKING_ZOOM_ANIM_DURATION, animationDuration) val target = pluginAnimatorCoordinator.animatorMap[ANIMATOR_ZOOM]?.target as Float assertEquals(zoom, target) @@ -183,7 +183,7 @@ class PluginAnimatorCoordinatorTest { pluginAnimatorCoordinator.feedNewTilt( 30.0, cameraPosition, - DEFAULT_TRACKING_TILT_ANIMATION_DURATION, + DEFAULT_TRACKING_TILT_ANIM_DURATION, null ) @@ -196,12 +196,12 @@ class PluginAnimatorCoordinatorTest { pluginAnimatorCoordinator.feedNewTilt( tilt.toDouble(), cameraPosition, - DEFAULT_TRACKING_TILT_ANIMATION_DURATION, + DEFAULT_TRACKING_TILT_ANIM_DURATION, null ) val animationDuration = pluginAnimatorCoordinator.animatorMap[ANIMATOR_TILT]?.duration as Long - assertEquals(DEFAULT_TRACKING_TILT_ANIMATION_DURATION, animationDuration) + assertEquals(DEFAULT_TRACKING_TILT_ANIM_DURATION, animationDuration) val target = pluginAnimatorCoordinator.animatorMap[ANIMATOR_TILT]?.target as Float assertEquals(tilt, target) @@ -220,7 +220,7 @@ class PluginAnimatorCoordinatorTest { pluginAnimatorCoordinator.feedNewZoomLevel( 15.0, cameraPosition, - DEFAULT_TRACKING_ZOOM_ANIMATION_DURATION, + DEFAULT_TRACKING_ZOOM_ANIM_DURATION, null ) pluginAnimatorCoordinator.cancelZoomAnimation() @@ -233,7 +233,7 @@ class PluginAnimatorCoordinatorTest { pluginAnimatorCoordinator.feedNewTilt( 30.0, cameraPosition, - DEFAULT_TRACKING_TILT_ANIMATION_DURATION, + DEFAULT_TRACKING_TILT_ANIM_DURATION, null ) -- cgit v1.2.1