Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 33f8af14 authored by Hyundo Moon's avatar Hyundo Moon
Browse files

Add public getMediaKeyEventSession(PackageName)

The MediaSessionManager has two system APIs
 - getMediaKeyEventSession
 - getMediaKeyEventSessionPackageName
which get the current the media key session and its package name.

Since the APIs require MEDIA_CONTENT_CONTROL permission,
non-system apps could not use the APIs.

This CL adds an overloaded version of those APIs so that apps with
enabled notification listener can use the functionality.

Bug: 187483932
Test: Passed MediaSessionManager(Host)Test
Change-Id: I18904fb68ad175a668f79deb77ced507b7fc800b
parent 27c9c79f
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -25270,6 +25270,8 @@ package android.media.session {
    method public void addOnSession2TokensChangedListener(@NonNull android.media.session.MediaSessionManager.OnSession2TokensChangedListener);
    method public void addOnSession2TokensChangedListener(@NonNull android.media.session.MediaSessionManager.OnSession2TokensChangedListener, @NonNull android.os.Handler);
    method @NonNull public java.util.List<android.media.session.MediaController> getActiveSessions(@Nullable android.content.ComponentName);
    method @Nullable public android.media.session.MediaSession.Token getMediaKeyEventSession(@NonNull android.content.ComponentName);
    method @NonNull public String getMediaKeyEventSessionPackageName(@NonNull android.content.ComponentName);
    method @NonNull public java.util.List<android.media.Session2Token> getSession2Tokens();
    method public boolean isTrustedForMediaControl(@NonNull android.media.session.MediaSessionManager.RemoteUserInfo);
    method @Deprecated public void notifySession2Created(@NonNull android.media.Session2Token);
+2 −2
Original line number Diff line number Diff line
@@ -39,8 +39,8 @@ interface ISessionManager {
    ISession createSession(String packageName, in ISessionCallback sessionCb, String tag,
            in Bundle sessionInfo, int userId);
    List<MediaSession.Token> getSessions(in ComponentName compName, int userId);
    MediaSession.Token getMediaKeyEventSession();
    String getMediaKeyEventSessionPackageName();
    MediaSession.Token getMediaKeyEventSession(in ComponentName compName);
    String getMediaKeyEventSessionPackageName(in ComponentName compName);
    void dispatchMediaKeyEvent(String packageName, boolean asSystemService, in KeyEvent keyEvent,
            boolean needWakeLock);
    boolean dispatchMediaKeyEventToSessionAsSystemService(String packageName,
+48 −3
Original line number Diff line number Diff line
@@ -205,7 +205,28 @@ public final class MediaSessionManager {
    @Nullable
    public MediaSession.Token getMediaKeyEventSession() {
        try {
            return mService.getMediaKeyEventSession();
            return mService.getMediaKeyEventSession(null);
        } catch (RemoteException ex) {
            Log.e(TAG, "Failed to get media key event session", ex);
        }
        return null;
    }

    /**
     * Gets the media key event session, which would receive a media key event unless specified.
     * <p>
     * This requires that your app is an enabled notificationlistener using the
     * {@link NotificationListenerService} APIs, in which case you must pass
     * the {@link ComponentName} of your enabled listener.
     *
     * @return The media key event session, which would receive key events by default, unless
     *          the caller has specified the target. Can be {@code null}.
     */
    @Nullable
    public MediaSession.Token getMediaKeyEventSession(@NonNull ComponentName notificationListener) {
        Objects.requireNonNull(notificationListener, "notificationListener shouldn't be null");
        try {
            return mService.getMediaKeyEventSession(notificationListener);
        } catch (RemoteException ex) {
            Log.e(TAG, "Failed to get media key event session", ex);
        }
@@ -224,10 +245,34 @@ public final class MediaSessionManager {
    @NonNull
    public String getMediaKeyEventSessionPackageName() {
        try {
            String packageName = mService.getMediaKeyEventSessionPackageName();
            String packageName = mService.getMediaKeyEventSessionPackageName(null);
            return (packageName != null) ? packageName : "";
        } catch (RemoteException ex) {
            Log.e(TAG, "Failed to get media key event session", ex);
            Log.e(TAG, "Failed to get media key event session package name", ex);
        }
        return "";
    }

    /**
     * Gets the package name of the media key event session.
     * <p>
     * This requires that your app is an enabled notificationlistener using the
     * {@link NotificationListenerService} APIs, in which case you must pass
     * the {@link ComponentName} of your enabled listener.
     *
     * @return The package name of the media key event session or the last session's media button
     *          receiver if the media key event session is {@code null}. Returns an empty string
     *          if neither of them exists.
     * @see #getMediaKeyEventSession(ComponentName)
     */
    @NonNull
    public String getMediaKeyEventSessionPackageName(@NonNull ComponentName notificationListener) {
        Objects.requireNonNull(notificationListener, "notificationListener shouldn't be null");
        try {
            String packageName = mService.getMediaKeyEventSessionPackageName(notificationListener);
            return (packageName != null) ? packageName : "";
        } catch (RemoteException ex) {
            Log.e(TAG, "Failed to get media key event session package name", ex);
        }
        return "";
    }
+18 −10
Original line number Diff line number Diff line
@@ -1236,15 +1236,19 @@ public class MediaSessionService extends SystemService implements Monitor {
        }

        @Override
        public MediaSession.Token getMediaKeyEventSession() {
        public MediaSession.Token getMediaKeyEventSession(ComponentName notificationListener) {
            final int pid = Binder.getCallingPid();
            final int uid = Binder.getCallingUid();
            final int userId = UserHandle.getUserHandleForUid(uid).getIdentifier();
            final UserHandle userHandle = UserHandle.getUserHandleForUid(uid);
            final int userId = userHandle.getIdentifier();
            final long token = Binder.clearCallingIdentity();
            try {
                if (!hasMediaControlPermission(pid, uid)) {
                    throw new SecurityException("MEDIA_CONTENT_CONTROL permission is required to"
                            + " get the media key event session");
                if (!hasMediaControlPermission(pid, uid)
                        && !isEnabledNotificationListener(
                                notificationListener, userHandle, userId)) {
                    throw new SecurityException("MEDIA_CONTENT_CONTROL permission or an enabled"
                            + " notification listener is required to"
                            + " get media key event session.");
                }
                MediaSessionRecordImpl record;
                synchronized (mLock) {
@@ -1267,15 +1271,19 @@ public class MediaSessionService extends SystemService implements Monitor {
        }

        @Override
        public String getMediaKeyEventSessionPackageName() {
        public String getMediaKeyEventSessionPackageName(ComponentName notificationListener) {
            final int pid = Binder.getCallingPid();
            final int uid = Binder.getCallingUid();
            final int userId = UserHandle.getUserHandleForUid(uid).getIdentifier();
            final UserHandle userHandle = UserHandle.getUserHandleForUid(uid);
            final int userId = userHandle.getIdentifier();
            final long token = Binder.clearCallingIdentity();
            try {
                if (!hasMediaControlPermission(pid, uid)) {
                    throw new SecurityException("MEDIA_CONTENT_CONTROL permission is required to"
                            + " get the media key event session package");
                if (!hasMediaControlPermission(pid, uid)
                        && !isEnabledNotificationListener(
                        notificationListener, userHandle, userId)) {
                    throw new SecurityException("MEDIA_CONTENT_CONTROL permission or an enabled"
                            + " notification listener is required to"
                            + " get media key event session package name");
                }
                MediaSessionRecordImpl record;
                synchronized (mLock) {