From a0a20dc23babe74e4ba59262323280367ad76185 Mon Sep 17 00:00:00 2001 From: arthurhung Date: Mon, 27 Apr 2020 12:57:26 +0800 Subject: [PATCH] Exception if receive move withouth down Throw an IllegalStateException if recevie touch move without down, that could give an explicit feedback about GestureDetector may not receive the full touch stream. Bug: 149578486 Test: manual Change-Id: I644ec1bebbca66dd0f0c219b63a6478b1290d1e1 --- core/java/android/view/GestureDetector.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core/java/android/view/GestureDetector.java b/core/java/android/view/GestureDetector.java index f6c72c4eefbc9..a9af59543c784 100644 --- a/core/java/android/view/GestureDetector.java +++ b/core/java/android/view/GestureDetector.java @@ -288,6 +288,11 @@ public class GestureDetector { */ private VelocityTracker mVelocityTracker; + /** + * True if the detector can throw exception when touch steam is unexpected . + */ + private boolean mExceptionForTouchStream; + /** * Consistency verifier for debugging purposes. */ @@ -467,6 +472,8 @@ public class GestureDetector { mTouchSlopSquare = touchSlop * touchSlop; mDoubleTapTouchSlopSquare = doubleTapTouchSlop * doubleTapTouchSlop; mDoubleTapSlopSquare = doubleTapSlop * doubleTapSlop; + mExceptionForTouchStream = context != null + && context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.R; } /** @@ -639,6 +646,13 @@ public class GestureDetector { break; case MotionEvent.ACTION_MOVE: + if (mExceptionForTouchStream && !mStillDown) { + throw new IllegalStateException("Incomplete event stream received: " + + "Received ACTION_MOVE before ACTION_DOWN. ACTION_DOWN must precede " + + "ACTION_MOVE following ACTION_UP or ACTION_CANCEL, or when this " + + "GestureDetector has not yet received any events."); + } + if (mInLongPress || mInContextClick) { break; }