Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] fix tracking mode + camera race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Guardiola31337 committed May 29, 2017
1 parent 2c8343a commit ebd8dab
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public class MapboxConstants {
public static final String STATE_MY_BEARING_TRACKING_MODE = "mapbox_myBearingTracking";
public static final String STATE_MY_LOCATION_TRACKING_DISMISS = "mapbox_myLocationTrackingDismiss";
public static final String STATE_MY_BEARING_TRACKING_DISMISS = "mapbox_myBearingTrackingDismiss";
public static final String STATE_MY_TRACKING_MODE_DISMISS_FOR_CAMERA = "mapbox_myBearingTrackingDismiss";
public static final String STATE_MY_TRACKING_MODE_DISMISS_FOR_CAMERA = "mapbox_myBearingTrackingDismissForCamera";
public static final String STATE_COMPASS_ENABLED = "mapbox_compassEnabled";
public static final String STATE_COMPASS_GRAVITY = "mapbox_compassGravity";
public static final String STATE_COMPASS_MARGIN_LEFT = "mapbox_compassMarginLeft";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -729,10 +729,38 @@ public final void easeCamera(CameraUpdate update, int durationMs, boolean easing
@UiThread
public final void easeCamera(final CameraUpdate update, final int durationMs, final boolean easingInterpolator,
final MapboxMap.CancelableCallback callback) {
easeCamera(update, durationMs, easingInterpolator, callback, false);
}

/**
* Gradually move the camera by a specified duration in milliseconds, zoom will not be affected
* unless specified within {@link CameraUpdate}. A callback can be used to be notified when
* easing the camera stops. If {@link #getCameraPosition()} is called during the animation, it
* will return the current location of the camera in flight.
* <p>
* Note that this will cancel location tracking mode if enabled. You can change this behaviour by calling
* {@link TrackingSettings#setDismissTrackingModeForCameraPositionChange(boolean)} with false before invoking this
* method and calling it with true in the {@link CancelableCallback#onFinish()}.
* </p>
*
* @param update The change that should be applied to the camera.
* @param durationMs The duration of the animation in milliseconds. This must be strictly
* positive, otherwise an IllegalArgumentException will be thrown.
* @param easingInterpolator True for easing interpolator, false for linear.
* @param callback An optional callback to be notified from the main thread when the animation
* stops. If the animation stops due to its natural completion, the callback
* will be notified with onFinish(). If the animation stops due to interruption
* by a later camera movement or a user gesture, onCancel() will be called.
* Do not update or ease the camera from within onCancel().
* @param isDismissable true will allow animated camera changes dismiss a tracking mode.
*/
@UiThread
public final void easeCamera(final CameraUpdate update, final int durationMs, final boolean easingInterpolator,
final MapboxMap.CancelableCallback callback, final boolean isDismissable) {
new Handler().post(new Runnable() {
@Override
public void run() {
transform.easeCamera(MapboxMap.this, update, durationMs, easingInterpolator, callback);
transform.easeCamera(MapboxMap.this, update, durationMs, easingInterpolator, callback, isDismissable);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public boolean isDismissTrackingModesForCameraPositionChange() {
* <p>
* </p>
*
* @param willAllowToDismiss True will allow animated camera changes dismiss a trackig mode
* @param willAllowToDismiss True will allow animated camera changes dismiss a tracking mode
*/
public void setDismissTrackingModeForCameraPositionChange(boolean willAllowToDismiss) {
isResetTrackingWithCameraPositionChange = willAllowToDismiss;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ final class Transform implements MapView.OnMapChangedListener {
private CameraPosition cameraPosition;
private MapboxMap.CancelableCallback cameraCancelableCallback;
private MapboxMap.OnCameraChangeListener onCameraChangeListener;
private boolean isFromLocation = false;

Transform(NativeMapView mapView, MarkerViewManager markerViewManager, TrackingSettings trackingSettings) {
this.mapView = mapView;
Expand Down Expand Up @@ -98,7 +99,8 @@ final void moveCamera(MapboxMap mapboxMap, CameraUpdate update, MapboxMap.Cancel

@UiThread
final void easeCamera(MapboxMap mapboxMap, CameraUpdate update, int durationMs, boolean easingInterpolator,
final MapboxMap.CancelableCallback callback) {
final MapboxMap.CancelableCallback callback, boolean isDismissable) {
isFromLocation = isDismissable;
CameraPosition cameraPosition = update.getCameraPosition(mapboxMap);
if (!cameraPosition.equals(this.cameraPosition)) {
trackingSettings.resetTrackingModesIfRequired(cameraPosition);
Expand All @@ -107,7 +109,10 @@ final void easeCamera(MapboxMap mapboxMap, CameraUpdate update, int durationMs,
cameraCancelableCallback = callback;
mapView.addOnMapChangedListener(this);
}

if (isFromLocation) {
trackingSettings.setDismissTrackingModeForCameraPositionChange(true);
isFromLocation = false;
}
mapView.easeTo(cameraPosition.bearing, cameraPosition.target, durationMs, cameraPosition.tilt,
cameraPosition.zoom, easingInterpolator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ void updateAccuracy(@NonNull Location location) {
abstract void invalidate();
}

private class MyLocationTrackingBehavior extends MyLocationBehavior implements MapboxMap.CancelableCallback {
private class MyLocationTrackingBehavior extends MyLocationBehavior {

@Override
void updateLatLng(@NonNull Location location) {
Expand Down Expand Up @@ -791,7 +791,8 @@ void updateLatLng(@NonNull Location location) {
// disable dismiss of tracking settings, enabled in #onFinish
mapboxMap.getTrackingSettings().setDismissTrackingModeForCameraPositionChange(false);
// ease to new camera position with a linear interpolator
mapboxMap.easeCamera(CameraUpdateFactory.newCameraPosition(builder.build()), animationDuration, false, this);
mapboxMap.easeCamera(CameraUpdateFactory.newCameraPosition(builder.build()), animationDuration, false, null,
true);
}

@Override
Expand All @@ -802,22 +803,6 @@ void invalidate() {
screenLocation = new PointF(x, y);
MyLocationView.this.invalidate();
}

@Override
public void onCancel() {
//no op
}

@Override
public void onFinish() {
// Posting to end message queue to avoid race condition #8560
post(new Runnable() {
@Override
public void run() {
mapboxMap.getTrackingSettings().setDismissTrackingModeForCameraPositionChange(true);
}
});
}
}

private class MyLocationShowBehavior extends MyLocationBehavior {
Expand Down

0 comments on commit ebd8dab

Please sign in to comment.