From 85f9f501b184e09a8f5e99ecbd2c35ba93a6bffd Mon Sep 17 00:00:00 2001 From: Charles Chen Date: Tue, 26 May 2020 16:52:05 +0800 Subject: [PATCH] Add an incorrect context violation on GestureDetector Also update the documentation of GestureDetector ctr. fixes: 157165393 Test: build & run Change-Id: If87d78d7337d7926af0a37819e4365a216a978b2 --- core/java/android/view/GestureDetector.java | 26 ++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/core/java/android/view/GestureDetector.java b/core/java/android/view/GestureDetector.java index f6c72c4eefbc9..55c527ba6fa68 100644 --- a/core/java/android/view/GestureDetector.java +++ b/core/java/android/view/GestureDetector.java @@ -16,6 +16,8 @@ package android.view; +import static android.os.StrictMode.vmIncorrectContextUseEnabled; + import static com.android.internal.util.FrameworkStatsLog.TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__DEEP_PRESS; import static com.android.internal.util.FrameworkStatsLog.TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__DOUBLE_TAP; import static com.android.internal.util.FrameworkStatsLog.TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__LONG_PRESS; @@ -26,9 +28,12 @@ import static com.android.internal.util.FrameworkStatsLog.TOUCH_GESTURE_CLASSIFI import android.compat.annotation.UnsupportedAppUsage; import android.content.Context; import android.os.Build; +import android.os.Bundle; import android.os.Handler; import android.os.Message; +import android.os.StrictMode; import android.os.SystemClock; +import android.util.Log; import com.android.internal.util.FrameworkStatsLog; @@ -228,6 +233,7 @@ public class GestureDetector { } } + private static final String TAG = GestureDetector.class.getSimpleName(); @UnsupportedAppUsage private int mTouchSlopSquare; private int mDoubleTapTouchSlopSquare; @@ -378,7 +384,8 @@ public class GestureDetector { * You may only use this constructor from a {@link android.os.Looper} thread. * @see android.os.Handler#Handler() * - * @param context the application's context + * @param context An {@link android.app.Activity} or a {@link Context} created from + * {@link Context#createWindowContext(int, Bundle)} * @param listener the listener invoked for all the callbacks, this must * not be null. If the listener implements the {@link OnDoubleTapListener} or * {@link OnContextClickListener} then it will also be set as the listener for @@ -395,7 +402,8 @@ public class GestureDetector { * thread associated with the supplied {@link android.os.Handler}. * @see android.os.Handler#Handler() * - * @param context the application's context + * @param context An {@link android.app.Activity} or a {@link Context} created from + * {@link Context#createWindowContext(int, Bundle)} * @param listener the listener invoked for all the callbacks, this must * not be null. If the listener implements the {@link OnDoubleTapListener} or * {@link OnContextClickListener} then it will also be set as the listener for @@ -425,7 +433,8 @@ public class GestureDetector { * thread associated with the supplied {@link android.os.Handler}. * @see android.os.Handler#Handler() * - * @param context the application's context + * @param context An {@link android.app.Activity} or a {@link Context} created from + * {@link Context#createWindowContext(int, Bundle)} * @param listener the listener invoked for all the callbacks, this must * not be null. * @param handler the handler to use for running deferred listener events. @@ -456,6 +465,17 @@ public class GestureDetector { mMaximumFlingVelocity = ViewConfiguration.getMaximumFlingVelocity(); mAmbiguousGestureMultiplier = ViewConfiguration.getAmbiguousGestureMultiplier(); } else { + if (!context.isUiContext() && vmIncorrectContextUseEnabled()) { + final String errorMessage = + "Tried to access UI constants from a non-visual Context."; + final String message = "GestureDetector must be accessed from Activity or other " + + "visual Context. Use an Activity or a Context created with " + + "Context#createWindowContext(int, Bundle), which are adjusted to the " + + "configuration and visual bounds of an area on screen."; + final Exception exception = new IllegalArgumentException(errorMessage); + StrictMode.onIncorrectContextUsed(message, exception); + Log.e(TAG, errorMessage + message, exception); + } final ViewConfiguration configuration = ViewConfiguration.get(context); touchSlop = configuration.getScaledTouchSlop(); doubleTapTouchSlop = configuration.getScaledDoubleTapTouchSlop();