Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9c6e668b authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Process DEEP_PRESS in GestureDetector"

parents ffb97186 07f440ad
Loading
Loading
Loading
Loading
+65 −20
Original line number Diff line number Diff line
@@ -237,6 +237,16 @@ public class GestureDetector {
    private static final int LONG_PRESS = 2;
    private static final int TAP = 3;

    /**
     * If a MotionEvent has CLASSIFICATION_AMBIGUOUS_GESTURE set, then certain actions, such as
     * scrolling, will be inhibited. However, to account for the possibility of incorrect
     * classification, the default scrolling will only be inhibited if the gesture moves beyond
     * (default touch slop * AMBIGUOUS_GESTURE_MULTIPLIER). Likewise, the default long press
     * timeout will be increased for some situations where the default behaviour
     * is to cancel it.
     */
    private static final int AMBIGUOUS_GESTURE_MULTIPLIER = 2;

    private final Handler mHandler;
    @UnsupportedAppUsage
    private final OnGestureListener mListener;
@@ -427,7 +437,7 @@ public class GestureDetector {
        if (context == null) {
            //noinspection deprecation
            touchSlop = ViewConfiguration.getTouchSlop();
            doubleTapTouchSlop = touchSlop; // Hack rather than adding a hiden method for this
            doubleTapTouchSlop = touchSlop; // Hack rather than adding a hidden method for this
            doubleTapSlop = ViewConfiguration.getDoubleTapSlop();
            //noinspection deprecation
            mMinimumFlingVelocity = ViewConfiguration.getMinimumFlingVelocity();
@@ -605,6 +615,10 @@ public class GestureDetector {
                if (mInLongPress || mInContextClick) {
                    break;
                }

                final int motionClassification = ev.getClassification();
                final boolean hasPendingLongPress = mHandler.hasMessages(LONG_PRESS);

                final float scrollX = mLastFocusX - focusX;
                final float scrollY = mLastFocusY - focusY;
                if (mIsDoubleTapping) {
@@ -615,6 +629,31 @@ public class GestureDetector {
                    final int deltaY = (int) (focusY - mDownFocusY);
                    int distance = (deltaX * deltaX) + (deltaY * deltaY);
                    int slopSquare = isGeneratedGesture ? 0 : mTouchSlopSquare;

                    final boolean ambiguousGesture =
                            motionClassification == MotionEvent.CLASSIFICATION_AMBIGUOUS_GESTURE;
                    final boolean shouldInhibitDefaultAction =
                            hasPendingLongPress && ambiguousGesture;
                    if (shouldInhibitDefaultAction) {
                        // Inhibit default long press
                        if (distance > slopSquare) {
                            // The default action here is to remove long press. But if the touch
                            // slop below gets increased, and we never exceed the modified touch
                            // slop while still receiving AMBIGUOUS_GESTURE, we risk that *nothing*
                            // will happen in response to user input. To prevent this,
                            // reschedule long press with a modified timeout.
                            mHandler.removeMessages(LONG_PRESS);
                            final int longPressTimeout = ViewConfiguration.getLongPressTimeout();
                            mHandler.sendEmptyMessageAtTime(LONG_PRESS, ev.getDownTime()
                                    + longPressTimeout * AMBIGUOUS_GESTURE_MULTIPLIER);
                        }
                        // Inhibit default scroll. If a gesture is ambiguous, we prevent scroll
                        // until the gesture is resolved.
                        // However, for safety, simply increase the touch slop in case the
                        // classification is erroneous. Since the value is squared, multiply twice.
                        slopSquare *= AMBIGUOUS_GESTURE_MULTIPLIER * AMBIGUOUS_GESTURE_MULTIPLIER;
                    }

                    if (distance > slopSquare) {
                        handled = mListener.onScroll(mCurrentDownEvent, ev, scrollX, scrollY);
                        mLastFocusX = focusX;
@@ -633,6 +672,12 @@ public class GestureDetector {
                    mLastFocusX = focusX;
                    mLastFocusY = focusY;
                }
                final boolean deepPress =
                        motionClassification == MotionEvent.CLASSIFICATION_DEEP_PRESS;
                if (deepPress && hasPendingLongPress) {
                    mHandler.removeMessages(LONG_PRESS);
                    mHandler.sendEmptyMessage(LONG_PRESS);
                }
                break;

            case MotionEvent.ACTION_UP: