Magisk/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java

219 lines
9.0 KiB
Java
Raw Normal View History

package com.topjohnwu.magisk;
import android.content.SharedPreferences;
import android.os.Bundle;
2017-01-28 14:02:33 +00:00
import android.preference.ListPreference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
2017-01-27 22:13:07 +00:00
import android.preference.PreferenceScreen;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
2016-11-08 21:17:50 +00:00
import android.widget.Toast;
2017-02-12 11:49:46 +00:00
import com.topjohnwu.magisk.asyncs.MagiskHide;
2017-02-06 18:01:32 +00:00
import com.topjohnwu.magisk.components.Activity;
2017-02-14 21:24:02 +00:00
import com.topjohnwu.magisk.components.AlertDialogBuilder;
2017-05-31 19:18:41 +00:00
import com.topjohnwu.magisk.database.SuDatabaseHelper;
import com.topjohnwu.magisk.utils.Logger;
2016-11-08 21:17:50 +00:00
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Utils;
import butterknife.BindView;
import butterknife.ButterKnife;
2017-02-06 18:01:32 +00:00
public class SettingsActivity extends Activity {
@BindView(R.id.toolbar) Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2017-02-17 00:51:51 +00:00
if (getApplicationContext().isDarkTheme) {
2017-05-26 10:20:53 +00:00
setTheme(R.style.AppTheme_Transparent_Dark);
}
2017-05-26 10:20:53 +00:00
setContentView(R.layout.activity_settings);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(view -> finish());
ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setTitle(R.string.settings);
ab.setDisplayHomeAsUpEnabled(true);
}
setFloating();
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new SettingsFragment()).commit();
}
}
2016-12-11 12:38:15 +00:00
public static class SettingsFragment extends PreferenceFragment
implements SharedPreferences.OnSharedPreferenceChangeListener {
2016-12-11 12:38:15 +00:00
private SharedPreferences prefs;
2017-01-27 22:13:07 +00:00
private PreferenceScreen prefScreen;
2017-05-26 18:41:24 +00:00
private ListPreference suAccess, autoRes, suNotification, requestTimeout, multiuserMode;
private MagiskManager magiskManager;
2017-02-06 18:01:32 +00:00
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2016-10-17 02:11:26 +00:00
addPreferencesFromResource(R.xml.app_settings);
2016-12-11 12:38:15 +00:00
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
2017-01-27 22:13:07 +00:00
prefScreen = getPreferenceScreen();
2017-05-26 18:41:24 +00:00
magiskManager = Utils.getMagiskManager(getActivity());
2017-01-28 14:02:33 +00:00
PreferenceCategory magiskCategory = (PreferenceCategory) findPreference("magisk");
PreferenceCategory suCategory = (PreferenceCategory) findPreference("superuser");
2017-05-11 18:25:07 +00:00
PreferenceCategory developer = (PreferenceCategory) findPreference("developer");
2017-01-28 14:02:33 +00:00
2017-01-29 08:20:41 +00:00
suAccess = (ListPreference) findPreference("su_access");
autoRes = (ListPreference) findPreference("su_auto_response");
requestTimeout = (ListPreference) findPreference("su_request_timeout");
suNotification = (ListPreference) findPreference("su_notification");
2017-05-26 18:41:24 +00:00
multiuserMode = (ListPreference) findPreference("multiuser_mode");
2017-01-28 14:02:33 +00:00
2017-01-29 08:20:41 +00:00
setSummary();
2017-01-28 14:02:33 +00:00
// Disable dangerous settings in user mode if selected owner manage
if (getActivity().getApplicationInfo().uid > 99999) {
prefScreen.removePreference(magiskCategory);
prefScreen.removePreference(suCategory);
}
2017-01-28 14:02:33 +00:00
findPreference("clear").setOnPreferenceClickListener((pref) -> {
2017-02-12 12:53:41 +00:00
Utils.clearRepoCache(getActivity());
2017-01-11 11:10:30 +00:00
return true;
});
2017-05-11 18:25:07 +00:00
if (!BuildConfig.DEBUG) {
prefScreen.removePreference(developer);
}
2017-01-25 20:17:51 +00:00
if (!Shell.rootAccess()) {
2017-01-28 14:02:33 +00:00
prefScreen.removePreference(magiskCategory);
prefScreen.removePreference(suCategory);
} else {
2017-05-26 18:41:24 +00:00
if (!magiskManager.isSuClient) {
2017-01-28 14:02:33 +00:00
prefScreen.removePreference(suCategory);
}
2017-05-26 18:41:24 +00:00
if (magiskManager.magiskVersionCode < 130) {
2017-02-01 15:54:32 +00:00
prefScreen.removePreference(magiskCategory);
}
}
}
@Override
public void onResume() {
super.onResume();
2016-12-11 12:38:15 +00:00
prefs.registerOnSharedPreferenceChangeListener(this);
}
@Override
2017-01-26 05:46:54 +00:00
public void onPause() {
super.onPause();
2016-12-11 12:38:15 +00:00
prefs.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
2016-12-11 12:38:15 +00:00
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
Logger.dev("Settings: Prefs change " + key);
2017-01-27 22:13:07 +00:00
boolean enabled;
switch (key) {
2017-01-25 20:17:51 +00:00
case "dark_theme":
2017-01-27 22:13:07 +00:00
enabled = prefs.getBoolean("dark_theme", false);
2017-05-26 18:41:24 +00:00
if (magiskManager.isDarkTheme != enabled) {
magiskManager.isDarkTheme = enabled;
magiskManager.reloadMainActivity.trigger();
getActivity().recreate();
}
break;
2017-02-02 11:19:22 +00:00
case "disable":
enabled = prefs.getBoolean("disable", false);
if (enabled) {
Utils.createFile(MagiskManager.MAGISK_DISABLE_FILE);
} else {
Utils.removeItem(MagiskManager.MAGISK_DISABLE_FILE);
}
2017-02-02 11:19:22 +00:00
Toast.makeText(getActivity(), R.string.settings_reboot_toast, Toast.LENGTH_LONG).show();
2016-10-17 02:11:26 +00:00
break;
2017-02-02 11:19:22 +00:00
case "magiskhide":
enabled = prefs.getBoolean("magiskhide", false);
if (enabled) {
2017-05-26 18:41:24 +00:00
if (!magiskManager.isSuClient) {
2017-02-14 21:24:02 +00:00
new AlertDialogBuilder(getActivity())
.setTitle(R.string.no_magisksu_title)
.setMessage(R.string.no_magisksu_msg)
2017-02-12 11:49:46 +00:00
.setPositiveButton(R.string.understand, (dialog, which) -> new MagiskHide().enable())
.setCancelable(false)
.show();
} else {
new MagiskHide().enable();
}
} else {
2017-02-12 11:49:46 +00:00
new MagiskHide().disable();
}
2017-02-02 11:19:22 +00:00
break;
2016-11-08 21:17:50 +00:00
case "hosts":
2017-01-27 22:13:07 +00:00
enabled = prefs.getBoolean("hosts", false);
if (enabled) {
Shell.su_async(null,
"cp -af /system/etc/hosts /magisk/.core/hosts",
"mount -o bind /magisk/.core/hosts /system/etc/hosts");
} else {
Shell.su_async(null,
"umount -l /system/etc/hosts",
"rm -f /magisk/.core/hosts");
}
break;
2017-01-27 22:13:07 +00:00
case "su_access":
2017-05-31 19:18:41 +00:00
magiskManager.suAccessState = Utils.getPrefsInt(prefs, "su_access", 3);
magiskManager.suDB.setSettings(SuDatabaseHelper.ROOT_ACCESS, magiskManager.suAccessState);
2017-01-27 22:13:07 +00:00
break;
2017-05-26 18:41:24 +00:00
case "multiuser_mode":
magiskManager.multiuserMode = Utils.getPrefsInt(prefs, "multiuser_mode", 0);
2017-05-31 19:18:41 +00:00
magiskManager.suDB.setSettings(SuDatabaseHelper.MULTIUSER_MODE, magiskManager.multiuserMode);
2017-01-27 22:13:07 +00:00
case "su_request_timeout":
2017-05-26 18:41:24 +00:00
magiskManager.suRequestTimeout = Utils.getPrefsInt(prefs, "su_request_timeout", 10);
2017-01-27 22:13:07 +00:00
break;
case "su_auto_response":
2017-05-26 18:41:24 +00:00
magiskManager.suResponseType = Utils.getPrefsInt(prefs, "su_auto_response", 0);
2017-01-27 22:13:07 +00:00
break;
case "su_notification":
2017-05-26 18:41:24 +00:00
magiskManager.suNotificationType = Utils.getPrefsInt(prefs, "su_notification", 1);
2017-01-27 22:13:07 +00:00
break;
case "developer_logging":
MagiskManager.devLogging = prefs.getBoolean("developer_logging", false);
break;
case "shell_logging":
MagiskManager.shellLogging = prefs.getBoolean("shell_logging", false);
break;
}
2017-01-29 08:20:41 +00:00
setSummary();
}
private void setSummary() {
suAccess.setSummary(getResources()
2017-05-26 18:41:24 +00:00
.getStringArray(R.array.su_access)[magiskManager.suAccessState]);
2017-01-29 08:20:41 +00:00
autoRes.setSummary(getResources()
2017-05-26 18:41:24 +00:00
.getStringArray(R.array.auto_response)[magiskManager.suResponseType]);
2017-01-29 08:20:41 +00:00
suNotification.setSummary(getResources()
2017-05-26 18:41:24 +00:00
.getStringArray(R.array.su_notification)[magiskManager.suNotificationType]);
2017-01-29 08:20:41 +00:00
requestTimeout.setSummary(
getString(R.string.request_timeout_summary, prefs.getString("su_request_timeout", "10")));
2017-05-26 18:41:24 +00:00
multiuserMode.setSummary(getResources()
.getStringArray(R.array.multiuser_summary)[magiskManager.multiuserMode]);
}
}
}