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

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

Update user engaged sessions to handle multiple session per app case

- This is the first step for checking media notifications link with
media sessions for fullfilling requirements for app being in the
foreground.
- For simplicity, we are ignoring the cases of MediaSession2 as
Notification has no way to be linked with MediaSession2.

Bug: 295518668
Test: Manual playback
Change-Id: Ia62769215749e082c8cb7e2855852b2027f455d4
parent 01522013
Loading
Loading
Loading
Loading
+30 −23
Original line number Diff line number Diff line
@@ -105,6 +105,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
@@ -151,7 +152,9 @@ public class MediaSessionService extends SystemService implements Monitor {
    private boolean mHasFeatureLeanback;
    private ActivityManagerInternal mActivityManagerInternal;
    private UsageStatsManagerInternal mUsageStatsManagerInternal;
    private final Set<Integer> mUserEngagingSessions = new HashSet<>();

    /* Maps uid with all user engaging session tokens associated to it */
    private final Map<Integer, Set<MediaSession.Token>> mUserEngagingSessions = new HashMap<>();

    // 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.
@@ -615,24 +618,28 @@ public class MediaSessionService extends SystemService implements Monitor {
    }

    private void reportMediaInteractionEvent(MediaSessionRecordImpl record, boolean userEngaged) {
           if (!android.app.usage.Flags.userInteractionTypeApi()) {
        if (!android.app.usage.Flags.userInteractionTypeApi()
                || !(record instanceof MediaSessionRecord)) {
            return;
        }

        String packageName = record.getPackageName();
        int sessionUid = record.getUid();
        String actionToLog = null;
        MediaSession.Token token = ((MediaSessionRecord) record).getSessionToken();
        if (userEngaged) {
                if (!mUserEngagingSessions.contains(sessionUid)) {
            if (!mUserEngagingSessions.containsKey(sessionUid)) {
                mUserEngagingSessions.put(sessionUid, new HashSet<>());
                actionToLog = "start";
            }
                mUserEngagingSessions.add(sessionUid);
            } else {
                if (mUserEngagingSessions.contains(sessionUid)) {
            mUserEngagingSessions.get(sessionUid).add(token);
        } else if (mUserEngagingSessions.containsKey(sessionUid)) {
            mUserEngagingSessions.get(sessionUid).remove(token);
            if (mUserEngagingSessions.get(sessionUid).isEmpty()) {
                actionToLog = "stop";
                }
                mUserEngagingSessions.remove(sessionUid);
            }
        }

        if (actionToLog != null) {
            PersistableBundle extras = new PersistableBundle();