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:
olly 2018-11-09 05:33:57 -08:00 committed by Oliver Woodman
parent 4754aa59bd
commit 8ec757ad2b
4 changed files with 29 additions and 50 deletions

View file

@ -151,13 +151,8 @@ public class DefaultPlaybackController implements MediaSessionConnector.Playback
// CommandReceiver implementation.
@Override
public String[] getCommands() {
return null;
}
@Override
public void onCommand(Player player, String command, Bundle extras, ResultReceiver cb) {
// Do nothing.
public boolean onCommand(Player player, String command, Bundle extras, ResultReceiver cb) {
return false;
}
}

View file

@ -40,6 +40,7 @@ import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.ErrorMessageProvider;
import com.google.android.exoplayer2.util.Util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@ -85,11 +86,11 @@ public final class MediaSessionConnector {
/** Receiver of media commands sent by a media controller. */
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();
/** See {@link MediaSessionCompat.Callback#onCommand(String, Bundle, ResultReceiver)}. */
void onCommand(Player player, String command, Bundle extras, ResultReceiver cb);
boolean onCommand(Player player, String command, Bundle extras, ResultReceiver cb);
}
/** 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. */
public interface RatingCallback extends CommandReceiver {
long ACTIONS = PlaybackStateCompat.ACTION_SET_RATING;
/** See {@link MediaSessionCompat.Callback#onSetRating(RatingCompat)}. */
void onSetRating(Player player, RatingCompat rating);
@ -297,7 +296,7 @@ public final class MediaSessionConnector {
private final ExoPlayerEventListener exoPlayerEventListener;
private final MediaSessionCallback mediaSessionCallback;
private final PlaybackController playbackController;
private final Map<String, CommandReceiver> commandMap;
private final ArrayList<CommandReceiver> commandReceivers;
private Player player;
private CustomActionProvider[] customActionProviders;
@ -385,7 +384,7 @@ public final class MediaSessionConnector {
mediaSessionCallback = new MediaSessionCallback();
exoPlayerEventListener = new ExoPlayerEventListener();
customActionMap = Collections.emptyMap();
commandMap = new HashMap<>();
commandReceivers = new ArrayList<>();
registerCommandReceiver(playbackController);
}
@ -593,19 +592,13 @@ public final class MediaSessionConnector {
}
private void registerCommandReceiver(CommandReceiver commandReceiver) {
if (commandReceiver != null && commandReceiver.getCommands() != null) {
for (String command : commandReceiver.getCommands()) {
commandMap.put(command, commandReceiver);
}
if (!commandReceivers.contains(commandReceiver)) {
commandReceivers.add(commandReceiver);
}
}
private void unregisterCommandReceiver(CommandReceiver commandReceiver) {
if (commandReceiver != null && commandReceiver.getCommands() != null) {
for (String command : commandReceiver.getCommands()) {
commandMap.remove(command);
}
}
commandReceivers.remove(commandReceiver);
}
private long buildPlaybackActions() {
@ -619,7 +612,7 @@ public final class MediaSessionConnector {
(QueueNavigator.ACTIONS & queueNavigator.getSupportedQueueNavigatorActions(player));
}
if (ratingCallback != null) {
actions |= RatingCallback.ACTIONS;
actions |= PlaybackStateCompat.ACTION_SET_RATING;
}
return actions;
}
@ -642,10 +635,6 @@ public final class MediaSessionConnector {
&& (playbackPreparer.getSupportedPrepareActions() & PlaybackPreparer.ACTIONS & action) != 0;
}
private boolean canDispatchToRatingCallback(long action) {
return ratingCallback != null && (RatingCallback.ACTIONS & action) != 0;
}
private boolean canDispatchToPlaybackController(long action) {
return (playbackController.getSupportedPlaybackActions(player)
& PlaybackController.ACTIONS
@ -913,18 +902,18 @@ public final class MediaSessionConnector {
@Override
public void onCustomAction(@NonNull String action, @Nullable Bundle extras) {
Map<String, CustomActionProvider> actionMap = customActionMap;
if (actionMap.containsKey(action)) {
actionMap.get(action).onCustomAction(action, extras);
if (customActionMap.containsKey(action)) {
customActionMap.get(action).onCustomAction(action, extras);
invalidateMediaSessionPlaybackState();
}
}
@Override
public void onCommand(String command, Bundle extras, ResultReceiver cb) {
CommandReceiver commandReceiver = commandMap.get(command);
if (commandReceiver != null) {
commandReceiver.onCommand(player, command, extras, cb);
for (int i = 0; i < commandReceivers.size(); i++) {
if (commandReceivers.get(i).onCommand(player, command, extras, cb)) {
return;
}
}
}
@ -993,14 +982,14 @@ public final class MediaSessionConnector {
@Override
public void onSetRating(RatingCompat rating) {
if (canDispatchToRatingCallback(PlaybackStateCompat.ACTION_SET_RATING)) {
if (ratingCallback != null) {
ratingCallback.onSetRating(player, rating);
}
}
@Override
public void onSetRating(RatingCompat rating, Bundle extras) {
if (canDispatchToRatingCallback(PlaybackStateCompat.ACTION_SET_RATING)) {
if (ratingCallback != null) {
ratingCallback.onSetRating(player, rating, extras);
}
}

View file

@ -193,20 +193,18 @@ public final class TimelineQueueEditor
// CommandReceiver implementation.
@NonNull
@Override
public String[] getCommands() {
return new String[] {COMMAND_MOVE_QUEUE_ITEM};
}
@Override
public void onCommand(Player player, String command, Bundle extras, ResultReceiver cb) {
public boolean onCommand(Player player, String command, Bundle extras, ResultReceiver cb) {
if (!COMMAND_MOVE_QUEUE_ITEM.equals(command)) {
return false;
}
int from = extras.getInt(EXTRA_FROM_INDEX, C.INDEX_UNSET);
int to = extras.getInt(EXTRA_TO_INDEX, C.INDEX_UNSET);
if (from != C.INDEX_UNSET && to != C.INDEX_UNSET) {
queueDataAdapter.move(from, to);
queueMediaSource.moveMediaSource(from, to);
}
return true;
}
}

View file

@ -171,14 +171,11 @@ public abstract class TimelineQueueNavigator implements MediaSessionConnector.Qu
// CommandReceiver implementation.
@Override
public String[] getCommands() {
return null;
public boolean onCommand(Player player, String command, Bundle extras, ResultReceiver cb) {
return false;
}
@Override
public void onCommand(Player player, String command, Bundle extras, ResultReceiver cb) {
// Do nothing.
}
// Helper methods.
private void publishFloatingQueueWindow(Player player) {
if (player.getCurrentTimeline().isEmpty()) {