summaryrefslogtreecommitdiffstats
path: root/media
diff options
context:
space:
mode:
authorRoboErik <epastern@google.com>2014-07-10 13:48:01 -0700
committerRoboErik <epastern@google.com>2014-07-16 12:03:45 -0700
commit8b4bffcac996b4083e720310a09d315ca1c4a000 (patch)
tree56726422a371d15b204756f80bce5e487d1ed318 /media
parent54892c8b813eb2de3c5c6bcd08c90b8dd0abbec8 (diff)
downloadframeworks_base-8b4bffcac996b4083e720310a09d315ca1c4a000.zip
frameworks_base-8b4bffcac996b4083e720310a09d315ca1c4a000.tar.gz
frameworks_base-8b4bffcac996b4083e720310a09d315ca1c4a000.tar.bz2
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
Diffstat (limited to 'media')
-rw-r--r--media/java/android/media/session/MediaController.java27
-rw-r--r--media/java/android/media/session/MediaSession.java50
-rw-r--r--media/java/android/media/session/MediaSessionLegacyHelper.java12
-rw-r--r--media/java/android/media/session/MediaSessionManager.java41
4 files changed, 72 insertions, 58 deletions
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);
}