From 3e13fb5d56eb2a90c2a4a1ddfc05852b1f70add5 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Tue, 19 Sep 2023 04:53:22 +0200 Subject: [PATCH] fix: Check, if mounting is possible --- revanced-lib/api/revanced-lib.api | 6 ++++++ .../main/kotlin/app/revanced/lib/adb/AdbManager.kt | 14 +++++++++++++- .../main/kotlin/app/revanced/lib/adb/Commands.kt | 5 +++-- .../main/kotlin/app/revanced/lib/adb/Constants.kt | 3 ++- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/revanced-lib/api/revanced-lib.api b/revanced-lib/api/revanced-lib.api index 3da46041..de36951b 100644 --- a/revanced-lib/api/revanced-lib.api +++ b/revanced-lib/api/revanced-lib.api @@ -47,6 +47,12 @@ public final class app/revanced/lib/adb/AdbManager$Companion { public final class app/revanced/lib/adb/AdbManager$DeviceNotFoundException : java/lang/Exception { } +public final class app/revanced/lib/adb/AdbManager$FailedToFindInstalledPackageException : java/lang/Exception { +} + +public final class app/revanced/lib/adb/AdbManager$PackageNameRequiredException : java/lang/Exception { +} + public final class app/revanced/lib/adb/AdbManager$RootAdbManager : app/revanced/lib/adb/AdbManager { public static final field Utils Lapp/revanced/lib/adb/AdbManager$RootAdbManager$Utils; public fun install (Lapp/revanced/lib/adb/AdbManager$Apk;)V diff --git a/revanced-lib/src/main/kotlin/app/revanced/lib/adb/AdbManager.kt b/revanced-lib/src/main/kotlin/app/revanced/lib/adb/AdbManager.kt index c3443b3f..9529cb71 100644 --- a/revanced-lib/src/main/kotlin/app/revanced/lib/adb/AdbManager.kt +++ b/revanced-lib/src/main/kotlin/app/revanced/lib/adb/AdbManager.kt @@ -10,6 +10,7 @@ import app.revanced.lib.adb.Constants.MOUNT_PATH import app.revanced.lib.adb.Constants.MOUNT_SCRIPT import app.revanced.lib.adb.Constants.PATCHED_APK_PATH import app.revanced.lib.adb.Constants.PLACEHOLDER +import app.revanced.lib.adb.Constants.RESOLVE_ACTIVITY import app.revanced.lib.adb.Constants.RESTART import app.revanced.lib.adb.Constants.TMP_PATH import app.revanced.lib.adb.Constants.UMOUNT @@ -74,9 +75,14 @@ sealed class AdbManager private constructor(deviceSerial: String? = null) { logger.info("Installing by mounting") val applyReplacement = getPlaceholderReplacement( - apk.packageName ?: throw IllegalArgumentException("Package name is required") + apk.packageName ?: throw PackageNameRequiredException() ) + device.run(RESOLVE_ACTIVITY.applyReplacement()).inputStream.bufferedReader().readLine().let { line -> + if (line != "No activity found") return@let + throw throw FailedToFindInstalledPackageException(apk.packageName) + } + device.push(apk.file, TMP_PATH) device.run("$CREATE_DIR $INSTALLATION_PATH") @@ -142,4 +148,10 @@ sealed class AdbManager private constructor(deviceSerial: String? = null) { Exception(deviceSerial?.let { "The device with the ADB device serial \"$deviceSerial\" can not be found" } ?: "No ADB device found") + + class FailedToFindInstalledPackageException internal constructor(packageName: String) : + Exception("Failed to find installed package \"$packageName\" because no activity was found") + + class PackageNameRequiredException internal constructor() : + Exception("Package name is required") } \ No newline at end of file diff --git a/revanced-lib/src/main/kotlin/app/revanced/lib/adb/Commands.kt b/revanced-lib/src/main/kotlin/app/revanced/lib/adb/Commands.kt index 8185b453..371da099 100644 --- a/revanced-lib/src/main/kotlin/app/revanced/lib/adb/Commands.kt +++ b/revanced-lib/src/main/kotlin/app/revanced/lib/adb/Commands.kt @@ -2,6 +2,7 @@ package app.revanced.lib.adb import se.vidstige.jadb.JadbDevice import se.vidstige.jadb.RemoteFile +import se.vidstige.jadb.ShellProcess import se.vidstige.jadb.ShellProcessBuilder import java.io.File @@ -15,8 +16,8 @@ internal fun JadbDevice.buildCommand(command: String, su: Boolean = true): Shell return shellProcessBuilder(cmd, *args.toTypedArray()) } -internal fun JadbDevice.run(command: String, su: Boolean = true): Int { - return this.buildCommand(command, su).start().waitFor() +internal fun JadbDevice.run(command: String, su: Boolean = true): ShellProcess { + return this.buildCommand(command, su).start()!! } internal fun JadbDevice.hasSu() = diff --git a/revanced-lib/src/main/kotlin/app/revanced/lib/adb/Constants.kt b/revanced-lib/src/main/kotlin/app/revanced/lib/adb/Constants.kt index 620a2892..02691d2a 100644 --- a/revanced-lib/src/main/kotlin/app/revanced/lib/adb/Constants.kt +++ b/revanced-lib/src/main/kotlin/app/revanced/lib/adb/Constants.kt @@ -10,7 +10,8 @@ internal object Constants { internal const val DELETE = "rm -rf $PLACEHOLDER" internal const val CREATE_DIR = "mkdir -p" - internal const val RESTART = "pm resolve-activity --brief $PLACEHOLDER | tail -n 1 | " + + internal const val RESOLVE_ACTIVITY = "pm resolve-activity --brief $PLACEHOLDER" + internal const val RESTART = "$RESOLVE_ACTIVITY | tail -n 1 | " + "xargs am start -n && kill ${'$'}(pidof -s $PLACEHOLDER)" internal const val INSTALL_PATCHED_APK = "base_path=\"$PATCHED_APK_PATH\" && " +