use of com.android.ddmlib.CollectingOutputReceiver in project buck by facebook.
the class AdbHelper method executeCommandWithErrorChecking.
/**
* Runs a command on a device and throws an exception if it fails.
*
* <p>This will not work if your command contains "exit" or "trap" statements.
*
* @param device Device to run the command on.
* @param command Shell command to execute. Must not use "exit" or "trap".
* @return The full text output of the command.
* @throws CommandFailedException if the command fails.
*/
public static String executeCommandWithErrorChecking(IDevice device, String command) throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException, IOException {
CollectingOutputReceiver receiver = new CollectingOutputReceiver();
device.executeShellCommand(command + ECHO_COMMAND_SUFFIX, receiver);
return checkReceiverOutput(command, receiver);
}
use of com.android.ddmlib.CollectingOutputReceiver in project android by JetBrains.
the class ShellCommandLauncher method execute.
public static boolean execute(@NotNull String command, @NotNull IDevice device, @NotNull LaunchStatus launchStatus, @NotNull ConsolePrinter printer, long timeout, @NotNull TimeUnit timeoutUnit) {
printer.stdout("$ adb shell " + command);
CollectingOutputReceiver receiver = new AndroidLaunchReceiver(launchStatus);
try {
device.executeShellCommand(command, receiver, timeout, timeoutUnit);
} catch (Exception e) {
Logger logger = Logger.getInstance(ShellCommandLauncher.class);
logger.warn("Unexpected exception while executing shell command: " + command);
logger.warn(e);
launchStatus.terminateLaunch("Unexpected error while executing: " + command);
return false;
}
String output = receiver.getOutput();
if (output.toLowerCase(Locale.US).contains("error")) {
launchStatus.terminateLaunch("Error while executing: " + command);
printer.stderr(output);
return false;
}
return true;
}
use of com.android.ddmlib.CollectingOutputReceiver in project android by JetBrains.
the class MultiUserUtils method isCurrentUserThePrimaryUser.
public static boolean isCurrentUserThePrimaryUser(@NotNull IDevice device, long timeout, TimeUnit units, boolean defaultValue) {
if (device.getVersion().getApiLevel() < AndroidVersion.SUPPORTS_MULTI_USER.getApiLevel()) {
return false;
}
CountDownLatch latch = new CountDownLatch(1);
CollectingOutputReceiver receiver = new CollectingOutputReceiver(latch);
try {
device.executeShellCommand("am get-current-user", receiver);
} catch (Exception e) {
return defaultValue;
}
try {
latch.await(timeout, units);
} catch (InterruptedException e) {
Logger.getInstance(MultiUserUtils.class).warn("Timed out waiting for output from `am get-current-user`, returning " + defaultValue);
return defaultValue;
}
String output = receiver.getOutput();
try {
return Integer.parseInt(output.trim()) == PRIMARY_USERID;
} catch (NumberFormatException e) {
if (output.length() > 40) {
output = output.substring(0, 40) + "...";
}
Logger.getInstance(MultiUserUtils.class).warn("Error parsing output of `am get-current-user`: " + output);
return defaultValue;
}
}
use of com.android.ddmlib.CollectingOutputReceiver in project android by JetBrains.
the class DumpSysAction method performAction.
public void performAction() {
final CountDownLatch completionLatch = new CountDownLatch(1);
final CollectingOutputReceiver receiver = new CollectingOutputReceiver();
String description = myClient == null ? null : myClient.getClientData().getClientDescription();
final String pkgName = description != null ? description : "";
final String command = String.format(Locale.US, "dumpsys %1$s %2$s", myService, pkgName).trim();
ApplicationManager.getApplication().invokeAndWait(new Runnable() {
@Override
public void run() {
ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
@Override
public void run() {
try {
myDevice.executeShellCommand(command, receiver, 0, null);
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
try {
final CaptureService service = CaptureService.getInstance(myProject);
String name = service.getSuggestedName(myClient);
CaptureHandle handle = service.startCaptureFile(SystemInfoCaptureType.class, name, false);
service.appendData(handle, receiver.getOutput().getBytes());
service.finalizeCaptureFileAsynchronous(handle, new FutureCallback<Capture>() {
@Override
public void onSuccess(@Nullable Capture result) {
if (result != null) {
result.getFile().refresh(true, false);
service.notifyCaptureReady(result);
}
}
@Override
public void onFailure(@NotNull Throwable t) {
showError(myProject, "Unexpected error while saving system information", t);
}
}, EdtExecutor.INSTANCE);
} catch (IOException e) {
showError(myProject, "Unexpected error while saving system information", e);
}
}
});
} catch (Exception e) {
showError(myProject, "Unexpected error while obtaining system information", e);
} finally {
completionLatch.countDown();
}
}
});
new ShellTask(myProject, completionLatch, receiver).queue();
}
});
}
use of com.android.ddmlib.CollectingOutputReceiver in project android by JetBrains.
the class ScreenRecorderAction method performAction.
public void performAction() {
final ScreenRecorderOptionsDialog dialog = new ScreenRecorderOptionsDialog(myProject);
if (!dialog.showAndGet()) {
return;
}
final ScreenRecorderOptions options = dialog.getOptions();
final CountDownLatch latch = new CountDownLatch(1);
final CollectingOutputReceiver receiver = new CollectingOutputReceiver(latch);
ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
@Override
public void run() {
try {
myDevice.startScreenRecorder(REMOTE_PATH, options, receiver);
} catch (Exception e) {
showError(myProject, "Unexpected error while launching screen recorder", e);
latch.countDown();
}
}
});
Task.Modal screenRecorderShellTask = new ScreenRecorderTask(myProject, myDevice, latch, receiver);
screenRecorderShellTask.setCancelText("Stop Recording");
screenRecorderShellTask.queue();
}
Aggregations