diff --git a/app/src/full/java/com/topjohnwu/magisk/MagiskManager.java b/app/src/full/java/com/topjohnwu/magisk/MagiskManager.java index 3b1d30b3b..db25accaa 100644 --- a/app/src/full/java/com/topjohnwu/magisk/MagiskManager.java +++ b/app/src/full/java/com/topjohnwu/magisk/MagiskManager.java @@ -2,8 +2,6 @@ package com.topjohnwu.magisk; import android.content.SharedPreferences; import android.content.pm.PackageManager; -import android.content.res.Configuration; -import android.content.res.Resources; import android.preference.PreferenceManager; import android.text.TextUtils; @@ -11,21 +9,18 @@ import com.topjohnwu.magisk.container.Module; import com.topjohnwu.magisk.database.MagiskDatabaseHelper; import com.topjohnwu.magisk.database.RepoDatabaseHelper; import com.topjohnwu.magisk.utils.Const; +import com.topjohnwu.magisk.utils.LocaleManager; import com.topjohnwu.magisk.utils.RootUtils; import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.superuser.ContainerApp; import com.topjohnwu.superuser.Shell; import java.lang.ref.WeakReference; -import java.util.List; import java.util.Locale; import java.util.Map; public class MagiskManager extends ContainerApp { - public static Locale locale; - public static Locale defaultLocale; - // Topics public final Topic magiskHideDone = new Topic(); public final Topic reloadActivity = new Topic(); @@ -40,7 +35,6 @@ public class MagiskManager extends ContainerApp { // Data public Map moduleMap; - public List locales; // Global resources public SharedPreferences prefs; @@ -61,7 +55,7 @@ public class MagiskManager extends ContainerApp { prefs = PreferenceManager.getDefaultSharedPreferences(this); mDB = MagiskDatabaseHelper.getInstance(this); - locale = defaultLocale = Locale.getDefault(); + LocaleManager.locale = LocaleManager.defaultLocale = Locale.getDefault(); String pkg = mDB.getStrings(Const.Key.SU_MANAGER, null); if (pkg != null && getPackageName().equals(Const.ORIG_PKG_NAME)) { @@ -76,21 +70,8 @@ public class MagiskManager extends ContainerApp { } catch (PackageManager.NameNotFoundException ignored) {} } - setLocale(); + LocaleManager.setLocale(); Global.loadConfig(); } - public void setLocale() { - Global.localeConfig = prefs.getString(Const.Key.LOCALE, ""); - if (Global.localeConfig.isEmpty()) { - locale = defaultLocale; - } else { - locale = Locale.forLanguageTag(Global.localeConfig); - } - Resources res = getBaseContext().getResources(); - Configuration config = new Configuration(res.getConfiguration()); - config.setLocale(locale); - res.updateConfiguration(config, res.getDisplayMetrics()); - } - } diff --git a/app/src/full/java/com/topjohnwu/magisk/SettingsActivity.java b/app/src/full/java/com/topjohnwu/magisk/SettingsActivity.java index 373422595..3c95446d4 100644 --- a/app/src/full/java/com/topjohnwu/magisk/SettingsActivity.java +++ b/app/src/full/java/com/topjohnwu/magisk/SettingsActivity.java @@ -27,6 +27,7 @@ import com.topjohnwu.magisk.receivers.DownloadReceiver; import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Download; import com.topjohnwu.magisk.utils.FingerprintHelper; +import com.topjohnwu.magisk.utils.LocaleManager; import com.topjohnwu.magisk.utils.RootUtils; import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Utils; @@ -212,18 +213,18 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { } private void setLocalePreference(ListPreference lp) { - CharSequence[] entries = new CharSequence[mm.locales.size() + 1]; - CharSequence[] entryValues = new CharSequence[mm.locales.size() + 1]; - entries[0] = Utils.getLocaleString(MagiskManager.defaultLocale, R.string.system_default); + CharSequence[] entries = new CharSequence[LocaleManager.locales.size() + 1]; + CharSequence[] entryValues = new CharSequence[LocaleManager.locales.size() + 1]; + entries[0] = LocaleManager.getString(LocaleManager.defaultLocale, R.string.system_default); entryValues[0] = ""; int i = 1; - for (Locale locale : mm.locales) { + for (Locale locale : LocaleManager.locales) { entries[i] = locale.getDisplayName(locale); entryValues[i++] = locale.toLanguageTag(); } lp.setEntries(entries); lp.setEntryValues(entryValues); - lp.setSummary(MagiskManager.locale.getDisplayName(MagiskManager.locale)); + lp.setSummary(LocaleManager.locale.getDisplayName(LocaleManager.locale)); } @Override @@ -282,7 +283,7 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { mm.mDB.setSettings(key, Utils.getPrefsInt(prefs, key)); break; case Const.Key.LOCALE: - mm.setLocale(); + LocaleManager.setLocale(); mm.reloadActivity.publish(false); break; case Const.Key.UPDATE_CHANNEL: diff --git a/app/src/full/java/com/topjohnwu/magisk/SplashActivity.java b/app/src/full/java/com/topjohnwu/magisk/SplashActivity.java index cade7ec8e..25108928f 100644 --- a/app/src/full/java/com/topjohnwu/magisk/SplashActivity.java +++ b/app/src/full/java/com/topjohnwu/magisk/SplashActivity.java @@ -8,13 +8,13 @@ import android.os.Bundle; import com.topjohnwu.magisk.asyncs.CheckUpdates; import com.topjohnwu.magisk.asyncs.LoadModules; -import com.topjohnwu.magisk.asyncs.ParallelTask; import com.topjohnwu.magisk.asyncs.UpdateRepos; import com.topjohnwu.magisk.components.Activity; import com.topjohnwu.magisk.database.RepoDatabaseHelper; import com.topjohnwu.magisk.receivers.ShortcutReceiver; import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Download; +import com.topjohnwu.magisk.utils.LocaleManager; import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.superuser.Shell; @@ -32,7 +32,7 @@ public class SplashActivity extends Activity { Global.importPrefs(); // Dynamic detect all locales - new LoadLocale().exec(); + new LocaleManager.LoadLocale().exec(); // Create notification channel on Android O if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { @@ -72,16 +72,4 @@ public class SplashActivity extends Activity { startActivity(intent); finish(); } - - static class LoadLocale extends ParallelTask { - @Override - protected Void doInBackground(Void... voids) { - Global.MM().locales = Utils.getAvailableLocale(); - return null; - } - @Override - protected void onPostExecute(Void aVoid) { - Global.MM().localeDone.publish(); - } - } } diff --git a/app/src/full/java/com/topjohnwu/magisk/components/FlavorActivity.java b/app/src/full/java/com/topjohnwu/magisk/components/FlavorActivity.java index f577f3e3d..9454f2d3a 100644 --- a/app/src/full/java/com/topjohnwu/magisk/components/FlavorActivity.java +++ b/app/src/full/java/com/topjohnwu/magisk/components/FlavorActivity.java @@ -11,6 +11,7 @@ import android.view.WindowManager; import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; +import com.topjohnwu.magisk.utils.LocaleManager; import com.topjohnwu.magisk.utils.Topic; public abstract class FlavorActivity extends AppCompatActivity { @@ -20,7 +21,7 @@ public abstract class FlavorActivity extends AppCompatActivity { public FlavorActivity() { super(); Configuration configuration = new Configuration(); - configuration.setLocale(MagiskManager.locale); + configuration.setLocale(LocaleManager.locale); applyOverrideConfiguration(configuration); } diff --git a/app/src/full/java/com/topjohnwu/magisk/container/Repo.java b/app/src/full/java/com/topjohnwu/magisk/container/Repo.java index a422983a6..307303bf5 100644 --- a/app/src/full/java/com/topjohnwu/magisk/container/Repo.java +++ b/app/src/full/java/com/topjohnwu/magisk/container/Repo.java @@ -3,8 +3,8 @@ package com.topjohnwu.magisk.container; import android.content.ContentValues; import android.database.Cursor; -import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.utils.Const; +import com.topjohnwu.magisk.utils.LocaleManager; import com.topjohnwu.magisk.utils.Logger; import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.WebService; @@ -77,7 +77,7 @@ public class Repo extends BaseModule { public String getLastUpdateString() { return DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, - MagiskManager.locale).format(mLastUpdate); + LocaleManager.locale).format(mLastUpdate); } public Date getLastUpdate() { diff --git a/app/src/full/java/com/topjohnwu/magisk/container/SuLogEntry.java b/app/src/full/java/com/topjohnwu/magisk/container/SuLogEntry.java index 4b3518468..4fd905aea 100644 --- a/app/src/full/java/com/topjohnwu/magisk/container/SuLogEntry.java +++ b/app/src/full/java/com/topjohnwu/magisk/container/SuLogEntry.java @@ -3,7 +3,7 @@ package com.topjohnwu.magisk.container; import android.content.ContentValues; import android.database.Cursor; -import com.topjohnwu.magisk.MagiskManager; +import com.topjohnwu.magisk.utils.LocaleManager; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -47,10 +47,10 @@ public class SuLogEntry { } public String getDateString() { - return DateFormat.getDateInstance(DateFormat.MEDIUM, MagiskManager.locale).format(date); + return DateFormat.getDateInstance(DateFormat.MEDIUM, LocaleManager.locale).format(date); } public String getTimeString() { - return new SimpleDateFormat("h:mm a", MagiskManager.locale).format(date); + return new SimpleDateFormat("h:mm a", LocaleManager.locale).format(date); } } diff --git a/app/src/full/java/com/topjohnwu/magisk/database/MagiskDatabaseHelper.java b/app/src/full/java/com/topjohnwu/magisk/database/MagiskDatabaseHelper.java index bcd3f5d2f..6c75b3ce0 100644 --- a/app/src/full/java/com/topjohnwu/magisk/database/MagiskDatabaseHelper.java +++ b/app/src/full/java/com/topjohnwu/magisk/database/MagiskDatabaseHelper.java @@ -17,6 +17,7 @@ import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.container.Policy; import com.topjohnwu.magisk.container.SuLogEntry; import com.topjohnwu.magisk.utils.Const; +import com.topjohnwu.magisk.utils.LocaleManager; import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.io.SuFile; @@ -236,7 +237,7 @@ public class MagiskDatabaseHelper { String dateString = null, newString; while (c.moveToNext()) { Date date = new Date(c.getLong(c.getColumnIndex("time"))); - newString = DateFormat.getDateInstance(DateFormat.MEDIUM, MagiskManager.locale).format(date); + newString = DateFormat.getDateInstance(DateFormat.MEDIUM, LocaleManager.locale).format(date); if (!TextUtils.equals(dateString, newString)) { dateString = newString; list = new ArrayList<>(); diff --git a/app/src/full/java/com/topjohnwu/magisk/utils/LocaleManager.java b/app/src/full/java/com/topjohnwu/magisk/utils/LocaleManager.java new file mode 100644 index 000000000..658c05f06 --- /dev/null +++ b/app/src/full/java/com/topjohnwu/magisk/utils/LocaleManager.java @@ -0,0 +1,92 @@ +package com.topjohnwu.magisk.utils; + +import android.content.res.Configuration; +import android.content.res.Resources; +import android.support.annotation.StringRes; + +import com.topjohnwu.magisk.Global; +import com.topjohnwu.magisk.MagiskManager; +import com.topjohnwu.magisk.R; +import com.topjohnwu.magisk.asyncs.ParallelTask; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; + +public class LocaleManager { + public static Locale locale; + public static Locale defaultLocale; + public static List locales; + + public static void setLocale() { + MagiskManager mm = Global.MM(); + Global.localeConfig = mm.prefs.getString(Const.Key.LOCALE, ""); + if (Global.localeConfig.isEmpty()) { + locale = defaultLocale; + } else { + locale = Locale.forLanguageTag(Global.localeConfig); + } + Resources res = mm.getResources(); + Configuration config = new Configuration(res.getConfiguration()); + config.setLocale(locale); + res.updateConfiguration(config, res.getDisplayMetrics()); + } + + public static String getString(Locale locale, @StringRes int id) { + Configuration config = Global.MM().getResources().getConfiguration(); + config.setLocale(locale); + return getString(config, id); + } + + private static String getString(Configuration config, @StringRes int id) { + return Global.MM().createConfigurationContext(config).getString(id); + } + + private static List getAvailableLocale() { + List locales = new ArrayList<>(); + HashSet set = new HashSet<>(); + MagiskManager mm = Global.MM(); + Locale locale; + + @StringRes int compareId = R.string.download_file_error; + + // Add default locale + locales.add(Locale.ENGLISH); + set.add(getString(Locale.ENGLISH, compareId)); + + // Add some special locales + locales.add(Locale.TAIWAN); + set.add(getString(Locale.TAIWAN, compareId)); + locale = new Locale("pt", "BR"); + locales.add(locale); + set.add(getString(locale, compareId)); + + // Other locales + Configuration config = mm.getResources().getConfiguration(); + for (String s : mm.getAssets().getLocales()) { + locale = Locale.forLanguageTag(s); + config.setLocale(locale); + if (set.add(getString(config, compareId))) { + locales.add(locale); + } + } + + Collections.sort(locales, (a, b) -> a.getDisplayName(a).compareTo(b.getDisplayName(b))); + + return locales; + } + + public static class LoadLocale extends ParallelTask { + @Override + protected Void doInBackground(Void... voids) { + locales = getAvailableLocale(); + return null; + } + @Override + protected void onPostExecute(Void aVoid) { + Global.MM().localeDone.publish(); + } + } +} diff --git a/app/src/full/java/com/topjohnwu/magisk/utils/Utils.java b/app/src/full/java/com/topjohnwu/magisk/utils/Utils.java index 5b568ba5c..eeaca8e99 100644 --- a/app/src/full/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/full/java/com/topjohnwu/magisk/utils/Utils.java @@ -5,21 +5,14 @@ import android.app.job.JobScheduler; import android.content.ComponentName; import android.content.Context; import android.content.SharedPreferences; -import android.content.res.Configuration; import android.database.Cursor; import android.net.Uri; import android.provider.OpenableColumns; -import android.support.annotation.StringRes; import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.MagiskManager; -import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.services.UpdateCheckService; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; import java.util.Locale; public class Utils { @@ -54,45 +47,6 @@ public class Utils { return name; } - public static String getLocaleString(Locale locale, @StringRes int id) { - Context context = Global.MM(); - Configuration config = context.getResources().getConfiguration(); - config.setLocale(locale); - Context localizedContext = context.createConfigurationContext(config); - return localizedContext.getString(id); - } - - public static List getAvailableLocale() { - List locales = new ArrayList<>(); - HashSet set = new HashSet<>(); - Locale locale; - - @StringRes int compareId = R.string.download_file_error; - - // Add default locale - locales.add(Locale.ENGLISH); - set.add(getLocaleString(Locale.ENGLISH, compareId)); - - // Add some special locales - locales.add(Locale.TAIWAN); - set.add(getLocaleString(Locale.TAIWAN, compareId)); - locale = new Locale("pt", "BR"); - locales.add(locale); - set.add(getLocaleString(locale, compareId)); - - // Other locales - for (String s : Global.MM().getAssets().getLocales()) { - locale = Locale.forLanguageTag(s); - if (set.add(getLocaleString(locale, compareId))) { - locales.add(locale); - } - } - - Collections.sort(locales, (l1, l2) -> l1.getDisplayName(l1).compareTo(l2.getDisplayName(l2))); - - return locales; - } - public static int dpInPx(int dp) { Context context = Global.MM(); float scale = context.getResources().getDisplayMetrics().density;