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

Commit

Permalink
Cancelable callback invocation (#6891)
Browse files Browse the repository at this point in the history
* [android] - allow onCancel to be invoked from camera cancel callbacks

* set to null after finish
  • Loading branch information
tobrun authored and Cameron Mace committed Nov 3, 2016
1 parent 62f0d1b commit 7a03d88
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public class MapView extends FrameLayout {
private boolean styleWasSet = false;

private List<OnMapReadyCallback> onMapReadyCallbackList;
private MapboxMap.CancelableCallback cameraCancelableCallback;
private SnapshotRequest snapshotRequest;

@UiThread
Expand Down Expand Up @@ -708,7 +709,7 @@ void setDirection(@FloatRange(from = MapboxConstants.MINIMUM_DIRECTION, to = Map
return;
}
long duration = animated ? MapboxConstants.ANIMATION_DURATION : 0;
nativeMapView.cancelTransitions();
cancelTransitions();
// Out of range directions are normalised in setBearing
nativeMapView.setBearing(-direction, duration);
}
Expand All @@ -718,7 +719,7 @@ void resetNorth() {
return;
}
myLocationView.setBearing(0);
nativeMapView.cancelTransitions();
cancelTransitions();
nativeMapView.resetNorth();
}

Expand Down Expand Up @@ -796,7 +797,7 @@ private void zoom(boolean zoomIn) {

private void zoom(boolean zoomIn, float x, float y) {
// Cancel any animation
nativeMapView.cancelTransitions();
cancelTransitions();

if (zoomIn) {
nativeMapView.scaleBy(2.0, x / screenDensity, y / screenDensity, MapboxConstants.ANIMATION_DURATION);
Expand Down Expand Up @@ -1339,28 +1340,37 @@ public void invalidateContentPadding() {
// Mapbox Core GL Camera
//

private void cancelTransitions(){
if (cameraCancelableCallback != null) {
cameraCancelableCallback.onCancel();
cameraCancelableCallback = null;
}
nativeMapView.cancelTransitions();
}

void jumpTo(double bearing, LatLng center, double pitch, double zoom) {
if (destroyed) {
return;
}
nativeMapView.cancelTransitions();
cancelTransitions();
nativeMapView.jumpTo(bearing, center, pitch, zoom);
}

void easeTo(double bearing, LatLng center, long duration, double pitch, double zoom, boolean easingInterpolator, @Nullable final MapboxMap.CancelableCallback cancelableCallback) {
if (destroyed) {
return;
}
nativeMapView.cancelTransitions();
cancelTransitions();

// Register callbacks early enough
if (cancelableCallback != null) {
cameraCancelableCallback = cancelableCallback;
addOnMapChangedListener(new OnMapChangedListener() {
@Override
public void onMapChanged(@MapChange int change) {
if (change == REGION_DID_CHANGE_ANIMATED) {
cancelableCallback.onFinish();

if (change == REGION_DID_CHANGE_ANIMATED && cameraCancelableCallback != null) {
cameraCancelableCallback.onFinish();
cameraCancelableCallback = null;
// Clean up after self
removeOnMapChangedListener(this);
}
Expand All @@ -1375,16 +1385,17 @@ void flyTo(double bearing, LatLng center, long duration, double pitch, double zo
if (destroyed) {
return;
}
nativeMapView.cancelTransitions();
cancelTransitions();

// Register callbacks early enough
if (cancelableCallback != null) {
cameraCancelableCallback = cancelableCallback;
addOnMapChangedListener(new OnMapChangedListener() {
@Override
public void onMapChanged(@MapChange int change) {
if (change == REGION_DID_CHANGE_ANIMATED) {
if (change == REGION_DID_CHANGE_ANIMATED && cameraCancelableCallback != null) {
cancelableCallback.onFinish();

cameraCancelableCallback = null;
// Clean up after self
removeOnMapChangedListener(this);
}
Expand Down Expand Up @@ -1855,7 +1866,7 @@ public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
// Cancel any animation
nativeMapView.cancelTransitions();
cancelTransitions();
return true;
}

Expand Down Expand Up @@ -1957,7 +1968,7 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
double duration = speed / (deceleration * ease);

// Cancel any animation
nativeMapView.cancelTransitions();
cancelTransitions();

nativeMapView.moveBy(velocityX * duration / 2.0 / screenDensity, velocityY * duration / 2.0 / screenDensity, (long) (duration * 1000.0f));

Expand Down Expand Up @@ -1989,7 +2000,7 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
// reset tracking if needed
resetTrackingModesIfRequired(true, false);
// Cancel any animation
nativeMapView.cancelTransitions();
cancelTransitions();

// Scroll the map
nativeMapView.moveBy(-distanceX / screenDensity, -distanceY / screenDensity);
Expand Down Expand Up @@ -2060,7 +2071,7 @@ public boolean onScale(ScaleGestureDetector detector) {
}

// Cancel any animation
nativeMapView.cancelTransitions();
cancelTransitions();

// Gesture is a quickzoom if there aren't two fingers
quickZoom = !twoTap;
Expand Down Expand Up @@ -2141,7 +2152,7 @@ public boolean onRotate(RotateGestureDetector detector) {
}

// Cancel any animation
nativeMapView.cancelTransitions();
cancelTransitions();

// rotation constitutes translation of anything except the center of
// rotation, so cancel both location and bearing tracking if required
Expand Down Expand Up @@ -2217,7 +2228,7 @@ public boolean onShove(ShoveGestureDetector detector) {
}

// Cancel any animation
nativeMapView.cancelTransitions();
cancelTransitions();

// Get tilt value (scale and clamp)
double pitch = getTilt();
Expand Down Expand Up @@ -2284,7 +2295,7 @@ public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
}

// Cancel any animation
nativeMapView.cancelTransitions();
cancelTransitions();

// Move left
nativeMapView.moveBy(scrollDist / screenDensity, 0.0 / screenDensity);
Expand All @@ -2296,7 +2307,7 @@ public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
}

// Cancel any animation
nativeMapView.cancelTransitions();
cancelTransitions();

// Move right
nativeMapView.moveBy(-scrollDist / screenDensity, 0.0 / screenDensity);
Expand All @@ -2308,7 +2319,7 @@ public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
}

// Cancel any animation
nativeMapView.cancelTransitions();
cancelTransitions();

// Move up
nativeMapView.moveBy(0.0 / screenDensity, scrollDist / screenDensity);
Expand All @@ -2320,7 +2331,7 @@ public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
}

// Cancel any animation
nativeMapView.cancelTransitions();
cancelTransitions();

// Move down
nativeMapView.moveBy(0.0 / screenDensity, -scrollDist / screenDensity);
Expand Down Expand Up @@ -2400,7 +2411,7 @@ public boolean onTrackballEvent(MotionEvent event) {
}

// Cancel any animation
nativeMapView.cancelTransitions();
cancelTransitions();

// Scroll the map
nativeMapView.moveBy(-10.0 * event.getX() / screenDensity, -10.0 * event.getY() / screenDensity);
Expand Down Expand Up @@ -2496,7 +2507,7 @@ public boolean onGenericMotionEvent(MotionEvent event) {
}

// Cancel any animation
nativeMapView.cancelTransitions();
cancelTransitions();

// Get the vertical scroll amount, one click = 1
float scrollDist = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,17 @@ public void onClick(DialogInterface dialog, int which) {
.tilt(tilt)
.build();

mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 5000);
mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 5000, new MapboxMap.CancelableCallback() {
@Override
public void onCancel() {
Log.v(MapboxConstants.TAG, "OnCancel called");
}

@Override
public void onFinish() {
Log.v(MapboxConstants.TAG, "OnFinish called");
}
});
Log.v(MapboxConstants.TAG, cameraPosition.toString());
}
});
Expand Down

0 comments on commit 7a03d88

Please sign in to comment.