From 208887d538736353d2836ba1f3425fd67d1154a0 Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Sat, 18 Mar 2023 14:50:19 +0100 Subject: [PATCH] feat: improve audio track sorting, add prefer_descriptive_audio option --- .../org/schabi/newpipe/util/ListHelper.java | 53 ++++++++++--------- app/src/main/res/values/settings_keys.xml | 1 + app/src/main/res/values/strings.xml | 2 + app/src/main/res/xml/content_settings.xml | 8 +++ 4 files changed, 39 insertions(+), 25 deletions(-) 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 ed1e1e801..2714337c2 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ListHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ListHelper.java @@ -233,33 +233,16 @@ public final class ListHelper { // Fall back to English if the preferred language was not found final String preferredLanguageOrEnglish = hasPreferredLanguage ? preferredLanguage : Locale.ENGLISH.getISO3Language(); + final SharedPreferences preferences = + PreferenceManager.getDefaultSharedPreferences(context); + final boolean preferDescriptiveAudio = + preferences.getBoolean(context.getString(R.string.prefer_descriptive_audio_key), + false); + final Comparator trackCmp = + getAudioTrackComparator(preferredLanguageOrEnglish, preferDescriptiveAudio); // Sort collected streams - return collectedStreams.values().stream() - .sorted((s1, s2) -> { - // Preferred language comes first - if (s1.getAudioLocale() != null - && s1.getAudioLocale().getISO3Language() - .equals(preferredLanguageOrEnglish)) { - return -1; - } - if (s2.getAudioLocale() != null - && s2.getAudioLocale().getISO3Language() - .equals(preferredLanguageOrEnglish)) { - return 1; - } - - // Sort audio tracks alphabetically - if (s1.getAudioTrackName() != null) { - if (s2.getAudioTrackName() != null) { - return s1.getAudioTrackName().compareTo(s2.getAudioTrackName()); - } else { - return -1; - } - } - return 1; - }) - .collect(Collectors.toList()); + return collectedStreams.values().stream().sorted(trackCmp).collect(Collectors.toList()); } /*////////////////////////////////////////////////////////////////////////// @@ -680,4 +663,24 @@ public final class ListHelper { return manager.isActiveNetworkMetered(); } + + private static Comparator getAudioTrackComparator( + final String preferredLanguage, final boolean preferDescriptiveAudio) { + return Comparator.comparing(AudioStream::getAudioLocale, (o1, o2) -> Boolean.compare( + o1 == null || !o1.getISO3Language().equals(preferredLanguage), + o2 == null || !o2.getISO3Language().equals(preferredLanguage)) + ).thenComparing(AudioStream::isDescriptive, (o1, o2) -> + Boolean.compare(o1 ^ preferDescriptiveAudio, o2 ^ preferDescriptiveAudio) + ).thenComparing(AudioStream::getAudioTrackName, (o1, o2) -> { + if (o1 != null) { + if (o2 != null) { + return o1.compareTo(o2); + } else { + return -1; + } + } else { + return 1; + } + }); + } } diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index 6a1d5cd45..9ed34bf26 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -260,6 +260,7 @@ show_next_video show_description show_meta_info + prefer_descriptive_audio stream_info_selected_tab show_hold_to_append content_language diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e1c75bba4..9867a8f21 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -94,6 +94,8 @@ Turn off to hide video description and additional information Show meta info Turn off to hide meta info boxes with additional information about the stream creator, stream content or a search request + Prefer descriptive audio + Select an audio track with descriptions for visually impaired people if available Image cache wiped Wipe cached metadata Remove all cached webpage data diff --git a/app/src/main/res/xml/content_settings.xml b/app/src/main/res/xml/content_settings.xml index fddb966c8..684b9e558 100644 --- a/app/src/main/res/xml/content_settings.xml +++ b/app/src/main/res/xml/content_settings.xml @@ -114,6 +114,14 @@ app:singleLineTitle="false" app:iconSpaceReserved="false" /> + +