Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Nested Classes](https://developer.android.com/reference/android/app/UiAutomation#nestedclasses) \| [Constants](https://developer.android.com/reference/android/app/UiAutomation#constants) \| [Methods](https://developer.android.com/reference/android/app/UiAutomation#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/app/UiAutomation#inhmethods)

# UiAutomation

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/app/UiAutomation "View this page in Kotlin") \|Java


`
public

final

class
UiAutomation
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.app.UiAutomation |

<br />

*** ** * ** ***

Class for interacting with the device's UI by simulation user actions and
introspection of the screen content. It relies on the platform accessibility
APIs to introspect the screen and to perform some actions on the remote view
tree. It also allows injecting of arbitrary raw input events simulating user
interaction with keyboards and touch devices. One can think of a UiAutomation
as a special type of `https://developer.android.com/reference/android/accessibilityservice/AccessibilityService`
which does not provide hooks for the service life cycle and exposes other
APIs that are useful for UI test automation.


The APIs exposed by this class are low-level to maximize flexibility when
developing UI test automation tools and libraries. Generally, a UiAutomation
client should be using a higher-level library or implement high-level functions.
For example, performing a tap on the screen requires construction and injecting
of a touch down and up events which have to be delivered to the system by a
call to `https://developer.android.com/reference/android/app/UiAutomation#injectInputEvent(android.view.InputEvent,%20boolean)`.


The APIs exposed by this class operate across applications enabling a client
to write tests that cover use cases spanning over multiple applications. For
example, going to the settings application to change a setting and then
interacting with another application whose behavior depends on that setting.

<br />

## Summary

| ### Nested classes ||
|---|---|
| ` interface` | `https://developer.android.com/reference/android/app/UiAutomation.AccessibilityEventFilter` Listener for filtering accessibility events. |
| ` interface` | `https://developer.android.com/reference/android/app/UiAutomation.OnAccessibilityEventListener` Listener for observing the `https://developer.android.com/reference/android/view/accessibility/AccessibilityEvent` stream. |

