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

Commit 1a212544 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "[Media ML] Add button session management related classes"

parents 54212330 c4a53f5e
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -61,6 +61,7 @@ import java.util.concurrent.Executor;
 * @see MediaSession
 * @see MediaSession
 * @see MediaController
 * @see MediaController
 */
 */
// TODO: (jinpark) Add API for getting and setting session policies from MediaSessionService.
@SystemService(Context.MEDIA_SESSION_SERVICE)
@SystemService(Context.MEDIA_SESSION_SERVICE)
public final class MediaSessionManager {
public final class MediaSessionManager {
    private static final String TAG = "SessionManager";
    private static final String TAG = "SessionManager";
+39 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright 2020 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.annotation.NonNull;
import android.annotation.Nullable;
import android.media.session.MediaSession;
import android.view.KeyEvent;

/**
 * Provides a way to customize behavior for media key events.
 */
public interface MediaKeyDispatcher {
    /**
     * Implement this to customize the logic for which MediaSession should consume which key event.
     *
     * @param keyEvent a non-null KeyEvent whose key code is one of the supported media buttons.
     * @param asSystemService {@code true} if the event came from the system service via hardware
     *         devices. {@code false} if the event came from the app process through key injection.
     * @return a {@link MediaSession.Token} instance that should consume the given key event.
     */
    @Nullable
    MediaSession.Token getSessionForKeyEvent(@NonNull KeyEvent keyEvent,
            boolean asSystemService);
}
+19 −1
Original line number Original line Diff line number Diff line
@@ -50,15 +50,18 @@ public class MediaSession2Record implements MediaSessionRecordImpl {
    private final MediaSessionService mService;
    private final MediaSessionService mService;
    @GuardedBy("mLock")
    @GuardedBy("mLock")
    private boolean mIsConnected;
    private boolean mIsConnected;
    @GuardedBy("mLock")
    private int mPolicies;


    public MediaSession2Record(Session2Token sessionToken, MediaSessionService service,
    public MediaSession2Record(Session2Token sessionToken, MediaSessionService service,
            Looper handlerLooper) {
            Looper handlerLooper, int policies) {
        mSessionToken = sessionToken;
        mSessionToken = sessionToken;
        mService = service;
        mService = service;
        mHandlerExecutor = new HandlerExecutor(new Handler(handlerLooper));
        mHandlerExecutor = new HandlerExecutor(new Handler(handlerLooper));
        mController = new MediaController2.Builder(service.getContext(), sessionToken)
        mController = new MediaController2.Builder(service.getContext(), sessionToken)
                .setControllerCallback(mHandlerExecutor, new Controller2Callback())
                .setControllerCallback(mHandlerExecutor, new Controller2Callback())
                .build();
                .build();
        mPolicies = policies;
    }
    }


    @Override
    @Override
@@ -129,6 +132,21 @@ public class MediaSession2Record implements MediaSessionRecordImpl {
        return false;
        return false;
    }
    }



    @Override
    public int getSessionPolicies() {
        synchronized (mLock) {
            return mPolicies;
        }
    }

    @Override
    public void setSessionPolicies(int policies) {
        synchronized (mLock) {
            mPolicies = policies;
        }
    }

