diff --git a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java index ae3d6f435b9bf..3cc2d17c59904 100644 --- a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java +++ b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java @@ -573,10 +573,6 @@ public void handleMessage(Message msg) { case MSG_DISPATCH_MEDIA_KEY_REPEAT_WITH_WAKE_LOCK: dispatchMediaKeyRepeatWithWakeLock((KeyEvent)msg.obj); break; - case MSG_DISPATCH_VOLKEY_WITH_WAKE_LOCK: - dispatchMediaKeyWithWakeLockToAudioService((KeyEvent)msg.obj); - dispatchMediaKeyWithWakeLockToAudioService(KeyEvent.changeAction((KeyEvent)msg.obj, KeyEvent.ACTION_UP)); - break; case MSG_DISPATCH_SHOW_RECENTS: showRecentApps(false); break; @@ -601,6 +597,14 @@ public void handleMessage(Message msg) { case MSG_LAUNCH_VOICE_ASSIST_WITH_WAKE_LOCK: launchVoiceAssistWithWakeLock(msg.arg1 != 0); break; + case MSG_DISPATCH_VOLKEY_WITH_WAKE_LOCK: { + KeyEvent event = (KeyEvent) msg.obj; + mIsLongPress = true; + dispatchMediaKeyWithWakeLockToAudioService(event); + dispatchMediaKeyWithWakeLockToAudioService( + KeyEvent.changeAction(event, KeyEvent.ACTION_UP)); + break; + } } } } @@ -4434,38 +4438,52 @@ public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) { } } - if (isMusicActive() && (result & ACTION_PASS_TO_USER) == 0) { - if (mVolBtnMusicControls && down && (keyCode != KeyEvent.KEYCODE_VOLUME_MUTE)) { - mIsLongPress = false; - int newKeyCode = event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP ? - KeyEvent.KEYCODE_MEDIA_NEXT : KeyEvent.KEYCODE_MEDIA_PREVIOUS; - Message msg = mHandler.obtainMessage( - MSG_DISPATCH_VOLKEY_WITH_WAKE_LOCK, - new KeyEvent(event.getDownTime(), event.getEventTime(), event - .getAction(), newKeyCode, 0)); - msg.setAsynchronous(true); - mHandler.sendMessageDelayed(msg, ViewConfiguration.getLongPressTimeout()); - break; - } else { - if (mVolBtnMusicControls && !down) { - mHandler.removeMessages(MSG_DISPATCH_VOLKEY_WITH_WAKE_LOCK); - if (mIsLongPress) { + // Disable music and volume control when used as wake key + if ((result & ACTION_PASS_TO_USER) == 0 && !mVolumeRockerWake) { + boolean mayChangeVolume = false; + + if (isMusicActive()) { + if (mVolBtnMusicControls && (keyCode != KeyEvent.KEYCODE_VOLUME_MUTE)) { + // Detect long key presses. + if (down) { + mIsLongPress = false; + // TODO: Long press of MUTE could be mapped to KEYCODE_MEDIA_PLAY_PAUSE + int newKeyCode = event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP ? + KeyEvent.KEYCODE_MEDIA_NEXT : KeyEvent.KEYCODE_MEDIA_PREVIOUS; + scheduleLongPressKeyEvent(event, newKeyCode); + // Consume key down events of all presses. break; + } else { + mHandler.removeMessages(MSG_DISPATCH_VOLKEY_WITH_WAKE_LOCK); + // Consume key up events of long presses only. + if (mIsLongPress) { + break; + } + // Change volume only on key up events of short presses. + mayChangeVolume = true; } + } else { + // Long key press detection not applicable, change volume only + // on key down events + mayChangeVolume = down; } } - } - if ((result & ACTION_PASS_TO_USER) == 0) { - // If we aren't passing to the user and no one else - // handled it send it to the session manager to figure - // out. - MediaSessionLegacyHelper.getHelper(mContext) - .sendVolumeKeyEvent(event, true); + if (mayChangeVolume) { + // If we aren't passing to the user and no one else + // handled it send it to the session manager to figure + // out. + + // Rewrite the event to use key-down as sendVolumeKeyEvent will + // only change the volume on key down. + KeyEvent newEvent = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode); + MediaSessionLegacyHelper.getHelper(mContext) + .sendVolumeKeyEvent(newEvent, true); + } break; } - } break; + } case KeyEvent.KEYCODE_ENDCALL: { result &= ~ACTION_PASS_TO_USER; @@ -4635,6 +4653,14 @@ public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) { return result; } + private void scheduleLongPressKeyEvent(KeyEvent origEvent, int keyCode) { + KeyEvent event = new KeyEvent(origEvent.getDownTime(), origEvent.getEventTime(), + origEvent.getAction(), keyCode, 0); + Message msg = mHandler.obtainMessage(MSG_DISPATCH_VOLKEY_WITH_WAKE_LOCK, event); + msg.setAsynchronous(true); + mHandler.sendMessageDelayed(msg, ViewConfiguration.getLongPressTimeout()); + } + /** * When the screen is off we ignore some keys that might otherwise typically * be considered wake keys. We filter them out here.