From 66f8133268b63e46cb8991a1b9fe3dc473112dd4 Mon Sep 17 00:00:00 2001 From: Sudheer Shanka Date: Wed, 24 Mar 2021 07:06:16 -0700 Subject: [PATCH] Make a couple of constants in MediaSessionService configurable. As part of exempting certain use cases from FGS-from-BG restrictions, we added a couple of constants earlier to denote how long apps receiving the media button callbacks should be allowed to start FGS from BG. This change makes those constants configurable in case we decide to increase or decrease the duration later. Bug: 174699413 Test: manual Change-Id: I2e59a73202dd9067098b653e728775eb3a8530f7 --- .../media/MediaButtonReceiverHolder.java | 14 +-- .../server/media/MediaSessionConfig.java | 101 ++++++++++++++++++ .../server/media/MediaSessionService.java | 16 ++- 3 files changed, 113 insertions(+), 18 deletions(-) create mode 100644 services/core/java/com/android/server/media/MediaSessionConfig.java diff --git a/services/core/java/com/android/server/media/MediaButtonReceiverHolder.java b/services/core/java/com/android/server/media/MediaButtonReceiverHolder.java index cb0a668871e07..bb996a07ba668 100644 --- a/services/core/java/com/android/server/media/MediaButtonReceiverHolder.java +++ b/services/core/java/com/android/server/media/MediaButtonReceiverHolder.java @@ -64,13 +64,6 @@ final class MediaButtonReceiverHolder { private static final int PACKAGE_MANAGER_COMMON_FLAGS = PackageManager.MATCH_DIRECT_BOOT_AWARE | PackageManager.MATCH_DIRECT_BOOT_UNAWARE; - /** - * Denotes the duration during which a media button receiver will be exempted from - * FGS-from-BG restriction and so will be allowed to start an FGS even if it is in the - * background state while it receives a media key event. - */ - private static final long FGS_STARTS_TEMP_ALLOWLIST_DURATION_MS = 10_000; - private final int mUserId; private final PendingIntent mPendingIntent; private final ComponentName mComponentName; @@ -185,10 +178,13 @@ final class MediaButtonReceiverHolder { * Ignored if there's no valid pending intent. * @param handler handler to be used to call onFinishedListener * Ignored if there's no valid pending intent. + * @param fgsAllowlistDurationMs duration for which the media button receiver will be + * allowed to start FGS from BG. * @see PendingIntent#send(Context, int, Intent, PendingIntent.OnFinished, Handler) */ public boolean send(Context context, KeyEvent keyEvent, String callingPackageName, - int resultCode, PendingIntent.OnFinished onFinishedListener, Handler handler) { + int resultCode, PendingIntent.OnFinished onFinishedListener, Handler handler, + long fgsAllowlistDurationMs) { Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); mediaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent); @@ -196,7 +192,7 @@ final class MediaButtonReceiverHolder { mediaButtonIntent.putExtra(Intent.EXTRA_PACKAGE_NAME, callingPackageName); final BroadcastOptions options = BroadcastOptions.makeBasic(); - options.setTemporaryAppAllowlist(FGS_STARTS_TEMP_ALLOWLIST_DURATION_MS, + options.setTemporaryAppAllowlist(fgsAllowlistDurationMs, PowerWhitelistManager.TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED, PowerWhitelistManager.REASON_MEDIA_BUTTON, ""); if (mPendingIntent != null) { diff --git a/services/core/java/com/android/server/media/MediaSessionConfig.java b/services/core/java/com/android/server/media/MediaSessionConfig.java new file mode 100644 index 0000000000000..a866b0d4a38bb --- /dev/null +++ b/services/core/java/com/android/server/media/MediaSessionConfig.java @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.media; + +import android.content.Context; +import android.provider.DeviceConfig; +import android.text.TextUtils; + +import java.io.PrintWriter; +import java.util.Set; + +class MediaSessionConfig { + /** + * Denotes the duration for which a media button receiver will be exempted from + * FGS-from-BG restriction and so will be allowed to start an FGS even if it is in the + * background state while it receives a media key event. + */ + private static final String KEY_MEDIA_BUTTON_RECEIVER_FGS_ALLOWLIST_DURATION_MS = + "media_button_receiver_fgs_allowlist_duration_ms"; + private static final long DEFAULT_MEDIA_BUTTON_RECEIVER_FGS_ALLOWLIST_DURATION_MS = 10_000; + private static volatile long sMediaButtonReceiverFgsAllowlistDurationMs = + DEFAULT_MEDIA_BUTTON_RECEIVER_FGS_ALLOWLIST_DURATION_MS; + + /** + * Denotes the duration for which an app receiving a media session callback will be + * exempted from FGS-from-BG restriction and so will be allowed to start an FGS even if + * it is in the background state while it receives a media session callback. + */ + private static final String KEY_MEDIA_SESSION_CALLBACK_FGS_ALLOWLIST_DURATION_MS = + "media_session_calback_fgs_allowlist_duration_ms"; + private static final long DEFAULT_MEDIA_SESSION_CALLBACK_FGS_ALLOWLIST_DURATION_MS = 10_000; + private static volatile long sMediaSessionCallbackFgsAllowlistDurationMs = + DEFAULT_MEDIA_SESSION_CALLBACK_FGS_ALLOWLIST_DURATION_MS; + + private static void refresh(DeviceConfig.Properties properties) { + final Set keys = properties.getKeyset(); + properties.getKeyset().forEach(key -> { + switch (key) { + case KEY_MEDIA_BUTTON_RECEIVER_FGS_ALLOWLIST_DURATION_MS: + sMediaButtonReceiverFgsAllowlistDurationMs = properties.getLong(key, + DEFAULT_MEDIA_BUTTON_RECEIVER_FGS_ALLOWLIST_DURATION_MS); + break; + case KEY_MEDIA_SESSION_CALLBACK_FGS_ALLOWLIST_DURATION_MS: + sMediaSessionCallbackFgsAllowlistDurationMs = properties.getLong(key, + DEFAULT_MEDIA_SESSION_CALLBACK_FGS_ALLOWLIST_DURATION_MS); + break; + } + }); + } + + public static void initialize(Context context) { + DeviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_MEDIA, + context.getMainExecutor(), properties -> refresh(properties)); + refresh(DeviceConfig.getProperties(DeviceConfig.NAMESPACE_MEDIA)); + } + + /** + * Returns the duration for which a media button receiver will be exempted from + * FGS-from-BG restriction and so will be allowed to start an FGS even if it is in the + * background state while it receives a media key event. + */ + public static long getMediaButtonReceiverFgsAllowlistDurationMs() { + return sMediaButtonReceiverFgsAllowlistDurationMs; + } + + /** + * Returns the duration for which an app receiving a media session callback will be + * exempted from FGS-from-BG restriction and so will be allowed to start an FGS even if + * it is in the background state while it receives a media session callback. + */ + public static long getMediaSessionCallbackFgsAllowlistDurationMs() { + return sMediaSessionCallbackFgsAllowlistDurationMs; + } + + public static void dump(PrintWriter pw, String prefix) { + pw.println("Media session config:"); + final String dumpFormat = prefix + " %s: [cur: %s, def: %s]"; + pw.println(TextUtils.formatSimple(dumpFormat, + KEY_MEDIA_BUTTON_RECEIVER_FGS_ALLOWLIST_DURATION_MS, + sMediaButtonReceiverFgsAllowlistDurationMs, + DEFAULT_MEDIA_BUTTON_RECEIVER_FGS_ALLOWLIST_DURATION_MS)); + pw.println(TextUtils.formatSimple(dumpFormat, + KEY_MEDIA_SESSION_CALLBACK_FGS_ALLOWLIST_DURATION_MS, + sMediaSessionCallbackFgsAllowlistDurationMs, + DEFAULT_MEDIA_SESSION_CALLBACK_FGS_ALLOWLIST_DURATION_MS)); + } +} diff --git a/services/core/java/com/android/server/media/MediaSessionService.java b/services/core/java/com/android/server/media/MediaSessionService.java index db2e9085bdb3a..cdfd7aeb5b6dc 100644 --- a/services/core/java/com/android/server/media/MediaSessionService.java +++ b/services/core/java/com/android/server/media/MediaSessionService.java @@ -117,13 +117,6 @@ public class MediaSessionService extends SystemService implements Monitor { */ private static final String MEDIA_BUTTON_RECEIVER = "media_button_receiver"; - /** - * Denotes the duration during which an app receiving a media session callback will be - * exempted from FGS-from-BG restriction and so will be allowed to start an FGS even if it is - * in the background state while it receives a media session callback. - */ - private static final long FGS_STARTS_TEMP_ALLOWLIST_DURATION_MS = 10_000; - private final Context mContext; private final SessionManagerImpl mSessionManagerImpl; private final MessageHandler mHandler = new MessageHandler(); @@ -244,6 +237,9 @@ public class MediaSessionService extends SystemService implements Monitor { mCommunicationManager.registerSessionCallback(new HandlerExecutor(mHandler), mSession2TokenCallback); break; + case PHASE_ACTIVITY_MANAGER_READY: + MediaSessionConfig.initialize(mContext); + break; } } @@ -564,7 +560,7 @@ public class MediaSessionService extends SystemService implements Monitor { PowerExemptionManager.class); powerExemptionManager.addToTemporaryAllowList(targetPackage, PowerExemptionManager.REASON_MEDIA_SESSION_CALLBACK, reason, - FGS_STARTS_TEMP_ALLOWLIST_DURATION_MS); + MediaSessionConfig.getMediaSessionCallbackFgsAllowlistDurationMs()); } } finally { Binder.restoreCallingIdentity(token); @@ -1915,6 +1911,7 @@ public class MediaSessionService extends SystemService implements Monitor { } mAudioPlayerStateMonitor.dump(mContext, pw, ""); } + MediaSessionConfig.dump(pw, ""); } /** @@ -2198,7 +2195,8 @@ public class MediaSessionService extends SystemService implements Monitor { boolean sent = mediaButtonReceiverHolder.send( mContext, keyEvent, callingPackageName, needWakeLock ? mKeyEventReceiver.mLastTimeoutId : -1, mKeyEventReceiver, - mHandler); + mHandler, + MediaSessionConfig.getMediaButtonReceiverFgsAllowlistDurationMs()); if (sent) { String pkgName = mediaButtonReceiverHolder.getPackageName(); for (FullUserRecord.OnMediaKeyEventDispatchedListenerRecord cr