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

214 lines
8.7 KiB
Java
Raw Normal View History

package com.topjohnwu.magisk;
2017-01-11 11:10:30 +00:00
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
2017-01-11 11:10:30 +00:00
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.WindowManager;
2016-11-08 21:17:50 +00:00
import android.widget.Toast;
2017-01-24 20:27:05 +00:00
import com.topjohnwu.magisk.module.ModuleHelper;
import com.topjohnwu.magisk.utils.Async;
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;
public class SettingsActivity extends AppCompatActivity {
@BindView(R.id.toolbar) Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String theme = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("theme", "");
Logger.dev("AboutActivity: Theme is " + theme);
2017-01-25 05:16:50 +00:00
if (Global.Configs.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 {
private ListPreference themePreference;
2016-12-11 12:38:15 +00:00
private SharedPreferences prefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2016-10-17 02:11:26 +00:00
addPreferencesFromResource(R.xml.app_settings);
PreferenceManager.setDefaultValues(getActivity(), R.xml.app_settings, false);
2016-12-11 12:38:15 +00:00
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
themePreference = (ListPreference) findPreference("theme");
CheckBoxPreference busyboxPreference = (CheckBoxPreference) findPreference("busybox");
2016-10-17 02:11:26 +00:00
CheckBoxPreference magiskhidePreference = (CheckBoxPreference) findPreference("magiskhide");
2016-11-08 21:17:50 +00:00
CheckBoxPreference hostsPreference = (CheckBoxPreference) findPreference("hosts");
2017-01-11 11:10:30 +00:00
Preference clear = findPreference("clear");
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;
});
2017-01-25 05:16:50 +00:00
if (Global.Configs.isDarkTheme) {
2017-01-11 11:10:30 +00:00
themePreference.setSummary(R.string.theme_dark);
} else {
themePreference.setSummary(R.string.theme_default);
}
2017-01-24 20:27:05 +00:00
if (Global.Info.magiskVersion < 9) {
2016-11-08 21:17:50 +00:00
hostsPreference.setEnabled(false);
busyboxPreference.setEnabled(false);
2017-01-24 20:27:05 +00:00
} else if (Global.Info.magiskVersion < 8) {
2016-11-08 21:17:50 +00:00
magiskhidePreference.setEnabled(false);
2017-01-11 11:10:30 +00:00
} else if (! Shell.rootAccess()) {
busyboxPreference.setEnabled(false);
magiskhidePreference.setEnabled(false);
hostsPreference.setEnabled(false);
} else {
busyboxPreference.setEnabled(true);
2016-10-17 02:11:26 +00:00
magiskhidePreference.setEnabled(true);
2016-11-08 21:17:50 +00:00
hostsPreference.setEnabled(true);
}
}
@Override
public void onResume() {
super.onResume();
2016-12-11 12:38:15 +00:00
prefs.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onDestroy() {
2016-12-11 12:38:15 +00:00
prefs.unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}
@Override
2016-12-11 12:38:15 +00:00
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
Logger.dev("Settings: Prefs change " + key);
2016-11-08 21:17:50 +00:00
boolean checked;
switch (key) {
case "theme":
2017-01-10 14:30:05 +00:00
String theme = prefs.getString("theme", getString(R.string.theme_default_value));
2017-01-25 05:16:50 +00:00
if (Global.Configs.isDarkTheme != theme.equalsIgnoreCase(getString(R.string.theme_dark_value))) {
Global.Configs.isDarkTheme = !Global.Configs.isDarkTheme;
2017-01-11 11:10:30 +00:00
getActivity().recreate();
2017-01-24 20:27:05 +00:00
Global.Events.reloadMainActivity.trigger();
}
break;
2016-10-17 02:11:26 +00:00
case "magiskhide":
2017-01-11 11:10:30 +00:00
checked = prefs.getBoolean("magiskhide", false);
new Async.RootTask<Void, Void, Void>() {
private boolean enable = checked;
@Override
protected Void doInBackground(Void... params) {
if (enable) {
2016-10-17 02:11:26 +00:00
Utils.createFile("/magisk/.core/magiskhide/enable");
2017-01-11 11:10:30 +00:00
} else {
2016-10-17 02:11:26 +00:00
Utils.removeItem("/magisk/.core/magiskhide/enable");
}
2017-01-11 11:10:30 +00:00
return null;
}
}.exec();
2016-12-11 12:38:15 +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":
2016-12-11 12:38:15 +00:00
checked = prefs.getBoolean("busybox", false);
2017-01-11 11:10:30 +00:00
new Async.RootTask<Void, Void, Void>() {
private boolean enable = checked;
@Override
protected Void doInBackground(Void... params) {
if (enable) {
2016-11-08 21:17:50 +00:00
Utils.createFile("/magisk/.core/busybox/enable");
2017-01-11 11:10:30 +00:00
} else {
2016-11-08 21:17:50 +00:00
Utils.removeItem("/magisk/.core/busybox/enable");
}
2017-01-11 11:10:30 +00:00
return null;
}
}.exec();
2016-11-08 21:17:50 +00:00
Toast.makeText(getActivity(), R.string.settings_reboot_toast, Toast.LENGTH_LONG).show();
break;
case "hosts":
2016-12-11 12:38:15 +00:00
checked = prefs.getBoolean("hosts", false);
2017-01-11 11:10:30 +00:00
new Async.RootTask<Void, Void, Void>() {
private boolean enable = checked;
@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;
case "developer_logging":
2017-01-25 05:16:50 +00:00
Global.Configs.devLogging = prefs.getBoolean("developer_logging", false);
break;
case "shell_logging":
2017-01-25 05:16:50 +00:00
Global.Configs.shellLogging = prefs.getBoolean("shell_logging", false);
break;
}
}
}
}