Skip to content

Commit

Permalink
fix: Check, if mounting is possible
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Sep 19, 2023
1 parent db50cee commit 3e13fb5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
6 changes: 6 additions & 0 deletions revanced-lib/api/revanced-lib.api
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion revanced-lib/src/main/kotlin/app/revanced/lib/adb/AdbManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
}
5 changes: 3 additions & 2 deletions revanced-lib/src/main/kotlin/app/revanced/lib/adb/Commands.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\" && " +
Expand Down

0 comments on commit 3e13fb5

Please sign in to comment.