Update LocaleManager

This commit is contained in:
topjohnwu 2018-12-13 05:53:39 -05:00
parent ff3d66a661
commit bce9cfa39a
5 changed files with 60 additions and 16 deletions

View File

@ -24,6 +24,9 @@ public class SplashActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// Dynamic detect all locales
LocaleManager.loadAvailableLocales(R.string.app_changelog);
String pkg = app.mDB.getStrings(Const.Key.SU_MANAGER, null); String pkg = app.mDB.getStrings(Const.Key.SU_MANAGER, null);
if (pkg != null && getPackageName().equals(BuildConfig.APPLICATION_ID)) { if (pkg != null && getPackageName().equals(BuildConfig.APPLICATION_ID)) {
app.mDB.setStrings(Const.Key.SU_MANAGER, null); app.mDB.setStrings(Const.Key.SU_MANAGER, null);
@ -47,9 +50,6 @@ public class SplashActivity extends BaseActivity {
Data.importPrefs(); Data.importPrefs();
// Dynamic detect all locales
LocaleManager.loadAvailableLocales(R.string.download_file_error);
// Create notification channel on Android O // Create notification channel on Android O
Notifications.setup(this); Notifications.setup(this);

View File

@ -31,7 +31,7 @@ public abstract class BaseActivity extends AppCompatActivity implements Topic.Au
static int[] EMPTY_INT_ARRAY = new int[0]; static int[] EMPTY_INT_ARRAY = new int[0];
private ActivityResultListener activityResultListener; private ActivityResultListener activityResultListener;
public App app; public App app = App.self;
@Override @Override
public int[] getSubscribedTopics() { public int[] getSubscribedTopics() {
@ -49,7 +49,6 @@ public abstract class BaseActivity extends AppCompatActivity implements Topic.Au
Configuration config = base.getResources().getConfiguration(); Configuration config = base.getResources().getConfiguration();
config.setLocale(LocaleManager.locale); config.setLocale(LocaleManager.locale);
applyOverrideConfiguration(config); applyOverrideConfiguration(config);
app = App.self;
} }
@Override @Override

View File

@ -5,7 +5,7 @@ android {
buildToolsVersion rootProject.ext.buildToolsVersion buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig { defaultConfig {
minSdkVersion 16 minSdkVersion 17
targetSdkVersion rootProject.ext.compileSdkVersion targetSdkVersion rootProject.ext.compileSdkVersion
versionCode 1 versionCode 1
versionName "1.0" versionName "1.0"

View File

@ -2,6 +2,7 @@ package com.topjohnwu.core;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
@ -24,6 +25,8 @@ public class App extends ContainerApp {
public MagiskDB mDB; public MagiskDB mDB;
public RepoDatabaseHelper repoDB; public RepoDatabaseHelper repoDB;
private Resources mResource;
public App() { public App() {
self = this; self = this;
} }
@ -45,6 +48,15 @@ public class App extends ContainerApp {
Data.loadConfig(); Data.loadConfig();
} }
@Override
public Resources getResources() {
return mResource;
}
public void setResources(Resources res) {
mResource = res;
}
@Override @Override
public void onConfigurationChanged(Configuration newConfig) { public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig); super.onConfigurationChanged(newConfig);

View File

@ -1,11 +1,13 @@
package com.topjohnwu.core.utils; package com.topjohnwu.core.utils;
import android.content.Context;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.AsyncTask; import android.os.Build;
import com.topjohnwu.core.App; import com.topjohnwu.core.App;
import com.topjohnwu.core.Const; import com.topjohnwu.core.Const;
import com.topjohnwu.superuser.Shell;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -20,28 +22,59 @@ public class LocaleManager {
public final static Locale defaultLocale = Locale.getDefault(); public final static Locale defaultLocale = Locale.getDefault();
public static List<Locale> locales; public static List<Locale> 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) { public static void setLocale(App app) {
String localeConfig = app.prefs.getString(Const.Key.LOCALE, ""); String localeConfig = app.prefs.getString(Const.Key.LOCALE, "");
if (localeConfig.isEmpty()) { if (localeConfig.isEmpty()) {
locale = defaultLocale; locale = defaultLocale;
} else { } else {
locale = Locale.forLanguageTag(localeConfig); locale = forLanguageTag(localeConfig);
} }
Locale.setDefault(locale); Locale.setDefault(locale);
Resources res = app.getResources(); app.setResources(getLocaleContext(locale).getResources());
Configuration config = res.getConfiguration(); }
public static Context getLocaleContext(Locale locale) {
Configuration config = new Configuration(App.self.getBaseContext().getResources().getConfiguration());
config.setLocale(locale); config.setLocale(locale);
res.updateConfiguration(config, res.getDisplayMetrics()); return App.self.createConfigurationContext(config);
} }
public static String getString(Locale locale, @StringRes int id) { public static String getString(Locale locale, @StringRes int id) {
Configuration config = new Configuration(); return getLocaleContext(locale).getString(id);
config.setLocale(locale);
return App.self.createConfigurationContext(config).getString(id);
} }
public static void loadAvailableLocales(@StringRes int compareId) { public static void loadAvailableLocales(@StringRes int compareId) {
AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> { Shell.EXECUTOR.execute(() -> {
locales = new ArrayList<>(); locales = new ArrayList<>();
HashSet<String> set = new HashSet<>(); HashSet<String> set = new HashSet<>();
Resources res = App.self.getResources(); Resources res = App.self.getResources();
@ -60,7 +93,7 @@ public class LocaleManager {
// Other locales // Other locales
for (String s : res.getAssets().getLocales()) { for (String s : res.getAssets().getLocales()) {
locale = Locale.forLanguageTag(s); locale = forLanguageTag(s);
if (set.add(getString(locale, compareId))) { if (set.add(getString(locale, compareId))) {
locales.add(locale); locales.add(locale);
} }