com.android.ddmlib.IDevice.DeviceState Java Examples

The following examples show how to use com.android.ddmlib.IDevice.DeviceState. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AdbShellCommandTaskTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
public void commandExecution_timeoutException() {
  FakeDevice fakeDevice =
      FakeDevice.fromDeviceSpec("id1", DeviceState.ONLINE, lDeviceWithLocales("en-US"));
  fakeDevice.injectShellCommandOutput(
      "getprop",
      () -> {
        throw new TimeoutException("Timeout");
      });

  // unknown command will cause timeout.
  AdbShellCommandTask task = new AdbShellCommandTask(fakeDevice, "getprop");
  Throwable e = assertThrows(CommandExecutionException.class, () -> task.execute());
  assertThat(e).hasMessageThat().contains("Timeout while executing 'adb shell");
  assertThat(e).hasCauseThat().isInstanceOf(TimeoutException.class);
}
 
Example #2
Source File: DeviceMonitor.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@NonNull
public static DeviceListComparisonResult compare(@NonNull List<? extends IDevice> previous,
        @NonNull List<? extends IDevice> current) {
    current = Lists.newArrayList(current);

    final Map<IDevice,DeviceState> updated = Maps.newHashMapWithExpectedSize(current.size());
    final List<IDevice> added = Lists.newArrayListWithExpectedSize(1);
    final List<IDevice> removed = Lists.newArrayListWithExpectedSize(1);

    for (IDevice device : previous) {
        IDevice currentDevice = find(current, device);
        if (currentDevice != null) {
            if (currentDevice.getState() != device.getState()) {
                updated.put(device, currentDevice.getState());
            }
            current.remove(currentDevice);
        } else {
            removed.add(device);
        }
    }

    added.addAll(current);

    return new DeviceListComparisonResult(updated, added, removed);
}
 
Example #3
Source File: InstallApksCommandTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
@Theory
public void badAbisDevice_throws(@FromDataPoints("apksInDirectory") boolean apksInDirectory)
    throws Exception {
  Path apksFile =
      createApks(createSimpleTableOfContent(ZipPath.create("base-master.apk")), apksInDirectory);

  DeviceSpec deviceSpec = mergeSpecs(sdkVersion(21), density(480), abis(), locales("en-US"));
  FakeDevice fakeDevice = FakeDevice.fromDeviceSpec(DEVICE_ID, DeviceState.ONLINE, deviceSpec);
  AdbServer adbServer =
      new FakeAdbServer(/* hasInitialDeviceList= */ true, ImmutableList.of(fakeDevice));

  InstallApksCommand command =
      InstallApksCommand.builder()
          .setApksArchivePath(apksFile)
          .setAdbPath(adbPath)
          .setAdbServer(adbServer)
          .build();

  Throwable exception = assertThrows(IllegalStateException.class, () -> command.execute());
  assertThat(exception).hasMessageThat().contains("Error retrieving device ABIs");
}
 
Example #4
Source File: FakeDevice.java    From bundletool with Apache License 2.0 6 votes vote down vote up
public static FakeDevice fromDeviceSpec(
    String deviceId, DeviceState deviceState, DeviceSpec deviceSpec) {
  if (deviceSpec.getSdkVersion() < Versions.ANDROID_M_API_VERSION) {
    Locale deviceLocale = Locale.forLanguageTag(deviceSpec.getSupportedLocales(0));
    return fromDeviceSpecWithProperties(
        deviceId,
        deviceState,
        deviceSpec,
        ImmutableMap.of(
            "ro.product.locale.language",
            deviceLocale.getLanguage(),
            "ro.product.locale.region",
            deviceLocale.getCountry()));
  } else {
    return fromDeviceSpecWithProperties(
        deviceId,
        deviceState,
        deviceSpec,
        ImmutableMap.of("ro.product.locale", deviceSpec.getSupportedLocales(0)));
  }
}
 
Example #5
Source File: InstallApksCommandTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
public void badSdkVersionDevice_throws() throws Exception {
  Path apksFile =
      createApksArchiveFile(
          createSimpleTableOfContent(ZipPath.create("base-master.apk")),
          tmpDir.resolve("bundle.apks"));

  DeviceSpec deviceSpec =
      mergeSpecs(sdkVersion(1), density(480), abis("x86_64", "x86"), locales("en-US"));
  FakeDevice fakeDevice = FakeDevice.fromDeviceSpec(DEVICE_ID, DeviceState.ONLINE, deviceSpec);
  AdbServer adbServer =
      new FakeAdbServer(/* hasInitialDeviceList= */ true, ImmutableList.of(fakeDevice));

  InstallApksCommand command =
      InstallApksCommand.builder()
          .setApksArchivePath(apksFile)
          .setAdbPath(adbPath)
          .setAdbServer(adbServer)
          .build();

  Throwable exception = assertThrows(IllegalStateException.class, () -> command.execute());
  assertThat(exception).hasMessageThat().contains("Error retrieving device SDK version");
}
 
