From 3c783448127f2ff75889f54c7477b5d8336cb961 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 28 Jun 2020 06:51:58 -0700 Subject: [PATCH] Refactor version handling --- app/build.gradle | 1 + .../java/com/topjohnwu/magisk/core/Const.kt | 8 +-- .../magisk/core/su/SuRequestHandler.kt | 3 +- .../topjohnwu/magisk/core/utils/PatchAPK.kt | 3 +- .../topjohnwu/magisk/core/utils/RootInit.kt | 49 ++++++++++--------- app/src/main/res/raw/manager.sh | 1 + 6 files changed, 36 insertions(+), 29 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index b219f445c..d2d75a75f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -22,6 +22,7 @@ android { multiDexEnabled true versionName props['appVersion'] versionCode props['appVersionCode'] as Integer + buildConfigField 'int', 'LATEST_MAGISK', props['versionCode'] ?: 'Integer.MAX_VALUE' javaCompileOptions { annotationProcessorOptions { diff --git a/app/src/main/java/com/topjohnwu/magisk/core/Const.kt b/app/src/main/java/com/topjohnwu/magisk/core/Const.kt index 62d700969..d643fc4fb 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/Const.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/Const.kt @@ -1,6 +1,7 @@ package com.topjohnwu.magisk.core import android.os.Process +import com.topjohnwu.magisk.BuildConfig object Const { @@ -24,9 +25,10 @@ object Const { object Version { const val MIN_VERSION = "v19.0" const val MIN_VERCODE = 19000 - const val PROVIDER_CONNECT = 20200 - const val DYNAMIC_PATH = 20400 - const val SEPOLICY_REDESIGN = 20416 + + fun atLeast_20_2() = Info.env.magiskVersionCode >= 20200 + fun atLeast_20_4() = Info.env.magiskVersionCode >= 20400 + fun atLeastCanary() = Info.env.magiskVersionCode > BuildConfig.LATEST_MAGISK / 100 * 100 } object ID { diff --git a/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt b/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt index 642960e56..fa4f4bd00 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt @@ -9,7 +9,6 @@ import androidx.collection.ArrayMap import com.topjohnwu.magisk.BuildConfig import com.topjohnwu.magisk.core.Config import com.topjohnwu.magisk.core.Const -import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.magiskdb.PolicyDao import com.topjohnwu.magisk.core.model.MagiskPolicy import com.topjohnwu.magisk.core.model.toPolicy @@ -38,7 +37,7 @@ abstract class SuRequestHandler( val name = intent.getStringExtra("socket") ?: return false try { - if (Info.env.magiskVersionCode >= Const.Version.SEPOLICY_REDESIGN) { + if (Const.Version.atLeastCanary()) { val server = LocalServerSocket(name) val futureSocket = Shell.EXECUTOR.submit(Callable { server.accept() }) try { diff --git a/app/src/main/java/com/topjohnwu/magisk/core/utils/PatchAPK.kt b/app/src/main/java/com/topjohnwu/magisk/core/utils/PatchAPK.kt index cca10a290..06ea0b54b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/utils/PatchAPK.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/utils/PatchAPK.kt @@ -103,8 +103,7 @@ object PatchAPK { } private fun patchAndHide(context: Context, label: String): Boolean { - val dlStub = !isRunningAsStub && SDK_INT >= 28 && - Info.env.magiskVersionCode >= Const.Version.PROVIDER_CONNECT + val dlStub = !isRunningAsStub && SDK_INT >= 28 && Const.Version.atLeast_20_2() val src = if (dlStub) { val stub = File(context.cacheDir, "stub.apk") val svc = get() diff --git a/app/src/main/java/com/topjohnwu/magisk/core/utils/RootInit.kt b/app/src/main/java/com/topjohnwu/magisk/core/utils/RootInit.kt index dca3d8a45..c323f3a24 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/utils/RootInit.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/utils/RootInit.kt @@ -16,30 +16,35 @@ class RootInit : Shell.Initializer() { } fun init(context: Context, shell: Shell): Boolean { - val job = shell.newJob() - if (Info.env.magiskVersionCode >= Const.Version.DYNAMIC_PATH) { - job.add("export ASH_STANDALONE=1") - .add("[ -x /data/adb/magisk/busybox ] && exec /data/adb/magisk/busybox sh") - .add("MAGISKTMP=$(magisk --path)/.magisk") - } else { - job.add("export PATH=\"/sbin/.magisk/busybox:\$PATH\"") - .add("MAGISKTMP=/sbin/.magisk") - } - job.add(context.rawResource(R.raw.manager)) - if (shell.isRoot) { - job.add(context.rawResource(R.raw.util_functions)) - } - job.add("mm_init").exec() + shell.newJob().apply { + if (Const.Version.atLeast_20_4()) { + add("MAGISKTMP=$(magisk --path)/.magisk") + } else { + add("MAGISKTMP=/sbin/.magisk") + } + if (Const.Version.atLeastCanary()) { + add("export ASH_STANDALONE=1") + add("[ -x /data/adb/magisk/busybox ] && exec /data/adb/magisk/busybox sh") + } else { + add("export PATH=\"\$MAGISKTMP/busybox:\$PATH\"") + } + add(context.rawResource(R.raw.manager)) + if (shell.isRoot) { + add(context.rawResource(R.raw.util_functions)) + } + add("mm_init") + }.exec() - fun getvar(name: String) = ShellUtils.fastCmd(shell, "echo \$$name").toBoolean() + fun getvar(name: String) = ShellUtils.fastCmd(shell, "echo \$$name") + fun getBool(name: String) = getvar(name).toBoolean() - Const.MAGISKTMP = ShellUtils.fastCmd(shell, "echo \$MAGISKTMP") - Info.keepVerity = getvar("KEEPVERITY") - Info.keepEnc = getvar("KEEPFORCEENCRYPT") - Info.isSAR = getvar("SYSTEM_ROOT") - Info.ramdisk = shell.newJob().add("check_boot_ramdisk").exec().isSuccess - Info.recovery = getvar("RECOVERYMODE") - Info.isAB = getvar("ISAB") + Const.MAGISKTMP = getvar("MAGISKTMP") + Info.keepVerity = getBool("KEEPVERITY") + Info.keepEnc = getBool("KEEPFORCEENCRYPT") + Info.isSAR = getBool("SYSTEM_ROOT") + Info.ramdisk = getBool("RAMDISKEXIST") + Info.recovery = getBool("RECOVERYMODE") + Info.isAB = getBool("ISAB") return true } diff --git a/app/src/main/res/raw/manager.sh b/app/src/main/res/raw/manager.sh index 34f2b19d9..a6048fa42 100644 --- a/app/src/main/res/raw/manager.sh +++ b/app/src/main/res/raw/manager.sh @@ -145,4 +145,5 @@ mm_init() { get_flags run_migrations SHA1=$(grep_prop SHA1 $MAGISKTMP/config) + check_boot_ramdisk && RAMDISKEXIST=true || RAMDISKEXIST=false }