mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Some MediaSessionConnector simplifications
- Simplify logic for dispatching to the rating callback - Simplify CommandReceiver by removing getCommands. Simply calling receivers is simpler, and more flexible because it doesn't force implementations to define a static set of things that they handle up front. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=220788423
This commit is contained in:
parent
4754aa59bd
commit
8ec757ad2b
4 changed files with 29 additions and 50 deletions
|
|
@ -151,13 +151,8 @@ public class DefaultPlaybackController implements MediaSessionConnector.Playback
|
||||||
// CommandReceiver implementation.
|
// CommandReceiver implementation.
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] getCommands() {
|
public boolean onCommand(Player player, String command, Bundle extras, ResultReceiver cb) {
|
||||||
return null;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCommand(Player player, String command, Bundle extras, ResultReceiver cb) {
|
|
||||||
// Do nothing.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@ import com.google.android.exoplayer2.Timeline;
|
||||||
import com.google.android.exoplayer2.util.Assertions;
|
import com.google.android.exoplayer2.util.Assertions;
|
||||||
import com.google.android.exoplayer2.util.ErrorMessageProvider;
|
import com.google.android.exoplayer2.util.ErrorMessageProvider;
|
||||||
import com.google.android.exoplayer2.util.Util;
|
import com.google.android.exoplayer2.util.Util;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -85,11 +86,11 @@ public final class MediaSessionConnector {
|
||||||
/** Receiver of media commands sent by a media controller. */
|
/** Receiver of media commands sent by a media controller. */
|
||||||
public interface CommandReceiver {
|
public interface CommandReceiver {
|
||||||
/**
|
/**
|
||||||
* Returns the commands the receiver handles, or {@code null} if no commands need to be handled.
|
* See {@link MediaSessionCompat.Callback#onCommand(String, Bundle, ResultReceiver)}.
|
||||||
|
*
|
||||||
|
* @return Whether the command was handled by the receiver.
|
||||||
*/
|
*/
|
||||||
String[] getCommands();
|
boolean onCommand(Player player, String command, Bundle extras, ResultReceiver cb);
|
||||||
/** See {@link MediaSessionCompat.Callback#onCommand(String, Bundle, ResultReceiver)}. */
|
|
||||||
void onCommand(Player player, String command, Bundle extras, ResultReceiver cb);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Interface to which playback preparation actions are delegated. */
|
/** Interface to which playback preparation actions are delegated. */
|
||||||
|
|
@ -247,8 +248,6 @@ public final class MediaSessionConnector {
|
||||||
/** Callback receiving a user rating for the active media item. */
|
/** Callback receiving a user rating for the active media item. */
|
||||||
public interface RatingCallback extends CommandReceiver {
|
public interface RatingCallback extends CommandReceiver {
|
||||||
|
|
||||||
long ACTIONS = PlaybackStateCompat.ACTION_SET_RATING;
|
|
||||||
|
|
||||||
/** See {@link MediaSessionCompat.Callback#onSetRating(RatingCompat)}. */
|
/** See {@link MediaSessionCompat.Callback#onSetRating(RatingCompat)}. */
|
||||||
void onSetRating(Player player, RatingCompat rating);
|
void onSetRating(Player player, RatingCompat rating);
|
||||||
|
|
||||||
|
|
@ -297,7 +296,7 @@ public final class MediaSessionConnector {
|
||||||
private final ExoPlayerEventListener exoPlayerEventListener;
|
private final ExoPlayerEventListener exoPlayerEventListener;
|
||||||
private final MediaSessionCallback mediaSessionCallback;
|
private final MediaSessionCallback mediaSessionCallback;
|
||||||
private final PlaybackController playbackController;
|
private final PlaybackController playbackController;
|
||||||
private final Map<String, CommandReceiver> commandMap;
|
private final ArrayList<CommandReceiver> commandReceivers;
|
||||||
|
|
||||||
private Player player;
|
private Player player;
|
||||||
private CustomActionProvider[] customActionProviders;
|
private CustomActionProvider[] customActionProviders;
|
||||||
|
|
@ -385,7 +384,7 @@ public final class MediaSessionConnector {
|
||||||
mediaSessionCallback = new MediaSessionCallback();
|
mediaSessionCallback = new MediaSessionCallback();
|
||||||
exoPlayerEventListener = new ExoPlayerEventListener();
|
exoPlayerEventListener = new ExoPlayerEventListener();
|
||||||
customActionMap = Collections.emptyMap();
|
customActionMap = Collections.emptyMap();
|
||||||
commandMap = new HashMap<>();
|
commandReceivers = new ArrayList<>();
|
||||||
registerCommandReceiver(playbackController);
|
registerCommandReceiver(playbackController);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -593,19 +592,13 @@ public final class MediaSessionConnector {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void registerCommandReceiver(CommandReceiver commandReceiver) {
|
private void registerCommandReceiver(CommandReceiver commandReceiver) {
|
||||||
if (commandReceiver != null && commandReceiver.getCommands() != null) {
|
if (!commandReceivers.contains(commandReceiver)) {
|
||||||
for (String command : commandReceiver.getCommands()) {
|
commandReceivers.add(commandReceiver);
|
||||||
commandMap.put(command, commandReceiver);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void unregisterCommandReceiver(CommandReceiver commandReceiver) {
|
private void unregisterCommandReceiver(CommandReceiver commandReceiver) {
|
||||||
if (commandReceiver != null && commandReceiver.getCommands() != null) {
|
commandReceivers.remove(commandReceiver);
|
||||||
for (String command : commandReceiver.getCommands()) {
|
|
||||||
commandMap.remove(command);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private long buildPlaybackActions() {
|
private long buildPlaybackActions() {
|
||||||
|
|
@ -619,7 +612,7 @@ public final class MediaSessionConnector {
|
||||||
(QueueNavigator.ACTIONS & queueNavigator.getSupportedQueueNavigatorActions(player));
|
(QueueNavigator.ACTIONS & queueNavigator.getSupportedQueueNavigatorActions(player));
|
||||||
}
|
}
|
||||||
if (ratingCallback != null) {
|
if (ratingCallback != null) {
|
||||||
actions |= RatingCallback.ACTIONS;
|
actions |= PlaybackStateCompat.ACTION_SET_RATING;
|
||||||
}
|
}
|
||||||
return actions;
|
return actions;
|
||||||
}
|
}
|
||||||
|
|
@ -642,10 +635,6 @@ public final class MediaSessionConnector {
|
||||||
&& (playbackPreparer.getSupportedPrepareActions() & PlaybackPreparer.ACTIONS & action) != 0;
|
&& (playbackPreparer.getSupportedPrepareActions() & PlaybackPreparer.ACTIONS & action) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean canDispatchToRatingCallback(long action) {
|
|
||||||
return ratingCallback != null && (RatingCallback.ACTIONS & action) != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean canDispatchToPlaybackController(long action) {
|
private boolean canDispatchToPlaybackController(long action) {
|
||||||
return (playbackController.getSupportedPlaybackActions(player)
|
return (playbackController.getSupportedPlaybackActions(player)
|
||||||
& PlaybackController.ACTIONS
|
& PlaybackController.ACTIONS
|
||||||
|
|
@ -913,18 +902,18 @@ public final class MediaSessionConnector {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCustomAction(@NonNull String action, @Nullable Bundle extras) {
|
public void onCustomAction(@NonNull String action, @Nullable Bundle extras) {
|
||||||
Map<String, CustomActionProvider> actionMap = customActionMap;
|
if (customActionMap.containsKey(action)) {
|
||||||
if (actionMap.containsKey(action)) {
|
customActionMap.get(action).onCustomAction(action, extras);
|
||||||
actionMap.get(action).onCustomAction(action, extras);
|
|
||||||
invalidateMediaSessionPlaybackState();
|
invalidateMediaSessionPlaybackState();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCommand(String command, Bundle extras, ResultReceiver cb) {
|
public void onCommand(String command, Bundle extras, ResultReceiver cb) {
|
||||||
CommandReceiver commandReceiver = commandMap.get(command);
|
for (int i = 0; i < commandReceivers.size(); i++) {
|
||||||
if (commandReceiver != null) {
|
if (commandReceivers.get(i).onCommand(player, command, extras, cb)) {
|
||||||
commandReceiver.onCommand(player, command, extras, cb);
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -993,14 +982,14 @@ public final class MediaSessionConnector {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSetRating(RatingCompat rating) {
|
public void onSetRating(RatingCompat rating) {
|
||||||
if (canDispatchToRatingCallback(PlaybackStateCompat.ACTION_SET_RATING)) {
|
if (ratingCallback != null) {
|
||||||
ratingCallback.onSetRating(player, rating);
|
ratingCallback.onSetRating(player, rating);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSetRating(RatingCompat rating, Bundle extras) {
|
public void onSetRating(RatingCompat rating, Bundle extras) {
|
||||||
if (canDispatchToRatingCallback(PlaybackStateCompat.ACTION_SET_RATING)) {
|
if (ratingCallback != null) {
|
||||||
ratingCallback.onSetRating(player, rating, extras);
|
ratingCallback.onSetRating(player, rating, extras);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -193,20 +193,18 @@ public final class TimelineQueueEditor
|
||||||
|
|
||||||
// CommandReceiver implementation.
|
// CommandReceiver implementation.
|
||||||
|
|
||||||
@NonNull
|
|
||||||
@Override
|
@Override
|
||||||
public String[] getCommands() {
|
public boolean onCommand(Player player, String command, Bundle extras, ResultReceiver cb) {
|
||||||
return new String[] {COMMAND_MOVE_QUEUE_ITEM};
|
if (!COMMAND_MOVE_QUEUE_ITEM.equals(command)) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCommand(Player player, String command, Bundle extras, ResultReceiver cb) {
|
|
||||||
int from = extras.getInt(EXTRA_FROM_INDEX, C.INDEX_UNSET);
|
int from = extras.getInt(EXTRA_FROM_INDEX, C.INDEX_UNSET);
|
||||||
int to = extras.getInt(EXTRA_TO_INDEX, C.INDEX_UNSET);
|
int to = extras.getInt(EXTRA_TO_INDEX, C.INDEX_UNSET);
|
||||||
if (from != C.INDEX_UNSET && to != C.INDEX_UNSET) {
|
if (from != C.INDEX_UNSET && to != C.INDEX_UNSET) {
|
||||||
queueDataAdapter.move(from, to);
|
queueDataAdapter.move(from, to);
|
||||||
queueMediaSource.moveMediaSource(from, to);
|
queueMediaSource.moveMediaSource(from, to);
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -171,14 +171,11 @@ public abstract class TimelineQueueNavigator implements MediaSessionConnector.Qu
|
||||||
// CommandReceiver implementation.
|
// CommandReceiver implementation.
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] getCommands() {
|
public boolean onCommand(Player player, String command, Bundle extras, ResultReceiver cb) {
|
||||||
return null;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
// Helper methods.
|
||||||
public void onCommand(Player player, String command, Bundle extras, ResultReceiver cb) {
|
|
||||||
// Do nothing.
|
|
||||||
}
|
|
||||||
|
|
||||||
private void publishFloatingQueueWindow(Player player) {
|
private void publishFloatingQueueWindow(Player player) {
|
||||||
if (player.getCurrentTimeline().isEmpty()) {
|
if (player.getCurrentTimeline().isEmpty()) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue