diff options
9 files changed, 80 insertions, 65 deletions
diff --git a/api/current.txt b/api/current.txt index a6b6793..5abb0d2 100644 --- a/api/current.txt +++ b/api/current.txt @@ -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); } diff --git a/cmds/media/src/com/android/commands/media/Media.java b/cmds/media/src/com/android/commands/media/Media.java index 800b925..c771f65 100644 --- a/cmds/media/src/com/android/commands/media/Media.java +++ b/cmds/media/src/com/android/commands/media/Media.java @@ -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(); diff --git a/media/java/android/media/session/MediaController.java b/media/java/android/media/session/MediaController.java index cc8b31a..ed45a08 100644 --- a/media/java/android/media/session/MediaController.java +++ b/media/java/android/media/session/MediaController.java @@ -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()); } /** diff --git a/media/java/android/media/session/MediaSession.java b/media/java/android/media/session/MediaSession.java index 34997bd..cdb61e1 100644 --- a/media/java/android/media/session/MediaSession.java +++ b/media/java/android/media/session/MediaSession.java @@ -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); } /** diff --git a/media/java/android/media/session/MediaSessionLegacyHelper.java b/media/java/android/media/session/MediaSessionLegacyHelper.java index 11f7720..5af793f 100644 --- a/media/java/android/media/session/MediaSessionLegacyHelper.java +++ b/media/java/android/media/session/MediaSessionLegacyHelper.java @@ -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); diff --git a/media/java/android/media/session/MediaSessionManager.java b/media/java/android/media/session/MediaSessionManager.java index c477406..12013b6 100644 --- a/media/java/android/media/session/MediaSessionManager.java +++ b/media/java/android/media/session/MediaSessionManager.java @@ -64,42 +64,15 @@ public final class MediaSessionManager { } /** - * Creates a new session. + * Create a new session in the system and get the binder for it. * * @param tag A short name for debugging purposes. - * @return A {@link MediaSession} for the new session. - */ - public @NonNull MediaSession createSession(@NonNull String tag) { - return createSessionAsUser(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 tag A short name for debugging purposes. - * @param userId The user id to create the session as. - * @return A {@link MediaSession} for the new session. + * @return The binder object from the system * @hide */ - public @NonNull MediaSession createSessionAsUser(@NonNull String tag, int userId) { - if (TextUtils.isEmpty(tag)) { - throw new IllegalArgumentException("tag must not be null or empty"); - } - - try { - MediaSession.CallbackStub cbStub = new MediaSession.CallbackStub(); - MediaSession session = new MediaSession(mService - .createSession(mContext.getPackageName(), cbStub, tag, userId), cbStub); - cbStub.setMediaSession(session); - - return session; - } catch (RemoteException e) { - Log.e(TAG, "Failed to create session: ", e); - return null; - } + public @NonNull ISession createSession(@NonNull MediaSession.CallbackStub cbStub, + @NonNull String tag, int userId) throws RemoteException { + return mService.createSession(mContext.getPackageName(), cbStub, tag, userId); } /** @@ -142,7 +115,7 @@ public final class MediaSessionManager { List<IBinder> binders = mService.getSessions(notificationListener, userId); int size = binders.size(); for (int i = 0; i < size; i++) { - MediaController controller = MediaController.fromBinder(ISessionController.Stub + MediaController controller = new MediaController(ISessionController.Stub .asInterface(binders.get(i))); controllers.add(controller); } @@ -295,7 +268,7 @@ public final class MediaSessionManager { ArrayList<MediaController> controllers = new ArrayList<MediaController>(); int size = tokens.size(); for (int i = 0; i < size; i++) { - controllers.add(MediaController.fromToken(tokens.get(i))); + controllers.add(new MediaController(tokens.get(i))); } SessionListener.this.onActiveSessionsChanged(controllers); } diff --git a/packages/SystemUI/src/com/android/systemui/volume/VolumeUI.java b/packages/SystemUI/src/com/android/systemui/volume/VolumeUI.java index 375f94c..e38c2ac 100644 --- a/packages/SystemUI/src/com/android/systemui/volume/VolumeUI.java +++ b/packages/SystemUI/src/com/android/systemui/volume/VolumeUI.java @@ -189,7 +189,7 @@ public class VolumeUI extends SystemUI { @Override public void remoteVolumeChanged(ISessionController binder, int flags) throws RemoteException { - MediaController controller = MediaController.fromBinder(binder); + MediaController controller = new MediaController(binder); mPanel.postRemoteVolumeChanged(controller, flags); } diff --git a/tests/OneMedia/src/com/android/onemedia/PlayerController.java b/tests/OneMedia/src/com/android/onemedia/PlayerController.java index 9cbb455..b47cc3c 100644 --- a/tests/OneMedia/src/com/android/onemedia/PlayerController.java +++ b/tests/OneMedia/src/com/android/onemedia/PlayerController.java @@ -161,7 +161,7 @@ public class PlayerController { Log.e(TAG, "Error getting session", e); return; } - mController = MediaController.fromToken(token); + mController = new MediaController(token); mContext.setMediaController(mController); mController.addCallback(mControllerCb, mHandler); mTransportControls = mController.getTransportControls(); diff --git a/tests/OneMedia/src/com/android/onemedia/PlayerSession.java b/tests/OneMedia/src/com/android/onemedia/PlayerSession.java index 78353b2..feecfde 100644 --- a/tests/OneMedia/src/com/android/onemedia/PlayerSession.java +++ b/tests/OneMedia/src/com/android/onemedia/PlayerSession.java @@ -28,6 +28,7 @@ import android.media.session.PlaybackState; import android.os.Bundle; import android.support.media.protocols.MediaPlayerProtocol; import android.support.media.protocols.MediaPlayerProtocol.MediaStatus; +import android.os.RemoteException; import android.os.SystemClock; import android.util.Log; import android.view.KeyEvent; @@ -84,7 +85,7 @@ public class PlayerSession { .build()); mRouter.setRoutingCallback(new RoutingCallback(), null); - mSession = man.createSession("OneMedia"); + mSession = new MediaSession(mContext, "OneMedia"); mSession.addCallback(mCallback); mSession.addTransportControlsCallback(new TransportCallback()); mSession.setPlaybackState(mPlaybackState); |