Fix headset media button long press cannot reject call.

When making phone call with headset, another call incoming, long press
headset media button to reject new incoming call, call cannot be
rejected and first call will be muted. This is because SHORT_PRESS
should not be judged by ACTION_UP event's repeatCount which will be
always 0. Actually, ACTION_DOWN event's repeatCount only increases when
LONG_PRESS is performed.

Also, swapping SHORT and LONG press actions between MUTE and HANGUP to
match more of the existing devices.

Fix: 30012668

Change-Id: I6ec28a2676a86cfa06819e297136ff413c14f169
diff --git a/src/com/android/server/telecom/HeadsetMediaButton.java b/src/com/android/server/telecom/HeadsetMediaButton.java
index af0ce13..700bba1 100644
--- a/src/com/android/server/telecom/HeadsetMediaButton.java
+++ b/src/com/android/server/telecom/HeadsetMediaButton.java
@@ -92,6 +92,7 @@
     private final CallsManager mCallsManager;
     private final TelecomSystem.SyncRoot mLock;
     private MediaSession mSession;
+    private KeyEvent mLastHookEvent;
 
     public HeadsetMediaButton(
             Context context,
@@ -114,10 +115,24 @@
     private boolean handleHeadsetHook(KeyEvent event) {
         Log.d(this, "handleHeadsetHook()...%s %s", event.getAction(), event.getRepeatCount());
 
+        // Save ACTION_DOWN Event temporarily.
+        if (event.getAction() == KeyEvent.ACTION_DOWN) {
+            mLastHookEvent = event;
+        }
+
         if (event.isLongPress()) {
             return mCallsManager.onMediaButton(LONG_PRESS);
-        } else if (event.getAction() == KeyEvent.ACTION_UP && event.getRepeatCount() == 0) {
-            return mCallsManager.onMediaButton(SHORT_PRESS);
+        } else if (event.getAction() == KeyEvent.ACTION_UP) {
+            // We should not judge SHORT_PRESS by ACTION_UP event repeatCount, because it always
+            // return 0.
+            // Actually ACTION_DOWN event repeatCount only increases when LONG_PRESS performed.
+            if (mLastHookEvent != null && mLastHookEvent.getRepeatCount() == 0) {
+                return mCallsManager.onMediaButton(SHORT_PRESS);
+            }
+        }
+
+        if (event.getAction() != KeyEvent.ACTION_DOWN) {
+            mLastHookEvent = null;
         }
 
         return true;