From 1a5f868ecd0d278d574c12664ee95139c2423c17 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sun, 26 Nov 2023 05:11:58 +0100 Subject: [PATCH] feat: Select first Adb device, if none supplied automatically --- .../app/revanced/library/adb/AdbManager.kt | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/app/revanced/library/adb/AdbManager.kt b/src/main/kotlin/app/revanced/library/adb/AdbManager.kt index 632b30f..757378f 100644 --- a/src/main/kotlin/app/revanced/library/adb/AdbManager.kt +++ b/src/main/kotlin/app/revanced/library/adb/AdbManager.kt @@ -24,16 +24,23 @@ import java.util.logging.Logger /** * Adb manager. Used to install and uninstall [Apk] files. * - * @param deviceSerial The serial of the device. + * @param deviceSerial The serial of the device. If null, the first connected device will be used. */ -sealed class AdbManager private constructor(deviceSerial: String? = null) { +sealed class AdbManager private constructor(deviceSerial: String?) { protected val logger: Logger = Logger.getLogger(AdbManager::class.java.name) - protected val device = JadbConnection().devices.find { device -> device.serial == deviceSerial } - ?: throw DeviceNotFoundException(deviceSerial) + protected val device = with(JadbConnection().devices) { + if (isEmpty()) throw DeviceNotFoundException() + + deviceSerial?.let { + firstOrNull { it.serial == deviceSerial } ?: throw DeviceNotFoundException(deviceSerial) + } ?: first().also { + logger.warning("No device serial supplied. Using device with serial ${it.serial}") + } + }!! init { - logger.fine("Established connection to $deviceSerial") + logger.fine("Connected to ${device.serial}") } /** @@ -58,18 +65,23 @@ sealed class AdbManager private constructor(deviceSerial: String? = null) { /** * Gets an [AdbManager] for the supplied device serial. * - * @param deviceSerial The device serial. + * @param deviceSerial The device serial. If null, the first connected device will be used. * @param root Whether to use root or not. * @return The [AdbManager]. * @throws DeviceNotFoundException If the device can not be found. */ - fun getAdbManager(deviceSerial: String, root: Boolean = false): AdbManager = + fun getAdbManager(deviceSerial: String? = null, root: Boolean = false): AdbManager = if (root) RootAdbManager(deviceSerial) else UserAdbManager(deviceSerial) } - class RootAdbManager internal constructor(deviceSerial: String) : AdbManager(deviceSerial) { + /** + * Adb manager for rooted devices. + * + * @param deviceSerial The device serial. If null, the first connected device will be used. + */ + class RootAdbManager internal constructor(deviceSerial: String?) : AdbManager(deviceSerial) { init { - if (!device.hasSu()) throw IllegalArgumentException("Root required on $deviceSerial. Task failed") + if (!device.hasSu()) throw IllegalArgumentException("Root required on ${device.serial}. Task failed") } override fun install(apk: Apk) { @@ -116,7 +128,12 @@ sealed class AdbManager private constructor(deviceSerial: String? = null) { } } - class UserAdbManager internal constructor(deviceSerial: String) : AdbManager(deviceSerial) { + /** + * Adb manager for non-rooted devices. + * + * @param deviceSerial The device serial. If null, the first connected device will be used. + */ + class UserAdbManager internal constructor(deviceSerial: String?) : AdbManager(deviceSerial) { private val packageManager = PackageManager(device) override fun install(apk: Apk) { @@ -144,7 +161,7 @@ sealed class AdbManager private constructor(deviceSerial: String? = null) { */ class Apk(val file: File, val packageName: String? = null) - class DeviceNotFoundException internal constructor(deviceSerial: String?) : + class DeviceNotFoundException internal constructor(deviceSerial: String? = null) : Exception(deviceSerial?.let { "The device with the ADB device serial \"$deviceSerial\" can not be found" } ?: "No ADB device found")