From 7a03d883d36f630058564f6eda2ddc156eef0c72 Mon Sep 17 00:00:00 2001 From: Tobrun Date: Thu, 3 Nov 2016 17:26:57 +0100 Subject: [PATCH] Cancelable callback invocation (#6891) * [android] - allow onCancel to be invoked from camera cancel callbacks * set to null after finish --- .../com/mapbox/mapboxsdk/maps/MapView.java | 57 +++++++++++-------- .../camera/CameraPositionActivity.java | 12 +++- 2 files changed, 45 insertions(+), 24 deletions(-) diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java index 32b327e8d01..efa3d5e1a98 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java @@ -158,6 +158,7 @@ public class MapView extends FrameLayout { private boolean styleWasSet = false; private List onMapReadyCallbackList; + private MapboxMap.CancelableCallback cameraCancelableCallback; private SnapshotRequest snapshotRequest; @UiThread @@ -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); } @@ -718,7 +719,7 @@ void resetNorth() { return; } myLocationView.setBearing(0); - nativeMapView.cancelTransitions(); + cancelTransitions(); nativeMapView.resetNorth(); } @@ -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); @@ -1339,11 +1340,19 @@ 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); } @@ -1351,16 +1360,17 @@ void easeTo(double bearing, LatLng center, long duration, double pitch, double z 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); } @@ -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); } @@ -1855,7 +1866,7 @@ public boolean onSingleTapUp(MotionEvent motionEvent) { return false; } // Cancel any animation - nativeMapView.cancelTransitions(); + cancelTransitions(); return true; } @@ -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)); @@ -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); @@ -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; @@ -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 @@ -2217,7 +2228,7 @@ public boolean onShove(ShoveGestureDetector detector) { } // Cancel any animation - nativeMapView.cancelTransitions(); + cancelTransitions(); // Get tilt value (scale and clamp) double pitch = getTilt(); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/camera/CameraPositionActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/camera/CameraPositionActivity.java index 925244b4a42..77a158e37a7 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/camera/CameraPositionActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/camera/CameraPositionActivity.java @@ -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()); } });