Commit b8b9a641 by Oliver Woodman

Merge pull request #8412 from h6ah4i:media2-move-playlist-item

PiperOrigin-RevId: 351305387
parents de0e1e51 d522dbaf
......@@ -931,6 +931,38 @@ public class SessionPlayerConnectorTest {
assertThat(onPlaylistChangedLatch.getCount()).isEqualTo(1);
}
@Test
@LargeTest
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.KITKAT)
public void movePlaylistItem_calledOnlyOnce_notifiesPlaylistChangeOnlyOnce() throws Exception {
List<MediaItem> playlist = new ArrayList<>();
playlist.add(TestUtils.createMediaItem(R.raw.video_1));
playlist.add(TestUtils.createMediaItem(R.raw.video_2));
playlist.add(TestUtils.createMediaItem(R.raw.video_3));
assertPlayerResultSuccess(sessionPlayerConnector.setPlaylist(playlist, /* metadata= */ null));
assertPlayerResultSuccess(sessionPlayerConnector.prepare());
CountDownLatch onPlaylistChangedLatch = new CountDownLatch(2);
int moveFromIndex = 0;
int moveToIndex = 2;
playlist.add(moveToIndex, playlist.remove(moveFromIndex));
sessionPlayerConnector.registerPlayerCallback(
executor,
new SessionPlayer.PlayerCallback() {
@Override
public void onPlaylistChanged(
SessionPlayer player,
@Nullable List<MediaItem> list,
@Nullable MediaMetadata metadata) {
assertThat(list).isEqualTo(playlist);
onPlaylistChangedLatch.countDown();
}
});
sessionPlayerConnector.movePlaylistItem(moveFromIndex, moveToIndex);
assertThat(onPlaylistChangedLatch.await(PLAYLIST_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isFalse();
assertThat(onPlaylistChangedLatch.getCount()).isEqualTo(1);
}
// TODO(b/168860979): De-flake and re-enable.
@Ignore
@Test
......
......@@ -97,6 +97,9 @@ import java.util.concurrent.Callable;
/** Command code for {@link SessionPlayer#removePlaylistItem(int)} */
public static final int COMMAND_CODE_PLAYER_REMOVE_PLAYLIST_ITEM = 16;
/** Command code for {@link SessionPlayer#movePlaylistItem(int, int)} */
public static final int COMMAND_CODE_PLAYER_MOVE_PLAYLIST_ITEM = 17;
/** List of session commands whose result would be set after the command is finished. */
@Documented
@Retention(RetentionPolicy.SOURCE)
......@@ -119,6 +122,7 @@ import java.util.concurrent.Callable;
COMMAND_CODE_PLAYER_SET_PLAYLIST,
COMMAND_CODE_PLAYER_ADD_PLAYLIST_ITEM,
COMMAND_CODE_PLAYER_REMOVE_PLAYLIST_ITEM,
COMMAND_CODE_PLAYER_MOVE_PLAYLIST_ITEM,
})
public @interface CommandCode {}
......
......@@ -223,6 +223,19 @@ import java.util.List;
return true;
}
public boolean movePlaylistItem(
@IntRange(from = 0) int fromIndex, @IntRange(from = 0) int toIndex) {
int itemCount = player.getMediaItemCount();
if (!(fromIndex < itemCount && toIndex < itemCount)) {
return false;
}
if (fromIndex == toIndex) {
return true;
}
player.moveMediaItem(fromIndex, toIndex);
return true;
}
public boolean skipToPreviousPlaylistItem() {
Timeline timeline = player.getCurrentTimeline();
Assertions.checkState(!timeline.isEmpty());
......
......@@ -227,6 +227,7 @@ import java.util.concurrent.TimeoutException;
}
build.addAllPredefinedCommands(SessionCommand.COMMAND_VERSION_1);
build.addCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_MOVE_PLAYLIST_ITEM));
// TODO(internal b/142848015): Use removeCommand(int) when it's added.
if (mediaItemProvider == null) {
build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_SET_MEDIA_ITEM));
......
......@@ -325,6 +325,17 @@ public final class SessionPlayerConnector extends SessionPlayer {
}
@Override
public ListenableFuture<PlayerResult> movePlaylistItem(int fromIndex, int toIndex) {
Assertions.checkArgument(fromIndex >= 0);
Assertions.checkArgument(toIndex >= 0);
ListenableFuture<PlayerResult> result =
playerCommandQueue.addCommand(
PlayerCommandQueue.COMMAND_CODE_PLAYER_MOVE_PLAYLIST_ITEM,
/* command= */ () -> player.movePlaylistItem(fromIndex, toIndex));
return result;
}
@Override
public ListenableFuture<PlayerResult> skipToPreviousPlaylistItem() {
ListenableFuture<PlayerResult> result =
playerCommandQueue.addCommand(
......
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