Fixes failing PowerMenuTest.

mSingleKeyGestureDetector.interceptKey() for long press power events is
skipped.

Long presses can be triggered for SingleKeyRules as follows:
1. If key down is sent and no following onKeyUp is sent within
longPressTimeoutMs, SingleKeyRule.onLongPress() is triggered.
2. An explicit onKeyDown with flag LONG_PRESS event is injected into the system, which is called by mSingleKeyGestureDetector.interceptKey(), which triggers SingleKeyRule.onLongPress().

When testing manually, long presses are triggered only via the 1st
mechanism. However, when using "adb shell input --keyevent", both
mechanisms are triggered (since the method inserts an explicit
LONG_PRESS KeyEvent), which results in two long presses being triggered,
which is why the test is failing.

This test only allows for the 1st mechanism (which is how events are
triggered on-device when not using adb).

Bug: 382133936
Test: Manual test (1x, 2x, 5x, long-press of power button in captured
and non-capturable windows).
Test: atest PowerKeyGestureTests && atest GestureLauncherServiceTest
Test: atest PowerMenuTest && atest PowerMenuButtonTest
Flag: com.android.hardware.input.override_power_key_behavior_in_focused_window

Change-Id: I41b894ad562cf4aba7d863d7abb5c63aec3cc12f
diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java
index f8a9a9f..d5e4c3f 100644
--- a/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -5900,6 +5900,9 @@
 
         if (event.getKeyCode() == KEYCODE_POWER && event.getAction() == KeyEvent.ACTION_DOWN) {
             if (overridePowerKeyBehaviorInFocusedWindow()) {
+                if (event.getRepeatCount() > 0) {
+                    return;
+                }
                 if (mGestureLauncherService != null) {
                     mGestureLauncherService.processPowerKeyDown(event);
                 }
@@ -5937,10 +5940,8 @@
     }
 
     private boolean detectDoubleTapPower(KeyEvent event) {
-        if (event.getKeyCode() != KEYCODE_POWER || event.getAction() != KeyEvent.ACTION_DOWN) {
-            return false;
-        }
-        if (event.isLongPress()) {
+        if (event.getKeyCode() != KEYCODE_POWER || event.getAction() != KeyEvent.ACTION_DOWN
+                || event.getRepeatCount() != 0) {
             return false;
         }
 
diff --git a/services/tests/wmtests/src/com/android/server/policy/PowerKeyGestureTests.java b/services/tests/wmtests/src/com/android/server/policy/PowerKeyGestureTests.java
index be516e9..1cc4db9 100644
--- a/services/tests/wmtests/src/com/android/server/policy/PowerKeyGestureTests.java
+++ b/services/tests/wmtests/src/com/android/server/policy/PowerKeyGestureTests.java
@@ -124,14 +124,14 @@
     public void testPowerLongPress() {
         // Show assistant.
         mPhoneWindowManager.overrideLongPressOnPower(LONG_PRESS_POWER_ASSISTANT);
-        sendKey(KEYCODE_POWER, true);
+        sendKey(KEYCODE_POWER, SingleKeyGestureDetector.sDefaultLongPressTimeout);
         mPhoneWindowManager.assertSearchManagerLaunchAssist();
 
         mPhoneWindowManager.moveTimeForward(POWER_MULTI_PRESS_TIMEOUT_MILLIS);
 
         // Show global actions.
         mPhoneWindowManager.overrideLongPressOnPower(LONG_PRESS_POWER_GLOBAL_ACTIONS);
-        sendKey(KEYCODE_POWER, true);
+        sendKey(KEYCODE_POWER, SingleKeyGestureDetector.sDefaultLongPressTimeout);
         mPhoneWindowManager.assertShowGlobalActionsCalled();
     }
 
diff --git a/services/tests/wmtests/src/com/android/server/policy/ShortcutKeyTestBase.java b/services/tests/wmtests/src/com/android/server/policy/ShortcutKeyTestBase.java
index 5919501..2329a07 100644
--- a/services/tests/wmtests/src/com/android/server/policy/ShortcutKeyTestBase.java
+++ b/services/tests/wmtests/src/com/android/server/policy/ShortcutKeyTestBase.java
@@ -236,6 +236,10 @@
         sendKeyCombination(new int[]{keyCode}, 0 /*durationMillis*/, longPress, DEFAULT_DISPLAY);
     }
 
+    void sendKey(int keyCode, long durationMillis) {
+        sendKeyCombination(new int[]{keyCode}, durationMillis, false, DEFAULT_DISPLAY);
+    }
+
     boolean sendKeyGestureEventStart(int gestureType) {
         return mPhoneWindowManager.sendKeyGestureEvent(
                 new KeyGestureEvent.Builder().setKeyGestureType(gestureType).setAction(