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

Commit 8b4bffca authored by RoboErik's avatar RoboErik
Browse files

Make MediaSession and MediaController constructors public

This makes the MediaSession/Controller constructors public and registers
with the system behind the scenes.

This also adds a bit about needing to call setActive(true) to start receiving
commands in MediaSession's docs.

Change-Id: If882d229b54c36bf0831aca0255052dda667a2bc
parent 54892c8b
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -16219,12 +16219,12 @@ package android.media.routing {
package android.media.session {
  public final class MediaController {
    ctor public MediaController(android.media.session.MediaSession.Token);
    method public void addCallback(android.media.session.MediaController.Callback);
    method public void addCallback(android.media.session.MediaController.Callback, android.os.Handler);
    method public void adjustVolumeBy(int, int);
    method public android.media.routing.MediaRouter.Delegate createMediaRouterDelegate();
    method public boolean dispatchMediaButtonEvent(android.view.KeyEvent);
    method public static android.media.session.MediaController fromToken(android.media.session.MediaSession.Token);
    method public android.media.MediaMetadata getMetadata();
    method public android.media.session.PlaybackState getPlaybackState();
    method public int getRatingType();
@@ -16264,6 +16264,7 @@ package android.media.session {
  }
  public final class MediaSession {
    ctor public MediaSession(android.content.Context, java.lang.String);
    method public void addCallback(android.media.session.MediaSession.Callback);
    method public void addCallback(android.media.session.MediaSession.Callback, android.os.Handler);
    method public void addTransportControlsCallback(android.media.session.MediaSession.TransportControlsCallback);
@@ -16315,7 +16316,6 @@ package android.media.session {
  public final class MediaSessionManager {
    method public void addActiveSessionsListener(android.media.session.MediaSessionManager.SessionListener, android.content.ComponentName);
    method public android.media.session.MediaSession createSession(java.lang.String);
    method public java.util.List<android.media.session.MediaController> getActiveSessions(android.content.ComponentName);
  }
+2 −2
Original line number Diff line number Diff line
@@ -114,7 +114,7 @@ public class Media extends BaseCommand {
            List<IBinder> sessions = mSessionService
                    .getSessions(null, ActivityManager.getCurrentUser());
            for (IBinder session : sessions) {
                MediaController controller = MediaController.fromBinder(ISessionController.Stub
                MediaController controller = new MediaController(ISessionController.Stub
                        .asInterface(session));
                if (controller != null && controller.getSessionInfo().getId().equals(id)) {
                    ControllerMonitor monitor = new ControllerMonitor(controller);
@@ -248,7 +248,7 @@ public class Media extends BaseCommand {
            List<IBinder> sessions = mSessionService
                    .getSessions(null, ActivityManager.getCurrentUser());
            for (IBinder session : sessions) {
                MediaController controller = MediaController.fromBinder(ISessionController.Stub
                MediaController controller = new MediaController(ISessionController.Stub
                        .asInterface(session));
                if (controller != null) {
                    MediaSessionInfo info = controller.getSessionInfo();
+13 −14
Original line number Diff line number Diff line
@@ -66,28 +66,27 @@ public final class MediaController {

    private final TransportControls mTransportControls;

    private MediaController(ISessionController sessionBinder) {
        mSessionBinder = sessionBinder;
        mTransportControls = new TransportControls();
    }

    /**
     * Call for creating a MediaController directly from a binder. Should only
     * be used by framework code.
     *
     * @hide
     */
    public static MediaController fromBinder(ISessionController sessionBinder) {
        return new MediaController(sessionBinder);
    public MediaController(ISessionController sessionBinder) {
        if (sessionBinder == null) {
            throw new IllegalArgumentException("Session token cannot be null");
        }
        mSessionBinder = sessionBinder;
        mTransportControls = new TransportControls();
    }

    /**
     * Get a new media controller from a session token which may have
     * been obtained from another process.  If successful the controller returned
     * will be connected to the session that generated the token.
     * Create a new MediaController from a session's token.
     *
     * @param token The session token to control.
     * @return A controller for the session or null if inaccessible.
     * @param token The token for the session.
     */
    public static MediaController fromToken(@NonNull MediaSession.Token token) {
        return fromBinder(token.getBinder());
    public MediaController(@NonNull MediaSession.Token token) {
        this(token.getBinder());
    }

    /**
+41 −9
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaMetadata;
@@ -37,10 +38,13 @@ import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.ResultReceiver;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;

import com.android.internal.telephony.DctConstants.Activity;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
@@ -59,8 +63,9 @@ import java.util.List;
 * create a {@link MediaController} to interact with the session.
 * <p>
 * To receive commands, media keys, and other events a {@link Callback} must be
 * set with {@link #addCallback(Callback)}. To receive transport control
 * commands a {@link TransportControlsCallback} must be set with
 * set with {@link #addCallback(Callback)} and {@link #setActive(boolean)
 * setActive(true)} must be called. To receive transport control commands a
 * {@link TransportControlsCallback} must be set with
 * {@link #addTransportControlsCallback}.
 * <p>
 * When an app is finished performing playback it must call {@link #release()}
@@ -119,18 +124,45 @@ public final class MediaSession {
    private boolean mActive = false;

    /**
     * Creates a new session. The session will automatically be registered with
     * the system but will not be published until {@link #setActive(boolean)
     * setActive(true)} is called. You must call {@link #release()} when
     * finished with the session.
     *
     * @param context The context to use to create the session.
     * @param tag A short name for debugging purposes.
     */
    public MediaSession(@NonNull Context context, @NonNull String tag) {
        this(context, tag, UserHandle.myUserId());
    }

    /**
     * Creates a new session as the specified user. To create a session as a
     * user other than your own you must hold the
     * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL}
     * permission.
     *
     * @param context The context to use to create the session.
     * @param tag A short name for debugging purposes.
     * @param userId The user id to create the session as.
     * @hide
     */
    public MediaSession(ISession binder, CallbackStub cbStub) {
        mBinder = binder;
        mCbStub = cbStub;
        ISessionController controllerBinder = null;
    public MediaSession(@NonNull Context context, @NonNull String tag, int userId) {
        if (context == null) {
            throw new IllegalArgumentException("context cannot be null.");
        }
        if (TextUtils.isEmpty(tag)) {
            throw new IllegalArgumentException("tag cannot be null or empty");
        }
        mCbStub = new CallbackStub();
        MediaSessionManager manager = (MediaSessionManager) context
                .getSystemService(Context.MEDIA_SESSION_SERVICE);
        try {
            controllerBinder = mBinder.getController();
            mBinder = manager.createSession(mCbStub, tag, userId);
            mSessionToken = new Token(mBinder.getController());
        } catch (RemoteException e) {
            throw new RuntimeException("Dead object in MediaSessionController constructor: ", e);
            throw new RuntimeException("Remote error creating session.", e);
        }
        mSessionToken = new Token(controllerBinder);
    }

    /**
+11 −1
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.RemoteException;
import android.util.ArrayMap;
import android.util.Log;
import android.view.KeyEvent;
@@ -46,6 +47,7 @@ public class MediaSessionLegacyHelper {
    private static final Object sLock = new Object();
    private static MediaSessionLegacyHelper sInstance;

    private Context mContext;
    private MediaSessionManager mSessionManager;
    private Handler mHandler = new Handler(Looper.getMainLooper());
    // The legacy APIs use PendingIntents to register/unregister media button
@@ -54,6 +56,7 @@ public class MediaSessionLegacyHelper {
            = new ArrayMap<PendingIntent, SessionHolder>();

    private MediaSessionLegacyHelper(Context context) {
        mContext = context;
        mSessionManager = (MediaSessionManager) context
                .getSystemService(Context.MEDIA_SESSION_SERVICE);
    }
@@ -225,6 +228,9 @@ public class MediaSessionLegacyHelper {
            return;
        }
        SessionHolder holder = getHolder(pi, true);
        if (holder == null) {
            return;
        }
        if (holder.mRccListener != null) {
            if (holder.mRccListener == listener) {
                if (DEBUG) {
@@ -270,6 +276,9 @@ public class MediaSessionLegacyHelper {
            return;
        }
        SessionHolder holder = getHolder(pi, true);
        if (holder == null) {
            return;
        }
        if (holder.mMediaButtonListener != null) {
            // Already have this listener registered, but update it anyway as
            // the extras may have changed.
@@ -316,7 +325,8 @@ public class MediaSessionLegacyHelper {
    private SessionHolder getHolder(PendingIntent pi, boolean createIfMissing) {
        SessionHolder holder = mSessions.get(pi);
        if (holder == null && createIfMissing) {
            MediaSession session = mSessionManager.createSession(TAG);
            MediaSession session;
            session = new MediaSession(mContext, TAG);
            session.setActive(true);
            holder = new SessionHolder(session, pi);
            mSessions.put(pi, holder);
Loading