Skip to content

Commit

Permalink
add documentation to Desktop.kt && fix pattern compile at every getAt…
Browse files Browse the repository at this point in the history
…tachedDevicesByAdb function call
  • Loading branch information
Vladislav Sumin committed Nov 15, 2022
1 parent 1467593 commit 8ed449d
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,37 @@ class Desktop(

companion object {
private const val PAUSE_MS = 500L
private val DEVICE_PATTERN = Pattern.compile("^([a-zA-Z0-9\\-:.]+)(\\s+)(device)")
}

private val devices: MutableCollection<DeviceMirror> = mutableListOf()
private var isRunning = AtomicBoolean(false)

/**
* Start Desktop server.
* Blocking current thread while server working
* @throws IllegalStateException - if server already running
*/
fun startDevicesObservingSync() {
if (!isRunning.compareAndSet(false, true)) error("Desktop already running")
startDevicesObservingInternal()
}

/**
* Start Desktop server asynchronously
* @throws IllegalStateException - if server already running
*/
fun startDevicesObservingAsync() {
if (!isRunning.compareAndSet(false, true)) error("Desktop already running")
thread {
startDevicesObservingInternal()
}
}

/**
* Stop Desktop server
* @throws IllegalStateException - if server already stopped
*/
fun stopDevicesObserving() {
if (!isRunning.compareAndSet(true, false)) error("Desktop already stopped")
}
Expand Down Expand Up @@ -76,15 +90,14 @@ class Desktop(
}

private fun getAttachedDevicesByAdb(): List<String> {
val pattern = Pattern.compile("^([a-zA-Z0-9\\-:.]+)(\\s+)(device)")
val commandResult = adbCommandPerformer.perform("devices")
if (commandResult.status != ExecutorResultStatus.SUCCESS) {
return emptyList()
}
val adbDevicesCommandResult: String = commandResult.description
return adbDevicesCommandResult.lines()
.asSequence()
.map { pattern.matcher(it) }
.map { DEVICE_PATTERN.matcher(it) }
.filter { matcher -> matcher.find() }
.map { matcher -> matcher.group(1) }
.filter { foundEmulator -> presetEmulators.isEmpty() || presetEmulators.contains(foundEmulator) }
Expand Down

0 comments on commit 8ed449d

Please sign in to comment.