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

Commit cf107535 authored by Bishoy Gendy's avatar Bishoy Gendy
Browse files

Add Unique Identifier for MediaSessionRecord(s)

- This is part of the work done for b/295518668 and b/297052684 we need
to differentiate between MediaSessionRecords owned by the same app.

- For example an app should be allowed to go to the foreground if it
has at least an active media session in a user-engaged state.

- In case an app has 2 active media sessions that are in a user-engaged
states, moving one of them to user-not-engaged state shouldn't prevent
the app from being in the foreground because of the other session.

Bug: 322776229
Test: adb shell dumpsys media_session
Change-Id: I0958d9b955468757a94eb82c53fde29eb243c13b
parent a46e7255
Loading
Loading
Loading
Loading
+13 −3
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ public class MediaSession2Record implements MediaSessionRecordImpl {
    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
    private final Object mLock = new Object();

    private final int mUniqueId;
    @GuardedBy("mLock")
    private final Session2Token mSessionToken;
    @GuardedBy("mLock")
@@ -63,11 +64,13 @@ public class MediaSession2Record implements MediaSessionRecordImpl {
            MediaSessionService service,
            Looper handlerLooper,
            int pid,
            int policies) {
            int policies,
            int uniqueId) {
        // The lock is required to prevent `Controller2Callback` from using partially initialized
        // `MediaSession2Record.this`.
        synchronized (mLock) {
            mSessionToken = sessionToken;
            mUniqueId = uniqueId;
            mService = service;
            mHandlerExecutor = new HandlerExecutor(new Handler(handlerLooper));
            mController = new MediaController2.Builder(service.getContext(), sessionToken)
@@ -97,6 +100,13 @@ public class MediaSession2Record implements MediaSessionRecordImpl {
        }
    }

    @Override
    public int getUniqueId() {
        synchronized (mLock) {
            return mUniqueId;
        }
    }

    @Override
    public String getPackageName() {
        return mSessionToken.getPackageName();
@@ -200,6 +210,7 @@ public class MediaSession2Record implements MediaSessionRecordImpl {

    @Override
    public void dump(PrintWriter pw, String prefix) {
        pw.println(prefix + "uniqueId=" + mUniqueId);
        pw.println(prefix + "token=" + mSessionToken);
        pw.println(prefix + "controller=" + mController);

@@ -209,8 +220,7 @@ public class MediaSession2Record implements MediaSessionRecordImpl {

    @Override
    public String toString() {
        // TODO(jaewan): Also add getId().
        return getPackageName() + " (userId=" + getUserId() + ")";
        return getPackageName() + "/" + mUniqueId + " (userId=" + getUserId() + ")";
    }

    private class Controller2Callback extends MediaController2.ControllerCallback {
+25 −4
Original line number Diff line number Diff line
@@ -173,6 +173,7 @@ public class MediaSessionRecord implements IBinder.DeathRecipient, MediaSessionR
    private final int mUserId;
    private final String mPackageName;
    private final String mTag;
    private final int mUniqueId;
    private final Bundle mSessionInfo;
    private final ControllerStub mController;
    private final MediaSession.Token mSessionToken;
@@ -223,15 +224,25 @@ public class MediaSessionRecord implements IBinder.DeathRecipient, MediaSessionR

    private int mPolicies;

    public MediaSessionRecord(int ownerPid, int ownerUid, int userId, String ownerPackageName,
            ISessionCallback cb, String tag, Bundle sessionInfo,
            MediaSessionService service, Looper handlerLooper, int policies)
    public MediaSessionRecord(
            int ownerPid,
            int ownerUid,
            int userId,
            String ownerPackageName,
            ISessionCallback cb,
            String tag,
            int uniqueId,
            Bundle sessionInfo,
            MediaSessionService service,
            Looper handlerLooper,
            int policies)
            throws RemoteException {
        mOwnerPid = ownerPid;
        mOwnerUid = ownerUid;
        mUserId = userId;
        mPackageName = ownerPackageName;
        mTag = tag;
        mUniqueId = uniqueId;
        mSessionInfo = sessionInfo;
        mController = new ControllerStub();
        mSessionToken = new MediaSession.Token(ownerUid, mController);
@@ -291,6 +302,16 @@ public class MediaSessionRecord implements IBinder.DeathRecipient, MediaSessionR
        return mSessionToken;
    }

    /**
     * Get the unique id of this session record.
     *
     * @return a unique id of this session record.
     */
    @Override
    public int getUniqueId() {
        return mUniqueId;
    }

    /**
     * Get the info for this session.
     *
@@ -703,7 +724,7 @@ public class MediaSessionRecord implements IBinder.DeathRecipient, MediaSessionR

    @Override
    public String toString() {
        return mPackageName + "/" + mTag + " (userId=" + mUserId + ")";
        return mPackageName + "/" + mTag + "/" + mUniqueId + " (userId=" + mUserId + ")";
    }

    @Override
+7 −0
Original line number Diff line number Diff line
@@ -31,6 +31,13 @@ import java.io.PrintWriter;
 */
public interface MediaSessionRecordImpl extends AutoCloseable {

    /**
     * Get the unique id of this session record.
     *
     * @return a unique id of this session record.
     */
    int getUniqueId();

    /**
     * Get the info for this session.
     *
+18 −4
Original line number Diff line number Diff line
@@ -106,6 +106,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * System implementation of MediaSessionManager
@@ -155,6 +156,8 @@ public class MediaSessionService extends SystemService implements Monitor {
    /* Maps uid with all user engaging session tokens associated to it */
    private final SparseArray<Set<MediaSession.Token>> mUserEngagingSessions = new SparseArray<>();

    private final AtomicInteger mNextMediaSessionRecordId = new AtomicInteger(1);

    // The FullUserRecord of the current users. (i.e. The foreground user that isn't a profile)
    // It's always not null after the MediaSessionService is started.
    private FullUserRecord mCurrentFullUserRecord;
@@ -193,7 +196,8 @@ public class MediaSessionService extends SystemService implements Monitor {
                                    MediaSessionService.this,
                                    mRecordThread.getLooper(),
                                    pid,
                                    /* policies= */ 0);
                                    /* policies= */ 0,
                                    /* uniqueId= */ mNextMediaSessionRecordId.getAndIncrement());
                    synchronized (mLock) {
                        FullUserRecord user = getFullUserRecordLocked(record.getUserId());
                        if (user != null) {
@@ -794,9 +798,19 @@ public class MediaSessionService extends SystemService implements Monitor {

            final MediaSessionRecord session;
            try {
                session = new MediaSessionRecord(callerPid, callerUid, userId,
                        callerPackageName, cb, tag, sessionInfo, this,
                        mRecordThread.getLooper(), policies);
                session =
                        new MediaSessionRecord(
                                callerPid,
                                callerUid,
                                userId,
                                callerPackageName,
                                cb,
                                tag,
                                /* uniqueId= */ mNextMediaSessionRecordId.getAndIncrement(),
                                sessionInfo,
                                this,
                                mRecordThread.getLooper(),
                                policies);
            } catch (RemoteException e) {
                throw new RuntimeException("Media Session owner died prematurely.", e);
            }