StyledPlayerControlView: Some cleanup

PiperOrigin-RevId: 322317638
This commit is contained in:
olly 2020-07-21 10:03:54 +01:00 committed by Oliver Woodman
parent 63ca2b00fb
commit 899a78fca9
3 changed files with 94 additions and 113 deletions

View file

@ -640,23 +640,29 @@ public class StyledPlayerControlView extends FrameLayout {
}
// Related to Settings List View
List<String> settingsMainTextsList =
Arrays.asList(resources.getStringArray(R.array.exo_settings_main_texts));
TypedArray settingsIconTypedArray = resources.obtainTypedArray(R.array.exo_settings_icon_ids);
String[] settingTexts = new String[2];
Drawable[] settingIcons = new Drawable[2];
settingTexts[SETTINGS_PLAYBACK_SPEED_POSITION] =
resources.getString(R.string.exo_controls_playback_speed);
settingIcons[SETTINGS_PLAYBACK_SPEED_POSITION] =
resources.getDrawable(R.drawable.exo_styled_controls_speed);
settingTexts[SETTINGS_AUDIO_TRACK_SELECTION_POSITION] =
resources.getString(R.string.exo_track_selection_title_audio);
settingIcons[SETTINGS_AUDIO_TRACK_SELECTION_POSITION] =
resources.getDrawable(R.drawable.exo_styled_controls_audiotrack);
settingsAdapter = new SettingsAdapter(settingTexts, settingIcons);
playbackSpeedTextList =
new ArrayList<>(Arrays.asList(resources.getStringArray(R.array.exo_playback_speeds)));
String normalSpeed = resources.getString(R.string.exo_controls_playback_speed_normal);
selectedPlaybackSpeedIndex = playbackSpeedTextList.indexOf(normalSpeed);
playbackSpeedMultBy100List = new ArrayList<>();
int[] speeds = resources.getIntArray(R.array.exo_speed_multiplied_by_100);
for (int speed : speeds) {
playbackSpeedMultBy100List.add(speed);
}
selectedPlaybackSpeedIndex = playbackSpeedMultBy100List.indexOf(100);
customPlaybackSpeedIndex = UNDEFINED_POSITION;
settingsWindowMargin = resources.getDimensionPixelSize(R.dimen.exo_settings_offset);
settingsAdapter = new SettingsAdapter(settingsMainTextsList, settingsIconTypedArray);
subSettingsAdapter = new SubSettingsAdapter();
subSettingsAdapter.setCheckPosition(UNDEFINED_POSITION);
settingsView =
@ -1431,7 +1437,7 @@ public class StyledPlayerControlView extends FrameLayout {
}
selectedPlaybackSpeedIndex = indexForCurrentSpeed;
settingsAdapter.updateSubTexts(
settingsAdapter.setSubTextAtPosition(
SETTINGS_PLAYBACK_SPEED_POSITION, playbackSpeedTextList.get(indexForCurrentSpeed));
}
@ -1538,6 +1544,30 @@ public class StyledPlayerControlView extends FrameLayout {
}
}
private void onSettingViewClicked(int position) {
if (position == SETTINGS_PLAYBACK_SPEED_POSITION) {
subSettingsAdapter.setTexts(playbackSpeedTextList);
subSettingsAdapter.setCheckPosition(selectedPlaybackSpeedIndex);
selectedMainSettingsPosition = SETTINGS_PLAYBACK_SPEED_POSITION;
displaySettingsWindow(subSettingsAdapter);
} 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 = playbackSpeedMultBy100List.get(position) / 100.0f;
setPlaybackSpeed(speed);
}
}
settingsWindow.dismiss();
}
private void onLayoutChange(
View v,
int left,
@ -1796,38 +1826,38 @@ public class StyledPlayerControlView extends FrameLayout {
}
}
private class SettingsAdapter extends RecyclerView.Adapter<SettingsAdapter.SettingsViewHolder> {
private List<String> mainTexts;
@Nullable private List<String> subTexts;
@Nullable private TypedArray iconIds;
private class SettingsAdapter extends RecyclerView.Adapter<SettingViewHolder> {
private final String[] mainTexts;
private final String[] subTexts;
private final Drawable[] iconIds;
public SettingsAdapter(List<String> mainTexts, @Nullable TypedArray iconIds) {
public SettingsAdapter(String[] mainTexts, Drawable[] iconIds) {
this.mainTexts = mainTexts;
this.subTexts = Arrays.asList(new String[mainTexts.size()]);
this.subTexts = new String[mainTexts.length];
this.iconIds = iconIds;
}
@Override
public SettingsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
public SettingViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v =
LayoutInflater.from(getContext()).inflate(R.layout.exo_styled_settings_list_item, null);
return new SettingsViewHolder(v);
return new SettingViewHolder(v);
}
@Override
public void onBindViewHolder(SettingsViewHolder holder, int position) {
holder.mainTextView.setText(mainTexts.get(position));
public void onBindViewHolder(SettingViewHolder holder, int position) {
holder.mainTextView.setText(mainTexts[position]);
if (subTexts == null || subTexts.get(position) == null) {
if (subTexts[position] == null) {
holder.subTextView.setVisibility(GONE);
} else {
holder.subTextView.setText(subTexts.get(position));
holder.subTextView.setText(subTexts[position]);
}
if (iconIds == null || iconIds.getDrawable(position) == null) {
if (iconIds[position] == null) {
holder.iconView.setVisibility(GONE);
} else {
holder.iconView.setImageDrawable(iconIds.getDrawable(position));
holder.iconView.setImageDrawable(iconIds[position]);
}
}
@ -1838,65 +1868,43 @@ public class StyledPlayerControlView extends FrameLayout {
@Override
public int getItemCount() {
return mainTexts.size();
return mainTexts.length;
}
public void updateSubTexts(int position, String subText) {
if (this.subTexts != null) {
this.subTexts.set(position, subText);
}
}
private class SettingsViewHolder extends RecyclerView.ViewHolder {
TextView mainTextView;
TextView subTextView;
ImageView iconView;
SettingsViewHolder(View itemView) {
super(itemView);
mainTextView = itemView.findViewById(R.id.exo_main_text);
subTextView = itemView.findViewById(R.id.exo_sub_text);
iconView = itemView.findViewById(R.id.exo_icon);
itemView.setOnClickListener(
v -> {
int position = SettingsViewHolder.this.getAdapterPosition();
if (position == RecyclerView.NO_POSITION) {
return;
}
if (position == SETTINGS_PLAYBACK_SPEED_POSITION) {
subSettingsAdapter.setTexts(playbackSpeedTextList);
subSettingsAdapter.setCheckPosition(selectedPlaybackSpeedIndex);
selectedMainSettingsPosition = SETTINGS_PLAYBACK_SPEED_POSITION;
displaySettingsWindow(subSettingsAdapter);
} else if (position == SETTINGS_AUDIO_TRACK_SELECTION_POSITION) {
selectedMainSettingsPosition = SETTINGS_AUDIO_TRACK_SELECTION_POSITION;
displaySettingsWindow(audioTrackSelectionAdapter);
} else {
settingsWindow.dismiss();
}
});
}
public void setSubTextAtPosition(int position, String subText) {
this.subTexts[position] = subText;
}
}
private class SubSettingsAdapter
extends RecyclerView.Adapter<SubSettingsAdapter.SubSettingsViewHolder> {
private class SettingViewHolder extends RecyclerView.ViewHolder {
private final TextView mainTextView;
private final TextView subTextView;
private final ImageView iconView;
public SettingViewHolder(View itemView) {
super(itemView);
mainTextView = itemView.findViewById(R.id.exo_main_text);
subTextView = itemView.findViewById(R.id.exo_sub_text);
iconView = itemView.findViewById(R.id.exo_icon);
itemView.setOnClickListener(
v -> onSettingViewClicked(SettingViewHolder.this.getAdapterPosition()));
}
}
private class SubSettingsAdapter extends RecyclerView.Adapter<SubSettingViewHolder> {
@Nullable private List<String> texts;
private int checkPosition;
@Override
public SubSettingsViewHolder 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 SubSettingsViewHolder(v);
return new SubSettingViewHolder(v);
}
@Override
public void onBindViewHolder(SubSettingsViewHolder holder, int position) {
public void onBindViewHolder(SubSettingViewHolder holder, int position) {
if (texts != null) {
holder.textView.setText(texts.get(position));
}
@ -1915,33 +1923,18 @@ public class StyledPlayerControlView extends FrameLayout {
public void setCheckPosition(int checkPosition) {
this.checkPosition = checkPosition;
}
}
private class SubSettingsViewHolder extends RecyclerView.ViewHolder {
TextView textView;
View checkView;
private class SubSettingViewHolder extends RecyclerView.ViewHolder {
private final TextView textView;
private final View checkView;
SubSettingsViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.exo_text);
checkView = itemView.findViewById(R.id.exo_check);
itemView.setOnClickListener(
v -> {
int position = SubSettingsViewHolder.this.getAdapterPosition();
if (position == RecyclerView.NO_POSITION) {
return;
}
if (selectedMainSettingsPosition == SETTINGS_PLAYBACK_SPEED_POSITION) {
if (position != selectedPlaybackSpeedIndex) {
float speed = playbackSpeedMultBy100List.get(position) / 100.0f;
setPlaybackSpeed(speed);
}
}
settingsWindow.dismiss();
});
}
public SubSettingViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.exo_text);
checkView = itemView.findViewById(R.id.exo_check);
itemView.setOnClickListener(
v -> onSubSettingViewClicked(SubSettingViewHolder.this.getAdapterPosition()));
}
}
@ -2057,7 +2050,7 @@ public class StyledPlayerControlView extends FrameLayout {
}
checkNotNull(trackSelector).setParameters(parametersBuilder);
}
settingsAdapter.updateSubTexts(
settingsAdapter.setSubTextAtPosition(
SETTINGS_AUDIO_TRACK_SELECTION_POSITION,
getResources().getString(R.string.exo_track_selection_auto));
settingsWindow.dismiss();
@ -2066,7 +2059,7 @@ public class StyledPlayerControlView extends FrameLayout {
@Override
public void onTrackSelection(String subtext) {
settingsAdapter.updateSubTexts(SETTINGS_AUDIO_TRACK_SELECTION_POSITION, subtext);
settingsAdapter.setSubTextAtPosition(SETTINGS_AUDIO_TRACK_SELECTION_POSITION, subtext);
}
@Override
@ -2086,20 +2079,20 @@ public class StyledPlayerControlView extends FrameLayout {
}
}
if (trackInfos.isEmpty()) {
settingsAdapter.updateSubTexts(
settingsAdapter.setSubTextAtPosition(
SETTINGS_AUDIO_TRACK_SELECTION_POSITION,
getResources().getString(R.string.exo_track_selection_none));
// TODO(insun) : Make the audio item in main settings (settingsAdapater)
// to be non-clickable.
} else if (!hasSelectionOverride) {
settingsAdapter.updateSubTexts(
settingsAdapter.setSubTextAtPosition(
SETTINGS_AUDIO_TRACK_SELECTION_POSITION,
getResources().getString(R.string.exo_track_selection_auto));
} else {
for (int i = 0; i < trackInfos.size(); i++) {
TrackInfo track = trackInfos.get(i);
if (track.selected) {
settingsAdapter.updateSubTexts(
settingsAdapter.setSubTextAtPosition(
SETTINGS_AUDIO_TRACK_SELECTION_POSITION, track.trackName);
break;
}

View file

@ -34,14 +34,4 @@
<item>1.5x</item>
<item>2x</item>
</string-array>
<string-array translatable="false" name="exo_settings_main_texts">
<item>@string/exo_controls_playback_speed</item>
<item>@string/exo_controls_audio_track</item>
</string-array>
<array name="exo_settings_icon_ids">
<item>@drawable/exo_styled_controls_speed</item>
<item>@drawable/exo_styled_controls_audiotrack</item>
</array>
</resources>

View file

@ -44,6 +44,7 @@
<string name="exo_controls_shuffle_off_description">Shuffle off</string>
<!-- Description for a media control button that toggles whether a video playback is in VR mode. [CHAR LIMIT=30] -->
<string name="exo_controls_vr_description">VR mode</string>
<!-- Description for a button that downloads a piece of media content onto the device. [CHAR LIMIT=20] -->
<string name="exo_download_description">Download</string>
<!-- Default name for a notification channel corresponding to media downloads. [CHAR LIMIT=40] -->
@ -56,6 +57,7 @@
<string name="exo_download_failed">Download failed</string>
<!-- Shown in a notification or UI component to indicate downloads are being removed from the device. [CHAR LIMIT=40] -->
<string name="exo_download_removing">Removing downloads</string>
<!-- The title of a track selection view for video tracks. [CHAR LIMIT=20] -->
<string name="exo_track_selection_title_video">Video</string>
<!-- The title of a track selection view for audio tracks. [CHAR LIMIT=20] -->
@ -90,13 +92,10 @@
<string name="exo_track_role_closed_captions">CC</string>
<!-- Describes the bitrate of a media track in Megabits (https://en.wikipedia.org/wiki/Megabit) per second. [CHAR LIMIT=20] -->
<string name="exo_track_bitrate"><xliff:g id="bitrate" example="5.2">%1$.2f</xliff:g> Mbps</string>
<!-- Defines a way of appending an item to a list of items. For example appending "banana" to "apple, pear" to get "apple, pear, banana". Note: the command separator will appear between all consecutive list items, so do not use an equivalent of 'and'. [CHAR LIMIT=40] -->
<string name="exo_item_list"><xliff:g id="list" example="apple, pear">%1$s</xliff:g>, <xliff:g id="item" example="banana">%2$s</xliff:g></string>
<!-- The title of audio track selection. It is shown with the currently selected audio track's
information (such as language). If a user clicks it, the list of possible audio tracks
will be shown. [CHAR_LIMIT=20] -->
<string name="exo_controls_audio_track">Audio track</string>
<!-- The title of playback speed selection. It is shown with the current playback speed.
If a user clicks it, the list of possible playback speeds will be shown.
[CHAR_LIMIT=32] -->
@ -104,9 +103,8 @@
<!-- It implies that the playback speed is normal (1.0x). [CHAR_LIMIT=16] -->
<string name="exo_controls_playback_speed_normal">Normal</string>
<!-- Text for displaying custom playback speed. [CHAR_LIMIT=16] -->
<string translatable="false" name="exo_controls_custom_playback_speed">
<xliff:g id="playback_speed" example="1.05">%1$.2f</xliff:g>x
</string>
<string translatable="false" name="exo_controls_custom_playback_speed"><xliff:g id="playback_speed" example="1.05">%1$.2f</xliff:g>x</string>
<!-- Placeholder text for displaying time. Used to calculate which size layout to use. -->
<string translatable="false" name="exo_controls_time_placeholder">00:00:00</string>
<!-- Content description of the left arrow button to navigate a list of buttons.