From 446380433804c1c38213c37562d8d34151caca3b Mon Sep 17 00:00:00 2001 From: Xiang Rong Lin <41164160+XiangRongLin@users.noreply.github.com> Date: Sat, 29 Feb 2020 11:53:04 +0100 Subject: [PATCH] Update seek options on inexact seek option change. Reset to 10 seconds when previous value is not valid anymore --- .../settings/VideoAudioSettingsFragment.java | 66 +++++++++++-------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/settings/VideoAudioSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/VideoAudioSettingsFragment.java index 5476158c4..20e68078a 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/VideoAudioSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/VideoAudioSettingsFragment.java @@ -24,32 +24,7 @@ public class VideoAudioSettingsFragment extends BasePreferenceFragment { public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - //initializing R.array.seek_duration_description to display the translation of seconds - Resources res = getResources(); - String[] durationsValues = res.getStringArray(R.array.seek_duration_value); - String[] durationsDescriptions = res.getStringArray(R.array.seek_duration_description); - List durationsValResult = new LinkedList<>(); - List durationsDesResult = new LinkedList<>(); - int currentDurationValue; - final boolean inexactSeek = getPreferenceManager().getSharedPreferences() - .getBoolean(res.getString(R.string.use_inexact_seek_key), false); - - for (int i = 0; i < durationsDescriptions.length; i++) { - currentDurationValue = Integer.parseInt(durationsValues[i]) / 1000; - if (inexactSeek && currentDurationValue % 10 != 5) { - try { - durationsValResult.add(durationsValues[i]); - durationsDesResult.add(String.format( - res.getQuantityString(R.plurals.dynamic_seek_duration_description, currentDurationValue), - currentDurationValue)); - } catch (Resources.NotFoundException ignored) { - //if this happens, the translation is missing, and the english string will be displayed instead - } - } - } - ListPreference durations = (ListPreference) findPreference(getString(R.string.seek_duration_key)); - durations.setEntryValues(durationsValResult.toArray(new CharSequence[0])); - durations.setEntries(durationsDesResult.toArray(new CharSequence[0])); + updateSeekOptions(); listener = (sharedPreferences, s) -> { @@ -69,10 +44,49 @@ public class VideoAudioSettingsFragment extends BasePreferenceFragment { .show(); } + } else if (s.equals(getString(R.string.use_inexact_seek_key))) { + updateSeekOptions(); } }; } + /** + * Update fast-forward/-rewind seek duration options according to language and inexact seek setting. + * Exoplayer can't seek 5 seconds in audio when using inexact seek. + */ + private void updateSeekOptions() { + //initializing R.array.seek_duration_description to display the translation of seconds + final Resources res = getResources(); + final String[] durationsValues = res.getStringArray(R.array.seek_duration_value); + final List displayedDurationValues = new LinkedList<>(); + final List displayedDescriptionValues = new LinkedList<>(); + int currentDurationValue; + final boolean inexactSeek = getPreferenceManager().getSharedPreferences() + .getBoolean(res.getString(R.string.use_inexact_seek_key), false); + + for (String durationsValue : durationsValues) { + currentDurationValue = Integer.parseInt(durationsValue) / 1000; + if (inexactSeek && currentDurationValue % 10 == 5) { + continue; + } + try { + displayedDurationValues.add(durationsValue); + displayedDescriptionValues.add(String.format( + res.getQuantityString(R.plurals.dynamic_seek_duration_description, + currentDurationValue), + currentDurationValue)); + } catch (Resources.NotFoundException ignored) { + //if this happens, the translation is missing, and the english string will be displayed instead + } + } + final ListPreference durations = (ListPreference) findPreference(getString(R.string.seek_duration_key)); + durations.setEntryValues(displayedDurationValues.toArray(new CharSequence[0])); + durations.setEntries(displayedDescriptionValues.toArray(new CharSequence[0])); + if (Integer.parseInt(durations.getValue()) / 1000 % 10 == 5) { + durations.setValueIndex(0); + } + } + @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { addPreferencesFromResource(R.xml.video_audio_settings);