Send long-press of KEYCODE_HEADSETHOOK to the media key listener
For the key events with the code KEYCODE_HEADSETHOOK, the
MediaSessionService always starts the voice input for long-presses
regardless of the media key listener, and only short-presses can be sent
to the media key listener.
This CL sends all media key events to the media key listener first
if the listener is set. If the key event isn't consumed, short-presses
will be sent to the media session and long-presses will start the voice
input.
Bug: 35348856
Test: Manual test (Install the OnMediaKeyListener test app and confirm
that the app can consume the media key long-press)
Change-Id: I82f8e5f355efe16867e6f4345c46470c690e1f80
diff --git a/services/core/java/com/android/server/media/MediaSessionService.java b/services/core/java/com/android/server/media/MediaSessionService.java
index dfa2f86..9ab685d 100644
--- a/services/core/java/com/android/server/media/MediaSessionService.java
+++ b/services/core/java/com/android/server/media/MediaSessionService.java
@@ -811,10 +811,26 @@
+ "to the global priority session.");
return;
}
+ if (!isGlobalPriorityActive) {
+ // Only consider full user.
+ UserRecord user = mUserRecords.get(mCurrentUserIdList.get(0));
+ if (user.mOnMediaKeyListener != null) {
+ if (DEBUG_KEY_EVENT) {
+ Log.d(TAG, "Send " + keyEvent + " to media key listener");
+ }
+ try {
+ user.mOnMediaKeyListener.onMediaKey(keyEvent,
+ new MediaKeyListenerResultReceiver(keyEvent, needWakeLock));
+ return;
+ } catch (RemoteException e) {
+ Log.w(TAG, "Failed to send " + keyEvent + " to media key listener");
+ }
+ }
+ }
if (!isGlobalPriorityActive && isVoiceKey(keyEvent.getKeyCode())) {
handleVoiceKeyEventLocked(keyEvent, needWakeLock);
} else {
- dispatchMediaKeyEventLocked(keyEvent, needWakeLock, true);
+ dispatchMediaKeyEventLocked(keyEvent, needWakeLock);
}
}
} finally {
@@ -1193,15 +1209,14 @@
if (!mVoiceButtonHandled && !keyEvent.isCanceled()) {
// Resend the down then send this event through
KeyEvent downEvent = KeyEvent.changeAction(keyEvent, KeyEvent.ACTION_DOWN);
- dispatchMediaKeyEventLocked(downEvent, needWakeLock, true);
- dispatchMediaKeyEventLocked(keyEvent, needWakeLock, true);
+ dispatchMediaKeyEventLocked(downEvent, needWakeLock);
+ dispatchMediaKeyEventLocked(keyEvent, needWakeLock);
}
}
}
}
- private void dispatchMediaKeyEventLocked(KeyEvent keyEvent, boolean needWakeLock,
- boolean checkMediaKeyListener) {
+ private void dispatchMediaKeyEventLocked(KeyEvent keyEvent, boolean needWakeLock) {
// If we don't have a media button receiver to fall back on
// include non-playing sessions for dispatching.
boolean useNotPlayingSessions = true;
@@ -1220,25 +1235,6 @@
MediaSessionRecord session = mPriorityStack.getDefaultMediaButtonSession(
mCurrentUserIdList, useNotPlayingSessions);
-
- if ((session == null
- || !session.hasFlag(MediaSession.FLAG_EXCLUSIVE_GLOBAL_PRIORITY))
- && checkMediaKeyListener) {
- // Only consider full user.
- UserRecord user = mUserRecords.get(mCurrentUserIdList.get(0));
- if (user.mOnMediaKeyListener != null) {
- if (DEBUG_KEY_EVENT) {
- Log.d(TAG, "Send " + keyEvent + " to media key listener");
- }
- try {
- user.mOnMediaKeyListener.onMediaKey(keyEvent,
- new MediaKeyListenerResultReceiver(keyEvent, needWakeLock));
- return;
- } catch (RemoteException e) {
- Log.w(TAG, "Failed to send " + keyEvent + " to media key listener");
- }
- }
- }
if (session != null) {
if (DEBUG_KEY_EVENT) {
Log.d(TAG, "Sending " + keyEvent + " to " + session);
@@ -1397,7 +1393,12 @@
mHandled = true;
mHandler.removeCallbacks(this);
synchronized (mLock) {
- dispatchMediaKeyEventLocked(mKeyEvent, mNeedWakeLock, false);
+ if (!mPriorityStack.isGlobalPriorityActive()
+ && isVoiceKey(mKeyEvent.getKeyCode())) {
+ handleVoiceKeyEventLocked(mKeyEvent, mNeedWakeLock);
+ } else {
+ dispatchMediaKeyEventLocked(mKeyEvent, mNeedWakeLock);
+ }
}
}
}