| ### Constants ||
|---|---|
| `int` | `https://developer.android.com/reference/android/app/UiAutomation#FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES` UiAutomation suppresses accessibility services by default. |
| `int` | `https://developer.android.com/reference/android/app/UiAutomation#FLAG_DONT_USE_ACCESSIBILITY` UiAutomation uses the accessibility subsystem by default. |
| `int` | `https://developer.android.com/reference/android/app/UiAutomation#ROTATION_FREEZE_0` Rotation constant: Freeze rotation to 0 degrees (natural orientation) |
| `int` | `https://developer.android.com/reference/android/app/UiAutomation#ROTATION_FREEZE_180` Rotation constant: Freeze rotation to 180 degrees . |
| `int` | `https://developer.android.com/reference/android/app/UiAutomation#ROTATION_FREEZE_270` Rotation constant: Freeze rotation to 270 degrees . |
| `int` | `https://developer.android.com/reference/android/app/UiAutomation#ROTATION_FREEZE_90` Rotation constant: Freeze rotation to 90 degrees . |
| `int` | `https://developer.android.com/reference/android/app/UiAutomation#ROTATION_FREEZE_CURRENT` Rotation constant: Freeze rotation to its current state. |
| `int` | `https://developer.android.com/reference/android/app/UiAutomation#ROTATION_UNFREEZE` Rotation constant: Unfreeze rotation (rotating the device changes its rotation state). |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/app/UiAutomation#adoptShellPermissionIdentity(java.lang.String[])(https://developer.android.com/reference/java/lang/String permissions) ` Adopt the permission identity of the shell UID only for the provided permissions. |
| ` void` | ` https://developer.android.com/reference/android/app/UiAutomation#adoptShellPermissionIdentity()() ` Adopt the permission identity of the shell UID for all permissions. |
| ` boolean` | ` https://developer.android.com/reference/android/app/UiAutomation#clearCache()() ` Clears the accessibility cache. |
| ` void` | ` https://developer.android.com/reference/android/app/UiAutomation#clearWindowAnimationFrameStats()() ` *This method was deprecated in API level 33. animation-frames are no-longer used. Use Shared [FrameTimeline](https://perfetto.dev/docs/data-sources/frametimeline) jank metrics instead.* |
| ` boolean` | ` https://developer.android.com/reference/android/app/UiAutomation#clearWindowContentFrameStats(int)(int windowId) ` Clears the frame statistics for the content of a given window. |
| ` void` | ` https://developer.android.com/reference/android/app/UiAutomation#dropShellPermissionIdentity()() ` Drop the shell permission identity adopted by a previous call to `https://developer.android.com/reference/android/app/UiAutomation#adoptShellPermissionIdentity()`. |
| ` https://developer.android.com/reference/android/view/accessibility/AccessibilityEvent` | ` https://developer.android.com/reference/android/app/UiAutomation#executeAndWaitForEvent(java.lang.Runnable,%20android.app.UiAutomation.AccessibilityEventFilter,%20long)(https://developer.android.com/reference/java/lang/Runnable command, https://developer.android.com/reference/android/app/UiAutomation.AccessibilityEventFilter filter, long timeoutMillis) ` Executes a command and waits for a specific accessibility event up to a given wait timeout. |
| ` https://developer.android.com/reference/android/os/ParcelFileDescriptor` | ` https://developer.android.com/reference/android/app/UiAutomation#executeShellCommand(java.lang.String)(https://developer.android.com/reference/java/lang/String command) ` Executes a shell command. |
| ` https://developer.android.com/reference/android/os/ParcelFileDescriptor` | ` https://developer.android.com/reference/android/app/UiAutomation#executeShellCommandRw(java.lang.String)(https://developer.android.com/reference/java/lang/String command) ` Executes a shell command. |
| ` https://developer.android.com/reference/android/os/ParcelFileDescriptor` | ` https://developer.android.com/reference/android/app/UiAutomation#executeShellCommandRwe(java.lang.String)(https://developer.android.com/reference/java/lang/String command) ` Executes a shell command. |
| ` https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo` | ` https://developer.android.com/reference/android/app/UiAutomation#findFocus(int)(int focus) ` Find the view that has the specified focus type. |
| ` https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo` | ` https://developer.android.com/reference/android/app/UiAutomation#getRootInActiveWindow()() ` Gets the root `https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo` in the active window. |
| ` https://developer.android.com/reference/android/accessibilityservice/AccessibilityServiceInfo` | ` https://developer.android.com/reference/android/app/UiAutomation#getServiceInfo()() ` Gets the an `https://developer.android.com/reference/android/accessibilityservice/AccessibilityServiceInfo` describing this UiAutomation. |
| ` https://developer.android.com/reference/android/view/WindowAnimationFrameStats` | ` https://developer.android.com/reference/android/app/UiAutomation#getWindowAnimationFrameStats()() ` *This method was deprecated in API level 33. animation-frames are no-longer used.* |
| ` https://developer.android.com/reference/android/view/WindowContentFrameStats` | ` https://developer.android.com/reference/android/app/UiAutomation#getWindowContentFrameStats(int)(int windowId) ` Gets the frame statistics for a given window. |
| ` https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/android/view/accessibility/AccessibilityWindowInfo>` | ` https://developer.android.com/reference/android/app/UiAutomation#getWindows()() ` Gets the windows on the screen associated with the `https://developer.android.com/reference/android/app/UiAutomation` context (usually the `https://developer.android.com/reference/android/view/Display#DEFAULT_DISPLAY`. |
| ` https://developer.android.com/reference/android/util/SparseArray<https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/android/view/accessibility/AccessibilityWindowInfo>>` | ` https://developer.android.com/reference/android/app/UiAutomation#getWindowsOnAllDisplays()() ` Gets the windows on the screen of all displays. |
| ` void` | ` https://developer.android.com/reference/android/app/UiAutomation#grantRuntimePermission(java.lang.String,%20java.lang.String)(https://developer.android.com/reference/java/lang/String packageName, https://developer.android.com/reference/java/lang/String permission) ` Grants a runtime permission to a package. |
| ` void` | ` https://developer.android.com/reference/android/app/UiAutomation#grantRuntimePermissionAsUser(java.lang.String,%20java.lang.String,%20android.os.UserHandle)(https://developer.android.com/reference/java/lang/String packageName, https://developer.android.com/reference/java/lang/String permission, https://developer.android.com/reference/android/os/UserHandle userHandle) ` Grants a runtime permission to a package for a user. |
| ` boolean` | ` https://developer.android.com/reference/android/app/UiAutomation#injectInputEvent(android.view.InputEvent,%20boolean)(https://developer.android.com/reference/android/view/InputEvent event, boolean sync) ` A method for injecting an arbitrary input event. |
| ` boolean` | ` https://developer.android.com/reference/android/app/UiAutomation#performGlobalAction(int)(int action) ` Performs a global action. |
| ` void` | ` https://developer.android.com/reference/android/app/UiAutomation#revokeRuntimePermission(java.lang.String,%20java.lang.String)(https://developer.android.com/reference/java/lang/String packageName, https://developer.android.com/reference/java/lang/String permission) ` Revokes a runtime permission from a package. |
| ` void` | ` https://developer.android.com/reference/android/app/UiAutomation#revokeRuntimePermissionAsUser(java.lang.String,%20java.lang.String,%20android.os.UserHandle)(https://developer.android.com/reference/java/lang/String packageName, https://developer.android.com/reference/java/lang/String permission, https://developer.android.com/reference/android/os/UserHandle userHandle) ` Revokes a runtime permission from a package. |
| ` void` | ` https://developer.android.com/reference/android/app/UiAutomation#setAnimationScale(float)(float scale) ` Sets the system settings values that control the scaling factor for animations. |
| ` void` | ` https://developer.android.com/reference/android/app/UiAutomation#setOnAccessibilityEventListener(android.app.UiAutomation.OnAccessibilityEventListener)(https://developer.android.com/reference/android/app/UiAutomation.OnAccessibilityEventListener listener) ` Sets a callback for observing the stream of `https://developer.android.com/reference/android/view/accessibility/AccessibilityEvent`s. |
| ` boolean` | ` https://developer.android.com/reference/android/app/UiAutomation#setRotation(int)(int rotation) ` Sets the device rotation. |
| ` void` | ` https://developer.android.com/reference/android/app/UiAutomation#setRunAsMonkey(boolean)(boolean enable) ` Sets whether this UiAutomation to run in a "monkey" mode. |
| ` void` | ` https://developer.android.com/reference/android/app/UiAutomation#setServiceInfo(android.accessibilityservice.AccessibilityServiceInfo)(https://developer.android.com/reference/android/accessibilityservice/AccessibilityServiceInfo info) ` Sets the `https://developer.android.com/reference/android/accessibilityservice/AccessibilityServiceInfo` that describes how this UiAutomation will be handled by the platform accessibility layer. |
| ` https://developer.android.com/reference/android/graphics/Bitmap` | ` https://developer.android.com/reference/android/app/UiAutomation#takeScreenshot()() ` Takes a screenshot. |
| ` https://developer.android.com/reference/android/graphics/Bitmap` | ` https://developer.android.com/reference/android/app/UiAutomation#takeScreenshot(android.view.Window)(https://developer.android.com/reference/android/view/Window window) ` Used to capture a screenshot of a Window. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/app/UiAutomation#toString()() ` Returns a string representation of the object. |
| ` void` | ` https://developer.android.com/reference/android/app/UiAutomation#waitForIdle(long,%20long)(long idleTimeoutMillis, long globalTimeoutMillis) ` Waits for the accessibility event stream to become idle, which is not to have received an accessibility event within `idleTimeoutMillis`. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||

## Constants

### FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES
```

UiAutomation suppresses accessibility services by default. This flag specifies that
existing accessibility services should continue to run, and that new ones may start.
This flag is set when obtaining the UiAutomation from
`https://developer.android.com/reference/android/app/Instrumentation#getUiAutomation(int)`.

<br />

Constant Value:

1
(0x00000001)


### FLAG_DONT_USE_ACCESSIBILITY

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int FLAG_DONT_USE_ACCESSIBILITY
```

UiAutomation uses the accessibility subsystem by default. This flag provides an option to
eliminate the overhead of engaging the accessibility subsystem for tests that do not need to
interact with the user interface. Setting this flag disables methods that rely on
accessibility. This flag is set when obtaining the UiAutomation from
`https://developer.android.com/reference/android/app/Instrumentation#getUiAutomation(int)`.

<br />

Constant Value:

2
(0x00000002)


### ROTATION_FREEZE_0

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int ROTATION_FREEZE_0
```

Rotation constant: Freeze rotation to 0 degrees (natural orientation)

<br />

Constant Value:

0
(0x00000000)


### ROTATION_FREEZE_180

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int ROTATION_FREEZE_180
```

Rotation constant: Freeze rotation to 180 degrees .

<br />

Constant Value:

2
(0x00000002)


### ROTATION_FREEZE_270

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int ROTATION_FREEZE_270
```

Rotation constant: Freeze rotation to 270 degrees .

<br />

Constant Value:

3
(0x00000003)


### ROTATION_FREEZE_90

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int ROTATION_FREEZE_90
```

Rotation constant: Freeze rotation to 90 degrees .

<br />

Constant Value:

1
(0x00000001)


### ROTATION_FREEZE_CURRENT

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int ROTATION_FREEZE_CURRENT
```

Rotation constant: Freeze rotation to its current state.

<br />

Constant Value:

-1
(0xffffffff)


### ROTATION_UNFREEZE

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int ROTATION_UNFREEZE
```

Rotation constant: Unfreeze rotation (rotating the device changes its rotation state).

<br />

Constant Value:

-2
(0xfffffffe)


## Public methods

### adoptShellPermissionIdentity

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void adoptShellPermissionIdentity (String... permissions)
```

Adopt the permission identity of the shell UID only for the provided permissions.
This allows you to call APIs protected permissions which normal apps cannot hold
but are granted to the shell UID. If you already adopted shell permissions by calling
this method, or `https://developer.android.com/reference/android/app/UiAutomation#adoptShellPermissionIdentity()` a subsequent call will replace any
previous adoption.


**Note:** This method behave differently from
`https://developer.android.com/reference/android/app/UiAutomation#adoptShellPermissionIdentity()`. Only the listed permissions will use the shell
identity and other permissions will still check against the original UID

<br />

| Parameters ||
|---|---|
| `permissions` | `String`: The permissions to adopt or `null` to adopt all. <br /> |

**See also:**

- `https://developer.android.com/reference/android/app/UiAutomation#adoptShellPermissionIdentity()`
- `https://developer.android.com/reference/android/app/UiAutomation#dropShellPermissionIdentity()`

### adoptShellPermissionIdentity

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void adoptShellPermissionIdentity ()
```

Adopt the permission identity of the shell UID for all permissions. This allows
you to call APIs protected permissions which normal apps cannot hold but are
granted to the shell UID. If you already adopted all shell permissions by calling
this method or `https://developer.android.com/reference/android/app/UiAutomation#adoptShellPermissionIdentity(java.lang.String[])` a subsequent call will
replace any previous adoption. Note that your permission state becomes that of the shell UID
and it is not a combination of your and the shell UID permissions.


**Note:** Calling this method adopts all shell permissions and overrides
any subset of adopted permissions via `https://developer.android.com/reference/android/app/UiAutomation#adoptShellPermissionIdentity(java.lang.String[])`.

<br />

**See also:**

- `https://developer.android.com/reference/android/app/UiAutomation#adoptShellPermissionIdentity(java.lang.String[])`
- `https://developer.android.com/reference/android/app/UiAutomation#dropShellPermissionIdentity()`

### clearCache

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean clearCache ()
```

Clears the accessibility cache.

<br />

| Returns ||
|---|---|
| `boolean` | `true` if the cache was cleared <br /> |

**See also:**

- `https://developer.android.com/reference/android/accessibilityservice/AccessibilityService#clearCache()`

### clearWindowAnimationFrameStats

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)   
Deprecated in [API level
33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void clearWindowAnimationFrameStats ()
```


**This method was deprecated
in API level 33.**   

animation-frames are no-longer used. Use Shared
[FrameTimeline](https://perfetto.dev/docs/data-sources/frametimeline)
jank metrics instead.

Clears the window animation rendering statistics. These statistics contain
information about the most recently rendered window animation frames, i.e.
for window transition animations.

<br />

**See also:**

- `https://developer.android.com/reference/android/view/WindowAnimationFrameStats`
- `https://developer.android.com/reference/android/app/UiAutomation#getWindowAnimationFrameStats()`
- `https://developer.android.com/reference/android/R.styleable#WindowAnimation`

### clearWindowContentFrameStats

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean clearWindowContentFrameStats (int windowId)
```

Clears the frame statistics for the content of a given window. These
statistics contain information about the most recently rendered content
frames.

<br />

| Parameters ||
|---|---|
| `windowId` | `int`: The window id. <br /> |

| Returns ||
|---|---|
| `boolean` | Whether the window is present and its frame statistics were cleared. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | If the connection to the accessibility subsystem is not established. |

**See also:**

- `https://developer.android.com/reference/android/view/WindowContentFrameStats`
- `https://developer.android.com/reference/android/app/UiAutomation#getWindowContentFrameStats(int)`
- `https://developer.android.com/reference/android/app/UiAutomation#getWindows()`
- `https://developer.android.com/reference/android/view/accessibility/AccessibilityWindowInfo#getId()`

### dropShellPermissionIdentity

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void dropShellPermissionIdentity ()
```

Drop the shell permission identity adopted by a previous call to
`https://developer.android.com/reference/android/app/UiAutomation#adoptShellPermissionIdentity()`. If you did not adopt the shell permission
identity this method would be a no-op.

<br />

**See also:**

- `https://developer.android.com/reference/android/app/UiAutomation#adoptShellPermissionIdentity()`

### executeAndWaitForEvent

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public AccessibilityEvent executeAndWaitForEvent (Runnable command, 
                UiAutomation.AccessibilityEventFilter filter, 
                long timeoutMillis)
```

Executes a command and waits for a specific accessibility event up to a
given wait timeout. To detect a sequence of events one can implement a
filter that keeps track of seen events of the expected sequence and
returns true after the last event of that sequence is received.


**Note:** It is caller's responsibility to recycle the returned event.

<br />

<br />

| Parameters ||
|---|---|
| `command` | `Runnable`: The command to execute. <br /> |
| `filter` | `UiAutomation.AccessibilityEventFilter`: Filter that recognizes the expected event. <br /> |
| `timeoutMillis` | `long`: The wait timeout in milliseconds. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/view/accessibility/AccessibilityEvent` | <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | If the connection to the accessibility subsystem is not established. |
| `https://developer.android.com/reference/java/util/concurrent/TimeoutException` | If the expected event is not received within the timeout. |

### executeShellCommand

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public ParcelFileDescriptor executeShellCommand (String command)
```

Executes a shell command. This method returns a file descriptor that points
to the standard output stream. The command execution is similar to running
"adb shell " from a host connected to the device.


**Note:** It is your responsibility to close the returned file
descriptor once you are done reading.

<br />

<br />

| Parameters ||
|---|---|
| `command` | `String`: The command to execute. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/ParcelFileDescriptor` | A file descriptor to the standard output stream. <br /> |

**See also:**

- `https://developer.android.com/reference/android/app/UiAutomation#adoptShellPermissionIdentity()`

### executeShellCommandRw

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public ParcelFileDescriptor[] executeShellCommandRw (String command)
```

Executes a shell command. This method returns two file descriptors,
one that points to the standard output stream (element at index 0), and one that points
to the standard input stream (element at index 1). The command execution is similar
to running "adb shell " from a host connected to the device.


**Note:** It is your responsibility to close the returned file
descriptors once you are done reading/writing.

<br />

<br />

| Parameters ||
|---|---|
| `command` | `String`: The command to execute. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/ParcelFileDescriptor` | File descriptors (out, in) to the standard output/input streams. This value cannot be `null`. <br /> |

### executeShellCommandRwe

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public ParcelFileDescriptor[] executeShellCommandRwe (String command)
```

Executes a shell command. This method returns three file descriptors,
one that points to the standard output stream (element at index 0), one that points
to the standard input stream (element at index 1), and one points to
standard error stream (element at index 2). The command execution is similar
to running "adb shell " from a host connected to the device.


**Note:** It is your responsibility to close the returned file
descriptors once you are done reading/writing.

<br />

<br />

| Parameters ||
|---|---|
| `command` | `String`: The command to execute. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/ParcelFileDescriptor` | File descriptors (out, in, err) to the standard output/input/error streams. This value cannot be `null`. <br /> |

### findFocus

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public AccessibilityNodeInfo findFocus (int focus)
```

Find the view that has the specified focus type. The search is performed
across all windows.


**Note:** In order to access the windows you have to opt-in
to retrieve the interactive windows by setting the
`https://developer.android.com/reference/android/accessibilityservice/AccessibilityServiceInfo#FLAG_RETRIEVE_INTERACTIVE_WINDOWS` flag.
Otherwise, the search will be performed only in the active window.

<br />

<br />

| Parameters ||
|---|---|
| `focus` | `int`: The focus to find. One of `https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo#FOCUS_INPUT` or `https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo#FOCUS_ACCESSIBILITY`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo` | The node info of the focused view or null. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | If the connection to the accessibility subsystem is not established. |

**See also:**

- `https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo#FOCUS_INPUT`
- `https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo#FOCUS_ACCESSIBILITY`

### getRootInActiveWindow

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public AccessibilityNodeInfo getRootInActiveWindow ()
```

Gets the root `https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo` in the active window.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo` | The root info. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | If the connection to the accessibility subsystem is not established. |

### getServiceInfo

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public AccessibilityServiceInfo getServiceInfo ()
```

Gets the an `https://developer.android.com/reference/android/accessibilityservice/AccessibilityServiceInfo` describing this UiAutomation.
This method is useful if one wants to change some of the dynamically
configurable properties at runtime.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/accessibilityservice/AccessibilityServiceInfo` | The accessibility service info. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | If the connection to the accessibility subsystem is not established. |

**See also:**

- `https://developer.android.com/reference/android/accessibilityservice/AccessibilityServiceInfo`

### getWindowAnimationFrameStats

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)   
Deprecated in [API level
33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public WindowAnimationFrameStats getWindowAnimationFrameStats ()
```


**This method was deprecated
in API level 33.**   

animation-frames are no-longer used.

Gets the window animation frame statistics. These statistics contain
information about the most recently rendered window animation frames, i.e.
for window transition animations.


A typical usage requires clearing the window animation frame statistics via
`https://developer.android.com/reference/android/app/UiAutomation#clearWindowAnimationFrameStats()` followed by an interaction that causes
a window transition which uses a window animation and finally getting the window
animation frame statistics by calling this method.

```
 // Start with a clean slate.
 uiAutimation.clearWindowAnimationFrameStats();

 // Do stuff to trigger a window transition.

 // Get the frame statistics.
 WindowAnimationFrameStats stats = uiAutomation.getWindowAnimationFrameStats();
 
```

<br />

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/view/WindowAnimationFrameStats` | The window animation frame statistics. <br /> |

**See also:**

- `https://developer.android.com/reference/android/view/WindowAnimationFrameStats`
- `https://developer.android.com/reference/android/app/UiAutomation#clearWindowAnimationFrameStats()`
- `https://developer.android.com/reference/android/R.styleable#WindowAnimation`

### getWindowContentFrameStats

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public WindowContentFrameStats getWindowContentFrameStats (int windowId)
```

Gets the frame statistics for a given window. These statistics contain
information about the most recently rendered content frames.


A typical usage requires clearing the window frame statistics via `https://developer.android.com/reference/android/app/UiAutomation#clearWindowContentFrameStats(int)` followed by an interaction with the UI and
finally getting the window frame statistics via calling this method.

```
 // Assume we have at least one window.
 final int windowId = getWindows().get(0).getId();

 // Start with a clean slate.
 uiAutimation.clearWindowContentFrameStats(windowId);

 // Do stuff with the UI.

 // Get the frame statistics.
 WindowContentFrameStats stats = uiAutomation.getWindowContentFrameStats(windowId);
 
```

<br />

<br />

| Parameters ||
|---|---|
| `windowId` | `int`: The window id. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/view/WindowContentFrameStats` | The window frame statistics, or null if the window is not present. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | If the connection to the accessibility subsystem is not established. |

**See also:**

- `https://developer.android.com/reference/android/view/WindowContentFrameStats`
- `https://developer.android.com/reference/android/app/UiAutomation#clearWindowContentFrameStats(int)`
- `https://developer.android.com/reference/android/app/UiAutomation#getWindows()`
- `https://developer.android.com/reference/android/view/accessibility/AccessibilityWindowInfo#getId()`

### getWindows

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public List<AccessibilityWindowInfo> getWindows ()
```

Gets the windows on the screen associated with the `https://developer.android.com/reference/android/app/UiAutomation` context (usually the
`https://developer.android.com/reference/android/view/Display#DEFAULT_DISPLAY`.


This method returns only the windows that a sighted user can interact with, as opposed to
all windows.


For example, if there is a modal dialog shown and the user cannot touch
anything behind it, then only the modal window will be reported
(assuming it is the top one). For convenience the returned windows
are ordered in a descending layer order, which is the windows that
are higher in the Z-order are reported first.


**Note:** In order to access the windows you have to opt-in
to retrieve the interactive windows by setting the
`https://developer.android.com/reference/android/accessibilityservice/AccessibilityServiceInfo#FLAG_RETRIEVE_INTERACTIVE_WINDOWS` flag.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/android/view/accessibility/AccessibilityWindowInfo>` | The windows if there are windows such, otherwise an empty list. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | If the connection to the accessibility subsystem is not established. |

### getWindowsOnAllDisplays

Added in [API level 30](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public SparseArray<List<AccessibilityWindowInfo>> getWindowsOnAllDisplays ()
```

Gets the windows on the screen of all displays. This method returns only the windows
that a sighted user can interact with, as opposed to all windows.
For example, if there is a modal dialog shown and the user cannot touch
anything behind it, then only the modal window will be reported
(assuming it is the top one). For convenience the returned windows
are ordered in a descending layer order, which is the windows that
are higher in the Z-order are reported first.


**Note:** In order to access the windows you have to opt-in
to retrieve the interactive windows by setting the
`https://developer.android.com/reference/android/accessibilityservice/AccessibilityServiceInfo#FLAG_RETRIEVE_INTERACTIVE_WINDOWS` flag.

<br />

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/util/SparseArray<https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/android/view/accessibility/AccessibilityWindowInfo>>` | The windows of all displays if there are windows and the service is can retrieve them, otherwise an empty list. The key of SparseArray is display ID. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | If the connection to the accessibility subsystem is not established. |

### grantRuntimePermission

Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void grantRuntimePermission (String packageName, 
                String permission)
```

Grants a runtime permission to a package.

<br />

| Parameters ||
|---|---|
| `packageName` | `String`: The package to which to grant. <br /> |
| `permission` | `String`: The permission to grant. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/SecurityException` | if unable to grant the permission. |

### grantRuntimePermissionAsUser

Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void grantRuntimePermissionAsUser (String packageName, 
                String permission, 
                UserHandle userHandle)
```

Grants a runtime permission to a package for a user.

<br />

| Parameters ||
|---|---|
| `packageName` | `String`: The package to which to grant. <br /> |
| `permission` | `String`: The permission to grant. <br /> |
| `userHandle` | `UserHandle` <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/SecurityException` | if unable to grant the permission. |

### injectInputEvent

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean injectInputEvent (InputEvent event, 
                boolean sync)
```

A method for injecting an arbitrary input event.

This method waits for all window container animations and surface operations to complete.


**Note:** It is caller's responsibility to recycle the event.

<br />

<br />

| Parameters ||
|---|---|
| `event` | `InputEvent`: the event to inject <br /> |
| `sync` | `boolean`: whether to inject the event synchronously <br /> |

| Returns ||
|---|---|
| `boolean` | `true` if event injection succeeded <br /> |

### performGlobalAction

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean performGlobalAction (int action)
```

Performs a global action. Such an action can be performed at any moment
regardless of the current application or user location in that application.
For example going back, going home, opening recents, etc.

<br />

| Parameters ||
|---|---|
| `action` | `int`: The action to perform. <br /> |

| Returns ||
|---|---|
| `boolean` | Whether the action was successfully performed. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | If the connection to the accessibility subsystem is not established. |

**See also:**

- `https://developer.android.com/reference/android/accessibilityservice/AccessibilityService#GLOBAL_ACTION_BACK`
- `https://developer.android.com/reference/android/accessibilityservice/AccessibilityService#GLOBAL_ACTION_HOME`
- `https://developer.android.com/reference/android/accessibilityservice/AccessibilityService#GLOBAL_ACTION_NOTIFICATIONS`
- `https://developer.android.com/reference/android/accessibilityservice/AccessibilityService#GLOBAL_ACTION_RECENTS`

### revokeRuntimePermission

Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void revokeRuntimePermission (String packageName, 
                String permission)
```

Revokes a runtime permission from a package.

<br />

| Parameters ||
|---|---|
| `packageName` | `String`: The package to which to grant. <br /> |
| `permission` | `String`: The permission to grant. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/SecurityException` | if unable to revoke the permission. |

### revokeRuntimePermissionAsUser

Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void revokeRuntimePermissionAsUser (String packageName, 
                String permission, 
                UserHandle userHandle)
```

Revokes a runtime permission from a package.

<br />

| Parameters ||
|---|---|
| `packageName` | `String`: The package to which to grant. <br /> |
| `permission` | `String`: The permission to grant. <br /> |
| `userHandle` | `UserHandle` <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/SecurityException` | if unable to revoke the permission. |

### setAnimationScale

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setAnimationScale (float scale)
```

Sets the system settings values that control the scaling factor for animations. The scale
controls the animation playback speed for animations that respect these settings. Animations
that do not respect the settings values will not be affected by this function. A lower scale
value results in a faster speed. A value of `0` disables animations entirely. When
animations are disabled services receive window change events more quickly which can reduce
the potential by confusion by reducing the time during which windows are in transition.

<br />

| Parameters ||
|---|---|
| `scale` | `float`: The scaling factor for all animations. <br /> |

**See also:**

- `https://developer.android.com/reference/android/view/accessibility/AccessibilityEvent#TYPE_WINDOWS_CHANGED`
- `https://developer.android.com/reference/android/view/accessibility/AccessibilityEvent#TYPE_WINDOW_STATE_CHANGED`
- `https://developer.android.com/reference/android/provider/Settings.Global#WINDOW_ANIMATION_SCALE`
- `https://developer.android.com/reference/android/provider/Settings.Global#TRANSITION_ANIMATION_SCALE`
- `https://developer.android.com/reference/android/provider/Settings.Global#ANIMATOR_DURATION_SCALE`

### setOnAccessibilityEventListener

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setOnAccessibilityEventListener (UiAutomation.OnAccessibilityEventListener listener)
```

Sets a callback for observing the stream of `https://developer.android.com/reference/android/view/accessibility/AccessibilityEvent`s.
The callbacks are delivered on the main application thread.

<br />

| Parameters ||
|---|---|
| `listener` | `UiAutomation.OnAccessibilityEventListener`: The callback. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | If the connection to the accessibility subsystem is not established. |

### setRotation

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean setRotation (int rotation)
```

Sets the device rotation. A client can freeze the rotation in
desired state or freeze the rotation to its current state or
unfreeze the rotation (rotating the device changes its rotation
state).

<br />

| Parameters ||
|---|---|
| `rotation` | `int`: The desired rotation. <br /> |

| Returns ||
|---|---|
| `boolean` | Whether the rotation was set successfully. <br /> |

**See also:**

- `https://developer.android.com/reference/android/app/UiAutomation#ROTATION_FREEZE_0`
- `https://developer.android.com/reference/android/app/UiAutomation#ROTATION_FREEZE_90`
- `https://developer.android.com/reference/android/app/UiAutomation#ROTATION_FREEZE_180`
- `https://developer.android.com/reference/android/app/UiAutomation#ROTATION_FREEZE_270`
- `https://developer.android.com/reference/android/app/UiAutomation#ROTATION_FREEZE_CURRENT`
- `https://developer.android.com/reference/android/app/UiAutomation#ROTATION_UNFREEZE`

### setRunAsMonkey

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setRunAsMonkey (boolean enable)
```

Sets whether this UiAutomation to run in a "monkey" mode. Applications can query whether
they are executed in a "monkey" mode, i.e. run by a test framework, and avoid doing
potentially undesirable actions such as calling 911 or posting on public forums etc.

<br />

| Parameters ||
|---|---|
| `enable` | `boolean`: whether to run in a "monkey" mode or not. Default is not. <br /> |

**See also:**

- `https://developer.android.com/reference/android/app/ActivityManager#isUserAMonkey()`

### setServiceInfo

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setServiceInfo (AccessibilityServiceInfo info)
```

Sets the `https://developer.android.com/reference/android/accessibilityservice/AccessibilityServiceInfo` that describes how this
UiAutomation will be handled by the platform accessibility layer.

<br />

| Parameters ||
|---|---|
| `info` | `AccessibilityServiceInfo`: The info. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | If the connection to the accessibility subsystem is not established. |

**See also:**

- `https://developer.android.com/reference/android/accessibilityservice/AccessibilityServiceInfo`

### takeScreenshot

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Bitmap takeScreenshot ()
```

Takes a screenshot.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/graphics/Bitmap` | The screenshot bitmap on success, null otherwise. <br /> |

### takeScreenshot

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Bitmap takeScreenshot (Window window)
```

Used to capture a screenshot of a Window. This can return null in the following cases:
1. Window content hasn't been layed out.
2. Window doesn't have a valid SurfaceControl
3. An error occurred in SurfaceFlinger when trying to take the screenshot.

<br />

| Parameters ||
|---|---|
| `window` | `Window`: Window to take a screenshot of. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/graphics/Bitmap` | The screenshot bitmap on success, null otherwise. <br /> |

### toString

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public String toString ()
```

Returns a string representation of the object.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | a string representation of the object. <br /> |

### waitForIdle

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void waitForIdle (long idleTimeoutMillis, 
                long globalTimeoutMillis)
```

Waits for the accessibility event stream to become idle, which is not to
have received an accessibility event within `idleTimeoutMillis`.
The total time spent to wait for an idle accessibility event stream is bounded
by the `globalTimeoutMillis`.

<br />

| Parameters ||
|---|---|
| `idleTimeoutMillis` | `long`: The timeout in milliseconds between two events to consider the device idle. <br /> |
| `globalTimeoutMillis` | `long`: The maximal global timeout in milliseconds in which to wait for an idle state. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | If the connection to the accessibility subsystem is not established. |
| `https://developer.android.com/reference/java/util/concurrent/TimeoutException` | If no idle state was detected within `globalTimeoutMillis.` |