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

Commit db92d6d7 authored by Yuhan Yang's avatar Yuhan Yang
Browse files

Update double click implementation to use postDelayed

Use handler.postDelayed() for the double click interval.
Also did a refactor to sendMotionEvent() to make it consistent
with sendLongPress() and startDragEvent().

Test: atest AutoclickControllerTest
Bug: 400744833
Flag: com.android.server.accessibility.enable_autoclick_indicator
Change-Id: If7000cb74925bde74b08bb119dfe440b6412f5ee
parent fc45b428
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
@@ -844,7 +849,11 @@ public class AutoclickController extends BaseEventStreamTransformation {
            }

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


            // If the user is currently dragging, do not reset their click type.
            boolean stillDragging = mActiveClickType == AUTOCLICK_TYPE_DRAG
@@ -1126,8 +1135,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:
@@ -1135,9 +1142,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) {
@@ -1153,7 +1158,7 @@ public class AutoclickController extends BaseEventStreamTransformation {
                default:
                    break;
            }
            sendMotionEvent(actionButton, now);
            sendMotionEvent(actionButton);
        }

        /**
@@ -1203,9 +1208,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);
@@ -1233,6 +1239,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();
@@ -1268,6 +1287,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(