import android.content.pm.ParceledListSlice;
import android.media.AudioAttributes;
import android.media.AudioManager;
-import android.media.MediaDescription;
import android.media.MediaMetadata;
import android.media.Rating;
import android.media.VolumeProvider;
import android.view.KeyEvent;
import java.lang.ref.WeakReference;
-import java.lang.UnsupportedOperationException;
import java.util.ArrayList;
import java.util.List;
}
/**
- * Get a {@link TransportControls} instance to send transport actions to this session.
+ * Get a {@link TransportControls} instance to send transport actions to
+ * the associated session.
*
* @return A transport controls instance.
*/
try {
return mSessionBinder.getPlaybackState();
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling getPlaybackState", e);
+ Log.wtf(TAG, "Error calling getPlaybackState.", e);
return null;
}
}
try {
return mSessionBinder.getMetadata();
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling getMetadata", e);
+ Log.wtf(TAG, "Error calling getMetadata.", e);
return null;
}
}
return queue.getList();
}
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling getQueue", e);
+ Log.wtf(TAG, "Error calling getQueue.", e);
}
return null;
}
/**
- * Add a queue item from the given {@code description} at the end of the play queue
- * of this session. Not all sessions may support this.
- *
- * @param description The {@link MediaDescription} for creating the
- * {@link MediaSession.QueueItem} to be inserted.
- * @throws UnsupportedOperationException If this session doesn't support this.
- * @see MediaSession#FLAG_HANDLES_QUEUE_COMMANDS
- */
- public void addQueueItem(MediaDescription description) {
- try {
- long flags = mSessionBinder.getFlags();
- if ((flags & MediaSession.FLAG_HANDLES_QUEUE_COMMANDS) == 0) {
- throw new UnsupportedOperationException(
- "This session doesn't support queue management operations");
- }
- mSessionBinder.addQueueItem(description);
- } catch (RemoteException e) {
- Log.wtf(TAG, "Error calling addQueueItem", e);
- }
- }
-
- /**
- * Add a queue item from the given {@code description} at the specified position
- * in the play queue of this session. Shifts the queue item currently at that position
- * (if any) and any subsequent queue items to the right (adds one to their indices).
- * Not all sessions may support this.
- *
- * @param description The {@link MediaDescription} for creating the
- * {@link MediaSession.QueueItem} to be inserted.
- * @param index The index at which the created {@link MediaSession.QueueItem} is to be inserted.
- * @throws UnsupportedOperationException If this session doesn't support this.
- * @see MediaSession#FLAG_HANDLES_QUEUE_COMMANDS
- */
- public void addQueueItem(MediaDescription description, int index) {
- try {
- long flags = mSessionBinder.getFlags();
- if ((flags & MediaSession.FLAG_HANDLES_QUEUE_COMMANDS) == 0) {
- throw new UnsupportedOperationException(
- "This session doesn't support queue management operations");
- }
- mSessionBinder.addQueueItemAt(description, index);
- } catch (RemoteException e) {
- Log.wtf(TAG, "Error calling addQueueItemAt", e);
- }
- }
-
- /**
- * Remove the first occurrence of the specified {@link MediaSession.QueueItem}
- * with the given {@link MediaDescription description} in the play queue of the associated
- * session. Not all sessions may support this.
- *
- * @param description The {@link MediaDescription} for denoting the
- * {@link MediaSession.QueueItem} to be removed.
- * @throws UnsupportedOperationException If this session doesn't support this.
- * @see MediaSession#FLAG_HANDLES_QUEUE_COMMANDS
- */
- public void removeQueueItem(MediaDescription description) {
- try {
- long flags = mSessionBinder.getFlags();
- if ((flags & MediaSession.FLAG_HANDLES_QUEUE_COMMANDS) == 0) {
- throw new UnsupportedOperationException(
- "This session doesn't support queue management operations");
- }
- mSessionBinder.removeQueueItem(description);
- } catch (RemoteException e) {
- Log.wtf(TAG, "Error calling removeQueueItem", e);
- }
- }
-
- /**
- * Remove an queue item at the specified position in the play queue
- * of this session. Not all sessions may support this.
- *
- * @param index The index of the element to be removed.
- * @throws UnsupportedOperationException If this session doesn't support this.
- * @see MediaSession#FLAG_HANDLES_QUEUE_COMMANDS
- */
- public void removeQueueItemAt(int index) {
- try {
- long flags = mSessionBinder.getFlags();
- if ((flags & MediaSession.FLAG_HANDLES_QUEUE_COMMANDS) == 0) {
- throw new UnsupportedOperationException(
- "This session doesn't support queue management operations");
- }
- mSessionBinder.removeQueueItemAt(index);
- } catch (RemoteException e) {
- Log.wtf(TAG, "Error calling removeQueueItemAt", e);
- }
- }
-
- /**
* Get the queue title for this session.
*/
public @Nullable CharSequence getQueueTitle() {
try {
return mSessionBinder.getRatingType();
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling getRatingType", e);
+ Log.wtf(TAG, "Error calling getRatingType.", e);
return Rating.RATING_NONE;
}
}
try {
return mSessionBinder.getRepeatMode();
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling getRepeatMode", e);
+ Log.wtf(TAG, "Error calling getRepeatMode.", e);
return PlaybackState.REPEAT_MODE_NONE;
}
}
try {
return mSessionBinder.isShuffleModeEnabled();
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling isShuffleModeEnabled", e);
+ Log.wtf(TAG, "Error calling isShuffleModeEnabled.", e);
return false;
}
}
try {
return mSessionBinder.getFlags();
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling getFlags", e);
+ Log.wtf(TAG, "Error calling getFlags.", e);
}
return 0;
}
result.maxVolume, result.currentVolume);
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling getAudioInfo", e);
+ Log.wtf(TAG, "Error calling getAudioInfo.", e);
}
return null;
}
try {
return mSessionBinder.getLaunchPendingIntent();
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling getPendingIntent", e);
+ Log.wtf(TAG, "Error calling getPendingIntent.", e);
}
return null;
}
try {
mSessionBinder.setVolumeTo(value, flags, mContext.getPackageName());
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling setVolumeTo", e);
+ Log.wtf(TAG, "Error calling setVolumeTo.", e);
}
}
try {
mSessionBinder.adjustVolume(direction, flags, mContext.getPackageName());
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling adjustVolumeBy", e);
+ Log.wtf(TAG, "Error calling adjustVolumeBy.", e);
}
}
try {
mSessionBinder.sendCommand(command, args, cb);
} catch (RemoteException e) {
- Log.d(TAG, "Dead object in sendCommand", e);
+ Log.d(TAG, "Dead object in sendCommand.", e);
}
}
try {
mPackageName = mSessionBinder.getPackageName();
} catch (RemoteException e) {
- Log.d(TAG, "Dead object in getPackageName", e);
+ Log.d(TAG, "Dead object in getPackageName.", e);
}
}
return mPackageName;
try {
mTag = mSessionBinder.getTag();
} catch (RemoteException e) {
- Log.d(TAG, "Dead object in getTag", e);
+ Log.d(TAG, "Dead object in getTag.", e);
}
}
return mTag;
try {
mSessionBinder.prepare();
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling prepare", e);
+ Log.wtf(TAG, "Error calling prepare.", e);
}
}
public void prepareFromMediaId(String mediaId, Bundle extras) {
if (TextUtils.isEmpty(mediaId)) {
throw new IllegalArgumentException(
- "You must specify a non-empty String for prepareFromMediaId");
+ "You must specify a non-empty String for prepareFromMediaId.");
}
try {
mSessionBinder.prepareFromMediaId(mediaId, extras);
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling prepare(" + mediaId + ")", e);
+ Log.wtf(TAG, "Error calling prepare(" + mediaId + ").", e);
}
}
try {
mSessionBinder.prepareFromSearch(query, extras);
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling prepare(" + query + ")", e);
+ Log.wtf(TAG, "Error calling prepare(" + query + ").", e);
}
}
public void prepareFromUri(Uri uri, Bundle extras) {
if (uri == null || Uri.EMPTY.equals(uri)) {
throw new IllegalArgumentException(
- "You must specify a non-empty Uri for prepareFromUri");
+ "You must specify a non-empty Uri for prepareFromUri.");
}
try {
mSessionBinder.prepareFromUri(uri, extras);
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling prepare(" + uri + ")", e);
+ Log.wtf(TAG, "Error calling prepare(" + uri + ").", e);
}
}
try {
mSessionBinder.play();
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling play", e);
+ Log.wtf(TAG, "Error calling play.", e);
}
}
public void playFromMediaId(String mediaId, Bundle extras) {
if (TextUtils.isEmpty(mediaId)) {
throw new IllegalArgumentException(
- "You must specify a non-empty String for playFromMediaId");
+ "You must specify a non-empty String for playFromMediaId.");
}
try {
mSessionBinder.playFromMediaId(mediaId, extras);
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling play(" + mediaId + ")", e);
+ Log.wtf(TAG, "Error calling play(" + mediaId + ").", e);
}
}
try {
mSessionBinder.playFromSearch(query, extras);
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling play(" + query + ")", e);
+ Log.wtf(TAG, "Error calling play(" + query + ").", e);
}
}
public void playFromUri(Uri uri, Bundle extras) {
if (uri == null || Uri.EMPTY.equals(uri)) {
throw new IllegalArgumentException(
- "You must specify a non-empty Uri for playFromUri");
+ "You must specify a non-empty Uri for playFromUri.");
}
try {
mSessionBinder.playFromUri(uri, extras);
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling play(" + uri + ")", e);
+ Log.wtf(TAG, "Error calling play(" + uri + ").", e);
}
}
try {
mSessionBinder.skipToQueueItem(id);
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling skipToItem(" + id + ")", e);
+ Log.wtf(TAG, "Error calling skipToItem(" + id + ").", e);
}
}
try {
mSessionBinder.pause();
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling pause", e);
+ Log.wtf(TAG, "Error calling pause.", e);
}
}
try {
mSessionBinder.stop();
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling stop", e);
+ Log.wtf(TAG, "Error calling stop.", e);
}
}
try {
mSessionBinder.seekTo(pos);
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling seekTo", e);
+ Log.wtf(TAG, "Error calling seekTo.", e);
}
}
try {
mSessionBinder.fastForward();
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling fastForward", e);
+ Log.wtf(TAG, "Error calling fastForward.", e);
}
}
try {
mSessionBinder.next();
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling next", e);
+ Log.wtf(TAG, "Error calling next.", e);
}
}
try {
mSessionBinder.rewind();
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling rewind", e);
+ Log.wtf(TAG, "Error calling rewind.", e);
}
}
try {
mSessionBinder.previous();
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling previous", e);
+ Log.wtf(TAG, "Error calling previous.", e);
}
}
try {
mSessionBinder.rate(rating);
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling rate", e);
+ Log.wtf(TAG, "Error calling rate.", e);
}
}
try {
mSessionBinder.repeatMode(repeatMode);
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling setRepeatMode", e);
+ Log.wtf(TAG, "Error calling setRepeatMode.", e);
}
}
try {
mSessionBinder.shuffleMode(enabled);
} catch (RemoteException e) {
- Log.wtf(TAG, "Error calling shuffleMode", e);
+ Log.wtf(TAG, "Error calling shuffleMode.", e);
}
}
public void sendCustomAction(@NonNull PlaybackState.CustomAction customAction,
@Nullable Bundle args) {
if (customAction == null) {
- throw new IllegalArgumentException("CustomAction cannot be null");
+ throw new IllegalArgumentException("CustomAction cannot be null.");
}
sendCustomAction(customAction.getAction(), args);
}
*/
public void sendCustomAction(@NonNull String action, @Nullable Bundle args) {
if (TextUtils.isEmpty(action)) {
- throw new IllegalArgumentException("CustomAction cannot be null");
+ throw new IllegalArgumentException("CustomAction cannot be null.");
}
try {
mSessionBinder.sendCustomAction(action, args);
} catch (RemoteException e) {
- Log.d(TAG, "Dead object in sendCustomAction", e);
+ Log.d(TAG, "Dead object in sendCustomAction.", e);
}
}
}
public static final int FLAG_HANDLES_TRANSPORT_CONTROLS = 1 << 1;
/**
- * Set this flag on the session to indicate that it handles queue
- * management commands through its {@link Callback}.
- */
- public static final int FLAG_HANDLES_QUEUE_COMMANDS = 1 << 2;
-
- /**
* System only flag for a session that needs to have priority over all other
* sessions. This flag ensures this session will receive media button events
* regardless of the current ordering in the system.
@IntDef(flag = true, value = {
FLAG_HANDLES_MEDIA_BUTTONS,
FLAG_HANDLES_TRANSPORT_CONTROLS,
- FLAG_HANDLES_QUEUE_COMMANDS,
FLAG_EXCLUSIVE_GLOBAL_PRIORITY })
public @interface SessionFlags { }
postToCallback(CallbackMessageHandler.MSG_CUSTOM_ACTION, action, args);
}
- private void dispatchAddQueueItem(MediaDescription description) {
- postToCallback(CallbackMessageHandler.MSG_ADD_QUEUE_ITEM, description);
- }
-
- private void dispatchAddQueueItem(MediaDescription description, int index) {
- postToCallback(CallbackMessageHandler.MSG_ADD_QUEUE_ITEM_AT, description, index);
- }
-
- private void dispatchRemoveQueueItem(MediaDescription description) {
- postToCallback(CallbackMessageHandler.MSG_REMOVE_QUEUE_ITEM, description);
- }
-
- private void dispatchRemoveQueueItemAt(int index) {
- postToCallback(CallbackMessageHandler.MSG_REMOVE_QUEUE_ITEM_AT, index);
- }
-
private void dispatchMediaButton(Intent mediaButtonIntent) {
postToCallback(CallbackMessageHandler.MSG_MEDIA_BUTTON, mediaButtonIntent);
}
postToCallback(CallbackMessageHandler.MSG_COMMAND, cmd);
}
- private void postToCallback(int what, int arg1) {
- postToCallback(what, null, arg1);
- }
-
private void postToCallback(int what, Object obj) {
postToCallback(what, obj, null);
}
- private void postToCallback(int what, Object obj, int arg1) {
- synchronized (mLock) {
- if (mCallback != null) {
- mCallback.post(what, obj, arg1);
- }
- }
- }
-
private void postToCallback(int what, Object obj, Bundle extras) {
synchronized (mLock) {
if (mCallback != null) {
*/
public void onCustomAction(@NonNull String action, @Nullable Bundle extras) {
}
-
- /**
- * Called when a {@link MediaController} wants to add a {@link QueueItem} with the given
- * {@link MediaDescription description} at the end of the play queue.
- *
- * @param description The {@link MediaDescription} for creating the {@link QueueItem} to be
- * inserted.
- */
- public void onAddQueueItem(MediaDescription description) {
- }
-
- /**
- * Called when a {@link MediaController} wants to add a {@link QueueItem} with the given
- * {@link MediaDescription description} at the specified position in the play queue.
- *
- * @param description The {@link MediaDescription} for creating the {@link QueueItem} to be
- * inserted.
- * @param index The index at which the created {@link QueueItem} is to be inserted.
- */
- public void onAddQueueItem(MediaDescription description, int index) {
- }
-
- /**
- * Called when a {@link MediaController} wants to remove the first occurrence of the
- * specified {@link QueueItem} with the given {@link MediaDescription description}
- * in the play queue.
- *
- * @param description The {@link MediaDescription} for denoting the {@link QueueItem} to be
- * removed.
- */
- public void onRemoveQueueItem(MediaDescription description) {
- }
-
- /**
- * Called when a {@link MediaController} wants to remove a {@link QueueItem} at the
- * specified position in the play queue.
- *
- * @param index The index of the element to be removed.
- */
- public void onRemoveQueueItemAt(int index) {
- }
}
/**
}
@Override
- public void onAddQueueItem(MediaDescription description) {
- MediaSession session = mMediaSession.get();
- if (session != null) {
- session.dispatchAddQueueItem(description);
- }
- }
-
- @Override
- public void onAddQueueItemAt(MediaDescription description, int index) {
- MediaSession session = mMediaSession.get();
- if (session != null) {
- session.dispatchAddQueueItem(description, index);
- }
- }
-
- @Override
- public void onRemoveQueueItem(MediaDescription description) {
- MediaSession session = mMediaSession.get();
- if (session != null) {
- session.dispatchRemoveQueueItem(description);
- }
- }
-
- @Override
- public void onRemoveQueueItemAt(int index) {
- MediaSession session = mMediaSession.get();
- if (session != null) {
- session.dispatchRemoveQueueItemAt(index);
- }
- }
-
- @Override
public void onAdjustVolume(int direction) {
MediaSession session = mMediaSession.get();
if (session != null) {
private static final int MSG_CUSTOM_ACTION = 22;
private static final int MSG_ADJUST_VOLUME = 23;
private static final int MSG_SET_VOLUME = 24;
- private static final int MSG_ADD_QUEUE_ITEM = 25;
- private static final int MSG_ADD_QUEUE_ITEM_AT = 26;
- private static final int MSG_REMOVE_QUEUE_ITEM = 27;
- private static final int MSG_REMOVE_QUEUE_ITEM_AT = 28;
private MediaSession.Callback mCallback;
mCallback.onSetRating((Rating) msg.obj);
break;
case MSG_REPEAT_MODE:
- mCallback.onSetRepeatMode(msg.arg1);
+ mCallback.onSetRepeatMode((int) msg.obj);
break;
case MSG_SHUFFLE_MODE:
mCallback.onSetShuffleModeEnabled((boolean) msg.obj);
case MSG_CUSTOM_ACTION:
mCallback.onCustomAction((String) msg.obj, msg.getData());
break;
- case MSG_ADD_QUEUE_ITEM:
- mCallback.onAddQueueItem((MediaDescription) msg.obj);
- break;
- case MSG_ADD_QUEUE_ITEM_AT:
- mCallback.onAddQueueItem((MediaDescription) msg.obj, msg.arg1);
- break;
- case MSG_REMOVE_QUEUE_ITEM:
- mCallback.onRemoveQueueItem((MediaDescription) msg.obj);
- break;
- case MSG_REMOVE_QUEUE_ITEM_AT:
- mCallback.onRemoveQueueItemAt(msg.arg1);
- break;
case MSG_ADJUST_VOLUME:
synchronized (mLock) {
vp = mVolumeProvider;
}
if (vp != null) {
- vp.onAdjustVolume(msg.arg1);
+ vp.onAdjustVolume((int) msg.obj);
}
break;
case MSG_SET_VOLUME:
vp = mVolumeProvider;
}
if (vp != null) {
- vp.onSetVolumeTo(msg.arg1);
+ vp.onSetVolumeTo((int) msg.obj);
}
break;
}