From 7239c2e31a9eb5e4208ab428e73ce0a36eb7d318 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 5 Feb 2017 04:40:52 +0800 Subject: [PATCH] Update to the latest settings --- .../java/com/topjohnwu/magisk/Global.java | 30 +++------- .../com/topjohnwu/magisk/MainActivity.java | 2 - .../topjohnwu/magisk/SettingsActivity.java | 58 +++++++++++-------- .../com/topjohnwu/magisk/SplashActivity.java | 23 +++----- .../com/topjohnwu/magisk/StatusFragment.java | 8 ++- .../magisk/receivers/BootReceiver.java | 23 ++++++-- .../com/topjohnwu/magisk/utils/Async.java | 36 ++++++++++++ .../com/topjohnwu/magisk/utils/Shell.java | 2 +- app/src/main/res/values/strings.xml | 5 +- app/src/main/res/xml/app_settings.xml | 2 +- 10 files changed, 116 insertions(+), 73 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/Global.java b/app/src/main/java/com/topjohnwu/magisk/Global.java index 127cda8b6..ee334017c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/Global.java +++ b/app/src/main/java/com/topjohnwu/magisk/Global.java @@ -17,6 +17,8 @@ import java.util.List; public class Global { + public static final String MAGISK_DISABLE_FILE = "/cache/.disable_magisk"; + public static class Info { public static double magiskVersion; public static String magiskVersionString = "(none)"; @@ -27,6 +29,7 @@ public class Global { public static String bootBlock = null; public static boolean isSuClient = false; public static String suVersion = null; + public static boolean disabled = false; } public static class Data { public static ValueSortedMap repoMap; @@ -64,28 +67,6 @@ public class Global { public static int suNotificationType; } - public static void init(Context context) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - Configs.isDarkTheme = prefs.getBoolean("dark_theme", false); - Configs.devLogging = prefs.getBoolean("developer_logging", false); - Configs.shellLogging = prefs.getBoolean("shell_logging", false); - Configs.magiskHide = prefs.getBoolean("magiskhide", false); - updateMagiskInfo(); - initSuAccess(); - initSuConfigs(context); - // Initialize prefs - prefs.edit() - .putBoolean("dark_theme", Configs.isDarkTheme) - .putBoolean("magiskhide", Configs.magiskHide) - .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); Configs.suRequestTimeout = Utils.getPrefsInt(prefs, "su_request_timeout", 10); @@ -110,7 +91,7 @@ public class Global { } } - static void updateMagiskInfo() { + public static void updateMagiskInfo() { List ret = Shell.sh("getprop magisk.version"); if (!Utils.isValidShellResponse(ret)) { Info.magiskVersion = -1; @@ -123,6 +104,9 @@ public class Global { Info.magiskVersion = Double.POSITIVE_INFINITY; } } + ret = Shell.sh("getprop ro.magisk.disable"); + if (Utils.isValidShellResponse(ret)) + Info.disabled = Integer.parseInt(ret.get(0)) != 0; } } diff --git a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java index 52eb28090..f36bd6e43 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java @@ -101,8 +101,6 @@ public class MainActivity extends AppCompatActivity @Override protected void onDestroy() { CallbackHandler.unRegister(Global.Events.reloadMainActivity, this); - // Let garbage collector remove them - Global.Data.clear(); super.onDestroy(); } diff --git a/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java b/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java index 839e5c923..edfa307db 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java @@ -2,7 +2,6 @@ package com.topjohnwu.magisk; import android.content.SharedPreferences; import android.os.Bundle; -import android.preference.CheckBoxPreference; import android.preference.ListPreference; import android.preference.PreferenceCategory; import android.preference.PreferenceFragment; @@ -21,9 +20,6 @@ import com.topjohnwu.magisk.utils.Logger; import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Utils; -import java.io.File; -import java.io.IOException; - import butterknife.BindView; import butterknife.ButterKnife; @@ -88,6 +84,10 @@ public class SettingsActivity extends AppCompatActivity { prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); prefScreen = getPreferenceScreen(); + SwitchPreference busybox = (SwitchPreference) findPreference("busybox"); + SwitchPreference magiskHide = (SwitchPreference) findPreference("magiskhide"); + SwitchPreference hosts = (SwitchPreference) findPreference("hosts"); + PreferenceCategory magiskCategory = (PreferenceCategory) findPreference("magisk"); PreferenceCategory suCategory = (PreferenceCategory) findPreference("superuser"); @@ -111,6 +111,11 @@ public class SettingsActivity extends AppCompatActivity { prefScreen.removePreference(suCategory); if (Global.Info.magiskVersion < 11) prefScreen.removePreference(magiskCategory); + if (Global.Info.disabled) { + busybox.setEnabled(false); + magiskHide.setEnabled(false); + hosts.setEnabled(false); + } } } @@ -142,31 +147,38 @@ public class SettingsActivity extends AppCompatActivity { break; case "disable": enabled = prefs.getBoolean("disable", false); - File disable = new File(getActivity().getFilesDir() + "/disable"); - if (enabled) - try { - disable.createNewFile(); - } catch (IOException e) { - e.printStackTrace(); - prefs.edit().putBoolean("disable", false).apply(); + new Async.RootTask() { + private boolean enable = enabled; + @Override + protected Void doInBackground(Void... voids) { + if (enable) { + Utils.createFile(Global.MAGISK_DISABLE_FILE); + } else { + Utils.removeItem(Global.MAGISK_DISABLE_FILE); + } + return null; } - else - disable.delete(); + }.exec(); Toast.makeText(getActivity(), R.string.settings_reboot_toast, Toast.LENGTH_LONG).show(); break; case "busybox": enabled = prefs.getBoolean("busybox", false); - File busybox = new File(getActivity().getFilesDir() + "/busybox"); - if (enabled) - try { - busybox.createNewFile(); - } catch (IOException e) { - e.printStackTrace(); - prefs.edit().putBoolean("busybox", false).apply(); + new Async.RootTask() { + private boolean enable = enabled; + @Override + protected Void doInBackground(Void... voids) { + if (enable) { + Shell.su( + "setprop persist.magisk.busybox 1", + "sh /sbin/magic_mask.sh mount_busybox"); + } else { + Shell.su( + "setprop persist.magisk.busybox 0", + "umount /system/xbin"); + } + return null; } - else - busybox.delete(); - Toast.makeText(getActivity(), R.string.settings_reboot_toast, Toast.LENGTH_LONG).show(); + }.exec(); break; case "magiskhide": enabled = prefs.getBoolean("magiskhide", false); diff --git a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java index a79318cf1..3448cdd64 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java @@ -1,9 +1,7 @@ package com.topjohnwu.magisk; import android.content.Intent; -import android.content.SharedPreferences; import android.os.Bundle; -import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; import com.topjohnwu.magisk.utils.Async; @@ -14,15 +12,17 @@ public class SplashActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); - - Global.init(getApplicationContext()); - - if (Global.Configs.isDarkTheme) { - setTheme(R.style.AppTheme_dh); - } // Start all async tasks + new Async.InitConfigs(getApplicationContext()){ + @Override + protected void onPostExecute(Void v) { + // Start main activity only after configs are loaded + Intent intent = new Intent(getApplicationContext(), MainActivity.class); + startActivity(intent); + finish(); + } + }.exec(); new Async.GetBootBlocks().exec(); new Async.CheckUpdates().exec(); new Async.LoadModules() { @@ -33,10 +33,5 @@ public class SplashActivity extends AppCompatActivity { } }.exec(); new Async.LoadApps(getPackageManager()).exec(); - - // Start main activity - Intent intent = new Intent(getApplicationContext(), MainActivity.class); - startActivity(intent); - finish(); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java b/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java index 2f85e2695..bd704ae6f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java @@ -163,6 +163,8 @@ public class StatusFragment extends Fragment implements CallbackHandler.EventLis if (Global.Info.magiskVersion < 0) { magiskVersionText.setText(R.string.magisk_version_error); + } else if (Global.Info.disabled) { + magiskVersionText.setText(getString(R.string.magisk_version_disable, Global.Info.magiskVersionString)); } else { magiskVersionText.setText(getString(R.string.magisk_version, Global.Info.magiskVersionString)); } @@ -215,7 +217,11 @@ public class StatusFragment extends Fragment implements CallbackHandler.EventLis if (Global.Info.magiskVersion < 0) { color = colorBad; image = R.drawable.ic_cancel; + } else if (Global.Info.disabled) { + color = colorNeutral; + image = R.drawable.ic_cancel; } + magiskStatusContainer.setBackgroundColor(color); magiskVersionText.setTextColor(color); magiskUpdateText.setTextColor(color); @@ -231,7 +237,7 @@ public class StatusFragment extends Fragment implements CallbackHandler.EventLis .setPositiveButton(R.string.goto_install, (dialogInterface, i) -> { ((MainActivity) getActivity()).navigationView.setCheckedItem(R.id.install); FragmentTransaction transaction = getFragmentManager().beginTransaction(); - transaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out); + transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out); try { transaction.replace(R.id.content_frame, new InstallFragment(), "install").commit(); } catch (IllegalStateException ignored) {} diff --git a/app/src/main/java/com/topjohnwu/magisk/receivers/BootReceiver.java b/app/src/main/java/com/topjohnwu/magisk/receivers/BootReceiver.java index 308af0fa8..00a6adb6b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/receivers/BootReceiver.java +++ b/app/src/main/java/com/topjohnwu/magisk/receivers/BootReceiver.java @@ -14,11 +14,22 @@ import com.topjohnwu.magisk.utils.Async; public class BootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - Global.initSuAccess(); - if (prefs.getBoolean("magiskhide", false)) { - Toast.makeText(context, R.string.start_magiskhide, Toast.LENGTH_SHORT).show(); - new Async.MagiskHide().enable(); - } + new Async.RootTask() { + @Override + protected Void doInBackground(Void... params) { + Global.initSuAccess(); + Global.updateMagiskInfo(); + return null; + } + + @Override + protected void onPostExecute(Void v) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + if (prefs.getBoolean("magiskhide", false) && !Global.Info.disabled && Global.Info.magiskVersion >= 11) { + Toast.makeText(context, R.string.start_magiskhide, Toast.LENGTH_SHORT).show(); + new Async.MagiskHide().enable(); + } + } + }.exec(); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Async.java b/app/src/main/java/com/topjohnwu/magisk/utils/Async.java index 95d5d7e23..a2910bd1e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Async.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Async.java @@ -2,11 +2,13 @@ package com.topjohnwu.magisk.utils; import android.app.ProgressDialog; import android.content.Context; +import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; import android.os.AsyncTask; +import android.preference.PreferenceManager; import android.provider.OpenableColumns; import android.widget.Toast; @@ -49,6 +51,40 @@ public class Async { public static final String MAGISK_HIDE_PATH = "/magisk/.core/magiskhide/"; public static final String TMP_FOLDER_PATH = "/dev/tmp"; + public static class InitConfigs extends RootTask { + + Context mContext; + + public InitConfigs(Context context) { + mContext = context; + } + + @Override + protected Void doInBackground(Void... params) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); + Global.Configs.isDarkTheme = prefs.getBoolean("dark_theme", false); + Global.Configs.devLogging = prefs.getBoolean("developer_logging", false); + Global.Configs.shellLogging = prefs.getBoolean("shell_logging", false); + Global.Configs.magiskHide = prefs.getBoolean("magiskhide", false); + Global.updateMagiskInfo(); + Global.initSuAccess(); + Global.initSuConfigs(mContext); + // Initialize prefs + prefs.edit() + .putBoolean("dark_theme", Global.Configs.isDarkTheme) + .putBoolean("magiskhide", Global.Configs.magiskHide) + .putBoolean("busybox", Utils.commandExists("busybox")) + .putBoolean("hosts", Utils.itemExist(false, "/magisk/.core/hosts")) + .putBoolean("disable", Utils.itemExist(Global.MAGISK_DISABLE_FILE)) + .putString("su_request_timeout", String.valueOf(Global.Configs.suRequestTimeout)) + .putString("su_auto_response", String.valueOf(Global.Configs.suResponseType)) + .putString("su_notification", String.valueOf(Global.Configs.suNotificationType)) + .putString("su_access", String.valueOf(Global.Configs.suAccessState)) + .apply(); + return null; + } + } + public static class CheckUpdates extends NormalTask { @Override diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java b/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java index 7d0ae8cae..229ecb522 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java @@ -41,7 +41,7 @@ public class Shell { // Setup umask and PATH su("umask 022"); - su("PATH=/data/busybox:$PATH"); + su("PATH=`[ -e /dev/busybox ] && echo /dev/busybox || echo /data/busybox`:$PATH"); List ret = su("echo -BOC-", "id"); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9c9c74d2a..2357e449c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -17,6 +17,7 @@ Installed Magisk v%1$s + Magisk v%1$s disabled Magisk not installed Checking for updates… @@ -127,6 +128,8 @@ Clear Repo Cache Clear the cached information for online repos, forces the app to refresh online + Disable Magisk + Everything will be disabled except root (MagiskSU) Hide Magisk from various detections Enable BusyBox Bind mount Magisk\'s built-in busybox to xbin\nRequires reboot @@ -185,7 +188,5 @@ PID:\u0020 Target UID:\u0020 Command:\u0020 - Disable Magic Mount - Disable Magic Mount, which will prevent all modules to work diff --git a/app/src/main/res/xml/app_settings.xml b/app/src/main/res/xml/app_settings.xml index e4f73372d..7856dab78 100644 --- a/app/src/main/res/xml/app_settings.xml +++ b/app/src/main/res/xml/app_settings.xml @@ -26,7 +26,7 @@ android:title="@string/settings_disable_title" android:summary="@string/settings_disable_summary" /> -