diff --git a/extensions/shared/src/main/java/app/revanced/extension/shared/utils/Utils.java b/extensions/shared/src/main/java/app/revanced/extension/shared/utils/Utils.java index bbad47a50..956f60039 100644 --- a/extensions/shared/src/main/java/app/revanced/extension/shared/utils/Utils.java +++ b/extensions/shared/src/main/java/app/revanced/extension/shared/utils/Utils.java @@ -60,6 +60,7 @@ public class Utils { private static WeakReference activityRef = new WeakReference<>(null); @SuppressLint("StaticFieldLeak") private static volatile Context context; + private static Locale contextLocale; protected Utils() { } // utility class @@ -308,34 +309,51 @@ public class Utils { * @return Context with locale applied. */ public static Context getLocalizedContext(Context mContext) { - Activity mActivity = activityRef.get(); - if (mActivity == null) { - return mContext; - } - if (mContext == null) { - return null; + try { + Activity mActivity = activityRef.get(); + if (mActivity != null && mContext != null) { + AppLanguage language = BaseSettings.REVANCED_LANGUAGE.get(); + + // Locale of Application. + Locale applicationLocale = language == AppLanguage.DEFAULT + ? mActivity.getResources().getConfiguration().locale + : language.getLocale(); + + // Locale of Context. + Locale contextLocale = mContext.getResources().getConfiguration().locale; + + // If they are different, overrides the Locale of the Context and resource. + if (applicationLocale != contextLocale) { + Utils.contextLocale = contextLocale; + + // If they are different, overrides the Locale of the Context and resource. + Locale.setDefault(applicationLocale); + Configuration configuration = new Configuration(mContext.getResources().getConfiguration()); + configuration.setLocale(applicationLocale); + return mContext.createConfigurationContext(configuration); + } + } + } catch (Exception ex) { + Logger.printException(() -> "getLocalizedContext failed", ex); } - AppLanguage language = BaseSettings.REVANCED_LANGUAGE.get(); + return mContext; + } - // Locale of Application. - Locale applicationLocale = language == AppLanguage.DEFAULT - ? mActivity.getResources().getConfiguration().locale - : language.getLocale(); - - // Locale of Context. - Locale contextLocale = mContext.getResources().getConfiguration().locale; - - // If they are identical, no need to override them. - if (applicationLocale == contextLocale) { - return mContext; + public static void resetLocalizedContext() { + try { + if (contextLocale != null) { + Locale.setDefault(contextLocale); + Context mContext = getContext(); + if (mContext != null) { + Configuration config = mContext.getResources().getConfiguration(); + config.setLocale(contextLocale); + setContext(mContext.createConfigurationContext(config)); + } + } + } catch (Exception ex) { + Logger.printException(() -> "resetLocalizedContext failed", ex); } - - // If they are different, overrides the Locale of the Context and resource. - Locale.setDefault(applicationLocale); - Configuration configuration = new Configuration(mContext.getResources().getConfiguration()); - configuration.setLocale(applicationLocale); - return mContext.createConfigurationContext(configuration); } public static void setActivity(Activity mainActivity) { @@ -353,14 +371,6 @@ public class Utils { // Must initially set context to check the app language. context = appContext; Logger.initializationInfo(Utils.class, "Set context: " + appContext); - - AppLanguage language = BaseSettings.REVANCED_LANGUAGE.get(); - if (language != AppLanguage.DEFAULT) { - // Create a new context with the desired language. - Configuration config = appContext.getResources().getConfiguration(); - config.setLocale(language.getLocale()); - context = appContext.createConfigurationContext(config); - } } public static void setClipboard(@NonNull String text) { @@ -538,14 +548,6 @@ public class Utils { return Build.VERSION.SDK_INT >= sdk; } - public static int dpToPx(float dp) { - if (context == null) { - return (int) dp; - } else { - return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics()); - } - } - public static int dpToPx(int dp) { if (context == null) { return dp; diff --git a/extensions/shared/src/main/java/app/revanced/extension/youtube/settings/preference/ReVancedPreferenceFragment.java b/extensions/shared/src/main/java/app/revanced/extension/youtube/settings/preference/ReVancedPreferenceFragment.java index af2864f36..6a2fd721f 100644 --- a/extensions/shared/src/main/java/app/revanced/extension/youtube/settings/preference/ReVancedPreferenceFragment.java +++ b/extensions/shared/src/main/java/app/revanced/extension/youtube/settings/preference/ReVancedPreferenceFragment.java @@ -377,6 +377,7 @@ public class ReVancedPreferenceFragment extends PreferenceFragment { @Override public void onDestroy() { mSharedPreferences.unregisterOnSharedPreferenceChangeListener(listener); + Utils.resetLocalizedContext(); super.onDestroy(); } diff --git a/extensions/shared/src/main/java/com/google/android/apps/youtube/app/settings/videoquality/VideoQualitySettingsActivity.java b/extensions/shared/src/main/java/com/google/android/apps/youtube/app/settings/videoquality/VideoQualitySettingsActivity.java index a969c6bd4..f9c0c50f3 100644 --- a/extensions/shared/src/main/java/com/google/android/apps/youtube/app/settings/videoquality/VideoQualitySettingsActivity.java +++ b/extensions/shared/src/main/java/com/google/android/apps/youtube/app/settings/videoquality/VideoQualitySettingsActivity.java @@ -1,7 +1,9 @@ package com.google.android.apps.youtube.app.settings.videoquality; +import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; +import android.content.res.Resources; import android.os.Bundle; import android.util.TypedValue; import android.view.View; @@ -25,8 +27,8 @@ import app.revanced.extension.youtube.utils.ThemeUtils; @SuppressWarnings("deprecation") public class VideoQualitySettingsActivity extends Activity { - private static final String rvxSettingsLabel = ResourceUtils.getString("revanced_extended_settings_title"); - private static final String searchLabel = ResourceUtils.getString("revanced_extended_settings_search_title"); + private static String rvxSettingsLabel; + private static String searchLabel; private static WeakReference searchViewRef = new WeakReference<>(null); private static WeakReference closeButtonRef = new WeakReference<>(null); private ReVancedPreferenceFragment fragment; @@ -71,6 +73,10 @@ public class VideoQualitySettingsActivity extends Activity { return; } + // Set label + rvxSettingsLabel = getString("revanced_extended_settings_title"); + searchLabel = getString("revanced_extended_settings_search_title"); + // Set toolbar setToolbar(); @@ -85,6 +91,14 @@ public class VideoQualitySettingsActivity extends Activity { } } + @SuppressLint("DiscouragedApi") + private String getString(String str) { + Context baseContext = getBaseContext(); + Resources resources = baseContext.getResources(); + int identifier = resources.getIdentifier(str, "string", baseContext.getPackageName()); + return resources.getString(identifier); + } + private void filterPreferences(String query) { if (fragment == null) return; fragment.filterPreferences(query);