From b3b2149ebbfb46efb800347f74df8ef38b1d5cd2 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 5 Feb 2017 22:02:14 +0800 Subject: [PATCH] Optimize root shell and startups --- .../java/com/topjohnwu/magisk/Global.java | 41 ++++++++++++---- .../com/topjohnwu/magisk/SplashActivity.java | 25 +++++----- .../magisk/receivers/BootReceiver.java | 25 ++++------ .../com/topjohnwu/magisk/utils/Async.java | 47 ++++--------------- .../com/topjohnwu/magisk/utils/Shell.java | 18 +++---- .../com/topjohnwu/magisk/utils/Utils.java | 26 +++++----- 6 files changed, 85 insertions(+), 97 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/Global.java b/app/src/main/java/com/topjohnwu/magisk/Global.java index ee334017c..4674bc373 100644 --- a/app/src/main/java/com/topjohnwu/magisk/Global.java +++ b/app/src/main/java/com/topjohnwu/magisk/Global.java @@ -13,6 +13,7 @@ import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.ValueSortedMap; +import java.io.File; import java.util.List; public class Global { @@ -37,13 +38,6 @@ public class Global { public static List blockList; public static List appList; public static List magiskHideList; - public static void clear() { - repoMap = null; - moduleMap = null; - blockList = null; - appList = null; - magiskHideList = null; - } } public static class Events { public static final CallbackHandler.Event blockDetectionDone = new CallbackHandler.Event(); @@ -66,6 +60,29 @@ public class Global { public static int suResponseType; 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", new File("/magisk/.core/hosts").exists()) + .putBoolean("disable", Utils.itemExist(MAGISK_DISABLE_FILE)) + .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); @@ -85,7 +102,7 @@ public class Global { if (Utils.isValidShellResponse(ret)) Configs.suAccessState = Integer.parseInt(ret.get(0)); else { - Shell.su("setprop persist.sys.root_access 3"); + Shell.su(true, "setprop persist.sys.root_access 3"); Configs.suAccessState = 3; } } @@ -105,8 +122,12 @@ public class Global { } } ret = Shell.sh("getprop ro.magisk.disable"); - if (Utils.isValidShellResponse(ret)) - Info.disabled = Integer.parseInt(ret.get(0)) != 0; + try { + Info.disabled = Utils.isValidShellResponse(ret) && Integer.parseInt(ret.get(0)) != 0; + } catch (NumberFormatException e) { + Info.disabled = false; + } + } } diff --git a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java index 3448cdd64..d3bf60217 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java @@ -13,16 +13,15 @@ public class SplashActivity extends AppCompatActivity { super.onCreate(savedInstanceState); - // 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(); + // Init the info and configs and root shell + Global.init(getApplicationContext()); + + // Start MagiskHide if not started at boot + if (Global.Configs.magiskHide && !Global.Info.disabled && Global.Info.magiskVersion > 10.3) + new Async.MagiskHide().enable(); + + // Now fire all async tasks + new Async.LoadApps(getPackageManager()).exec(); new Async.GetBootBlocks().exec(); new Async.CheckUpdates().exec(); new Async.LoadModules() { @@ -32,6 +31,10 @@ public class SplashActivity extends AppCompatActivity { new Async.LoadRepos(getApplicationContext()).exec(); } }.exec(); - new Async.LoadApps(getPackageManager()).exec(); + + // Preparation done, now start main activity + Intent intent = new Intent(getApplicationContext(), MainActivity.class); + startActivity(intent); + finish(); } } 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 00a6adb6b..b7eec6ce4 100644 --- a/app/src/main/java/com/topjohnwu/magisk/receivers/BootReceiver.java +++ b/app/src/main/java/com/topjohnwu/magisk/receivers/BootReceiver.java @@ -12,24 +12,15 @@ import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.utils.Async; public class BootReceiver extends BroadcastReceiver { + @Override public void onReceive(Context context, Intent intent) { - 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(); + Global.initSuAccess(); + Global.updateMagiskInfo(); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + if (prefs.getBoolean("magiskhide", false) && !Global.Info.disabled && Global.Info.magiskVersion > 10.3) { + Toast.makeText(context, R.string.start_magiskhide, Toast.LENGTH_SHORT).show(); + new Async.MagiskHide(true).enable(); + } } } 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 a2910bd1e..4dafd1df7 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Async.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Async.java @@ -2,13 +2,11 @@ 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; @@ -51,40 +49,6 @@ 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 @@ -327,10 +291,19 @@ public class Async { } public static class MagiskHide extends RootTask { + + private boolean newShell = false; + + public MagiskHide() {} + + public MagiskHide(boolean b) { + newShell = b; + } + @Override protected Void doInBackground(Object... params) { String command = (String) params[0]; - Shell.su(MAGISK_HIDE_PATH + command); + Shell.su(newShell, MAGISK_HIDE_PATH + command); return null; } 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 229ecb522..8fa728b6a 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java @@ -15,16 +15,15 @@ public class Shell { // -1 = problematic/unknown issue; 0 = not rooted; 1 = properly rooted public static int rootStatus; + private static boolean isInit = false; private static Process rootShell; private static DataOutputStream rootSTDIN; private static StreamGobbler rootSTDOUT; - private static List rootOutList = new ArrayList<>(); + private static List rootOutList = Collections.synchronizedList(new ArrayList()); - static { - init(); - } + public static void init() { - private static void init() { + isInit = true; try { rootShell = Runtime.getRuntime().exec("su"); @@ -64,7 +63,7 @@ public class Shell { } public static boolean rootAccess() { - return rootStatus > 0; + return isInit && rootStatus > 0; } public static List sh(String... commands) { @@ -120,9 +119,12 @@ public class Shell { DataOutputStream STDIN; StreamGobbler STDOUT; - if (!rootAccess()) { + // Create the default shell if not init + if (!newShell && !isInit) + init(); + + if (!newShell && !rootAccess()) return null; - } if (newShell) { res = Collections.synchronizedList(new ArrayList()); 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 fb16d954b..8070fa112 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -25,18 +25,9 @@ public class Utils { public static boolean isDownloading = false; public static boolean itemExist(String path) { - return itemExist(true, path); - } - - public static boolean itemExist(boolean root, String path) { String command = "if [ -e " + path + " ]; then echo true; else echo false; fi"; - List ret; - if (Shell.rootAccess() && root) { - ret = Shell.su(command); - return isValidShellResponse(ret) && Boolean.parseBoolean(ret.get(0)); - } else { - return new File(path).exists(); - } + List ret = Shell.su(command); + return isValidShellResponse(ret) && Boolean.parseBoolean(ret.get(0)); } public static boolean commandExists(String s) { @@ -59,10 +50,8 @@ public class Utils { } public static List getModList(String path) { - List ret; String command = "find " + path + " -type d -maxdepth 1 ! -name \"*.core\" ! -name \"*lost+found\" ! -name \"*magisk\""; - ret = Shell.su(command); - return ret; + return Shell.su(command); } public static List readFile(String path) { @@ -151,4 +140,13 @@ public class Utils { return Integer.parseInt(prefs.getString(key, String.valueOf(def))); } + public static void checkAndStartMagiskHide() { + String command = "ps | grep magiskhide >/dev/null; echo $?"; + List ret = Shell.su(command); + if (!isValidShellResponse(ret)) + return; + if (Integer.parseInt(ret.get(0)) != 0) + new Async.MagiskHide().enable(); + } + } \ No newline at end of file