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

Commit 7709fbf3 authored by Bishoy Gendy's avatar Bishoy Gendy
Browse files

Add pid to MediaSession2Record

Bug: 318745416
Test: mp droid
Change-Id: I687a97b2bfa636725997cc6f94e21352742c35a5
parent 60a6ed0e
Loading
Loading
Loading
Loading
+30 −4
Original line number Diff line number Diff line
@@ -55,8 +55,15 @@ public class MediaSession2Record implements MediaSessionRecordImpl {
    @GuardedBy("mLock")
    private boolean mIsClosed;

    public MediaSession2Record(Session2Token sessionToken, MediaSessionService service,
            Looper handlerLooper, int policies) {
    private final int mPid;
    private final ForegroundServiceDelegationOptions mForegroundServiceDelegationOptions;

    public MediaSession2Record(
            Session2Token sessionToken,
            MediaSessionService service,
            Looper handlerLooper,
            int pid,
            int policies) {
        // The lock is required to prevent `Controller2Callback` from using partially initialized
        // `MediaSession2Record.this`.
        synchronized (mLock) {
@@ -66,7 +73,27 @@ public class MediaSession2Record implements MediaSessionRecordImpl {
            mController = new MediaController2.Builder(service.getContext(), sessionToken)
                    .setControllerCallback(mHandlerExecutor, new Controller2Callback())
                    .build();
            mPid = pid;
            mPolicies = policies;
            mForegroundServiceDelegationOptions =
                    new ForegroundServiceDelegationOptions.Builder()
                            .setClientPid(mPid)
                            .setClientUid(getUid())
                            .setClientPackageName(getPackageName())
                            .setClientAppThread(null)
                            .setSticky(false)
                            .setClientInstanceName(
                                    "MediaSessionFgsDelegate_"
                                            + getUid()
                                            + "_"
                                            + mPid
                                            + "_"
                                            + getPackageName())
                            .setForegroundServiceTypes(0)
                            .setDelegationService(
                                    ForegroundServiceDelegationOptions
                                            .DELEGATION_SERVICE_MEDIA_PLAYBACK)
                            .build();
        }
    }

@@ -91,8 +118,7 @@ public class MediaSession2Record implements MediaSessionRecordImpl {

    @Override
    public ForegroundServiceDelegationOptions getForegroundServiceDelegationOptions() {
        // TODO: Implement when MediaSession2 knows about its owner pid.
        return null;
        return mForegroundServiceDelegationOptions;
    }

    @Override
+19 −3
Original line number Diff line number Diff line
@@ -171,12 +171,27 @@ public class MediaSessionService extends SystemService implements Monitor {
    private final MediaCommunicationManager.SessionCallback mSession2TokenCallback =
            new MediaCommunicationManager.SessionCallback() {
                @Override
                // TODO (b/324266224): Deprecate this method once other overload is published.
                public void onSession2TokenCreated(Session2Token token) {
                    addSession(token, Process.INVALID_PID);
                }

                @Override
                public void onSession2TokenCreated(Session2Token token, int pid) {
                    addSession(token, pid);
                }

                private void addSession(Session2Token token, int pid) {
                    if (DEBUG) {
                        Log.d(TAG, "Session2 is created " + token);
                    }
                    MediaSession2Record record = new MediaSession2Record(token,
                            MediaSessionService.this, mRecordThread.getLooper(), 0);
                    MediaSession2Record record =
                            new MediaSession2Record(
                                    token,
                                    MediaSessionService.this,
                                    mRecordThread.getLooper(),
                                    pid,
                                    /* policies= */ 0);
                    synchronized (mLock) {
                        FullUserRecord user = getFullUserRecordLocked(record.getUserId());
                        if (user != null) {
@@ -583,7 +598,8 @@ public class MediaSessionService extends SystemService implements Monitor {
        }
        ForegroundServiceDelegationOptions foregroundServiceDelegationOptions =
                record.getForegroundServiceDelegationOptions();
        if (foregroundServiceDelegationOptions == null) {
        if (foregroundServiceDelegationOptions == null
                || foregroundServiceDelegationOptions.mClientPid == Process.INVALID_PID) {
            // This record doesn't support FGS delegation. In practice, this is MediaSession2.
            return;
        }