From d1a82a85cdc28d9226a3b4e6d7dcfceac479e6b6 Mon Sep 17 00:00:00 2001 From: Edward Date: Wed, 29 Mar 2023 01:04:26 +0800 Subject: [PATCH] Include a high-resolution option in the default resolution settings. --- .../settings/VideoAudioSettingsFragment.java | 65 ++++++++++++++++++- .../org/schabi/newpipe/util/ListHelper.java | 25 +++++++ app/src/main/res/values/settings_keys.xml | 15 +++++ app/src/main/res/values/strings.xml | 4 ++ 4 files changed, 108 insertions(+), 1 deletion(-) 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 aae9cfca5..b498bcf30 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/VideoAudioSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/VideoAudioSettingsFragment.java @@ -13,6 +13,7 @@ import androidx.preference.ListPreference; import com.google.android.material.snackbar.Snackbar; import org.schabi.newpipe.R; +import org.schabi.newpipe.util.ListHelper; import org.schabi.newpipe.util.PermissionHelper; import java.util.LinkedList; @@ -26,7 +27,7 @@ public class VideoAudioSettingsFragment extends BasePreferenceFragment { addPreferencesFromResourceRegistry(); updateSeekOptions(); - + updateResolutionOptions(); listener = (sharedPreferences, key) -> { // on M and above, if user chooses to minimise to popup player on exit @@ -48,10 +49,72 @@ public class VideoAudioSettingsFragment extends BasePreferenceFragment { } } else if (getString(R.string.use_inexact_seek_key).equals(key)) { updateSeekOptions(); + } else if (getString(R.string.show_higher_resolutions_key).equals(key)) { + updateResolutionOptions(); } }; } + /** + * Update default resolution, default popup resolution & mobile data resolution options. + * show high resolution when "Show higher resolution" option enabled. + */ + private void updateResolutionOptions() { + final ListPreference defaultResolution = findPreference( + getString(R.string.default_resolution_key)); + final ListPreference defaultPopupResolution = findPreference( + getString(R.string.default_popup_resolution_key)); + final ListPreference mobileDataResolution = findPreference( + getString(R.string.limit_mobile_data_usage_key)); + final Resources resources = getResources(); + final boolean showHigherResolutions = getPreferenceManager().getSharedPreferences() + .getBoolean(resources.getString(R.string.show_higher_resolutions_key), false); + final List resolutionListDescriptions = ListHelper.getSortedResolutionList( + resources, + R.array.resolution_list_description, + R.array.high_resolution_list_descriptions, + showHigherResolutions); + final List resolutionListValues = ListHelper.getSortedResolutionList( + resources, + R.array.resolution_list_values, + R.array.high_resolution_list_values, + showHigherResolutions); + final List limitDataUsageResolutionValues = ListHelper.getSortedResolutionList( + resources, + R.array.limit_data_usage_values_list, + R.array.high_resolution_limit_data_usage_values_list, + showHigherResolutions); + final List limitDataUsageResolutionDescriptions = ListHelper + .getSortedResolutionList(resources, + R.array.limit_data_usage_description_list, + R.array.high_resolution_list_descriptions, + showHigherResolutions); + defaultResolution.setEntries(resolutionListDescriptions.toArray(new String[0])); + defaultResolution.setEntryValues(resolutionListValues.toArray(new String[0])); + defaultPopupResolution.setEntries(resolutionListDescriptions.toArray(new String[0])); + defaultPopupResolution.setEntryValues(resolutionListValues.toArray(new String[0])); + mobileDataResolution.setEntries( + limitDataUsageResolutionDescriptions.toArray(new String[0])); + mobileDataResolution.setEntryValues(limitDataUsageResolutionValues.toArray(new String[0])); + if (!showHigherResolutions) { + if (ListHelper.isHighResolutionSelected(defaultResolution.getValue(), + R.array.high_resolution_list_values, + resources)) { + defaultResolution.setValueIndex(0); + } + if (ListHelper.isHighResolutionSelected(defaultPopupResolution.getValue(), + R.array.high_resolution_list_values, + resources)) { + defaultPopupResolution.setValueIndex(0); + } + if (ListHelper.isHighResolutionSelected(mobileDataResolution.getValue(), + R.array.high_resolution_limit_data_usage_values_list, + resources)) { + mobileDataResolution.setValueIndex(0); + } + } + } + /** * Update fast-forward/-rewind seek duration options * according to language and inexact seek setting. diff --git a/app/src/main/java/org/schabi/newpipe/util/ListHelper.java b/app/src/main/java/org/schabi/newpipe/util/ListHelper.java index f45f3786d..ead98211e 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ListHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ListHelper.java @@ -4,6 +4,7 @@ import static org.schabi.newpipe.extractor.ServiceList.YouTube; import android.content.Context; import android.content.SharedPreferences; +import android.content.res.Resources; import android.net.ConnectivityManager; import androidx.annotation.NonNull; @@ -239,6 +240,30 @@ public final class ListHelper { videoOnlyStreams, ascendingOrder, preferVideoOnlyStreams); } + public static List getSortedResolutionList( + final Resources resources, + final int defaultResolutionKey, + final int additionalResolutionKey, + final boolean showHigherResolutions) { + final List defaultResolution = new ArrayList(Arrays.asList( + resources.getStringArray(defaultResolutionKey))); + if (!showHigherResolutions) { + return defaultResolution; + } + final List additionalResolutions = Arrays.asList( + resources.getStringArray(additionalResolutionKey)); + defaultResolution.addAll(1, additionalResolutions); + return defaultResolution; + } + + public static boolean isHighResolutionSelected(final String selectedResolution, + final int additionalResolutionKey, + final Resources resources) { + return Arrays.asList(resources.getStringArray( + additionalResolutionKey)) + .contains(selectedResolution); + } + /** * Filter the list of audio streams and return a list with the preferred stream for * each audio track. Streams are sorted with the preferred language in the first position. diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index 56fc19eed..0ec60dbdf 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -124,6 +124,16 @@ 480p best_resolution + + 2160p + 1440p + + + + 2160p + 1440p + + @string/best_resolution_key 1080p60 @@ -1301,6 +1311,11 @@ 144p + + 2160p + 1440p + + list_view_mode @string/list_view_mode_auto_key diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5cf214c0d..984f52233 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -543,6 +543,10 @@ 240p 144p + + 2160p + 1440p + New streams notifications Notify about new streams from subscriptions