Tweak UI behavior when commands are missing.

For most missing commands, we already disable the corresponding
controls. This change extends this to more UI elements that are
disabled in case the corresponding action is unavailable.

#minor-release

PiperOrigin-RevId: 505057751
This commit is contained in:
tonihei 2023-01-27 09:50:24 +00:00 committed by christosts
parent e5e17d6f2c
commit 641c3b1b22

View File

@ -1016,7 +1016,10 @@ public class StyledPlayerControlView extends FrameLayout {
boolean enableFastForward = false; boolean enableFastForward = false;
boolean enableNext = false; boolean enableNext = false;
if (player != null) { if (player != null) {
enableSeeking = player.isCommandAvailable(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM); enableSeeking =
(showMultiWindowTimeBar && canShowMultiWindowTimeBar(player, window))
? player.isCommandAvailable(COMMAND_SEEK_TO_MEDIA_ITEM)
: player.isCommandAvailable(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM);
enablePrevious = player.isCommandAvailable(COMMAND_SEEK_TO_PREVIOUS); enablePrevious = player.isCommandAvailable(COMMAND_SEEK_TO_PREVIOUS);
enableRewind = player.isCommandAvailable(COMMAND_SEEK_BACK); enableRewind = player.isCommandAvailable(COMMAND_SEEK_BACK);
enableFastForward = player.isCommandAvailable(COMMAND_SEEK_FORWARD); enableFastForward = player.isCommandAvailable(COMMAND_SEEK_FORWARD);
@ -1132,6 +1135,7 @@ public class StyledPlayerControlView extends FrameLayout {
private void updateTrackLists() { private void updateTrackLists() {
initTrackSelectionAdapter(); initTrackSelectionAdapter();
updateButton(textTrackSelectionAdapter.getItemCount() > 0, subtitleButton); updateButton(textTrackSelectionAdapter.getItemCount() > 0, subtitleButton);
updateSettingsButton();
} }
private void initTrackSelectionAdapter() { private void initTrackSelectionAdapter() {
@ -1307,6 +1311,11 @@ public class StyledPlayerControlView extends FrameLayout {
playbackSpeedAdapter.updateSelectedIndex(player.getPlaybackParameters().speed); playbackSpeedAdapter.updateSelectedIndex(player.getPlaybackParameters().speed);
settingsAdapter.setSubTextAtPosition( settingsAdapter.setSubTextAtPosition(
SETTINGS_PLAYBACK_SPEED_POSITION, playbackSpeedAdapter.getSelectedText()); SETTINGS_PLAYBACK_SPEED_POSITION, playbackSpeedAdapter.getSelectedText());
updateSettingsButton();
}
private void updateSettingsButton() {
updateButton(settingsAdapter.hasSettingsToShow(), settingsButton);
} }
private void updateSettingsWindowSize() { private void updateSettingsWindowSize() {
@ -1360,25 +1369,26 @@ public class StyledPlayerControlView extends FrameLayout {
} }
private void seekToTimeBarPosition(Player player, long positionMs) { private void seekToTimeBarPosition(Player player, long positionMs) {
if (multiWindowTimeBar if (multiWindowTimeBar) {
&& player.isCommandAvailable(COMMAND_GET_TIMELINE) if (player.isCommandAvailable(COMMAND_GET_TIMELINE)
&& player.isCommandAvailable(COMMAND_SEEK_TO_MEDIA_ITEM)) { && player.isCommandAvailable(COMMAND_SEEK_TO_MEDIA_ITEM)) {
Timeline timeline = player.getCurrentTimeline(); Timeline timeline = player.getCurrentTimeline();
int windowCount = timeline.getWindowCount(); int windowCount = timeline.getWindowCount();
int windowIndex = 0; int windowIndex = 0;
while (true) { while (true) {
long windowDurationMs = timeline.getWindow(windowIndex, window).getDurationMs(); long windowDurationMs = timeline.getWindow(windowIndex, window).getDurationMs();
if (positionMs < windowDurationMs) { if (positionMs < windowDurationMs) {
break; break;
} else if (windowIndex == windowCount - 1) { } else if (windowIndex == windowCount - 1) {
// Seeking past the end of the last window should seek to the end of the timeline. // Seeking past the end of the last window should seek to the end of the timeline.
positionMs = windowDurationMs; positionMs = windowDurationMs;
break; break;
}
positionMs -= windowDurationMs;
windowIndex++;
} }
positionMs -= windowDurationMs; player.seekTo(windowIndex, positionMs);
windowIndex++;
} }
player.seekTo(windowIndex, positionMs);
} else if (player.isCommandAvailable(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM)) { } else if (player.isCommandAvailable(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM)) {
player.seekTo(positionMs); player.seekTo(positionMs);
} }
@ -1590,15 +1600,14 @@ public class StyledPlayerControlView extends FrameLayout {
* @return Whether the specified timeline can be shown on a multi-window time bar. * @return Whether the specified timeline can be shown on a multi-window time bar.
*/ */
private static boolean canShowMultiWindowTimeBar(Player player, Timeline.Window window) { private static boolean canShowMultiWindowTimeBar(Player player, Timeline.Window window) {
if (!player.isCommandAvailable(COMMAND_GET_TIMELINE) if (!player.isCommandAvailable(COMMAND_GET_TIMELINE)) {
|| !player.isCommandAvailable(COMMAND_SEEK_TO_MEDIA_ITEM)) {
return false; return false;
} }
Timeline timeline = player.getCurrentTimeline(); Timeline timeline = player.getCurrentTimeline();
if (timeline.getWindowCount() > MAX_WINDOWS_FOR_MULTI_WINDOW_TIME_BAR) { int windowCount = timeline.getWindowCount();
if (windowCount <= 1 || windowCount > MAX_WINDOWS_FOR_MULTI_WINDOW_TIME_BAR) {
return false; return false;
} }
int windowCount = timeline.getWindowCount();
for (int i = 0; i < windowCount; i++) { for (int i = 0; i < windowCount; i++) {
if (timeline.getWindow(i, window).durationUs == C.TIME_UNSET) { if (timeline.getWindow(i, window).durationUs == C.TIME_UNSET) {
return false; return false;
@ -1641,17 +1650,24 @@ public class StyledPlayerControlView extends FrameLayout {
@Override @Override
public void onEvents(Player player, Events events) { public void onEvents(Player player, Events events) {
if (events.containsAny(EVENT_PLAYBACK_STATE_CHANGED, EVENT_PLAY_WHEN_READY_CHANGED)) { if (events.containsAny(
EVENT_PLAYBACK_STATE_CHANGED,
EVENT_PLAY_WHEN_READY_CHANGED,
EVENT_AVAILABLE_COMMANDS_CHANGED)) {
updatePlayPauseButton(); updatePlayPauseButton();
} }
if (events.containsAny( if (events.containsAny(
EVENT_PLAYBACK_STATE_CHANGED, EVENT_PLAY_WHEN_READY_CHANGED, EVENT_IS_PLAYING_CHANGED)) { EVENT_PLAYBACK_STATE_CHANGED,
EVENT_PLAY_WHEN_READY_CHANGED,
EVENT_IS_PLAYING_CHANGED,
EVENT_AVAILABLE_COMMANDS_CHANGED)) {
updateProgress(); updateProgress();
} }
if (events.contains(EVENT_REPEAT_MODE_CHANGED)) { if (events.containsAny(EVENT_REPEAT_MODE_CHANGED, EVENT_AVAILABLE_COMMANDS_CHANGED)) {
updateRepeatModeButton(); updateRepeatModeButton();
} }
if (events.contains(EVENT_SHUFFLE_MODE_ENABLED_CHANGED)) { if (events.containsAny(
EVENT_SHUFFLE_MODE_ENABLED_CHANGED, EVENT_AVAILABLE_COMMANDS_CHANGED)) {
updateShuffleButton(); updateShuffleButton();
} }
if (events.containsAny( if (events.containsAny(
@ -1664,13 +1680,14 @@ public class StyledPlayerControlView extends FrameLayout {
EVENT_AVAILABLE_COMMANDS_CHANGED)) { EVENT_AVAILABLE_COMMANDS_CHANGED)) {
updateNavigation(); updateNavigation();
} }
if (events.containsAny(EVENT_POSITION_DISCONTINUITY, EVENT_TIMELINE_CHANGED)) { if (events.containsAny(
EVENT_POSITION_DISCONTINUITY, EVENT_TIMELINE_CHANGED, EVENT_AVAILABLE_COMMANDS_CHANGED)) {
updateTimeline(); updateTimeline();
} }
if (events.contains(EVENT_PLAYBACK_PARAMETERS_CHANGED)) { if (events.containsAny(EVENT_PLAYBACK_PARAMETERS_CHANGED, EVENT_AVAILABLE_COMMANDS_CHANGED)) {
updatePlaybackSpeedList(); updatePlaybackSpeedList();
} }
if (events.contains(EVENT_TRACKS_CHANGED)) { if (events.containsAny(EVENT_TRACKS_CHANGED, EVENT_AVAILABLE_COMMANDS_CHANGED)) {
updateTrackLists(); updateTrackLists();
} }
} }
@ -1780,6 +1797,14 @@ public class StyledPlayerControlView extends FrameLayout {
@Override @Override
public void onBindViewHolder(SettingViewHolder holder, int position) { public void onBindViewHolder(SettingViewHolder holder, int position) {
if (shouldShowSetting(position)) {
holder.itemView.setLayoutParams(
new RecyclerView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
} else {
holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));
}
holder.mainTextView.setText(mainTexts[position]); holder.mainTextView.setText(mainTexts[position]);
if (subTexts[position] == null) { if (subTexts[position] == null) {
@ -1808,6 +1833,26 @@ public class StyledPlayerControlView extends FrameLayout {
public void setSubTextAtPosition(int position, String subText) { public void setSubTextAtPosition(int position, String subText) {
this.subTexts[position] = subText; this.subTexts[position] = subText;
} }
public boolean hasSettingsToShow() {
return shouldShowSetting(SETTINGS_AUDIO_TRACK_SELECTION_POSITION)
|| shouldShowSetting(SETTINGS_PLAYBACK_SPEED_POSITION);
}
private boolean shouldShowSetting(int position) {
if (player == null) {
return false;
}
switch (position) {
case SETTINGS_AUDIO_TRACK_SELECTION_POSITION:
return player.isCommandAvailable(COMMAND_GET_TRACKS)
&& player.isCommandAvailable(COMMAND_SET_TRACK_SELECTION_PARAMETERS);
case SETTINGS_PLAYBACK_SPEED_POSITION:
return player.isCommandAvailable(COMMAND_SET_SPEED_AND_PITCH);
default:
return true;
}
}
} }
private final class SettingViewHolder extends RecyclerView.ViewHolder { private final class SettingViewHolder extends RecyclerView.ViewHolder {