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 d5f46fb22..d4897ecaa 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/VideoAudioSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/VideoAudioSettingsFragment.java @@ -6,11 +6,15 @@ import android.os.Build; import android.os.Bundle; import android.provider.Settings; +import android.text.format.DateUtils; +import android.widget.Toast; import androidx.annotation.Nullable; import androidx.preference.ListPreference; import com.google.android.material.snackbar.Snackbar; +import java.util.LinkedList; +import java.util.List; import org.schabi.newpipe.R; import org.schabi.newpipe.util.PermissionHelper; @@ -22,23 +26,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); - int currentDurationValue; - for (int i = 0; i < durationsDescriptions.length; i++) { - currentDurationValue = Integer.parseInt(durationsValues[i]) / 1000; - try { - durationsDescriptions[i] = 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.setEntries(durationsDescriptions); + updateSeekOptions(); listener = (sharedPreferences, s) -> { @@ -58,10 +46,59 @@ 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) / (int) DateUtils.SECOND_IN_MILLIS; + if (inexactSeek && currentDurationValue % 10 == 5) { + continue; + } + + displayedDurationValues.add(durationsValue); + try { + 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])); + final int selectedDuration = Integer.parseInt(durations.getValue()); + if (selectedDuration / (int) DateUtils.SECOND_IN_MILLIS % 10 == 5) { + final int newDuration = selectedDuration / (int) DateUtils.SECOND_IN_MILLIS + 5; + durations.setValue(Integer.toString(newDuration * (int) DateUtils.SECOND_IN_MILLIS)); + + Toast toast = Toast + .makeText(getContext(), + getString(R.string.new_seek_duration_toast, newDuration), + Toast.LENGTH_LONG); + toast.show(); + } + } @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8cae10a63..9b34e75ec 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -71,7 +71,7 @@ Remember popup size and position Remember last size and position of popup Use fast inexact seek - Inexact seek allows the player to seek to positions faster with reduced precision + Inexact seek allows the player to seek to positions faster with reduced precision. Seeking for 5, 15 or 25 seconds doesn\'t work with this. Fast-forward/-rewind seek duration Load thumbnails Show comments @@ -593,6 +593,7 @@ App language System default %s seconds + Due to ExoPlayer constraints the seek duration was set to %d seconds %s seconds