feat: [ADB] device serial detection (#109)

* feat: [ADB] device serial detection

* feat: [ADB] device serial detection

* added device plugin

* sucessfully got the serial

* sucessfully got the serial

---------

Co-authored-by: Florent Champigny <florent@bere.al>
This commit is contained in:
Florent CHAMPIGNY 2025-08-15 22:35:46 +02:00 committed by GitHub
parent 08698acf25
commit 0321da0c4a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 294 additions and 4 deletions

View file

@ -19,6 +19,15 @@ object Protocol {
}
}
object Device {
const val Plugin = "device"
object Method {
const val RegisterDevice = "registerDevice"
}
}
object Database {
const val Plugin = "database"

View file

@ -4,8 +4,17 @@ import io.github.openflocon.domain.common.Either
interface AdbRepository {
fun getAdbSerial(deviceId: String) : String?
fun saveAdbSerial(deviceId: String, serial: String)
fun executeAdbCommand(adbPath: String, command: String): Either<Throwable, String>
fun executeAdbAskSerialToAllDevices(
adbPath: String,
command: String,
serialVariableName: String,
): Either<Throwable, String>
fun findAdbPath(): String?
}

View file

@ -12,6 +12,7 @@ import io.github.openflocon.domain.device.usecase.ObserveCurrentDeviceUseCase
import io.github.openflocon.domain.device.usecase.ObserveDevicesUseCase
import io.github.openflocon.domain.device.usecase.SelectDeviceAppUseCase
import io.github.openflocon.domain.device.usecase.SelectDeviceUseCase
import io.github.openflocon.domain.messages.usecase.HandleNewDeviceUseCase
import org.koin.core.module.dsl.factoryOf
import org.koin.dsl.module
@ -21,6 +22,7 @@ internal val deviceModule = module {
factoryOf(::GetCurrentDeviceIdUseCase)
factoryOf(::GetCurrentDeviceUseCase)
factoryOf(::HandleDeviceUseCase)
factoryOf(::HandleNewDeviceUseCase)
factoryOf(::ObserveCurrentDeviceIdUseCase)
factoryOf(::ObserveCurrentDeviceAppUseCase)
factoryOf(::ObserveCurrentDeviceIdAndPackageNameUseCase)

View file

@ -11,6 +11,7 @@ internal val messagesModule = module {
messagesRepository = get(),
plugins = getAll(),
handleDeviceUseCase = get(),
handleNewDeviceUseCase = get(),
)
}
factoryOf(::StartServerUseCase)

View file

@ -15,12 +15,21 @@ class HandleIncomingMessagesUseCase(
private val messagesRepository: MessagesRepository,
private val plugins: List<MessagesReceiverRepository>,
private val handleDeviceUseCase: HandleDeviceUseCase,
private val handleNewDeviceUseCase: HandleNewDeviceUseCase,
) {
operator fun invoke(): Flow<Unit> = messagesRepository
.listenMessages()
.onEach {
val handleDeviceResult = handleDeviceUseCase(device = getDevice(it))
if(handleDeviceResult.isNewDevice) {
handleNewDeviceUseCase(
deviceIdAndPackageName = DeviceIdAndPackageNameDomainModel(
deviceId = handleDeviceResult.deviceId,
packageName = it.appPackageName,
)
)
}
plugins.forEach { plugin ->
if (handleDeviceResult.isNewDevice) {
plugin.onNewDevice(

View file

@ -0,0 +1,32 @@
package io.github.openflocon.domain.messages.usecase
import io.github.openflocon.domain.adb.repository.AdbRepository
import io.github.openflocon.domain.device.models.DeviceIdAndPackageNameDomainModel
import io.github.openflocon.domain.settings.repository.SettingsRepository
class HandleNewDeviceUseCase(
private val adbRepository: AdbRepository,
private val settingsRepository: SettingsRepository,
) {
operator fun invoke(deviceIdAndPackageName: DeviceIdAndPackageNameDomainModel) {
// check if we have a match for serial + app/package
val serial = adbRepository.getAdbSerial(deviceIdAndPackageName.deviceId)
if (serial == null) {
val adbPath = settingsRepository.getAdbPath() ?: return
// if not send a message through adb
val serialVariableName = "FLOCON_SERIAL"
adbRepository.executeAdbAskSerialToAllDevices(
adbPath = adbPath,
command = buildString {
append("shell am broadcast")
append(" ")
append("-a \"io.github.openflocon.flocon.DETECT\"")
append(" ")
append("-n \"${deviceIdAndPackageName.packageName}/io.github.openflocon.flocon.FloconBroadcastReceiver\"")
append(" --es \"serial\" \"$serialVariableName\"")
},
serialVariableName = serialVariableName,
)
}
}
}