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

198 lines
7.7 KiB
Java
Raw Normal View History

package com.topjohnwu.magisk;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
2017-01-11 11:10:30 +00:00
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
2017-01-25 20:17:51 +00:00
import android.preference.SwitchPreference;
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 {
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());
CheckBoxPreference busyboxPreference = (CheckBoxPreference) findPreference("busybox");
2016-10-17 02:11:26 +00:00
CheckBoxPreference magiskhidePreference = (CheckBoxPreference) findPreference("magiskhide");
2017-01-25 20:17:51 +00:00
SwitchPreference hostsPreference = (SwitchPreference) findPreference("hosts");
2017-01-11 11:10:30 +00:00
Preference clear = findPreference("clear");
clear.setOnPreferenceClickListener((pref) -> {
2017-01-25 20:17:51 +00:00
ModuleHelper.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-11 11:10:30 +00:00
busyboxPreference.setEnabled(false);
magiskhidePreference.setEnabled(false);
hostsPreference.setEnabled(false);
} else {
2017-01-25 20:17:51 +00:00
if (Global.Info.magiskVersion < 9) {
hostsPreference.setEnabled(false);
busyboxPreference.setEnabled(false);
}
if (Global.Info.magiskVersion < 8) {
magiskhidePreference.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);
2016-11-08 21:17:50 +00:00
boolean checked;
switch (key) {
2017-01-25 20:17:51 +00:00
case "dark_theme":
checked = prefs.getBoolean("dark_theme", false);
if (Global.Configs.isDarkTheme != checked) {
Global.Configs.isDarkTheme = checked;
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;
}
}
}
}