diff options
| author | 2024-01-29 15:38:34 +0000 | |
|---|---|---|
| committer | 2024-02-23 08:29:03 +0000 | |
| commit | cf10753501ebf203e751c9fd6e9b9857105f9f4d (patch) | |
| tree | e0528be01d83f96f3f29bb0fb234f1f9b79d4be0 | |
| parent | a46e7255379b9c8ed9534916d36bf944046636ac (diff) | |
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
4 files changed, 63 insertions, 11 deletions
diff --git a/services/core/java/com/android/server/media/MediaSession2Record.java b/services/core/java/com/android/server/media/MediaSession2Record.java index db70ce281eb5..a110e5637f82 100644 --- a/services/core/java/com/android/server/media/MediaSession2Record.java +++ b/services/core/java/com/android/server/media/MediaSession2Record.java @@ -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) @@ -98,6 +101,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 { diff --git a/services/core/java/com/android/server/media/MediaSessionRecord.java b/services/core/java/com/android/server/media/MediaSessionRecord.java index 53f780e4d19e..15527041d8eb 100644 --- a/services/core/java/com/android/server/media/MediaSessionRecord.java +++ b/services/core/java/com/android/server/media/MediaSessionRecord.java @@ -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); @@ -292,6 +303,16 @@ public class MediaSessionRecord implements IBinder.DeathRecipient, MediaSessionR } /** + * 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. * * @return Info that identifies 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 diff --git a/services/core/java/com/android/server/media/MediaSessionRecordImpl.java b/services/core/java/com/android/server/media/MediaSessionRecordImpl.java index 99c8ea93936e..e53a2dbe8101 100644 --- a/services/core/java/com/android/server/media/MediaSessionRecordImpl.java +++ b/services/core/java/com/android/server/media/MediaSessionRecordImpl.java @@ -32,6 +32,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. * * @return Info that identifies this session. diff --git a/services/core/java/com/android/server/media/MediaSessionService.java b/services/core/java/com/android/server/media/MediaSessionService.java index 757b26c45ab1..9e98a5809650 100644 --- a/services/core/java/com/android/server/media/MediaSessionService.java +++ b/services/core/java/com/android/server/media/MediaSessionService.java @@ -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); } |