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

Commit 7096685f authored by tonihei's avatar tonihei Committed by Julia Bibik
Browse files

Inform ActivityManager of initially inactive sessions

When posting a new notification, we currently only inform
about already active sessions, but not when the session is
already inactive.

There is also no need to track explicitly which sessions
were already reported as active as we can just re-report
the active or inactive state as often as we want. This
simplifies some of the state tracking.

Bug: 385075120
Flag: com.android.media.flags.enable_notifying_activity_manager_with_media_session_status_change
Test: manually verified + atest ActivityManagerFgsDelegateTest
Change-Id: I96f207342403075c3e8459331e7360516be1849a
parent ecfc25b7
Loading
Loading
Loading
Loading
+32 −25
Original line number Diff line number Diff line
@@ -196,16 +196,6 @@ public class MediaSessionService extends SystemService implements Monitor {
    @GuardedBy("mLock")
    private final Map<Integer, Set<StatusBarNotification>> mMediaNotifications = new HashMap<>();

    /**
     * Holds all {@link MediaSessionRecordImpl} which we've reported as being {@link
     * ActivityManagerInternal#startForegroundServiceDelegate user engaged}.
     *
     * <p>This map simply prevents invoking {@link
     * ActivityManagerInternal#startForegroundServiceDelegate} more than once per session.
     */
    @GuardedBy("mLock")
    private final Set<MediaSessionRecordImpl> mFgsAllowedMediaSessionRecords = new HashSet<>();

    // 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;
@@ -759,9 +749,6 @@ public class MediaSessionService extends SystemService implements Monitor {
    @GuardedBy("mLock")
    private void setFgsActiveLocked(MediaSessionRecordImpl mediaSessionRecord,
            StatusBarNotification sbn) {
        if (!mFgsAllowedMediaSessionRecords.add(mediaSessionRecord)) {
            return; // This record already is FGS-activated.
        }
        final long token = Binder.clearCallingIdentity();
        try {
            final String packageName = sbn.getPackageName();
@@ -826,10 +813,6 @@ public class MediaSessionService extends SystemService implements Monitor {
    @GuardedBy("mLock")
    private void setFgsInactiveLocked(MediaSessionRecordImpl mediaSessionRecord,
            StatusBarNotification sbn) {
        if (!mFgsAllowedMediaSessionRecords.remove(mediaSessionRecord)) {
            return; // This record is not FGS-active. No need to set inactive.
        }

        final long token = Binder.clearCallingIdentity();
        try {
            final String packageName = sbn.getPackageName();
@@ -3273,6 +3256,7 @@ public class MediaSessionService extends SystemService implements Monitor {
        public void onNotificationPosted(StatusBarNotification sbn) {
            super.onNotificationPosted(sbn);
            int uid = sbn.getUid();
            int userId = sbn.getUser().getIdentifier();
            final Notification postedNotification = sbn.getNotification();
            if (!postedNotification.isMediaNotification()) {
                return;
@@ -3280,12 +3264,16 @@ public class MediaSessionService extends SystemService implements Monitor {
            synchronized (mLock) {
                mMediaNotifications.putIfAbsent(uid, new HashSet<>());
                mMediaNotifications.get(uid).add(sbn);
                for (MediaSessionRecordImpl mediaSessionRecord :
                        mUserEngagedSessionsForFgs.getOrDefault(uid, Set.of())) {
                    if (mediaSessionRecord.isLinkedToNotification(postedNotification)) {
                        setFgsActiveLocked(mediaSessionRecord, sbn);
                MediaSessionRecordImpl userEngagedRecord =
                        getUserEngagedMediaSessionRecordForNotification(uid, postedNotification);
                if (userEngagedRecord != null) {
                    setFgsActiveLocked(userEngagedRecord, sbn);
                    return;
                }
                MediaSessionRecordImpl notificationRecord =
                        getAnyMediaSessionRecordForNotification(uid, userId, postedNotification);
                if (notificationRecord != null) {
                    setFgsInactiveIfNoSessionIsLinkedToNotification(notificationRecord);
                }
            }
        }
@@ -3308,7 +3296,7 @@ public class MediaSessionService extends SystemService implements Monitor {
                }

                MediaSessionRecordImpl notificationRecord =
                        getLinkedMediaSessionRecord(uid, removedNotification);
                        getUserEngagedMediaSessionRecordForNotification(uid, removedNotification);

                if (notificationRecord == null) {
                    return;
@@ -3317,7 +3305,7 @@ public class MediaSessionService extends SystemService implements Monitor {
            }
        }

        private MediaSessionRecordImpl getLinkedMediaSessionRecord(
        private MediaSessionRecordImpl getUserEngagedMediaSessionRecordForNotification(
                int uid, Notification notification) {
            synchronized (mLock) {
                for (MediaSessionRecordImpl mediaSessionRecord :
@@ -3329,5 +3317,24 @@ public class MediaSessionService extends SystemService implements Monitor {
            }
            return null;
        }

        private MediaSessionRecordImpl getAnyMediaSessionRecordForNotification(
                int uid, int userId, Notification notification) {
            synchronized (mLock) {
                FullUserRecord userRecord = getFullUserRecordLocked(userId);
                if (userRecord == null) {
                    return null;
                }
                List<MediaSessionRecord> allUserSessions =
                        userRecord.mPriorityStack.getPriorityList(/* activeOnly= */ false, userId);
                for (MediaSessionRecordImpl mediaSessionRecord : allUserSessions) {
                    if (mediaSessionRecord.getUid() == uid
                            && mediaSessionRecord.isLinkedToNotification(notification)) {
                        return mediaSessionRecord;
                    }
                }
            }
            return null;
        }
    }
}