Commit 5dd9ef00 by bachinger Committed by Oliver Woodman

add invalidate methods for playback state, metadata and queue of the media session

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=202136530
parent 3ec0cae0
...@@ -429,8 +429,8 @@ public final class MediaSessionConnector { ...@@ -429,8 +429,8 @@ public final class MediaSessionConnector {
mediaSession.setCallback(mediaSessionCallback, handler); mediaSession.setCallback(mediaSessionCallback, handler);
player.addListener(exoPlayerEventListener); player.addListener(exoPlayerEventListener);
} }
updateMediaSessionPlaybackState(); invalidateMediaSessionPlaybackState();
updateMediaSessionMetadata(); invalidateMediaSessionMetadata();
} }
/** /**
...@@ -442,7 +442,7 @@ public final class MediaSessionConnector { ...@@ -442,7 +442,7 @@ public final class MediaSessionConnector {
@Nullable ErrorMessageProvider<? super ExoPlaybackException> errorMessageProvider) { @Nullable ErrorMessageProvider<? super ExoPlaybackException> errorMessageProvider) {
if (this.errorMessageProvider != errorMessageProvider) { if (this.errorMessageProvider != errorMessageProvider) {
this.errorMessageProvider = errorMessageProvider; this.errorMessageProvider = errorMessageProvider;
updateMediaSessionPlaybackState(); invalidateMediaSessionPlaybackState();
} }
} }
...@@ -494,13 +494,19 @@ public final class MediaSessionConnector { ...@@ -494,13 +494,19 @@ public final class MediaSessionConnector {
* <p>Apps normally only need to call this method when the backing data for a given media item has * <p>Apps normally only need to call this method when the backing data for a given media item has
* changed and the metadata should be updated immediately. * changed and the metadata should be updated immediately.
*/ */
public final void updateMediaSessionMetadata() { public final void invalidateMediaSessionMetadata() {
if (mediaMetadataProvider != null) { if (mediaMetadataProvider != null && player != null) {
mediaSession.setMetadata(mediaMetadataProvider.getMetadata(player)); mediaSession.setMetadata(mediaMetadataProvider.getMetadata(player));
} }
} }
private void updateMediaSessionPlaybackState() { /**
* Updates the playback state of the media session.
*
* <p>Apps normally only need to call this method when the custom actions provided by a {@link
* CustomActionProvider} changed and the playback state needs to be updated immediately.
*/
public final void invalidateMediaSessionPlaybackState() {
PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder(); PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder();
if (player == null) { if (player == null) {
builder.setActions(buildPlaybackActions()).setState(PlaybackStateCompat.STATE_NONE, 0, 0, 0); builder.setActions(buildPlaybackActions()).setState(PlaybackStateCompat.STATE_NONE, 0, 0, 0);
...@@ -548,6 +554,19 @@ public final class MediaSessionConnector { ...@@ -548,6 +554,19 @@ public final class MediaSessionConnector {
mediaSession.setPlaybackState(builder.build()); mediaSession.setPlaybackState(builder.build());
} }
/**
* Updates the queue of the media session by calling {@link
* QueueNavigator#onTimelineChanged(Player)}.
*
* <p>Apps normally only need to call this method when the backing data for a given queue item has
* changed and the queue should be updated immediately.
*/
public final void invalidateMediaSessionQueue() {
if (queueNavigator != null && player != null) {
queueNavigator.onTimelineChanged(player);
}
}
private void registerCommandReceiver(CommandReceiver commandReceiver) { private void registerCommandReceiver(CommandReceiver commandReceiver) {
if (commandReceiver != null && commandReceiver.getCommands() != null) { if (commandReceiver != null && commandReceiver.getCommands() != null) {
for (String command : commandReceiver.getCommands()) { for (String command : commandReceiver.getCommands()) {
...@@ -728,19 +747,19 @@ public final class MediaSessionConnector { ...@@ -728,19 +747,19 @@ public final class MediaSessionConnector {
int windowIndex = player.getCurrentWindowIndex(); int windowIndex = player.getCurrentWindowIndex();
if (queueNavigator != null) { if (queueNavigator != null) {
queueNavigator.onTimelineChanged(player); queueNavigator.onTimelineChanged(player);
updateMediaSessionPlaybackState(); invalidateMediaSessionPlaybackState();
} else if (currentWindowCount != windowCount || currentWindowIndex != windowIndex) { } else if (currentWindowCount != windowCount || currentWindowIndex != windowIndex) {
// active queue item and queue navigation actions may need to be updated // active queue item and queue navigation actions may need to be updated
updateMediaSessionPlaybackState(); invalidateMediaSessionPlaybackState();
} }
currentWindowCount = windowCount; currentWindowCount = windowCount;
currentWindowIndex = windowIndex; currentWindowIndex = windowIndex;
updateMediaSessionMetadata(); invalidateMediaSessionMetadata();
} }
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
updateMediaSessionPlaybackState(); invalidateMediaSessionPlaybackState();
} }
@Override @Override
...@@ -751,7 +770,7 @@ public final class MediaSessionConnector { ...@@ -751,7 +770,7 @@ public final class MediaSessionConnector {
: repeatMode == Player.REPEAT_MODE_ALL : repeatMode == Player.REPEAT_MODE_ALL
? PlaybackStateCompat.REPEAT_MODE_ALL ? PlaybackStateCompat.REPEAT_MODE_ALL
: PlaybackStateCompat.REPEAT_MODE_NONE); : PlaybackStateCompat.REPEAT_MODE_NONE);
updateMediaSessionPlaybackState(); invalidateMediaSessionPlaybackState();
} }
@Override @Override
...@@ -760,7 +779,7 @@ public final class MediaSessionConnector { ...@@ -760,7 +779,7 @@ public final class MediaSessionConnector {
shuffleModeEnabled shuffleModeEnabled
? PlaybackStateCompat.SHUFFLE_MODE_ALL ? PlaybackStateCompat.SHUFFLE_MODE_ALL
: PlaybackStateCompat.SHUFFLE_MODE_NONE); : PlaybackStateCompat.SHUFFLE_MODE_NONE);
updateMediaSessionPlaybackState(); invalidateMediaSessionPlaybackState();
} }
@Override @Override
...@@ -772,16 +791,16 @@ public final class MediaSessionConnector { ...@@ -772,16 +791,16 @@ public final class MediaSessionConnector {
currentWindowIndex = player.getCurrentWindowIndex(); currentWindowIndex = player.getCurrentWindowIndex();
// Update playback state after queueNavigator.onCurrentWindowIndexChanged has been called // Update playback state after queueNavigator.onCurrentWindowIndexChanged has been called
// and before updating metadata. // and before updating metadata.
updateMediaSessionPlaybackState(); invalidateMediaSessionPlaybackState();
updateMediaSessionMetadata(); invalidateMediaSessionMetadata();
return; return;
} }
updateMediaSessionPlaybackState(); invalidateMediaSessionPlaybackState();
} }
@Override @Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) { public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
updateMediaSessionPlaybackState(); invalidateMediaSessionPlaybackState();
} }
} }
...@@ -869,7 +888,7 @@ public final class MediaSessionConnector { ...@@ -869,7 +888,7 @@ public final class MediaSessionConnector {
Map<String, CustomActionProvider> actionMap = customActionMap; Map<String, CustomActionProvider> actionMap = customActionMap;
if (actionMap.containsKey(action)) { if (actionMap.containsKey(action)) {
actionMap.get(action).onCustomAction(action, extras); actionMap.get(action).onCustomAction(action, extras);
updateMediaSessionPlaybackState(); invalidateMediaSessionPlaybackState();
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment