Commit 93605303 by tonihei Committed by Rohit Singh

Update available commands when setting a new player in MediaSession

PiperOrigin-RevId: 523633865
(cherry picked from commit ae875648)
parent abc9d8ce
......@@ -15,7 +15,6 @@
*/
package androidx.media3.session;
import static androidx.media3.common.Player.COMMAND_GET_TRACKS;
import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.common.util.Assertions.checkStateNotNull;
import static androidx.media3.common.util.Util.castNonNull;
......@@ -278,8 +277,7 @@ import org.checkerframework.checker.initialization.qual.Initialized;
}
playerInfo = newPlayerWrapper.createPlayerInfoForBundling();
onPlayerInfoChangedHandler.sendPlayerInfoChangedMessage(
/* excludeTimeline= */ false, /* excludeTracks= */ false);
handleAvailablePlayerCommandsChanged(newPlayerWrapper.getAvailableCommands());
}
public void release() {
......@@ -772,6 +770,20 @@ import org.checkerframework.checker.initialization.qual.Initialized;
}
}
private void handleAvailablePlayerCommandsChanged(Player.Commands availableCommands) {
// Update PlayerInfo and do not force exclude elements in case they need to be updated because
// an available command has been removed.
onPlayerInfoChangedHandler.sendPlayerInfoChangedMessage(
/* excludeTimeline= */ false, /* excludeTracks= */ false);
dispatchRemoteControllerTaskWithoutReturn(
(callback, seq) -> callback.onAvailableCommandsChangedFromPlayer(seq, availableCommands));
// Forcefully update playback info to update VolumeProviderCompat in case
// COMMAND_ADJUST_DEVICE_VOLUME or COMMAND_SET_DEVICE_VOLUME value has changed.
dispatchRemoteControllerTaskToLegacyStub(
(callback, seq) -> callback.onDeviceInfoChanged(seq, playerInfo.deviceInfo));
}
/* @FunctionalInterface */
interface RemoteControllerTask {
......@@ -1182,16 +1194,7 @@ import org.checkerframework.checker.initialization.qual.Initialized;
if (player == null) {
return;
}
boolean excludeTracks = !availableCommands.contains(COMMAND_GET_TRACKS);
session.onPlayerInfoChangedHandler.sendPlayerInfoChangedMessage(
/* excludeTimeline= */ false, excludeTracks);
session.dispatchRemoteControllerTaskWithoutReturn(
(callback, seq) -> callback.onAvailableCommandsChangedFromPlayer(seq, availableCommands));
// Forcefully update playback info to update VolumeProviderCompat in case
// COMMAND_ADJUST_DEVICE_VOLUME or COMMAND_SET_DEVICE_VOLUME value has changed.
session.dispatchRemoteControllerTaskToLegacyStub(
(callback, seq) -> callback.onDeviceInfoChanged(seq, session.playerInfo.deviceInfo));
session.handleAvailablePlayerCommandsChanged(availableCommands);
}
@Override
......
......@@ -103,6 +103,7 @@ public class CommonConstants {
public static final String KEY_MAX_SEEK_TO_PREVIOUS_POSITION_MS = "maxSeekToPreviousPositionMs";
public static final String KEY_TRACK_SELECTION_PARAMETERS = "trackSelectionParameters";
public static final String KEY_CURRENT_TRACKS = "currentTracks";
public static final String KEY_AVAILABLE_COMMANDS = "availableCommands";
// SessionCompat arguments
public static final String KEY_SESSION_COMPAT_TOKEN = "sessionCompatToken";
......
......@@ -326,6 +326,8 @@ public class MediaControllerListenerTest {
@Player.RepeatMode int testRepeatMode = Player.REPEAT_MODE_ALL;
int testCurrentAdGroupIndex = 33;
int testCurrentAdIndexInAdGroup = 11;
Commands testCommands =
new Commands.Builder().addAllCommands().remove(Player.COMMAND_STOP).build();
AtomicInteger stateRef = new AtomicInteger();
AtomicReference<Timeline> timelineRef = new AtomicReference<>();
AtomicReference<MediaMetadata> playlistMetadataRef = new AtomicReference<>();
......@@ -335,7 +337,8 @@ public class MediaControllerListenerTest {
AtomicInteger currentAdIndexInAdGroupRef = new AtomicInteger();
AtomicBoolean shuffleModeEnabledRef = new AtomicBoolean();
AtomicInteger repeatModeRef = new AtomicInteger();
CountDownLatch latch = new CountDownLatch(7);
AtomicReference<Commands> commandsRef = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(8);
MediaController controller = controllerTestRule.createController(remoteSession.getToken());
threadTestRule
.getHandler()
......@@ -344,6 +347,12 @@ public class MediaControllerListenerTest {
controller.addListener(
new Player.Listener() {
@Override
public void onAvailableCommandsChanged(Commands availableCommands) {
commandsRef.set(availableCommands);
latch.countDown();
}
@Override
public void onAudioAttributesChanged(AudioAttributes attributes) {
audioAttributesRef.set(attributes);
latch.countDown();
......@@ -402,6 +411,7 @@ public class MediaControllerListenerTest {
.setIsPlayingAd(true)
.setCurrentAdGroupIndex(testCurrentAdGroupIndex)
.setCurrentAdIndexInAdGroup(testCurrentAdIndexInAdGroup)
.setAvailableCommands(testCommands)
.build();
remoteSession.setPlayer(playerConfig);
......@@ -415,6 +425,7 @@ public class MediaControllerListenerTest {
assertThat(currentAdIndexInAdGroupRef.get()).isEqualTo(testCurrentAdIndexInAdGroup);
assertThat(shuffleModeEnabledRef.get()).isEqualTo(testShuffleModeEnabled);
assertThat(repeatModeRef.get()).isEqualTo(testRepeatMode);
assertThat(commandsRef.get()).isEqualTo(testCommands);
}
@Test
......
......@@ -18,6 +18,7 @@ package androidx.media3.session;
import static androidx.media3.common.Player.COMMAND_GET_TRACKS;
import static androidx.media3.test.session.common.CommonConstants.ACTION_MEDIA3_SESSION;
import static androidx.media3.test.session.common.CommonConstants.KEY_AUDIO_ATTRIBUTES;
import static androidx.media3.test.session.common.CommonConstants.KEY_AVAILABLE_COMMANDS;
import static androidx.media3.test.session.common.CommonConstants.KEY_BUFFERED_PERCENTAGE;
import static androidx.media3.test.session.common.CommonConstants.KEY_BUFFERED_POSITION;
import static androidx.media3.test.session.common.CommonConstants.KEY_CONTENT_BUFFERED_POSITION;
......@@ -397,6 +398,10 @@ public class MediaSessionProviderService extends Service {
player.trackSelectionParameters =
TrackSelectionParameters.fromBundle(trackSelectionParametersBundle);
}
@Nullable Bundle availableCommandsBundle = config.getBundle(KEY_AVAILABLE_COMMANDS);
if (availableCommandsBundle != null) {
player.commands = Player.Commands.CREATOR.fromBundle(availableCommandsBundle);
}
return player;
}
......
......@@ -17,6 +17,7 @@ package androidx.media3.session;
import static androidx.media3.test.session.common.CommonConstants.ACTION_MEDIA3_SESSION;
import static androidx.media3.test.session.common.CommonConstants.KEY_AUDIO_ATTRIBUTES;
import static androidx.media3.test.session.common.CommonConstants.KEY_AVAILABLE_COMMANDS;
import static androidx.media3.test.session.common.CommonConstants.KEY_BUFFERED_PERCENTAGE;
import static androidx.media3.test.session.common.CommonConstants.KEY_BUFFERED_POSITION;
import static androidx.media3.test.session.common.CommonConstants.KEY_CONTENT_BUFFERED_POSITION;
......@@ -742,6 +743,12 @@ public class RemoteMediaSession {
return this;
}
@CanIgnoreReturnValue
public MockPlayerConfigBuilder setAvailableCommands(Player.Commands availableCommands) {
bundle.putBundle(KEY_AVAILABLE_COMMANDS, availableCommands.toBundle());
return this;
}
public Bundle build() {
return bundle;
}
......
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