Merge pull request #3160 from XiangRongLin/b3127

Hide 5, 15, 25 second seek options if inexact seek is enabled
This commit is contained in:
Stypox 2020-03-03 21:58:18 +01:00 committed by GitHub
commit d5c29bf1b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 18 deletions

View File

@ -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<String> displayedDurationValues = new LinkedList<>();
final List<String> 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) {

View File

@ -71,7 +71,7 @@
<string name="popup_remember_size_pos_title">Remember popup size and position</string>
<string name="popup_remember_size_pos_summary">Remember last size and position of popup</string>
<string name="use_inexact_seek_title">Use fast inexact seek</string>
<string name="use_inexact_seek_summary">Inexact seek allows the player to seek to positions faster with reduced precision</string>
<string name="use_inexact_seek_summary">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.</string>
<string name="seek_duration_title">Fast-forward/-rewind seek duration</string>
<string name="download_thumbnail_title">Load thumbnails</string>
<string name="show_comments_title">Show comments</string>
@ -593,6 +593,7 @@
<string name="app_language_title">App language</string>
<string name="systems_language">System default</string>
<string name="dynamic_seek_duration_description">%s seconds</string>
<string name="new_seek_duration_toast">Due to ExoPlayer constraints the seek duration was set to %d seconds</string>
<plurals name="dynamic_seek_duration_description">
<item quantity="other">%s seconds</item>
</plurals>