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

Commit 6e95af57 authored by Sudheer Shanka's avatar Sudheer Shanka
Browse files

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
parent 3faaf538
Loading
Loading
Loading
Loading
+15 −2
Original line number Diff line number Diff line
@@ -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 "