From 53477f0f59c9ad0f112159565ddac82db4ac11e4 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 31 Jul 2017 00:44:38 +0800 Subject: [PATCH] Improve locale settings --- .../com/topjohnwu/magisk/MagiskManager.java | 2 +- .../com/topjohnwu/magisk/MainActivity.java | 2 +- .../topjohnwu/magisk/SettingsActivity.java | 34 +++++++++++-------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java index a94968730..c75093cbc 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java @@ -44,7 +44,7 @@ public class MagiskManager extends Application { // Events public final CallbackEvent magiskHideDone = new CallbackEvent(); - public final CallbackEvent reloadMainActivity = new CallbackEvent(); + public final CallbackEvent reloadActivity = new CallbackEvent(); public final CallbackEvent moduleLoadDone = new CallbackEvent(); public final CallbackEvent repoLoadDone = new CallbackEvent(); public final CallbackEvent updateCheckDone = new CallbackEvent(); diff --git a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java index 9fc19df3f..7c610ee89 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java @@ -116,7 +116,7 @@ public class MainActivity extends Activity @Override public CallbackEvent[] getRegisterEvents() { - return new CallbackEvent[] { getApplicationContext().reloadMainActivity }; + return new CallbackEvent[] { getApplicationContext().reloadActivity }; } public void checkHideSection() { diff --git a/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java b/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java index 8016b7539..0c2f99f29 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java @@ -25,7 +25,7 @@ import java.util.Locale; import butterknife.BindView; import butterknife.ButterKnife; -public class SettingsActivity extends Activity { +public class SettingsActivity extends Activity implements CallbackEvent.Listener { @BindView(R.id.toolbar) Toolbar toolbar; @@ -57,6 +57,16 @@ public class SettingsActivity extends Activity { } + @Override + public void onTrigger(CallbackEvent event) { + recreate(); + } + + @Override + public CallbackEvent[] getRegisterEvents() { + return new CallbackEvent[] { getApplicationContext().reloadActivity }; + } + public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener, CallbackEvent.Listener { @@ -124,10 +134,10 @@ public class SettingsActivity extends Activity { } } - private ListPreference setLocalePreference(ListPreference lp) { - if (lp == null) { + private void setLocalePreference(ListPreference lp) { + boolean isNew = lp == null; + if (isNew) { lp = new ListPreference(getActivity()); - generalCatagory.addPreference(lp); } CharSequence[] entries = new CharSequence[magiskManager.locales.size() + 1]; CharSequence[] entryValues = new CharSequence[magiskManager.locales.size() + 1]; @@ -143,7 +153,9 @@ public class SettingsActivity extends Activity { lp.setTitle(R.string.language); lp.setKey("locale"); lp.setSummary(MagiskManager.locale.getDisplayName(MagiskManager.locale)); - return lp; + if (isNew) { + generalCatagory.addPreference(lp); + } } @Override @@ -170,8 +182,7 @@ public class SettingsActivity extends Activity { enabled = prefs.getBoolean("dark_theme", false); if (magiskManager.isDarkTheme != enabled) { magiskManager.isDarkTheme = enabled; - magiskManager.reloadMainActivity.trigger(false); - getActivity().recreate(); + magiskManager.reloadActivity.trigger(false); } break; case "disable": @@ -232,8 +243,7 @@ public class SettingsActivity extends Activity { break; case "locale": magiskManager.setLocale(); - magiskManager.reloadMainActivity.trigger(false); - getActivity().recreate(); + magiskManager.reloadActivity.trigger(false); break; } setSummary(); @@ -256,11 +266,7 @@ public class SettingsActivity extends Activity { @Override public void onTrigger(CallbackEvent event) { - ListPreference language = setLocalePreference((ListPreference) findPreference("locale")); - language.setOnPreferenceClickListener((pref) -> { - setLocalePreference((ListPreference) pref); - return false; - }); + setLocalePreference((ListPreference) findPreference("locale")); } @Override