Search in sources :

Example 1 with CollectingOutputReceiver

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);
}
Also used : CollectingOutputReceiver(com.android.ddmlib.CollectingOutputReceiver)

Example 2 with CollectingOutputReceiver

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;
}
Also used : CollectingOutputReceiver(com.android.ddmlib.CollectingOutputReceiver) Logger(com.intellij.openapi.diagnostic.Logger)

Example 3 with CollectingOutputReceiver

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;
    }
}
Also used : CollectingOutputReceiver(com.android.ddmlib.CollectingOutputReceiver) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with CollectingOutputReceiver

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();
        }
    });
}
Also used : CollectingOutputReceiver(com.android.ddmlib.CollectingOutputReceiver) CaptureService(com.android.tools.idea.profiling.capture.CaptureService) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) NotNull(org.jetbrains.annotations.NotNull) Capture(com.android.tools.idea.profiling.capture.Capture) IOException(java.io.IOException) SystemInfoCaptureType(com.android.tools.idea.editors.systeminfo.SystemInfoCaptureType) CaptureHandle(com.android.tools.idea.profiling.capture.CaptureHandle) FutureCallback(com.google.common.util.concurrent.FutureCallback) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with CollectingOutputReceiver

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();
}
Also used : ScreenRecorderOptions(com.android.ddmlib.ScreenRecorderOptions) Task(com.intellij.openapi.progress.Task) CollectingOutputReceiver(com.android.ddmlib.CollectingOutputReceiver) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

CollectingOutputReceiver (com.android.ddmlib.CollectingOutputReceiver)10 CountDownLatch (java.util.concurrent.CountDownLatch)5 IOException (java.io.IOException)2 Nullable (javax.annotation.Nullable)2 AdbCommandRejectedException (com.android.ddmlib.AdbCommandRejectedException)1 ScreenRecorderOptions (com.android.ddmlib.ScreenRecorderOptions)1 ShellCommandUnresponsiveException (com.android.ddmlib.ShellCommandUnresponsiveException)1 TimeoutException (com.android.ddmlib.TimeoutException)1 SystemInfoCaptureType (com.android.tools.idea.editors.systeminfo.SystemInfoCaptureType)1 Capture (com.android.tools.idea.profiling.capture.Capture)1 CaptureHandle (com.android.tools.idea.profiling.capture.CaptureHandle)1 CaptureService (com.android.tools.idea.profiling.capture.CaptureService)1 ApkInfo (com.android.tools.idea.run.ApkInfo)1 SuppressForbidden (com.facebook.buck.annotations.SuppressForbidden)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Task (com.intellij.openapi.progress.Task)1 File (java.io.File)1 Matcher (java.util.regex.Matcher)1