summaryrefslogtreecommitdiff
path: root/device-provisioner/src/main/com/android/sdklib/deviceprovisioner/LocalEmulatorProvisionerPlugin.kt
blob: f1ac11c461226de01f117f7c1b4f3f8a7e75f19a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.sdklib.deviceprovisioner

import com.android.adblib.AdbSession
import com.android.adblib.ConnectedDevice
import com.android.adblib.deviceProperties
import com.android.adblib.serialNumber
import com.android.adblib.thisLogger
import com.android.adblib.tools.EmulatorConsole
import com.android.adblib.tools.localConsoleAddress
import com.android.adblib.tools.openEmulatorConsole
import com.android.adblib.utils.createChildScope
import com.android.annotations.concurrency.GuardedBy
import com.android.prefs.AndroidLocationsSingleton
import com.android.sdklib.SdkVersionInfo
import com.android.sdklib.deviceprovisioner.DeviceState.Connected
import com.android.sdklib.deviceprovisioner.DeviceState.Disconnected
import com.android.sdklib.devices.Abi
import com.android.sdklib.internal.avd.AvdInfo
import com.android.sdklib.repository.IdDisplay
import com.android.sdklib.repository.targets.SystemImage.ANDROID_TV_TAG
import com.android.sdklib.repository.targets.SystemImage.AUTOMOTIVE_PLAY_STORE_TAG
import com.android.sdklib.repository.targets.SystemImage.AUTOMOTIVE_TAG
import com.android.sdklib.repository.targets.SystemImage.GOOGLE_TV_TAG
import com.android.sdklib.repository.targets.SystemImage.WEAR_TAG
import java.io.IOException
import java.nio.file.Path
import java.time.Duration
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicReference
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.job
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext

/**
 * Provides access to emulators running on the local machine from the standard AVD directory.
 * Supports creating, editing, starting, and stopping AVD instances.
 *
 * This plugin creates device handles for all AVDs present in the standard AVD directory, running or
 * not. The AVD path is used to identify devices and establish the link between connected devices
 * and their handles. The directory is periodically rescanned to find new devices, and immediately
 * rescanned after an edit is made via a device action.
 */
