Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -529,6 +531,27 @@ class LocationLayerPluginTest {

@Test
fun lifecycle_forceLocationUpdateAfterStopped() {
val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction<LocationLayerPlugin> {
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<LocationLayerPlugin> {
override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap,
uiController: UiController, context: Context) {
Expand All @@ -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,
Expand Down Expand Up @@ -980,6 +1003,28 @@ class LocationLayerPluginTest {
executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider(rule.activity))
}

@Test
fun cameraPositionAdjustedToTrackingModeWhenPluginEnabled() {
val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction<LocationLayerPlugin> {
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public void cancelTiltWhileTrackingAnimation() {
* @since 0.1.0
*/
public void forceLocationUpdate(@Nullable Location location) {
updateLocation(location);
updateLocation(location, false);
}

/**
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -730,15 +728,17 @@ 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) {
lastLocation = location;
return;
}

staleStateManager.updateLatestLocationTime();
if (!fromLastLocation) {
staleStateManager.updateLatestLocationTime();
}
CameraPosition currentCameraPosition = mapboxMap.getCameraPosition();
boolean isGpsNorth = getCameraMode() == CameraMode.TRACKING_GPS_NORTH;
pluginAnimatorCoordinator.feedNewLocation(location, currentCameraPosition, isGpsNorth);
Expand All @@ -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() {
Expand Down Expand Up @@ -883,7 +881,7 @@ public void onConnected() {

@Override
public void onLocationChanged(Location location) {
updateLocation(location);
updateLocation(location, false);
}
};

Expand Down