Example #6
Source File: InstallApksCommandTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
public void missingDeviceWithId() throws Exception {
  Path apksFile = tmpDir.resolve("appbundle.apks");
  Files.createFile(apksFile);

  AdbServer adbServer =
      new FakeAdbServer(
          /* hasInitialDeviceList= */ true,
          ImmutableList.of(
              FakeDevice.fromDeviceSpec(
                  DEVICE_ID, DeviceState.ONLINE, lDeviceWithLocales("en-US"))));

  InstallApksCommand command =
      InstallApksCommand.builder()
          .setApksArchivePath(apksFile)
          .setAdbPath(adbPath)
          .setAdbServer(adbServer)
          .setDeviceId("doesnt-exist")
          .build();

  Throwable exception = assertThrows(CommandExecutionException.class, () -> command.execute());
  assertThat(exception).hasMessageThat().contains("Unable to find the requested device.");
}
 
Example #7
Source File: InstallApksCommandTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
public void noDeviceId_moreThanOneDeviceConnected() throws Exception {
  Path apksFile = tmpDir.resolve("appbundle.apks");
  Files.createFile(apksFile);

  AdbServer adbServer =
      new FakeAdbServer(
          /* hasInitialDeviceList= */ true,
          ImmutableList.of(
              FakeDevice.fromDeviceSpec(
                  DEVICE_ID, DeviceState.ONLINE, lDeviceWithLocales("en-US")),
              FakeDevice.fromDeviceSpec(
                  "id2", DeviceState.ONLINE, lDeviceWithLocales("en-US", "en-GB"))));

  InstallApksCommand command =
      InstallApksCommand.builder()
          .setApksArchivePath(apksFile)
          .setAdbPath(adbPath)
          .setAdbServer(adbServer)
          .build();

  Throwable exception = assertThrows(CommandExecutionException.class, () -> command.execute());
  assertThat(exception)
      .hasMessageThat()
      .contains("More than one device connected, please provide --device-id.");
}
 
Example #8
Source File: DeviceAnalyzer.java    From bundletool with Apache License 2.0 6 votes vote down vote up
/** Gets and validates the connected device. */
public Device getAndValidateDevice(Optional<String> deviceId) throws TimeoutException {
  Device device =
      getTargetDevice(deviceId)
          .orElseThrow(
              () ->
                  CommandExecutionException.builder()
                      .withInternalMessage("Unable to find the requested device.")
                      .build());

  if (device.getState().equals(DeviceState.UNAUTHORIZED)) {
    throw CommandExecutionException.builder()
        .withInternalMessage(
            "Device found but not authorized for connecting. "
                + "Please allow USB debugging on the device.")
        .build();
  } else if (!device.getState().equals(DeviceState.ONLINE)) {
    throw CommandExecutionException.builder()
        .withInternalMessage(
            "Unable to connect to the device (device state: '%s').", device.getState().name())
        .build();
  }
  return device;
}
 
Example #9
Source File: InstallApksCommandTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
@Theory
public void badDensityDevice_throws(@FromDataPoints("apksInDirectory") boolean apksInDirectory)
    throws Exception {
  Path apksFile =
      createApks(createSimpleTableOfContent(ZipPath.create("base-master.apk")), apksInDirectory);

  DeviceSpec deviceSpec =
      mergeSpecs(sdkVersion(21), density(-1), abis("x86_64", "x86"), locales("en-US"));
  FakeDevice fakeDevice = FakeDevice.fromDeviceSpec(DEVICE_ID, DeviceState.ONLINE, deviceSpec);
  AdbServer adbServer =
      new FakeAdbServer(/* hasInitialDeviceList= */ true, ImmutableList.of(fakeDevice));

  InstallApksCommand command =
      InstallApksCommand.builder()
          .setApksArchivePath(apksFile)
          .setAdbPath(adbPath)
          .setAdbServer(adbServer)
          .build();

  Throwable exception = assertThrows(IllegalStateException.class, () -> command.execute());
  assertThat(exception).hasMessageThat().contains("Error retrieving device density");
}
 
