From 7becd9619fed5924b31464f933beb0be59164db8 Mon Sep 17 00:00:00 2001 From: "Re:Shion" Date: Fri, 6 Oct 2023 18:39:37 +0800 Subject: [PATCH] Add KernelSU Support * This is an initial support for KernelSU, I am not sure it's works well or not, If it doesn't work well, please tell me. * Because of Substratum is no longer update now, I will not make a pull request to the upstream. * Still need improve, the way to detect KernelSU is not good as I expect. --- .../projekt/substratum/common/References.java | 36 +++++++++++++------ .../substratum/util/helpers/MagiskHelper.java | 8 +++-- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/projekt/substratum/common/References.java b/app/src/main/java/projekt/substratum/common/References.java index 50a02bf16..945d0b763 100644 --- a/app/src/main/java/projekt/substratum/common/References.java +++ b/app/src/main/java/projekt/substratum/common/References.java @@ -250,19 +250,35 @@ public static String getSystemDir() { return getMagiskDirectory() + "/system"; } + public static boolean isInteger(String str) { + try { + Integer.parseInt(str); + return true; + } catch (NumberFormatException e) { + return false; + } + } + public static String getMagiskDirectory() { - final int magiskVer = Integer.parseInt(Root.runCommand("su -V")); + final int suVer = Integer.parseInt(Root.runCommand("su -V")); if (magiskDir != null) - return magiskDir; - if (magiskVer >= 18000 && magiskVer <= 18100) { - magiskDir = "/sbin/.magisk/img/substratum"; - } else if (magiskVer >= 18101) { + return magiskDir; + String result = Root.runCommand("su -c \"magisk -V\""); + if (isInteger(result)){ + final int magiskVer = Integer.parseInt(Root.runCommand("su -c \"magisk -V\"")); + if (magiskVer >= 18000 && magiskVer <= 18100) { + magiskDir = "/sbin/.magisk/img/substratum"; + } else if (magiskVer >= 18101) { + magiskDir = "/data/adb/modules/substratum"; + } else { + Log.d("MagiskCheck", "Magisk version cannot be lesser than 18.0, switching to system-installation"); + magiskDir = "/"; + } + } + else { magiskDir = "/data/adb/modules/substratum"; - } else { - Log.d("MagiskCheck", "Magisk version cannot be lesser than 18.0, switching to system-installation"); - magiskDir = "/"; } - Log.d("MagiskCheck", String.format("Detected directory %s for version %d", magiskDir, magiskVer)); + Log.d("MagiskCheck", String.format("Detected directory %s for version %d", magiskDir, suVer)); return magiskDir; } @@ -954,4 +970,4 @@ protected Void doInBackground(Void... sUrl) { return null; } } -} \ No newline at end of file +} diff --git a/app/src/main/java/projekt/substratum/util/helpers/MagiskHelper.java b/app/src/main/java/projekt/substratum/util/helpers/MagiskHelper.java index f1d922ede..b81c92fce 100644 --- a/app/src/main/java/projekt/substratum/util/helpers/MagiskHelper.java +++ b/app/src/main/java/projekt/substratum/util/helpers/MagiskHelper.java @@ -23,14 +23,14 @@ public class MagiskHelper { private static void installModule(final Context context) { // Return if not using Magisk, when directory is "/" it means the version // is unsupported and we're falling back to modifying system. - if (!checkMagisk() || MAGISK_DIR.equals("/")) + if ((!checkMagisk() &&!checkKernelSU())|| MAGISK_DIR.equals("/")) return; Substratum.log(TAG, "Magisk module does not exist, creating!"); String command = "set -ex \n" + String.format("mkdir -p %s; ", MAGISK_DIR) + String.format( - "printf 'name=substratum\nversion=%s\nversionCode=%s\nauthor=substratum development team\ndescription=Systemless overlays for Substratum\nminMagisk=1500\n' > %s/module.prop; ", + "printf 'id=substratum\nname=substratum\nversion=%s\nversionCode=%s\nauthor=substratum development team\ndescription=Systemless overlays for Substratum\nminMagisk=1500\n' > %s/module.prop; ", BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE, MAGISK_DIR @@ -49,7 +49,9 @@ public static void handleModule(final Context context) { private static boolean checkMagisk() { return Root.runCommand("su --version").contains("MAGISKSU"); } - + private static boolean checkKernelSU() { + return Root.runCommand("su --version").contains("KernelSU"); + } private static boolean moduleExists() { return Root.runCommand(String.format("test -d %s && echo '1'", MAGISK_DIR)).equals("1"); }