Inter process interrogation ocassionally gets stuck.

1. There was a bug that was not handling correctly the
   case for which the interrogator requests an accessibility
   node info and the message describing how to fetch the
   latter for the same process case was delivered after the
   code that checks whether the message is there in order to
   dispatch it is executed. Now the message handling is done
   correctly - the caller checks if the message is present and
   if so processes it, otherwise the caller sleeps and is
   interrupted if such a message arrives.

bug:5138933

Change-Id: I4c2940b46c9a52a51c5ee48b83ca6811489765d6
diff --git a/core/java/android/view/accessibility/AccessibilityInteractionClient.java b/core/java/android/view/accessibility/AccessibilityInteractionClient.java
index 071701e..25b980b 100644
--- a/core/java/android/view/accessibility/AccessibilityInteractionClient.java
+++ b/core/java/android/view/accessibility/AccessibilityInteractionClient.java
@@ -104,6 +104,7 @@
     public void setSameThreadMessage(Message message) {
         synchronized (mInstanceLock) {
             mSameThreadMessage = message;
+            mInstanceLock.notifyAll();
         }
     }
 
@@ -125,7 +126,6 @@
                     Thread.currentThread().getId());
             // If the scale is zero the call has failed.
             if (windowScale > 0) {
-                handleSameThreadMessageIfNeeded();
                 AccessibilityNodeInfo info = getFindAccessibilityNodeInfoResultAndClear(
                         interactionId);
                 finalizeAccessibilityNodeInfo(info, connection, windowScale);
@@ -142,7 +142,7 @@
      * in the currently active window and starts from the root View in the window.
      *
      * @param connection A connection for interacting with the system.
-     * @param id The id of the node.
+     * @param viewId The id of the view.
      * @return An {@link AccessibilityNodeInfo} if found, null otherwise.
      */
     public AccessibilityNodeInfo findAccessibilityNodeInfoByViewIdInActiveWindow(
@@ -153,7 +153,6 @@
                     viewId, interactionId, this, Thread.currentThread().getId());
             // If the scale is zero the call has failed.
             if (windowScale > 0) {
-                handleSameThreadMessageIfNeeded();
                 AccessibilityNodeInfo info = getFindAccessibilityNodeInfoResultAndClear(
                         interactionId);
                 finalizeAccessibilityNodeInfo(info, connection, windowScale);
@@ -182,7 +181,6 @@
                     text, interactionId, this, Thread.currentThread().getId());
             // If the scale is zero the call has failed.
             if (windowScale > 0) {
-                handleSameThreadMessageIfNeeded();
                 List<AccessibilityNodeInfo> infos = getFindAccessibilityNodeInfosResultAndClear(
                         interactionId);
                 finalizeAccessibilityNodeInfos(infos, connection, windowScale);
@@ -217,7 +215,6 @@
                     Thread.currentThread().getId());
             // If the scale is zero the call has failed.
             if (windowScale > 0) {
-                handleSameThreadMessageIfNeeded();
                 List<AccessibilityNodeInfo> infos = getFindAccessibilityNodeInfosResultAndClear(
                         interactionId);
                 finalizeAccessibilityNodeInfos(infos, connection, windowScale);
@@ -246,7 +243,6 @@
                     accessibilityWindowId, accessibilityViewId, action, interactionId, this,
                     Thread.currentThread().getId());
             if (success) {
-                handleSameThreadMessageIfNeeded();
                 return getPerformAccessibilityActionResult(interactionId);
             }
         } catch (RemoteException re) {
@@ -363,6 +359,11 @@
         final long startTimeMillis = SystemClock.uptimeMillis();
         while (true) {
             try {
+                Message sameProcessMessage = getSameProcessMessageAndClear();
+                if (sameProcessMessage != null) {
+                    sameProcessMessage.getTarget().handleMessage(sameProcessMessage);
+                }
+
                 if (mInteractionId == interactionId) {
                     return true;
                 }
@@ -402,17 +403,6 @@
     }
 
     /**
-     * Handles the message stored if the interacted and interacting
-     * threads are the same otherwise this is a NOP.
-     */
-    private void handleSameThreadMessageIfNeeded() {
-        Message sameProcessMessage = getSameProcessMessageAndClear();
-        if (sameProcessMessage != null) {
-            sameProcessMessage.getTarget().handleMessage(sameProcessMessage);
-        }
-    }
-
-    /**
      * Finalize an {@link AccessibilityNodeInfo} before passing it to the client.
      *
      * @param info The info.