Merge "Fixing bugs discovered by the CTS tests."
diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java
index 93c6d6e..b0f67ac 100644
--- a/core/java/android/view/ViewRootImpl.java
+++ b/core/java/android/view/ViewRootImpl.java
@@ -6435,11 +6435,17 @@
         public long mLastEventTimeMillis;
 
         public void run() {
-            mLastEventTimeMillis = SystemClock.uptimeMillis();
-            AccessibilityEvent event = AccessibilityEvent.obtain();
-            event.setEventType(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
-            event.setContentChangeType(AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
-            mSource.sendAccessibilityEventUnchecked(event);
+            // The accessibility may be turned off while we were waiting so check again.
+            if (AccessibilityManager.getInstance(mContext).isEnabled()) {
+                mLastEventTimeMillis = SystemClock.uptimeMillis();
+                AccessibilityEvent event = AccessibilityEvent.obtain();
+                event.setEventType(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
+                event.setContentChangeType(AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
+                mSource.sendAccessibilityEventUnchecked(event);
+            } else {
+                mLastEventTimeMillis = 0;
+            }
+            // In any case reset to initial state.
             mSource.resetSubtreeAccessibilityStateChanged();
             mSource = null;
         }
diff --git a/core/java/android/view/accessibility/AccessibilityNodeInfoCache.java b/core/java/android/view/accessibility/AccessibilityNodeInfoCache.java
index dded74c..1fde2fa 100644
--- a/core/java/android/view/accessibility/AccessibilityNodeInfoCache.java
+++ b/core/java/android/view/accessibility/AccessibilityNodeInfoCache.java
@@ -39,7 +39,7 @@
 
     private static final boolean ENABLED = true;
 
-    private static final boolean DEBUG = true;
+    private static final boolean DEBUG = false;
 
     private static final boolean CHECK_INTEGRITY_IF_DEBUGGABLE_BUILD = true;
 
@@ -108,7 +108,7 @@
 
     private void refreshCachedNode(long sourceId) {
         if (DEBUG) {
-            Log.i(LOG_TAG, "Refresing cached node.");
+            Log.i(LOG_TAG, "Refreshing cached node.");
         }
         synchronized (mLock) {
             AccessibilityNodeInfo cachedInfo = mCacheImpl.get(sourceId);
@@ -117,7 +117,7 @@
                 return;
             }
             // The node changed so we will just refresh it right now.
-            if (cachedInfo.refresh(false)) {
+            if (cachedInfo.refresh(true)) {
                 return;
             }
             // Weird, we could not refresh. Just evict the entire sub-tree.
@@ -141,7 +141,7 @@
                     info = AccessibilityNodeInfo.obtain(info);
                 }
                 if (DEBUG) {
-//                    Log.i(LOG_TAG, "get(" + accessibilityNodeId + ") = " + info);
+                    Log.i(LOG_TAG, "get(" + accessibilityNodeId + ") = " + info);
                 }
                 return info;
             }
@@ -159,7 +159,7 @@
         if (ENABLED) {
             synchronized(mLock) {
                 if (DEBUG) {
-//                    Log.i(LOG_TAG, "add(" + info + ")");
+                    Log.i(LOG_TAG, "add(" + info + ")");
                 }
 
                 final long sourceId = info.getSourceNodeId();
@@ -319,8 +319,6 @@
                         Log.e(LOG_TAG, "Node from: " + info.getWindowId() + " not from:"
                                 + windowId + " " + info);
                     }
-                    mCacheImpl.removeAt(i);
-                    i--;
                 }
             }
         }