@@ -158,6 +158,7 @@ private void addWindowByDisplayLocked(int displayId, AccessibilityWindowInfo win
158158 * @param event An event.
159159 */
160160 public void onAccessibilityEvent (AccessibilityEvent event ) {
161+ AccessibilityNodeInfo nodeToRefresh = null ;
161162 synchronized (mLock ) {
162163 if (DEBUG ) {
163164 Log .i (LOG_TAG , "onAccessibilityEvent(" + event + ")" );
@@ -166,35 +167,38 @@ public void onAccessibilityEvent(AccessibilityEvent event) {
166167 switch (eventType ) {
167168 case AccessibilityEvent .TYPE_VIEW_ACCESSIBILITY_FOCUSED : {
168169 if (mAccessibilityFocus != AccessibilityNodeInfo .UNDEFINED_ITEM_ID ) {
169- refreshCachedNodeLocked (mAccessibilityFocusedWindow , mAccessibilityFocus );
170+ removeCachedNodeLocked (mAccessibilityFocusedWindow , mAccessibilityFocus );
170171 }
171172 mAccessibilityFocus = event .getSourceNodeId ();
172173 mAccessibilityFocusedWindow = event .getWindowId ();
173- refreshCachedNodeLocked (mAccessibilityFocusedWindow , mAccessibilityFocus );
174+ nodeToRefresh = removeCachedNodeLocked (mAccessibilityFocusedWindow ,
175+ mAccessibilityFocus );
174176 } break ;
175177
176178 case AccessibilityEvent .TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED : {
177179 if (mAccessibilityFocus == event .getSourceNodeId ()
178180 && mAccessibilityFocusedWindow == event .getWindowId ()) {
179- refreshCachedNodeLocked (mAccessibilityFocusedWindow , mAccessibilityFocus );
181+ nodeToRefresh = removeCachedNodeLocked (mAccessibilityFocusedWindow ,
182+ mAccessibilityFocus );
180183 mAccessibilityFocus = AccessibilityNodeInfo .UNDEFINED_ITEM_ID ;
181184 mAccessibilityFocusedWindow = AccessibilityWindowInfo .UNDEFINED_WINDOW_ID ;
182185 }
183186 } break ;
184187
185188 case AccessibilityEvent .TYPE_VIEW_FOCUSED : {
186189 if (mInputFocus != AccessibilityNodeInfo .UNDEFINED_ITEM_ID ) {
187- refreshCachedNodeLocked (event .getWindowId (), mInputFocus );
190+ removeCachedNodeLocked (event .getWindowId (), mInputFocus );
188191 }
189192 mInputFocus = event .getSourceNodeId ();
190- refreshCachedNodeLocked (event .getWindowId (), mInputFocus );
193+ nodeToRefresh = removeCachedNodeLocked (event .getWindowId (), mInputFocus );
191194 } break ;
192195
193196 case AccessibilityEvent .TYPE_VIEW_SELECTED :
194197 case AccessibilityEvent .TYPE_VIEW_TEXT_CHANGED :
195198 case AccessibilityEvent .TYPE_VIEW_CLICKED :
196199 case AccessibilityEvent .TYPE_VIEW_TEXT_SELECTION_CHANGED : {
197- refreshCachedNodeLocked (event .getWindowId (), event .getSourceNodeId ());
200+ nodeToRefresh = removeCachedNodeLocked (event .getWindowId (),
201+ event .getSourceNodeId ());
198202 } break ;
199203
200204 case AccessibilityEvent .TYPE_WINDOW_CONTENT_CHANGED : {
@@ -205,7 +209,7 @@ public void onAccessibilityEvent(AccessibilityEvent event) {
205209 & AccessibilityEvent .CONTENT_CHANGE_TYPE_SUBTREE ) != 0 ) {
206210 clearSubTreeLocked (windowId , sourceId );
207211 } else {
208- refreshCachedNodeLocked (windowId , sourceId );
212+ nodeToRefresh = removeCachedNodeLocked (windowId , sourceId );
209213 }
210214 }
211215 } break ;
@@ -218,8 +222,8 @@ public void onAccessibilityEvent(AccessibilityEvent event) {
218222 if (event .getWindowChanges ()
219223 == AccessibilityEvent .WINDOWS_CHANGE_ACCESSIBILITY_FOCUSED ) {
220224 // Don't need to clear all cache. Unless the changes are related to
221- // content, we won't clear all cache here.
222- refreshCachedWindowLocked ( event . getWindowId () );
225+ // content, we won't clear all cache here with clear() .
226+ clearWindowCacheLocked ( );
223227 break ;
224228 }
225229 case AccessibilityEvent .TYPE_WINDOW_STATE_CHANGED : {
@@ -228,59 +232,34 @@ public void onAccessibilityEvent(AccessibilityEvent event) {
228232 }
229233 }
230234
235+ if (nodeToRefresh != null ) {
236+ if (DEBUG ) {
237+ Log .i (LOG_TAG , "Refreshing and re-adding cached node." );
238+ }
239+ if (mAccessibilityNodeRefresher .refreshNode (nodeToRefresh , true )) {
240+ add (nodeToRefresh );
241+ }
242+ }
231243 if (CHECK_INTEGRITY ) {
232244 checkIntegrity ();
233245 }
234246 }
235247
236- private void refreshCachedNodeLocked (int windowId , long sourceId ) {
248+ private AccessibilityNodeInfo removeCachedNodeLocked (int windowId , long sourceId ) {
237249 if (DEBUG ) {
238- Log .i (LOG_TAG , "Refreshing cached node." );
250+ Log .i (LOG_TAG , "Removing cached node." );
239251 }
240-
241252 LongSparseArray <AccessibilityNodeInfo > nodes = mNodeCache .get (windowId );
242253 if (nodes == null ) {
243- return ;
254+ return null ;
244255 }
245256 AccessibilityNodeInfo cachedInfo = nodes .get (sourceId );
246257 // If the source is not in the cache - nothing to do.
247258 if (cachedInfo == null ) {
248- return ;
249- }
250- // The node changed so we will just refresh it right now.
251- if (mAccessibilityNodeRefresher .refreshNode (cachedInfo , true )) {
252- return ;
253- }
254- // Weird, we could not refresh. Just evict the entire sub-tree.
255- clearSubTreeLocked (windowId , sourceId );
256- }
257-
258- private void refreshCachedWindowLocked (int windowId ) {
259- if (DEBUG ) {
260- Log .i (LOG_TAG , "Refreshing cached window." );
261- }
262-
263- if (windowId == AccessibilityWindowInfo .UNDEFINED_WINDOW_ID ) {
264- return ;
265- }
266-
267- final int displayCounts = mWindowCacheByDisplay .size ();
268- for (int i = 0 ; i < displayCounts ; i ++) {
269- final SparseArray <AccessibilityWindowInfo > windowsOfDisplay =
270- mWindowCacheByDisplay .valueAt (i );
271- if (windowsOfDisplay == null ) {
272- continue ;
273- }
274- final AccessibilityWindowInfo window = windowsOfDisplay .get (windowId );
275- if (window == null ) {
276- continue ;
277- }
278- if (!mAccessibilityNodeRefresher .refreshWindow (window )) {
279- // If we fail to refresh the window, clear all windows.
280- clearWindowCacheLocked ();
281- }
282- return ;
259+ return null ;
283260 }
261+ nodes .remove (sourceId );
262+ return cachedInfo ;
284263 }
285264
286265 /**
@@ -450,7 +429,7 @@ public void add(AccessibilityNodeInfo info) {
450429 if (clone .isAccessibilityFocused ()) {
451430 if (mAccessibilityFocus != AccessibilityNodeInfo .UNDEFINED_ITEM_ID
452431 && mAccessibilityFocus != sourceId ) {
453- refreshCachedNodeLocked (windowId , mAccessibilityFocus );
432+ removeCachedNodeLocked (windowId , mAccessibilityFocus );
454433 }
455434 mAccessibilityFocus = sourceId ;
456435 mAccessibilityFocusedWindow = windowId ;
0 commit comments