Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.view.accessibility; |
| 18 | |
| 19 | import android.os.Build; |
| 20 | import android.util.ArraySet; |
| 21 | import android.util.Log; |
| 22 | import android.util.LongArray; |
| 23 | import android.util.LongSparseArray; |
| 24 | import android.util.SparseArray; |
| 25 | |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.List; |
| 28 | |
| 29 | /** |
| 30 | * Cache for AccessibilityWindowInfos and AccessibilityNodeInfos. |
| 31 | * It is updated when windows change or nodes change. |
Phil Weaver | b010b12 | 2016-08-17 17:47:48 -0700 | [diff] [blame] | 32 | * @hide |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 33 | */ |
Phil Weaver | c140fdc | 2017-11-09 15:24:17 -0800 | [diff] [blame] | 34 | public class AccessibilityCache { |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 35 | |
| 36 | private static final String LOG_TAG = "AccessibilityCache"; |
| 37 | |
| 38 | private static final boolean DEBUG = false; |
| 39 | |
Jeff Sharkey | 5ab0243 | 2017-06-27 11:01:36 -0600 | [diff] [blame] | 40 | private static final boolean CHECK_INTEGRITY = Build.IS_ENG; |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 41 | |
Eugene Susla | eb1375c | 2016-12-20 16:32:59 -0800 | [diff] [blame] | 42 | /** |
| 43 | * {@link AccessibilityEvent} types that are critical for the cache to stay up to date |
| 44 | * |
| 45 | * When adding new event types in {@link #onAccessibilityEvent}, please add it here also, to |
| 46 | * make sure that the events are delivered to cache regardless of |
| 47 | * {@link android.accessibilityservice.AccessibilityServiceInfo#eventTypes} |
| 48 | */ |
| 49 | public static final int CACHE_CRITICAL_EVENTS_MASK = |
| 50 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED |
| 51 | | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED |
| 52 | | AccessibilityEvent.TYPE_VIEW_FOCUSED |
| 53 | | AccessibilityEvent.TYPE_VIEW_SELECTED |
| 54 | | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED |
| 55 | | AccessibilityEvent.TYPE_VIEW_CLICKED |
| 56 | | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED |
| 57 | | AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED |
| 58 | | AccessibilityEvent.TYPE_VIEW_SCROLLED |
| 59 | | AccessibilityEvent.TYPE_WINDOWS_CHANGED |
| 60 | | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED; |
| 61 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 62 | private final Object mLock = new Object(); |
| 63 | |
Phil Weaver | b010b12 | 2016-08-17 17:47:48 -0700 | [diff] [blame] | 64 | private final AccessibilityNodeRefresher mAccessibilityNodeRefresher; |
| 65 | |
Svetoslav | 27ad2e9 | 2015-02-12 17:54:15 -0800 | [diff] [blame] | 66 | private long mAccessibilityFocus = AccessibilityNodeInfo.UNDEFINED_ITEM_ID; |
| 67 | private long mInputFocus = AccessibilityNodeInfo.UNDEFINED_ITEM_ID; |
| 68 | |
Maxim Bogatov | c328120 | 2015-10-21 12:38:01 -0700 | [diff] [blame] | 69 | private boolean mIsAllWindowsCached; |
| 70 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 71 | private final SparseArray<AccessibilityWindowInfo> mWindowCache = |
Svetoslav | f7c42c5 | 2014-07-24 12:19:22 -0700 | [diff] [blame] | 72 | new SparseArray<>(); |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 73 | |
| 74 | private final SparseArray<LongSparseArray<AccessibilityNodeInfo>> mNodeCache = |
Svetoslav | f7c42c5 | 2014-07-24 12:19:22 -0700 | [diff] [blame] | 75 | new SparseArray<>(); |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 76 | |
| 77 | private final SparseArray<AccessibilityWindowInfo> mTempWindowArray = |
Svetoslav | f7c42c5 | 2014-07-24 12:19:22 -0700 | [diff] [blame] | 78 | new SparseArray<>(); |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 79 | |
Phil Weaver | b010b12 | 2016-08-17 17:47:48 -0700 | [diff] [blame] | 80 | public AccessibilityCache(AccessibilityNodeRefresher nodeRefresher) { |
| 81 | mAccessibilityNodeRefresher = nodeRefresher; |
| 82 | } |
| 83 | |
Maxim Bogatov | c328120 | 2015-10-21 12:38:01 -0700 | [diff] [blame] | 84 | public void setWindows(List<AccessibilityWindowInfo> windows) { |
| 85 | synchronized (mLock) { |
| 86 | if (DEBUG) { |
| 87 | Log.i(LOG_TAG, "Set windows"); |
| 88 | } |
| 89 | clearWindowCache(); |
| 90 | if (windows == null) { |
| 91 | return; |
| 92 | } |
| 93 | final int windowCount = windows.size(); |
| 94 | for (int i = 0; i < windowCount; i++) { |
| 95 | final AccessibilityWindowInfo window = windows.get(i); |
| 96 | addWindow(window); |
| 97 | } |
| 98 | mIsAllWindowsCached = true; |
| 99 | } |
| 100 | } |
| 101 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 102 | public void addWindow(AccessibilityWindowInfo window) { |
| 103 | synchronized (mLock) { |
| 104 | if (DEBUG) { |
| 105 | Log.i(LOG_TAG, "Caching window: " + window.getId()); |
| 106 | } |
Svetoslav | 04cab1b | 2014-08-25 18:35:57 -0700 | [diff] [blame] | 107 | final int windowId = window.getId(); |
| 108 | AccessibilityWindowInfo oldWindow = mWindowCache.get(windowId); |
| 109 | if (oldWindow != null) { |
| 110 | oldWindow.recycle(); |
| 111 | } |
| 112 | mWindowCache.put(windowId, AccessibilityWindowInfo.obtain(window)); |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 116 | /** |
| 117 | * Notifies the cache that the something in the UI changed. As a result |
| 118 | * the cache will either refresh some nodes or evict some nodes. |
| 119 | * |
Eugene Susla | eb1375c | 2016-12-20 16:32:59 -0800 | [diff] [blame] | 120 | * Note: any event that ends up affecting the cache should also be present in |
| 121 | * {@link #CACHE_CRITICAL_EVENTS_MASK} |
| 122 | * |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 123 | * @param event An event. |
| 124 | */ |
| 125 | public void onAccessibilityEvent(AccessibilityEvent event) { |
| 126 | synchronized (mLock) { |
| 127 | final int eventType = event.getEventType(); |
| 128 | switch (eventType) { |
Svetoslav | 27ad2e9 | 2015-02-12 17:54:15 -0800 | [diff] [blame] | 129 | case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: { |
| 130 | if (mAccessibilityFocus != AccessibilityNodeInfo.UNDEFINED_ITEM_ID) { |
| 131 | refreshCachedNodeLocked(event.getWindowId(), mAccessibilityFocus); |
| 132 | } |
| 133 | mAccessibilityFocus = event.getSourceNodeId(); |
| 134 | refreshCachedNodeLocked(event.getWindowId(), mAccessibilityFocus); |
| 135 | } break; |
| 136 | |
| 137 | case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: { |
| 138 | if (mAccessibilityFocus == event.getSourceNodeId()) { |
| 139 | refreshCachedNodeLocked(event.getWindowId(), mAccessibilityFocus); |
| 140 | mAccessibilityFocus = AccessibilityNodeInfo.UNDEFINED_ITEM_ID; |
| 141 | } |
| 142 | } break; |
| 143 | |
| 144 | case AccessibilityEvent.TYPE_VIEW_FOCUSED: { |
| 145 | if (mInputFocus != AccessibilityNodeInfo.UNDEFINED_ITEM_ID) { |
| 146 | refreshCachedNodeLocked(event.getWindowId(), mInputFocus); |
| 147 | } |
| 148 | mInputFocus = event.getSourceNodeId(); |
| 149 | refreshCachedNodeLocked(event.getWindowId(), mInputFocus); |
| 150 | } break; |
| 151 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 152 | case AccessibilityEvent.TYPE_VIEW_SELECTED: |
| 153 | case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED: |
Svet Ganov | 0322100 | 2014-12-05 16:52:27 -0800 | [diff] [blame] | 154 | case AccessibilityEvent.TYPE_VIEW_CLICKED: |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 155 | case AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED: { |
| 156 | refreshCachedNodeLocked(event.getWindowId(), event.getSourceNodeId()); |
| 157 | } break; |
| 158 | |
| 159 | case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED: { |
| 160 | synchronized (mLock) { |
| 161 | final int windowId = event.getWindowId(); |
| 162 | final long sourceId = event.getSourceNodeId(); |
| 163 | if ((event.getContentChangeTypes() |
| 164 | & AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE) != 0) { |
| 165 | clearSubTreeLocked(windowId, sourceId); |
| 166 | } else { |
| 167 | refreshCachedNodeLocked(windowId, sourceId); |
| 168 | } |
| 169 | } |
| 170 | } break; |
| 171 | |
| 172 | case AccessibilityEvent.TYPE_VIEW_SCROLLED: { |
| 173 | clearSubTreeLocked(event.getWindowId(), event.getSourceNodeId()); |
| 174 | } break; |
Svetoslav | a4725ef | 2014-07-24 14:17:05 -0700 | [diff] [blame] | 175 | |
Svetoslav | 13bd771 | 2014-09-19 18:18:04 -0700 | [diff] [blame] | 176 | case AccessibilityEvent.TYPE_WINDOWS_CHANGED: |
| 177 | case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED: { |
| 178 | clear(); |
Svetoslav | a4725ef | 2014-07-24 14:17:05 -0700 | [diff] [blame] | 179 | } break; |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | if (CHECK_INTEGRITY) { |
| 184 | checkIntegrity(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | private void refreshCachedNodeLocked(int windowId, long sourceId) { |
| 189 | if (DEBUG) { |
| 190 | Log.i(LOG_TAG, "Refreshing cached node."); |
| 191 | } |
| 192 | |
| 193 | LongSparseArray<AccessibilityNodeInfo> nodes = mNodeCache.get(windowId); |
| 194 | if (nodes == null) { |
| 195 | return; |
| 196 | } |
| 197 | AccessibilityNodeInfo cachedInfo = nodes.get(sourceId); |
| 198 | // If the source is not in the cache - nothing to do. |
| 199 | if (cachedInfo == null) { |
| 200 | return; |
| 201 | } |
| 202 | // The node changed so we will just refresh it right now. |
Phil Weaver | b010b12 | 2016-08-17 17:47:48 -0700 | [diff] [blame] | 203 | if (mAccessibilityNodeRefresher.refreshNode(cachedInfo, true)) { |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 204 | return; |
| 205 | } |
| 206 | // Weird, we could not refresh. Just evict the entire sub-tree. |
| 207 | clearSubTreeLocked(windowId, sourceId); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Gets a cached {@link AccessibilityNodeInfo} given the id of the hosting |
| 212 | * window and the accessibility id of the node. |
| 213 | * |
| 214 | * @param windowId The id of the window hosting the node. |
| 215 | * @param accessibilityNodeId The info accessibility node id. |
| 216 | * @return The cached {@link AccessibilityNodeInfo} or null if such not found. |
| 217 | */ |
| 218 | public AccessibilityNodeInfo getNode(int windowId, long accessibilityNodeId) { |
| 219 | synchronized(mLock) { |
| 220 | LongSparseArray<AccessibilityNodeInfo> nodes = mNodeCache.get(windowId); |
| 221 | if (nodes == null) { |
| 222 | return null; |
| 223 | } |
| 224 | AccessibilityNodeInfo info = nodes.get(accessibilityNodeId); |
| 225 | if (info != null) { |
| 226 | // Return a copy since the client calls to AccessibilityNodeInfo#recycle() |
| 227 | // will wipe the data of the cached info. |
| 228 | info = AccessibilityNodeInfo.obtain(info); |
| 229 | } |
| 230 | if (DEBUG) { |
| 231 | Log.i(LOG_TAG, "get(" + accessibilityNodeId + ") = " + info); |
| 232 | } |
| 233 | return info; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | public List<AccessibilityWindowInfo> getWindows() { |
| 238 | synchronized (mLock) { |
Maxim Bogatov | c328120 | 2015-10-21 12:38:01 -0700 | [diff] [blame] | 239 | if (!mIsAllWindowsCached) { |
| 240 | return null; |
| 241 | } |
| 242 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 243 | final int windowCount = mWindowCache.size(); |
| 244 | if (windowCount > 0) { |
| 245 | // Careful to return the windows in a decreasing layer order. |
| 246 | SparseArray<AccessibilityWindowInfo> sortedWindows = mTempWindowArray; |
| 247 | sortedWindows.clear(); |
| 248 | |
| 249 | for (int i = 0; i < windowCount; i++) { |
| 250 | AccessibilityWindowInfo window = mWindowCache.valueAt(i); |
| 251 | sortedWindows.put(window.getLayer(), window); |
| 252 | } |
| 253 | |
Phil Weaver | adaafb2 | 2016-05-19 10:32:52 -0700 | [diff] [blame] | 254 | // It's possible in transient conditions for two windows to share the same |
| 255 | // layer, which results in sortedWindows being smaller than mWindowCache |
| 256 | final int sortedWindowCount = sortedWindows.size(); |
| 257 | List<AccessibilityWindowInfo> windows = new ArrayList<>(sortedWindowCount); |
| 258 | for (int i = sortedWindowCount - 1; i >= 0; i--) { |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 259 | AccessibilityWindowInfo window = sortedWindows.valueAt(i); |
| 260 | windows.add(AccessibilityWindowInfo.obtain(window)); |
Svetoslav | 04cab1b | 2014-08-25 18:35:57 -0700 | [diff] [blame] | 261 | sortedWindows.removeAt(i); |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 264 | return windows; |
| 265 | } |
| 266 | return null; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | public AccessibilityWindowInfo getWindow(int windowId) { |
| 271 | synchronized (mLock) { |
| 272 | AccessibilityWindowInfo window = mWindowCache.get(windowId); |
| 273 | if (window != null) { |
| 274 | return AccessibilityWindowInfo.obtain(window); |
| 275 | } |
| 276 | return null; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Caches an {@link AccessibilityNodeInfo}. |
| 282 | * |
| 283 | * @param info The node to cache. |
| 284 | */ |
| 285 | public void add(AccessibilityNodeInfo info) { |
| 286 | synchronized(mLock) { |
| 287 | if (DEBUG) { |
| 288 | Log.i(LOG_TAG, "add(" + info + ")"); |
| 289 | } |
| 290 | |
| 291 | final int windowId = info.getWindowId(); |
| 292 | LongSparseArray<AccessibilityNodeInfo> nodes = mNodeCache.get(windowId); |
| 293 | if (nodes == null) { |
Svetoslav | f7c42c5 | 2014-07-24 12:19:22 -0700 | [diff] [blame] | 294 | nodes = new LongSparseArray<>(); |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 295 | mNodeCache.put(windowId, nodes); |
| 296 | } |
| 297 | |
| 298 | final long sourceId = info.getSourceNodeId(); |
| 299 | AccessibilityNodeInfo oldInfo = nodes.get(sourceId); |
| 300 | if (oldInfo != null) { |
| 301 | // If the added node is in the cache we have to be careful if |
| 302 | // the new one represents a source state where some of the |
| 303 | // children have been removed to remove the descendants that |
| 304 | // are no longer present. |
| 305 | final LongArray newChildrenIds = info.getChildNodeIds(); |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 306 | |
Svetoslav | f7c42c5 | 2014-07-24 12:19:22 -0700 | [diff] [blame] | 307 | final int oldChildCount = oldInfo.getChildCount(); |
| 308 | for (int i = 0; i < oldChildCount; i++) { |
Phil Weaver | f1eb10b | 2018-08-14 16:25:53 -0700 | [diff] [blame] | 309 | final long oldChildId = oldInfo.getChildId(i); |
| 310 | // If the child is no longer present, remove the sub-tree. |
| 311 | if (newChildrenIds == null || newChildrenIds.indexOf(oldChildId) < 0) { |
| 312 | clearSubTreeLocked(windowId, oldChildId); |
| 313 | } |
Phil Weaver | 61a1fab | 2017-05-03 13:55:51 -0700 | [diff] [blame] | 314 | if (nodes.get(sourceId) == null) { |
| 315 | // We've removed (and thus recycled) this node because it was its own |
| 316 | // ancestor (the app gave us bad data), we can't continue using it. |
| 317 | // Clear the cache for this window and give up on adding the node. |
| 318 | clearNodesForWindowLocked(windowId); |
| 319 | return; |
| 320 | } |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | // Also be careful if the parent has changed since the new |
| 324 | // parent may be a predecessor of the old parent which will |
Phil Weaver | b010b12 | 2016-08-17 17:47:48 -0700 | [diff] [blame] | 325 | // add cycles to the cache. |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 326 | final long oldParentId = oldInfo.getParentNodeId(); |
| 327 | if (info.getParentNodeId() != oldParentId) { |
| 328 | clearSubTreeLocked(windowId, oldParentId); |
Phil Weaver | c140fdc | 2017-11-09 15:24:17 -0800 | [diff] [blame] | 329 | } else { |
| 330 | oldInfo.recycle(); |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
| 334 | // Cache a copy since the client calls to AccessibilityNodeInfo#recycle() |
| 335 | // will wipe the data of the cached info. |
| 336 | AccessibilityNodeInfo clone = AccessibilityNodeInfo.obtain(info); |
| 337 | nodes.put(sourceId, clone); |
Phil Weaver | b010b12 | 2016-08-17 17:47:48 -0700 | [diff] [blame] | 338 | if (clone.isAccessibilityFocused()) { |
Rhed Jao | ca9b526 | 2018-07-27 21:13:00 +0800 | [diff] [blame] | 339 | if (mAccessibilityFocus != AccessibilityNodeInfo.UNDEFINED_ITEM_ID |
| 340 | && mAccessibilityFocus != sourceId) { |
| 341 | refreshCachedNodeLocked(windowId, mAccessibilityFocus); |
| 342 | } |
Phil Weaver | b010b12 | 2016-08-17 17:47:48 -0700 | [diff] [blame] | 343 | mAccessibilityFocus = sourceId; |
Rhed Jao | ca9b526 | 2018-07-27 21:13:00 +0800 | [diff] [blame] | 344 | } else if (mAccessibilityFocus == sourceId) { |
| 345 | mAccessibilityFocus = AccessibilityNodeInfo.UNDEFINED_ITEM_ID; |
Phil Weaver | b010b12 | 2016-08-17 17:47:48 -0700 | [diff] [blame] | 346 | } |
| 347 | if (clone.isFocused()) { |
| 348 | mInputFocus = sourceId; |
| 349 | } |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Clears the cache. |
| 355 | */ |
| 356 | public void clear() { |
| 357 | synchronized(mLock) { |
| 358 | if (DEBUG) { |
| 359 | Log.i(LOG_TAG, "clear()"); |
| 360 | } |
Maxim Bogatov | c328120 | 2015-10-21 12:38:01 -0700 | [diff] [blame] | 361 | clearWindowCache(); |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 362 | final int nodesForWindowCount = mNodeCache.size(); |
Rhed Jao | fe89325 | 2019-03-07 19:24:34 +0800 | [diff] [blame] | 363 | for (int i = nodesForWindowCount - 1; i >= 0; i--) { |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 364 | final int windowId = mNodeCache.keyAt(i); |
| 365 | clearNodesForWindowLocked(windowId); |
| 366 | } |
Svetoslav | 27ad2e9 | 2015-02-12 17:54:15 -0800 | [diff] [blame] | 367 | |
| 368 | mAccessibilityFocus = AccessibilityNodeInfo.UNDEFINED_ITEM_ID; |
| 369 | mInputFocus = AccessibilityNodeInfo.UNDEFINED_ITEM_ID; |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
Maxim Bogatov | c328120 | 2015-10-21 12:38:01 -0700 | [diff] [blame] | 373 | private void clearWindowCache() { |
| 374 | final int windowCount = mWindowCache.size(); |
| 375 | for (int i = windowCount - 1; i >= 0; i--) { |
| 376 | AccessibilityWindowInfo window = mWindowCache.valueAt(i); |
| 377 | window.recycle(); |
| 378 | mWindowCache.removeAt(i); |
| 379 | } |
| 380 | mIsAllWindowsCached = false; |
| 381 | } |
| 382 | |
Eugene Susla | 74c6cba | 2016-12-28 14:42:54 -0800 | [diff] [blame] | 383 | /** |
| 384 | * Clears nodes for the window with the given id |
| 385 | */ |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 386 | private void clearNodesForWindowLocked(int windowId) { |
| 387 | if (DEBUG) { |
Svetoslav | 13bd771 | 2014-09-19 18:18:04 -0700 | [diff] [blame] | 388 | Log.i(LOG_TAG, "clearNodesForWindowLocked(" + windowId + ")"); |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 389 | } |
| 390 | LongSparseArray<AccessibilityNodeInfo> nodes = mNodeCache.get(windowId); |
| 391 | if (nodes == null) { |
| 392 | return; |
| 393 | } |
| 394 | // Recycle the nodes before clearing the cache. |
| 395 | final int nodeCount = nodes.size(); |
| 396 | for (int i = nodeCount - 1; i >= 0; i--) { |
| 397 | AccessibilityNodeInfo info = nodes.valueAt(i); |
| 398 | nodes.removeAt(i); |
| 399 | info.recycle(); |
| 400 | } |
| 401 | mNodeCache.remove(windowId); |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Clears a subtree rooted at the node with the given id that is |
| 406 | * hosted in a given window. |
| 407 | * |
| 408 | * @param windowId The id of the hosting window. |
| 409 | * @param rootNodeId The root id. |
| 410 | */ |
| 411 | private void clearSubTreeLocked(int windowId, long rootNodeId) { |
| 412 | if (DEBUG) { |
| 413 | Log.i(LOG_TAG, "Clearing cached subtree."); |
| 414 | } |
| 415 | LongSparseArray<AccessibilityNodeInfo> nodes = mNodeCache.get(windowId); |
| 416 | if (nodes != null) { |
| 417 | clearSubTreeRecursiveLocked(nodes, rootNodeId); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Clears a subtree given a pointer to the root id and the nodes |
| 423 | * in the hosting window. |
| 424 | * |
| 425 | * @param nodes The nodes in the hosting window. |
| 426 | * @param rootNodeId The id of the root to evict. |
Qasid Ahmad Sadiq | b2499c5 | 2019-02-28 14:35:45 -0800 | [diff] [blame] | 427 | * |
| 428 | * @return {@code true} if the cache was cleared |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 429 | */ |
Qasid Ahmad Sadiq | b2499c5 | 2019-02-28 14:35:45 -0800 | [diff] [blame] | 430 | private boolean clearSubTreeRecursiveLocked(LongSparseArray<AccessibilityNodeInfo> nodes, |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 431 | long rootNodeId) { |
| 432 | AccessibilityNodeInfo current = nodes.get(rootNodeId); |
| 433 | if (current == null) { |
Qasid Ahmad Sadiq | b2499c5 | 2019-02-28 14:35:45 -0800 | [diff] [blame] | 434 | // The node isn't in the cache, but its descendents might be. |
| 435 | clear(); |
| 436 | return true; |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 437 | } |
| 438 | nodes.remove(rootNodeId); |
| 439 | final int childCount = current.getChildCount(); |
| 440 | for (int i = 0; i < childCount; i++) { |
| 441 | final long childNodeId = current.getChildId(i); |
Qasid Ahmad Sadiq | b2499c5 | 2019-02-28 14:35:45 -0800 | [diff] [blame] | 442 | if (clearSubTreeRecursiveLocked(nodes, childNodeId)) { |
| 443 | current.recycle(); |
| 444 | return true; |
| 445 | } |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 446 | } |
Phil Weaver | b010b12 | 2016-08-17 17:47:48 -0700 | [diff] [blame] | 447 | current.recycle(); |
Qasid Ahmad Sadiq | b2499c5 | 2019-02-28 14:35:45 -0800 | [diff] [blame] | 448 | return false; |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /** |
| 452 | * Check the integrity of the cache which is nodes from different windows |
| 453 | * are not mixed, there is a single active window, there is a single focused |
| 454 | * window, for every window there are no duplicates nodes, all nodes for a |
| 455 | * window are connected, for every window there is a single input focused |
| 456 | * node, and for every window there is a single accessibility focused node. |
| 457 | */ |
| 458 | public void checkIntegrity() { |
| 459 | synchronized (mLock) { |
| 460 | // Get the root. |
| 461 | if (mWindowCache.size() <= 0 && mNodeCache.size() == 0) { |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | AccessibilityWindowInfo focusedWindow = null; |
| 466 | AccessibilityWindowInfo activeWindow = null; |
| 467 | |
| 468 | final int windowCount = mWindowCache.size(); |
| 469 | for (int i = 0; i < windowCount; i++) { |
| 470 | AccessibilityWindowInfo window = mWindowCache.valueAt(i); |
| 471 | |
| 472 | // Check for one active window. |
| 473 | if (window.isActive()) { |
| 474 | if (activeWindow != null) { |
| 475 | Log.e(LOG_TAG, "Duplicate active window:" + window); |
| 476 | } else { |
| 477 | activeWindow = window; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | // Check for one focused window. |
| 482 | if (window.isFocused()) { |
| 483 | if (focusedWindow != null) { |
| 484 | Log.e(LOG_TAG, "Duplicate focused window:" + window); |
| 485 | } else { |
| 486 | focusedWindow = window; |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | // Traverse the tree and do some checks. |
| 492 | AccessibilityNodeInfo accessFocus = null; |
| 493 | AccessibilityNodeInfo inputFocus = null; |
| 494 | |
| 495 | final int nodesForWindowCount = mNodeCache.size(); |
| 496 | for (int i = 0; i < nodesForWindowCount; i++) { |
| 497 | LongSparseArray<AccessibilityNodeInfo> nodes = mNodeCache.valueAt(i); |
| 498 | if (nodes.size() <= 0) { |
| 499 | continue; |
| 500 | } |
| 501 | |
Svetoslav | f7c42c5 | 2014-07-24 12:19:22 -0700 | [diff] [blame] | 502 | ArraySet<AccessibilityNodeInfo> seen = new ArraySet<>(); |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 503 | final int windowId = mNodeCache.keyAt(i); |
| 504 | |
| 505 | final int nodeCount = nodes.size(); |
| 506 | for (int j = 0; j < nodeCount; j++) { |
| 507 | AccessibilityNodeInfo node = nodes.valueAt(j); |
| 508 | |
| 509 | // Check for duplicates |
| 510 | if (!seen.add(node)) { |
| 511 | Log.e(LOG_TAG, "Duplicate node: " + node |
| 512 | + " in window:" + windowId); |
Svetoslav | f7c42c5 | 2014-07-24 12:19:22 -0700 | [diff] [blame] | 513 | // Stop now as we potentially found a loop. |
| 514 | continue; |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | // Check for one accessibility focus. |
| 518 | if (node.isAccessibilityFocused()) { |
| 519 | if (accessFocus != null) { |
| 520 | Log.e(LOG_TAG, "Duplicate accessibility focus:" + node |
| 521 | + " in window:" + windowId); |
| 522 | } else { |
| 523 | accessFocus = node; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | // Check for one input focus. |
| 528 | if (node.isFocused()) { |
| 529 | if (inputFocus != null) { |
| 530 | Log.e(LOG_TAG, "Duplicate input focus: " + node |
| 531 | + " in window:" + windowId); |
| 532 | } else { |
| 533 | inputFocus = node; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | // The node should be a child of its parent if we have the parent. |
| 538 | AccessibilityNodeInfo nodeParent = nodes.get(node.getParentNodeId()); |
| 539 | if (nodeParent != null) { |
| 540 | boolean childOfItsParent = false; |
| 541 | final int childCount = nodeParent.getChildCount(); |
| 542 | for (int k = 0; k < childCount; k++) { |
| 543 | AccessibilityNodeInfo child = nodes.get(nodeParent.getChildId(k)); |
| 544 | if (child == node) { |
| 545 | childOfItsParent = true; |
| 546 | break; |
| 547 | } |
| 548 | } |
| 549 | if (!childOfItsParent) { |
Svetoslav | 13bd771 | 2014-09-19 18:18:04 -0700 | [diff] [blame] | 550 | Log.e(LOG_TAG, "Invalid parent-child relation between parent: " |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 551 | + nodeParent + " and child: " + node); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | // The node should be the parent of its child if we have the child. |
| 556 | final int childCount = node.getChildCount(); |
| 557 | for (int k = 0; k < childCount; k++) { |
| 558 | AccessibilityNodeInfo child = nodes.get(node.getChildId(k)); |
| 559 | if (child != null) { |
| 560 | AccessibilityNodeInfo parent = nodes.get(child.getParentNodeId()); |
| 561 | if (parent != node) { |
Svetoslav | 13bd771 | 2014-09-19 18:18:04 -0700 | [diff] [blame] | 562 | Log.e(LOG_TAG, "Invalid child-parent relation between child: " |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 563 | + node + " and parent: " + nodeParent); |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | } |
Phil Weaver | b010b12 | 2016-08-17 17:47:48 -0700 | [diff] [blame] | 571 | |
| 572 | // Layer of indirection included to break dependency chain for testing |
| 573 | public static class AccessibilityNodeRefresher { |
| 574 | public boolean refreshNode(AccessibilityNodeInfo info, boolean bypassCache) { |
Phil Weaver | c2e2893 | 2016-12-08 12:29:25 -0800 | [diff] [blame] | 575 | return info.refresh(null, bypassCache); |
Phil Weaver | b010b12 | 2016-08-17 17:47:48 -0700 | [diff] [blame] | 576 | } |
| 577 | } |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 578 | } |