| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view.accessibility; |
| 18 |
|
| 19 |
import static android.accessibilityservice.AccessibilityTrace.FLAGS_ACCESSIBILITY_INTERACTION_CLIENT; |
| 20 |
import static android.accessibilityservice.AccessibilityTrace.FLAGS_ACCESSIBILITY_INTERACTION_CONNECTION_CALLBACK; |
| 21 |
import static android.os.Build.VERSION_CODES.S; |
| 22 |
import static android.view.accessibility.AccessibilityNodeInfo.FLAG_PREFETCH_DESCENDANTS_MASK; |
| 23 |
import static android.view.accessibility.AccessibilityNodeInfo.FLAG_PREFETCH_MASK; |
| 24 |
|
| 25 |
import android.accessibilityservice.IAccessibilityServiceConnection; |
| 26 |
import android.annotation.NonNull; |
| 27 |
import android.annotation.Nullable; |
| 28 |
import android.annotation.SuppressLint; |
| 29 |
import android.compat.annotation.UnsupportedAppUsage; |
| 30 |
import android.content.Context; |
| 31 |
import android.os.Binder; |
| 32 |
import android.os.Build; |
| 33 |
import android.os.Bundle; |
| 34 |
import android.os.IBinder; |
| 35 |
import android.os.Message; |
| 36 |
import android.os.Process; |
| 37 |
import android.os.RemoteException; |
| 38 |
import android.os.SystemClock; |
| 39 |
import android.util.Log; |
| 40 |
import android.util.LongSparseArray; |
| 41 |
import android.util.SparseArray; |
| 42 |
import android.util.SparseLongArray; |
| 43 |
import android.view.Display; |
| 44 |
import android.view.ViewConfiguration; |
| 45 |
|
| 46 |
import com.android.internal.annotations.VisibleForTesting; |
| 47 |
import com.android.internal.util.ArrayUtils; |
| 48 |
|
| 49 |
import java.util.ArrayDeque; |
| 50 |
import java.util.ArrayList; |
| 51 |
import java.util.Arrays; |
| 52 |
import java.util.Collections; |
| 53 |
import java.util.HashSet; |
| 54 |
import java.util.List; |
| 55 |
import java.util.Queue; |
| 56 |
import java.util.concurrent.atomic.AtomicInteger; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public final class AccessibilityInteractionClient |
| 91 |
extends IAccessibilityInteractionConnectionCallback.Stub { |
| 92 |
|
| 93 |
public static final int NO_ID = -1; |
| 94 |
|
| 95 |
public static final String CALL_STACK = "call_stack"; |
| 96 |
public static final String IGNORE_CALL_STACK = "ignore_call_stack"; |
| 97 |
|
| 98 |
private static final String LOG_TAG = "AccessibilityInteractionClient"; |
| 99 |
|
| 100 |
private static final boolean DEBUG = false; |
| 101 |
|
| 102 |
private static final boolean CHECK_INTEGRITY = true; |
| 103 |
|
| 104 |
private static final long TIMEOUT_INTERACTION_MILLIS = 5000; |
| 105 |
|
| 106 |
private static final long DISABLE_PREFETCHING_FOR_SCROLLING_MILLIS = |
| 107 |
(long) (ViewConfiguration.getSendRecurringAccessibilityEventsInterval() * 1.5); |
| 108 |
|
| 109 |
private static final Object sStaticLock = new Object(); |
| 110 |
|
| 111 |
private static final LongSparseArray<AccessibilityInteractionClient> sClients = |
| 112 |
new LongSparseArray<>(); |
| 113 |
|
| 114 |
private static final SparseArray<IAccessibilityServiceConnection> sConnectionCache = |
| 115 |
new SparseArray<>(); |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
private static final SparseLongArray sScrollingWindows = new SparseLongArray(); |
| 120 |
|
| 121 |
private static SparseArray<AccessibilityCache> sCaches = new SparseArray<>(); |
| 122 |
|
| 123 |
private final AtomicInteger mInteractionIdCounter = new AtomicInteger(); |
| 124 |
|
| 125 |
private final Object mInstanceLock = new Object(); |
| 126 |
|
| 127 |
private final AccessibilityManager mAccessibilityManager; |
| 128 |
|
| 129 |
private volatile int mInteractionId = -1; |
| 130 |
private volatile int mCallingUid = Process.INVALID_UID; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
private List<StackTraceElement> mCallStackOfCallback; |
| 137 |
|
| 138 |
private AccessibilityNodeInfo mFindAccessibilityNodeInfoResult; |
| 139 |
|
| 140 |
private List<AccessibilityNodeInfo> mFindAccessibilityNodeInfosResult; |
| 141 |
|
| 142 |
private boolean mPerformAccessibilityActionResult; |
| 143 |
|
| 144 |
private Message mSameThreadMessage; |
| 145 |
|
| 146 |
private int mInteractionIdWaitingForPrefetchResult = -1; |
| 147 |
private int mConnectionIdWaitingForPrefetchResult; |
| 148 |
private String[] mPackageNamesForNextPrefetchResult; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
@UnsupportedAppUsage() |
| 154 |
public static AccessibilityInteractionClient getInstance() { |
| 155 |
final long threadId = Thread.currentThread().getId(); |
| 156 |
return getInstanceForThread(threadId); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public static AccessibilityInteractionClient getInstanceForThread(long threadId) { |
| 168 |
synchronized (sStaticLock) { |
| 169 |
AccessibilityInteractionClient client = sClients.get(threadId); |
| 170 |
if (client == null) { |
| 171 |
client = new AccessibilityInteractionClient(); |
| 172 |
sClients.put(threadId, client); |
| 173 |
} |
| 174 |
return client; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public static AccessibilityInteractionClient getInstance(Context context) { |
| 182 |
final long threadId = Thread.currentThread().getId(); |
| 183 |
if (context != null) { |
| 184 |
return getInstanceForThread(threadId, context); |
| 185 |
} |
| 186 |
return getInstanceForThread(threadId); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public static AccessibilityInteractionClient getInstanceForThread(long threadId, |
| 198 |
Context context) { |
| 199 |
synchronized (sStaticLock) { |
| 200 |
AccessibilityInteractionClient client = sClients.get(threadId); |
| 201 |
if (client == null) { |
| 202 |
client = new AccessibilityInteractionClient(context); |
| 203 |
sClients.put(threadId, client); |
| 204 |
} |
| 205 |
return client; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public static IAccessibilityServiceConnection getConnection(int connectionId) { |
| 216 |
synchronized (sConnectionCache) { |
| 217 |
return sConnectionCache.get(connectionId); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public static void addConnection(int connectionId, IAccessibilityServiceConnection connection, |
| 230 |
boolean initializeCache) { |
| 231 |
if (connectionId == NO_ID) { |
| 232 |
return; |
| 233 |
} |
| 234 |
synchronized (sConnectionCache) { |
| 235 |
sConnectionCache.put(connectionId, connection); |
| 236 |
if (!initializeCache) { |
| 237 |
return; |
| 238 |
} |
| 239 |
sCaches.put(connectionId, new AccessibilityCache( |
| 240 |
new AccessibilityCache.AccessibilityNodeRefresher())); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public static AccessibilityCache getCache(int connectionId) { |
| 249 |
synchronized (sConnectionCache) { |
| 250 |
return sCaches.get(connectionId); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public static void removeConnection(int connectionId) { |
| 260 |
synchronized (sConnectionCache) { |
| 261 |
sConnectionCache.remove(connectionId); |
| 262 |
sCaches.remove(connectionId); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
@VisibleForTesting |
| 271 |
public static void setCache(int connectionId, AccessibilityCache cache) { |
| 272 |
synchronized (sConnectionCache) { |
| 273 |
sCaches.put(connectionId, cache); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
private AccessibilityInteractionClient() { |
| 278 |
|
| 279 |
mAccessibilityManager = null; |
| 280 |
} |
| 281 |
|
| 282 |
private AccessibilityInteractionClient(Context context) { |
| 283 |
mAccessibilityManager = context.getSystemService(AccessibilityManager.class); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 293 |
public void setSameThreadMessage(Message message) { |
| 294 |
synchronized (mInstanceLock) { |
| 295 |
mSameThreadMessage = message; |
| 296 |
mInstanceLock.notifyAll(); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
public AccessibilityNodeInfo getRootInActiveWindow(int connectionId, |
| 307 |
@AccessibilityNodeInfo.PrefetchingStrategy int strategy) { |
| 308 |
return findAccessibilityNodeInfoByAccessibilityId(connectionId, |
| 309 |
AccessibilityWindowInfo.ACTIVE_WINDOW_ID, AccessibilityNodeInfo.ROOT_NODE_ID, |
| 310 |
false, strategy, null); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
public AccessibilityWindowInfo getWindow(int connectionId, int accessibilityWindowId) { |
| 323 |
return getWindow(connectionId, accessibilityWindowId, false); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public AccessibilityWindowInfo getWindow(int connectionId, int accessibilityWindowId, |
| 337 |
boolean bypassCache) { |
| 338 |
try { |
| 339 |
IAccessibilityServiceConnection connection = getConnection(connectionId); |
| 340 |
if (connection != null) { |
| 341 |
AccessibilityWindowInfo window; |
| 342 |
AccessibilityCache cache = getCache(connectionId); |
| 343 |
if (cache != null) { |
| 344 |
if (!bypassCache) { |
| 345 |
window = cache.getWindow(accessibilityWindowId); |
| 346 |
if (window != null) { |
| 347 |
if (DEBUG) { |
| 348 |
Log.i(LOG_TAG, "Window cache hit"); |
| 349 |
} |
| 350 |
if (shouldTraceClient()) { |
| 351 |
logTraceClient(connection, "getWindow cache", |
| 352 |
"connectionId=" + connectionId + ";accessibilityWindowId=" |
| 353 |
+ accessibilityWindowId + ";bypassCache=false"); |
| 354 |
} |
| 355 |
return window; |
| 356 |
} |
| 357 |
if (DEBUG) { |
| 358 |
Log.i(LOG_TAG, "Window cache miss"); |
| 359 |
} |
| 360 |
} |
| 361 |
} else { |
| 362 |
if (DEBUG) { |
| 363 |
Log.w(LOG_TAG, "Cache is null for connection id: " + connectionId); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
final long identityToken = Binder.clearCallingIdentity(); |
| 368 |
try { |
| 369 |
window = connection.getWindow(accessibilityWindowId); |
| 370 |
} finally { |
| 371 |
Binder.restoreCallingIdentity(identityToken); |
| 372 |
} |
| 373 |
if (shouldTraceClient()) { |
| 374 |
logTraceClient(connection, "getWindow", "connectionId=" + connectionId |
| 375 |
+ ";accessibilityWindowId=" + accessibilityWindowId + ";bypassCache=" |
| 376 |
+ bypassCache); |
| 377 |
} |
| 378 |
|
| 379 |
if (window != null) { |
| 380 |
if (!bypassCache && cache != null) { |
| 381 |
cache.addWindow(window); |
| 382 |
} |
| 383 |
return window; |
| 384 |
} |
| 385 |
} else { |
| 386 |
if (DEBUG) { |
| 387 |
Log.w(LOG_TAG, "No connection for connection id: " + connectionId); |
| 388 |
} |
| 389 |
} |
| 390 |
} catch (RemoteException re) { |
| 391 |
Log.e(LOG_TAG, "Error while calling remote getWindow", re); |
| 392 |
} |
| 393 |
return null; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
public List<AccessibilityWindowInfo> getWindows(int connectionId) { |
| 403 |
final SparseArray<List<AccessibilityWindowInfo>> windows = |
| 404 |
getWindowsOnAllDisplays(connectionId); |
| 405 |
if (windows.size() > 0) { |
| 406 |
return windows.valueAt(Display.DEFAULT_DISPLAY); |
| 407 |
} |
| 408 |
return Collections.emptyList(); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
public SparseArray<List<AccessibilityWindowInfo>> getWindowsOnAllDisplays(int connectionId) { |
| 419 |
try { |
| 420 |
IAccessibilityServiceConnection connection = getConnection(connectionId); |
| 421 |
if (connection != null) { |
| 422 |
SparseArray<List<AccessibilityWindowInfo>> windows; |
| 423 |
AccessibilityCache cache = getCache(connectionId); |
| 424 |
if (cache != null) { |
| 425 |
windows = cache.getWindowsOnAllDisplays(); |
| 426 |
if (windows != null) { |
| 427 |
if (DEBUG) { |
| 428 |
Log.i(LOG_TAG, "Windows cache hit"); |
| 429 |
} |
| 430 |
if (shouldTraceClient()) { |
| 431 |
logTraceClient( |
| 432 |
connection, "getWindows cache", "connectionId=" + connectionId); |
| 433 |
} |
| 434 |
return windows; |
| 435 |
} |
| 436 |
if (DEBUG) { |
| 437 |
Log.i(LOG_TAG, "Windows cache miss"); |
| 438 |
} |
| 439 |
} else { |
| 440 |
if (DEBUG) { |
| 441 |
Log.w(LOG_TAG, "Cache is null for connection id: " + connectionId); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
long populationTimeStamp; |
| 446 |
final long identityToken = Binder.clearCallingIdentity(); |
| 447 |
try { |
| 448 |
populationTimeStamp = SystemClock.uptimeMillis(); |
| 449 |
windows = connection.getWindows(); |
| 450 |
} finally { |
| 451 |
Binder.restoreCallingIdentity(identityToken); |
| 452 |
} |
| 453 |
if (shouldTraceClient()) { |
| 454 |
logTraceClient(connection, "getWindows", "connectionId=" + connectionId); |
| 455 |
} |
| 456 |
if (windows != null) { |
| 457 |
if (cache != null) { |
| 458 |
cache.setWindowsOnAllDisplays(windows, populationTimeStamp); |
| 459 |
} |
| 460 |
return windows; |
| 461 |
} |
| 462 |
} else { |
| 463 |
if (DEBUG) { |
| 464 |
Log.w(LOG_TAG, "No connection for connection id: " + connectionId); |
| 465 |
} |
| 466 |
} |
| 467 |
} catch (RemoteException re) { |
| 468 |
Log.e(LOG_TAG, "Error while calling remote getWindowsOnAllDisplays", re); |
| 469 |
} |
| 470 |
|
| 471 |
final SparseArray<List<AccessibilityWindowInfo>> emptyWindows = new SparseArray<>(); |
| 472 |
return emptyWindows; |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
public @Nullable AccessibilityNodeInfo findAccessibilityNodeInfoByAccessibilityId( |
| 492 |
int connectionId, @NonNull IBinder leashToken, long accessibilityNodeId, |
| 493 |
boolean bypassCache, int prefetchFlags, Bundle arguments) { |
| 494 |
if (leashToken == null) { |
| 495 |
return null; |
| 496 |
} |
| 497 |
int windowId = -1; |
| 498 |
try { |
| 499 |
IAccessibilityServiceConnection connection = getConnection(connectionId); |
| 500 |
if (connection != null) { |
| 501 |
windowId = connection.getWindowIdForLeashToken(leashToken); |
| 502 |
} else { |
| 503 |
if (DEBUG) { |
| 504 |
Log.w(LOG_TAG, "No connection for connection id: " + connectionId); |
| 505 |
} |
| 506 |
} |
| 507 |
} catch (RemoteException re) { |
| 508 |
Log.e(LOG_TAG, "Error while calling remote getWindowIdForLeashToken", re); |
| 509 |
} |
| 510 |
if (windowId == -1) { |
| 511 |
return null; |
| 512 |
} |
| 513 |
return findAccessibilityNodeInfoByAccessibilityId(connectionId, windowId, |
| 514 |
accessibilityNodeId, bypassCache, prefetchFlags, arguments); |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
public @Nullable AccessibilityNodeInfo findAccessibilityNodeInfoByAccessibilityId( |
| 533 |
int connectionId, int accessibilityWindowId, long accessibilityNodeId, |
| 534 |
boolean bypassCache, int prefetchFlags, Bundle arguments) { |
| 535 |
try { |
| 536 |
IAccessibilityServiceConnection connection = getConnection(connectionId); |
| 537 |
if (connection != null) { |
| 538 |
if (!bypassCache) { |
| 539 |
AccessibilityCache cache = getCache(connectionId); |
| 540 |
if (cache != null) { |
| 541 |
AccessibilityNodeInfo cachedInfo = cache.getNode( |
| 542 |
accessibilityWindowId, accessibilityNodeId); |
| 543 |
if (cachedInfo != null) { |
| 544 |
if (DEBUG) { |
| 545 |
Log.i(LOG_TAG, "Node cache hit for " |
| 546 |
+ idToString(accessibilityWindowId, accessibilityNodeId)); |
| 547 |
} |
| 548 |
if (shouldTraceClient()) { |
| 549 |
logTraceClient(connection, |
| 550 |
"findAccessibilityNodeInfoByAccessibilityId cache", |
| 551 |
"connectionId=" + connectionId + ";accessibilityWindowId=" |
| 552 |
+ accessibilityWindowId + ";accessibilityNodeId=" |
| 553 |
+ accessibilityNodeId + ";bypassCache=" |
| 554 |
+ bypassCache + ";prefetchFlags=" + prefetchFlags |
| 555 |
+ ";arguments=" + arguments); |
| 556 |
} |
| 557 |
return cachedInfo; |
| 558 |
} |
| 559 |
if (!cache.isEnabled()) { |
| 560 |
|
| 561 |
prefetchFlags &= ~FLAG_PREFETCH_MASK; |
| 562 |
} |
| 563 |
if (DEBUG) { |
| 564 |
Log.i(LOG_TAG, "Node cache miss for " |
| 565 |
+ idToString(accessibilityWindowId, accessibilityNodeId)); |
| 566 |
} |
| 567 |
} else { |
| 568 |
if (DEBUG) { |
| 569 |
Log.w(LOG_TAG, "Cache is null for connection id: " + connectionId); |
| 570 |
} |
| 571 |
} |
| 572 |
} else { |
| 573 |
|
| 574 |
prefetchFlags &= ~FLAG_PREFETCH_MASK; |
| 575 |
} |
| 576 |
|
| 577 |
if ((prefetchFlags & FLAG_PREFETCH_MASK) != 0 |
| 578 |
&& isWindowScrolling(accessibilityWindowId)) { |
| 579 |
prefetchFlags &= ~FLAG_PREFETCH_MASK; |
| 580 |
} |
| 581 |
|
| 582 |
final int descendantPrefetchFlags = prefetchFlags & FLAG_PREFETCH_DESCENDANTS_MASK; |
| 583 |
if ((descendantPrefetchFlags & (descendantPrefetchFlags - 1)) != 0) { |
| 584 |
throw new IllegalArgumentException("There can be no more than one descendant" |
| 585 |
+ " prefetching strategy"); |
| 586 |
} |
| 587 |
final int interactionId = mInteractionIdCounter.getAndIncrement(); |
| 588 |
if (shouldTraceClient()) { |
| 589 |
logTraceClient(connection, "findAccessibilityNodeInfoByAccessibilityId", |
| 590 |
"InteractionId:" + interactionId + "connectionId=" + connectionId |
| 591 |
+ ";accessibilityWindowId=" + accessibilityWindowId |
| 592 |
+ ";accessibilityNodeId=" + accessibilityNodeId + ";bypassCache=" |
| 593 |
+ bypassCache + ";prefetchFlags=" + prefetchFlags + ";arguments=" |
| 594 |
+ arguments); |
| 595 |
} |
| 596 |
final String[] packageNames; |
| 597 |
final long identityToken = Binder.clearCallingIdentity(); |
| 598 |
try { |
| 599 |
packageNames = connection.findAccessibilityNodeInfoByAccessibilityId( |
| 600 |
accessibilityWindowId, accessibilityNodeId, interactionId, this, |
| 601 |
prefetchFlags, Thread.currentThread().getId(), arguments); |
| 602 |
} finally { |
| 603 |
Binder.restoreCallingIdentity(identityToken); |
| 604 |
} |
| 605 |
if (packageNames != null) { |
| 606 |
if ((prefetchFlags |
| 607 |
& AccessibilityNodeInfo.FLAG_PREFETCH_UNINTERRUPTIBLE) != 0) { |
| 608 |
List<AccessibilityNodeInfo> infos = |
| 609 |
getFindAccessibilityNodeInfosResultAndClear( |
| 610 |
interactionId); |
| 611 |
if (shouldTraceCallback()) { |
| 612 |
logTraceCallback(connection, |
| 613 |
"findAccessibilityNodeInfoByAccessibilityId", |
| 614 |
"InteractionId:" + interactionId + ";connectionId=" |
| 615 |
+ connectionId + ";Result: " + infos); |
| 616 |
} |
| 617 |
finalizeAndCacheAccessibilityNodeInfos(infos, connectionId, |
| 618 |
bypassCache, packageNames); |
| 619 |
if (infos != null && !infos.isEmpty()) { |
| 620 |
return infos.get(0); |
| 621 |
} |
| 622 |
} else { |
| 623 |
AccessibilityNodeInfo info = |
| 624 |
getFindAccessibilityNodeInfoResultAndClear(interactionId); |
| 625 |
if (shouldTraceCallback()) { |
| 626 |
logTraceCallback(connection, |
| 627 |
"findAccessibilityNodeInfoByAccessibilityId", |
| 628 |
"InteractionId:" + interactionId + ";connectionId=" |
| 629 |
+ connectionId + ";Result: " + info); |
| 630 |
} |
| 631 |
if ((prefetchFlags & FLAG_PREFETCH_MASK) != 0 |
| 632 |
&& info != null) { |
| 633 |
setInteractionWaitingForPrefetchResult(interactionId, connectionId, |
| 634 |
packageNames); |
| 635 |
} |
| 636 |
finalizeAndCacheAccessibilityNodeInfo(info, connectionId, |
| 637 |
bypassCache, packageNames); |
| 638 |
return info; |
| 639 |
} |
| 640 |
|
| 641 |
} |
| 642 |
} else { |
| 643 |
if (DEBUG) { |
| 644 |
Log.w(LOG_TAG, "No connection for connection id: " + connectionId); |
| 645 |
} |
| 646 |
} |
| 647 |
} catch (RemoteException re) { |
| 648 |
Log.e(LOG_TAG, "Error while calling remote" |
| 649 |
+ " findAccessibilityNodeInfoByAccessibilityId", re); |
| 650 |
} |
| 651 |
return null; |
| 652 |
} |
| 653 |
|
| 654 |
private void setInteractionWaitingForPrefetchResult(int interactionId, int connectionId, |
| 655 |
String[] packageNames) { |
| 656 |
synchronized (mInstanceLock) { |
| 657 |
mInteractionIdWaitingForPrefetchResult = interactionId; |
| 658 |
mConnectionIdWaitingForPrefetchResult = connectionId; |
| 659 |
mPackageNamesForNextPrefetchResult = packageNames; |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
private static String idToString(int accessibilityWindowId, long accessibilityNodeId) { |
| 664 |
return accessibilityWindowId + "/" |
| 665 |
+ AccessibilityNodeInfo.idToString(accessibilityNodeId); |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
public List<AccessibilityNodeInfo> findAccessibilityNodeInfosByViewId(int connectionId, |
| 685 |
int accessibilityWindowId, long accessibilityNodeId, String viewId) { |
| 686 |
try { |
| 687 |
IAccessibilityServiceConnection connection = getConnection(connectionId); |
| 688 |
if (connection != null) { |
| 689 |
final int interactionId = mInteractionIdCounter.getAndIncrement(); |
| 690 |
final String[] packageNames; |
| 691 |
final long identityToken = Binder.clearCallingIdentity(); |
| 692 |
try { |
| 693 |
if (shouldTraceClient()) { |
| 694 |
logTraceClient(connection, "findAccessibilityNodeInfosByViewId", |
| 695 |
"InteractionId=" + interactionId + ";connectionId=" + connectionId |
| 696 |
+ ";accessibilityWindowId=" + accessibilityWindowId |
| 697 |
+ ";accessibilityNodeId=" + accessibilityNodeId + ";viewId=" |
| 698 |
+ viewId); |
| 699 |
} |
| 700 |
|
| 701 |
packageNames = connection.findAccessibilityNodeInfosByViewId( |
| 702 |
accessibilityWindowId, accessibilityNodeId, viewId, interactionId, this, |
| 703 |
Thread.currentThread().getId()); |
| 704 |
} finally { |
| 705 |
Binder.restoreCallingIdentity(identityToken); |
| 706 |
} |
| 707 |
|
| 708 |
if (packageNames != null) { |
| 709 |
List<AccessibilityNodeInfo> infos = getFindAccessibilityNodeInfosResultAndClear( |
| 710 |
interactionId); |
| 711 |
if (shouldTraceCallback()) { |
| 712 |
logTraceCallback(connection, "findAccessibilityNodeInfosByViewId", |
| 713 |
"InteractionId=" + interactionId + ";connectionId=" + connectionId |
| 714 |
+ ":Result: " + infos); |
| 715 |
} |
| 716 |
if (infos != null) { |
| 717 |
finalizeAndCacheAccessibilityNodeInfos(infos, connectionId, |
| 718 |
false, packageNames); |
| 719 |
return infos; |
| 720 |
} |
| 721 |
} |
| 722 |
} else { |
| 723 |
if (DEBUG) { |
| 724 |
Log.w(LOG_TAG, "No connection for connection id: " + connectionId); |
| 725 |
} |
| 726 |
} |
| 727 |
} catch (RemoteException re) { |
| 728 |
Log.w(LOG_TAG, "Error while calling remote" |
| 729 |
+ " findAccessibilityNodeInfoByViewIdInActiveWindow", re); |
| 730 |
} |
| 731 |
return Collections.emptyList(); |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
public List<AccessibilityNodeInfo> findAccessibilityNodeInfosByText(int connectionId, |
| 752 |
int accessibilityWindowId, long accessibilityNodeId, String text) { |
| 753 |
try { |
| 754 |
IAccessibilityServiceConnection connection = getConnection(connectionId); |
| 755 |
if (connection != null) { |
| 756 |
final int interactionId = mInteractionIdCounter.getAndIncrement(); |
| 757 |
if (shouldTraceClient()) { |
| 758 |
logTraceClient(connection, "findAccessibilityNodeInfosByText", |
| 759 |
"InteractionId:" + interactionId + "connectionId=" + connectionId |
| 760 |
+ ";accessibilityWindowId=" + accessibilityWindowId |
| 761 |
+ ";accessibilityNodeId=" + accessibilityNodeId + ";text=" + text); |
| 762 |
} |
| 763 |
final String[] packageNames; |
| 764 |
final long identityToken = Binder.clearCallingIdentity(); |
| 765 |
try { |
| 766 |
packageNames = connection.findAccessibilityNodeInfosByText( |
| 767 |
accessibilityWindowId, accessibilityNodeId, text, interactionId, this, |
| 768 |
Thread.currentThread().getId()); |
| 769 |
} finally { |
| 770 |
Binder.restoreCallingIdentity(identityToken); |
| 771 |
} |
| 772 |
|
| 773 |
if (packageNames != null) { |
| 774 |
List<AccessibilityNodeInfo> infos = getFindAccessibilityNodeInfosResultAndClear( |
| 775 |
interactionId); |
| 776 |
if (shouldTraceCallback()) { |
| 777 |
logTraceCallback(connection, "findAccessibilityNodeInfosByText", |
| 778 |
"InteractionId=" + interactionId + ";connectionId=" + connectionId |
| 779 |
+ ";Result: " + infos); |
| 780 |
} |
| 781 |
if (infos != null) { |
| 782 |
finalizeAndCacheAccessibilityNodeInfos(infos, connectionId, |
| 783 |
false, packageNames); |
| 784 |
return infos; |
| 785 |
} |
| 786 |
} |
| 787 |
} else { |
| 788 |
if (DEBUG) { |
| 789 |
Log.w(LOG_TAG, "No connection for connection id: " + connectionId); |
| 790 |
} |
| 791 |
} |
| 792 |
} catch (RemoteException re) { |
| 793 |
Log.w(LOG_TAG, "Error while calling remote" |
| 794 |
+ " findAccessibilityNodeInfosByViewText", re); |
| 795 |
} |
| 796 |
return Collections.emptyList(); |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
@SuppressLint("LongLogTag") |
| 818 |
public AccessibilityNodeInfo findFocus(int connectionId, int accessibilityWindowId, |
| 819 |
long accessibilityNodeId, int focusType) { |
| 820 |
try { |
| 821 |
IAccessibilityServiceConnection connection = getConnection(connectionId); |
| 822 |
if (connection != null) { |
| 823 |
AccessibilityCache cache = getCache(connectionId); |
| 824 |
if (cache != null) { |
| 825 |
AccessibilityNodeInfo cachedInfo = cache.getFocus(focusType, |
| 826 |
accessibilityNodeId, accessibilityWindowId); |
| 827 |
if (cachedInfo != null) { |
| 828 |
if (DEBUG) { |
| 829 |
Log.i(LOG_TAG, "Focused node cache hit retrieved" |
| 830 |
+ idToString(cachedInfo.getWindowId(), |
| 831 |
cachedInfo.getSourceNodeId())); |
| 832 |
} |
| 833 |
return cachedInfo; |
| 834 |
} |
| 835 |
if (DEBUG) { |
| 836 |
Log.i(LOG_TAG, "Focused node cache miss with " |
| 837 |
+ idToString(accessibilityWindowId, accessibilityNodeId)); |
| 838 |
} |
| 839 |
} else { |
| 840 |
if (DEBUG) { |
| 841 |
Log.w(LOG_TAG, "Cache is null for connection id: " + connectionId); |
| 842 |
} |
| 843 |
} |
| 844 |
final int interactionId = mInteractionIdCounter.getAndIncrement(); |
| 845 |
if (shouldTraceClient()) { |
| 846 |
logTraceClient(connection, "findFocus", |
| 847 |
"InteractionId:" + interactionId + "connectionId=" + connectionId |
| 848 |
+ ";accessibilityWindowId=" + accessibilityWindowId |
| 849 |
+ ";accessibilityNodeId=" + accessibilityNodeId + ";focusType=" |
| 850 |
+ focusType); |
| 851 |
} |
| 852 |
final String[] packageNames; |
| 853 |
final long identityToken = Binder.clearCallingIdentity(); |
| 854 |
try { |
| 855 |
packageNames = connection.findFocus(accessibilityWindowId, |
| 856 |
accessibilityNodeId, focusType, interactionId, this, |
| 857 |
Thread.currentThread().getId()); |
| 858 |
} finally { |
| 859 |
Binder.restoreCallingIdentity(identityToken); |
| 860 |
} |
| 861 |
|
| 862 |
if (packageNames != null) { |
| 863 |
AccessibilityNodeInfo info = getFindAccessibilityNodeInfoResultAndClear( |
| 864 |
interactionId); |
| 865 |
if (shouldTraceCallback()) { |
| 866 |
logTraceCallback(connection, "findFocus", "InteractionId=" + interactionId |
| 867 |
+ ";connectionId=" + connectionId + ";Result:" + info); |
| 868 |
} |
| 869 |
finalizeAndCacheAccessibilityNodeInfo(info, connectionId, false, packageNames); |
| 870 |
return info; |
| 871 |
} |
| 872 |
} else { |
| 873 |
if (DEBUG) { |
| 874 |
Log.w(LOG_TAG, "No connection for connection id: " + connectionId); |
| 875 |
} |
| 876 |
} |
| 877 |
} catch (RemoteException re) { |
| 878 |
Log.w(LOG_TAG, "Error while calling remote findFocus", re); |
| 879 |
} |
| 880 |
return null; |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
public AccessibilityNodeInfo focusSearch(int connectionId, int accessibilityWindowId, |
| 900 |
long accessibilityNodeId, int direction) { |
| 901 |
try { |
| 902 |
IAccessibilityServiceConnection connection = getConnection(connectionId); |
| 903 |
if (connection != null) { |
| 904 |
final int interactionId = mInteractionIdCounter.getAndIncrement(); |
| 905 |
if (shouldTraceClient()) { |
| 906 |
logTraceClient(connection, "focusSearch", |
| 907 |
"InteractionId:" + interactionId + "connectionId=" + connectionId |
| 908 |
+ ";accessibilityWindowId=" + accessibilityWindowId |
| 909 |
+ ";accessibilityNodeId=" + accessibilityNodeId + ";direction=" |
| 910 |
+ direction); |
| 911 |
} |
| 912 |
final String[] packageNames; |
| 913 |
final long identityToken = Binder.clearCallingIdentity(); |
| 914 |
try { |
| 915 |
packageNames = connection.focusSearch(accessibilityWindowId, |
| 916 |
accessibilityNodeId, direction, interactionId, this, |
| 917 |
Thread.currentThread().getId()); |
| 918 |
} finally { |
| 919 |
Binder.restoreCallingIdentity(identityToken); |
| 920 |
} |
| 921 |
|
| 922 |
if (packageNames != null) { |
| 923 |
AccessibilityNodeInfo info = getFindAccessibilityNodeInfoResultAndClear( |
| 924 |
interactionId); |
| 925 |
finalizeAndCacheAccessibilityNodeInfo(info, connectionId, false, packageNames); |
| 926 |
if (shouldTraceCallback()) { |
| 927 |
logTraceCallback(connection, "focusSearch", "InteractionId=" + interactionId |
| 928 |
+ ";connectionId=" + connectionId + ";Result:" + info); |
| 929 |
} |
| 930 |
return info; |
| 931 |
} |
| 932 |
} else { |
| 933 |
if (DEBUG) { |
| 934 |
Log.w(LOG_TAG, "No connection for connection id: " + connectionId); |
| 935 |
} |
| 936 |
} |
| 937 |
} catch (RemoteException re) { |
| 938 |
Log.w(LOG_TAG, "Error while calling remote accessibilityFocusSearch", re); |
| 939 |
} |
| 940 |
return null; |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
public boolean performAccessibilityAction(int connectionId, int accessibilityWindowId, |
| 959 |
long accessibilityNodeId, int action, Bundle arguments) { |
| 960 |
try { |
| 961 |
IAccessibilityServiceConnection connection = getConnection(connectionId); |
| 962 |
if (connection != null) { |
| 963 |
final int interactionId = mInteractionIdCounter.getAndIncrement(); |
| 964 |
if (shouldTraceClient()) { |
| 965 |
logTraceClient(connection, "performAccessibilityAction", |
| 966 |
"InteractionId:" + interactionId + "connectionId=" + connectionId |
| 967 |
+ ";accessibilityWindowId=" + accessibilityWindowId |
| 968 |
+ ";accessibilityNodeId=" + accessibilityNodeId + ";action=" + action |
| 969 |
+ ";arguments=" + arguments); |
| 970 |
} |
| 971 |
final boolean success; |
| 972 |
final long identityToken = Binder.clearCallingIdentity(); |
| 973 |
try { |
| 974 |
success = connection.performAccessibilityAction( |
| 975 |
accessibilityWindowId, accessibilityNodeId, action, arguments, |
| 976 |
interactionId, this, Thread.currentThread().getId()); |
| 977 |
} finally { |
| 978 |
Binder.restoreCallingIdentity(identityToken); |
| 979 |
} |
| 980 |
|
| 981 |
if (success) { |
| 982 |
final boolean result = |
| 983 |
getPerformAccessibilityActionResultAndClear(interactionId); |
| 984 |
if (shouldTraceCallback()) { |
| 985 |
logTraceCallback(connection, "performAccessibilityAction", |
| 986 |
"InteractionId=" + interactionId + ";connectionId=" + connectionId |
| 987 |
+ ";Result: " + result); |
| 988 |
} |
| 989 |
return result; |
| 990 |
} |
| 991 |
} else { |
| 992 |
if (DEBUG) { |
| 993 |
Log.w(LOG_TAG, "No connection for connection id: " + connectionId); |
| 994 |
} |
| 995 |
} |
| 996 |
} catch (RemoteException re) { |
| 997 |
Log.w(LOG_TAG, "Error while calling remote performAccessibilityAction", re); |
| 998 |
} |
| 999 |
return false; |
| 1000 |
} |
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
@UnsupportedAppUsage(maxTargetSdk = S, publicAlternatives = |
| 1007 |
"{@link android.accessibilityservice.AccessibilityService#clearCache()}") |
| 1008 |
public void clearCache(int connectionId) { |
| 1009 |
AccessibilityCache cache = getCache(connectionId); |
| 1010 |
if (cache == null) { |
| 1011 |
return; |
| 1012 |
} |
| 1013 |
cache.clear(); |
| 1014 |
} |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
public void onAccessibilityEvent(AccessibilityEvent event, int connectionId) { |
| 1022 |
switch (event.getEventType()) { |
| 1023 |
case AccessibilityEvent.TYPE_VIEW_SCROLLED: |
| 1024 |
updateScrollingWindow(event.getWindowId(), SystemClock.uptimeMillis()); |
| 1025 |
break; |
| 1026 |
case AccessibilityEvent.TYPE_WINDOWS_CHANGED: |
| 1027 |
if (event.getWindowChanges() == AccessibilityEvent.WINDOWS_CHANGE_REMOVED) { |
| 1028 |
deleteScrollingWindow(event.getWindowId()); |
| 1029 |
} |
| 1030 |
break; |
| 1031 |
default: |
| 1032 |
break; |
| 1033 |
} |
| 1034 |
AccessibilityCache cache = getCache(connectionId); |
| 1035 |
if (cache == null) { |
| 1036 |
if (DEBUG) { |
| 1037 |
Log.w(LOG_TAG, "Cache is null for connection id: " + connectionId); |
| 1038 |
} |
| 1039 |
return; |
| 1040 |
} |
| 1041 |
cache.onAccessibilityEvent(event); |
| 1042 |
} |
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
private AccessibilityNodeInfo getFindAccessibilityNodeInfoResultAndClear(int interactionId) { |
| 1051 |
synchronized (mInstanceLock) { |
| 1052 |
final boolean success = waitForResultTimedLocked(interactionId); |
| 1053 |
AccessibilityNodeInfo result = success ? mFindAccessibilityNodeInfoResult : null; |
| 1054 |
clearResultLocked(); |
| 1055 |
return result; |
| 1056 |
} |
| 1057 |
} |
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
public void setFindAccessibilityNodeInfoResult(AccessibilityNodeInfo info, |
| 1063 |
int interactionId) { |
| 1064 |
synchronized (mInstanceLock) { |
| 1065 |
if (interactionId > mInteractionId) { |
| 1066 |
mFindAccessibilityNodeInfoResult = info; |
| 1067 |
mInteractionId = interactionId; |
| 1068 |
mCallingUid = Binder.getCallingUid(); |
| 1069 |
mCallStackOfCallback = new ArrayList<StackTraceElement>( |
| 1070 |
Arrays.asList(Thread.currentThread().getStackTrace())); |
| 1071 |
} |
| 1072 |
mInstanceLock.notifyAll(); |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
private List<AccessibilityNodeInfo> getFindAccessibilityNodeInfosResultAndClear( |
| 1083 |
int interactionId) { |
| 1084 |
synchronized (mInstanceLock) { |
| 1085 |
final boolean success = waitForResultTimedLocked(interactionId); |
| 1086 |
final List<AccessibilityNodeInfo> result; |
| 1087 |
if (success) { |
| 1088 |
result = mFindAccessibilityNodeInfosResult; |
| 1089 |
} else { |
| 1090 |
result = Collections.emptyList(); |
| 1091 |
} |
| 1092 |
clearResultLocked(); |
| 1093 |
if (Build.IS_DEBUGGABLE && CHECK_INTEGRITY) { |
| 1094 |
checkFindAccessibilityNodeInfoResultIntegrity(result); |
| 1095 |
} |
| 1096 |
return result; |
| 1097 |
} |
| 1098 |
} |
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
public void setFindAccessibilityNodeInfosResult(List<AccessibilityNodeInfo> infos, |
| 1104 |
int interactionId) { |
| 1105 |
synchronized (mInstanceLock) { |
| 1106 |
if (interactionId > mInteractionId) { |
| 1107 |
if (infos != null) { |
| 1108 |
|
| 1109 |
|
| 1110 |
final boolean isIpcCall = (Binder.getCallingPid() != Process.myPid()); |
| 1111 |
if (!isIpcCall) { |
| 1112 |
mFindAccessibilityNodeInfosResult = new ArrayList<>(infos); |
| 1113 |
} else { |
| 1114 |
mFindAccessibilityNodeInfosResult = infos; |
| 1115 |
} |
| 1116 |
} else { |
| 1117 |
mFindAccessibilityNodeInfosResult = Collections.emptyList(); |
| 1118 |
} |
| 1119 |
mInteractionId = interactionId; |
| 1120 |
mCallingUid = Binder.getCallingUid(); |
| 1121 |
mCallStackOfCallback = new ArrayList<StackTraceElement>( |
| 1122 |
Arrays.asList(Thread.currentThread().getStackTrace())); |
| 1123 |
} |
| 1124 |
mInstanceLock.notifyAll(); |
| 1125 |
} |
| 1126 |
} |
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
@Override |
| 1132 |
public void setPrefetchAccessibilityNodeInfoResult(@NonNull List<AccessibilityNodeInfo> infos, |
| 1133 |
int interactionId) { |
| 1134 |
int interactionIdWaitingForPrefetchResultCopy = -1; |
| 1135 |
int connectionIdWaitingForPrefetchResultCopy = -1; |
| 1136 |
String[] packageNamesForNextPrefetchResultCopy = null; |
| 1137 |
|
| 1138 |
if (infos.isEmpty()) { |
| 1139 |
return; |
| 1140 |
} |
| 1141 |
|
| 1142 |
synchronized (mInstanceLock) { |
| 1143 |
if (mInteractionIdWaitingForPrefetchResult == interactionId) { |
| 1144 |
interactionIdWaitingForPrefetchResultCopy = mInteractionIdWaitingForPrefetchResult; |
| 1145 |
connectionIdWaitingForPrefetchResultCopy = |
| 1146 |
mConnectionIdWaitingForPrefetchResult; |
| 1147 |
if (mPackageNamesForNextPrefetchResult != null) { |
| 1148 |
packageNamesForNextPrefetchResultCopy = |
| 1149 |
new String[mPackageNamesForNextPrefetchResult.length]; |
| 1150 |
for (int i = 0; i < mPackageNamesForNextPrefetchResult.length; i++) { |
| 1151 |
packageNamesForNextPrefetchResultCopy[i] = |
| 1152 |
mPackageNamesForNextPrefetchResult[i]; |
| 1153 |
} |
| 1154 |
} |
| 1155 |
} |
| 1156 |
} |
| 1157 |
|
| 1158 |
if (interactionIdWaitingForPrefetchResultCopy == interactionId) { |
| 1159 |
finalizeAndCacheAccessibilityNodeInfos( |
| 1160 |
infos, connectionIdWaitingForPrefetchResultCopy, false, |
| 1161 |
packageNamesForNextPrefetchResultCopy); |
| 1162 |
if (shouldTraceCallback()) { |
| 1163 |
logTrace(getConnection(connectionIdWaitingForPrefetchResultCopy), |
| 1164 |
"setPrefetchAccessibilityNodeInfoResult", |
| 1165 |
"InteractionId:" + interactionId + ";connectionId=" |
| 1166 |
+ connectionIdWaitingForPrefetchResultCopy + ";Result: " + infos, |
| 1167 |
Binder.getCallingUid(), |
| 1168 |
Arrays.asList(Thread.currentThread().getStackTrace()), |
| 1169 |
new HashSet<>(Collections.singletonList("getStackTrace")), |
| 1170 |
FLAGS_ACCESSIBILITY_INTERACTION_CONNECTION_CALLBACK); |
| 1171 |
} |
| 1172 |
} else if (DEBUG) { |
| 1173 |
Log.w(LOG_TAG, "Prefetching for interaction with id " + interactionId + " dropped " |
| 1174 |
+ infos.size() + " nodes"); |
| 1175 |
} |
| 1176 |
} |
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
private boolean getPerformAccessibilityActionResultAndClear(int interactionId) { |
| 1185 |
synchronized (mInstanceLock) { |
| 1186 |
final boolean success = waitForResultTimedLocked(interactionId); |
| 1187 |
final boolean result = success ? mPerformAccessibilityActionResult : false; |
| 1188 |
clearResultLocked(); |
| 1189 |
return result; |
| 1190 |
} |
| 1191 |
} |
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
public void setPerformAccessibilityActionResult(boolean succeeded, int interactionId) { |
| 1197 |
synchronized (mInstanceLock) { |
| 1198 |
if (interactionId > mInteractionId) { |
| 1199 |
mPerformAccessibilityActionResult = succeeded; |
| 1200 |
mInteractionId = interactionId; |
| 1201 |
mCallingUid = Binder.getCallingUid(); |
| 1202 |
mCallStackOfCallback = new ArrayList<StackTraceElement>( |
| 1203 |
Arrays.asList(Thread.currentThread().getStackTrace())); |
| 1204 |
} |
| 1205 |
mInstanceLock.notifyAll(); |
| 1206 |
} |
| 1207 |
} |
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
private void clearResultLocked() { |
| 1213 |
mInteractionId = -1; |
| 1214 |
mFindAccessibilityNodeInfoResult = null; |
| 1215 |
mFindAccessibilityNodeInfosResult = null; |
| 1216 |
mPerformAccessibilityActionResult = false; |
| 1217 |
} |
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
private boolean waitForResultTimedLocked(int interactionId) { |
| 1226 |
long waitTimeMillis = TIMEOUT_INTERACTION_MILLIS; |
| 1227 |
final long startTimeMillis = SystemClock.uptimeMillis(); |
| 1228 |
while (true) { |
| 1229 |
try { |
| 1230 |
Message sameProcessMessage = getSameProcessMessageAndClear(); |
| 1231 |
if (sameProcessMessage != null) { |
| 1232 |
sameProcessMessage.getTarget().handleMessage(sameProcessMessage); |
| 1233 |
} |
| 1234 |
|
| 1235 |
if (mInteractionId == interactionId) { |
| 1236 |
return true; |
| 1237 |
} |
| 1238 |
if (mInteractionId > interactionId) { |
| 1239 |
return false; |
| 1240 |
} |
| 1241 |
final long elapsedTimeMillis = SystemClock.uptimeMillis() - startTimeMillis; |
| 1242 |
waitTimeMillis = TIMEOUT_INTERACTION_MILLIS - elapsedTimeMillis; |
| 1243 |
if (waitTimeMillis <= 0) { |
| 1244 |
return false; |
| 1245 |
} |
| 1246 |
mInstanceLock.wait(waitTimeMillis); |
| 1247 |
} catch (InterruptedException ie) { |
| 1248 |
|
| 1249 |
} |
| 1250 |
} |
| 1251 |
} |
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
private void finalizeAndCacheAccessibilityNodeInfo(AccessibilityNodeInfo info, |
| 1263 |
int connectionId, boolean bypassCache, String[] packageNames) { |
| 1264 |
if (info != null) { |
| 1265 |
info.setConnectionId(connectionId); |
| 1266 |
|
| 1267 |
if (!ArrayUtils.isEmpty(packageNames)) { |
| 1268 |
CharSequence packageName = info.getPackageName(); |
| 1269 |
if (packageName == null |
| 1270 |
|| !ArrayUtils.contains(packageNames, packageName.toString())) { |
| 1271 |
|
| 1272 |
|
| 1273 |
info.setPackageName(packageNames[0]); |
| 1274 |
} |
| 1275 |
} |
| 1276 |
info.setSealed(true); |
| 1277 |
if (!bypassCache) { |
| 1278 |
AccessibilityCache cache = getCache(connectionId); |
| 1279 |
if (cache == null) { |
| 1280 |
if (DEBUG) { |
| 1281 |
Log.w(LOG_TAG, "Cache is null for connection id: " + connectionId); |
| 1282 |
} |
| 1283 |
return; |
| 1284 |
} |
| 1285 |
cache.add(info); |
| 1286 |
} |
| 1287 |
} |
| 1288 |
} |
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
private void finalizeAndCacheAccessibilityNodeInfos(List<AccessibilityNodeInfo> infos, |
| 1300 |
int connectionId, boolean bypassCache, String[] packageNames) { |
| 1301 |
if (infos != null) { |
| 1302 |
final int infosCount = infos.size(); |
| 1303 |
for (int i = 0; i < infosCount; i++) { |
| 1304 |
AccessibilityNodeInfo info = infos.get(i); |
| 1305 |
finalizeAndCacheAccessibilityNodeInfo(info, connectionId, |
| 1306 |
bypassCache, packageNames); |
| 1307 |
} |
| 1308 |
} |
| 1309 |
} |
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
private Message getSameProcessMessageAndClear() { |
| 1318 |
synchronized (mInstanceLock) { |
| 1319 |
Message result = mSameThreadMessage; |
| 1320 |
mSameThreadMessage = null; |
| 1321 |
return result; |
| 1322 |
} |
| 1323 |
} |
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
private void checkFindAccessibilityNodeInfoResultIntegrity(List<AccessibilityNodeInfo> infos) { |
| 1331 |
if (infos.size() == 0) { |
| 1332 |
return; |
| 1333 |
} |
| 1334 |
|
| 1335 |
AccessibilityNodeInfo root = infos.get(0); |
| 1336 |
final int infoCount = infos.size(); |
| 1337 |
for (int i = 1; i < infoCount; i++) { |
| 1338 |
for (int j = i; j < infoCount; j++) { |
| 1339 |
AccessibilityNodeInfo candidate = infos.get(j); |
| 1340 |
if (root.getParentNodeId() == candidate.getSourceNodeId()) { |
| 1341 |
root = candidate; |
| 1342 |
break; |
| 1343 |
} |
| 1344 |
} |
| 1345 |
} |
| 1346 |
if (root == null) { |
| 1347 |
Log.e(LOG_TAG, "No root."); |
| 1348 |
} |
| 1349 |
|
| 1350 |
HashSet<AccessibilityNodeInfo> seen = new HashSet<>(); |
| 1351 |
Queue<AccessibilityNodeInfo> fringe = new ArrayDeque<>(); |
| 1352 |
fringe.add(root); |
| 1353 |
while (!fringe.isEmpty()) { |
| 1354 |
AccessibilityNodeInfo current = fringe.poll(); |
| 1355 |
if (!seen.add(current)) { |
| 1356 |
Log.e(LOG_TAG, "Duplicate node."); |
| 1357 |
return; |
| 1358 |
} |
| 1359 |
final int childCount = current.getChildCount(); |
| 1360 |
for (int i = 0; i < childCount; i++) { |
| 1361 |
final long childId = current.getChildId(i); |
| 1362 |
for (int j = 0; j < infoCount; j++) { |
| 1363 |
AccessibilityNodeInfo child = infos.get(j); |
| 1364 |
if (child.getSourceNodeId() == childId) { |
| 1365 |
fringe.add(child); |
| 1366 |
} |
| 1367 |
} |
| 1368 |
} |
| 1369 |
} |
| 1370 |
final int disconnectedCount = infos.size() - seen.size(); |
| 1371 |
if (disconnectedCount > 0) { |
| 1372 |
Log.e(LOG_TAG, disconnectedCount + " Disconnected nodes."); |
| 1373 |
} |
| 1374 |
} |
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
private void updateScrollingWindow(int windowId, long uptimeMillis) { |
| 1383 |
synchronized (sScrollingWindows) { |
| 1384 |
sScrollingWindows.put(windowId, uptimeMillis); |
| 1385 |
} |
| 1386 |
} |
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
private void deleteScrollingWindow(int windowId) { |
| 1394 |
synchronized (sScrollingWindows) { |
| 1395 |
sScrollingWindows.delete(windowId); |
| 1396 |
} |
| 1397 |
} |
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
private boolean isWindowScrolling(int windowId) { |
| 1406 |
synchronized (sScrollingWindows) { |
| 1407 |
final long latestScrollingTime = sScrollingWindows.get(windowId); |
| 1408 |
if (latestScrollingTime == 0) { |
| 1409 |
return false; |
| 1410 |
} |
| 1411 |
final long currentUptime = SystemClock.uptimeMillis(); |
| 1412 |
if (currentUptime > (latestScrollingTime + DISABLE_PREFETCHING_FOR_SCROLLING_MILLIS)) { |
| 1413 |
sScrollingWindows.delete(windowId); |
| 1414 |
return false; |
| 1415 |
} |
| 1416 |
} |
| 1417 |
return true; |
| 1418 |
} |
| 1419 |
|
| 1420 |
private boolean shouldTraceClient() { |
| 1421 |
return (mAccessibilityManager != null) |
| 1422 |
&& mAccessibilityManager.isA11yInteractionClientTraceEnabled(); |
| 1423 |
} |
| 1424 |
|
| 1425 |
private boolean shouldTraceCallback() { |
| 1426 |
return (mAccessibilityManager != null) |
| 1427 |
&& mAccessibilityManager.isA11yInteractionConnectionCBTraceEnabled(); |
| 1428 |
} |
| 1429 |
|
| 1430 |
private void logTrace( |
| 1431 |
IAccessibilityServiceConnection connection, String method, String params, |
| 1432 |
int callingUid, List<StackTraceElement> callStack, HashSet<String> ignoreSet, |
| 1433 |
long logTypes) { |
| 1434 |
try { |
| 1435 |
Bundle b = new Bundle(); |
| 1436 |
b.putSerializable(CALL_STACK, new ArrayList<StackTraceElement>(callStack)); |
| 1437 |
if (ignoreSet != null) { |
| 1438 |
b.putSerializable(IGNORE_CALL_STACK, ignoreSet); |
| 1439 |
} |
| 1440 |
connection.logTrace(SystemClock.elapsedRealtimeNanos(), |
| 1441 |
LOG_TAG + "." + method, |
| 1442 |
logTypes, params, Process.myPid(), Thread.currentThread().getId(), |
| 1443 |
callingUid, b); |
| 1444 |
} catch (RemoteException e) { |
| 1445 |
Log.e(LOG_TAG, "Failed to log trace. " + e); |
| 1446 |
} |
| 1447 |
} |
| 1448 |
|
| 1449 |
private void logTraceCallback( |
| 1450 |
IAccessibilityServiceConnection connection, String method, String params) { |
| 1451 |
logTrace(connection, method + " callback", params, mCallingUid, mCallStackOfCallback, |
| 1452 |
new HashSet<String>(Arrays.asList("getStackTrace")), |
| 1453 |
FLAGS_ACCESSIBILITY_INTERACTION_CONNECTION_CALLBACK); |
| 1454 |
} |
| 1455 |
|
| 1456 |
private void logTraceClient( |
| 1457 |
IAccessibilityServiceConnection connection, String method, String params) { |
| 1458 |
logTrace(connection, method, params, Binder.getCallingUid(), |
| 1459 |
Arrays.asList(Thread.currentThread().getStackTrace()), |
| 1460 |
new HashSet<String>(Arrays.asList("getStackTrace", "logTraceClient")), |
| 1461 |
FLAGS_ACCESSIBILITY_INTERACTION_CLIENT); |
| 1462 |
} |
| 1463 |
} |