@@ -15,6 +15,7 @@ import com.malinskiy.marathon.config.Configuration
15
15
import com.malinskiy.marathon.config.vendor.VendorConfiguration
16
16
import com.malinskiy.marathon.coroutines.newCoroutineExceptionHandler
17
17
import com.malinskiy.marathon.device.DeviceProvider
18
+ import com.malinskiy.marathon.device.DeviceProvider.DeviceEvent
18
19
import com.malinskiy.marathon.exceptions.NoDevicesException
19
20
import com.malinskiy.marathon.log.MarathonLogging
20
21
import com.malinskiy.marathon.time.Timer
@@ -52,6 +53,7 @@ class AdamDeviceProvider(
52
53
private val logger = MarathonLogging .logger(" AdamDeviceProvider" )
53
54
54
55
private val channel: Channel <DeviceProvider .DeviceEvent > = unboundedChannel()
56
+ private var scheduler: ReceiveChannel <DeviceProvider .DeviceEvent >? = null
55
57
56
58
private val dispatcher = newFixedThreadPoolContext(vendorConfiguration.threadingConfiguration.bootWaitingThreads, " DeviceMonitor" )
57
59
private val installDispatcher = Dispatchers .IO .limitedParallelism(vendorConfiguration.threadingConfiguration.installThreads)
@@ -210,7 +212,40 @@ class AdamDeviceProvider(
210
212
socketFactory.close()
211
213
}
212
214
213
- override fun subscribe () = channel
215
+ override fun subscribe (scheduler : ReceiveChannel <DeviceEvent >): ReceiveChannel <DeviceEvent > {
216
+ this .scheduler = scheduler
217
+ launch {
218
+ for (event in scheduler) {
219
+ processSchedulerEvents(event)
220
+ }
221
+ }
222
+ return channel
223
+ }
224
+
225
+ private suspend fun processSchedulerEvents (event : DeviceEvent ) {
226
+ when (event) {
227
+ is DeviceEvent .DeviceConnected -> Unit
228
+ is DeviceEvent .DeviceDisconnected -> {
229
+ if (event.device is AdamAndroidDevice ) {
230
+ val device = event.device as AdamAndroidDevice
231
+
232
+ val fullFormSerial = device.adbSerial.contains(" :" )
233
+ var host: String
234
+ var port: Int?
235
+ if (fullFormSerial) {
236
+ host = device.adbSerial.substringBefore(" :" )
237
+ port = device.adbSerial.substringAfter(" :" ).toIntOrNull()
238
+ } else {
239
+ host = device.adbSerial
240
+ port = null
241
+ }
242
+ device.client.execute(DisconnectDeviceRequest (host, port))
243
+ } else {
244
+ logger.warn { " Received $event from scheduler, expected device to be AdamAndroidDevice, but was ${event.device::class .java.simpleName} " }
245
+ }
246
+ }
247
+ }
248
+ }
214
249
}
215
250
216
251
data class ProvidedDevice (
0 commit comments