Example #10
Source File: FakeDevice.java    From bundletool with Apache License 2.0 6 votes vote down vote up
FakeDevice(
    String serialNumber,
    DeviceState state,
    int sdkVersion,
    ImmutableList<String> abis,
    int density,
    ImmutableList<String> features,
    ImmutableList<String> glExtensions,
    ImmutableMap<String, String> properties) {
  this.state = state;
  this.androidVersion = new AndroidVersion(sdkVersion);
  this.abis = abis;
  this.density = density;
  this.serialNumber = serialNumber;
  this.properties = properties;
  this.glExtensions = glExtensions;
  this.features = features;
}
 
Example #11
Source File: DeviceAnalyzerTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
public void activityManagerFails_propertiesFallback() {
  FakeDevice fakeDevice =
      FakeDevice.fromDeviceSpec(
          "id1",
          DeviceState.ONLINE,
          mergeSpecs(sdkVersion(26), locales("de-DE"), density(480), abis("armeabi")));
  fakeDevice.injectShellCommandOutput("am get-config", () -> "error");

  FakeAdbServer fakeAdbServer =
      new FakeAdbServer(
          /* hasInitialDeviceList= */ true, /* devices= */ ImmutableList.of(fakeDevice));
  fakeAdbServer.init(Paths.get("path/to/adb"));

  DeviceSpec spec = new DeviceAnalyzer(fakeAdbServer).getDeviceSpec(Optional.empty());

  assertThat(spec.getScreenDensity()).isEqualTo(480);
  assertThat(spec.getSupportedAbisList()).containsExactly("armeabi");
  assertThat(spec.getSdkVersion()).isEqualTo(26);
  assertThat(spec.getSupportedLocalesList()).containsExactly("de-DE");
}
 
Example #12
Source File: DeviceAnalyzerTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
public void extractsGlExtensions() {
  FakeDevice fakeDevice =
      FakeDevice.fromDeviceSpec(
          "id1",
          DeviceState.ONLINE,
          mergeSpecs(
              density(240),
              locales("en-US"),
              abis("x86"),
              sdkVersion(21),
              glExtensions("GL_EXT_extension1", "GL_EXT_extension2")));

  FakeAdbServer fakeAdbServer =
      new FakeAdbServer(
          /* hasInitialDeviceList= */ true, /* devices= */ ImmutableList.of(fakeDevice));
  fakeAdbServer.init(Paths.get("path/to/adb"));

  DeviceAnalyzer analyzer = new DeviceAnalyzer(fakeAdbServer);

  DeviceSpec deviceSpec = analyzer.getDeviceSpec(Optional.empty());
  assertThat(deviceSpec.getGlExtensionsList()).containsExactly("GL_EXT_extension1", "GL_EXT_extension2");
}
 
Example #13
Source File: DeviceAnalyzerTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
public void deviceWithBadAbis_throws() {
  FakeAdbServer fakeAdbServer =
      new FakeAdbServer(
          /* hasInitialDeviceList= */ true,
          /* devices= */ ImmutableList.of(
              FakeDevice.fromDeviceSpec(
                  "id1",
                  DeviceState.ONLINE,
                  mergeSpecs(density(240), locales("en-US"), abis(), sdkVersion(21)))));
  fakeAdbServer.init(Paths.get("path/to/adb"));

  DeviceAnalyzer analyzer = new DeviceAnalyzer(fakeAdbServer);

  Throwable exception =
      assertThrows(IllegalStateException.class, () -> analyzer.getDeviceSpec(Optional.empty()));
  assertThat(exception)
      .hasMessageThat()
      .contains("Error retrieving device ABIs. Please try again.");
}
 
Example #14
Source File: ActivityManagerRunnerTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
public void pixelDevice_detectsFeatures() {
  FakeDevice device =
      FakeDevice.fromDeviceSpec(
          SERIAL_NUMBER,
          DeviceState.ONLINE,
          mergeSpecs(sdkVersion(27), locales("en-US"), density(560), abis("armv64-v8a")));

  device.injectShellCommandOutput(
      "am get-config",
      () ->
          Joiner.on('\n')
              .join(
                  ImmutableList.of(
                      "abi: arm64-v8a,armeabi-v7a,armeabi",
                      "config: mcc234-mnc15-en-rGB,in-rID,pl-rPL-ldltr-sw411dp-w411dp-h746dp-"
                          + "normal-long-notround-lowdr-widecg-port-notnight-560dpi-finger-"
                          + "keysexposed-nokeys-navhidden-nonav-v27")));
  ActivityManagerRunner runner = new ActivityManagerRunner(device);
  assertThat(runner.getDeviceLocales()).containsExactly("en-GB", "in-ID", "pl-PL").inOrder();
  assertThat(runner.getDeviceAbis())
      .containsExactly("arm64-v8a", "armeabi-v7a", "armeabi")
      .inOrder();
}
 
