From bce9cfa39a252dacb392d01787c68b222fbbfa80 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 13 Dec 2018 05:53:39 -0500 Subject: [PATCH] Update LocaleManager --- .../com/topjohnwu/magisk/SplashActivity.java | 6 +-- .../magisk/components/BaseActivity.java | 3 +- core/build.gradle | 2 +- .../src/main/java/com/topjohnwu/core/App.java | 12 +++++ .../topjohnwu/core/utils/LocaleManager.java | 53 +++++++++++++++---- 5 files changed, 60 insertions(+), 16 deletions(-) diff --git a/app/src/full/java/com/topjohnwu/magisk/SplashActivity.java b/app/src/full/java/com/topjohnwu/magisk/SplashActivity.java index d06f61d89..fb8e7cac7 100644 --- a/app/src/full/java/com/topjohnwu/magisk/SplashActivity.java +++ b/app/src/full/java/com/topjohnwu/magisk/SplashActivity.java @@ -24,6 +24,9 @@ public class SplashActivity extends BaseActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + // Dynamic detect all locales + LocaleManager.loadAvailableLocales(R.string.app_changelog); + String pkg = app.mDB.getStrings(Const.Key.SU_MANAGER, null); if (pkg != null && getPackageName().equals(BuildConfig.APPLICATION_ID)) { app.mDB.setStrings(Const.Key.SU_MANAGER, null); @@ -47,9 +50,6 @@ public class SplashActivity extends BaseActivity { Data.importPrefs(); - // Dynamic detect all locales - LocaleManager.loadAvailableLocales(R.string.download_file_error); - // Create notification channel on Android O Notifications.setup(this); diff --git a/app/src/full/java/com/topjohnwu/magisk/components/BaseActivity.java b/app/src/full/java/com/topjohnwu/magisk/components/BaseActivity.java index 85ff2cc25..e8d5b0c76 100644 --- a/app/src/full/java/com/topjohnwu/magisk/components/BaseActivity.java +++ b/app/src/full/java/com/topjohnwu/magisk/components/BaseActivity.java @@ -31,7 +31,7 @@ public abstract class BaseActivity extends AppCompatActivity implements Topic.Au static int[] EMPTY_INT_ARRAY = new int[0]; private ActivityResultListener activityResultListener; - public App app; + public App app = App.self; @Override public int[] getSubscribedTopics() { @@ -49,7 +49,6 @@ public abstract class BaseActivity extends AppCompatActivity implements Topic.Au Configuration config = base.getResources().getConfiguration(); config.setLocale(LocaleManager.locale); applyOverrideConfiguration(config); - app = App.self; } @Override diff --git a/core/build.gradle b/core/build.gradle index cc266479a..e0ac986e7 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -5,7 +5,7 @@ android { buildToolsVersion rootProject.ext.buildToolsVersion defaultConfig { - minSdkVersion 16 + minSdkVersion 17 targetSdkVersion rootProject.ext.compileSdkVersion versionCode 1 versionName "1.0" diff --git a/core/src/main/java/com/topjohnwu/core/App.java b/core/src/main/java/com/topjohnwu/core/App.java index 35751db8d..d19d7e0e8 100644 --- a/core/src/main/java/com/topjohnwu/core/App.java +++ b/core/src/main/java/com/topjohnwu/core/App.java @@ -2,6 +2,7 @@ package com.topjohnwu.core; import android.content.SharedPreferences; import android.content.res.Configuration; +import android.content.res.Resources; import android.os.Handler; import android.os.Looper; import android.preference.PreferenceManager; @@ -24,6 +25,8 @@ public class App extends ContainerApp { public MagiskDB mDB; public RepoDatabaseHelper repoDB; + private Resources mResource; + public App() { self = this; } @@ -45,6 +48,15 @@ public class App extends ContainerApp { Data.loadConfig(); } + @Override + public Resources getResources() { + return mResource; + } + + public void setResources(Resources res) { + mResource = res; + } + @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); diff --git a/core/src/main/java/com/topjohnwu/core/utils/LocaleManager.java b/core/src/main/java/com/topjohnwu/core/utils/LocaleManager.java index 37d3332d8..471e32d46 100644 --- a/core/src/main/java/com/topjohnwu/core/utils/LocaleManager.java +++ b/core/src/main/java/com/topjohnwu/core/utils/LocaleManager.java @@ -1,11 +1,13 @@ package com.topjohnwu.core.utils; +import android.content.Context; import android.content.res.Configuration; import android.content.res.Resources; -import android.os.AsyncTask; +import android.os.Build; import com.topjohnwu.core.App; import com.topjohnwu.core.Const; +import com.topjohnwu.superuser.Shell; import java.util.ArrayList; import java.util.Collections; @@ -20,28 +22,59 @@ public class LocaleManager { public final static Locale defaultLocale = Locale.getDefault(); public static List locales; + public static Locale forLanguageTag(String tag) { + if (Build.VERSION.SDK_INT >= 21) { + return Locale.forLanguageTag(tag); + } else { + String[] tok = tag.split("-"); + if (tok.length == 0) { + return new Locale(""); + } + String language; + switch (tok[0]) { + case "und": + language = ""; // Undefined + break; + case "fil": + language = "tl"; // Filipino + break; + default: + language = tok[0]; + } + if ((language.length() != 2 && language.length() != 3)) + return new Locale(""); + if (tok.length == 1) + return new Locale(language); + String country = tok[1]; + if (country.length() != 2 && country.length() != 3) + return new Locale(language); + return new Locale(language, country); + } + } + public static void setLocale(App app) { String localeConfig = app.prefs.getString(Const.Key.LOCALE, ""); if (localeConfig.isEmpty()) { locale = defaultLocale; } else { - locale = Locale.forLanguageTag(localeConfig); + locale = forLanguageTag(localeConfig); } Locale.setDefault(locale); - Resources res = app.getResources(); - Configuration config = res.getConfiguration(); + app.setResources(getLocaleContext(locale).getResources()); + } + + public static Context getLocaleContext(Locale locale) { + Configuration config = new Configuration(App.self.getBaseContext().getResources().getConfiguration()); config.setLocale(locale); - res.updateConfiguration(config, res.getDisplayMetrics()); + return App.self.createConfigurationContext(config); } public static String getString(Locale locale, @StringRes int id) { - Configuration config = new Configuration(); - config.setLocale(locale); - return App.self.createConfigurationContext(config).getString(id); + return getLocaleContext(locale).getString(id); } public static void loadAvailableLocales(@StringRes int compareId) { - AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> { + Shell.EXECUTOR.execute(() -> { locales = new ArrayList<>(); HashSet set = new HashSet<>(); Resources res = App.self.getResources(); @@ -60,7 +93,7 @@ public class LocaleManager { // Other locales for (String s : res.getAssets().getLocales()) { - locale = Locale.forLanguageTag(s); + locale = forLanguageTag(s); if (set.add(getString(locale, compareId))) { locales.add(locale); }