    @Override
    @Override
    public void dump(PrintWriter pw, String prefix) {
    public void dump(PrintWriter pw, String prefix) {
        pw.println(prefix + "token=" + mSessionToken);
        pw.println(prefix + "token=" + mSessionToken);
+22 −1
Original line number Original line Diff line number Diff line
@@ -159,9 +159,12 @@ public class MediaSessionRecord implements IBinder.DeathRecipient, MediaSessionR
    private long mDuration = -1;
    private long mDuration = -1;
    private String mMetadataDescription;
    private String mMetadataDescription;


    private int mPolicies;

    public MediaSessionRecord(int ownerPid, int ownerUid, int userId, String ownerPackageName,
    public MediaSessionRecord(int ownerPid, int ownerUid, int userId, String ownerPackageName,
            ISessionCallback cb, String tag, Bundle sessionInfo,
            ISessionCallback cb, String tag, Bundle sessionInfo,
            MediaSessionService service, Looper handlerLooper) throws RemoteException {
            MediaSessionService service, Looper handlerLooper, int policies)
            throws RemoteException {
        mOwnerPid = ownerPid;
        mOwnerPid = ownerPid;
        mOwnerUid = ownerUid;
        mOwnerUid = ownerUid;
        mUserId = userId;
        mUserId = userId;
@@ -178,6 +181,7 @@ public class MediaSessionRecord implements IBinder.DeathRecipient, MediaSessionR
        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        mAudioManagerInternal = LocalServices.getService(AudioManagerInternal.class);
        mAudioManagerInternal = LocalServices.getService(AudioManagerInternal.class);
        mAudioAttrs = DEFAULT_ATTRIBUTES;
        mAudioAttrs = DEFAULT_ATTRIBUTES;
        mPolicies = policies;


        // May throw RemoteException if the session app is killed.
        // May throw RemoteException if the session app is killed.
        mSessionCb.mCb.asBinder().linkToDeath(this, 0);
        mSessionCb.mCb.asBinder().linkToDeath(this, 0);
@@ -437,6 +441,20 @@ public class MediaSessionRecord implements IBinder.DeathRecipient, MediaSessionR
                cb);
                cb);
    }
    }


    @Override
    public int getSessionPolicies() {
        synchronized (mLock) {
            return mPolicies;
        }
    }

    @Override
    public void setSessionPolicies(int policies) {
        synchronized (mLock) {
            mPolicies = policies;
        }
    }

    @Override
    @Override
    public void dump(PrintWriter pw, String prefix) {
    public void dump(PrintWriter pw, String prefix) {
        pw.println(prefix + mTag + " " + this);
        pw.println(prefix + mTag + " " + this);
@@ -808,6 +826,9 @@ public class MediaSessionRecord implements IBinder.DeathRecipient, MediaSessionR


        @Override
        @Override
        public void setMediaButtonReceiver(PendingIntent pi) throws RemoteException {
        public void setMediaButtonReceiver(PendingIntent pi) throws RemoteException {
            if ((mPolicies & SessionPolicyProvider.SESSION_POLICY_IGNORE_BUTTON_RECEIVER) == 1) {
                return;
            }
            mMediaButtonReceiver = pi;
            mMediaButtonReceiver = pi;
            final long token = Binder.clearCallingIdentity();
            final long token = Binder.clearCallingIdentity();
            try {
            try {
+14 −0
Original line number Original line Diff line number Diff line
@@ -20,6 +20,8 @@ import android.media.AudioManager;
import android.os.ResultReceiver;
import android.os.ResultReceiver;
import android.view.KeyEvent;
import android.view.KeyEvent;


import com.android.server.media.SessionPolicyProvider.SessionPolicy;

import java.io.PrintWriter;
import java.io.PrintWriter;


/**
/**
@@ -127,6 +129,18 @@ public interface MediaSessionRecordImpl extends AutoCloseable {
    boolean sendMediaButton(String packageName, int pid, int uid, boolean asSystemService,
    boolean sendMediaButton(String packageName, int pid, int uid, boolean asSystemService,
            KeyEvent ke, int sequenceId, ResultReceiver cb);
            KeyEvent ke, int sequenceId, ResultReceiver cb);


    /**
     * Get session policies from custom policy provider set when MediaSessionRecord is instantiated.
     * If custom policy does not exist, will return null.
     */
    @SessionPolicy
    int getSessionPolicies();

    /**
     * Overwrite session policies that have been set when MediaSessionRecord is instantiated.
     */
    void setSessionPolicies(@SessionPolicy int policies);

    /**
    /**
     * Dumps internal state
     * Dumps internal state
     *
     *
Loading