Example #15
Source File: AdbShellCommandTaskTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
public void commandExecution_ShellCommandUnresponsive() {
  FakeDevice fakeDevice =
      FakeDevice.fromDeviceSpec("id1", DeviceState.ONLINE, lDeviceWithLocales("en-US"));
  fakeDevice.injectShellCommandOutput(
      "getprop",
      () -> {
        throw new ShellCommandUnresponsiveException();
      });

  AdbShellCommandTask task = new AdbShellCommandTask(fakeDevice, "getprop");
  Throwable e = assertThrows(CommandExecutionException.class, () -> task.execute());
  assertThat(e)
      .hasMessageThat()
      .contains("Unresponsive shell command while executing 'adb shell");
  assertThat(e).hasCauseThat().isInstanceOf(ShellCommandUnresponsiveException.class);
}
 
Example #16
Source File: AdbRunnerTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
public void installApks_allowingDowngrade() {
  FakeDevice fakeDevice =
      FakeDevice.fromDeviceSpec("device1", DeviceState.ONLINE, lDeviceWithLocales("en-US"));
  AdbServer testAdbServer =
      new FakeAdbServer(/* hasInitialDeviceList= */ true, ImmutableList.of(fakeDevice));
  testAdbServer.init(Paths.get("/test/adb"));
  AdbRunner adbRunner = new AdbRunner(testAdbServer);

  fakeDevice.setInstallApksSideEffect(
      (apks, installOptions) -> {
        if (!installOptions.getAllowDowngrade()) {
          throw new RuntimeException("Downgrade disallowed.");
        }
      });

  adbRunner.run(
      device ->
          device.installApks(
              ImmutableList.of(apkPath),
              InstallOptions.builder().setAllowDowngrade(true).build()));
}
 
Example #17
Source File: AdbShellCommandTaskTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
public void commandExecution_AdbCommandRejectedException() {
  FakeDevice fakeDevice =
      FakeDevice.fromDeviceSpec("id1", DeviceState.ONLINE, lDeviceWithLocales("en-US"));

  // The AdbCommandRejectedException can't be instantiated outside the package.
  AdbCommandRejectedException exception = Mockito.mock(AdbCommandRejectedException.class);
  fakeDevice.injectShellCommandOutput(
      "getprop",
      () -> {
        throw exception;
      });

  AdbShellCommandTask task = new AdbShellCommandTask(fakeDevice, "getprop");
  Throwable e = assertThrows(CommandExecutionException.class, () -> task.execute());
  assertThat(e).hasMessageThat().contains("Rejected 'adb shell getprop' command");
}
 
Example #18
Source File: AdbRunnerTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
public void installApks_withDeviceId_connectedDevices_ok() {
  AdbServer testAdbServer =
      new FakeAdbServer(
          /* hasInitialDeviceList= */ true,
          ImmutableList.of(
              FakeDevice.fromDeviceSpec(
                  "device1", DeviceState.ONLINE, lDeviceWithLocales("en-US")),
              FakeDevice.inDisconnectedState("device2", DeviceState.UNAUTHORIZED)));
  testAdbServer.init(Paths.get("/test/adb"));
  AdbRunner adbRunner = new AdbRunner(testAdbServer);

  adbRunner.run(
      device -> device.installApks(ImmutableList.of(apkPath), DEFAULT_INSTALL_OPTIONS),
      "device1");
}
 
Example #19
Source File: AdbRunnerTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
public void installApks_noDeviceId_twoConnectedDevices_throws() {
  AdbServer testAdbServer =
      new FakeAdbServer(
          /* hasInitialDeviceList= */ true,
          ImmutableList.of(
              FakeDevice.fromDeviceSpec("a", DeviceState.ONLINE, lDeviceWithLocales("en-US")),
              FakeDevice.inDisconnectedState("b", DeviceState.UNAUTHORIZED)));
  testAdbServer.init(Paths.get("/test/adb"));
  AdbRunner adbRunner = new AdbRunner(testAdbServer);

  Throwable exception =
      assertThrows(
          CommandExecutionException.class,
          () ->
              adbRunner.run(
                  device ->
                      device.installApks(ImmutableList.of(apkPath), DEFAULT_INSTALL_OPTIONS)));
  assertThat(exception)
      .hasMessageThat()
      .contains("Expected to find one connected device, but found 2.");
}
 
Example #20
Source File: DeviceAnalyzerTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
public void deviceIdMatch_debuggingNotEnabled_throws() {
  FakeAdbServer fakeAdbServer =
      new FakeAdbServer(
          /* hasInitialDeviceList= */ true,
          /* devices= */ ImmutableList.of(
              FakeDevice.inDisconnectedState("a", DeviceState.UNAUTHORIZED)));
  fakeAdbServer.init(Paths.get("path/to/adb"));

  DeviceAnalyzer analyzer = new DeviceAnalyzer(fakeAdbServer);

  Throwable exception =
      assertThrows(
          CommandExecutionException.class, () -> analyzer.getDeviceSpec(Optional.of("a")));
  assertThat(exception)
      .hasMessageThat()
      .contains("Device found but not authorized for connecting.");
}
 
Example #21
Source File: DeviceAnalyzerTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
public void deviceIdMatch_otherUnsupportedState_throws() {
  FakeAdbServer fakeAdbServer =
      new FakeAdbServer(
          /* hasInitialDeviceList= */ true,
          /* devices= */ ImmutableList.of(
              FakeDevice.inDisconnectedState("a", DeviceState.BOOTLOADER)));
  fakeAdbServer.init(Paths.get("path/to/adb"));

  DeviceAnalyzer analyzer = new DeviceAnalyzer(fakeAdbServer);

  Throwable exception =
      assertThrows(
          CommandExecutionException.class, () -> analyzer.getDeviceSpec(Optional.of("a")));
  assertThat(exception)
      .hasMessageThat()
      .contains("Unable to connect to the device (device state: 'BOOTLOADER')");
}
 
Example #22
Source File: DeviceAnalyzerTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
public void deviceWithBadSdkVersion_throws() {
  FakeAdbServer fakeAdbServer =
      new FakeAdbServer(
          /* hasInitialDeviceList= */ true,
          /* devices= */ ImmutableList.of(
              FakeDevice.fromDeviceSpec(
                  "id1",
                  DeviceState.ONLINE,
                  mergeSpecs(density(240), locales("en-US"), abis("armeabi"), sdkVersion(1)))));
  fakeAdbServer.init(Paths.get("path/to/adb"));

  DeviceAnalyzer analyzer = new DeviceAnalyzer(fakeAdbServer);

  Throwable exception =
      assertThrows(IllegalStateException.class, () -> analyzer.getDeviceSpec(Optional.empty()));
  assertThat(exception)
      .hasMessageThat()
      .contains("Error retrieving device SDK version. Please try again.");
}
 
Example #23
Source File: DeviceAnalyzerTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
public void deviceWithBadDensity_throws() {
  FakeAdbServer fakeAdbServer =
      new FakeAdbServer(
          /* hasInitialDeviceList= */ true,
          /* devices= */ ImmutableList.of(
              FakeDevice.fromDeviceSpec(
                  "id1",
                  DeviceState.ONLINE,
                  mergeSpecs(density(-1), locales("en-US"), abis("armeabi"), sdkVersion(21)))));
  fakeAdbServer.init(Paths.get("path/to/adb"));

  DeviceAnalyzer analyzer = new DeviceAnalyzer(fakeAdbServer);

  Throwable exception =
      assertThrows(IllegalStateException.class, () -> analyzer.getDeviceSpec(Optional.empty()));
  assertThat(exception)
      .hasMessageThat()
      .contains("Error retrieving device density. Please try again.");
}
 
Example #24
Source File: DeviceAnalyzerTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
public void extractsDeviceFeatures() {
  FakeDevice fakeDevice =
      FakeDevice.fromDeviceSpec(
          "id1",
          DeviceState.ONLINE,
          mergeSpecs(
              density(240),
              locales("en-US"),
              abis("x86"),
              sdkVersion(21),
              deviceFeatures("com.feature1", "com.feature2")));

  FakeAdbServer fakeAdbServer =
      new FakeAdbServer(
          /* hasInitialDeviceList= */ true, /* devices= */ ImmutableList.of(fakeDevice));
  fakeAdbServer.init(Paths.get("path/to/adb"));

  DeviceAnalyzer analyzer = new DeviceAnalyzer(fakeAdbServer);

  DeviceSpec deviceSpec = analyzer.getDeviceSpec(Optional.empty());
  assertThat(deviceSpec.getDeviceFeaturesList()).containsExactly("com.feature1", "com.feature2");
}
 
