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

254 lines
11 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;
2017-01-25 20:17:51 +00:00
import android.preference.SwitchPreference;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.WindowManager;
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;
import com.topjohnwu.magisk.asyncs.SerialTask;
2017-02-06 18:01:32 +00:00
import com.topjohnwu.magisk.components.Activity;
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-06 18:01:32 +00:00
if (getTopApplication().isDarkTheme) {
setTheme(R.style.AppTheme_dh);
}
setContentView(R.layout.activity_container);
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();
}
}
public void setFloating() {
boolean isTablet = getResources().getBoolean(R.bool.isTablet);
if (isTablet) {
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = getResources().getDimensionPixelSize(R.dimen.floating_height);
params.width = getResources().getDimensionPixelSize(R.dimen.floating_width);
params.alpha = 1.0f;
params.dimAmount = 0.6f;
params.flags |= 2;
getWindow().setAttributes(params);
setFinishOnTouchOutside(true);
}
}
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-01-29 08:20:41 +00:00
private ListPreference suAccess, autoRes, suNotification, requestTimeout;
2017-02-06 18:01:32 +00:00
private MagiskManager getApplication() {
return Utils.getMagiskManager(getActivity());
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-02-04 20:40:52 +00:00
SwitchPreference busybox = (SwitchPreference) findPreference("busybox");
SwitchPreference magiskHide = (SwitchPreference) findPreference("magiskhide");
SwitchPreference hosts = (SwitchPreference) findPreference("hosts");
2017-01-28 14:02:33 +00:00
PreferenceCategory magiskCategory = (PreferenceCategory) findPreference("magisk");
PreferenceCategory suCategory = (PreferenceCategory) findPreference("superuser");
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-01-28 14:02:33 +00:00
2017-01-29 08:20:41 +00:00
setSummary();
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-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-02-06 18:01:32 +00:00
if (!getApplication().isSuClient)
2017-01-28 14:02:33 +00:00
prefScreen.removePreference(suCategory);
2017-02-06 18:01:32 +00:00
if (getApplication().magiskVersion < 11)
2017-02-01 15:54:32 +00:00
prefScreen.removePreference(magiskCategory);
2017-02-06 18:01:32 +00:00
if (getApplication().disabled) {
2017-02-04 20:40:52 +00:00
busybox.setEnabled(false);
magiskHide.setEnabled(false);
hosts.setEnabled(false);
}
}
}
@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-02-06 18:01:32 +00:00
if (getApplication().isDarkTheme != enabled) {
getApplication().isDarkTheme = enabled;
2017-01-11 11:10:30 +00:00
getActivity().recreate();
2017-02-06 18:01:32 +00:00
getApplication().reloadMainActivity.trigger();
}
break;
2017-02-02 11:19:22 +00:00
case "disable":
enabled = prefs.getBoolean("disable", false);
2017-02-12 11:49:46 +00:00
new SerialTask<Void, Void, Void>() {
2017-02-04 20:40:52 +00:00
private boolean enable = enabled;
@Override
protected Void doInBackground(Void... voids) {
if (enable) {
2017-02-06 18:01:32 +00:00
Utils.createFile(MagiskManager.MAGISK_DISABLE_FILE);
2017-02-04 20:40:52 +00:00
} else {
2017-02-06 18:01:32 +00:00
Utils.removeItem(MagiskManager.MAGISK_DISABLE_FILE);
2017-02-04 20:40:52 +00:00
}
return null;
2017-02-02 11:19:22 +00:00
}
2017-02-04 20:40:52 +00:00
}.exec();
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;
case "busybox":
2017-01-27 22:13:07 +00:00
enabled = prefs.getBoolean("busybox", false);
2017-02-12 11:49:46 +00:00
new SerialTask<Void, Void, Void>() {
2017-02-04 20:40:52 +00:00
private boolean enable = enabled;
@Override
protected Void doInBackground(Void... voids) {
if (enable) {
Shell.su(
"setprop persist.magisk.busybox 1",
"sh /sbin/magic_mask.sh mount_busybox");
} else {
Shell.su(
"setprop persist.magisk.busybox 0",
"umount /system/xbin");
}
return null;
2017-01-11 11:10:30 +00:00
}
2017-02-04 20:40:52 +00:00
}.exec();
2016-11-08 21:17:50 +00:00
break;
2017-02-02 11:19:22 +00:00
case "magiskhide":
enabled = prefs.getBoolean("magiskhide", false);
if (enabled) {
2017-02-06 18:01:32 +00:00
if (!getApplication().isSuClient) {
Utils.getAlertDialogBuilder(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();
2017-02-12 11:49:46 +00:00
} 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);
2017-02-12 11:49:46 +00:00
new SerialTask<Void, Void, Void>() {
2017-01-27 22:13:07 +00:00
private boolean enable = enabled;
2017-01-11 11:10:30 +00:00
@Override
protected Void doInBackground(Void... voids) {
if (enable) {
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");
2016-11-08 21:17:50 +00:00
}
2017-01-11 11:10:30 +00:00
return null;
}
}.exec();
break;
2017-01-27 22:13:07 +00:00
case "su_access":
2017-02-06 18:01:32 +00:00
getApplication().suAccessState = Utils.getPrefsInt(prefs, "su_access", 0);
Shell.su("setprop persist.sys.root_access " + getApplication().suAccessState);
2017-01-27 22:13:07 +00:00
break;
case "su_request_timeout":
2017-02-06 18:01:32 +00:00
getApplication().suRequestTimeout = Utils.getPrefsInt(prefs, "su_request_timeout", 10);
2017-01-27 22:13:07 +00:00
break;
case "su_auto_response":
2017-02-06 18:01:32 +00:00
getApplication().suResponseType = Utils.getPrefsInt(prefs, "su_auto_response", 0);
2017-01-27 22:13:07 +00:00
break;
case "su_notification":
2017-02-06 18:01:32 +00:00
getApplication().suNotificationType = Utils.getPrefsInt(prefs, "su_notification", 1);
2017-01-27 22:13:07 +00:00
break;
case "developer_logging":
2017-02-06 18:01:32 +00:00
getApplication().devLogging = prefs.getBoolean("developer_logging", false);
break;
case "shell_logging":
2017-02-06 18:01:32 +00:00
getApplication().shellLogging = prefs.getBoolean("shell_logging", false);
break;
}
2017-01-29 08:20:41 +00:00
setSummary();
}
private void setSummary() {
suAccess.setSummary(getResources()
2017-02-06 18:01:32 +00:00
.getStringArray(R.array.su_access)[getApplication().suAccessState]);
2017-01-29 08:20:41 +00:00
autoRes.setSummary(getResources()
2017-02-06 18:01:32 +00:00
.getStringArray(R.array.auto_response)[getApplication().suResponseType]);
2017-01-29 08:20:41 +00:00
suNotification.setSummary(getResources()
2017-02-06 18:01:32 +00:00
.getStringArray(R.array.su_notification)[getApplication().suNotificationType]);
2017-01-29 08:20:41 +00:00
requestTimeout.setSummary(
getString(R.string.request_timeout_summary, prefs.getString("su_request_timeout", "10")));
}
}
}