From 35bdbfb8b2328426e5013f4e72957313c560298a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Fri, 20 Jul 2018 12:10:31 +0200 Subject: [PATCH] [LLP] adjust camera based on the camera mode when plugin enabled --- .../locationlayer/LocationLayerPluginTest.kt | 49 ++++++++++++++++++- .../locationlayer/LocationLayerPlugin.java | 44 ++++++++--------- 2 files changed, 68 insertions(+), 25 deletions(-) diff --git a/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPluginTest.kt b/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPluginTest.kt index a460dbbcb..ee0813fa0 100644 --- a/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPluginTest.kt +++ b/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPluginTest.kt @@ -19,7 +19,9 @@ import android.support.test.runner.AndroidJUnit4 import android.support.v4.content.ContextCompat import com.mapbox.geojson.Point import com.mapbox.mapboxsdk.camera.CameraPosition +import com.mapbox.mapboxsdk.camera.CameraUpdateFactory import com.mapbox.mapboxsdk.constants.Style +import com.mapbox.mapboxsdk.geometry.LatLng import com.mapbox.mapboxsdk.maps.MapView import com.mapbox.mapboxsdk.maps.MapboxMap import com.mapbox.mapboxsdk.maps.MapboxMapOptions @@ -529,6 +531,27 @@ class LocationLayerPluginTest { @Test fun lifecycle_forceLocationUpdateAfterStopped() { + val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { + override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + + val testLifecycleOwner = TestLifecycleOwner() + testLifecycleOwner.markState(Lifecycle.State.RESUMED) + testLifecycleOwner.lifecycle.addObserver(plugin) + + testLifecycleOwner.markState(Lifecycle.State.CREATED) + plugin.forceLocationUpdate(location) + uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + + assertThat(mapboxMap.querySourceFeatures(LOCATION_SOURCE).isEmpty(), `is`(true)) + } + } + executePluginTest(pluginAction, + PluginGenerationUtil.getLocationLayerPluginProvider(rule.activity, false, null, null, false)) + } + + @Test + fun lifecycle_acceptAndReuseLocationUpdatesBeforeLayerStarted() { val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, uiController: UiController, context: Context) { @@ -543,8 +566,8 @@ class LocationLayerPluginTest { uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) val point: Point = mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].geometry() as Point - assertNotEquals(point.latitude(), location.latitude, 0.1) - assertNotEquals(point.longitude(), location.longitude, 0.1) + assertEquals(point.latitude(), location.latitude, 0.1) + assertEquals(point.longitude(), location.longitude, 0.1) } } executePluginTest(pluginAction, @@ -980,6 +1003,28 @@ class LocationLayerPluginTest { executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider(rule.activity)) } + @Test + fun cameraPositionAdjustedToTrackingModeWhenPluginEnabled() { + val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { + override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.cameraMode = CameraMode.TRACKING_GPS + plugin.forceLocationUpdate(location) + plugin.isLocationLayerEnabled = false + mapboxMap.moveCamera(CameraUpdateFactory.newLatLng(LatLng(51.0, 17.0))) + mapboxMap.moveCamera(CameraUpdateFactory.bearingTo(90.0)) + plugin.isLocationLayerEnabled = true + uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + + assertEquals(location.bearing.toDouble(), mapboxMap.cameraPosition.bearing, 0.1) + assertEquals(location.latitude, mapboxMap.cameraPosition.target.latitude, 0.1) + assertEquals(location.longitude, mapboxMap.cameraPosition.target.longitude, 0.1) + } + } + + executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider(rule.activity)) + } + @After fun afterTest() { Timber.e("@After: unregister idle resource") 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 893a84f31..355236f86 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 @@ -432,7 +432,7 @@ public void cancelTiltWhileTrackingAnimation() { * @since 0.1.0 */ public void forceLocationUpdate(@Nullable Location location) { - updateLocation(location); + updateLocation(location, false); } /** @@ -627,29 +627,27 @@ void onLocationLayerStart() { return; } + if (!isLocationLayerStarted) { + isLocationLayerStarted = true; + if (mapboxMap != null) { + mapboxMap.addOnCameraMoveListener(onCameraMoveListener); + mapboxMap.addOnCameraIdleListener(onCameraIdleListener); + } + if (options.enableStaleState()) { + staleStateManager.onStart(); + } + compassManager.onStart(); + } + if (isEnabled) { if (locationEngine != null) { locationEngine.addLocationEngineListener(locationEngineListener); } - setLastLocation(); - setLastCompassHeading(); locationLayer.show(); setCameraMode(locationLayerCamera.getCameraMode()); + setLastLocation(); + setLastCompassHeading(); } - - if (isLocationLayerStarted) { - return; - } - - isLocationLayerStarted = true; - if (mapboxMap != null) { - mapboxMap.addOnCameraMoveListener(onCameraMoveListener); - mapboxMap.addOnCameraIdleListener(onCameraIdleListener); - } - if (options.enableStaleState()) { - staleStateManager.onStart(); - } - compassManager.onStart(); } void onLocationLayerStop() { @@ -730,7 +728,7 @@ private void updateMapWithOptions(final LocationLayerOptions options) { * @param location the latest user location * @since 0.1.0 */ - private void updateLocation(final Location location) { + private void updateLocation(final Location location, boolean fromLastLocation) { if (location == null) { return; } else if (!isLocationLayerStarted) { @@ -738,7 +736,9 @@ private void updateLocation(final Location location) { return; } - staleStateManager.updateLatestLocationTime(); + if (!fromLastLocation) { + staleStateManager.updateLatestLocationTime(); + } CameraPosition currentCameraPosition = mapboxMap.getCameraPosition(); boolean isGpsNorth = getCameraMode() == CameraMode.TRACKING_GPS_NORTH; pluginAnimatorCoordinator.feedNewLocation(location, currentCameraPosition, isGpsNorth); @@ -756,9 +756,7 @@ private void updateCompassHeading(float heading) { */ @SuppressLint("MissingPermission") private void setLastLocation() { - if (locationEngine != null) { - updateLocation(getLastKnownLocation()); - } + updateLocation(getLastKnownLocation(), true); } private void setLastCompassHeading() { @@ -883,7 +881,7 @@ public void onConnected() { @Override public void onLocationChanged(Location location) { - updateLocation(location); + updateLocation(location, false); } };