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

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

Report MediaSession2 usages

- Note that UsageStats interaction reports don't require us to check
the existence of a notification.

Bug: 295518668
Bug: 297052684
Test: Manual playback
Change-Id: I9058b8c31e67525c3bf047841316786c1b4119e6
parent a6e3edc5
Loading
Loading
Loading
Loading
+5 −6
Original line number Diff line number Diff line
@@ -153,7 +153,8 @@ public class MediaSessionService extends SystemService implements Monitor {
    private UsageStatsManagerInternal mUsageStatsManagerInternal;

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

    // 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.
@@ -624,22 +625,20 @@ public class MediaSessionService extends SystemService implements Monitor {
    }

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

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