Update available commands when MediaSessionCompat actions change

This is a bug currently, where commands are created once but never
updated again if the actions in MediaSessionCompat are changed.

PiperOrigin-RevId: 525999084
(cherry picked from commit 79fab6783e)
This commit is contained in:
tonihei 2023-04-21 12:35:39 +01:00 committed by Ian Baker
parent 58cf3a7ba2
commit 0f6a1eb6b8
3 changed files with 39 additions and 7 deletions

View file

@ -8,6 +8,10 @@
* Fix issue where last frame may not be rendered if the last sample with * Fix issue where last frame may not be rendered if the last sample with
frames is dequeued without reading the 'end of stream' sample. frames is dequeued without reading the 'end of stream' sample.
([#11079](https://github.com/google/ExoPlayer/issues/11079)). ([#11079](https://github.com/google/ExoPlayer/issues/11079)).
* Session:
* Fix issue where `MediaController` doesn't update its available commands
when connected to a legacy `MediaSessionCompat` that updates its
actions.
### 1.0.1 (2023-04-18) ### 1.0.1 (2023-04-18)

View file

@ -1885,13 +1885,11 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
? newLegacyPlayerInfo.playbackInfoCompat.getVolumeControl() ? newLegacyPlayerInfo.playbackInfoCompat.getVolumeControl()
: VolumeProviderCompat.VOLUME_CONTROL_FIXED; : VolumeProviderCompat.VOLUME_CONTROL_FIXED;
availablePlayerCommands = availablePlayerCommands =
(oldControllerInfo.availablePlayerCommands == Commands.EMPTY) MediaUtils.convertToPlayerCommands(
? MediaUtils.convertToPlayerCommands( newLegacyPlayerInfo.playbackStateCompat,
newLegacyPlayerInfo.playbackStateCompat, volumeControlType,
volumeControlType, sessionFlags,
sessionFlags, isSessionReady);
isSessionReady)
: oldControllerInfo.availablePlayerCommands;
PlaybackException playerError = PlaybackException playerError =
MediaUtils.convertToPlaybackException(newLegacyPlayerInfo.playbackStateCompat); MediaUtils.convertToPlaybackException(newLegacyPlayerInfo.playbackStateCompat);

View file

@ -1502,6 +1502,36 @@ public class MediaControllerWithMediaSessionCompatTest {
assertThat(errorFromGetterRef.get().getMessage()).isEqualTo(testConvertedErrorMessage); assertThat(errorFromGetterRef.get().getMessage()).isEqualTo(testConvertedErrorMessage);
} }
@Test
public void setPlaybackState_withActions_updatesAndNotifiesAvailableCommands() throws Exception {
MediaController controller = controllerTestRule.createController(session.getSessionToken());
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<Player.Commands> commandsFromParamRef = new AtomicReference<>();
AtomicReference<Player.Commands> commandsFromGetterRef = new AtomicReference<>();
Player.Listener listener =
new Player.Listener() {
@Override
public void onAvailableCommandsChanged(Player.Commands commands) {
commandsFromParamRef.set(commands);
commandsFromGetterRef.set(controller.getAvailableCommands());
latch.countDown();
}
};
controller.addListener(listener);
session.setPlaybackState(
new PlaybackStateCompat.Builder()
.setActions(
PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_FAST_FORWARD)
.build());
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(commandsFromParamRef.get().contains(Player.COMMAND_PLAY_PAUSE)).isTrue();
assertThat(commandsFromParamRef.get().contains(Player.COMMAND_SEEK_FORWARD)).isTrue();
assertThat(commandsFromGetterRef.get().contains(Player.COMMAND_PLAY_PAUSE)).isTrue();
assertThat(commandsFromGetterRef.get().contains(Player.COMMAND_SEEK_FORWARD)).isTrue();
}
@Test @Test
public void setPlaybackToRemote_notifiesDeviceInfoAndVolume() throws Exception { public void setPlaybackToRemote_notifiesDeviceInfoAndVolume() throws Exception {
int volumeControlType = VolumeProviderCompat.VOLUME_CONTROL_ABSOLUTE; int volumeControlType = VolumeProviderCompat.VOLUME_CONTROL_ABSOLUTE;