Refactor transport controls APIs
This merges TransportPerformer into MediaSession + a
TransportControlsCallback and makes TransportController into an
inner class on MediaController called TransportControls. Also makes
the PlaybackState and Metadata part of the session APIs instead of
transport controls.
Change-Id: I16ad392e6d318abe3119ad5d89656d253af25e16
diff --git a/api/current.txt b/api/current.txt
index acf6e87..34a3a48 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -15723,28 +15723,49 @@
method public void addCallback(android.media.session.MediaController.Callback, android.os.Handler);
method public boolean dispatchMediaButtonEvent(android.view.KeyEvent);
method public static android.media.session.MediaController fromToken(android.media.session.MediaSessionToken);
- method public android.media.session.TransportController getTransportController();
+ method public android.media.MediaMetadata getMetadata();
+ method public android.media.session.PlaybackState getPlaybackState();
+ method public int getRatingType();
+ method public android.media.session.MediaController.TransportControls getTransportControls();
method public void removeCallback(android.media.session.MediaController.Callback);
method public void sendControlCommand(java.lang.String, android.os.Bundle, android.os.ResultReceiver);
}
public static abstract class MediaController.Callback {
ctor public MediaController.Callback();
+ method public void onMetadataChanged(android.media.MediaMetadata);
+ method public void onPlaybackStateChanged(android.media.session.PlaybackState);
method public void onSessionEvent(java.lang.String, android.os.Bundle);
}
+ public final class MediaController.TransportControls {
+ method public void fastForward();
+ method public void pause();
+ method public void play();
+ method public void rewind();
+ method public void seekTo(long);
+ method public void setRating(android.media.Rating);
+ method public void skipToNext();
+ method public void skipToPrevious();
+ method public void stop();
+ }
+
public final class MediaSession {
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);
+ method public void addTransportControlsCallback(android.media.session.MediaSession.TransportControlsCallback, android.os.Handler);
method public android.media.session.MediaSessionToken getSessionToken();
- method public android.media.session.TransportPerformer getTransportPerformer();
method public boolean isActive();
method public void release();
method public void removeCallback(android.media.session.MediaSession.Callback);
+ method public void removeTransportControlsCallback(android.media.session.MediaSession.TransportControlsCallback);
method public void sendSessionEvent(java.lang.String, android.os.Bundle);
method public void setActive(boolean);
method public void setFlags(int);
method public void setLaunchPendingIntent(android.app.PendingIntent);
+ method public void setMetadata(android.media.MediaMetadata);
+ method public void setPlaybackState(android.media.session.PlaybackState);
method public void setPlaybackToLocal(int);
method public void setPlaybackToRemote(android.media.session.RemoteVolumeProvider);
field public static final int FLAG_HANDLES_MEDIA_BUTTONS = 1; // 0x1
@@ -15757,6 +15778,19 @@
method public void onMediaButtonEvent(android.content.Intent);
}
+ public static abstract class MediaSession.TransportControlsCallback {
+ ctor public MediaSession.TransportControlsCallback();
+ method public void onFastForward();
+ method public void onPause();
+ method public void onPlay();
+ method public void onRewind();
+ method public void onSeekTo(long);
+ method public void onSetRating(android.media.Rating);
+ method public void onSkipToNext();
+ method public void onSkipToPrevious();
+ method public void onStop();
+ }
+
public final class MediaSessionManager {
method public android.media.session.MediaSession createSession(java.lang.String);
}
@@ -15808,60 +15842,15 @@
public abstract class RemoteVolumeProvider {
ctor public RemoteVolumeProvider(int, int);
- method public final int getFlags();
method public final int getMaxVolume();
+ method public final int getVolumeControl();
method public final void notifyVolumeChanged();
method public void onAdjustVolumeBy(int);
method public abstract int onGetCurrentVolume();
method public void onSetVolumeTo(int);
- field public static final int VOLUME_CONTROL_ABSOLUTE = 4; // 0x4
- field public static final int VOLUME_CONTROL_FIXED = 1; // 0x1
- field public static final int VOLUME_CONTROL_RELATIVE = 2; // 0x2
- }
-
- public final class TransportController {
- method public void addStateListener(android.media.session.TransportController.TransportStateListener);
- method public void addStateListener(android.media.session.TransportController.TransportStateListener, android.os.Handler);
- method public void fastForward();
- method public android.media.MediaMetadata getMetadata();
- method public android.media.session.PlaybackState getPlaybackState();
- method public int getRatingType();
- method public void pause();
- method public void play();
- method public void removeStateListener(android.media.session.TransportController.TransportStateListener);
- method public void rewind();
- method public void seekTo(long);
- method public void setRating(android.media.Rating);
- method public void skipToNext();
- method public void skipToPrevious();
- method public void stop();
- }
-
- public static abstract class TransportController.TransportStateListener {
- ctor public TransportController.TransportStateListener();
- method public void onMetadataChanged(android.media.MediaMetadata);
- method public void onPlaybackStateChanged(android.media.session.PlaybackState);
- }
-
- public final class TransportPerformer {
- method public void addCallback(android.media.session.TransportPerformer.Callback);
- method public void addCallback(android.media.session.TransportPerformer.Callback, android.os.Handler);
- method public void removeCallback(android.media.session.TransportPerformer.Callback);
- method public void setMetadata(android.media.MediaMetadata);
- method public void setPlaybackState(android.media.session.PlaybackState);
- }
-
- public static abstract class TransportPerformer.Callback {
- ctor public TransportPerformer.Callback();
- method public void onFastForward();
- method public void onPause();
- method public void onPlay();
- method public void onRewind();
- method public void onSeekTo(long);
- method public void onSetRating(android.media.Rating);
- method public void onSkipToNext();
- method public void onSkipToPrevious();
- method public void onStop();
+ field public static final int VOLUME_CONTROL_ABSOLUTE = 2; // 0x2
+ field public static final int VOLUME_CONTROL_FIXED = 0; // 0x0
+ field public static final int VOLUME_CONTROL_RELATIVE = 1; // 0x1
}
}
diff --git a/media/java/android/media/RemoteControlClient.java b/media/java/android/media/RemoteControlClient.java
index b89affd..0caea5f 100644
--- a/media/java/android/media/RemoteControlClient.java
+++ b/media/java/android/media/RemoteControlClient.java
@@ -27,7 +27,6 @@
import android.media.session.MediaSessionLegacyHelper;
import android.media.session.PlaybackState;
import android.media.session.MediaSession;
-import android.media.session.TransportPerformer;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
@@ -584,7 +583,7 @@
// USE_SESSIONS
if (mSession != null && mMetadataBuilder != null) {
- mSession.getTransportPerformer().setMetadata(mMetadataBuilder.build());
+ mSession.setMetadata(mMetadataBuilder.build());
}
mApplied = true;
}
@@ -702,7 +701,7 @@
mSessionPlaybackState.setState(pbState, hasPosition ?
mPlaybackPositionMs : PlaybackState.PLAYBACK_POSITION_UNKNOWN,
playbackSpeed);
- mSession.getTransportPerformer().setPlaybackState(mSessionPlaybackState);
+ mSession.setPlaybackState(mSessionPlaybackState);
}
}
}
@@ -789,7 +788,7 @@
if (mSession != null) {
mSessionPlaybackState.setActions(PlaybackState
.getActionsFromRccControlFlags(transportControlFlags));
- mSession.getTransportPerformer().setPlaybackState(mSessionPlaybackState);
+ mSession.setPlaybackState(mSessionPlaybackState);
}
}
}
@@ -1317,7 +1316,8 @@
}
// USE_SESSIONS
- private TransportPerformer.Callback mTransportListener = new TransportPerformer.Callback() {
+ private MediaSession.TransportControlsCallback mTransportListener
+ = new MediaSession.TransportControlsCallback() {
@Override
public void onSeekTo(long pos) {
diff --git a/media/java/android/media/session/MediaController.java b/media/java/android/media/session/MediaController.java
index 63c0839..caff1ad 100644
--- a/media/java/android/media/session/MediaController.java
+++ b/media/java/android/media/session/MediaController.java
@@ -17,6 +17,7 @@
package android.media.session;
import android.media.MediaMetadata;
+import android.media.Rating;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
@@ -45,8 +46,8 @@
private static final String TAG = "SessionController";
private static final int MSG_EVENT = 1;
- private static final int MESSAGE_PLAYBACK_STATE = 2;
- private static final int MESSAGE_METADATA = 3;
+ private static final int MSG_UPDATE_PLAYBACK_STATE = 2;
+ private static final int MSG_UPDATE_METADATA = 3;
private static final int MSG_ROUTE = 4;
private final ISessionController mSessionBinder;
@@ -57,10 +58,11 @@
private boolean mCbRegistered = false;
- private TransportController mTransportController;
+ private TransportControls mTransportController;
private MediaController(ISessionController sessionBinder) {
mSessionBinder = sessionBinder;
+ mTransportController = new TransportControls();
}
/**
@@ -70,9 +72,6 @@
MediaController controller = new MediaController(sessionBinder);
try {
controller.mSessionBinder.registerCallbackListener(controller.mCbStub);
- if (controller.mSessionBinder.isTransportControlEnabled()) {
- controller.mTransportController = new TransportController(sessionBinder);
- }
} catch (RemoteException e) {
Log.wtf(TAG, "MediaController created with expired token", e);
controller = null;
@@ -93,12 +92,11 @@
}
/**
- * Get a TransportController if the session supports it. If it is not
- * supported null will be returned.
+ * Get a {@link TransportControls} instance for this session.
*
- * @return A TransportController or null
+ * @return A controls instance
*/
- public TransportController getTransportController() {
+ public TransportControls getTransportControls() {
return mTransportController;
}
@@ -119,12 +117,63 @@
try {
return mSessionBinder.sendMediaButton(keyEvent);
} catch (RemoteException e) {
- Log.d(TAG, "Dead object in sendMediaButton", e);
+ // System is dead. =(
}
return false;
}
/**
+ * Get the current playback state for this session.
+ *
+ * @return The current PlaybackState or null
+ */
+ public PlaybackState getPlaybackState() {
+ try {
+ return mSessionBinder.getPlaybackState();
+ } catch (RemoteException e) {
+ Log.wtf(TAG, "Error calling getPlaybackState.", e);
+ return null;
+ }
+ }
+
+ /**
+ * Get the current metadata for this session.
+ *
+ * @return The current MediaMetadata or null.
+ */
+ public MediaMetadata getMetadata() {
+ try {
+ return mSessionBinder.getMetadata();
+ } catch (RemoteException e) {
+ Log.wtf(TAG, "Error calling getMetadata.", e);
+ return null;
+ }
+ }
+
+ /**
+ * Get the rating type supported by the session. One of:
+ * <ul>
+ * <li>{@link Rating#RATING_NONE}</li>
+ * <li>{@link Rating#RATING_HEART}</li>
+ * <li>{@link Rating#RATING_THUMB_UP_DOWN}</li>
+ * <li>{@link Rating#RATING_3_STARS}</li>
+ * <li>{@link Rating#RATING_4_STARS}</li>
+ * <li>{@link Rating#RATING_5_STARS}</li>
+ * <li>{@link Rating#RATING_PERCENTAGE}</li>
+ * </ul>
+ *
+ * @return The supported rating type
+ */
+ public int getRatingType() {
+ try {
+ return mSessionBinder.getRatingType();
+ } catch (RemoteException e) {
+ Log.wtf(TAG, "Error calling getRatingType.", e);
+ return Rating.RATING_NONE;
+ }
+ }
+
+ /**
* Adds a callback to receive updates from the Session. Updates will be
* posted on the caller's thread.
*
@@ -255,18 +304,10 @@
return null;
}
- private void postEvent(String event, Bundle extras) {
+ private final void postMessage(int what, Object obj, Bundle data) {
synchronized (mLock) {
for (int i = mCallbacks.size() - 1; i >= 0; i--) {
- mCallbacks.get(i).post(MSG_EVENT, event, extras);
- }
- }
- }
-
- private void postRouteChanged(RouteInfo route) {
- synchronized (mLock) {
- for (int i = mCallbacks.size() - 1; i >= 0; i--) {
- mCallbacks.get(i).post(MSG_ROUTE, route, null);
+ mCallbacks.get(i).post(what, obj, data);
}
}
}
@@ -294,6 +335,143 @@
*/
public void onRouteChanged(RouteInfo route) {
}
+
+ /**
+ * Override to handle changes in playback state.
+ *
+ * @param state The new playback state of the session
+ */
+ public void onPlaybackStateChanged(PlaybackState state) {
+ }
+
+ /**
+ * Override to handle changes to the current metadata.
+ *
+ * @see MediaMetadata
+ * @param metadata The current metadata for the session or null
+ */
+ public void onMetadataChanged(MediaMetadata metadata) {
+ }
+ }
+
+ /**
+ * Interface for controlling media playback on a session. This allows an app
+ * to send media transport commands to the session.
+ */
+ public final class TransportControls {
+ private static final String TAG = "TransportController";
+
+ private TransportControls() {
+ }
+
+ /**
+ * Request that the player start its playback at its current position.
+ */
+ public void play() {
+ try {
+ mSessionBinder.play();
+ } catch (RemoteException e) {
+ Log.wtf(TAG, "Error calling play.", e);
+ }
+ }
+
+ /**
+ * Request that the player pause its playback and stay at its current
+ * position.
+ */
+ public void pause() {
+ try {
+ mSessionBinder.pause();
+ } catch (RemoteException e) {
+ Log.wtf(TAG, "Error calling pause.", e);
+ }
+ }
+
+ /**
+ * Request that the player stop its playback; it may clear its state in
+ * whatever way is appropriate.
+ */
+ public void stop() {
+ try {
+ mSessionBinder.stop();
+ } catch (RemoteException e) {
+ Log.wtf(TAG, "Error calling stop.", e);
+ }
+ }
+
+ /**
+ * Move to a new location in the media stream.
+ *
+ * @param pos Position to move to, in milliseconds.
+ */
+ public void seekTo(long pos) {
+ try {
+ mSessionBinder.seekTo(pos);
+ } catch (RemoteException e) {
+ Log.wtf(TAG, "Error calling seekTo.", e);
+ }
+ }
+
+ /**
+ * Start fast forwarding. If playback is already fast forwarding this
+ * may increase the rate.
+ */
+ public void fastForward() {
+ try {
+ mSessionBinder.fastForward();
+ } catch (RemoteException e) {
+ Log.wtf(TAG, "Error calling fastForward.", e);
+ }
+ }
+
+ /**
+ * Skip to the next item.
+ */
+ public void skipToNext() {
+ try {
+ mSessionBinder.next();
+ } catch (RemoteException e) {
+ Log.wtf(TAG, "Error calling next.", e);
+ }
+ }
+
+ /**
+ * Start rewinding. If playback is already rewinding this may increase
+ * the rate.
+ */
+ public void rewind() {
+ try {
+ mSessionBinder.rewind();
+ } catch (RemoteException e) {
+ Log.wtf(TAG, "Error calling rewind.", e);
+ }
+ }
+
+ /**
+ * Skip to the previous item.
+ */
+ public void skipToPrevious() {
+ try {
+ mSessionBinder.previous();
+ } catch (RemoteException e) {
+ Log.wtf(TAG, "Error calling previous.", e);
+ }
+ }
+
+ /**
+ * Rate the current content. This will cause the rating to be set for
+ * the current user. The Rating type must match the type returned by
+ * {@link #getRatingType()}.
+ *
+ * @param rating The rating to set for the current content
+ */
+ public void setRating(Rating rating) {
+ try {
+ mSessionBinder.rate(rating);
+ } catch (RemoteException e) {
+ Log.wtf(TAG, "Error calling rate.", e);
+ }
+ }
}
private final static class CallbackStub extends ISessionControllerCallback.Stub {
@@ -307,7 +485,7 @@
public void onEvent(String event, Bundle extras) {
MediaController controller = mController.get();
if (controller != null) {
- controller.postEvent(event, extras);
+ controller.postMessage(MSG_EVENT, event, extras);
}
}
@@ -315,7 +493,7 @@
public void onRouteChanged(RouteInfo route) {
MediaController controller = mController.get();
if (controller != null) {
- controller.postRouteChanged(route);
+ controller.postMessage(MSG_ROUTE, route, null);
}
}
@@ -323,10 +501,7 @@
public void onPlaybackStateChanged(PlaybackState state) {
MediaController controller = mController.get();
if (controller != null) {
- TransportController tc = controller.getTransportController();
- if (tc != null) {
- tc.postPlaybackStateChanged(state);
- }
+ controller.postMessage(MSG_UPDATE_PLAYBACK_STATE, state, null);
}
}
@@ -334,10 +509,7 @@
public void onMetadataChanged(MediaMetadata metadata) {
MediaController controller = mController.get();
if (controller != null) {
- TransportController tc = controller.getTransportController();
- if (tc != null) {
- tc.postMetadataChanged(metadata);
- }
+ controller.postMessage(MSG_UPDATE_METADATA, metadata, null);
}
}
@@ -359,6 +531,13 @@
break;
case MSG_ROUTE:
mCallback.onRouteChanged((RouteInfo) msg.obj);
+ break;
+ case MSG_UPDATE_PLAYBACK_STATE:
+ mCallback.onPlaybackStateChanged((PlaybackState) msg.obj);
+ break;
+ case MSG_UPDATE_METADATA:
+ mCallback.onMetadataChanged((MediaMetadata) msg.obj);
+ break;
}
}
diff --git a/media/java/android/media/session/MediaSession.java b/media/java/android/media/session/MediaSession.java
index 51dcee1..90ccf68 100644
--- a/media/java/android/media/session/MediaSession.java
+++ b/media/java/android/media/session/MediaSession.java
@@ -16,10 +16,12 @@
package android.media.session;
+import android.annotation.NonNull;
+import android.annotation.Nullable;
import android.app.PendingIntent;
import android.content.Intent;
-import android.media.AudioAttributes;
import android.media.AudioManager;
+import android.media.MediaMetadata;
import android.media.Rating;
import android.media.session.ISessionController;
import android.media.session.ISession;
@@ -52,8 +54,10 @@
* the owner of the session may use {@link #getSessionToken()} to allow apps to
* create a {@link MediaController} to interact with this session.
* <p>
- * To receive commands, media keys, and other events a Callback must be set with
- * {@link #addCallback(Callback)}.
+ * 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
+ * {@link #addTransportControlsCallback}.
* <p>
* When an app is finished performing playback it must call {@link #release()}
* to clean up the session and notify any controllers.
@@ -70,9 +74,10 @@
public static final int FLAG_HANDLES_MEDIA_BUTTONS = 1 << 0;
/**
- * Set this flag on the session to indicate that it handles commands through
- * the {@link TransportPerformer}. The performer can be retrieved by calling
- * {@link #getTransportPerformer()}.
+ * Set this flag on the session to indicate that it handles transport
+ * control commands through a {@link TransportControlsCallback}. The
+ * callback can be retrieved by calling
+ * {@link #addTransportControlsCallback}.
*/
public static final int FLAG_HANDLES_TRANSPORT_CONTROLS = 1 << 1;
@@ -119,12 +124,6 @@
*/
public static final int DISCONNECT_REASON_SESSION_DESTROYED = 5;
- private static final int MSG_MEDIA_BUTTON = 1;
- private static final int MSG_COMMAND = 2;
- private static final int MSG_ROUTE_CHANGE = 3;
- private static final int MSG_ROUTE_CONNECTED = 4;
- private static final int MSG_ROUTE_DISCONNECTED = 5;
-
private static final String KEY_COMMAND = "command";
private static final String KEY_EXTRAS = "extras";
private static final String KEY_CALLBACK = "callback";
@@ -135,12 +134,14 @@
private final ISession mBinder;
private final CallbackStub mCbStub;
- private final ArrayList<MessageHandler> mCallbacks = new ArrayList<MessageHandler>();
+ private final ArrayList<CallbackMessageHandler> mCallbacks
+ = new ArrayList<CallbackMessageHandler>();
+ private final ArrayList<TransportMessageHandler> mTransportCallbacks
+ = new ArrayList<TransportMessageHandler>();
// TODO route interfaces
private final ArrayMap<String, RouteInterface.EventListener> mInterfaceListeners
= new ArrayMap<String, RouteInterface.EventListener>();
- private TransportPerformer mPerformer;
private Route mRoute;
private boolean mActive = false;;
@@ -158,7 +159,6 @@
throw new RuntimeException("Dead object in MediaSessionController constructor: ", e);
}
mSessionToken = new MediaSessionToken(controllerBinder);
- mPerformer = new TransportPerformer(mBinder);
}
/**
@@ -191,7 +191,8 @@
if (handler == null) {
handler = new Handler();
}
- MessageHandler msgHandler = new MessageHandler(handler.getLooper(), callback);
+ CallbackMessageHandler msgHandler = new CallbackMessageHandler(handler.getLooper(),
+ callback);
mCallbacks.add(msgHandler);
}
}
@@ -208,18 +209,6 @@
}
/**
- * Retrieves the {@link TransportPerformer} for this session. To receive
- * commands through the performer you must also set the
- * {@link #FLAG_HANDLES_TRANSPORT_CONTROLS} flag using
- * {@link #setFlags(int)}.
- *
- * @return The performer associated with this session.
- */
- public TransportPerformer getTransportPerformer() {
- return mPerformer;
- }
-
- /**
* Set an intent for launching UI for this Session. This can be used as a
* quick link to an ongoing media screen.
*
@@ -430,12 +419,160 @@
return true;
}
- private MessageHandler getHandlerForCallbackLocked(Callback cb) {
+ /**
+ * Add a callback to receive transport controls on, such as play, rewind, or
+ * fast forward.
+ *
+ * @param callback The callback object
+ */
+ public void addTransportControlsCallback(@NonNull TransportControlsCallback callback) {
+ addTransportControlsCallback(callback, null);
+ }
+
+ /**
+ * Add a callback to receive transport controls on, such as play, rewind, or
+ * fast forward. The updates will be posted to the specified handler. If no
+ * handler is provided they will be posted to the caller's thread.
+ *
+ * @param callback The callback to receive updates on
+ * @param handler The handler to post the updates on
+ */
+ public void addTransportControlsCallback(@NonNull TransportControlsCallback callback,
+ @Nullable Handler handler) {
+ if (callback == null) {
+ throw new IllegalArgumentException("Callback cannot be null");
+ }
+ synchronized (mLock) {
+ if (getTransportControlsHandlerForCallbackLocked(callback) != null) {
+ Log.w(TAG, "Callback is already added, ignoring");
+ return;
+ }
+ if (handler == null) {
+ handler = new Handler();
+ }
+ TransportMessageHandler msgHandler = new TransportMessageHandler(handler.getLooper(),
+ callback);
+ mTransportCallbacks.add(msgHandler);
+ }
+ }
+
+ /**
+ * Stop receiving transport controls on the specified callback. If an update
+ * has already been posted you may still receive it after this call returns.
+ *
+ * @param callback The callback to stop receiving updates on
+ */
+ public void removeTransportControlsCallback(@NonNull TransportControlsCallback callback) {
+ if (callback == null) {
+ throw new IllegalArgumentException("Callback cannot be null");
+ }
+ synchronized (mLock) {
+ removeTransportControlsCallbackLocked(callback);
+ }
+ }
+
+ /**
+ * Update the current playback state.
+ *
+ * @param state The current state of playback
+ */
+ public void setPlaybackState(PlaybackState state) {
+ try {
+ mBinder.setPlaybackState(state);
+ } catch (RemoteException e) {
+ Log.wtf(TAG, "Dead object in setPlaybackState.", e);
+ }
+ }
+
+ /**
+ * Update the current metadata. New metadata can be created using
+ * {@link android.media.MediaMetadata.Builder}.
+ *
+ * @param metadata The new metadata
+ */
+ public void setMetadata(MediaMetadata metadata) {
+ try {
+ mBinder.setMetadata(metadata);
+ } catch (RemoteException e) {
+ Log.wtf(TAG, "Dead object in setPlaybackState.", e);
+ }
+ }
+
+ private void dispatchPlay() {
+ postToTransportCallbacks(TransportMessageHandler.MSG_PLAY);
+ }
+
+ private void dispatchPause() {
+ postToTransportCallbacks(TransportMessageHandler.MSG_PAUSE);
+ }
+
+ private void dispatchStop() {
+ postToTransportCallbacks(TransportMessageHandler.MSG_STOP);
+ }
+
+ private void dispatchNext() {
+ postToTransportCallbacks(TransportMessageHandler.MSG_NEXT);
+ }
+
+ private void dispatchPrevious() {
+ postToTransportCallbacks(TransportMessageHandler.MSG_PREVIOUS);
+ }
+
+ private void dispatchFastForward() {
+ postToTransportCallbacks(TransportMessageHandler.MSG_FAST_FORWARD);
+ }
+
+ private void dispatchRewind() {
+ postToTransportCallbacks(TransportMessageHandler.MSG_REWIND);
+ }
+
+ private void dispatchSeekTo(long pos) {
+ postToTransportCallbacks(TransportMessageHandler.MSG_SEEK_TO, pos);
+ }
+
+ private void dispatchRate(Rating rating) {
+ postToTransportCallbacks(TransportMessageHandler.MSG_RATE, rating);
+ }
+
+ private TransportMessageHandler getTransportControlsHandlerForCallbackLocked(
+ TransportControlsCallback callback) {
+ for (int i = mTransportCallbacks.size() - 1; i >= 0; i--) {
+ TransportMessageHandler handler = mTransportCallbacks.get(i);
+ if (callback == handler.mCallback) {
+ return handler;
+ }
+ }
+ return null;
+ }
+
+ private boolean removeTransportControlsCallbackLocked(TransportControlsCallback callback) {
+ for (int i = mTransportCallbacks.size() - 1; i >= 0; i--) {
+ if (callback == mTransportCallbacks.get(i).mCallback) {
+ mTransportCallbacks.remove(i);
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private void postToTransportCallbacks(int what, Object obj) {
+ synchronized (mLock) {
+ for (int i = mTransportCallbacks.size() - 1; i >= 0; i--) {
+ mTransportCallbacks.get(i).post(what, obj);
+ }
+ }
+ }
+
+ private void postToTransportCallbacks(int what) {
+ postToTransportCallbacks(what, null);
+ }
+
+ private CallbackMessageHandler getHandlerForCallbackLocked(Callback cb) {
if (cb == null) {
throw new IllegalArgumentException("Callback cannot be null");
}
for (int i = mCallbacks.size() - 1; i >= 0; i--) {
- MessageHandler handler = mCallbacks.get(i);
+ CallbackMessageHandler handler = mCallbacks.get(i);
if (cb == handler.mCallback) {
return handler;
}
@@ -448,7 +585,7 @@
throw new IllegalArgumentException("Callback cannot be null");
}
for (int i = mCallbacks.size() - 1; i >= 0; i--) {
- MessageHandler handler = mCallbacks.get(i);
+ CallbackMessageHandler handler = mCallbacks.get(i);
if (cb == handler.mCallback) {
mCallbacks.remove(i);
return true;
@@ -461,7 +598,7 @@
Command cmd = new Command(command, extras, resultCb);
synchronized (mLock) {
for (int i = mCallbacks.size() - 1; i >= 0; i--) {
- mCallbacks.get(i).post(MSG_COMMAND, cmd);
+ mCallbacks.get(i).post(CallbackMessageHandler.MSG_COMMAND, cmd);
}
}
}
@@ -469,7 +606,7 @@
private void postMediaButton(Intent mediaButtonIntent) {
synchronized (mLock) {
for (int i = mCallbacks.size() - 1; i >= 0; i--) {
- mCallbacks.get(i).post(MSG_MEDIA_BUTTON, mediaButtonIntent);
+ mCallbacks.get(i).post(CallbackMessageHandler.MSG_MEDIA_BUTTON, mediaButtonIntent);
}
}
}
@@ -477,7 +614,7 @@
private void postRequestRouteChange(RouteInfo route) {
synchronized (mLock) {
for (int i = mCallbacks.size() - 1; i >= 0; i--) {
- mCallbacks.get(i).post(MSG_ROUTE_CHANGE, route);
+ mCallbacks.get(i).post(CallbackMessageHandler.MSG_ROUTE_CHANGE, route);
}
}
}
@@ -486,7 +623,7 @@
synchronized (mLock) {
mRoute = new Route(route, options, this);
for (int i = mCallbacks.size() - 1; i >= 0; i--) {
- mCallbacks.get(i).post(MSG_ROUTE_CONNECTED, mRoute);
+ mCallbacks.get(i).post(CallbackMessageHandler.MSG_ROUTE_CONNECTED, mRoute);
}
}
}
@@ -495,16 +632,16 @@
synchronized (mLock) {
if (mRoute != null && TextUtils.equals(mRoute.getRouteInfo().getId(), route.getId())) {
for (int i = mCallbacks.size() - 1; i >= 0; i--) {
- mCallbacks.get(i).post(MSG_ROUTE_DISCONNECTED, mRoute, reason);
+ mCallbacks.get(i).post(CallbackMessageHandler.MSG_ROUTE_DISCONNECTED, mRoute,
+ reason);
}
}
}
}
/**
- * Receives commands or updates from controllers and routes. An app can
- * specify what commands and buttons it supports by setting them on the
- * MediaSession.
+ * Receives generic commands or updates from controllers and the system.
+ * Callbacks may be registered using {@link #addCallback}.
*/
public abstract static class Callback {
@@ -580,6 +717,82 @@
}
/**
+ * Receives transport control commands. Callbacks may be registered using
+ * {@link #addTransportControlsCallback}.
+ */
+ public static abstract class TransportControlsCallback {
+
+ /**
+ * Override to handle requests to begin playback.
+ */
+ public void onPlay() {
+ }
+
+ /**
+ * Override to handle requests to pause playback.
+ */
+ public void onPause() {
+ }
+
+ /**
+ * Override to handle requests to skip to the next media item.
+ */
+ public void onSkipToNext() {
+ }
+
+ /**
+ * Override to handle requests to skip to the previous media item.
+ */
+ public void onSkipToPrevious() {
+ }
+
+ /**
+ * Override to handle requests to fast forward.
+ */
+ public void onFastForward() {
+ }
+
+ /**
+ * Override to handle requests to rewind.
+ */
+ public void onRewind() {
+ }
+
+ /**
+ * Override to handle requests to stop playback.
+ */
+ public void onStop() {
+ }
+
+ /**
+ * Override to handle requests to seek to a specific position in ms.
+ *
+ * @param pos New position to move to, in milliseconds.
+ */
+ public void onSeekTo(long pos) {
+ }
+
+ /**
+ * Override to handle the item being rated.
+ *
+ * @param rating
+ */
+ public void onSetRating(Rating rating) {
+ }
+
+ /**
+ * Report that audio focus has changed on the app. This only happens if
+ * you have indicated you have started playing with
+ * {@link #setPlaybackState}.
+ *
+ * @param focusChange The type of focus change, TBD.
+ * @hide
+ */
+ public void onRouteFocusChange(int focusChange) {
+ }
+ }
+
+ /**
* @hide
*/
public static class CallbackStub extends ISessionCallback.Stub {
@@ -641,10 +854,7 @@
public void onPlay() throws RemoteException {
MediaSession session = mMediaSession.get();
if (session != null) {
- TransportPerformer tp = session.getTransportPerformer();
- if (tp != null) {
- tp.dispatchPlay();
- }
+ session.dispatchPlay();
}
}
@@ -652,10 +862,7 @@
public void onPause() throws RemoteException {
MediaSession session = mMediaSession.get();
if (session != null) {
- TransportPerformer tp = session.getTransportPerformer();
- if (tp != null) {
- tp.dispatchPause();
- }
+ session.dispatchPause();
}
}
@@ -663,10 +870,7 @@
public void onStop() throws RemoteException {
MediaSession session = mMediaSession.get();
if (session != null) {
- TransportPerformer tp = session.getTransportPerformer();
- if (tp != null) {
- tp.dispatchStop();
- }
+ session.dispatchStop();
}
}
@@ -674,10 +878,7 @@
public void onNext() throws RemoteException {
MediaSession session = mMediaSession.get();
if (session != null) {
- TransportPerformer tp = session.getTransportPerformer();
- if (tp != null) {
- tp.dispatchNext();
- }
+ session.dispatchNext();
}
}
@@ -685,10 +886,7 @@
public void onPrevious() throws RemoteException {
MediaSession session = mMediaSession.get();
if (session != null) {
- TransportPerformer tp = session.getTransportPerformer();
- if (tp != null) {
- tp.dispatchPrevious();
- }
+ session.dispatchPrevious();
}
}
@@ -696,10 +894,7 @@
public void onFastForward() throws RemoteException {
MediaSession session = mMediaSession.get();
if (session != null) {
- TransportPerformer tp = session.getTransportPerformer();
- if (tp != null) {
- tp.dispatchFastForward();
- }
+ session.dispatchFastForward();
}
}
@@ -707,10 +902,7 @@
public void onRewind() throws RemoteException {
MediaSession session = mMediaSession.get();
if (session != null) {
- TransportPerformer tp = session.getTransportPerformer();
- if (tp != null) {
- tp.dispatchRewind();
- }
+ session.dispatchRewind();
}
}
@@ -718,10 +910,7 @@
public void onSeekTo(long pos) throws RemoteException {
MediaSession session = mMediaSession.get();
if (session != null) {
- TransportPerformer tp = session.getTransportPerformer();
- if (tp != null) {
- tp.dispatchSeekTo(pos);
- }
+ session.dispatchSeekTo(pos);
}
}
@@ -729,10 +918,7 @@
public void onRate(Rating rating) throws RemoteException {
MediaSession session = mMediaSession.get();
if (session != null) {
- TransportPerformer tp = session.getTransportPerformer();
- if (tp != null) {
- tp.dispatchRate(rating);
- }
+ session.dispatchRate(rating);
}
}
@@ -758,10 +944,16 @@
}
- private class MessageHandler extends Handler {
+ private class CallbackMessageHandler extends Handler {
+ private static final int MSG_MEDIA_BUTTON = 1;
+ private static final int MSG_COMMAND = 2;
+ private static final int MSG_ROUTE_CHANGE = 3;
+ private static final int MSG_ROUTE_CONNECTED = 4;
+ private static final int MSG_ROUTE_DISCONNECTED = 5;
+
private MediaSession.Callback mCallback;
- public MessageHandler(Looper looper, MediaSession.Callback callback) {
+ public CallbackMessageHandler(Looper looper, MediaSession.Callback callback) {
super(looper, null, true);
mCallback = callback;
}
@@ -813,4 +1005,64 @@
this.stub = stub;
}
}
+
+ private class TransportMessageHandler extends Handler {
+ private static final int MSG_PLAY = 1;
+ private static final int MSG_PAUSE = 2;
+ private static final int MSG_STOP = 3;
+ private static final int MSG_NEXT = 4;
+ private static final int MSG_PREVIOUS = 5;
+ private static final int MSG_FAST_FORWARD = 6;
+ private static final int MSG_REWIND = 7;
+ private static final int MSG_SEEK_TO = 8;
+ private static final int MSG_RATE = 9;
+
+ private TransportControlsCallback mCallback;
+
+ public TransportMessageHandler(Looper looper, TransportControlsCallback cb) {
+ super(looper);
+ mCallback = cb;
+ }
+
+ public void post(int what, Object obj) {
+ obtainMessage(what, obj).sendToTarget();
+ }
+
+ public void post(int what) {
+ post(what, null);
+ }
+
+ @Override
+ public void handleMessage(Message msg) {
+ switch (msg.what) {
+ case MSG_PLAY:
+ mCallback.onPlay();
+ break;
+ case MSG_PAUSE:
+ mCallback.onPause();
+ break;
+ case MSG_STOP:
+ mCallback.onStop();
+ break;
+ case MSG_NEXT:
+ mCallback.onSkipToNext();
+ break;
+ case MSG_PREVIOUS:
+ mCallback.onSkipToPrevious();
+ break;
+ case MSG_FAST_FORWARD:
+ mCallback.onFastForward();
+ break;
+ case MSG_REWIND:
+ mCallback.onRewind();
+ break;
+ case MSG_SEEK_TO:
+ mCallback.onSeekTo((Long) msg.obj);
+ break;
+ case MSG_RATE:
+ mCallback.onSetRating((Rating) msg.obj);
+ break;
+ }
+ }
+ }
}
diff --git a/media/java/android/media/session/MediaSessionLegacyHelper.java b/media/java/android/media/session/MediaSessionLegacyHelper.java
index bdf3628..c303e77 100644
--- a/media/java/android/media/session/MediaSessionLegacyHelper.java
+++ b/media/java/android/media/session/MediaSessionLegacyHelper.java
@@ -76,13 +76,13 @@
}
}
- public void addRccListener(PendingIntent pi, TransportPerformer.Callback listener) {
+ public void addRccListener(PendingIntent pi,
+ MediaSession.TransportControlsCallback listener) {
if (pi == null) {
Log.w(TAG, "Pending intent was null, can't add rcc listener.");
return;
}
SessionHolder holder = getHolder(pi, true);
- TransportPerformer performer = holder.mSession.getTransportPerformer();
if (holder.mRccListener != null) {
if (holder.mRccListener == listener) {
if (DEBUG) {
@@ -92,9 +92,9 @@
return;
}
// Otherwise it changed so we need to switch to the new one
- performer.removeCallback(holder.mRccListener);
+ holder.mSession.removeTransportControlsCallback(holder.mRccListener);
}
- performer.addCallback(listener, mHandler);
+ holder.mSession.addTransportControlsCallback(listener, mHandler);
holder.mRccListener = listener;
holder.mFlags |= MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS;
holder.mSession.setFlags(holder.mFlags);
@@ -110,7 +110,7 @@
}
SessionHolder holder = getHolder(pi, false);
if (holder != null && holder.mRccListener != null) {
- holder.mSession.getTransportPerformer().removeCallback(holder.mRccListener);
+ holder.mSession.removeTransportControlsCallback(holder.mRccListener);
holder.mRccListener = null;
holder.mFlags &= ~MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS;
holder.mSession.setFlags(holder.mFlags);
@@ -141,7 +141,7 @@
// set this flag
holder.mFlags |= MediaSession.FLAG_HANDLES_MEDIA_BUTTONS;
holder.mSession.setFlags(holder.mFlags);
- holder.mSession.getTransportPerformer().addCallback(holder.mMediaButtonListener, mHandler);
+ holder.mSession.addTransportControlsCallback(holder.mMediaButtonListener, mHandler);
holder.mMediaButtonReceiver = new MediaButtonReceiver(pi, context);
holder.mSession.addCallback(holder.mMediaButtonReceiver, mHandler);
@@ -156,7 +156,7 @@
}
SessionHolder holder = getHolder(pi, false);
if (holder != null && holder.mMediaButtonListener != null) {
- holder.mSession.getTransportPerformer().removeCallback(holder.mMediaButtonListener);
+ holder.mSession.removeTransportControlsCallback(holder.mMediaButtonListener);
holder.mFlags &= ~MediaSession.FLAG_HANDLES_MEDIA_BUTTONS;
holder.mSession.setFlags(holder.mFlags);
holder.mMediaButtonListener = null;
@@ -206,7 +206,7 @@
}
}
- private static final class MediaButtonListener extends TransportPerformer.Callback {
+ private static final class MediaButtonListener extends MediaSession.TransportControlsCallback {
private final PendingIntent mPendingIntent;
private final Context mContext;
@@ -272,7 +272,7 @@
public final PendingIntent mPi;
public MediaButtonListener mMediaButtonListener;
public MediaButtonReceiver mMediaButtonReceiver;
- public TransportPerformer.Callback mRccListener;
+ public MediaSession.TransportControlsCallback mRccListener;
public int mFlags;
public SessionHolder(MediaSession session, PendingIntent pi) {
diff --git a/media/java/android/media/session/RemoteVolumeProvider.java b/media/java/android/media/session/RemoteVolumeProvider.java
index 2dcf649..47f672f3 100644
--- a/media/java/android/media/session/RemoteVolumeProvider.java
+++ b/media/java/android/media/session/RemoteVolumeProvider.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (C) 2014 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 android.media.session;
/**
@@ -12,23 +27,23 @@
* The volume is fixed and can not be modified. Requests to change volume
* should be ignored.
*/
- public static final int VOLUME_CONTROL_FIXED = 1 << 0;
+ public static final int VOLUME_CONTROL_FIXED = 0;
/**
* The volume control uses relative adjustment via
* {@link #onAdjustVolumeBy(int)}. Attempts to set the volume to a specific
* value should be ignored.
*/
- public static final int VOLUME_CONTROL_RELATIVE = 1 << 1;
+ public static final int VOLUME_CONTROL_RELATIVE = 1;
/**
* The volume control uses an absolute value. It may be adjusted using
* {@link #onAdjustVolumeBy(int)} or set directly using
* {@link #onSetVolumeTo(int)}.
*/
- public static final int VOLUME_CONTROL_ABSOLUTE = 1 << 2;
+ public static final int VOLUME_CONTROL_ABSOLUTE = 2;
- private final int mFlags;
+ private final int mControlType;
private final int mMaxVolume;
/**
@@ -40,7 +55,7 @@
* @param maxVolume The maximum allowed volume.
*/
public RemoteVolumeProvider(int volumeControl, int maxVolume) {
- mFlags = volumeControl;
+ mControlType = volumeControl;
mMaxVolume = maxVolume;
}
@@ -52,12 +67,12 @@
public abstract int onGetCurrentVolume();
/**
- * Get the flags that were set for this volume provider.
+ * Get the volume control type that this volume provider uses.
*
- * @return The flags for this volume provider
+ * @return The volume control type for this volume provider
*/
- public final int getFlags() {
- return mFlags;
+ public final int getVolumeControl() {
+ return mControlType;
}
/**
diff --git a/media/java/android/media/session/TransportController.java b/media/java/android/media/session/TransportController.java
deleted file mode 100644
index 4bd39ff..0000000
--- a/media/java/android/media/session/TransportController.java
+++ /dev/null
@@ -1,343 +0,0 @@
-/*
- * Copyright (C) 2014 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 android.media.session;
-
-import android.media.MediaMetadata;
-import android.media.Rating;
-import android.os.Handler;
-import android.os.Looper;
-import android.os.Message;
-import android.os.RemoteException;
-import android.util.Log;
-
-import java.util.ArrayList;
-
-/**
- * Interface for controlling media playback on a session. This allows an app to
- * request changes in playback, retrieve the current playback state and
- * metadata, and listen for changes to the playback state and metadata.
- */
-public final class TransportController {
- private static final String TAG = "TransportController";
-
- private final Object mLock = new Object();
- private final ArrayList<MessageHandler> mListeners = new ArrayList<MessageHandler>();
- private final ISessionController mBinder;
-
- /**
- * @hide
- */
- public TransportController(ISessionController binder) {
- mBinder = binder;
- }
-
- /**
- * Start listening to changes in playback state.
- */
- public void addStateListener(TransportStateListener listener) {
- addStateListener(listener, null);
- }
-
- public void addStateListener(TransportStateListener listener, Handler handler) {
- if (listener == null) {
- throw new IllegalArgumentException("Listener cannot be null");
- }
- synchronized (mLock) {
- if (getHandlerForListenerLocked(listener) != null) {
- Log.w(TAG, "Listener is already added, ignoring");
- return;
- }
- if (handler == null) {
- handler = new Handler();
- }
-
- MessageHandler msgHandler = new MessageHandler(handler.getLooper(), listener);
- mListeners.add(msgHandler);
- }
- }
-
- /**
- * Stop listening to changes in playback state.
- */
- public void removeStateListener(TransportStateListener listener) {
- if (listener == null) {
- throw new IllegalArgumentException("Listener cannot be null");
- }
- synchronized (mLock) {
- removeStateListenerLocked(listener);
- }
- }
-
- /**
- * Request that the player start its playback at its current position.
- */
- public void play() {
- try {
- mBinder.play();
- } catch (RemoteException e) {
- Log.wtf(TAG, "Error calling play.", e);
- }
- }
-
- /**
- * Request that the player pause its playback and stay at its current
- * position.
- */
- public void pause() {
- try {
- mBinder.pause();
- } catch (RemoteException e) {
- Log.wtf(TAG, "Error calling pause.", e);
- }
- }
-
- /**
- * Request that the player stop its playback; it may clear its state in
- * whatever way is appropriate.
- */
- public void stop() {
- try {
- mBinder.stop();
- } catch (RemoteException e) {
- Log.wtf(TAG, "Error calling stop.", e);
- }
- }
-
- /**
- * Move to a new location in the media stream.
- *
- * @param pos Position to move to, in milliseconds.
- */
- public void seekTo(long pos) {
- try {
- mBinder.seekTo(pos);
- } catch (RemoteException e) {
- Log.wtf(TAG, "Error calling seekTo.", e);
- }
- }
-
- /**
- * Start fast forwarding. If playback is already fast forwarding this may
- * increase the rate.
- */
- public void fastForward() {
- try {
- mBinder.fastForward();
- } catch (RemoteException e) {
- Log.wtf(TAG, "Error calling fastForward.", e);
- }
- }
-
- /**
- * Skip to the next item.
- */
- public void skipToNext() {
- try {
- mBinder.next();
- } catch (RemoteException e) {
- Log.wtf(TAG, "Error calling next.", e);
- }
- }
-
- /**
- * Start rewinding. If playback is already rewinding this may increase the
- * rate.
- */
- public void rewind() {
- try {
- mBinder.rewind();
- } catch (RemoteException e) {
- Log.wtf(TAG, "Error calling rewind.", e);
- }
- }
-
- /**
- * Skip to the previous item.
- */
- public void skipToPrevious() {
- try {
- mBinder.previous();
- } catch (RemoteException e) {
- Log.wtf(TAG, "Error calling previous.", e);
- }
- }
-
- /**
- * Rate the current content. This will cause the rating to be set for the
- * current user. The Rating type must match the type returned by
- * {@link #getRatingType()}.
- *
- * @param rating The rating to set for the current content
- */
- public void setRating(Rating rating) {
- try {
- mBinder.rate(rating);
- } catch (RemoteException e) {
- Log.wtf(TAG, "Error calling rate.", e);
- }
- }
-
- /**
- * Get the rating type supported by the session. One of:
- * <ul>
- * <li>{@link Rating#RATING_NONE}</li>
- * <li>{@link Rating#RATING_HEART}</li>
- * <li>{@link Rating#RATING_THUMB_UP_DOWN}</li>
- * <li>{@link Rating#RATING_3_STARS}</li>
- * <li>{@link Rating#RATING_4_STARS}</li>
- * <li>{@link Rating#RATING_5_STARS}</li>
- * <li>{@link Rating#RATING_PERCENTAGE}</li>
- * </ul>
- *
- * @return The supported rating type
- */
- public int getRatingType() {
- try {
- return mBinder.getRatingType();
- } catch (RemoteException e) {
- Log.wtf(TAG, "Error calling getRatingType.", e);
- return Rating.RATING_NONE;
- }
- }
-
- /**
- * Get the current playback state for this session.
- *
- * @return The current PlaybackState or null
- */
- public PlaybackState getPlaybackState() {
- try {
- return mBinder.getPlaybackState();
- } catch (RemoteException e) {
- Log.wtf(TAG, "Error calling getPlaybackState.", e);
- return null;
- }
- }
-
- /**
- * Get the current metadata for this session.
- *
- * @return The current MediaMetadata or null.
- */
- public MediaMetadata getMetadata() {
- try {
- return mBinder.getMetadata();
- } catch (RemoteException e) {
- Log.wtf(TAG, "Error calling getMetadata.", e);
- return null;
- }
- }
-
- /**
- * @hide
- */
- public final void postPlaybackStateChanged(PlaybackState state) {
- synchronized (mLock) {
- for (int i = mListeners.size() - 1; i >= 0; i--) {
- mListeners.get(i).post(MessageHandler.MSG_UPDATE_PLAYBACK_STATE, state);
- }
- }
- }
-
- /**
- * @hide
- */
- public final void postMetadataChanged(MediaMetadata metadata) {
- synchronized (mLock) {
- for (int i = mListeners.size() - 1; i >= 0; i--) {
- mListeners.get(i).post(MessageHandler.MSG_UPDATE_METADATA,
- metadata);
- }
- }
- }
-
- private MessageHandler getHandlerForListenerLocked(TransportStateListener listener) {
- for (int i = mListeners.size() - 1; i >= 0; i--) {
- MessageHandler handler = mListeners.get(i);
- if (listener == handler.mListener) {
- return handler;
- }
- }
- return null;
- }
-
- private boolean removeStateListenerLocked(TransportStateListener listener) {
- for (int i = mListeners.size() - 1; i >= 0; i--) {
- if (listener == mListeners.get(i).mListener) {
- mListeners.remove(i);
- return true;
- }
- }
- return false;
- }
-
- /**
- * Register using {@link #addStateListener} to receive updates when there
- * are playback changes on the session.
- */
- public static abstract class TransportStateListener {
- private MessageHandler mHandler;
- /**
- * Override to handle changes in playback state.
- *
- * @param state The new playback state of the session
- */
- public void onPlaybackStateChanged(PlaybackState state) {
- }
-
- /**
- * Override to handle changes to the current metadata.
- *
- * @see MediaMetadata
- * @param metadata The current metadata for the session or null
- */
- public void onMetadataChanged(MediaMetadata metadata) {
- }
-
- private void setHandler(Handler handler) {
- mHandler = new MessageHandler(handler.getLooper(), this);
- }
- }
-
- private static class MessageHandler extends Handler {
- private static final int MSG_UPDATE_PLAYBACK_STATE = 1;
- private static final int MSG_UPDATE_METADATA = 2;
-
- private TransportStateListener mListener;
-
- public MessageHandler(Looper looper, TransportStateListener cb) {
- super(looper, null, true);
- mListener = cb;
- }
-
- public void post(int msg, Object obj) {
- obtainMessage(msg, obj).sendToTarget();
- }
-
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case MSG_UPDATE_PLAYBACK_STATE:
- mListener.onPlaybackStateChanged((PlaybackState) msg.obj);
- break;
- case MSG_UPDATE_METADATA:
- mListener.onMetadataChanged((MediaMetadata) msg.obj);
- break;
- }
- }
- }
-
-}
diff --git a/media/java/android/media/session/TransportPerformer.java b/media/java/android/media/session/TransportPerformer.java
deleted file mode 100644
index 9c4c686..0000000
--- a/media/java/android/media/session/TransportPerformer.java
+++ /dev/null
@@ -1,352 +0,0 @@
-/*
- * Copyright (C) 2014 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 android.media.session;
-
-import android.media.AudioManager;
-import android.media.MediaMetadata;
-import android.media.Rating;
-import android.os.Handler;
-import android.os.Looper;
-import android.os.Message;
-import android.os.RemoteException;
-import android.util.Log;
-
-import java.lang.ref.WeakReference;
-import java.util.ArrayList;
-
-/**
- * Allows broadcasting of playback changes.
- */
-public final class TransportPerformer {
- private static final String TAG = "TransportPerformer";
- private final Object mLock = new Object();
- private final ArrayList<MessageHandler> mCallbacks = new ArrayList<MessageHandler>();
-
- private ISession mBinder;
-
- /**
- * @hide
- */
- public TransportPerformer(ISession binder) {
- mBinder = binder;
- }
-
- /**
- * Add a callback to receive updates on.
- *
- * @param callback The callback object
- */
- public void addCallback(Callback callback) {
- addCallback(callback, null);
- }
-
- /**
- * Add a callback to receive updates on. The updates will be posted to the
- * specified handler. If no handler is provided they will be posted to the
- * caller's thread.
- *
- * @param callback The callback to receive updates on
- * @param handler The handler to post the updates on
- */
- public void addCallback(Callback callback, Handler handler) {
- if (callback == null) {
- throw new IllegalArgumentException("Callback cannot be null");
- }
- synchronized (mLock) {
- if (getHandlerForCallbackLocked(callback) != null) {
- Log.w(TAG, "Callback is already added, ignoring");
- }
- if (handler == null) {
- handler = new Handler();
- }
- MessageHandler msgHandler = new MessageHandler(handler.getLooper(), callback);
- mCallbacks.add(msgHandler);
- }
- }
-
- /**
- * Stop receiving updates on the specified handler. If an update has already
- * been posted you may still receive it after this call returns.
- *
- * @param callback The callback to stop receiving updates on
- */
- public void removeCallback(Callback callback) {
- if (callback == null) {
- throw new IllegalArgumentException("Callback cannot be null");
- }
- synchronized (mLock) {
- removeCallbackLocked(callback);
- }
- }
-
- /**
- * Update the current playback state.
- *
- * @param state The current state of playback
- */
- public void setPlaybackState(PlaybackState state) {
- try {
- mBinder.setPlaybackState(state);
- } catch (RemoteException e) {
- Log.wtf(TAG, "Dead object in setPlaybackState.", e);
- }
- }
-
- /**
- * Update the current metadata. New metadata can be created using
- * {@link MediaMetadata.Builder}.
- *
- * @param metadata The new metadata
- */
- public void setMetadata(MediaMetadata metadata) {
- try {
- mBinder.setMetadata(metadata);
- } catch (RemoteException e) {
- Log.wtf(TAG, "Dead object in setPlaybackState.", e);
- }
- }
-
- /**
- * @hide
- */
- public void dispatchPlay() {
- post(MessageHandler.MESSAGE_PLAY);
- }
-
- /**
- * @hide
- */
- public void dispatchPause() {
- post(MessageHandler.MESSAGE_PAUSE);
- }
-
- /**
- * @hide
- */
- public void dispatchStop() {
- post(MessageHandler.MESSAGE_STOP);
- }
-
- /**
- * @hide
- */
- public void dispatchNext() {
- post(MessageHandler.MESSAGE_NEXT);
- }
-
- /**
- * @hide
- */
- public void dispatchPrevious() {
- post(MessageHandler.MESSAGE_PREVIOUS);
- }
-
- /**
- * @hide
- */
- public void dispatchFastForward() {
- post(MessageHandler.MESSAGE_FAST_FORWARD);
- }
-
- /**
- * @hide
- */
- public void dispatchRewind() {
- post(MessageHandler.MESSAGE_REWIND);
- }
-
- /**
- * @hide
- */
- public void dispatchSeekTo(long pos) {
- post(MessageHandler.MESSAGE_SEEK_TO, pos);
- }
-
- /**
- * @hide
- */
- public void dispatchRate(Rating rating) {
- post(MessageHandler.MESSAGE_RATE, rating);
- }
-
- private MessageHandler getHandlerForCallbackLocked(Callback callback) {
- for (int i = mCallbacks.size() - 1; i >= 0; i--) {
- MessageHandler handler = mCallbacks.get(i);
- if (callback == handler.mCallback) {
- return handler;
- }
- }
- return null;
- }
-
- private boolean removeCallbackLocked(Callback callback) {
- for (int i = mCallbacks.size() - 1; i >= 0; i--) {
- if (callback == mCallbacks.get(i).mCallback) {
- mCallbacks.remove(i);
- return true;
- }
- }
- return false;
- }
-
- private void post(int what, Object obj) {
- synchronized (mLock) {
- for (int i = mCallbacks.size() - 1; i >= 0; i--) {
- mCallbacks.get(i).post(what, obj);
- }
- }
- }
-
- private void post(int what) {
- post(what, null);
- }
-
- /**
- * Extend to handle transport controls. Callbacks can be registered using
- * {@link #addCallback}.
- */
- public static abstract class Callback {
-
- /**
- * Override to handle requests to begin playback.
- */
- public void onPlay() {
- }
-
- /**
- * Override to handle requests to pause playback.
- */
- public void onPause() {
- }
-
- /**
- * Override to handle requests to skip to the next media item.
- */
- public void onSkipToNext() {
- }
-
- /**
- * Override to handle requests to skip to the previous media item.
- */
- public void onSkipToPrevious() {
- }
-
- /**
- * Override to handle requests to fast forward.
- */
- public void onFastForward() {
- }
-
- /**
- * Override to handle requests to rewind.
- */
- public void onRewind() {
- }
-
- /**
- * Override to handle requests to stop playback.
- */
- public void onStop() {
- }
-
- /**
- * Override to handle requests to seek to a specific position in ms.
- *
- * @param pos New position to move to, in milliseconds.
- */
- public void onSeekTo(long pos) {
- }
-
- /**
- * Override to handle the item being rated.
- *
- * @param rating
- */
- public void onSetRating(Rating rating) {
- }
-
- /**
- * Report that audio focus has changed on the app. This only happens if
- * you have indicated you have started playing with
- * {@link #setPlaybackState}.
- *
- * @param focusChange The type of focus change, TBD.
- * @hide
- */
- public void onRouteFocusChange(int focusChange) {
- }
- }
-
- private class MessageHandler extends Handler {
- private static final int MESSAGE_PLAY = 1;
- private static final int MESSAGE_PAUSE = 2;
- private static final int MESSAGE_STOP = 3;
- private static final int MESSAGE_NEXT = 4;
- private static final int MESSAGE_PREVIOUS = 5;
- private static final int MESSAGE_FAST_FORWARD = 6;
- private static final int MESSAGE_REWIND = 7;
- private static final int MESSAGE_SEEK_TO = 8;
- private static final int MESSAGE_RATE = 9;
-
- private TransportPerformer.Callback mCallback;
-
- public MessageHandler(Looper looper, TransportPerformer.Callback cb) {
- super(looper);
- mCallback = cb;
- }
-
- public void post(int what, Object obj) {
- obtainMessage(what, obj).sendToTarget();
- }
-
- public void post(int what) {
- post(what, null);
- }
-
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case MESSAGE_PLAY:
- mCallback.onPlay();
- break;
- case MESSAGE_PAUSE:
- mCallback.onPause();
- break;
- case MESSAGE_STOP:
- mCallback.onStop();
- break;
- case MESSAGE_NEXT:
- mCallback.onSkipToNext();
- break;
- case MESSAGE_PREVIOUS:
- mCallback.onSkipToPrevious();
- break;
- case MESSAGE_FAST_FORWARD:
- mCallback.onFastForward();
- break;
- case MESSAGE_REWIND:
- mCallback.onRewind();
- break;
- case MESSAGE_SEEK_TO:
- mCallback.onSeekTo((Long) msg.obj);
- break;
- case MESSAGE_RATE:
- mCallback.onSetRating((Rating) msg.obj);
- break;
- }
- }
- }
-}
diff --git a/tests/OneMedia/src/com/android/onemedia/PlayerController.java b/tests/OneMedia/src/com/android/onemedia/PlayerController.java
index 9f7bb26..145b389 100644
--- a/tests/OneMedia/src/com/android/onemedia/PlayerController.java
+++ b/tests/OneMedia/src/com/android/onemedia/PlayerController.java
@@ -21,7 +21,6 @@
import android.media.session.RouteInfo;
import android.media.session.MediaSessionManager;
import android.media.session.PlaybackState;
-import android.media.session.TransportController;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
@@ -42,12 +41,11 @@
protected MediaController mController;
protected IPlayerService mBinder;
- protected TransportController mTransportControls;
+ protected MediaController.TransportControls mTransportControls;
private final Intent mServiceIntent;
private Context mContext;
private Listener mListener;
- private TransportListener mTransportListener = new TransportListener();
private SessionCallback mControllerCb;
private MediaSessionManager mManager;
private Handler mHandler = new Handler();
@@ -161,16 +159,13 @@
return;
}
mController.addCallback(mControllerCb, mHandler);
- mTransportControls = mController.getTransportController();
- if (mTransportControls != null) {
- mTransportControls.addStateListener(mTransportListener);
- }
+ mTransportControls = mController.getTransportControls();
Log.d(TAG, "Ready to use PlayerService");
if (mListener != null) {
mListener.onConnectionStateChange(STATE_CONNECTED);
if (mTransportControls != null) {
- mListener.onPlaybackStateChange(mTransportControls.getPlaybackState());
+ mListener.onPlaybackStateChange(mController.getPlaybackState());
}
}
}
@@ -181,9 +176,7 @@
public void onRouteChanged(RouteInfo route) {
// TODO
}
- }
- private class TransportListener extends TransportController.TransportStateListener {
@Override
public void onPlaybackStateChanged(PlaybackState state) {
if (state == null) {
diff --git a/tests/OneMedia/src/com/android/onemedia/PlayerSession.java b/tests/OneMedia/src/com/android/onemedia/PlayerSession.java
index 97a0a08..c1fa74f 100644
--- a/tests/OneMedia/src/com/android/onemedia/PlayerSession.java
+++ b/tests/OneMedia/src/com/android/onemedia/PlayerSession.java
@@ -25,7 +25,6 @@
import android.media.session.MediaSessionManager;
import android.media.session.MediaSessionToken;
import android.media.session.PlaybackState;
-import android.media.session.TransportPerformer;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
@@ -45,7 +44,6 @@
protected Renderer mRenderer;
protected MediaSession.Callback mCallback;
protected Renderer.Listener mRenderListener;
- protected TransportPerformer mPerformer;
protected PlaybackState mPlaybackState;
protected Listener mListener;
@@ -84,9 +82,8 @@
Log.d(TAG, "Creating session for package " + mContext.getBasePackageName());
mSession = man.createSession("OneMedia");
mSession.addCallback(mCallback);
- mPerformer = mSession.getTransportPerformer();
- mPerformer.addCallback(new TransportListener());
- mPerformer.setPlaybackState(mPlaybackState);
+ mSession.addTransportControlsCallback(new TransportListener());
+ mSession.setPlaybackState(mPlaybackState);
mSession.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
mSession.setRouteOptions(mRouteOptions);
mSession.setActive(true);
@@ -123,7 +120,7 @@
float rate = newState == PlaybackState.STATE_PLAYING ? 1 : 0;
long position = mRenderer == null ? -1 : mRenderer.getSeekPosition();
mPlaybackState.setState(newState, position, rate);
- mPerformer.setPlaybackState(mPlaybackState);
+ mSession.setPlaybackState(mPlaybackState);
}
public interface Listener {
@@ -139,7 +136,7 @@
if (error != null) {
mPlaybackState.setErrorMessage(error.getLocalizedMessage());
}
- mPerformer.setPlaybackState(mPlaybackState);
+ mSession.setPlaybackState(mPlaybackState);
if (mListener != null) {
mListener.onPlayStateChanged(mPlaybackState);
}
@@ -177,7 +174,7 @@
mPlaybackState.setErrorMessage("unkown state");
break;
}
- mPerformer.setPlaybackState(mPlaybackState);
+ mSession.setPlaybackState(mPlaybackState);
if (mListener != null) {
mListener.onPlayStateChanged(mPlaybackState);
}
@@ -192,7 +189,7 @@
Log.d(TAG, "Focus lost, changing state to " + Renderer.STATE_PAUSED);
long position = mRenderer == null ? -1 : mRenderer.getSeekPosition();
mPlaybackState.setState(PlaybackState.STATE_PAUSED, position, 0);
- mPerformer.setPlaybackState(mPlaybackState);
+ mSession.setPlaybackState(mPlaybackState);
if (mListener != null) {
mListener.onPlayStateChanged(mPlaybackState);
}
@@ -258,7 +255,7 @@
}
}
- private class TransportListener extends TransportPerformer.Callback {
+ private class TransportListener extends MediaSession.TransportControlsCallback {
@Override
public void onPlay() {
mRenderer.onPlay();