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

Commit

Permalink
[android] break location camera tracking when a developer invoked ani…
Browse files Browse the repository at this point in the history
…mation starts
  • Loading branch information
LukasPaczos authored and Łukasz Paczos committed May 24, 2019
1 parent aa3a7cf commit bc74a8e
Show file tree
Hide file tree
Showing 9 changed files with 319 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.location.modes.CameraMode;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.Transform;

import java.util.HashSet;
import java.util.Set;
Expand All @@ -29,6 +30,7 @@ final class LocationCameraController {
private int cameraMode;

private final MapboxMap mapboxMap;
private final Transform transform;
private final OnCameraTrackingChangedListener internalCameraTrackingChangedListener;
private LocationComponentOptions options;
private boolean adjustFocalPoint;
Expand All @@ -44,10 +46,12 @@ final class LocationCameraController {
LocationCameraController(
Context context,
MapboxMap mapboxMap,
Transform transform,
OnCameraTrackingChangedListener internalCameraTrackingChangedListener,
@NonNull LocationComponentOptions options,
OnCameraMoveInvalidateListener onCameraMoveInvalidateListener) {
this.mapboxMap = mapboxMap;
this.transform = transform;

initialGesturesManager = mapboxMap.getGesturesManager();
internalGesturesManager = new LocationGesturesManager(context);
Expand All @@ -63,12 +67,14 @@ final class LocationCameraController {

// Package private for testing purposes
LocationCameraController(MapboxMap mapboxMap,
Transform transform,
MoveGestureDetector moveGestureDetector,
OnCameraTrackingChangedListener internalCameraTrackingChangedListener,
OnCameraMoveInvalidateListener onCameraMoveInvalidateListener,
AndroidGesturesManager initialGesturesManager,
AndroidGesturesManager internalGesturesManager) {
this.mapboxMap = mapboxMap;
this.transform = transform;
this.moveGestureDetector = moveGestureDetector;
this.internalCameraTrackingChangedListener = internalCameraTrackingChangedListener;
this.onCameraMoveInvalidateListener = onCameraMoveInvalidateListener;
Expand Down Expand Up @@ -157,11 +163,13 @@ public void onFinish() {

CameraPosition currentPosition = mapboxMap.getCameraPosition();
if (Utils.immediateAnimation(mapboxMap.getProjection(), currentPosition.target, target)) {
mapboxMap.moveCamera(
transform.moveCamera(
mapboxMap,
update,
callback);
} else {
mapboxMap.animateCamera(
transform.animateCamera(
mapboxMap,
update,
(int) transitionDuration,
callback);
Expand All @@ -182,7 +190,7 @@ private void setBearing(float bearing) {
return;
}

mapboxMap.moveCamera(CameraUpdateFactory.bearingTo(bearing));
transform.moveCamera(mapboxMap, CameraUpdateFactory.bearingTo(bearing), null);
onCameraMoveInvalidateListener.onInvalidateCameraMove();
}

Expand All @@ -191,7 +199,7 @@ private void setLatLng(@NonNull LatLng latLng) {
return;
}

mapboxMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
transform.moveCamera(mapboxMap, CameraUpdateFactory.newLatLng(latLng), null);
onCameraMoveInvalidateListener.onInvalidateCameraMove();

if (adjustFocalPoint) {
Expand All @@ -206,7 +214,7 @@ private void setZoom(float zoom) {
return;
}

mapboxMap.moveCamera(CameraUpdateFactory.zoomTo(zoom));
transform.moveCamera(mapboxMap, CameraUpdateFactory.zoomTo(zoom), null);
onCameraMoveInvalidateListener.onInvalidateCameraMove();
}

Expand All @@ -215,7 +223,7 @@ private void setTilt(float tilt) {
return;
}

mapboxMap.moveCamera(CameraUpdateFactory.tiltTo(tilt));
transform.moveCamera(mapboxMap, CameraUpdateFactory.tiltTo(tilt), null);
onCameraMoveInvalidateListener.onInvalidateCameraMove();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
import com.mapbox.mapboxsdk.maps.MapboxMap.OnCameraMoveListener;
import com.mapbox.mapboxsdk.maps.MapboxMap.OnMapClickListener;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.maps.Transform;

import java.lang.ref.WeakReference;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;

Expand Down Expand Up @@ -96,6 +98,8 @@ public final class LocationComponent {

@NonNull
private final MapboxMap mapboxMap;
@NonNull
private final Transform transform;
private Style style;
private LocationComponentOptions options;
@NonNull
Expand Down Expand Up @@ -179,19 +183,26 @@ public final class LocationComponent {
* <p>
* To get the component object use {@link MapboxMap#getLocationComponent()}.
*/
public LocationComponent(@NonNull MapboxMap mapboxMap) {
public LocationComponent(@NonNull MapboxMap mapboxMap,
@NonNull Transform transform,
@NonNull List<MapboxMap.OnDeveloperAnimationListener> developerAnimationListeners) {
this.mapboxMap = mapboxMap;
this.transform = transform;
developerAnimationListeners.add(developerAnimationListener);
}

// used for creating a spy
LocationComponent() {
//noinspection ConstantConditions
mapboxMap = null;
transform = null;
}

@VisibleForTesting
LocationComponent(@NonNull MapboxMap mapboxMap,
@NonNull LocationEngineCallback<LocationEngineResult> currentlistener,
@NonNull Transform transform,
@NonNull List<MapboxMap.OnDeveloperAnimationListener> developerAnimationListeners,
@NonNull LocationEngineCallback<LocationEngineResult> currentListener,
@NonNull LocationEngineCallback<LocationEngineResult> lastListener,
@NonNull LocationLayerController locationLayerController,
@NonNull LocationCameraController locationCameraController,
Expand All @@ -200,7 +211,9 @@ public LocationComponent(@NonNull MapboxMap mapboxMap) {
@NonNull CompassEngine compassEngine,
@NonNull InternalLocationEngineProvider internalLocationEngineProvider) {
this.mapboxMap = mapboxMap;
this.currentLocationEngineListener = currentlistener;
this.transform = transform;
developerAnimationListeners.add(developerAnimationListener);
this.currentLocationEngineListener = currentListener;
this.lastLocationEngineListener = lastListener;
this.locationLayerController = locationLayerController;
this.locationCameraController = locationCameraController;
Expand Down Expand Up @@ -1204,7 +1217,7 @@ private void initialize(@NonNull final Context context, @NonNull Style style,
locationLayerController = new LocationLayerController(mapboxMap, style, sourceProvider, featureProvider,
bitmapProvider, options, renderModeChangedListener);
locationCameraController = new LocationCameraController(
context, mapboxMap, cameraTrackingChangedListener, options, onCameraMoveInvalidateListener);
context, mapboxMap, transform, cameraTrackingChangedListener, options, onCameraMoveInvalidateListener);

locationAnimatorCoordinator = new LocationAnimatorCoordinator(
mapboxMap.getProjection(),
Expand Down Expand Up @@ -1535,6 +1548,17 @@ public void onRenderModeChanged(int currentMode) {
}
};

@NonNull
private final MapboxMap.OnDeveloperAnimationListener developerAnimationListener =
new MapboxMap.OnDeveloperAnimationListener() {
@Override
public void onDeveloperAnimationStarted() {
if (isComponentInitialized && isEnabled) {
setCameraMode(CameraMode.NONE);
}
}
};

static class InternalLocationEngineProvider {
LocationEngine getBestLocationEngine(@NonNull Context context, boolean background) {
return LocationEngineProvider.getBestLocationEngine(context, background);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ private void initialiseMap() {
Transform transform = new Transform(this, nativeMapView, cameraDispatcher);

// MapboxMap
mapboxMap = new MapboxMap(nativeMapView, transform, uiSettings, proj, registerTouchListener, cameraDispatcher);
List<MapboxMap.OnDeveloperAnimationListener> developerAnimationListeners = new ArrayList<>();
mapboxMap = new MapboxMap(nativeMapView, transform, uiSettings, proj, registerTouchListener, cameraDispatcher,
developerAnimationListeners);
mapboxMap.injectAnnotationManager(annotationManager);

// user input
Expand All @@ -182,7 +184,7 @@ private void initialiseMap() {
compassView.setOnClickListener(createCompassClickListener(cameraDispatcher));

// LocationComponent
mapboxMap.injectLocationComponent(new LocationComponent(mapboxMap));
mapboxMap.injectLocationComponent(new LocationComponent(mapboxMap, transform, developerAnimationListeners));

// inject widgets with MapboxMap
attrView.setOnClickListener(attributionClickListener = new AttributionClickListener(context, mapboxMap));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.support.annotation.UiThread;
import android.text.TextUtils;
import android.view.View;

import com.mapbox.android.gestures.AndroidGesturesManager;
import com.mapbox.android.gestures.MoveGestureDetector;
import com.mapbox.android.gestures.RotateGestureDetector;
Expand Down Expand Up @@ -63,6 +64,7 @@ public final class MapboxMap {
private final CameraChangeDispatcher cameraChangeDispatcher;
private final OnGesturesManagerInteractionListener onGesturesManagerInteractionListener;
private final List<Style.OnStyleLoaded> awaitingStyleGetters = new ArrayList<>();
private final List<OnDeveloperAnimationListener> developerAnimationStartedListeners;

@Nullable
private Style.OnStyleLoaded styleLoadedCallback;
Expand All @@ -79,13 +81,15 @@ public final class MapboxMap {
private boolean debugActive;

MapboxMap(NativeMap map, Transform transform, UiSettings ui, Projection projection,
OnGesturesManagerInteractionListener listener, CameraChangeDispatcher cameraChangeDispatcher) {
OnGesturesManagerInteractionListener listener, CameraChangeDispatcher cameraChangeDispatcher,
List<OnDeveloperAnimationListener> developerAnimationStartedListeners) {
this.nativeMapView = map;
this.uiSettings = ui;
this.projection = projection;
this.transform = transform;
this.onGesturesManagerInteractionListener = listener;
this.cameraChangeDispatcher = cameraChangeDispatcher;
this.developerAnimationStartedListeners = developerAnimationStartedListeners;
}

void initialise(@NonNull Context context, @NonNull MapboxMapOptions options) {
Expand Down Expand Up @@ -414,6 +418,7 @@ public final void moveCamera(@NonNull CameraUpdate update) {
*/
public final void moveCamera(@NonNull final CameraUpdate update,
@Nullable final MapboxMap.CancelableCallback callback) {
notifyDeveloperAnimationListeners();
transform.moveCamera(MapboxMap.this, update, callback);
}

Expand Down Expand Up @@ -522,10 +527,10 @@ public final void easeCamera(@NonNull final CameraUpdate update,
final int durationMs,
final boolean easingInterpolator,
@Nullable final MapboxMap.CancelableCallback callback) {

if (durationMs <= 0) {
throw new IllegalArgumentException("Null duration passed into easeCamera");
}
notifyDeveloperAnimationListeners();
transform.easeCamera(MapboxMap.this, update, durationMs, easingInterpolator, callback);
}

Expand Down Expand Up @@ -596,7 +601,7 @@ public final void animateCamera(@NonNull final CameraUpdate update, final int du
if (durationMs <= 0) {
throw new IllegalArgumentException("Null duration passed into animateCamera");
}

notifyDeveloperAnimationListeners();
transform.animateCamera(MapboxMap.this, update, durationMs, callback);
}

Expand All @@ -608,7 +613,7 @@ public final void animateCamera(@NonNull final CameraUpdate update, final int du
* @param y Amount of pixels to scroll to in y direction
*/
public void scrollBy(float x, float y) {
nativeMapView.moveBy(x, y, 0);
scrollBy(x, y, 0);
}

/**
Expand All @@ -620,6 +625,7 @@ public void scrollBy(float x, float y) {
* @param duration Amount of time the scrolling should take
*/
public void scrollBy(float x, float y, long duration) {
notifyDeveloperAnimationListeners();
nativeMapView.moveBy(x, y, duration);
}

Expand All @@ -631,6 +637,7 @@ public void scrollBy(float x, float y, long duration) {
* Resets the map view to face north.
*/
public void resetNorth() {
notifyDeveloperAnimationListeners();
transform.resetNorth();
}

Expand All @@ -643,6 +650,7 @@ public void resetNorth() {
* @param duration The duration of the transformation
*/
public void setFocalBearing(double bearing, float focalX, float focalY, long duration) {
notifyDeveloperAnimationListeners();
transform.setBearing(bearing, focalX, focalY, duration);
}

Expand Down Expand Up @@ -2342,11 +2350,28 @@ public interface SnapshotReadyCallback {
void onSnapshotReady(@NonNull Bitmap snapshot);
}

/**
* Internal use.
*/
public interface OnDeveloperAnimationListener {

/**
* Notifies listener when a developer invoked animation is about to start.
*/
void onDeveloperAnimationStarted();
}

//
// Used for instrumentation testing
//
@NonNull
Transform getTransform() {
return transform;
}

private void notifyDeveloperAnimationListeners() {
for (OnDeveloperAnimationListener listener : developerAnimationStartedListeners) {
listener.onDeveloperAnimationStarted();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
import static com.mapbox.mapboxsdk.maps.MapboxMap.OnCameraMoveStartedListener;

/**
* Internal use.
* <p>
* Resembles the current Map transformation.
* </p>
* <p>
* Responsible for synchronising {@link CameraPosition} state and notifying camera change listeners.
* </p>
*/
final class Transform implements MapView.OnCameraDidChangeListener {
public final class Transform implements MapView.OnCameraDidChangeListener {

private static final String TAG = "Mbgl-Transform";

Expand Down Expand Up @@ -95,9 +98,12 @@ public void run() {
}
}

/**
* Internal use.
*/
@UiThread
final void moveCamera(@NonNull MapboxMap mapboxMap, CameraUpdate update,
@Nullable final MapboxMap.CancelableCallback callback) {
public final void moveCamera(@NonNull MapboxMap mapboxMap, CameraUpdate update,
@Nullable final MapboxMap.CancelableCallback callback) {
CameraPosition cameraPosition = update.getCameraPosition(mapboxMap);
if (isValidCameraPosition(cameraPosition)) {
cancelTransitions();
Expand Down Expand Up @@ -133,9 +139,12 @@ final void easeCamera(@NonNull MapboxMap mapboxMap, CameraUpdate update, int dur
}
}

/**
* Internal use.
*/
@UiThread
final void animateCamera(@NonNull MapboxMap mapboxMap, CameraUpdate update, int durationMs,
@Nullable final MapboxMap.CancelableCallback callback) {
public final void animateCamera(@NonNull MapboxMap mapboxMap, CameraUpdate update, int durationMs,
@Nullable final MapboxMap.CancelableCallback callback) {
CameraPosition cameraPosition = update.getCameraPosition(mapboxMap);
if (isValidCameraPosition(cameraPosition)) {
cancelTransitions();
Expand Down
Loading

0 comments on commit bc74a8e

Please sign in to comment.