Commit 74a9bf09 by olly Committed by marcbaechinger

Simplify handling of playback speed in StyledPlayerControlView

PiperOrigin-RevId: 360404403
parent 14711d1f
......@@ -436,14 +436,10 @@ public class StyledPlayerControlView extends FrameLayout {
private StyledPlayerControlViewLayoutManager controlViewLayoutManager;
private Resources resources;
private int selectedMainSettingsPosition;
private RecyclerView settingsView;
private SettingsAdapter settingsAdapter;
private SubSettingsAdapter subSettingsAdapter;
private PlaybackSpeedAdapter playbackSpeedAdapter;
private PopupWindow settingsWindow;
private String[] playbackSpeedTexts;
private int[] playbackSpeedsMultBy100;
private int selectedPlaybackSpeedIndex;
private boolean needToHideBars;
private int settingsWindowMargin;
......@@ -675,12 +671,7 @@ public class StyledPlayerControlView extends FrameLayout {
settingIcons[SETTINGS_AUDIO_TRACK_SELECTION_POSITION] =
resources.getDrawable(R.drawable.exo_styled_controls_audiotrack);
settingsAdapter = new SettingsAdapter(settingTexts, settingIcons);
playbackSpeedTexts = resources.getStringArray(R.array.exo_playback_speeds);
playbackSpeedsMultBy100 = resources.getIntArray(R.array.exo_speed_multiplied_by_100);
settingsWindowMargin = resources.getDimensionPixelSize(R.dimen.exo_settings_offset);
subSettingsAdapter = new SubSettingsAdapter();
settingsView =
(RecyclerView)
LayoutInflater.from(context).inflate(R.layout.exo_styled_settings_list, null);
......@@ -705,6 +696,10 @@ public class StyledPlayerControlView extends FrameLayout {
resources.getString(R.string.exo_controls_cc_disabled_description);
textTrackSelectionAdapter = new TextTrackSelectionAdapter();
audioTrackSelectionAdapter = new AudioTrackSelectionAdapter();
playbackSpeedAdapter =
new PlaybackSpeedAdapter(
resources.getStringArray(R.array.exo_playback_speeds),
resources.getIntArray(R.array.exo_speed_multiplied_by_100));
fullScreenExitDrawable = resources.getDrawable(R.drawable.exo_styled_controls_fullscreen_exit);
fullScreenEnterDrawable =
......@@ -782,7 +777,6 @@ public class StyledPlayerControlView extends FrameLayout {
this.trackSelector = null;
}
updateAll();
updateSettingsPlaybackSpeedLists();
}
/**
......@@ -1114,6 +1108,7 @@ public class StyledPlayerControlView extends FrameLayout {
updateRepeatModeButton();
updateShuffleButton();
updateTrackLists();
updatePlaybackSpeedList();
updateTimeline();
}
......@@ -1449,24 +1444,13 @@ public class StyledPlayerControlView extends FrameLayout {
}
}
private void updateSettingsPlaybackSpeedLists() {
private void updatePlaybackSpeedList() {
if (player == null) {
return;
}
float speed = player.getPlaybackParameters().speed;
int currentSpeedMultBy100 = Math.round(speed * 100);
int closestMatchIndex = 0;
int closestMatchDifference = Integer.MAX_VALUE;
for (int i = 0; i < playbackSpeedsMultBy100.length; i++) {
int difference = Math.abs(currentSpeedMultBy100 - playbackSpeedsMultBy100[i]);
if (difference < closestMatchDifference) {
closestMatchIndex = i;
closestMatchDifference = difference;
}
}
selectedPlaybackSpeedIndex = closestMatchIndex;
playbackSpeedAdapter.updateSelectedIndex(player.getPlaybackParameters().speed);
settingsAdapter.setSubTextAtPosition(
SETTINGS_PLAYBACK_SPEED_POSITION, playbackSpeedTexts[closestMatchIndex]);
SETTINGS_PLAYBACK_SPEED_POSITION, playbackSpeedAdapter.getSelectedText());
}
private void updateSettingsWindowSize() {
......@@ -1582,27 +1566,14 @@ public class StyledPlayerControlView extends FrameLayout {
private void onSettingViewClicked(int position) {
if (position == SETTINGS_PLAYBACK_SPEED_POSITION) {
subSettingsAdapter.init(playbackSpeedTexts, selectedPlaybackSpeedIndex);
selectedMainSettingsPosition = SETTINGS_PLAYBACK_SPEED_POSITION;
displaySettingsWindow(subSettingsAdapter);
displaySettingsWindow(playbackSpeedAdapter);
} else if (position == SETTINGS_AUDIO_TRACK_SELECTION_POSITION) {
selectedMainSettingsPosition = SETTINGS_AUDIO_TRACK_SELECTION_POSITION;
displaySettingsWindow(audioTrackSelectionAdapter);
} else {
settingsWindow.dismiss();
}
}
private void onSubSettingViewClicked(int position) {
if (selectedMainSettingsPosition == SETTINGS_PLAYBACK_SPEED_POSITION) {
if (position != selectedPlaybackSpeedIndex) {
float speed = playbackSpeedsMultBy100[position] / 100.0f;
setPlaybackSpeed(speed);
}
}
settingsWindow.dismiss();
}
private void onLayoutChange(
View v,
int left,
......@@ -1847,7 +1818,7 @@ public class StyledPlayerControlView extends FrameLayout {
updateTimeline();
}
if (events.contains(EVENT_PLAYBACK_PARAMETERS_CHANGED)) {
updateSettingsPlaybackSpeedLists();
updatePlaybackSpeedList();
}
if (events.contains(EVENT_TRACKS_CHANGED)) {
updateTrackLists();
......@@ -1967,18 +1938,33 @@ public class StyledPlayerControlView extends FrameLayout {
}
}
private class SubSettingsAdapter extends RecyclerView.Adapter<SubSettingViewHolder> {
private final class PlaybackSpeedAdapter extends RecyclerView.Adapter<SubSettingViewHolder> {
private String[] texts;
private final String[] playbackSpeedTexts;
private final int[] playbackSpeedsMultBy100;
private int selectedIndex;
public SubSettingsAdapter() {
texts = new String[0];
public PlaybackSpeedAdapter(String[] playbackSpeedTexts, int[] playbackSpeedsMultBy100) {
this.playbackSpeedTexts = playbackSpeedTexts;
this.playbackSpeedsMultBy100 = playbackSpeedsMultBy100;
}
public void updateSelectedIndex(float playbackSpeed) {
int currentSpeedMultBy100 = Math.round(playbackSpeed * 100);
int closestMatchIndex = 0;
int closestMatchDifference = Integer.MAX_VALUE;
for (int i = 0; i < playbackSpeedsMultBy100.length; i++) {
int difference = Math.abs(currentSpeedMultBy100 - playbackSpeedsMultBy100[i]);
if (difference < closestMatchDifference) {
closestMatchIndex = i;
closestMatchDifference = difference;
}
}
selectedIndex = closestMatchIndex;
}
public void init(String[] texts, int selectedIndex) {
this.texts = texts;
this.selectedIndex = selectedIndex;
public String getSelectedText() {
return playbackSpeedTexts[selectedIndex];
}
@Override
......@@ -1991,27 +1977,23 @@ public class StyledPlayerControlView extends FrameLayout {
@Override
public void onBindViewHolder(SubSettingViewHolder holder, int position) {
if (position < texts.length) {
holder.textView.setText(texts[position]);
if (position < playbackSpeedTexts.length) {
holder.textView.setText(playbackSpeedTexts[position]);
}
holder.checkView.setVisibility(position == selectedIndex ? VISIBLE : INVISIBLE);
holder.itemView.setOnClickListener(
v -> {
if (position != selectedIndex) {
float speed = playbackSpeedsMultBy100[position] / 100.0f;
setPlaybackSpeed(speed);
}
settingsWindow.dismiss();
});
}
@Override
public int getItemCount() {
return texts.length;
}
}
private final class SubSettingViewHolder extends RecyclerView.ViewHolder {
private final TextView textView;
private final View checkView;
public SubSettingViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.exo_text);
checkView = itemView.findViewById(R.id.exo_check);
itemView.setOnClickListener(v -> onSubSettingViewClicked(getAdapterPosition()));
return playbackSpeedTexts.length;
}
}
......@@ -2059,7 +2041,7 @@ public class StyledPlayerControlView extends FrameLayout {
}
@Override
public void onBindViewHolderAtZeroPosition(TrackSelectionViewHolder holder) {
public void onBindViewHolderAtZeroPosition(SubSettingViewHolder holder) {
// CC options include "Off" at the first position, which disables text rendering.
holder.textView.setText(R.string.exo_track_selection_none);
boolean isTrackSelectionOff = true;
......@@ -2088,7 +2070,7 @@ public class StyledPlayerControlView extends FrameLayout {
}
@Override
public void onBindViewHolder(TrackSelectionViewHolder holder, int position) {
public void onBindViewHolder(SubSettingViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
if (position > 0) {
TrackInfo track = tracks.get(position - 1);
......@@ -2105,7 +2087,7 @@ public class StyledPlayerControlView extends FrameLayout {
private final class AudioTrackSelectionAdapter extends TrackSelectionAdapter {
@Override
public void onBindViewHolderAtZeroPosition(TrackSelectionViewHolder holder) {
public void onBindViewHolderAtZeroPosition(SubSettingViewHolder holder) {
// Audio track selection option includes "Auto" at the top.
holder.textView.setText(R.string.exo_track_selection_auto);
// hasSelectionOverride is true means there is an explicit track selection, not "Auto".
......@@ -2184,8 +2166,7 @@ public class StyledPlayerControlView extends FrameLayout {
}
}
private abstract class TrackSelectionAdapter
extends RecyclerView.Adapter<TrackSelectionViewHolder> {
private abstract class TrackSelectionAdapter extends RecyclerView.Adapter<SubSettingViewHolder> {
protected List<Integer> rendererIndices;
protected List<TrackInfo> tracks;
......@@ -2201,19 +2182,19 @@ public class StyledPlayerControlView extends FrameLayout {
List<Integer> rendererIndices, List<TrackInfo> trackInfos, MappedTrackInfo mappedTrackInfo);
@Override
public TrackSelectionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
public SubSettingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v =
LayoutInflater.from(getContext())
.inflate(R.layout.exo_styled_sub_settings_list_item, null);
return new TrackSelectionViewHolder(v);
return new SubSettingViewHolder(v);
}
public abstract void onBindViewHolderAtZeroPosition(TrackSelectionViewHolder holder);
public abstract void onBindViewHolderAtZeroPosition(SubSettingViewHolder holder);
public abstract void onTrackSelection(String subtext);
@Override
public void onBindViewHolder(TrackSelectionViewHolder holder, int position) {
public void onBindViewHolder(SubSettingViewHolder holder, int position) {
if (trackSelector == null || mappedTrackInfo == null) {
return;
}
......@@ -2269,12 +2250,12 @@ public class StyledPlayerControlView extends FrameLayout {
}
}
private static class TrackSelectionViewHolder extends RecyclerView.ViewHolder {
private static class SubSettingViewHolder extends RecyclerView.ViewHolder {
public final TextView textView;
public final View checkView;
public TrackSelectionViewHolder(View itemView) {
public SubSettingViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.exo_text);
checkView = itemView.findViewById(R.id.exo_check);
......
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