Adding new APIs to UiAutomation.

1. Accessibility service may set a flag to request a compressed
   view of the node tree representing the screen. The compressed
   state does not contain nodes that do to react to user actions
   and do not draw content, i.e. they are dumb view managers. This
   compressed hierarchy was very beneficial to the test team and
   this change is exposing the APIs. The compression has to be
   configurable since old tests are written against uncompressed
   view tree. Basically we do not hide the fact that UIAutomation
   is simply an accessibility service with some other useful APIs.

bug:8051095

2. Accessibility service can perform global actions such as opening
   notifications, opening recent apps, etc. These are also needed
   for UI testing since there is no other way to do it via the
   existing UIAutomation APIs. Basically we do not hide the fact
   that UIAutomation is simply an accessibility service with some
   other useful APIs.

bug:8117582

Change-Id: I7b6e24b5f7a973fdada0cc199cff9f882b10720b
diff --git a/api/current.txt b/api/current.txt
index c8f999f..d1155c4 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -4155,9 +4155,12 @@
   public final class UiAutomation {
     method public android.view.accessibility.AccessibilityEvent executeAndWaitForEvent(java.lang.Runnable, com.android.internal.util.Predicate<android.view.accessibility.AccessibilityEvent>, long) throws java.util.concurrent.TimeoutException;
     method public android.view.accessibility.AccessibilityNodeInfo getRootInActiveWindow();
+    method public final android.accessibilityservice.AccessibilityServiceInfo getServiceInfo();
     method public boolean injectInputEvent(android.view.InputEvent, boolean);
+    method public final boolean performGlobalAction(int);
     method public void setOnAccessibilityEventListener(android.app.UiAutomation.OnAccessibilityEventListener);
     method public boolean setRotation(int);
+    method public final void setServiceInfo(android.accessibilityservice.AccessibilityServiceInfo);
     method public android.graphics.Bitmap takeScreenshot();
     method public void waitForIdle(long, long) throws java.util.concurrent.TimeoutException;
     field public static final int ROTATION_FREEZE_0 = 0; // 0x0
diff --git a/core/java/android/accessibilityservice/AccessibilityService.java b/core/java/android/accessibilityservice/AccessibilityService.java
index 8dddbc1..811b92a 100644
--- a/core/java/android/accessibilityservice/AccessibilityService.java
+++ b/core/java/android/accessibilityservice/AccessibilityService.java
@@ -457,7 +457,7 @@
      *
      * @return The accessibility service info.
      *
-     * @see AccessibilityNodeInfo
+     * @see AccessibilityServiceInfo
      */
     public final AccessibilityServiceInfo getServiceInfo() {
         IAccessibilityServiceConnection connection =
diff --git a/core/java/android/app/UiAutomation.java b/core/java/android/app/UiAutomation.java
index e611f6d..8d865da 100644
--- a/core/java/android/app/UiAutomation.java
+++ b/core/java/android/app/UiAutomation.java
@@ -18,7 +18,9 @@
 
 import android.accessibilityservice.AccessibilityService.Callbacks;
 import android.accessibilityservice.AccessibilityService.IAccessibilityServiceClientWrapper;
+import android.accessibilityservice.AccessibilityServiceInfo;
 import android.accessibilityservice.IAccessibilityServiceClient;
+import android.accessibilityservice.IAccessibilityServiceConnection;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
 import android.graphics.Point;
@@ -45,7 +47,10 @@
  * 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.
+ * interaction with keyboards and touch devices. One can think of a UiAutomation
+ * as a special type of {@link android.accessibilityservice.AccessibilityService}
+ * which does not provide hooks for the service life cycle and exposes other
+ * APIs that are useful for UI test automation.
  * <p>
  * The APIs exposed by this class are low-level to maximize flexibility when
  * developing UI test automation tools and libraries. Generally, a UiAutomation
@@ -243,6 +248,90 @@
     }
 
     /**
+     * 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.
+     *
+     * @param action The action to perform.
+     * @return Whether the action was successfully performed.
+     *
+     * @see AccessibilityService#GLOBAL_ACTION_BACK
+     * @see AccessibilityService#GLOBAL_ACTION_HOME
+     * @see AccessibilityService#GLOBAL_ACTION_NOTIFICATIONS
+     * @see AccessibilityService#GLOBAL_ACTION_RECENTS
+     */
+    public final boolean performGlobalAction(int action) {
+        final IAccessibilityServiceConnection connection;
+        synchronized (mLock) {
+            throwIfNotConnectedLocked();
+            connection = AccessibilityInteractionClient.getInstance()
+                    .getConnection(mConnectionId);
+        }
+        // Calling out without a lock held.
+        if (connection != null) {
+            try {
+                return connection.performGlobalAction(action);
+            } catch (RemoteException re) {
+                Log.w(LOG_TAG, "Error while calling performGlobalAction", re);
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Gets the an {@link AccessibilityServiceInfo} describing this UiAutomation.
+     * This method is useful if one wants to change some of the dynamically
+     * configurable properties at runtime.
+     *
+     * @return The accessibility service info.
+     *
+     * @see AccessibilityServiceInfo
+     */
+    public final AccessibilityServiceInfo getServiceInfo() {
+        final IAccessibilityServiceConnection connection;
+        synchronized (mLock) {
+            throwIfNotConnectedLocked();
+            connection = AccessibilityInteractionClient.getInstance()
+                    .getConnection(mConnectionId);
+        }
+        // Calling out without a lock held.
+        if (connection != null) {
+            try {
+                return connection.getServiceInfo();
+            } catch (RemoteException re) {
+                Log.w(LOG_TAG, "Error while getting AccessibilityServiceInfo", re);
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Sets the {@link AccessibilityServiceInfo} that describes how this
+     * UiAutomation will be handled by the platform accessibility layer.
+     *
+     * @param info The info.
+     *
+     * @see AccessibilityServiceInfo
+     */
+    public final void setServiceInfo(AccessibilityServiceInfo info) {
+        final IAccessibilityServiceConnection connection;
+        synchronized (mLock) {
+            throwIfNotConnectedLocked();
+            AccessibilityInteractionClient.getInstance().clearCache();
+            connection = AccessibilityInteractionClient.getInstance()
+                    .getConnection(mConnectionId);
+        }
+        // Calling out without a lock held.
+        if (connection != null) {
+            try {
+                connection.setServiceInfo(info);
+            } catch (RemoteException re) {
+                Log.w(LOG_TAG, "Error while setting AccessibilityServiceInfo", re);
+            }
+        }
+    }
+
+    /**
      * Gets the root {@link AccessibilityNodeInfo} in the active window.
      *
      * @return The root info.