Add SessionPlayer#movePlaylistItem(int, int) support

This commit is contained in:
Haruki Hasegawa 2020-12-30 17:16:06 +09:00
parent 1347d572ef
commit d522dbaf44
No known key found for this signature in database
GPG Key ID: B904442AFD173739
5 changed files with 60 additions and 1 deletions

View File

@ -931,6 +931,38 @@ public class SessionPlayerConnectorTest {
assertThat(onPlaylistChangedLatch.getCount()).isEqualTo(1); 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. // TODO(b/168860979): De-flake and re-enable.
@Ignore @Ignore
@Test @Test

View File

@ -97,6 +97,8 @@ import java.util.concurrent.Callable;
/** Command code for {@link SessionPlayer#removePlaylistItem(int)} */ /** Command code for {@link SessionPlayer#removePlaylistItem(int)} */
public static final int COMMAND_CODE_PLAYER_REMOVE_PLAYLIST_ITEM = 16; 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. */ /** List of session commands whose result would be set after the command is finished. */
@Documented @Documented
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
@ -119,6 +121,7 @@ import java.util.concurrent.Callable;
COMMAND_CODE_PLAYER_SET_PLAYLIST, COMMAND_CODE_PLAYER_SET_PLAYLIST,
COMMAND_CODE_PLAYER_ADD_PLAYLIST_ITEM, COMMAND_CODE_PLAYER_ADD_PLAYLIST_ITEM,
COMMAND_CODE_PLAYER_REMOVE_PLAYLIST_ITEM, COMMAND_CODE_PLAYER_REMOVE_PLAYLIST_ITEM,
COMMAND_CODE_PLAYER_MOVE_PLAYLIST_ITEM,
}) })
public @interface CommandCode {} public @interface CommandCode {}

View File

@ -223,6 +223,18 @@ import java.util.List;
return true; 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() { public boolean skipToPreviousPlaylistItem() {
Timeline timeline = player.getCurrentTimeline(); Timeline timeline = player.getCurrentTimeline();
Assertions.checkState(!timeline.isEmpty()); Assertions.checkState(!timeline.isEmpty());

View File

@ -226,7 +226,7 @@ import java.util.concurrent.TimeoutException;
build = new SessionCommandGroup.Builder(); build = new SessionCommandGroup.Builder();
} }
build.addAllPredefinedCommands(SessionCommand.COMMAND_VERSION_1); build.addAllPredefinedCommands(SessionCommand.COMMAND_VERSION_2);
// TODO(internal b/142848015): Use removeCommand(int) when it's added. // TODO(internal b/142848015): Use removeCommand(int) when it's added.
if (mediaItemProvider == null) { if (mediaItemProvider == null) {
build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_SET_MEDIA_ITEM)); build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_SET_MEDIA_ITEM));
@ -234,6 +234,7 @@ import java.util.concurrent.TimeoutException;
build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_ADD_PLAYLIST_ITEM)); build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_ADD_PLAYLIST_ITEM));
build.removeCommand( build.removeCommand(
new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_REPLACE_PLAYLIST_ITEM)); new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_REPLACE_PLAYLIST_ITEM));
build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_MOVE_PLAYLIST_ITEM));
} }
if (ratingCallback == null) { if (ratingCallback == null) {
build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_SESSION_SET_RATING)); build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_SESSION_SET_RATING));

View File

@ -324,6 +324,17 @@ public final class SessionPlayerConnector extends SessionPlayer {
return result; return result;
} }
@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 @Override
public ListenableFuture<PlayerResult> skipToPreviousPlaylistItem() { public ListenableFuture<PlayerResult> skipToPreviousPlaylistItem() {
ListenableFuture<PlayerResult> result = ListenableFuture<PlayerResult> result =