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

Commit 65c75617 authored by Yuhan Yang's avatar Yuhan Yang Committed by Android (Google) Code Review
Browse files

Merge "Update double click implementation to use postDelayed" into main

parents bdfa35a3 db92d6d7
Loading
Loading
Loading
Loading
+29 −9
Original line number Diff line number Diff line
@@ -90,6 +90,9 @@ public class AutoclickController extends BaseEventStreamTransformation {
    public static final int DEFAULT_AUTOCLICK_DELAY_TIME = Flags.enableAutoclickIndicator()
            ? AUTOCLICK_DELAY_WITH_INDICATOR_DEFAULT : AUTOCLICK_DELAY_DEFAULT;

    // Time interval between two left click events are considered as a double click.
    public static long DOUBLE_CLICK_MINIMUM_TIMEOUT = ViewConfiguration.getDoubleTapMinTime();

    // Duration before a press turns into a long press.
    // Factor 1.5 is needed, otherwise a long press is not safely detected.
    public static final long LONG_PRESS_TIMEOUT =
@@ -152,6 +155,8 @@ public class AutoclickController extends BaseEventStreamTransformation {
    // event.
    private long mLongPressDownTime;

    private boolean mHasOngoingDoubleClick = false;

    /**
     * Controller for the auto click type UI panel, allowing users to 1) select what type of auto
     * click they want to perform, 2) pause the auto click, and 3) move the UI panel itself to a
@@ -849,7 +854,11 @@ public class AutoclickController extends BaseEventStreamTransformation {
            }

            sendClick();
            // Hold off resetting internal state until double click complete.
            if (!mHasOngoingDoubleClick) {
                resetInternalState();
            }


            boolean stillDragging = mActiveClickType == AUTOCLICK_TYPE_DRAG
                    && mDragModeIsDragging;
@@ -1134,8 +1143,6 @@ public class AutoclickController extends BaseEventStreamTransformation {
            }
            mLastMotionEvent.getPointerCoords(pointerIndex, mTempPointerCoords[0]);

            final long now = SystemClock.uptimeMillis();

            int actionButton = BUTTON_PRIMARY;
            switch (selectedClickType) {
                case AUTOCLICK_TYPE_RIGHT_CLICK:
@@ -1143,9 +1150,7 @@ public class AutoclickController extends BaseEventStreamTransformation {
                    break;
                case AUTOCLICK_TYPE_DOUBLE_CLICK:
                    actionButton = BUTTON_PRIMARY;
                    long doubleTapMinimumTimeout = ViewConfiguration.getDoubleTapMinTime();
                    sendMotionEvent(actionButton, now);
                    sendMotionEvent(actionButton, now + doubleTapMinimumTimeout);
                    sendDoubleClickEvent();
                    return;
                case AUTOCLICK_TYPE_DRAG:
                    if (mDragModeIsDragging) {
@@ -1161,7 +1166,7 @@ public class AutoclickController extends BaseEventStreamTransformation {
                default:
                    break;
            }
            sendMotionEvent(actionButton, now);
            sendMotionEvent(actionButton);
        }

        /**
@@ -1211,9 +1216,10 @@ public class AutoclickController extends BaseEventStreamTransformation {
            return true;
        }

        private void sendMotionEvent(int actionButton, long eventTime) {
        private void sendMotionEvent(int actionButton) {
            final long now = SystemClock.uptimeMillis();
            MotionEvent downEvent = buildMotionEvent(
                    eventTime, eventTime, actionButton, mLastMotionEvent);
                    now, now, actionButton, mLastMotionEvent);

            MotionEvent pressEvent = MotionEvent.obtain(downEvent);
            pressEvent.setAction(MotionEvent.ACTION_BUTTON_PRESS);
@@ -1241,6 +1247,19 @@ public class AutoclickController extends BaseEventStreamTransformation {
            upEvent.recycle();
        }

        private void sendDoubleClickEvent() {
            mHasOngoingDoubleClick = true;
            sendMotionEvent(BUTTON_PRIMARY);
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    sendMotionEvent(BUTTON_PRIMARY);
                    mHasOngoingDoubleClick = false;
                    resetInternalState();
                }
            }, DOUBLE_CLICK_MINIMUM_TIMEOUT);
        }

        private void sendLongPress() {
            mHasOngoingLongPress = true;
            mLongPressDownTime = SystemClock.uptimeMillis();
@@ -1276,6 +1295,7 @@ public class AutoclickController extends BaseEventStreamTransformation {
                    releaseEvent.recycle();
                    upEvent.recycle();
                    mHasOngoingLongPress = false;
                    resetInternalState();
                }
            }, LONG_PRESS_TIMEOUT);
        }
+4 −0
Original line number Diff line number Diff line
@@ -1089,6 +1089,10 @@ public class AutoclickControllerTest {
        injectFakeMouseMoveEvent(/* x= */ 30f, /* y= */ 100f, MotionEvent.ACTION_HOVER_MOVE);
        mTestableLooper.processAllMessages();

        // When all messages (with delays) are processed.
        mTestableLooper.moveTimeForward(2 * mController.LONG_PRESS_TIMEOUT);
        mTestableLooper.processAllMessages();

        // Verify left click sent.
        assertThat(mMotionEventCaptor.downEvent).isNotNull();
        assertThat(mMotionEventCaptor.downEvent.getButtonState()).isEqualTo(