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

Commit 2c0acc42 authored by Pradeep Sawlani's avatar Pradeep Sawlani
Browse files

Configure temp user engaged timeout.



Adds support to configure the duration media session remains in the engaged
state after active playback via command line flag. This allows for quicker
testing and development of media app behavior.

Flag: com.android.media.flags.enable_notifying_activity_manager_with_media_session_status_change
BUG: 281762171
Test: adb root; adb shell device_config put media media_session_temp_user_engaged_duration_ms 10000; adb shell dumpsys media_session

Change-Id: I7c249c2e8c241d9b391afe1b6fe201a301b46120
Signed-off-by: default avatarPradeep Sawlani <sawlani@google.com>
parent a270408a
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ class MediaSessionDeviceConfig {
    private static volatile long sMediaSessionCallbackFgsAllowlistDurationMs =
            DEFAULT_MEDIA_SESSION_CALLBACK_FGS_ALLOWLIST_DURATION_MS;


    /**
     * Denotes the duration for which an app receiving a media session callback and the FGS started
     * there can be temporarily allowed to have while-in-use permissions such as
@@ -58,6 +59,16 @@ class MediaSessionDeviceConfig {
    private static volatile long sMediaSessionCallbackFgsWhileInUseTempAllowDurationMs =
            DEFAULT_MEDIA_SESSION_CALLBACK_FGS_WHILE_IN_USE_TEMP_ALLOW_DURATION_MS;

    /**
     * Denotes the duration (in milliseconds) that a media session can remain in an engaged state,
     * where it is only considered engaged if transitioning from active playback.
     */
    private static final String KEY_MEDIA_SESSION_TEMP_USER_ENGAGED_DURATION_MS =
            "media_session_temp_user_engaged_duration_ms";
    private static final long DEFAULT_MEDIA_SESSION_TEMP_USER_ENGAGED_DURATION_MS = 600_000;
    private static volatile long sMediaSessionTempUserEngagedDurationMs =
            DEFAULT_MEDIA_SESSION_TEMP_USER_ENGAGED_DURATION_MS;

    private static void refresh(DeviceConfig.Properties properties) {
        final Set<String> keys = properties.getKeyset();
        properties.getKeyset().forEach(key -> {
@@ -73,6 +84,11 @@ class MediaSessionDeviceConfig {
                case KEY_MEDIA_SESSION_CALLBACK_FGS_WHILE_IN_USE_TEMP_ALLOW_DURATION_MS:
                    sMediaSessionCallbackFgsWhileInUseTempAllowDurationMs = properties.getLong(key,
                            DEFAULT_MEDIA_SESSION_CALLBACK_FGS_WHILE_IN_USE_TEMP_ALLOW_DURATION_MS);
                    break;
                case KEY_MEDIA_SESSION_TEMP_USER_ENGAGED_DURATION_MS:
                    sMediaSessionTempUserEngagedDurationMs = properties.getLong(key,
                            DEFAULT_MEDIA_SESSION_TEMP_USER_ENGAGED_DURATION_MS);
                    break;
            }
        });
    }
@@ -110,6 +126,15 @@ class MediaSessionDeviceConfig {
        return sMediaSessionCallbackFgsWhileInUseTempAllowDurationMs;
    }

    /**
     * Returns the duration (in milliseconds) that a media session can remain in an engaged state,
     * where it is only considered engaged if transitioning from active playback. After this
     * duration, the session is disengaged until explicit user action triggers active playback.
     */
    public static long getMediaSessionTempUserEngagedDurationMs() {
        return sMediaSessionTempUserEngagedDurationMs;
    }

    public static void dump(PrintWriter pw, String prefix) {
        pw.println("Media session config:");
        final String dumpFormat = prefix + "  %s: [cur: %s, def: %s]";
@@ -125,5 +150,9 @@ class MediaSessionDeviceConfig {
                KEY_MEDIA_SESSION_CALLBACK_FGS_WHILE_IN_USE_TEMP_ALLOW_DURATION_MS,
                sMediaSessionCallbackFgsWhileInUseTempAllowDurationMs,
                DEFAULT_MEDIA_SESSION_CALLBACK_FGS_WHILE_IN_USE_TEMP_ALLOW_DURATION_MS));
        pw.println(TextUtils.formatSimple(dumpFormat,
                KEY_MEDIA_SESSION_TEMP_USER_ENGAGED_DURATION_MS,
                sMediaSessionTempUserEngagedDurationMs,
                DEFAULT_MEDIA_SESSION_TEMP_USER_ENGAGED_DURATION_MS));
    }
}
+4 −11
Original line number Diff line number Diff line
@@ -264,15 +264,6 @@ public class MediaSessionRecord extends MediaSessionRecordImpl implements IBinde
     */
    private static final int USER_DISENGAGED = 2;

    /**
     * Indicates the duration of the temporary engaged state, in milliseconds.
     *
     * <p>When switching to an {@linkplain PlaybackState#isActive() inactive state}, the user is
     * treated as temporarily engaged, meaning the corresponding session is only considered in an
     * engaged state for the duration of this timeout, and only if coming from an engaged state.
     */
    private static final int TEMP_USER_ENGAGED_TIMEOUT_MS = 600000;

    public MediaSessionRecord(
            int ownerPid,
            int ownerUid,
@@ -605,7 +596,8 @@ public class MediaSessionRecord extends MediaSessionRecordImpl implements IBinde
                            if (mUserEngagementState == USER_TEMPORARILY_ENGAGED) {
                                mHandler.postDelayed(
                                        mUserEngagementTimeoutExpirationRunnable,
                                        TEMP_USER_ENGAGED_TIMEOUT_MS);
                                        MediaSessionDeviceConfig
                                                .getMediaSessionTempUserEngagedDurationMs());
                            }
                        }
                    }
@@ -1079,7 +1071,8 @@ public class MediaSessionRecord extends MediaSessionRecordImpl implements IBinde
        mUserEngagementState = newUserEngagedState;
        if (newUserEngagedState == USER_TEMPORARILY_ENGAGED && !isGlobalPrioritySessionActive) {
            mHandler.postDelayed(
                    mUserEngagementTimeoutExpirationRunnable, TEMP_USER_ENGAGED_TIMEOUT_MS);
                    mUserEngagementTimeoutExpirationRunnable,
                    MediaSessionDeviceConfig.getMediaSessionTempUserEngagedDurationMs());
        } else {
            mHandler.removeCallbacks(mUserEngagementTimeoutExpirationRunnable);
        }