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:
parent
99003bab07
commit
f48e73eb2a
@ -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,30 +56,33 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
|
||||
return true;
|
||||
});
|
||||
newAppLanguagePref.setVisible(true);
|
||||
} else {
|
||||
appLanguagePref.setOnPreferenceChangeListener((preference, newValue) -> {
|
||||
final String language = (String) newValue;
|
||||
final String systemLang = getString(R.string.default_localization_key);
|
||||
final String tag = systemLang.equals(language) ? null : language;
|
||||
AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(tag));
|
||||
return true;
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
appLanguagePref.setOnPreferenceChangeListener((preference, newValue) -> {
|
||||
final String language = (String) newValue;
|
||||
final String systemLang = getString(R.string.default_localization_key);
|
||||
final String tag = systemLang.equals(language) ? null : language;
|
||||
AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(tag));
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
private void setupImageQualityPref() {
|
||||
requirePreference(R.string.image_quality_key).setOnPreferenceChangeListener(
|
||||
(preference, newValue) -> {
|
||||
ImageStrategy.setPreferredImageQuality(PreferredImageQuality
|
||||
.fromPreferenceKey(requireContext(), (String) newValue));
|
||||
try {
|
||||
PicassoHelper.clearCache(preference.getContext());
|
||||
Toast.makeText(preference.getContext(),
|
||||
R.string.thumbnail_cache_wipe_complete_notice, Toast.LENGTH_SHORT)
|
||||
.show();
|
||||
} catch (final IOException e) {
|
||||
Log.e(TAG, "Unable to clear Picasso cache", e);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
(preference, newValue) -> {
|
||||
ImageStrategy.setPreferredImageQuality(PreferredImageQuality
|
||||
.fromPreferenceKey(requireContext(), (String) newValue));
|
||||
try {
|
||||
PicassoHelper.clearCache(preference.getContext());
|
||||
Toast.makeText(preference.getContext(),
|
||||
R.string.thumbnail_cache_wipe_complete_notice, Toast.LENGTH_SHORT)
|
||||
.show();
|
||||
} catch (final IOException e) {
|
||||
Log.e(TAG, "Unable to clear Picasso cache", e);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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"
|
||||
|
Loading…
x
Reference in New Issue
Block a user