feat: Select first Adb device, if none supplied automatically

This commit is contained in:
oSumAtrIX
2023-11-26 05:11:58 +01:00
parent 969ca16b77
commit 1a5f868ecd

View File

@@ -24,16 +24,23 @@ import java.util.logging.Logger
/** /**
* Adb manager. Used to install and uninstall [Apk] files. * 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 logger: Logger = Logger.getLogger(AdbManager::class.java.name)
protected val device = JadbConnection().devices.find { device -> device.serial == deviceSerial } protected val device = with(JadbConnection().devices) {
?: throw DeviceNotFoundException(deviceSerial) 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 { 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. * 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. * @param root Whether to use root or not.
* @return The [AdbManager]. * @return The [AdbManager].
* @throws DeviceNotFoundException If the device can not be found. * @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) 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 { 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) { 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) private val packageManager = PackageManager(device)
override fun install(apk: Apk) { 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 Apk(val file: File, val packageName: String? = null)
class DeviceNotFoundException internal constructor(deviceSerial: String?) : class DeviceNotFoundException internal constructor(deviceSerial: String? = null) :
Exception(deviceSerial?.let { Exception(deviceSerial?.let {
"The device with the ADB device serial \"$deviceSerial\" can not be found" "The device with the ADB device serial \"$deviceSerial\" can not be found"
} ?: "No ADB device found") } ?: "No ADB device found")