From 071aac26723ed22e359d38a4b7dffa2fcdf41cf8 Mon Sep 17 00:00:00 2001 From: Cameron Mace Date: Mon, 21 May 2018 10:25:32 -0400 Subject: [PATCH 1/2] Adds constructor with locationEngine initializer --- .../IsolatedActivityPluginTest.kt | 18 ++++++++++ .../locationlayer/LocationLayerPlugin.java | 33 ++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/IsolatedActivityPluginTest.kt b/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/IsolatedActivityPluginTest.kt index 64609494d..b0a52dfd7 100644 --- a/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/IsolatedActivityPluginTest.kt +++ b/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/IsolatedActivityPluginTest.kt @@ -76,6 +76,24 @@ class IsolatedActivityPluginTest { location!!.longitude = 2.0 } + @Test + fun locationLayerPlugin_initializesLocationEngineCorrectlyWhenOnesNotProvided() { + val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { + override fun onGenericPluginAction(plugin: LocationLayerPlugin?, mapboxMap: MapboxMap?, + uiController: UiController, context: Context) { + + mapView = fragment?.view as MapView? + val locationLayerPlugin = LocationLayerPlugin(mapView!!, mapboxMap!!) + val locationEngine = locationLayerPlugin.locationEngine + assertThat(locationEngine, notNullValue()) + + uiController.loopMainThreadForAtLeast(500) + assertThat(locationEngine?.isConnected, `is`(equalTo(true))) + } + } + executePluginTest(pluginAction) + } + @Test fun settingMapStyleImmediatelyBeforeLoadingPlugin_doesStillLoadLayersProperly() { // Using Empty fragment activity to test since Mapbox Style change needs to happen immediately. 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 c9b4eeaa2..5e30a0140 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 @@ -13,6 +13,8 @@ import com.mapbox.android.core.location.LocationEngine; import com.mapbox.android.core.location.LocationEngineListener; +import com.mapbox.android.core.location.LocationEnginePriority; +import com.mapbox.android.core.location.LocationEngineProvider; import com.mapbox.mapboxsdk.camera.CameraPosition; import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.maps.MapView; @@ -59,6 +61,7 @@ public final class LocationLayerPlugin implements LifecycleObserver { private LocationLayerOptions options; private LocationEngine locationEngine; private CompassManager compassManager; + private boolean usingInternalLocationEngine; private LocationLayer locationLayer; private LocationLayerCamera locationLayerCamera; @@ -78,6 +81,24 @@ public final class LocationLayerPlugin implements LifecycleObserver { private final CopyOnWriteArrayList onCameraTrackingChangedListeners = new CopyOnWriteArrayList<>(); + /** + * Construct a LocationLayerPlugin + *

+ * Note: This constructor will initialize and use an internal {@link LocationEngine}. + *

+ * + * @param mapView the MapView to apply the LocationLayerPlugin to + * @param mapboxMap the MapboxMap to apply the LocationLayerPlugin with + * @since 0.5.3 + */ + public LocationLayerPlugin(@NonNull MapView mapView, @NonNull MapboxMap mapboxMap) { + this.mapboxMap = mapboxMap; + this.mapView = mapView; + options = LocationLayerOptions.createFromAttributes(mapView.getContext(), R.style.mapbox_LocationLayer); + initializeLocationEngine(); + initialize(); + } + /** * Construct a LocationLayerPlugin * @@ -483,6 +504,14 @@ private void initialize() { setCameraMode(CameraMode.NONE); } + private void initializeLocationEngine() { + usingInternalLocationEngine = true; + locationEngine = new LocationEngineProvider(mapView.getContext()).obtainBestLocationEngineAvailable(); + locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY); + locationEngine.setFastestInterval(1000); + locationEngine.activate(); + } + private void enableLocationLayerPlugin() { isEnabled = true; onLocationLayerStart(); @@ -643,7 +672,9 @@ public void onCompassAccuracyChange(int compassStatus) { @Override @SuppressWarnings( {"MissingPermission"}) public void onConnected() { - // Currently don't handle this inside SDK + if (usingInternalLocationEngine) { + locationEngine.requestLocationUpdates(); + } } @Override From fb0a91af5f73590cfaf297a36bb910b15078fe6a Mon Sep 17 00:00:00 2001 From: Cameron Mace Date: Tue, 22 May 2018 14:16:56 -0400 Subject: [PATCH 2/2] deconstruct the internal location engine if being used. --- .../plugins/locationlayer/LocationLayerPlugin.java | 7 +++++++ 1 file changed, 7 insertions(+) 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 5e30a0140..e41d6d7a8 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 @@ -284,6 +284,13 @@ public void forceLocationUpdate(@Nullable Location location) { */ public void setLocationEngine(@Nullable LocationEngine locationEngine) { if (this.locationEngine != null) { + // If internal location engines being used, extra steps need to be taken to deconstruct the + // instance. + if (usingInternalLocationEngine) { + this.locationEngine.removeLocationUpdates(); + this.locationEngine.deactivate(); + usingInternalLocationEngine = false; + } this.locationEngine.removeLocationEngineListener(locationEngineListener); this.locationEngine = null; }