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

Commit 5287fb01 authored by Bishoy Gendy's avatar Bishoy Gendy
Browse files

Allow session to be in foreground if active and in allowed state

Bug: 295518668
Test: atest cts/tests/app/src/android/app/cts/ActivityManagerFgsDelegateTest.java#testFgsDelegateFromMediaSession
Change-Id: I279010ca1bbac94d223a9956ad35b0a91ba91fda
parent f3241dbf
Loading
Loading
Loading
Loading
+0 −22
Original line number Diff line number Diff line
@@ -524,28 +524,6 @@ public final class PlaybackState implements Parcelable {
        return false;
    }

    /**
     * Returns whether the service holding the media session should run in the foreground when the
     * media session has this playback state or not.
     *
     * @hide
     */
    public boolean shouldAllowServiceToRunInForeground() {
        switch (mState) {
            case PlaybackState.STATE_PLAYING:
            case PlaybackState.STATE_FAST_FORWARDING:
            case PlaybackState.STATE_REWINDING:
            case PlaybackState.STATE_BUFFERING:
            case PlaybackState.STATE_CONNECTING:
            case PlaybackState.STATE_SKIPPING_TO_PREVIOUS:
            case PlaybackState.STATE_SKIPPING_TO_NEXT:
            case PlaybackState.STATE_SKIPPING_TO_QUEUE_ITEM:
                return true;
            default:
                return false;
        }
    }

    public static final @android.annotation.NonNull Parcelable.Creator<PlaybackState> CREATOR =
            new Parcelable.Creator<PlaybackState>() {
        @Override
+6 −1
Original line number Diff line number Diff line
@@ -198,7 +198,12 @@ public class MediaSession2Record implements MediaSessionRecordImpl {
                mIsConnected = true;
                service = mService;
            }
            service.onSessionActiveStateChanged(MediaSession2Record.this);

            // TODO (b/318745416): Add support for FGS in MediaSession2. Passing a
            // null playback state means the owning process will not be allowed to
            // run in the foreground.
            service.onSessionActiveStateChanged(MediaSession2Record.this,
                    /* playbackState= */ null);
        }

        @Override
+1 −1
Original line number Diff line number Diff line
@@ -1144,7 +1144,7 @@ public class MediaSessionRecord implements IBinder.DeathRecipient, MediaSessionR
            mIsActive = active;
            long token = Binder.clearCallingIdentity();
            try {
                mService.onSessionActiveStateChanged(MediaSessionRecord.this);
                mService.onSessionActiveStateChanged(MediaSessionRecord.this, mPlaybackState);
            } finally {
                Binder.restoreCallingIdentity(token);
            }
+8 −3
Original line number Diff line number Diff line
@@ -260,7 +260,8 @@ public class MediaSessionService extends SystemService implements Monitor {
        return mGlobalPrioritySession != null && mGlobalPrioritySession.isActive();
    }

    void onSessionActiveStateChanged(MediaSessionRecordImpl record) {
    void onSessionActiveStateChanged(
            MediaSessionRecordImpl record, @Nullable PlaybackState playbackState) {
        synchronized (mLock) {
            FullUserRecord user = getFullUserRecordLocked(record.getUserId());
            if (user == null) {
@@ -287,7 +288,9 @@ public class MediaSessionService extends SystemService implements Monitor {
                user.mPriorityStack.onSessionActiveStateChanged(record);
            }
            setForegroundServiceAllowance(
                    record, /* allowRunningInForeground= */ record.isActive());
                    record,
                    /* allowRunningInForeground= */ record.isActive()
                            && (playbackState == null || playbackState.isActive()));
            mHandler.postSessionsChanged(record);
        }
    }
@@ -386,7 +389,9 @@ public class MediaSessionService extends SystemService implements Monitor {
            user.mPriorityStack.onPlaybackStateChanged(record, shouldUpdatePriority);
            if (playbackState != null) {
                setForegroundServiceAllowance(
                        record, playbackState.shouldAllowServiceToRunInForeground());
                        record,
                        /* allowRunningInForeground= */ playbackState.isActive()
                                && record.isActive());
            }
        }
    }