Skip to content

Commit

Permalink
Add separate interface RatingCallback to handle user ratings.
Browse files Browse the repository at this point in the history
To support Google Assistant media apps need to support the action
ACTION_SET_RATING. Before this change developers have to use a QueueEditor for
this which does not have any other mandatory actions required for Assistant.
With this change developers can implement the rating action with the
PlaybackPreparer which they need to implement anyway to support Assistant.

https://developer.android.com/guide/topics/media-apps/interacting-with-assistant.html#transport_controls

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=188056722
  • Loading branch information
marcbaechinger authored and ojw28 committed Mar 7, 2018
1 parent 0160b87 commit 7a386d9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,24 @@ public interface CommandReceiver {
*/
public interface PlaybackPreparer extends CommandReceiver {

long ACTIONS = PlaybackStateCompat.ACTION_PREPARE
| PlaybackStateCompat.ACTION_PREPARE_FROM_MEDIA_ID
| PlaybackStateCompat.ACTION_PREPARE_FROM_SEARCH
| PlaybackStateCompat.ACTION_PREPARE_FROM_URI
| PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID
| PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH
| PlaybackStateCompat.ACTION_PLAY_FROM_URI;
long ACTIONS =
PlaybackStateCompat.ACTION_PREPARE
| PlaybackStateCompat.ACTION_PREPARE_FROM_MEDIA_ID
| PlaybackStateCompat.ACTION_PREPARE_FROM_SEARCH
| PlaybackStateCompat.ACTION_PREPARE_FROM_URI
| PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID
| PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH
| PlaybackStateCompat.ACTION_PLAY_FROM_URI;

/**
* Returns the actions which are supported by the preparer. The supported actions must be a
* bitmask combined out of {@link PlaybackStateCompat#ACTION_PREPARE},
* {@link PlaybackStateCompat#ACTION_PREPARE_FROM_MEDIA_ID},
* {@link PlaybackStateCompat#ACTION_PREPARE_FROM_SEARCH},
* {@link PlaybackStateCompat#ACTION_PREPARE_FROM_URI},
* {@link PlaybackStateCompat#ACTION_PLAY_FROM_MEDIA_ID},
* {@link PlaybackStateCompat#ACTION_PLAY_FROM_SEARCH} and
* {@link PlaybackStateCompat#ACTION_PLAY_FROM_URI}.
* bitmask combined out of {@link PlaybackStateCompat#ACTION_PREPARE}, {@link
* PlaybackStateCompat#ACTION_PREPARE_FROM_MEDIA_ID}, {@link
* PlaybackStateCompat#ACTION_PREPARE_FROM_SEARCH}, {@link
* PlaybackStateCompat#ACTION_PREPARE_FROM_URI}, {@link
* PlaybackStateCompat#ACTION_PLAY_FROM_MEDIA_ID}, {@link
* PlaybackStateCompat#ACTION_PLAY_FROM_SEARCH} and {@link
* PlaybackStateCompat#ACTION_PLAY_FROM_URI}.
*
* @return The bitmask of the supported media actions.
*/
Expand Down Expand Up @@ -264,15 +265,6 @@ public interface QueueNavigator extends CommandReceiver {
*/
public interface QueueEditor extends CommandReceiver {

long ACTIONS = PlaybackStateCompat.ACTION_SET_RATING;

/**
* Returns {@link PlaybackStateCompat#ACTION_SET_RATING} or {@code 0}. The Media API does
* not declare action constants for adding and removing queue items.
*
* @param player The {@link Player}.
*/
long getSupportedQueueEditorActions(@Nullable Player player);
/**
* See {@link MediaSessionCompat.Callback#onAddQueueItem(MediaDescriptionCompat description)}.
*/
Expand All @@ -291,9 +283,14 @@ public interface QueueEditor extends CommandReceiver {
* See {@link MediaSessionCompat.Callback#onRemoveQueueItemAt(int index)}.
*/
void onRemoveQueueItemAt(Player player, int index);
/**
* See {@link MediaSessionCompat.Callback#onSetRating(RatingCompat)}.
*/
}

/** 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);
}

Expand Down Expand Up @@ -341,6 +338,7 @@ public interface CustomActionProvider {
private PlaybackPreparer playbackPreparer;
private QueueNavigator queueNavigator;
private QueueEditor queueEditor;
private RatingCallback ratingCallback;
private ExoPlaybackException playbackException;

/**
Expand Down Expand Up @@ -471,6 +469,17 @@ public void setQueueEditor(QueueEditor queueEditor) {
: EDITOR_MEDIA_SESSION_FLAGS);
}

/**
* Sets the {@link RatingCallback} to handle user ratings.
*
* @param ratingCallback The rating callback.
*/
public void setRatingCallback(RatingCallback ratingCallback) {
unregisterCommandReceiver(this.ratingCallback);
this.ratingCallback = ratingCallback;
registerCommandReceiver(this.ratingCallback);
}

private void registerCommandReceiver(CommandReceiver commandReceiver) {
if (commandReceiver != null && commandReceiver.getCommands() != null) {
for (String command : commandReceiver.getCommands()) {
Expand Down Expand Up @@ -539,8 +548,8 @@ private long buildPlaybackActions() {
actions |= (QueueNavigator.ACTIONS & queueNavigator.getSupportedQueueNavigatorActions(
player));
}
if (queueEditor != null) {
actions |= (QueueEditor.ACTIONS & queueEditor.getSupportedQueueEditorActions(player));
if (ratingCallback != null) {
actions |= RatingCallback.ACTIONS;
}
return actions;
}
Expand Down Expand Up @@ -634,6 +643,10 @@ private boolean canDispatchToPlaybackPreparer(long action) {
& 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 & action) != 0;
Expand All @@ -644,11 +657,6 @@ private boolean canDispatchToQueueNavigator(long action) {
& QueueNavigator.ACTIONS & action) != 0;
}

private boolean canDispatchToQueueEditor(long action) {
return queueEditor != null && (queueEditor.getSupportedQueueEditorActions(player)
& QueueEditor.ACTIONS & action) != 0;
}

private class ExoPlayerEventListener extends Player.DefaultEventListener {

private int currentWindowIndex;
Expand Down Expand Up @@ -879,6 +887,13 @@ public void onPlayFromUri(Uri uri, Bundle extras) {
}
}

@Override
public void onSetRating(RatingCompat rating) {
if (canDispatchToRatingCallback(PlaybackStateCompat.ACTION_SET_RATING)) {
ratingCallback.onSetRating(player, rating);
}
}

@Override
public void onAddQueueItem(MediaDescriptionCompat description) {
if (queueEditor != null) {
Expand Down Expand Up @@ -907,13 +922,6 @@ public void onRemoveQueueItemAt(int index) {
}
}

@Override
public void onSetRating(RatingCompat rating) {
if (canDispatchToQueueEditor(PlaybackStateCompat.ACTION_SET_RATING)) {
queueEditor.onSetRating(player, rating);
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.media.MediaDescriptionCompat;
import android.support.v4.media.RatingCompat;
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.MediaSessionCompat;
import com.google.android.exoplayer2.C;
Expand Down Expand Up @@ -164,11 +163,6 @@ public TimelineQueueEditor(@NonNull MediaControllerCompat mediaController,
this.equalityChecker = equalityChecker;
}

@Override
public long getSupportedQueueEditorActions(@Nullable Player player) {
return 0;
}

@Override
public void onAddQueueItem(Player player, MediaDescriptionCompat description) {
onAddQueueItem(player, description, player.getCurrentTimeline().getWindowCount());
Expand Down Expand Up @@ -200,11 +194,6 @@ public void onRemoveQueueItemAt(Player player, int index) {
queueMediaSource.removeMediaSource(index);
}

@Override
public void onSetRating(Player player, RatingCompat rating) {
// Do nothing.
}

// CommandReceiver implementation.

@NonNull
Expand Down

0 comments on commit 7a386d9

Please sign in to comment.