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

Commit 6070aa3e authored by Arthur Hung's avatar Arthur Hung
Browse files

Fix PowerMenuTest flaky

The long press key gesture could be triggered by long press timeout or
the key event has `FLAG_LONG_PRESS`, we should keep processing the long
press behavior if it's already triggered by FLAG_LONG_PRESS or the press
time is over the timeout.

Bug: 242031003
Test: atest PlatformScenarioTests:PowerMenuTest --rerun-until-failure 20
Test: atest SingleKeyGestureTests
Change-Id: Ibcfc29db67219f9e2b5df9ae6b10b32f74a06435
parent 42c71c61
Loading
Loading
Loading
Loading
+18 −7
Original line number Diff line number Diff line
@@ -49,7 +49,7 @@ public final class SingleKeyGestureDetector {

    // Key code of current key down event, reset when key up.
    private int mDownKeyCode = KeyEvent.KEYCODE_UNKNOWN;
    private volatile boolean mHandledByLongPress = false;
    private boolean mHandledByLongPress = false;
    private final Handler mHandler;
    private long mLastDownTime = 0;

@@ -194,8 +194,8 @@ public final class SingleKeyGestureDetector {
                mHandledByLongPress = true;
                mHandler.removeMessages(MSG_KEY_LONG_PRESS);
                mHandler.removeMessages(MSG_KEY_VERY_LONG_PRESS);
                final Message msg = mHandler.obtainMessage(MSG_KEY_LONG_PRESS, mActiveRule.mKeyCode,
                        0, mActiveRule);
                final Message msg = mHandler.obtainMessage(MSG_KEY_LONG_PRESS, keyCode, 0,
                        mActiveRule);
                msg.setAsynchronous(true);
                mHandler.sendMessage(msg);
            }
@@ -274,13 +274,26 @@ public final class SingleKeyGestureDetector {
    }

    private boolean interceptKeyUp(KeyEvent event) {
        mHandler.removeMessages(MSG_KEY_LONG_PRESS);
        mHandler.removeMessages(MSG_KEY_VERY_LONG_PRESS);
        mDownKeyCode = KeyEvent.KEYCODE_UNKNOWN;
        if (mActiveRule == null) {
            return false;
        }

        if (!mHandledByLongPress) {
            final long eventTime = event.getEventTime();
            if (eventTime < mLastDownTime + mActiveRule.getLongPressTimeoutMs()) {
                mHandler.removeMessages(MSG_KEY_LONG_PRESS);
            } else {
                mHandledByLongPress = mActiveRule.supportLongPress();
            }

            if (eventTime < mLastDownTime + mActiveRule.getVeryLongPressTimeoutMs()) {
                mHandler.removeMessages(MSG_KEY_VERY_LONG_PRESS);
            } else {
                mHandledByLongPress = mActiveRule.supportVeryLongPress();
            }
        }

        if (mHandledByLongPress) {
            mHandledByLongPress = false;
            mKeyPressCounter = 0;
@@ -376,7 +389,6 @@ public final class SingleKeyGestureDetector {
                    if (DEBUG) {
                        Log.i(TAG, "Detect long press " + KeyEvent.keyCodeToString(keyCode));
                    }
                    mHandledByLongPress = true;
                    rule.onLongPress(mLastDownTime);
                    break;
                case MSG_KEY_VERY_LONG_PRESS:
@@ -384,7 +396,6 @@ public final class SingleKeyGestureDetector {
                        Log.i(TAG, "Detect very long press "
                                + KeyEvent.keyCodeToString(keyCode));
                    }
                    mHandledByLongPress = true;
                    rule.onVeryLongPress(mLastDownTime);
                    break;
                case MSG_KEY_DELAYED_PRESS: