public void initManager() { AndroidDebugBridge.init(false); Runtime.getRuntime().addShutdownHook(this); AndroidDebugBridge.addDebugBridgeChangeListener(this); AndroidDebugBridge.addDeviceChangeListener(this); }
public static AndroidDebugBridge startAdb(String adbExe, final DeviceListener listner) { try { Runtime.getRuntime().exec(adbExe + " start-server"); } catch (IOException ex) { LLog.i(ex + ", env has no adb"); } AndroidDebugBridge.initIfNeeded(); final AndroidDebugBridge bridge = AndroidDebugBridge.createBridge(); if (listner != null) { AndroidDebugBridge.addDeviceChangeListener(listner); } return bridge; }
public void usingDeviceChangeListener() throws Exception { AndroidDebugBridge.addDeviceChangeListener(new AndroidDebugBridge.IDeviceChangeListener() { // this gets invoked on another thread, but you probably shouldn't count on it public void deviceConnected(IDevice device) { System.out.println("* " + device.getSerialNumber()); } public void deviceDisconnected(IDevice device) { } public void deviceChanged(IDevice device, int changeMask) { } }); AndroidDebugBridge adb = AndroidDebugBridge.createBridge(); Thread.sleep(1000); if (!adb.isConnected()) { System.out.println("Couldn't connect to ADB server"); } AndroidDebugBridge.disconnectBridge(); }
@Override protected Boolean process(AndroidDevice device) throws Exception { final DeviceDiscovery deviceDiscovery = new DeviceDiscovery(); deviceDiscovery.setDevice(device); AndroidDebugBridge.addDeviceChangeListener(deviceDiscovery); try { getExecutionService().execute(sendEmulatorCommand(extractPortFromDevice(device), "kill")).awaitAtMost(10, TimeUnit.SECONDS); } catch (ExecutionException ex) { throw new AndroidExecutionException(String.format("Sending of kill command to emulator {0} was not completed " + "successfully in 10 seconds.", device.getSerialNumber()), ex); } boolean shuttedDown = false; try { shuttedDown = getExecutionService().execute(new Callable<Boolean>() { @Override public Boolean call() throws Exception { return deviceDiscovery.isOffline(); } }).reexecuteEvery(1, TimeUnit.SECONDS).until(countdown, new ExecutionCondition<Boolean>() { @Override public boolean satisfiedBy(Boolean offline) throws ExecutionException { return offline; } }); } catch (ExecutionException ex) { throw new AndroidExecutionException(String.format("Unable to get the emulator {0} down", device.getSerialNumber()), ex); } AndroidDebugBridge.removeDeviceChangeListener(deviceDiscovery); return shuttedDown; }
AndroidDebugBridge.addDeviceChangeListener(deviceDiscovery);
public void shutdownEmulator(@Observes AfterSuite event, AndroidExtensionConfiguration configuration, AndroidDevice device, ProcessExecutor executor) throws AndroidExecutionException { AndroidEmulator emulator = androidEmulator.get(); // we created the emulator, test shut it down if (emulator != null && device.isEmulator()) { final Process p = emulator.getProcess(); CountDownWatch countdown = new CountDownWatch(configuration.getEmulatorShutdownTimeoutInSeconds(), TimeUnit.SECONDS); log.log(Level.INFO, "Waiting {0} seconds for emulator {1} to be disconnected and shutdown.", new Object[] { countdown.timeout(), device.getAvdName() }); try { final DeviceDisconnectDiscovery listener = new DeviceDisconnectDiscovery(device); AndroidDebugBridge.addDeviceChangeListener(listener); stopEmulator(p, executor, device, countdown); waitUntilShutDownIsComplete(device, listener, executor, countdown); AndroidDebugBridge.removeDeviceChangeListener(listener); } finally { executor.removeShutdownHook(p); } } androidDeviceShutdown.fire(new AndroidDeviceShutdown(device)); }
public void createAndroidEmulator(@Observes AndroidVirtualDeviceAvailable event) throws AndroidExecutionException { if (!androidBridge.get().isConnected()) { throw new IllegalStateException("Android debug bridge must be connected in order to spawn the emulator"); } AndroidContainerConfiguration configuration = this.configuration.get(); AndroidDevice emulator = null; CountDownWatch countdown = new CountDownWatch(configuration.getEmulatorBootupTimeoutInSeconds(), TimeUnit.SECONDS); logger.log(Level.INFO, "Waiting {0} seconds for emulator {1} to be started and connected.", new Object[] { countdown.timeout(), configuration.getAvdName() }); ProcessExecutor emulatorProcessExecutor = this.executor.get(); DeviceConnectDiscovery deviceDiscovery = new DeviceConnectDiscovery(); AndroidDebugBridge.addDeviceChangeListener(deviceDiscovery); Process emulatorProcess = startEmulator(emulatorProcessExecutor); androidEmulator.set(new AndroidEmulator(emulatorProcess)); logger.log(Level.INFO, "Emulator process started, {0} seconds remaining to start the device {1}", new Object[] { countdown.timeLeft(), configuration.getAvdName() }); waitUntilBootUpIsComplete(deviceDiscovery, emulatorProcessExecutor, countdown); unlockEmulator(deviceDiscovery, emulatorProcessExecutor); emulator = deviceDiscovery.getDiscoveredDevice(); setDronePorts(emulator); AndroidDebugBridge.removeDeviceChangeListener(deviceDiscovery); androidDevice.set(emulator); screenshooter.set(new AndroidScreenshooter(emulator)); androidDeviceReady.fire(new AndroidDeviceReady(emulator)); }
public void shutdownEmulator(@Observes AndroidContainerStop event) throws AndroidExecutionException { AndroidEmulator emulator = androidEmulator.get(); AndroidDevice device = androidDevice.get(); AndroidContainerConfiguration configuration = this.configuration.get(); if (emulator != null && device.isEmulator()) { final ProcessExecutor executor = this.executor.get(); final Process p = emulator.getProcess(); CountDownWatch countdown = new CountDownWatch(configuration.getEmulatorShutdownTimeoutInSeconds(), TimeUnit.SECONDS); logger.info("Waiting " + countdown.timeout() + " seconds for emulator " + device.getAvdName() + " to be disconnected and shutdown."); try { final DeviceDisconnectDiscovery listener = new DeviceDisconnectDiscovery(device); AndroidDebugBridge.addDeviceChangeListener(listener); stopEmulator(p, executor, device, countdown); waitUntilShutDownIsComplete(device, listener, executor, countdown); AndroidDebugBridge.removeDeviceChangeListener(listener); if (configuration.isAVDGenerated()) { androidVirtualDeviceDelete.fire(new AndroidVirtualDeviceDelete()); } androidEmulatorShuttedDown.fire(new AndroidEmulatorShuttedDown(device)); } finally { executor.removeShutdownHook(p); } } }
AndroidDebugBridge.addDeviceChangeListener(deviceDiscovery);