From cd4dfc986162b2c5eb76adbdb11b0dbb46b09932 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 28 Jan 2017 06:13:07 +0800 Subject: [PATCH] Add Superuser settings --- .../java/com/topjohnwu/magisk/Global.java | 35 ++++++++++++- .../topjohnwu/magisk/SettingsActivity.java | 39 +++++++++----- .../com/topjohnwu/magisk/SplashActivity.java | 8 --- .../com/topjohnwu/magisk/StatusFragment.java | 2 + .../magisk/adapters/PolicyAdapter.java | 2 - .../magisk/adapters/SuLogAdapter.java | 2 +- .../magisk/superuser/RequestActivity.java | 4 ++ .../magisk/superuser/SuReceiver.java | 10 +++- .../magisk/superuser/SuRequestActivity.java | 49 +++++++++++------- .../com/topjohnwu/magisk/utils/Utils.java | 5 ++ app/src/main/res/layout/activity_request.xml | 2 +- app/src/main/res/values-ar/strings.xml | 2 - app/src/main/res/values-de/strings.xml | 4 +- app/src/main/res/values-es/strings.xml | 2 - app/src/main/res/values-it/strings.xml | 2 - app/src/main/res/values-nl/strings.xml | 2 - app/src/main/res/values-pl/strings.xml | 4 +- app/src/main/res/values-pt/strings.xml | 6 +-- app/src/main/res/values-zh-rCN/strings.xml | 2 - app/src/main/res/values/arrays.xml | 51 +++++++++++++++---- app/src/main/res/values/strings.xml | 27 ++++++---- app/src/main/res/xml/app_settings.xml | 35 +++++++++++-- 22 files changed, 209 insertions(+), 86 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/Global.java b/app/src/main/java/com/topjohnwu/magisk/Global.java index 1617a7bf1..6c9cf9a55 100644 --- a/app/src/main/java/com/topjohnwu/magisk/Global.java +++ b/app/src/main/java/com/topjohnwu/magisk/Global.java @@ -28,6 +28,7 @@ public class Global { public static String bootBlock = null; public static boolean isSuClient = false; public static String suVersion = null; + public static int shellUid; } public static class Data { public static ValueSortedMap repoMap = new ValueSortedMap<>(); @@ -47,9 +48,11 @@ public class Global { public static boolean isDarkTheme; public static boolean shellLogging; public static boolean devLogging; - public static int suRequestTimeout = 10; + public static int suRequestTimeout; public static int suLogTimeout = 14; - + public static int suAccessState; + public static int suResponseType; + public static int suNotificationType; } public static void init(Context context) { @@ -63,6 +66,34 @@ public class Global { Info.isSuClient = Info.suVersion.toUpperCase().contains("MAGISK"); } updateMagiskInfo(); + initSuConfigs(context); + // Initialize prefs + prefs.edit() + .putBoolean("magiskhide", Utils.itemExist(false, "/magisk/.core/magiskhide/enable")) + .putBoolean("busybox", Utils.commandExists("busybox")) + .putBoolean("hosts", Utils.itemExist(false, "/magisk/.core/hosts")) + .putString("su_request_timeout", String.valueOf(Configs.suRequestTimeout)) + .putString("su_auto_response", String.valueOf(Configs.suResponseType)) + .putString("su_notification", String.valueOf(Configs.suNotificationType)) + .putString("su_access", String.valueOf(Configs.suAccessState)) + .apply(); + } + + public static void initSuConfigs(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + List ret = Shell.sh("getprop persist.sys.root_access"); + if (Utils.isValidShellResponse(ret)) + Configs.suAccessState = Integer.parseInt(ret.get(0)); + else { + Shell.su("setprop persist.sys.root_access 3"); + Configs.suAccessState = 3; + } + Configs.suRequestTimeout = Utils.getPrefsInt(prefs, "su_request_timeout", 10); + Configs.suResponseType = Utils.getPrefsInt(prefs, "su_auto_response", 0); + Configs.suNotificationType = Utils.getPrefsInt(prefs, "su_notification", 1); + ret = Shell.sh("id -u shell"); + if (Utils.isValidShellResponse(ret)) + Info.shellUid = Integer.parseInt(ret.get(0)); } static void updateMagiskInfo() { diff --git a/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java b/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java index 441003d8e..31f056f8e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java @@ -6,6 +6,7 @@ import android.preference.CheckBoxPreference; import android.preference.Preference; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; +import android.preference.PreferenceScreen; import android.preference.SwitchPreference; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; @@ -29,8 +30,6 @@ public class SettingsActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - String theme = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("theme", ""); - Logger.dev("AboutActivity: Theme is " + theme); if (Global.Configs.isDarkTheme) { setTheme(R.style.AppTheme_dh); } @@ -74,6 +73,7 @@ public class SettingsActivity extends AppCompatActivity { implements SharedPreferences.OnSharedPreferenceChangeListener { private SharedPreferences prefs; + private PreferenceScreen prefScreen; @Override public void onCreate(Bundle savedInstanceState) { @@ -81,6 +81,7 @@ public class SettingsActivity extends AppCompatActivity { addPreferencesFromResource(R.xml.app_settings); PreferenceManager.setDefaultValues(getActivity(), R.xml.app_settings, false); prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); + prefScreen = getPreferenceScreen(); CheckBoxPreference busyboxPreference = (CheckBoxPreference) findPreference("busybox"); CheckBoxPreference magiskhidePreference = (CheckBoxPreference) findPreference("magiskhide"); @@ -122,21 +123,21 @@ public class SettingsActivity extends AppCompatActivity { @Override public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { Logger.dev("Settings: Prefs change " + key); - boolean checked; + boolean enabled; switch (key) { case "dark_theme": - checked = prefs.getBoolean("dark_theme", false); - if (Global.Configs.isDarkTheme != checked) { - Global.Configs.isDarkTheme = checked; + enabled = prefs.getBoolean("dark_theme", false); + if (Global.Configs.isDarkTheme != enabled) { + Global.Configs.isDarkTheme = enabled; getActivity().recreate(); Global.Events.reloadMainActivity.trigger(); } break; case "magiskhide": - checked = prefs.getBoolean("magiskhide", false); + enabled = prefs.getBoolean("magiskhide", false); new Async.RootTask() { - private boolean enable = checked; + private boolean enable = enabled; @Override protected Void doInBackground(Void... params) { if (enable) { @@ -151,9 +152,9 @@ public class SettingsActivity extends AppCompatActivity { Toast.makeText(getActivity(), R.string.settings_reboot_toast, Toast.LENGTH_LONG).show(); break; case "busybox": - checked = prefs.getBoolean("busybox", false); + enabled = prefs.getBoolean("busybox", false); new Async.RootTask() { - private boolean enable = checked; + private boolean enable = enabled; @Override protected Void doInBackground(Void... params) { if (enable) { @@ -167,9 +168,9 @@ public class SettingsActivity extends AppCompatActivity { Toast.makeText(getActivity(), R.string.settings_reboot_toast, Toast.LENGTH_LONG).show(); break; case "hosts": - checked = prefs.getBoolean("hosts", false); + enabled = prefs.getBoolean("hosts", false); new Async.RootTask() { - private boolean enable = checked; + private boolean enable = enabled; @Override protected Void doInBackground(Void... voids) { if (enable) { @@ -183,6 +184,19 @@ public class SettingsActivity extends AppCompatActivity { } }.exec(); break; + case "su_access": + Global.Configs.suAccessState = Utils.getPrefsInt(prefs, "su_access", 0); + Shell.su("setprop persist.sys.root_access " + Global.Configs.suAccessState); + break; + case "su_request_timeout": + Global.Configs.suRequestTimeout = Utils.getPrefsInt(prefs, "su_request_timeout", 10); + break; + case "su_auto_response": + Global.Configs.suResponseType = Utils.getPrefsInt(prefs, "su_auto_response", 0); + break; + case "su_notification": + Global.Configs.suNotificationType = Utils.getPrefsInt(prefs, "su_notification", 1); + break; case "developer_logging": Global.Configs.devLogging = prefs.getBoolean("developer_logging", false); break; @@ -190,7 +204,6 @@ public class SettingsActivity extends AppCompatActivity { Global.Configs.shellLogging = prefs.getBoolean("shell_logging", false); break; } - } } diff --git a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java index d19486cf5..a79318cf1 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java @@ -7,7 +7,6 @@ import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; import com.topjohnwu.magisk.utils.Async; -import com.topjohnwu.magisk.utils.Utils; public class SplashActivity extends AppCompatActivity { @@ -23,13 +22,6 @@ public class SplashActivity extends AppCompatActivity { setTheme(R.style.AppTheme_dh); } - // Initialize prefs - prefs.edit() - .putBoolean("magiskhide", Utils.itemExist(false, "/magisk/.core/magiskhide/enable")) - .putBoolean("busybox", Utils.commandExists("busybox")) - .putBoolean("hosts", Utils.itemExist(false, "/magisk/.core/hosts")) - .apply(); - // Start all async tasks new Async.GetBootBlocks().exec(); new Async.CheckUpdates().exec(); diff --git a/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java b/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java index 303321d6d..f875db801 100644 --- a/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java @@ -172,6 +172,7 @@ public class StatusFragment extends Fragment implements CallbackHandler.EventLis color = colorBad; image = R.drawable.ic_cancel; rootStatusText.setText(R.string.not_rooted); + rootInfoText.setText(R.string.root_info_warning); break; case 1: if (Global.Info.suVersion != null) { @@ -186,6 +187,7 @@ public class StatusFragment extends Fragment implements CallbackHandler.EventLis color = colorNeutral; image = R.drawable.ic_help; rootStatusText.setText(R.string.root_error); + rootInfoText.setText(R.string.root_info_warning); } rootStatusContainer.setBackgroundColor(color); rootStatusText.setTextColor(color); diff --git a/app/src/main/java/com/topjohnwu/magisk/adapters/PolicyAdapter.java b/app/src/main/java/com/topjohnwu/magisk/adapters/PolicyAdapter.java index 9e7004d6a..54f81487d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/adapters/PolicyAdapter.java +++ b/app/src/main/java/com/topjohnwu/magisk/adapters/PolicyAdapter.java @@ -117,8 +117,6 @@ public class PolicyAdapter extends RecyclerView.Adapter adapter = ArrayAdapter.createFromResource(this, R.array.timeout, android.R.layout.simple_spinner_item); - adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - timeout.setAdapter(adapter); + appIcon.setImageDrawable(info.applicationInfo.loadIcon(pm)); appNameView.setText(appName); packageNameView.setText(packageName); + ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.allow_timeout, android.R.layout.simple_spinner_item); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + timeout.setAdapter(adapter); + timer = new CountDownTimer(Global.Configs.suRequestTimeout * 1000, 1000) { @Override public void onTick(long millisUntilFinished) { - deny_btn.setText(getString(R.string.deny, "(" + millisUntilFinished / 1000 + ")")); + deny_btn.setText(getString(R.string.deny_with_str, "(" + millisUntilFinished / 1000 + ")")); } @Override public void onFinish() { - deny_btn.setText(getString(R.string.deny, "(0)")); + deny_btn.setText(getString(R.string.deny_with_str, "(0)")); handleAction(false, -1); } }; @@ -90,16 +111,15 @@ public class SuRequestActivity extends AppCompatActivity { deny_btn.setOnClickListener(v -> handleAction(false, timeoutList[timeout.getSelectedItemPosition()])); suPopup.setOnClickListener((v) -> { timer.cancel(); - deny_btn.setText(getString(R.string.deny, "")); + deny_btn.setText(getString(R.string.deny)); }); timeout.setOnTouchListener((v, event) -> { timer.cancel(); - deny_btn.setText(getString(R.string.deny, "")); + deny_btn.setText(getString(R.string.deny)); return false; }); timer.start(); - } void handleAction(boolean action, int timeout) { @@ -175,14 +195,9 @@ public class SuRequestActivity extends AppCompatActivity { try { if (!result) throw new Throwable(); String[] pkgs = pm.getPackagesForUid(uid); - if (pkgs != null && pkgs.length > 0) { - info = pm.getPackageInfo(pkgs[0], 0); - packageName = pkgs[0]; - appName = info.applicationInfo.loadLabel(pm).toString(); - updateUI(); - } - else - throw new Throwable(); + if (pkgs == null || pkgs.length == 0) throw new Throwable(); + info = pm.getPackageInfo(pkgs[0], 0); + showRequest(); } catch (Throwable e) { handleAction(false, -1); } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java index 36a90e652..fb16d954b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -5,6 +5,7 @@ import android.app.AlertDialog; import android.app.DownloadManager; import android.content.Context; import android.content.IntentFilter; +import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Environment; @@ -146,4 +147,8 @@ public class Utils { return false; } + public static int getPrefsInt(SharedPreferences prefs, String key, int def) { + return Integer.parseInt(prefs.getString(key, String.valueOf(def))); + } + } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_request.xml b/app/src/main/res/layout/activity_request.xml index 3a0659f51..2fad57bda 100644 --- a/app/src/main/res/layout/activity_request.xml +++ b/app/src/main/res/layout/activity_request.xml @@ -99,7 +99,7 @@