class LocalEmulatorProvisionerPlugin(
  private val scope: CoroutineScope,
  private val adbSession: AdbSession,
  private val avdManager: AvdManager,
  rescanPeriod: Duration = Duration.ofSeconds(10)
) : DeviceProvisionerPlugin {
  val logger = thisLogger(adbSession)

  /**
   * An abstraction of the AvdManager / AvdManagerConnection classes to be injected, allowing for
   * testing and decoupling from Studio.
   */
  interface AvdManager {
    suspend fun rescanAvds(): List<AvdInfo>
    suspend fun createAvd(): Boolean
    suspend fun editAvd(avdInfo: AvdInfo): Boolean
    suspend fun startAvd(avdInfo: AvdInfo)
    suspend fun stopAvd(avdInfo: AvdInfo)
  }

  // We can identify local emulators reliably, so this can be relatively high priority.
  override val priority = 100

  private val mutex = Mutex()
  @GuardedBy("mutex") private val deviceHandles = HashMap<Path, LocalEmulatorDeviceHandle>()

  private val _devices = MutableStateFlow<List<DeviceHandle>>(emptyList())
  override val devices: StateFlow<List<DeviceHandle>> = _devices.asStateFlow()

  private val emulatorConsoles = ConcurrentHashMap<ConnectedDevice, EmulatorConsole>()

  // TODO: Consider if it would be better to use a filesystem watcher here instead of polling.
  private val avdScanner = PeriodicAction(scope, rescanPeriod, ::rescanAvds)

  init {
    avdScanner.runNow()

    scope.coroutineContext.job.invokeOnCompletion {
      avdScanner.cancel()
      emulatorConsoles.values.forEach { it.close() }
    }
  }

  /**
   * Scans the AVDs on disk and updates our devices.
   *
   * Do not call directly; this should only be called by PeriodicAction.
   */
  private suspend fun rescanAvds() {
    val avdsOnDisk = avdManager.rescanAvds().associateBy { it.dataFolderPath }
    mutex.withLock {
      // Remove any current DeviceHandles that are no longer present on disk, unless they are
      // connected. (If a client holds on to the disconnected device handle, and it gets
      // recreated with the same path, the client will get a new device handle, which is fine.)
      deviceHandles.entries.removeIf { (path, handle) ->
        !avdsOnDisk.containsKey(path) && handle.state is Disconnected
      }

      for ((path, avdInfo) in avdsOnDisk) {
        when (val handle = deviceHandles[path]) {
          null ->
            deviceHandles[path] =
              LocalEmulatorDeviceHandle(
                scope.createChildScope(isSupervisor = true),
                Disconnected(toDeviceProperties(avdInfo)),
                avdInfo
              )
          else ->
            // Update the avdInfo if we're not currently running. If we are running, the old
            // values are probably still in effect, but we will update on the next scan after
            // shutdown.
            if (handle.avdInfo != avdInfo && handle.state is Disconnected) {
              handle.avdInfo = avdInfo
              handle.stateFlow.value = Disconnected(toDeviceProperties(avdInfo))
            }
        }
      }

      _devices.value = deviceHandles.values.toList()
    }
  }

  override suspend fun claim(device: ConnectedDevice): DeviceHandle? {
    val result = LOCAL_EMULATOR_REGEX.matchEntire(device.serialNumber) ?: return null
    val port = result.groupValues[1].toIntOrNull() ?: return null

    val emulatorConsole =
      adbSession.openEmulatorConsole(
        localConsoleAddress(port),
        AndroidLocationsSingleton.userHomeLocation.resolve(".emulator_console_auth_token")
      )
    emulatorConsoles[device] = emulatorConsole

    // This will fail on emulator versions prior to 30.0.18.
    val pathResult = kotlin.runCatching { emulatorConsole.avdPath() }
    val path = pathResult.getOrNull()

    if (path == null) {
      // If we can't connect to the emulator console, this isn't operationally a local emulator
      logger.debug { "Unable to read path for device ${device.serialNumber} from emulator console" }
      emulatorConsoles.remove(device)?.close()
      return null
    }

    // Try to link this device to an existing handle.
    var handle = tryConnect(path, port, device)
    if (handle == null) {
      // We didn't read this path from disk yet. Rescan and try again.
      avdScanner.runNow().join()
      handle = tryConnect(path, port, device)
    }
    if (handle == null) {
      // Apparently this emulator is not on disk, or it is not in the directory that we scan for
      // AVDs. (Perhaps GMD or Crow failed to pick it up.)
      logger.debug { "Unexpected device at $path" }
      emulatorConsoles.remove(device)?.close()
      return null
    }

    // We need to make sure that emulators change to Disconnected state once they are terminated.
    device.invokeOnDisconnection {
      handle.stateFlow.value = Disconnected(handle.state.properties)
      logger.debug { "Device ${device.serialNumber} closed; disconnecting from console" }
      emulatorConsoles.remove(device)?.close()
    }

    logger.debug { "Linked ${device.serialNumber} to AVD at $path" }

    return handle
  }

  private suspend fun tryConnect(
    path: Path,
    port: Int,
    device: ConnectedDevice
  ): LocalEmulatorDeviceHandle? =
    mutex.withLock {
      val handle = deviceHandles[path] ?: return@withLock null
      // For the offline device, we got most properties from the AvdInfo, though we had to
      // compute androidRelease. Now read them from the device.
      val deviceProperties = device.deviceProperties().all().asMap()
      val properties =
        LocalEmulatorProperties.build {
          readCommonProperties(deviceProperties)
          avdName = handle.avdInfo.name
          displayName = handle.avdInfo.displayName
          disambiguator = port.toString()
          wearPairingId = path.toString()
        }
      handle.stateFlow.value = Connected(properties, device)
      handle
    }

  private fun toDeviceProperties(avdInfo: AvdInfo) =
    LocalEmulatorProperties.build {
      manufacturer = avdInfo.deviceManufacturer
      model = avdInfo.deviceName
      androidVersion = avdInfo.androidVersion
      androidRelease = SdkVersionInfo.getVersionString(avdInfo.androidVersion.apiLevel)
      abi = Abi.getEnum(avdInfo.abiType)
      avdName = avdInfo.name
      displayName = avdInfo.displayName
      deviceType = avdInfo.tag.toDeviceType()
      wearPairingId = avdInfo.id
    }

  private fun IdDisplay.toDeviceType(): DeviceType =
    when (this) {
      ANDROID_TV_TAG,
      GOOGLE_TV_TAG -> DeviceType.TV
      AUTOMOTIVE_TAG,
      AUTOMOTIVE_PLAY_STORE_TAG -> DeviceType.AUTOMOTIVE
      WEAR_TAG -> DeviceType.WEAR
      else -> DeviceType.HANDHELD
    }

  private fun refreshDevices() {
    avdScanner.runNow()
  }

  override val createDeviceAction =
    object : CreateDeviceAction {
      override val label = "Create AVD"
      override val isEnabled = MutableStateFlow(true).asStateFlow()

      override suspend fun create() {
        if (avdManager.createAvd()) {
          refreshDevices()
        }
      }
    }

  /**
   * A handle for a local AVD stored in the SDK's AVD directory. These are only created when reading
   * an AVD off the disk; only devices that have already been read from disk will be claimed.
   */
  private inner class LocalEmulatorDeviceHandle(
    override val scope: CoroutineScope,
    initialState: DeviceState,
    initialAvdInfo: AvdInfo
  ) : DeviceHandle {
    override val stateFlow = MutableStateFlow(initialState)

    private val _avdInfo = AtomicReference(initialAvdInfo)

    /** AvdInfo can be updated when the device is edited on-disk and rescanned. */
    var avdInfo: AvdInfo
      get() = _avdInfo.get()
      set(value) = _avdInfo.set(value)

    /** The emulator console is present when the device is connected. */
    val emulatorConsole: EmulatorConsole?
      get() = state.connectedDevice?.let { emulatorConsoles[it] }

    override val activationAction =
      object : ActivationAction {
        override val label: String = "Start"

        override val isEnabled =
          stateFlow
            .map { it is Disconnected && !it.isTransitioning }
            .stateIn(scope, SharingStarted.WhileSubscribed(), false)

        override suspend fun activate(params: ActivationParams) {
          try {
            withContext(scope.coroutineContext) {
              stateFlow.advanceStateWithTimeout(
                timeout = CONNECTION_TIMEOUT,
                updateState = {
                  (it as? Disconnected)?.let {
                    Disconnected(it.properties, isTransitioning = true, status = "Starting up")
                  }
                },
                advanceAction = { avdManager.startAvd(avdInfo) }
              )
            }
          } catch (e: TimeoutCancellationException) {
            logger.warn("Emulator failed to connect within $CONNECTION_TIMEOUT_MINUTES minutes")
          }
        }
      }

    override val editAction =
      object : EditAction {
        override val label = "Edit"

        override val isEnabled = MutableStateFlow(true).asStateFlow()

        override suspend fun edit() {
          if (avdManager.editAvd(avdInfo)) {
            refreshDevices()
          }
        }
      }

    override val deactivationAction: DeactivationAction =
      object : DeactivationAction {
        override val label = "Stop"

        // We could check this with AvdManagerConnection.isAvdRunning, but that's expensive, and if
        // it's not running we should see it from ADB anyway
        override val isEnabled =
          stateFlow
            .map { it is Connected && !it.isTransitioning }
            .stateIn(scope, SharingStarted.WhileSubscribed(), false)

        override suspend fun deactivate() {
          try {
            withContext(scope.coroutineContext) {
              stateFlow.advanceStateWithTimeout(
                timeout = DISCONNECTION_TIMEOUT,
                updateState = {
                  // TODO: In theory, we could cancel from the Connecting state, but that would
                  // require a lot of work in AvdManagerConnection to make everything shutdown
                  // cleanly.
                  (it as? Connected)?.let {
                    Connected(
                      it.properties,
                      isTransitioning = true,
                      status = "Shutting down",
                      connectedDevice = it.connectedDevice
                    )
                  }
                },
                advanceAction = ::stop
              )
            }
          } catch (e: TimeoutCancellationException) {
            logger.warn(
              "Emulator failed to disconnect within $DISCONNECTION_TIMEOUT_MINUTES minutes"
            )
          }
        }
      }

    /**
     * Attempts to stop the AVD. We can either use the emulator console or AvdManager (which uses a
     * shell command to kill the process)
     */
    private suspend fun stop() {
      emulatorConsole?.let {
        try {
          it.kill()
          return
        } catch (e: IOException) {
          // Connection to emulator console is closed, possibly due to a harmless race condition.
          logger.debug(e) { "Failed to shutdown via emulator console; falling back to AvdManager" }
        }
      }
      avdManager.stopAvd(avdInfo)
    }
  }
}

class LocalEmulatorProperties(
  base: DeviceProperties,
  val avdName: String,
  val displayName: String
) : DeviceProperties by base {

  override val title = displayName

  companion object {
    fun build(block: Builder.() -> Unit) =
      Builder()
        .apply { isVirtual = true }
        .apply(block)
        .run {
          LocalEmulatorProperties(buildBase(), checkNotNull(avdName), checkNotNull(displayName))
        }
  }

  class Builder : DeviceProperties.Builder() {
    var avdName: String? = null
    var displayName: String? = null
  }
}

private val LOCAL_EMULATOR_REGEX = "emulator-(\\d+)".toRegex()

private const val CONNECTION_TIMEOUT_MINUTES: Long = 5
private val CONNECTION_TIMEOUT = Duration.ofMinutes(CONNECTION_TIMEOUT_MINUTES)

private const val DISCONNECTION_TIMEOUT_MINUTES: Long = 1
private val DISCONNECTION_TIMEOUT = Duration.ofMinutes(DISCONNECTION_TIMEOUT_MINUTES)