if (!bridge.isConnected()) {
throw new IllegalStateException("Android debug bridge must be connected in order to spawn emulator");
}
String name = configuration.getAvdName();
AndroidDevice running = null;
for (AndroidDevice device : bridge.getDevices()) {
if (equalsIgnoreNulls(name, device.getAvdName())) {
running = device;
break;
}
}
if (running == null) {
CountDownWatch countdown = new CountDownWatch(configuration.getEmulatorBootupTimeoutInSeconds(), TimeUnit.SECONDS);
log.log(Level.INFO, "Waiting {0} seconds for emulator {1} to be started and connected.", new Object[] {
countdown.timeout(), name });
// discover what device was added here
DeviceConnectDiscovery deviceDiscovery = new DeviceConnectDiscovery();
AndroidDebugBridge.addDeviceChangeListener(deviceDiscovery);
Process emulator = startEmulator(executor, sdk, name, configuration.getEmulatorOptions());
androidEmulator.set(new AndroidEmulator(emulator));
log.log(Level.FINE, "Emulator process started, {0} seconds remaining to start the device {1}", new Object[] {
countdown.timeLeft(), name });
waitUntilBootUpIsComplete(deviceDiscovery, executor, sdk, countdown);
running = deviceDiscovery.getDiscoveredDevice();
AndroidDebugBridge.removeDeviceChangeListener(deviceDiscovery);
} else {
log.info("Emulator for device " + name + " is already started, device serial is " + running.getSerialNumber()
+ ". Emulator will not be reinitialized.");
}
// fire event that we have a device ready
androidDevice.set(running);