2
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-08-22 01:58:16 +00:00

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
This commit is contained in:
litetex 2025-07-20 21:52:07 +02:00
parent 99003bab07
commit f48e73eb2a
No known key found for this signature in database
GPG Key ID: 525B43E6039B3689
2 changed files with 42 additions and 32 deletions

View File

@ -33,10 +33,17 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
addPreferencesFromResourceRegistry();
final var appLanguagePref = requirePreference(R.string.app_language_key);
if (Build.VERSION.SDK_INT >= 33) {
setupAppLanguagePreferences();
setupImageQualityPref();
}
private void setupAppLanguagePreferences() {
final Preference appLanguagePref = requirePreference(R.string.app_language_key);
// Android 13+ allows to set app specific languages
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
appLanguagePref.setVisible(false);
final var newAppLanguagePref =
final Preference newAppLanguagePref =
requirePreference(R.string.app_language_android_13_and_up_key);
newAppLanguagePref.setSummaryProvider(preference -> {
final Locale loc = AppCompatDelegate.getApplicationLocales().get(0);
@ -49,7 +56,9 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
return true;
});
newAppLanguagePref.setVisible(true);
} else {
return;
}
appLanguagePref.setOnPreferenceChangeListener((preference, newValue) -> {
final String language = (String) newValue;
final String systemLang = getString(R.string.default_localization_key);
@ -59,6 +68,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
});
}
private void setupImageQualityPref() {
requirePreference(R.string.image_quality_key).setOnPreferenceChangeListener(
(preference, newValue) -> {
ImageStrategy.setPreferredImageQuality(PreferredImageQuality
@ -93,9 +103,9 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
public void onDestroy() {
super.onDestroy();
final var context = requireContext();
final var selectedLocalization = Localization.getPreferredLocalization(context);
final var selectedContentCountry = Localization.getPreferredContentCountry(context);
NewPipe.setupLocalization(selectedLocalization, selectedContentCountry);
final Context context = requireContext();
NewPipe.setupLocalization(
Localization.getPreferredLocalization(context),
Localization.getPreferredContentCountry(context));
}
}

View File

@ -448,8 +448,8 @@ public final class Localization {
final String appLanguageKey = context.getString(R.string.app_language_key);
final String appLanguageValue = sp.getString(appLanguageKey, null);
if (appLanguageValue != null) {
// The app language key is used on Android versions < Tiramisu; for more info, see
// ContentSettingsFragment.
// The app language key is used on Android versions < 33
// for more info, see ContentSettingsFragment
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
sp.edit().remove(appLanguageKey).apply();
}
@ -457,8 +457,8 @@ public final class Localization {
context.getString(R.string.default_localization_key);
if (!appLanguageValue.equals(appLanguageDefaultValue)) {
try {
final var locales = LocaleListCompat.forLanguageTags(appLanguageValue);
AppCompatDelegate.setApplicationLocales(locales);
AppCompatDelegate.setApplicationLocales(
LocaleListCompat.forLanguageTags(appLanguageValue));
} catch (final RuntimeException e) {
Log.e(TAG, "Failed to migrate previous custom app language "
+ "setting to public per-app language APIs"