コード例 #1
0
  /**
   * Performs the callback action on the devices determined by {@link
   * #shouldDoWithThisDevice(com.android.ddmlib.IDevice)}
   *
   * @param deviceCallback the action to perform on each device
   * @throws org.apache.maven.plugin.MojoExecutionException in case there is a problem
   * @throws org.apache.maven.plugin.MojoFailureException in case there is a problem
   */
  protected void doWithDevices(final DeviceCallback deviceCallback)
      throws MojoExecutionException, MojoFailureException {
    final AndroidDebugBridge androidDebugBridge = initAndroidDebugBridge();

    if (!androidDebugBridge.isConnected()) {
      throw new MojoExecutionException("Android Debug Bridge is not connected.");
    }

    waitForInitialDeviceList(androidDebugBridge);
    List<IDevice> devices = Arrays.asList(androidDebugBridge.getDevices());
    int numberOfDevices = devices.size();
    getLog().info("Found " + numberOfDevices + " devices connected with the Android Debug Bridge");
    if (devices.size() == 0) {
      throw new MojoExecutionException("No online devices attached.");
    }

    boolean shouldRunOnAllDevices = StringUtils.isBlank(device);
    if (shouldRunOnAllDevices) {
      getLog().info("android.device parameter not set, using all attached devices");
    } else {
      getLog().info("android.device parameter set to " + device);
    }

    ArrayList<DoThread> doThreads = new ArrayList<DoThread>();
    for (final IDevice idevice : devices) {
      if (shouldRunOnAllDevices) {
        String deviceType = idevice.isEmulator() ? "Emulator " : "Device ";
        getLog().info(deviceType + DeviceHelper.getDescriptiveName(idevice) + " found.");
      }
      if (shouldRunOnAllDevices || shouldDoWithThisDevice(idevice)) {
        DoThread deviceDoThread =
            new DoThread() {
              public void runDo() throws MojoFailureException, MojoExecutionException {
                deviceCallback.doWithDevice(idevice);
              }
            };
        doThreads.add(deviceDoThread);
        deviceDoThread.start();
      }
    }

    joinAllThreads(doThreads);
    throwAnyDoThreadErrors(doThreads);

    if (!shouldRunOnAllDevices && doThreads.isEmpty()) {
      throw new MojoExecutionException("No device found for android.device=" + device);
    }
  }