Improve settings

This commit is contained in:
topjohnwu 2017-01-11 19:10:30 +08:00
parent 61e2c3444a
commit 01d847ae4e
5 changed files with 99 additions and 77 deletions

View File

@ -35,6 +35,8 @@ public class MainActivity extends AppCompatActivity
private static final String SELECTED_ITEM_ID = "SELECTED_ITEM_ID"; private static final String SELECTED_ITEM_ID = "SELECTED_ITEM_ID";
public static CallbackHandler.Event recreate = new CallbackHandler.Event();
private final Handler mDrawerHandler = new Handler(); private final Handler mDrawerHandler = new Handler();
private SharedPreferences prefs; private SharedPreferences prefs;
@ -90,23 +92,29 @@ public class MainActivity extends AppCompatActivity
} }
navigationView.setNavigationItemSelectedListener(this); navigationView.setNavigationItemSelectedListener(this);
if (StatusFragment.updateCheckDone.isTriggered) {
onTrigger(StatusFragment.updateCheckDone);
}
} }
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
CallbackHandler.register(StatusFragment.updateCheckDone, this); CallbackHandler.register(StatusFragment.updateCheckDone, this);
CallbackHandler.register(recreate, this);
if (StatusFragment.updateCheckDone.isTriggered) {
onTrigger(StatusFragment.updateCheckDone);
}
checkHideSection(); checkHideSection();
} }
@Override
protected void onPause() {
super.onPause();
CallbackHandler.unRegister(StatusFragment.updateCheckDone, this);
}
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
CallbackHandler.unRegister(StatusFragment.updateCheckDone, this); CallbackHandler.unRegister(recreate, this);
} }
@Override @Override
@ -135,18 +143,22 @@ public class MainActivity extends AppCompatActivity
@Override @Override
public void onTrigger(CallbackHandler.Event event) { public void onTrigger(CallbackHandler.Event event) {
Menu menu = navigationView.getMenu(); if (event == StatusFragment.updateCheckDone) {
menu.findItem(R.id.install).setVisible(StatusFragment.remoteMagiskVersion > 0 && Menu menu = navigationView.getMenu();
Shell.rootAccess()); menu.findItem(R.id.install).setVisible(StatusFragment.remoteMagiskVersion > 0 &&
Shell.rootAccess());
} else if (event == recreate) {
recreate();
}
} }
private void checkHideSection() { private void checkHideSection() {
Menu menu = navigationView.getMenu(); Menu menu = navigationView.getMenu();
menu.findItem(R.id.magiskhide).setVisible(StatusFragment.magiskVersion > 0 && menu.findItem(R.id.magiskhide).setVisible(StatusFragment.magiskVersion >= 8 &&
prefs.getBoolean("magiskhide", false) && Shell.rootAccess()); prefs.getBoolean("magiskhide", false) && Shell.rootAccess());
menu.findItem(R.id.modules).setVisible(StatusFragment.magiskVersion > 0 && menu.findItem(R.id.modules).setVisible(StatusFragment.magiskVersion >= 4 &&
Shell.rootAccess()); Shell.rootAccess());
menu.findItem(R.id.downloads).setVisible(StatusFragment.magiskVersion > 0 && menu.findItem(R.id.downloads).setVisible(StatusFragment.magiskVersion >= 4 &&
Shell.rootAccess()); Shell.rootAccess());
menu.findItem(R.id.log).setVisible(Shell.rootAccess()); menu.findItem(R.id.log).setVisible(Shell.rootAccess());
menu.findItem(R.id.install).setVisible(Shell.rootAccess()); menu.findItem(R.id.install).setVisible(Shell.rootAccess());

View File

@ -1,10 +1,11 @@
package com.topjohnwu.magisk; package com.topjohnwu.magisk;
import android.content.Intent; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.CheckBoxPreference; import android.preference.CheckBoxPreference;
import android.preference.ListPreference; import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
@ -15,6 +16,7 @@ import android.widget.Toast;
import com.topjohnwu.magisk.utils.Async; import com.topjohnwu.magisk.utils.Async;
import com.topjohnwu.magisk.utils.Logger; import com.topjohnwu.magisk.utils.Logger;
import com.topjohnwu.magisk.utils.ModuleHelper;
import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
@ -86,14 +88,34 @@ public class SettingsActivity extends AppCompatActivity {
CheckBoxPreference busyboxPreference = (CheckBoxPreference) findPreference("busybox"); CheckBoxPreference busyboxPreference = (CheckBoxPreference) findPreference("busybox");
CheckBoxPreference magiskhidePreference = (CheckBoxPreference) findPreference("magiskhide"); CheckBoxPreference magiskhidePreference = (CheckBoxPreference) findPreference("magiskhide");
CheckBoxPreference hostsPreference = (CheckBoxPreference) findPreference("hosts"); CheckBoxPreference hostsPreference = (CheckBoxPreference) findPreference("hosts");
Preference clear = findPreference("clear");
themePreference.setSummary(themePreference.getValue()); clear.setOnPreferenceClickListener((pref) -> {
SharedPreferences repoMap = getActivity().getSharedPreferences(ModuleHelper.FILE_KEY, Context.MODE_PRIVATE);
repoMap.edit()
.putString(ModuleHelper.ETAG_KEY, "")
.putInt(ModuleHelper.VERSION_KEY, 0)
.apply();
new Async.LoadRepos(getActivity()).exec();
Toast.makeText(getActivity(), R.string.repo_cache_cleared, Toast.LENGTH_LONG).show();
return true;
});
if (Utils.isDarkTheme) {
themePreference.setSummary(R.string.theme_dark);
} else {
themePreference.setSummary(R.string.theme_default);
}
if (StatusFragment.magiskVersion < 9) { if (StatusFragment.magiskVersion < 9) {
hostsPreference.setEnabled(false); hostsPreference.setEnabled(false);
busyboxPreference.setEnabled(false); busyboxPreference.setEnabled(false);
} else if (StatusFragment.magiskVersion < 8) { } else if (StatusFragment.magiskVersion < 8) {
magiskhidePreference.setEnabled(false); magiskhidePreference.setEnabled(false);
} else if (! Shell.rootAccess()) {
busyboxPreference.setEnabled(false);
magiskhidePreference.setEnabled(false);
hostsPreference.setEnabled(false);
} else { } else {
busyboxPreference.setEnabled(true); busyboxPreference.setEnabled(true);
magiskhidePreference.setEnabled(true); magiskhidePreference.setEnabled(true);
@ -121,81 +143,61 @@ public class SettingsActivity extends AppCompatActivity {
switch (key) { switch (key) {
case "theme": case "theme":
String theme = prefs.getString("theme", getString(R.string.theme_default_value)); String theme = prefs.getString("theme", getString(R.string.theme_default_value));
Utils.isDarkTheme = theme.equalsIgnoreCase(getString(R.string.theme_dark_value)); if (Utils.isDarkTheme != theme.equalsIgnoreCase(getString(R.string.theme_dark_value))) {
Utils.isDarkTheme = !Utils.isDarkTheme;
themePreference.setSummary(theme); getActivity().recreate();
if (Utils.isDarkTheme) { MainActivity.recreate.trigger();
getActivity().getApplication().setTheme(R.style.AppTheme_dh);
} else {
getActivity().getApplication().setTheme(R.style.AppTheme);
} }
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
break; break;
case "magiskhide": case "magiskhide":
checked = prefs.getBoolean("magiskhide", false); checked = prefs.getBoolean("magiskhide", false);
if (checked) { new Async.RootTask<Void, Void, Void>() {
new Async.RootTask<Void, Void, Void>() { private boolean enable = checked;
@Override @Override
protected Void doInBackground(Void... params) { protected Void doInBackground(Void... params) {
if (enable) {
Utils.createFile("/magisk/.core/magiskhide/enable"); Utils.createFile("/magisk/.core/magiskhide/enable");
return null; } else {
}
}.exec();
} else {
new Async.RootTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
Utils.removeItem("/magisk/.core/magiskhide/enable"); Utils.removeItem("/magisk/.core/magiskhide/enable");
return null;
} }
}.exec();
} return null;
}
}.exec();
Toast.makeText(getActivity(), R.string.settings_reboot_toast, Toast.LENGTH_LONG).show(); Toast.makeText(getActivity(), R.string.settings_reboot_toast, Toast.LENGTH_LONG).show();
break; break;
case "busybox": case "busybox":
checked = prefs.getBoolean("busybox", false); checked = prefs.getBoolean("busybox", false);
if (checked) { new Async.RootTask<Void, Void, Void>() {
new Async.RootTask<Void, Void, Void>() { private boolean enable = checked;
@Override @Override
protected Void doInBackground(Void... params) { protected Void doInBackground(Void... params) {
if (enable) {
Utils.createFile("/magisk/.core/busybox/enable"); Utils.createFile("/magisk/.core/busybox/enable");
return null; } else {
}
}.exec();
} else {
new Async.RootTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
Utils.removeItem("/magisk/.core/busybox/enable"); Utils.removeItem("/magisk/.core/busybox/enable");
return null;
} }
}.exec(); return null;
} }
}.exec();
Toast.makeText(getActivity(), R.string.settings_reboot_toast, Toast.LENGTH_LONG).show(); Toast.makeText(getActivity(), R.string.settings_reboot_toast, Toast.LENGTH_LONG).show();
break; break;
case "hosts": case "hosts":
checked = prefs.getBoolean("hosts", false); checked = prefs.getBoolean("hosts", false);
if (checked) { new Async.RootTask<Void, Void, Void>() {
new Async.RootTask<Void, Void, Void>() { private boolean enable = checked;
@Override @Override
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
Shell.su("cp -af /system/etc/hosts /magisk/.core/hosts"); if (enable) {
return null; Shell.su("cp -af /system/etc/hosts /magisk/.core/hosts",
"mount -o bind /magisk/.core/hosts /system/etc/hosts");
} else {
Shell.su("umount -l /system/etc/hosts",
"rm -f /magisk/.core/hosts");
} }
}.exec(); return null;
} else { }
new Async.RootTask<Void, Void, Void>() { }.exec();
@Override
protected Void doInBackground(Void... voids) {
Shell.su("umount -l /system/etc/hosts", "rm -f /magisk/.core/hosts");
return null;
}
}.exec();
}
Toast.makeText(getActivity(), R.string.settings_reboot_toast, Toast.LENGTH_LONG).show();
break; break;
case "developer_logging": case "developer_logging":
Logger.devLog = prefs.getBoolean("developer_logging", false); Logger.devLog = prefs.getBoolean("developer_logging", false);

View File

@ -28,10 +28,10 @@ import java.util.Map;
public class ModuleHelper { public class ModuleHelper {
private static final String MAGISK_PATH = "/magisk"; private static final String MAGISK_PATH = "/magisk";
private static final String FILE_KEY = "RepoMap"; public static final String FILE_KEY = "RepoMap";
private static final String REPO_KEY = "repomap"; private static final String REPO_KEY = "repomap";
private static final String VERSION_KEY = "version"; public static final String VERSION_KEY = "version";
private static final String ETAG_KEY = "ETag"; public static final String ETAG_KEY = "ETag";
private static final int DATABASE_VER = 1; private static final int DATABASE_VER = 1;
private static ValueSortedMap<String, Repo> repoMap = new ValueSortedMap<>(); private static ValueSortedMap<String, Repo> repoMap = new ValueSortedMap<>();

View File

@ -108,7 +108,9 @@
<string name="downloading_toast">Downloading %1$s</string> <string name="downloading_toast">Downloading %1$s</string>
<string name="magisk_update_title">New Magisk Update Available!</string> <string name="magisk_update_title">New Magisk Update Available!</string>
<string name="magisk_update_message">Magisk v%1$.1f update is live, do you want to install?</string> <string name="magisk_update_message">Magisk v%1$.1f update is live, do you want to install?</string>
<string name="settings_reboot_toast">Reboot to apply settings</string>
<string name="check_release_notes">Check release notes</string> <string name="check_release_notes">Check release notes</string>
<string name="repo_cache_cleared">Repo cache cleared</string>
<!--URL Templates--> <!--URL Templates-->
<string name="url_main" translatable="false">https://api.github.com/orgs/Magisk-Modules-Repo/repos</string> <string name="url_main" translatable="false">https://api.github.com/orgs/Magisk-Modules-Repo/repos</string>
@ -121,6 +123,8 @@
<string name="settings_theme_summary">Select a theme</string> <string name="settings_theme_summary">Select a theme</string>
<string name="theme_default">Default</string> <string name="theme_default">Default</string>
<string name="theme_dark">Dark</string> <string name="theme_dark">Dark</string>
<string name="settings_clear_cache_title">Clear Repo Cache</string>
<string name="settings_clear_cache_summary">Clear the cached information for online repos, forces the app to refresh online</string>
<string name="settings_magiskhide_title">Enable Magisk Hide</string> <string name="settings_magiskhide_title">Enable Magisk Hide</string>
<string name="settings_magiskhide_summary">Hide Magisk from various detections</string> <string name="settings_magiskhide_summary">Hide Magisk from various detections</string>
@ -131,14 +135,13 @@
<string name="settings_development_category">Development</string> <string name="settings_development_category">Development</string>
<string name="settings_developer_logging_title">Enable advanced debug logging</string> <string name="settings_developer_logging_title">Enable advanced debug logging</string>
<string name="settings_developer_logging_summary">Check this to enable more verbose logging.</string> <string name="settings_developer_logging_summary">Check this to enable verbose logging</string>
<string name="settings_shell_logging_title">Enable shell command debug logging</string> <string name="settings_shell_logging_title">Enable shell command debug logging</string>
<string name="settings_shell_logging_summary">Check this to enable logging all shell commands and output</string> <string name="settings_shell_logging_summary">Check this to enable logging all shell commands and its output</string>
<string name="settings_reboot_toast">Reboot to apply settings</string>
<!-- Themes --> <!-- Themes -->
<string name="theme_default_value" translatable="false">default</string> <string name="theme_default_value" translatable="false">default</string>
<string name="theme_dark_value" translatable="false">dark</string> <string name="theme_dark_value" translatable="false">dark</string>
</resources> </resources>

View File

@ -12,6 +12,11 @@
android:entries="@array/themes" android:entries="@array/themes"
android:entryValues="@array/themes_values"/> android:entryValues="@array/themes_values"/>
<Preference
android:key="clear"
android:title="@string/settings_clear_cache_title"
android:summary="@string/settings_clear_cache_summary" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory