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

278 lines
11 KiB
Java
Raw Normal View History

package com.topjohnwu.magisk;
import android.Manifest;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
2017-01-28 14:02:33 +00:00
import android.preference.ListPreference;
2017-08-21 19:01:54 +00:00
import android.preference.Preference;
2017-01-28 14:02:33 +00:00
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.preference.SwitchPreference;
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-08-28 17:34:42 +00:00
import com.topjohnwu.magisk.asyncs.CheckUpdates;
2017-08-21 19:01:54 +00:00
import com.topjohnwu.magisk.asyncs.HideManager;
2017-10-15 15:54:34 +00:00
import com.topjohnwu.magisk.asyncs.UpdateRepos;
2017-02-06 18:01:32 +00:00
import com.topjohnwu.magisk.components.Activity;
2017-05-31 19:18:41 +00:00
import com.topjohnwu.magisk.database.SuDatabaseHelper;
2016-11-08 21:17:50 +00:00
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Topic;
import com.topjohnwu.magisk.utils.Utils;
2017-07-22 14:14:02 +00:00
import java.util.Locale;
import butterknife.BindView;
import butterknife.ButterKnife;
public class SettingsActivity extends Activity implements Topic.Subscriber {
@BindView(R.id.toolbar) Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2017-09-03 07:35:14 +00:00
if (getMagiskManager().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();
}
}
2017-07-30 16:44:38 +00:00
@Override
2017-10-07 09:12:36 +00:00
public void onTopicPublished(Topic topic, Object result) {
2017-07-30 16:44:38 +00:00
recreate();
}
@Override
public Topic[] getSubscription() {
2017-09-03 07:35:14 +00:00
return new Topic[] { getMagiskManager().reloadActivity };
2017-07-30 16:44:38 +00:00
}
2017-08-28 16:37:52 +00:00
public static class SettingsFragment extends PreferenceFragment
2017-07-22 14:14:02 +00:00
implements SharedPreferences.OnSharedPreferenceChangeListener,
Topic.Subscriber {
2016-12-11 12:38:15 +00:00
private SharedPreferences prefs;
2017-01-27 22:13:07 +00:00
private PreferenceScreen prefScreen;
2017-08-28 17:34:42 +00:00
private ListPreference updateChannel, suAccess, autoRes, suNotification,
requestTimeout, multiuserMode, namespaceMode;
2017-09-03 07:35:14 +00:00
private MagiskManager mm;
2017-07-22 14:14:02 +00:00
private PreferenceCategory generalCatagory;
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-09-03 07:35:14 +00:00
mm = Utils.getMagiskManager(getActivity());
2017-07-22 14:14:02 +00:00
generalCatagory = (PreferenceCategory) findPreference("general");
2017-01-28 14:02:33 +00:00
PreferenceCategory magiskCategory = (PreferenceCategory) findPreference("magisk");
PreferenceCategory suCategory = (PreferenceCategory) findPreference("superuser");
2017-08-28 17:34:42 +00:00
updateChannel = (ListPreference) findPreference("update_channel");
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-06-08 14:27:24 +00:00
namespaceMode = (ListPreference) findPreference("mnt_ns");
SwitchPreference reauth = (SwitchPreference) findPreference("su_reauth");
2017-08-21 19:01:54 +00:00
Preference hideManager = findPreference("hide");
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) {
suCategory.removePreference(multiuserMode);
2017-08-21 19:01:54 +00:00
generalCatagory.removePreference(hideManager);
}
// Remove re-authentication option on Android O, it will not work
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
suCategory.removePreference(reauth);
}
2017-01-28 14:02:33 +00:00
findPreference("clear").setOnPreferenceClickListener((pref) -> {
2017-10-15 15:54:34 +00:00
mm.prefs.edit().remove(UpdateRepos.ETAG_KEY).apply();
mm.repoDB.clearRepo();
mm.toast(R.string.repo_cache_cleared, Toast.LENGTH_SHORT);
2017-01-11 11:10:30 +00:00
return true;
});
2017-08-21 19:01:54 +00:00
hideManager.setOnPreferenceClickListener((pref) -> {
Utils.runWithPermission(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE,
() -> new HideManager(getActivity()).exec());
2017-08-21 19:01:54 +00:00
return true;
});
2017-01-11 11:10:30 +00:00
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);
2017-08-30 19:07:33 +00:00
generalCatagory.removePreference(hideManager);
} else {
2017-09-03 07:35:14 +00:00
if (!mm.isSuClient) {
2017-01-28 14:02:33 +00:00
prefScreen.removePreference(suCategory);
}
2017-09-03 07:35:14 +00:00
if (mm.magiskVersionCode < 1300) {
2017-02-01 15:54:32 +00:00
prefScreen.removePreference(magiskCategory);
}
}
}
2017-07-30 16:44:38 +00:00
private void setLocalePreference(ListPreference lp) {
boolean isNew = lp == null;
if (isNew) {
2017-07-22 14:14:02 +00:00
lp = new ListPreference(getActivity());
}
2017-09-03 07:35:14 +00:00
CharSequence[] entries = new CharSequence[mm.locales.size() + 1];
CharSequence[] entryValues = new CharSequence[mm.locales.size() + 1];
2017-07-22 14:14:02 +00:00
entries[0] = getString(R.string.system_default);
entryValues[0] = "";
int i = 1;
2017-09-03 07:35:14 +00:00
for (Locale locale : mm.locales) {
2017-07-22 14:14:02 +00:00
entries[i] = locale.getDisplayName(locale);
entryValues[i++] = locale.toLanguageTag();
}
lp.setEntries(entries);
lp.setEntryValues(entryValues);
lp.setTitle(R.string.language);
lp.setKey("locale");
lp.setSummary(MagiskManager.locale.getDisplayName(MagiskManager.locale));
2017-07-30 16:44:38 +00:00
if (isNew) {
generalCatagory.addPreference(lp);
}
2017-07-22 14:14:02 +00:00
}
@Override
public void onResume() {
super.onResume();
2016-12-11 12:38:15 +00:00
prefs.registerOnSharedPreferenceChangeListener(this);
subscribeTopics();
}
@Override
2017-01-26 05:46:54 +00:00
public void onPause() {
2016-12-11 12:38:15 +00:00
prefs.unregisterOnSharedPreferenceChangeListener(this);
unsubscribeTopics();
2017-07-22 16:12:15 +00:00
super.onPause();
}
@Override
2016-12-11 12:38:15 +00:00
public void onSharedPreferenceChanged(SharedPreferences prefs, String 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-09-03 07:35:14 +00:00
if (mm.isDarkTheme != enabled) {
mm.reloadActivity.publish(false);
}
break;
2017-02-02 11:19:22 +00:00
case "disable":
enabled = prefs.getBoolean("disable", false);
if (enabled) {
2017-10-15 15:02:44 +00:00
Utils.createFile(MagiskManager.MAGISK_DISABLE_FILE);
} else {
2017-10-15 15:02:44 +00:00
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-10-15 15:54:34 +00:00
Shell.su_raw("magiskhide --enable");
} else {
2017-10-15 15:54:34 +00:00
Shell.su_raw("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) {
2017-10-15 15:02:44 +00:00
Shell.su_raw(
2017-09-25 09:55:40 +00:00
"cp -af /system/etc/hosts " + MagiskManager.MAGISK_HOST_FILE,
"mount -o bind " + MagiskManager.MAGISK_HOST_FILE + " /system/etc/hosts");
} else {
2017-10-15 15:02:44 +00:00
Shell.su_raw(
"umount -l /system/etc/hosts",
2017-09-25 09:55:40 +00:00
"rm -f " + MagiskManager.MAGISK_HOST_FILE);
}
break;
2017-01-27 22:13:07 +00:00
case "su_access":
2017-09-03 07:35:14 +00:00
mm.suDB.setSettings(SuDatabaseHelper.ROOT_ACCESS, Utils.getPrefsInt(prefs, "su_access"));
2017-01-27 22:13:07 +00:00
break;
2017-05-26 18:41:24 +00:00
case "multiuser_mode":
2017-09-03 07:35:14 +00:00
mm.suDB.setSettings(SuDatabaseHelper.MULTIUSER_MODE, Utils.getPrefsInt(prefs, "multiuser_mode"));
2017-06-08 14:27:24 +00:00
break;
case "mnt_ns":
2017-09-03 07:35:14 +00:00
mm.suDB.setSettings(SuDatabaseHelper.MNT_NS, Utils.getPrefsInt(prefs, "mnt_ns"));
break;
2017-07-22 14:14:02 +00:00
case "locale":
2017-09-03 07:35:14 +00:00
mm.setLocale();
mm.reloadActivity.publish(false);
2017-07-22 14:14:02 +00:00
break;
2017-08-28 17:34:42 +00:00
case "update_channel":
2017-09-03 07:35:14 +00:00
mm.updateChannel = Utils.getPrefsInt(prefs, "update_channel");
new CheckUpdates(mm, true).exec();
2017-08-28 17:34:42 +00:00
break;
}
2017-09-03 07:35:14 +00:00
mm.loadConfig();
2017-01-29 08:20:41 +00:00
setSummary();
}
private void setSummary() {
2017-08-28 17:34:42 +00:00
updateChannel.setSummary(getResources()
2017-09-03 07:35:14 +00:00
.getStringArray(R.array.update_channel)[mm.updateChannel]);
2017-01-29 08:20:41 +00:00
suAccess.setSummary(getResources()
2017-09-03 07:35:14 +00:00
.getStringArray(R.array.su_access)[mm.suAccessState]);
2017-01-29 08:20:41 +00:00
autoRes.setSummary(getResources()
2017-09-03 07:35:14 +00:00
.getStringArray(R.array.auto_response)[mm.suResponseType]);
2017-01-29 08:20:41 +00:00
suNotification.setSummary(getResources()
2017-09-03 07:35:14 +00:00
.getStringArray(R.array.su_notification)[mm.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()
2017-09-03 07:35:14 +00:00
.getStringArray(R.array.multiuser_summary)[mm.multiuserMode]);
2017-06-08 14:27:24 +00:00
namespaceMode.setSummary(getResources()
2017-09-03 07:35:14 +00:00
.getStringArray(R.array.namespace_summary)[mm.suNamespaceMode]);
}
2017-07-22 14:14:02 +00:00
@Override
2017-10-07 09:12:36 +00:00
public void onTopicPublished(Topic topic, Object result) {
2017-07-30 16:44:38 +00:00
setLocalePreference((ListPreference) findPreference("locale"));
2017-07-22 14:14:02 +00:00
}
2017-07-22 16:12:15 +00:00
@Override
public Topic[] getSubscription() {
2017-09-03 07:35:14 +00:00
return new Topic[] { mm.localeDone };
2017-07-22 16:12:15 +00:00
}
}
}