From e1927201f625ed0faeb493563a827e9ce4f13bf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Tue, 29 May 2018 17:51:20 +0200 Subject: [PATCH 1/5] Revert "enable/disable inside lifecycle methods (#528)" This reverts commit 1225652 --- .../plugins/locationlayer/LocationLayerPlugin.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java index e41d6d7a8..85424fb56 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java @@ -441,7 +441,7 @@ public void removeOnLocationStaleListener(@NonNull OnLocationStaleListener liste */ @OnLifecycleEvent(Lifecycle.Event.ON_START) public void onStart() { - enableLocationLayerPlugin(); + onLocationLayerStart(); if (isEnabled) { mapView.addOnMapChangedListener(onMapChangedListener); } @@ -454,7 +454,7 @@ public void onStart() { */ @OnLifecycleEvent(Lifecycle.Event.ON_STOP) public void onStop() { - disableLocationLayerPlugin(); + onLocationLayerStop(); mapView.removeOnMapChangedListener(onMapChangedListener); } @@ -547,7 +547,7 @@ private void updateMapWithOptions(final LocationLayerOptions options) { * @since 0.1.0 */ private void updateLocation(final Location location) { - if (location == null || !isEnabled) { + if (location == null) { return; } staleStateManager.updateLatestLocationTime(); From d422a5c315072c19985ce82bf608657fa6a1da8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Wed, 30 May 2018 10:31:05 +0200 Subject: [PATCH 2/5] [location-layer-plugin] - remove location source addition checks --- .../plugins/locationlayer/LocationLayer.java | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java index 6649b8dc9..e2b43c0bb 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java @@ -93,7 +93,6 @@ final class LocationLayer implements LocationLayerAnimator.OnLayerAnimationsValu Point.fromLngLat(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY) ); private GeoJsonSource locationSource; - private boolean isSourceInitialized; LocationLayer(MapView mapView, MapboxMap mapboxMap, LocationLayerOptions options) { this.mapboxMap = mapboxMap; @@ -103,7 +102,6 @@ final class LocationLayer implements LocationLayerAnimator.OnLayerAnimationsValu } void initializeComponents(LocationLayerOptions options) { - prepareLocationSource(); addLocationSource(); addLayers(); applyStyle(options); @@ -299,7 +297,7 @@ void updateForegroundBearing(float bearing) { // Source actions // - private void prepareLocationSource() { + private void addLocationSource() { locationFeature.addNumberProperty(PROPERTY_GPS_BEARING, 0f); locationFeature.addNumberProperty(PROPERTY_COMPASS_BEARING, 0f); locationFeature.addBooleanProperty(PROPERTY_LOCATION_STALE, false); @@ -309,11 +307,8 @@ private void prepareLocationSource() { locationFeature, new GeoJsonOptions().withMaxZoom(16) ); - } - private void addLocationSource() { mapboxMap.addSource(locationSource); - isSourceInitialized = true; } private void refreshSource() { @@ -326,10 +321,6 @@ private void setLocationPoint(Point locationPoint) { locationFeature = Feature.fromGeometry(locationPoint, properties); locationSource.setGeoJson(locationFeature); } - - if (!isSourceInitialized) { - addLocationSource(); - } } // From 965b6d9ae545001eca78b3c6857e7f656012c449 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Wed, 30 May 2018 11:14:33 +0200 Subject: [PATCH 3/5] [location-layer-plugin] - reject location samples when plugin is stopped --- .../locationlayer/LocationLayerPlugin.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java index 85424fb56..2c5b3eadf 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java @@ -71,6 +71,8 @@ public final class LocationLayerPlugin implements LifecycleObserver { private CameraPosition lastCameraPosition; private boolean isEnabled; + private boolean isStarted; + private StaleStateManager staleStateManager; private final CopyOnWriteArrayList onLocationStaleListeners = new CopyOnWriteArrayList<>(); @@ -163,6 +165,16 @@ public void setLocationLayerEnabled(boolean isEnabled) { } } + /** + * Returns whether the plugin is enabled, meaning that location can be displayed and camera modes can be used. + * + * @return true if the plugin is enabled, false otherwise + * @since 0.6.0 + */ + public boolean isLocationLayerEnabled() { + return isEnabled; + } + /** * Sets the camera mode, which determines how the map camera will track the rendered location. *

@@ -459,12 +471,15 @@ public void onStop() { } void onLocationLayerStart() { + isStarted = true; if (isEnabled) { if (locationEngine != null) { locationEngine.addLocationEngineListener(locationEngineListener); } setLastLocation(); setLastCompassHeading(); + locationLayer.show(); + setCameraMode(locationLayerCamera.getCameraMode()); } if (mapboxMap != null) { mapboxMap.addOnCameraMoveListener(onCameraMoveListener); @@ -476,6 +491,8 @@ void onLocationLayerStart() { } void onLocationLayerStop() { + isStarted = false; + locationLayer.hide(); staleStateManager.onStop(); compassManager.onStop(); locationLayerAnimator.cancelAllAnimations(); @@ -522,13 +539,11 @@ private void initializeLocationEngine() { private void enableLocationLayerPlugin() { isEnabled = true; onLocationLayerStart(); - locationLayer.show(); } private void disableLocationLayerPlugin() { isEnabled = false; onLocationLayerStop(); - locationLayer.hide(); } private void updateMapWithOptions(final LocationLayerOptions options) { @@ -547,7 +562,7 @@ private void updateMapWithOptions(final LocationLayerOptions options) { * @since 0.1.0 */ private void updateLocation(final Location location) { - if (location == null) { + if (location == null || !isStarted) { return; } staleStateManager.updateLatestLocationTime(); @@ -649,8 +664,6 @@ public void onMapChanged(int change) { } else if (change == MapView.DID_FINISH_LOADING_STYLE) { locationLayer.initializeComponents(options); locationLayerCamera.initializeOptions(options); - setRenderMode(locationLayer.getRenderMode()); - setCameraMode(locationLayerCamera.getCameraMode()); onLocationLayerStart(); } } From da17b64c7c76bc7c01e2f5a7ac4fdb73ea039a03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Wed, 30 May 2018 11:15:29 +0200 Subject: [PATCH 4/5] [location-layer-plugin] - keep visibility state when initializing layer with a new style --- .../mapboxsdk/plugins/locationlayer/LocationLayer.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java index e2b43c0bb..faf227ea7 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java @@ -94,6 +94,8 @@ final class LocationLayer implements LocationLayerAnimator.OnLayerAnimationsValu ); private GeoJsonSource locationSource; + private boolean isHidden; + LocationLayer(MapView mapView, MapboxMap mapboxMap, LocationLayerOptions options) { this.mapboxMap = mapboxMap; this.context = mapView.getContext(); @@ -105,6 +107,12 @@ void initializeComponents(LocationLayerOptions options) { addLocationSource(); addLayers(); applyStyle(options); + + if (isHidden) { + hide(); + } else { + show(); + } } void applyStyle(@NonNull LocationLayerOptions options) { @@ -165,12 +173,14 @@ int getRenderMode() { void show() { setRenderMode(renderMode); + isHidden = false; } void hide() { for (String layerId : layerMap.keySet()) { setLayerVisibility(layerId, false); } + isHidden = true; } private void setLayerVisibility(String layerId, boolean visible) { From a463cc7c9c86690a998320bd05f7f77f204d8b62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Wed, 30 May 2018 11:17:33 +0200 Subject: [PATCH 5/5] [location-layer-plugin] - reattach map change listener after plugin was pushed to background and brought back in --- .../mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java index 2c5b3eadf..cfdbd3c4b 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java @@ -454,9 +454,7 @@ public void removeOnLocationStaleListener(@NonNull OnLocationStaleListener liste @OnLifecycleEvent(Lifecycle.Event.ON_START) public void onStart() { onLocationLayerStart(); - if (isEnabled) { - mapView.addOnMapChangedListener(onMapChangedListener); - } + mapView.addOnMapChangedListener(onMapChangedListener); } /**