diff options
| author | 2022-08-12 06:16:46 +0000 | |
|---|---|---|
| committer | 2022-08-18 08:11:02 +0000 | |
| commit | 6070aa3e2b6fd2c3f14a7a180a91c4493f32fd6a (patch) | |
| tree | 1909c885da48c88ce09c45e2e368f46e183ea94b | |
| parent | 42c71c612a0cc5678ec186d523f95b3d1e54895f (diff) | |
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
| -rw-r--r-- | services/core/java/com/android/server/policy/SingleKeyGestureDetector.java | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/services/core/java/com/android/server/policy/SingleKeyGestureDetector.java b/services/core/java/com/android/server/policy/SingleKeyGestureDetector.java index 9ad15c8b538d..f00edf3a76c5 100644 --- a/services/core/java/com/android/server/policy/SingleKeyGestureDetector.java +++ b/services/core/java/com/android/server/policy/SingleKeyGestureDetector.java @@ -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: |