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

299 lines
12 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;
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;
2017-11-19 19:09:08 +00:00
import android.text.InputType;
import android.widget.EditText;
import android.widget.LinearLayout;
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-02-06 18:01:32 +00:00
import com.topjohnwu.magisk.components.Activity;
2017-11-19 19:09:08 +00:00
import com.topjohnwu.magisk.components.AlertDialogBuilder;
2017-11-05 20:41:23 +00:00
import com.topjohnwu.magisk.utils.Const;
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-11-19 19:09:08 +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);
2017-09-03 07:35:14 +00:00
mm = Utils.getMagiskManager(getActivity());
2017-11-19 19:09:08 +00:00
prefs = mm.prefs;
prefScreen = getPreferenceScreen();
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-21 19:01:54 +00:00
Preference hideManager = findPreference("hide");
2017-11-05 20:41:23 +00:00
findPreference("clear").setOnPreferenceClickListener((pref) -> {
2017-11-19 19:09:08 +00:00
prefs.edit().remove(Const.Key.ETAG_KEY).apply();
2017-11-05 20:41:23 +00:00
mm.repoDB.clearRepo();
MagiskManager.toast(R.string.repo_cache_cleared, Toast.LENGTH_SHORT);
return true;
});
updateChannel = (ListPreference) findPreference(Const.Key.UPDATE_CHANNEL);
suAccess = (ListPreference) findPreference(Const.Key.ROOT_ACCESS);
autoRes = (ListPreference) findPreference(Const.Key.SU_AUTO_RESPONSE);
requestTimeout = (ListPreference) findPreference(Const.Key.SU_REQUEST_TIMEOUT);
suNotification = (ListPreference) findPreference(Const.Key.SU_NOTIFICATION);
multiuserMode = (ListPreference) findPreference(Const.Key.SU_MULTIUSER_MODE);
namespaceMode = (ListPreference) findPreference(Const.Key.SU_MNT_NS);
SwitchPreference reauth = (SwitchPreference) findPreference(Const.Key.SU_REAUTH);
2017-11-19 19:09:08 +00:00
updateChannel.setOnPreferenceChangeListener((pref, o) -> {
mm.updateChannel = Integer.parseInt((String) o);
if (mm.updateChannel == Const.Value.CUSTOM_CHANNEL) {
LinearLayout layout = new LinearLayout(getActivity());
EditText url = new EditText(getActivity());
url.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI);
url.setText(mm.customChannelUrl);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(url);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) url.getLayoutParams();
params.setMargins(Utils.dpInPx(15), 0, Utils.dpInPx(15), 0);
new AlertDialogBuilder(getActivity())
.setTitle(R.string.settings_update_custom)
.setMessage(R.string.settings_update_custom_msg)
.setView(layout)
.setPositiveButton(R.string.ok, (d, i) -> {
prefs.edit().putString(Const.Key.CUSTOM_CHANNEL, url.getText().toString()).apply();
})
2017-11-22 06:03:08 +00:00
.setNegativeButton(R.string.close, null)
2017-11-19 19:09:08 +00:00
.show();
}
return true;
});
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
2017-10-21 14:54:47 +00:00
if (mm.userId > 0) {
suCategory.removePreference(multiuserMode);
}
// Remove re-authentication option on Android O, it will not work
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
suCategory.removePreference(reauth);
}
if (mm.getPackageName().equals(Const.ORIG_PKG_NAME) && mm.magiskVersionCode >= 1440) {
2017-10-21 14:54:47 +00:00
hideManager.setOnPreferenceClickListener((pref) -> {
Utils.runWithPermission(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE,
() -> new HideManager().exec());
return true;
});
} else {
generalCatagory.removePreference(hideManager);
}
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);
2017-11-08 17:11:50 +00:00
} else if (mm.magiskVersionCode < 1300) {
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);
2017-11-05 20:41:23 +00:00
lp.setKey(Const.Key.LOCALE);
2017-07-22 14:14:02 +00:00
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-11-05 20:41:23 +00:00
case Const.Key.DARK_THEME:
enabled = prefs.getBoolean(Const.Key.DARK_THEME, false);
2017-09-03 07:35:14 +00:00
if (mm.isDarkTheme != enabled) {
mm.reloadActivity.publish(false);
}
break;
2017-11-05 20:41:23 +00:00
case Const.Key.DISABLE:
enabled = prefs.getBoolean(Const.Key.DISABLE, false);
if (enabled) {
2017-11-05 20:41:23 +00:00
Utils.createFile(Const.MAGISK_DISABLE_FILE);
} else {
2017-11-05 20:41:23 +00:00
Utils.removeItem(Const.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-11-05 20:41:23 +00:00
case Const.Key.MAGISKHIDE:
enabled = prefs.getBoolean(Const.Key.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;
2017-11-05 20:41:23 +00:00
case Const.Key.HOSTS:
enabled = prefs.getBoolean(Const.Key.HOSTS, false);
if (enabled) {
2017-10-15 15:02:44 +00:00
Shell.su_raw(
2017-11-08 16:03:37 +00:00
"cp -af /system/etc/hosts " + Const.MAGISK_HOST_FILE(),
"mount -o bind " + Const.MAGISK_HOST_FILE() + " /system/etc/hosts");
} else {
2017-10-15 15:02:44 +00:00
Shell.su_raw(
"umount -l /system/etc/hosts",
2017-11-08 16:03:37 +00:00
"rm -f " + Const.MAGISK_HOST_FILE());
}
break;
2017-11-05 20:41:23 +00:00
case Const.Key.ROOT_ACCESS:
mm.suDB.setSettings(Const.Key.ROOT_ACCESS, Utils.getPrefsInt(prefs, Const.Key.ROOT_ACCESS));
2017-01-27 22:13:07 +00:00
break;
2017-11-05 20:41:23 +00:00
case Const.Key.SU_MULTIUSER_MODE:
mm.suDB.setSettings(Const.Key.SU_MULTIUSER_MODE, Utils.getPrefsInt(prefs, Const.Key.SU_MULTIUSER_MODE));
2017-06-08 14:27:24 +00:00
break;
2017-11-05 20:41:23 +00:00
case Const.Key.SU_MNT_NS:
mm.suDB.setSettings(Const.Key.SU_MNT_NS, Utils.getPrefsInt(prefs, Const.Key.SU_MNT_NS));
break;
2017-11-05 20:41:23 +00:00
case Const.Key.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-11-05 20:41:23 +00:00
case Const.Key.UPDATE_CHANNEL:
2017-11-19 19:09:08 +00:00
new CheckUpdates().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(
2017-11-05 20:41:23 +00:00
getString(R.string.request_timeout_summary, prefs.getString(Const.Key.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-11-05 20:41:23 +00:00
setLocalePreference((ListPreference) findPreference(Const.Key.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
}
}
}