Example #25
Source File: MonkeyTestDevice.java    From Android-Monkey-Adapter with Apache License 2.0 5 votes vote down vote up
public boolean connect() {
    boolean result = false;
    for (IDevice device : AndroidDebugBridge.getBridge().getDevices()) {
        if (mDevice.getSerialNumber().equals(device.getSerialNumber())
                && mDevice.getState() == DeviceState.ONLINE) {
            mDevice = device;
            result = true;
            break;
        }
    }
    return result;
}
 
Example #26
Source File: ActivityManagerRunnerTest.java    From bundletool with Apache License 2.0 5 votes vote down vote up
@Test
public void preLDevice_noResults() {
  Device device =
      FakeDevice.fromDeviceSpec(
          SERIAL_NUMBER,
          DeviceState.ONLINE,
          mergeSpecs(sdkVersion(20), locales("en-US"), density(240), abis("armeabi")));

  ActivityManagerRunner runner = new ActivityManagerRunner(device);
  assertThat(runner.getDeviceLocales()).isEmpty();
  assertThat(runner.getDeviceAbis()).isEmpty();
}
 
Example #27
Source File: MonkeyTestDevice.java    From Android-Monkey-Adapter with Apache License 2.0 5 votes vote down vote up
/**
 * Returns if the device is on line.
 * 
 * @return <code>true</code> if {@link IDevice#getState()} returns
 *         {@link DeviceState#ONLINE}.
 */
public boolean isOnline() {
    if (mDevice == null) {
        return false;
    }
    if (mDevice.getState() == DeviceState.ONLINE) {
        return true;
    } else {
        return false;
    }
}
 
Example #28
Source File: DeviceConnectHelper.java    From Android-Monkey-Adapter with Apache License 2.0 5 votes vote down vote up
public IDevice getConnecedDevice(final String serialNumber) {
    for (IDevice device : AndroidDebugBridge.getBridge().getDevices()) {
        if (serialNumber.equals(device.getSerialNumber())
                && device.getState() == DeviceState.ONLINE) {
            return device;
        }
    }
    return null;
}
 
Example #29
Source File: DeviceUiChooser.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
private void setupDevicesTable() {
    DefaultTableModel tableModel = (DefaultTableModel) devicesTable.getModel();
    tableModel.setRowCount(0);
    for (IDevice device : devices) {
        String name;
        String target;
        if (device.isEmulator()) {
            name = device.getAvdName();
            AvdInfo info = avdManager.getAvd(device.getAvdName(), true /*validAvdOnly*/);
            target = info == null ? "?" : device.getAvdName();
        } else {
            name = "N/A";
            String deviceBuild = device.getProperty(IDevice.PROP_BUILD_VERSION);
            target = deviceBuild == null ? "unknown" : deviceBuild;
        }
        String state;
        if (DeviceState.BOOTLOADER.equals(device.getState())) {
            state = "bootloader";
        } else if (DeviceState.OFFLINE.equals(device.getState())) {
            state = "offline";
        } else if (DeviceState.ONLINE.equals(device.getState())) {
            state = "online";
        } else {
            state = "unknown";
        }

        tableModel.addRow(new Object[]{
            // TODO nulls?
            device.getSerialNumber(), name, target,
            Boolean.valueOf("1".equals(device.getProperty(IDevice.PROP_DEBUGGABLE))),
            state
        });
    }
    devicesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            DeviceUiChooser.this.updateState();
        }
    });
}
 
Example #30
Source File: AdbShellCommandTaskTest.java    From bundletool with Apache License 2.0 5 votes vote down vote up
@Test
public void commandExecution_IOException() {
  FakeDevice fakeDevice =
      FakeDevice.fromDeviceSpec("id1", DeviceState.ONLINE, lDeviceWithLocales("en-US"));
  fakeDevice.injectShellCommandOutput(
      "getprop",
      () -> {
        throw new IOException("I/O error.");
      });

  AdbShellCommandTask task = new AdbShellCommandTask(fakeDevice, "getprop");
  Throwable e = assertThrows(CommandExecutionException.class, () -> task.execute());
  assertThat(e).hasMessageThat().contains("I/O error while executing 'adb shell");
  assertThat(e).hasCauseThat().isInstanceOf(IOException.class);
}