From 8499d9e798ec49479befa0dfa5df89df31ac133c Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Thu, 24 Oct 2024 08:29:49 -0400 Subject: [PATCH] fix(YouTube - Settings): Use multiline preference title for localized languages --- .../revanced/integrations/shared/Utils.java | 30 +++++++++++++++++++ .../AbstractPreferenceFragment.java | 6 ++-- .../speed/CustomPlaybackSpeedPatch.java | 1 + .../ReVancedPreferenceFragment.java | 4 --- ...eturnYouTubeDislikePreferenceFragment.java | 3 ++ .../SponsorBlockPreferenceFragment.java | 4 +++ 6 files changed, 42 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/shared/Utils.java b/app/src/main/java/app/revanced/integrations/shared/Utils.java index 296c0b3bc0..d9ae8d0c76 100644 --- a/app/src/main/java/app/revanced/integrations/shared/Utils.java +++ b/app/src/main/java/app/revanced/integrations/shared/Utils.java @@ -13,6 +13,7 @@ import android.os.Handler; import android.os.Looper; import android.preference.Preference; +import android.preference.PreferenceCategory; import android.preference.PreferenceGroup; import android.preference.PreferenceScreen; import android.view.View; @@ -722,6 +723,35 @@ public static void sortPreferenceGroups(@NonNull PreferenceGroup group) { } } + /** + * Set all preferences to multiline titles if the device is not using an English variant. + * The English strings are heavily scrutinized and all titles fit on screen + * except 2 or 3 preference strings and those do not affect readability. + * + * Allowing multiline for those 2 or 3 English preferences looks weird and out of place, + * and visually it looks better to clip the text and keep all titles 1 line. + */ + @SuppressWarnings("deprecation") + public static void setPreferenceTitlesToMultiLineIfNeeded(PreferenceGroup group) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + return; + } + + String deviceLanguage = Utils.getContext().getResources().getConfiguration().locale.getLanguage(); + if (deviceLanguage.equals("en")) { + return; + } + + for (int i = 0, prefCount = group.getPreferenceCount(); i < prefCount; i++) { + Preference pref = group.getPreference(i); + pref.setSingleLineTitle(false); + + if (pref instanceof PreferenceCategory) { + setPreferenceTitlesToMultiLineIfNeeded((PreferenceCategory) pref); + } + } + } + /** * If {@link Fragment} uses [Android library] rather than [AndroidX library], * the Dialog theme corresponding to [Android library] should be used. diff --git a/app/src/main/java/app/revanced/integrations/shared/settings/preference/AbstractPreferenceFragment.java b/app/src/main/java/app/revanced/integrations/shared/settings/preference/AbstractPreferenceFragment.java index e6d07ccde1..5845bfbea1 100644 --- a/app/src/main/java/app/revanced/integrations/shared/settings/preference/AbstractPreferenceFragment.java +++ b/app/src/main/java/app/revanced/integrations/shared/settings/preference/AbstractPreferenceFragment.java @@ -80,10 +80,12 @@ public abstract class AbstractPreferenceFragment extends PreferenceFragment { */ protected void initialize() { final var identifier = Utils.getResourceIdentifier("revanced_prefs", "xml"); - if (identifier == 0) return; addPreferencesFromResource(identifier); - Utils.sortPreferenceGroups(getPreferenceScreen()); + + PreferenceScreen screen = getPreferenceScreen(); + Utils.sortPreferenceGroups(screen); + Utils.setPreferenceTitlesToMultiLineIfNeeded(screen); } private void showSettingUserDialogConfirmation(SwitchPreference switchPref, BooleanSetting setting) { diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/playback/speed/CustomPlaybackSpeedPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/playback/speed/CustomPlaybackSpeedPatch.java index 2ca56ed315..1be312d7e7 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/playback/speed/CustomPlaybackSpeedPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/playback/speed/CustomPlaybackSpeedPatch.java @@ -89,6 +89,7 @@ private static boolean arrayContains(float[] array, float value) { /** * Initialize a settings preference list with the available playback speeds. */ + @SuppressWarnings("deprecation") public static void initializeListPreference(ListPreference preference) { if (preferenceListEntries == null) { preferenceListEntries = new String[customPlaybackSpeeds.length]; diff --git a/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java b/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java index 58fd974633..3ae54a0d72 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java +++ b/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java @@ -1,11 +1,8 @@ package app.revanced.integrations.youtube.settings.preference; -import android.os.Build; import android.preference.ListPreference; import android.preference.Preference; -import androidx.annotation.RequiresApi; - import app.revanced.integrations.shared.Logger; import app.revanced.integrations.shared.settings.preference.AbstractPreferenceFragment; import app.revanced.integrations.youtube.patches.playback.speed.CustomPlaybackSpeedPatch; @@ -18,7 +15,6 @@ */ public class ReVancedPreferenceFragment extends AbstractPreferenceFragment { - @RequiresApi(api = Build.VERSION_CODES.O) @Override protected void initialize() { super.initialize(); diff --git a/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReturnYouTubeDislikePreferenceFragment.java b/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReturnYouTubeDislikePreferenceFragment.java index c4a6c9be97..c1c8cecdd0 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReturnYouTubeDislikePreferenceFragment.java +++ b/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReturnYouTubeDislikePreferenceFragment.java @@ -14,6 +14,7 @@ import android.preference.SwitchPreference; import app.revanced.integrations.shared.Logger; +import app.revanced.integrations.shared.Utils; import app.revanced.integrations.shared.settings.Setting; import app.revanced.integrations.shared.settings.BaseSettings; import app.revanced.integrations.youtube.patches.ReturnYouTubeDislikePatch; @@ -218,6 +219,8 @@ public void onCreate(Bundle savedInstanceState) { "revanced_ryd_statistics_getNumberOfRateLimitRequestsEncountered_non_zero_summary")); preferenceScreen.addPreference(statisticPreference); } + + Utils.setPreferenceTitlesToMultiLineIfNeeded(preferenceScreen); } catch (Exception ex) { Logger.printException(() -> "onCreate failure", ex); } diff --git a/app/src/main/java/app/revanced/integrations/youtube/settings/preference/SponsorBlockPreferenceFragment.java b/app/src/main/java/app/revanced/integrations/youtube/settings/preference/SponsorBlockPreferenceFragment.java index 7b00931995..30fc23c03e 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/settings/preference/SponsorBlockPreferenceFragment.java +++ b/app/src/main/java/app/revanced/integrations/youtube/settings/preference/SponsorBlockPreferenceFragment.java @@ -155,6 +155,8 @@ public void onCreate(Bundle savedInstanceState) { addAboutCategory(context, preferenceScreen); updateUI(); + + Utils.setPreferenceTitlesToMultiLineIfNeeded(preferenceScreen); } catch (Exception ex) { Logger.printException(() -> "onCreate failure", ex); } @@ -468,6 +470,8 @@ private void fetchAndDisplayStats() { Utils.runOnMainThread(() -> { // get back on main thread to modify UI elements addUserStats(loadingPlaceholderPreference, stats); addLocalUserStats(); + + Utils.setPreferenceTitlesToMultiLineIfNeeded(statsCategory); }); }); } else {