Skip to content

Commit f48e73e

Browse files
committed
Cleaned up some code related to app language
* Use build constants when possible * Inline variables * Don't use var for normal-sized types (that way it's easier to review) * Split code into methods
1 parent 99003ba commit f48e73e

2 files changed

Lines changed: 42 additions & 32 deletions

File tree

app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,17 @@ public void onCreatePreferences(final Bundle savedInstanceState, final String ro
3333

3434
addPreferencesFromResourceRegistry();
3535

36-
final var appLanguagePref = requirePreference(R.string.app_language_key);
37-
if (Build.VERSION.SDK_INT >= 33) {
36+
setupAppLanguagePreferences();
37+
setupImageQualityPref();
38+
}
39+
40+
private void setupAppLanguagePreferences() {
41+
final Preference appLanguagePref = requirePreference(R.string.app_language_key);
42+
// Android 13+ allows to set app specific languages
43+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
3844
appLanguagePref.setVisible(false);
39-
final var newAppLanguagePref =
45+
46+
final Preference newAppLanguagePref =
4047
requirePreference(R.string.app_language_android_13_and_up_key);
4148
newAppLanguagePref.setSummaryProvider(preference -> {
4249
final Locale loc = AppCompatDelegate.getApplicationLocales().get(0);
@@ -49,30 +56,33 @@ public void onCreatePreferences(final Bundle savedInstanceState, final String ro
4956
return true;
5057
});
5158
newAppLanguagePref.setVisible(true);
52-
} else {
53-
appLanguagePref.setOnPreferenceChangeListener((preference, newValue) -> {
54-
final String language = (String) newValue;
55-
final String systemLang = getString(R.string.default_localization_key);
56-
final String tag = systemLang.equals(language) ? null : language;
57-
AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(tag));
58-
return true;
59-
});
59+
return;
6060
}
6161

62+
appLanguagePref.setOnPreferenceChangeListener((preference, newValue) -> {
63+
final String language = (String) newValue;
64+
final String systemLang = getString(R.string.default_localization_key);
65+
final String tag = systemLang.equals(language) ? null : language;
66+
AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(tag));
67+
return true;
68+
});
69+
}
70+
71+
private void setupImageQualityPref() {
6272
requirePreference(R.string.image_quality_key).setOnPreferenceChangeListener(
63-
(preference, newValue) -> {
64-
ImageStrategy.setPreferredImageQuality(PreferredImageQuality
65-
.fromPreferenceKey(requireContext(), (String) newValue));
66-
try {
67-
PicassoHelper.clearCache(preference.getContext());
68-
Toast.makeText(preference.getContext(),
69-
R.string.thumbnail_cache_wipe_complete_notice, Toast.LENGTH_SHORT)
70-
.show();
71-
} catch (final IOException e) {
72-
Log.e(TAG, "Unable to clear Picasso cache", e);
73-
}
74-
return true;
75-
});
73+
(preference, newValue) -> {
74+
ImageStrategy.setPreferredImageQuality(PreferredImageQuality
75+
.fromPreferenceKey(requireContext(), (String) newValue));
76+
try {
77+
PicassoHelper.clearCache(preference.getContext());
78+
Toast.makeText(preference.getContext(),
79+
R.string.thumbnail_cache_wipe_complete_notice, Toast.LENGTH_SHORT)
80+
.show();
81+
} catch (final IOException e) {
82+
Log.e(TAG, "Unable to clear Picasso cache", e);
83+
}
84+
return true;
85+
});
7686
}
7787

7888
@Override
@@ -93,9 +103,9 @@ public boolean onPreferenceTreeClick(final Preference preference) {
93103
public void onDestroy() {
94104
super.onDestroy();
95105

96-
final var context = requireContext();
97-
final var selectedLocalization = Localization.getPreferredLocalization(context);
98-
final var selectedContentCountry = Localization.getPreferredContentCountry(context);
99-
NewPipe.setupLocalization(selectedLocalization, selectedContentCountry);
106+
final Context context = requireContext();
107+
NewPipe.setupLocalization(
108+
Localization.getPreferredLocalization(context),
109+
Localization.getPreferredContentCountry(context));
100110
}
101111
}

app/src/main/java/org/schabi/newpipe/util/Localization.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,17 +448,17 @@ public static void migrateAppLanguageSettingIfNecessary(@NonNull final Context c
448448
final String appLanguageKey = context.getString(R.string.app_language_key);
449449
final String appLanguageValue = sp.getString(appLanguageKey, null);
450450
if (appLanguageValue != null) {
451-
// The app language key is used on Android versions < Tiramisu; for more info, see
452-
// ContentSettingsFragment.
451+
// The app language key is used on Android versions < 33
452+
// for more info, see ContentSettingsFragment
453453
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
454454
sp.edit().remove(appLanguageKey).apply();
455455
}
456456
final String appLanguageDefaultValue =
457457
context.getString(R.string.default_localization_key);
458458
if (!appLanguageValue.equals(appLanguageDefaultValue)) {
459459
try {
460-
final var locales = LocaleListCompat.forLanguageTags(appLanguageValue);
461-
AppCompatDelegate.setApplicationLocales(locales);
460+
AppCompatDelegate.setApplicationLocales(
461+
LocaleListCompat.forLanguageTags(appLanguageValue));
462462
} catch (final RuntimeException e) {
463463
Log.e(TAG, "Failed to migrate previous custom app language "
464464
+ "setting to public per-app language APIs"

0 commit comments

Comments
 (0)