From aa75c8e5e416ec6f607b08cd7f80c5a89df2af35 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 8 Dec 2017 23:38:03 +0800 Subject: [PATCH] Fix issues of repackaging with multiuser --- .../com/topjohnwu/magisk/MagiskManager.java | 2 +- .../com/topjohnwu/magisk/SplashActivity.java | 4 +-- .../topjohnwu/magisk/asyncs/HideManager.java | 4 +-- .../magisk/asyncs/InstallMagisk.java | 5 ++-- .../com/topjohnwu/magisk/utils/Const.java | 2 +- .../com/topjohnwu/magisk/utils/Utils.java | 26 +++++++++---------- 6 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/topjohnwu/magisk/MagiskManager.java b/src/main/java/com/topjohnwu/magisk/MagiskManager.java index 3dfda6d5d..df67eb2a7 100644 --- a/src/main/java/com/topjohnwu/magisk/MagiskManager.java +++ b/src/main/java/com/topjohnwu/magisk/MagiskManager.java @@ -116,8 +116,8 @@ public class MagiskManager extends Application { } else { String pkg = suDB.getStrings(Const.Key.SU_REQUESTER, null); if (pkg != null) { - Shell.su_raw("pm uninstall " + pkg); suDB.setStrings(Const.Key.SU_REQUESTER, null); + Shell.su_raw("pm uninstall " + pkg); } } diff --git a/src/main/java/com/topjohnwu/magisk/SplashActivity.java b/src/main/java/com/topjohnwu/magisk/SplashActivity.java index d81889565..774603e84 100644 --- a/src/main/java/com/topjohnwu/magisk/SplashActivity.java +++ b/src/main/java/com/topjohnwu/magisk/SplashActivity.java @@ -47,9 +47,7 @@ public class SplashActivity extends Activity { } mm.loadMagiskInfo(); - if (Utils.itemExist(Const.MANAGER_CONFIGS)) { - Utils.loadPrefs(); - } + Utils.loadPrefs(); LoadModules loadModuleTask = new LoadModules(); diff --git a/src/main/java/com/topjohnwu/magisk/asyncs/HideManager.java b/src/main/java/com/topjohnwu/magisk/asyncs/HideManager.java index 3be804b5b..d1448d63f 100644 --- a/src/main/java/com/topjohnwu/magisk/asyncs/HideManager.java +++ b/src/main/java/com/topjohnwu/magisk/asyncs/HideManager.java @@ -123,9 +123,7 @@ public class HideManager extends ParallelTask { // Install the application - List ret = Shell.su(String.format(Locale.US, - "pm install --user %d %s >/dev/null && echo true || echo false", - mm.userId, repack)); + List ret = Shell.su(Utils.fmt("pm install %s >/dev/null && echo true || echo false", repack)); repack.delete(); if (!Utils.isValidShellResponse(ret) || !Boolean.parseBoolean(ret.get(0))) return false; diff --git a/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java b/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java index 78076dc01..0cf4d830b 100644 --- a/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java +++ b/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java @@ -188,8 +188,9 @@ public class InstallMagisk extends ParallelTask { // Patch boot image shell.run(console, logs, "cd " + install, - "KEEPFORCEENCRYPT=" + mKeepEnc + " KEEPVERITY=" + mKeepVerity + " HIGHCOMP=" + highCompression + - " sh update-binary indep boot_patch.sh " + boot + " || echo 'Failed!'"); + Utils.fmt("KEEPFORCEENCRYPT=%b KEEPVERITY=%b HIGHCOMP=%b " + + "sh update-binary indep boot_patch.sh %s || echo 'Failed!'", + mKeepEnc, mKeepVerity, highCompression, boot)); if (TextUtils.equals(console.get(console.size() - 1), "Failed!")) return false; diff --git a/src/main/java/com/topjohnwu/magisk/utils/Const.java b/src/main/java/com/topjohnwu/magisk/utils/Const.java index c1dcfcb0e..81c2c5811 100644 --- a/src/main/java/com/topjohnwu/magisk/utils/Const.java +++ b/src/main/java/com/topjohnwu/magisk/utils/Const.java @@ -25,7 +25,7 @@ public class Const { public static final String TMP_FOLDER_PATH = "/dev/tmp"; public static final String MAGISK_LOG = "/cache/magisk.log"; public static final File EXTERNAL_PATH = new File(Environment.getExternalStorageDirectory(), "MagiskManager"); - public static final String MANAGER_CONFIGS = "/data/.tmp.magisk.config"; + public static final String MANAGER_CONFIGS = ".tmp.magisk.config"; public static String BUSYBOX_PATH() { if (Utils.itemExist("/sbin/.core/busybox/busybox")) { diff --git a/src/main/java/com/topjohnwu/magisk/utils/Utils.java b/src/main/java/com/topjohnwu/magisk/utils/Utils.java index 1473f99bc..e362a635f 100644 --- a/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -46,25 +46,21 @@ public class Utils { public static boolean isDownloading = false; public static boolean itemExist(String path) { - String command = "[ -e " + path + " ] && echo true || echo false"; - List ret = Shell.su(command); + List ret = Shell.su(fmt("[ -e %s ] && echo true || echo false", path)); return isValidShellResponse(ret) && Boolean.parseBoolean(ret.get(0)); } public static void createFile(String path) { String folder = path.substring(0, path.lastIndexOf('/')); - String command = "mkdir -p " + folder + " 2>/dev/null; touch " + path + " 2>/dev/null;"; - Shell.su_raw(command); + Shell.su_raw(fmt("mkdir -p %s 2>/dev/null; touch %s 2>/dev/null", folder, path)); } public static void removeItem(String path) { - String command = "rm -rf " + path + " 2>/dev/null"; - Shell.su_raw(command); + Shell.su_raw(fmt("rm -rf %s 2>/dev/null", path)); } public static List readFile(String path) { - String command = "cat " + path + " | sed '$a\\ ' | sed '$d'"; - return Shell.su(command); + return Shell.su(fmt("cat %s | sed '$a\\ ' | sed '$d'", path)); } public static void dlAndReceive(Context context, DownloadReceiver receiver, String link, String filename) { @@ -256,16 +252,16 @@ public class Utils { } public static void dumpPrefs() { - Map prefMap = MagiskManager.get().prefs.getAll(); Gson gson = new Gson(); - String json = gson.toJson(prefMap, new TypeToken>(){}.getType()); - Shell.su("echo '" + json + "' > " + Const.MANAGER_CONFIGS); + String json = gson.toJson(MagiskManager.get().prefs.getAll(), new TypeToken>(){}.getType()); + Shell.su(fmt("for usr in /data/user/*; do echo '%s' > ${usr}/%s; done", json, Const.MANAGER_CONFIGS)); } public static void loadPrefs() { - List ret = Utils.readFile(Const.MANAGER_CONFIGS); + String config = fmt("/data/user/%d/%s", MagiskManager.get().userId, Const.MANAGER_CONFIGS); + List ret = readFile(config); if (isValidShellResponse(ret)) { - removeItem(Const.MANAGER_CONFIGS); + removeItem(config); SharedPreferences.Editor editor = MagiskManager.get().prefs.edit(); String json = ret.get(0); Gson gson = new Gson(); @@ -286,4 +282,8 @@ public class Utils { MagiskManager.get().loadConfig(); } } + + public static String fmt(String fmt, Object... args) { + return String.format(Locale.US, fmt, args); + } } \ No newline at end of file