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

Commit a4b052df authored by Iván Budnik's avatar Iván Budnik Committed by Automerger Merge Worker
Browse files

Merge "Validate ComponentName for MediaButtonBroadcastReceiver" into sc-dev...

Merge "Validate ComponentName for MediaButtonBroadcastReceiver" into sc-dev am: 3a186d92 am: eabbae01 am: 6a3ea2b6 am: cf672a9a am: 041dde32 am: 59bad085

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/23352460



Change-Id: I8e607dd28653e460b5d23dfd96320ab699c6fa64
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 1c8e8cd8 59bad085
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -297,9 +297,11 @@ public final class MediaSession {
     * class that should receive media buttons. This allows restarting playback after the session
     * has been stopped. If your app is started in this way an {@link Intent#ACTION_MEDIA_BUTTON}
     * intent will be sent to the broadcast receiver.
     * <p>
     * Note: The given {@link android.content.BroadcastReceiver} should belong to the same package
     * as the context that was given when creating {@link MediaSession}.
     *
     * <p>Note: The given {@link android.content.BroadcastReceiver} should belong to the same
     * package as the context that was given when creating {@link MediaSession}.
     *
     * <p>Calls with invalid or non-existent receivers will be ignored.
     *
     * @param broadcastReceiver the component name of the BroadcastReceiver class
     */
+33 −0
Original line number Diff line number Diff line
@@ -16,14 +16,19 @@

package com.android.server.media;

import android.Manifest;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ParceledListSlice;
import android.content.pm.ResolveInfo;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.AudioSystem;
@@ -54,6 +59,7 @@ import android.os.Process;
import android.os.RemoteException;
import android.os.ResultReceiver;
import android.os.SystemClock;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.EventLog;
import android.util.Log;
@@ -895,6 +901,22 @@ public class MediaSessionRecord implements IBinder.DeathRecipient, MediaSessionR
        }
    };

    @RequiresPermission(Manifest.permission.INTERACT_ACROSS_USERS)
    private static boolean componentNameExists(
            @NonNull ComponentName componentName, @NonNull Context context, int userId) {
        Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        mediaButtonIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        mediaButtonIntent.setComponent(componentName);

        UserHandle userHandle = UserHandle.of(userId);
        PackageManager pm = context.getPackageManager();

        List<ResolveInfo> resolveInfos =
                pm.queryBroadcastReceiversAsUser(
                        mediaButtonIntent, /* flags */ 0, userHandle);
        return !resolveInfos.isEmpty();
    }

    private final class SessionStub extends ISession.Stub {
        @Override
        public void destroySession() throws RemoteException {
@@ -965,6 +987,7 @@ public class MediaSessionRecord implements IBinder.DeathRecipient, MediaSessionR
        }

        @Override
        @RequiresPermission(Manifest.permission.INTERACT_ACROSS_USERS)
        public void setMediaButtonBroadcastReceiver(ComponentName receiver) throws RemoteException {
            final long token = Binder.clearCallingIdentity();
            try {
@@ -980,6 +1003,16 @@ public class MediaSessionRecord implements IBinder.DeathRecipient, MediaSessionR
                        != 0) {
                    return;
                }

                if (!componentNameExists(receiver, mContext, mUserId)) {
                    Log.w(
                            TAG,
                            "setMediaButtonBroadcastReceiver(): "
                                    + "Ignoring invalid component name="
                                    + receiver);
                    return;
                }

                mMediaButtonReceiverHolder = MediaButtonReceiverHolder.create(mUserId, receiver);
                mService.onMediaButtonReceiverChanged(MediaSessionRecord.this);
            } finally {