From f608ec3918e83af48b1caf7ef83c3c6a00d4298b Mon Sep 17 00:00:00 2001 From: Ryan Loebs Date: Fri, 19 Mar 2021 02:08:01 -0700 Subject: [PATCH] Add flag to disable automatic gesture handling (#1068) * Add flag to disable automatic gesture handling * Toggle onInterceptTouchEvents based on active gestures instead of adding a new flag * Add test for gesture toggle --- .../com/otaliastudios/cameraview/CameraViewTest.java | 11 +++++++++++ .../java/com/otaliastudios/cameraview/CameraView.java | 10 +++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java index 4ee81244..30be1f6f 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java @@ -216,6 +216,17 @@ public class CameraViewTest extends BaseTest { assertEquals(cameraView.getGestureAction(Gesture.PINCH), GestureAction.NONE); } + @Test + public void testGesture_enabled() { + // Ensure touch events are intercepted when at least one gesture is set + cameraView.mapGesture(Gesture.PINCH, GestureAction.ZOOM); + assertTrue(cameraView.onInterceptTouchEvent(null)); + + // Ensure touch events are NOT intercepted when no gestures are set + cameraView.clearGesture(Gesture.PINCH); + assertFalse(cameraView.onInterceptTouchEvent(null)); + } + @Test public void testGesture_enablingDisablingLayouts() { cameraView.clearGesture(Gesture.TAP); diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index f3fd9de8..656a2a43 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -133,6 +133,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { private Engine mEngine; private Filter mPendingFilter; private int mFrameProcessingExecutors; + private int mActiveGestures; // Components private Handler mUiHandler; @@ -605,6 +606,12 @@ public class CameraView extends FrameLayout implements LifecycleObserver { mGestureMap.get(Gesture.SCROLL_VERTICAL) != none); break; } + + mActiveGestures = 0; + for(GestureAction act : mGestureMap.values()) { + mActiveGestures += act == GestureAction.NONE ? 0 : 1; + } + return true; } mapGesture(gesture, none); @@ -633,7 +640,8 @@ public class CameraView extends FrameLayout implements LifecycleObserver { @Override public boolean onInterceptTouchEvent(MotionEvent ev) { - return true; // Steal our own events. + // Steal our own events if gestures are enabled + return mActiveGestures > 0; } @SuppressLint("ClickableViewAccessibility")