[DO NOT MERGE] Add setting custom MediaButtonReceiver am: 7d8d596370
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/11629766 Change-Id: I9842275c2fd75e4cb2a9fbe2f10abe8d5a1c7295
This commit is contained in:
@@ -19,6 +19,7 @@ package com.android.server.media;
|
|||||||
import android.annotation.IntDef;
|
import android.annotation.IntDef;
|
||||||
import android.annotation.NonNull;
|
import android.annotation.NonNull;
|
||||||
import android.annotation.Nullable;
|
import android.annotation.Nullable;
|
||||||
|
import android.app.PendingIntent;
|
||||||
import android.media.session.ISessionManager;
|
import android.media.session.ISessionManager;
|
||||||
import android.media.session.MediaSession;
|
import android.media.session.MediaSession;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
@@ -76,6 +77,9 @@ public abstract class MediaKeyDispatcher {
|
|||||||
/**
|
/**
|
||||||
* Implement this to customize the logic for which MediaSession should consume which key event.
|
* Implement this to customize the logic for which MediaSession should consume which key event.
|
||||||
*
|
*
|
||||||
|
* Note: This session will have greater priority over the {@link PendingIntent} returned from
|
||||||
|
* {@link #getMediaButtonReceiver(KeyEvent, int, boolean)}.
|
||||||
|
*
|
||||||
* @param keyEvent a non-null KeyEvent whose key code is one of the supported media buttons.
|
* @param keyEvent a non-null KeyEvent whose key code is one of the supported media buttons.
|
||||||
* @param uid the uid value retrieved by calling {@link Binder#getCallingUid()} from
|
* @param uid the uid value retrieved by calling {@link Binder#getCallingUid()} from
|
||||||
* {@link ISessionManager#dispatchMediaKeyEvent(String, boolean, KeyEvent, boolean)}
|
* {@link ISessionManager#dispatchMediaKeyEvent(String, boolean, KeyEvent, boolean)}
|
||||||
@@ -84,7 +88,27 @@ public abstract class MediaKeyDispatcher {
|
|||||||
* @return a {@link MediaSession.Token} instance that should consume the given key event.
|
* @return a {@link MediaSession.Token} instance that should consume the given key event.
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
MediaSession.Token getSessionForKeyEvent(@NonNull KeyEvent keyEvent, int uid,
|
MediaSession.Token getMediaSession(@NonNull KeyEvent keyEvent, int uid,
|
||||||
|
boolean asSystemService) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implement this to customize the logic for which MediaButtonReceiver should consume a
|
||||||
|
* dispatched key event.
|
||||||
|
*
|
||||||
|
* Note: This pending intent will have lower priority over the {@link MediaSession.Token}
|
||||||
|
* returned from {@link #getMediaSession(KeyEvent, int, boolean)}.
|
||||||
|
*
|
||||||
|
* @param keyEvent a non-null KeyEvent whose key code is one of the supported media buttons.
|
||||||
|
* @param uid the uid value retrieved by calling {@link Binder#getCallingUid()} from
|
||||||
|
* {@link ISessionManager#dispatchMediaKeyEvent(String, boolean, KeyEvent, boolean)}
|
||||||
|
* @param asSystemService {@code true} if the event came from the system service via hardware
|
||||||
|
* devices. {@code false} if the event came from the app process through key injection.
|
||||||
|
* @return a {@link PendingIntent} instance that should receive the dispatched key event.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
PendingIntent getMediaButtonReceiver(@NonNull KeyEvent keyEvent, int uid,
|
||||||
boolean asSystemService) {
|
boolean asSystemService) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2404,18 +2404,32 @@ public class MediaSessionService extends SystemService implements Monitor {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
MediaSessionRecord session = null;
|
MediaSessionRecord session = null;
|
||||||
|
MediaButtonReceiverHolder mediaButtonReceiverHolder = null;
|
||||||
|
|
||||||
// Retrieve custom session for key event if it exists.
|
|
||||||
if (mCustomMediaKeyDispatcher != null) {
|
if (mCustomMediaKeyDispatcher != null) {
|
||||||
MediaSession.Token token = mCustomMediaKeyDispatcher.getSessionForKeyEvent(
|
MediaSession.Token token = mCustomMediaKeyDispatcher.getMediaSession(keyEvent, uid,
|
||||||
keyEvent, uid, asSystemService);
|
asSystemService);
|
||||||
if (token != null) {
|
if (token != null) {
|
||||||
session = getMediaSessionRecordLocked(token);
|
session = getMediaSessionRecordLocked(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (session == null) {
|
||||||
|
PendingIntent pi = mCustomMediaKeyDispatcher.getMediaButtonReceiver(keyEvent,
|
||||||
|
uid, asSystemService);
|
||||||
|
if (pi != null) {
|
||||||
|
mediaButtonReceiverHolder = MediaButtonReceiverHolder.create(mContext,
|
||||||
|
mCurrentFullUserRecord.mFullUserId, pi);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (session == null) {
|
if (session == null && mediaButtonReceiverHolder == null) {
|
||||||
session = (MediaSessionRecord) mCurrentFullUserRecord.getMediaButtonSessionLocked();
|
session = (MediaSessionRecord) mCurrentFullUserRecord.getMediaButtonSessionLocked();
|
||||||
|
|
||||||
|
if (session == null) {
|
||||||
|
mediaButtonReceiverHolder =
|
||||||
|
mCurrentFullUserRecord.mLastMediaButtonReceiverHolder;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (session != null) {
|
if (session != null) {
|
||||||
@@ -2438,16 +2452,12 @@ public class MediaSessionService extends SystemService implements Monitor {
|
|||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
Log.w(TAG, "Failed to send callback", e);
|
Log.w(TAG, "Failed to send callback", e);
|
||||||
}
|
}
|
||||||
} else if (mCurrentFullUserRecord.mLastMediaButtonReceiverHolder != null) {
|
} else if (mediaButtonReceiverHolder != null) {
|
||||||
if (needWakeLock) {
|
if (needWakeLock) {
|
||||||
mKeyEventReceiver.acquireWakeLockLocked();
|
mKeyEventReceiver.acquireWakeLockLocked();
|
||||||
}
|
}
|
||||||
String callingPackageName =
|
String callingPackageName =
|
||||||
(asSystemService) ? mContext.getPackageName() : packageName;
|
(asSystemService) ? mContext.getPackageName() : packageName;
|
||||||
|
|
||||||
MediaButtonReceiverHolder mediaButtonReceiverHolder =
|
|
||||||
mCurrentFullUserRecord.mLastMediaButtonReceiverHolder;
|
|
||||||
|
|
||||||
boolean sent = mediaButtonReceiverHolder.send(
|
boolean sent = mediaButtonReceiverHolder.send(
|
||||||
mContext, keyEvent, callingPackageName,
|
mContext, keyEvent, callingPackageName,
|
||||||
needWakeLock ? mKeyEventReceiver.mLastTimeoutId : -1, mKeyEventReceiver,
|
needWakeLock ? mKeyEventReceiver.mLastTimeoutId : -1, mKeyEventReceiver,
|
||||||
|
|||||||
Reference in New Issue
Block a user