mirror of
https://github.com/openflocon/Flocon.git
synced 2026-05-02 03:00:10 +00:00
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:
parent
08698acf25
commit
0321da0c4a
25 changed files with 294 additions and 4 deletions
|
|
@ -19,6 +19,15 @@ object Protocol {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
object Device {
|
||||
const val Plugin = "device"
|
||||
|
||||
object Method {
|
||||
const val RegisterDevice = "registerDevice"
|
||||
}
|
||||
}
|
||||
|
||||
object Database {
|
||||
const val Plugin = "database"
|
||||
|
||||
|
|
|
|||
|
|
@ -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?
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ internal val messagesModule = module {
|
|||
messagesRepository = get(),
|
||||
plugins = getAll(),
|
||||
handleDeviceUseCase = get(),
|
||||
handleNewDeviceUseCase = get(),
|
||||
)
|
||||
}
|
||||
factoryOf(::StartServerUseCase)
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue