diff options
author | Svetoslav <svetoslavganov@google.com> | 2014-05-08 18:51:25 -0700 |
---|---|---|
committer | Svetoslav <svetoslavganov@google.com> | 2014-05-08 18:59:53 -0700 |
commit | 121e0c073992658ca0ba055f40bf3b130caa819a (patch) | |
tree | e87a53dc03b0b94a482676bbfb498a4d106ffd6f /core/java/android/app/UiAutomationConnection.java | |
parent | 201f9b8d49108ca9ab8c98a4b3fa578aa0bbc217 (diff) | |
download | frameworks_base-121e0c073992658ca0ba055f40bf3b130caa819a.tar.gz frameworks_base-121e0c073992658ca0ba055f40bf3b130caa819a.tar.bz2 frameworks_base-121e0c073992658ca0ba055f40bf3b130caa819a.zip |
Adding shell command execution to UiAutomation.
The UiAUtomation APIs are an extension object on instrumentation and
is not null only if the instrumentation was started from the shell.
The UiAutomation APIs allow performing privileged operations for testing.
Since UiAutomation tests are just regular instrumentation tests they
cannot take advantage of the rich set of command line utilities on the
platform. This change adds a capability to UiAutomation to execute
shell commands.
bug:12927198
Change-Id: Ifb764c8e89943654f3e35d7a0c65b0b730db672f
Diffstat (limited to 'core/java/android/app/UiAutomationConnection.java')
-rw-r--r-- | core/java/android/app/UiAutomationConnection.java | 46 |
1 files changed, 44 insertions, 2 deletions
diff --git a/core/java/android/app/UiAutomationConnection.java b/core/java/android/app/UiAutomationConnection.java index fa402863f1b..81bcb3913aa 100644 --- a/core/java/android/app/UiAutomationConnection.java +++ b/core/java/android/app/UiAutomationConnection.java @@ -23,6 +23,7 @@ import android.graphics.Bitmap; import android.hardware.input.InputManager; import android.os.Binder; import android.os.IBinder; +import android.os.ParcelFileDescriptor; import android.os.Process; import android.os.RemoteException; import android.os.ServiceManager; @@ -33,6 +34,12 @@ import android.view.WindowAnimationFrameStats; import android.view.WindowContentFrameStats; import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.IAccessibilityManager; +import libcore.io.IoUtils; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; /** * This is a remote object that is passed from the shell to an instrumentation @@ -50,8 +57,8 @@ public final class UiAutomationConnection extends IUiAutomationConnection.Stub { private final IWindowManager mWindowManager = IWindowManager.Stub.asInterface( ServiceManager.getService(Service.WINDOW_SERVICE)); - private final IAccessibilityManager mAccessibilityManager = IAccessibilityManager.Stub.asInterface( - ServiceManager.getService(Service.ACCESSIBILITY_SERVICE)); + private final IAccessibilityManager mAccessibilityManager = IAccessibilityManager.Stub + .asInterface(ServiceManager.getService(Service.ACCESSIBILITY_SERVICE)); private final Object mLock = new Object(); @@ -220,6 +227,41 @@ public final class UiAutomationConnection extends IUiAutomationConnection.Stub { } @Override + public void executeShellCommand(String command, ParcelFileDescriptor sink) + throws RemoteException { + synchronized (mLock) { + throwIfCalledByNotTrustedUidLocked(); + throwIfShutdownLocked(); + throwIfNotConnectedLocked(); + } + + InputStream in = null; + OutputStream out = null; + + try { + java.lang.Process process = Runtime.getRuntime().exec(command); + + in = process.getInputStream(); + out = new FileOutputStream(sink.getFileDescriptor()); + + final byte[] buffer = new byte[8192]; + while (true) { + final int readByteCount = in.read(buffer); + if (readByteCount < 0) { + break; + } + out.write(buffer, 0, readByteCount); + } + } catch (IOException ioe) { + throw new RuntimeException("Error running shell command", ioe); + } finally { + IoUtils.closeQuietly(in); + IoUtils.closeQuietly(out); + IoUtils.closeQuietly(sink); + } + } + + @Override public void shutdown() { synchronized (mLock) { if (isConnectedLocked()) { |