From 6e95af57aab824a57567e44e1581fc3d4fa49095 Mon Sep 17 00:00:00 2001 From: Sudheer Shanka Date: Tue, 2 Feb 2021 05:23:23 +0000 Subject: [PATCH] Allow apps receiving media button events to start FGS from BG. Bug: 174699413 Test: 1. Verified that media button receiver can start FGS when it is started due to user pressing play/pause button on headset. 2. Verified the same after rebooting the device. Change-Id: If6ccaa7af68aa3f3d1034db33e9fbafff9eb7669 --- .../server/media/MediaButtonReceiverHolder.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/services/core/java/com/android/server/media/MediaButtonReceiverHolder.java b/services/core/java/com/android/server/media/MediaButtonReceiverHolder.java index 0a4d17f20aec3..292239caa62c5 100644 --- a/services/core/java/com/android/server/media/MediaButtonReceiverHolder.java +++ b/services/core/java/com/android/server/media/MediaButtonReceiverHolder.java @@ -18,6 +18,7 @@ package com.android.server.media; import android.annotation.IntDef; import android.annotation.NonNull; +import android.app.BroadcastOptions; import android.app.PendingIntent; import android.content.ComponentName; import android.content.Context; @@ -59,6 +60,13 @@ final class MediaButtonReceiverHolder { private static final boolean DEBUG_KEY_EVENT = MediaSessionService.DEBUG_KEY_EVENT; private static final String COMPONENT_NAME_USER_ID_DELIM = ","; + /** + * 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; @@ -196,6 +204,9 @@ final class MediaButtonReceiverHolder { // TODO: Find a way to also send PID/UID in secure way. mediaButtonIntent.putExtra(Intent.EXTRA_PACKAGE_NAME, callingPackageName); + final BroadcastOptions options = BroadcastOptions.makeBasic(); + options.setTemporaryAppWhitelistDuration( + FGS_STARTS_TEMP_ALLOWLIST_DURATION_MS); if (mPendingIntent != null) { if (DEBUG_KEY_EVENT) { Log.d(TAG, "Sending " + keyEvent + " to the last known PendingIntent " @@ -203,7 +214,8 @@ final class MediaButtonReceiverHolder { } try { mPendingIntent.send( - context, resultCode, mediaButtonIntent, onFinishedListener, handler); + context, resultCode, mediaButtonIntent, onFinishedListener, handler, + /* requiredPermission= */ null, options.toBundle()); } catch (PendingIntent.CanceledException e) { Log.w(TAG, "Error sending key event to media button receiver " + mPendingIntent, e); return false; @@ -226,7 +238,8 @@ final class MediaButtonReceiverHolder { break; default: // Legacy behavior for other cases. - context.sendBroadcastAsUser(mediaButtonIntent, userHandle); + context.sendBroadcastAsUser(mediaButtonIntent, userHandle, + /* receiverPermission= */ null, options.toBundle()); } } catch (Exception e) { Log.w(TAG, "Error sending media button to the restored intent "