| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app; |
| 18 |
|
| 19 |
import android.accessibilityservice.AccessibilityGestureEvent; |
| 20 |
import android.accessibilityservice.AccessibilityService.Callbacks; |
| 21 |
import android.accessibilityservice.AccessibilityService.IAccessibilityServiceClientWrapper; |
| 22 |
import android.accessibilityservice.AccessibilityServiceInfo; |
| 23 |
import android.accessibilityservice.IAccessibilityServiceClient; |
| 24 |
import android.accessibilityservice.IAccessibilityServiceConnection; |
| 25 |
import android.accessibilityservice.MagnificationConfig; |
| 26 |
import android.annotation.IntDef; |
| 27 |
import android.annotation.NonNull; |
| 28 |
import android.annotation.Nullable; |
| 29 |
import android.annotation.SuppressLint; |
| 30 |
import android.annotation.TestApi; |
| 31 |
import android.compat.annotation.UnsupportedAppUsage; |
| 32 |
import android.graphics.Bitmap; |
| 33 |
import android.graphics.Point; |
| 34 |
import android.graphics.Rect; |
| 35 |
import android.graphics.Region; |
| 36 |
import android.hardware.display.DisplayManagerGlobal; |
| 37 |
import android.os.Build; |
| 38 |
import android.os.Handler; |
| 39 |
import android.os.HandlerThread; |
| 40 |
import android.os.IBinder; |
| 41 |
import android.os.Looper; |
| 42 |
import android.os.ParcelFileDescriptor; |
| 43 |
import android.os.Process; |
| 44 |
import android.os.RemoteException; |
| 45 |
import android.os.SystemClock; |
| 46 |
import android.os.UserHandle; |
| 47 |
import android.util.ArraySet; |
| 48 |
import android.util.Log; |
| 49 |
import android.util.SparseArray; |
| 50 |
import android.view.Display; |
| 51 |
import android.view.InputEvent; |
| 52 |
import android.view.KeyEvent; |
| 53 |
import android.view.MotionEvent; |
| 54 |
import android.view.Surface; |
| 55 |
import android.view.SurfaceControl; |
| 56 |
import android.view.View; |
| 57 |
import android.view.ViewRootImpl; |
| 58 |
import android.view.Window; |
| 59 |
import android.view.WindowAnimationFrameStats; |
| 60 |
import android.view.WindowContentFrameStats; |
| 61 |
import android.view.accessibility.AccessibilityEvent; |
| 62 |
import android.view.accessibility.AccessibilityInteractionClient; |
| 63 |
import android.view.accessibility.AccessibilityNodeInfo; |
| 64 |
import android.view.accessibility.AccessibilityWindowInfo; |
| 65 |
import android.view.accessibility.IAccessibilityInteractionConnection; |
| 66 |
import android.view.inputmethod.EditorInfo; |
| 67 |
|
| 68 |
import com.android.internal.annotations.GuardedBy; |
| 69 |
import com.android.internal.inputmethod.IAccessibilityInputMethodSessionCallback; |
| 70 |
import com.android.internal.inputmethod.RemoteAccessibilityInputConnection; |
| 71 |
import com.android.internal.util.function.pooled.PooledLambda; |
| 72 |
|
| 73 |
import libcore.io.IoUtils; |
| 74 |
|
| 75 |
import java.io.IOException; |
| 76 |
import java.lang.annotation.Retention; |
| 77 |
import java.lang.annotation.RetentionPolicy; |
| 78 |
import java.util.ArrayList; |
| 79 |
import java.util.Collections; |
| 80 |
import java.util.List; |
| 81 |
import java.util.Set; |
| 82 |
import java.util.concurrent.TimeoutException; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public final class UiAutomation { |
| 109 |
|
| 110 |
private static final String LOG_TAG = UiAutomation.class.getSimpleName(); |
| 111 |
|
| 112 |
private static final boolean DEBUG = false; |
| 113 |
|
| 114 |
private static final int CONNECTION_ID_UNDEFINED = -1; |
| 115 |
|
| 116 |
private static final long CONNECT_TIMEOUT_MILLIS = 5000; |
| 117 |
|
| 118 |
|
| 119 |
public static final int ROTATION_UNFREEZE = -2; |
| 120 |
|
| 121 |
|
| 122 |
public static final int ROTATION_FREEZE_CURRENT = -1; |
| 123 |
|
| 124 |
|
| 125 |
public static final int ROTATION_FREEZE_0 = Surface.ROTATION_0; |
| 126 |
|
| 127 |
|
| 128 |
public static final int ROTATION_FREEZE_90 = Surface.ROTATION_90; |
| 129 |
|
| 130 |
|
| 131 |
public static final int ROTATION_FREEZE_180 = Surface.ROTATION_180; |
| 132 |
|
| 133 |
|
| 134 |
public static final int ROTATION_FREEZE_270 = Surface.ROTATION_270; |
| 135 |
|
| 136 |
@Retention(RetentionPolicy.SOURCE) |
| 137 |
@IntDef(value = { |
| 138 |
ConnectionState.DISCONNECTED, |
| 139 |
ConnectionState.CONNECTING, |
| 140 |
ConnectionState.CONNECTED, |
| 141 |
ConnectionState.FAILED |
| 142 |
}) |
| 143 |
private @interface ConnectionState { |
| 144 |
|
| 145 |
int DISCONNECTED = 0; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
int CONNECTING = 1; |
| 152 |
|
| 153 |
int CONNECTED = 2; |
| 154 |
|
| 155 |
int FAILED = 3; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public static final int FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES = 0x00000001; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public static final int FLAG_DONT_USE_ACCESSIBILITY = 0x00000002; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
@TestApi |
| 182 |
@NonNull |
| 183 |
public static final Set<String> ALL_PERMISSIONS = Set.of("_ALL_PERMISSIONS_"); |
| 184 |
|
| 185 |
private final Object mLock = new Object(); |
| 186 |
|
| 187 |
private final ArrayList<AccessibilityEvent> mEventQueue = new ArrayList<AccessibilityEvent>(); |
| 188 |
|
| 189 |
private final Handler mLocalCallbackHandler; |
| 190 |
|
| 191 |
private final IUiAutomationConnection mUiAutomationConnection; |
| 192 |
|
| 193 |
private HandlerThread mRemoteCallbackThread; |
| 194 |
|
| 195 |
private IAccessibilityServiceClient mClient; |
| 196 |
|
| 197 |
private int mConnectionId = CONNECTION_ID_UNDEFINED; |
| 198 |
|
| 199 |
private OnAccessibilityEventListener mOnAccessibilityEventListener; |
| 200 |
|
| 201 |
private boolean mWaitingForEventDelivery; |
| 202 |
|
| 203 |
private long mLastEventTimeMillis; |
| 204 |
|
| 205 |
private @ConnectionState int mConnectionState = ConnectionState.DISCONNECTED; |
| 206 |
|
| 207 |
private boolean mIsDestroyed; |
| 208 |
|
| 209 |
private int mFlags; |
| 210 |
|
| 211 |
private int mGenerationId = 0; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public static interface OnAccessibilityEventListener { |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public void onAccessibilityEvent(AccessibilityEvent event); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public static interface AccessibilityEventFilter { |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public boolean accept(AccessibilityEvent event); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 261 |
public UiAutomation(Looper looper, IUiAutomationConnection connection) { |
| 262 |
if (looper == null) { |
| 263 |
throw new IllegalArgumentException("Looper cannot be null!"); |
| 264 |
} |
| 265 |
if (connection == null) { |
| 266 |
throw new IllegalArgumentException("Connection cannot be null!"); |
| 267 |
} |
| 268 |
mLocalCallbackHandler = new Handler(looper); |
| 269 |
mUiAutomationConnection = connection; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 279 |
public void connect() { |
| 280 |
try { |
| 281 |
connectWithTimeout(0, CONNECT_TIMEOUT_MILLIS); |
| 282 |
} catch (TimeoutException e) { |
| 283 |
throw new RuntimeException(e); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
public void connect(int flags) { |
| 293 |
try { |
| 294 |
connectWithTimeout(flags, CONNECT_TIMEOUT_MILLIS); |
| 295 |
} catch (TimeoutException e) { |
| 296 |
throw new RuntimeException(e); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
public void connectWithTimeout(int flags, long timeoutMillis) throws TimeoutException { |
| 314 |
synchronized (mLock) { |
| 315 |
throwIfConnectedLocked(); |
| 316 |
if (mConnectionState == ConnectionState.CONNECTING) { |
| 317 |
return; |
| 318 |
} |
| 319 |
mConnectionState = ConnectionState.CONNECTING; |
| 320 |
mRemoteCallbackThread = new HandlerThread("UiAutomation"); |
| 321 |
mRemoteCallbackThread.start(); |
| 322 |
|
| 323 |
mClient = new IAccessibilityServiceClientImpl( |
| 324 |
mRemoteCallbackThread.getLooper(), ++mGenerationId); |
| 325 |
} |
| 326 |
|
| 327 |
try { |
| 328 |
|
| 329 |
mUiAutomationConnection.connect(mClient, flags); |
| 330 |
mFlags = flags; |
| 331 |
|
| 332 |
|
| 333 |
if (!useAccessibility()) { |
| 334 |
mConnectionState = ConnectionState.DISCONNECTED; |
| 335 |
return; |
| 336 |
} |
| 337 |
} catch (RemoteException re) { |
| 338 |
throw new RuntimeException("Error while connecting " + this, re); |
| 339 |
} |
| 340 |
|
| 341 |
synchronized (mLock) { |
| 342 |
final long startTimeMillis = SystemClock.uptimeMillis(); |
| 343 |
while (true) { |
| 344 |
if (mConnectionState == ConnectionState.CONNECTED) { |
| 345 |
break; |
| 346 |
} |
| 347 |
final long elapsedTimeMillis = SystemClock.uptimeMillis() - startTimeMillis; |
| 348 |
final long remainingTimeMillis = timeoutMillis - elapsedTimeMillis; |
| 349 |
if (remainingTimeMillis <= 0) { |
| 350 |
mConnectionState = ConnectionState.FAILED; |
| 351 |
throw new TimeoutException("Timeout while connecting " + this); |
| 352 |
} |
| 353 |
try { |
| 354 |
mLock.wait(remainingTimeMillis); |
| 355 |
} catch (InterruptedException ie) { |
| 356 |
|
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
public int getFlags() { |
| 370 |
return mFlags; |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 379 |
public void disconnect() { |
| 380 |
synchronized (mLock) { |
| 381 |
if (mConnectionState == ConnectionState.CONNECTING) { |
| 382 |
throw new IllegalStateException( |
| 383 |
"Cannot call disconnect() while connecting " + this); |
| 384 |
} |
| 385 |
if (useAccessibility() && mConnectionState == ConnectionState.DISCONNECTED) { |
| 386 |
return; |
| 387 |
} |
| 388 |
mConnectionState = ConnectionState.DISCONNECTED; |
| 389 |
mConnectionId = CONNECTION_ID_UNDEFINED; |
| 390 |
|
| 391 |
++mGenerationId; |
| 392 |
} |
| 393 |
try { |
| 394 |
|
| 395 |
mUiAutomationConnection.disconnect(); |
| 396 |
} catch (RemoteException re) { |
| 397 |
throw new RuntimeException("Error while disconnecting " + this, re); |
| 398 |
} finally { |
| 399 |
if (mRemoteCallbackThread != null) { |
| 400 |
mRemoteCallbackThread.quit(); |
| 401 |
mRemoteCallbackThread = null; |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
public int getConnectionId() { |
| 414 |
synchronized (mLock) { |
| 415 |
throwIfNotConnectedLocked(); |
| 416 |
return mConnectionId; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
public boolean isDestroyed() { |
| 428 |
return mIsDestroyed; |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
public void setOnAccessibilityEventListener(OnAccessibilityEventListener listener) { |
| 441 |
synchronized (mLock) { |
| 442 |
throwIfNotConnectedLocked(); |
| 443 |
mOnAccessibilityEventListener = listener; |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
@TestApi |
| 454 |
public void destroy() { |
| 455 |
disconnect(); |
| 456 |
mIsDestroyed = true; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
public void adoptShellPermissionIdentity() { |
| 474 |
try { |
| 475 |
|
| 476 |
mUiAutomationConnection.adoptShellPermissionIdentity(Process.myUid(), null); |
| 477 |
} catch (RemoteException re) { |
| 478 |
Log.e(LOG_TAG, "Error executing adopting shell permission identity!", re); |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
public void adoptShellPermissionIdentity(@Nullable String... permissions) { |
| 499 |
try { |
| 500 |
|
| 501 |
mUiAutomationConnection.adoptShellPermissionIdentity(Process.myUid(), permissions); |
| 502 |
} catch (RemoteException re) { |
| 503 |
Log.e(LOG_TAG, "Error executing adopting shell permission identity!", re); |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
public void dropShellPermissionIdentity() { |
| 515 |
try { |
| 516 |
|
| 517 |
mUiAutomationConnection.dropShellPermissionIdentity(); |
| 518 |
} catch (RemoteException re) { |
| 519 |
Log.e(LOG_TAG, "Error executing dropping shell permission identity!", re); |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
@TestApi |
| 531 |
@NonNull |
| 532 |
public Set<String> getAdoptedShellPermissions() { |
| 533 |
try { |
| 534 |
final List<String> permissions = mUiAutomationConnection.getAdoptedShellPermissions(); |
| 535 |
return permissions == null ? ALL_PERMISSIONS : new ArraySet<>(permissions); |
| 536 |
} catch (RemoteException re) { |
| 537 |
Log.e(LOG_TAG, "Error getting adopted shell permissions", re); |
| 538 |
return Collections.emptySet(); |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
public final boolean performGlobalAction(int action) { |
| 558 |
final IAccessibilityServiceConnection connection; |
| 559 |
synchronized (mLock) { |
| 560 |
throwIfNotConnectedLocked(); |
| 561 |
connection = AccessibilityInteractionClient.getInstance() |
| 562 |
.getConnection(mConnectionId); |
| 563 |
} |
| 564 |
|
| 565 |
if (connection != null) { |
| 566 |
try { |
| 567 |
return connection.performGlobalAction(action); |
| 568 |
} catch (RemoteException re) { |
| 569 |
Log.w(LOG_TAG, "Error while calling performGlobalAction", re); |
| 570 |
} |
| 571 |
} |
| 572 |
return false; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
public AccessibilityNodeInfo findFocus(int focus) { |
| 595 |
synchronized (mLock) { |
| 596 |
throwIfNotConnectedLocked(); |
| 597 |
} |
| 598 |
return AccessibilityInteractionClient.getInstance().findFocus(mConnectionId, |
| 599 |
AccessibilityWindowInfo.ANY_WINDOW_ID, AccessibilityNodeInfo.ROOT_NODE_ID, focus); |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
public final AccessibilityServiceInfo getServiceInfo() { |
| 614 |
final IAccessibilityServiceConnection connection; |
| 615 |
synchronized (mLock) { |
| 616 |
throwIfNotConnectedLocked(); |
| 617 |
connection = AccessibilityInteractionClient.getInstance() |
| 618 |
.getConnection(mConnectionId); |
| 619 |
} |
| 620 |
|
| 621 |
if (connection != null) { |
| 622 |
try { |
| 623 |
return connection.getServiceInfo(); |
| 624 |
} catch (RemoteException re) { |
| 625 |
Log.w(LOG_TAG, "Error while getting AccessibilityServiceInfo", re); |
| 626 |
} |
| 627 |
} |
| 628 |
return null; |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
public final void setServiceInfo(AccessibilityServiceInfo info) { |
| 642 |
final IAccessibilityServiceConnection connection; |
| 643 |
synchronized (mLock) { |
| 644 |
throwIfNotConnectedLocked(); |
| 645 |
AccessibilityInteractionClient.getInstance().clearCache(mConnectionId); |
| 646 |
connection = AccessibilityInteractionClient.getInstance() |
| 647 |
.getConnection(mConnectionId); |
| 648 |
} |
| 649 |
|
| 650 |
if (connection != null) { |
| 651 |
try { |
| 652 |
connection.setServiceInfo(info); |
| 653 |
} catch (RemoteException re) { |
| 654 |
Log.w(LOG_TAG, "Error while setting AccessibilityServiceInfo", re); |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
public List<AccessibilityWindowInfo> getWindows() { |
| 678 |
final int connectionId; |
| 679 |
synchronized (mLock) { |
| 680 |
throwIfNotConnectedLocked(); |
| 681 |
connectionId = mConnectionId; |
| 682 |
} |
| 683 |
|
| 684 |
return AccessibilityInteractionClient.getInstance() |
| 685 |
.getWindows(connectionId); |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
@NonNull |
| 708 |
public SparseArray<List<AccessibilityWindowInfo>> getWindowsOnAllDisplays() { |
| 709 |
final int connectionId; |
| 710 |
synchronized (mLock) { |
| 711 |
throwIfNotConnectedLocked(); |
| 712 |
connectionId = mConnectionId; |
| 713 |
} |
| 714 |
|
| 715 |
return AccessibilityInteractionClient.getInstance() |
| 716 |
.getWindowsOnAllDisplays(connectionId); |
| 717 |
} |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
public AccessibilityNodeInfo getRootInActiveWindow() { |
| 727 |
final int connectionId; |
| 728 |
synchronized (mLock) { |
| 729 |
throwIfNotConnectedLocked(); |
| 730 |
connectionId = mConnectionId; |
| 731 |
} |
| 732 |
|
| 733 |
return AccessibilityInteractionClient.getInstance() |
| 734 |
.getRootInActiveWindow(connectionId, |
| 735 |
AccessibilityNodeInfo.FLAG_PREFETCH_DESCENDANTS_HYBRID); |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
public boolean injectInputEvent(InputEvent event, boolean sync) { |
| 752 |
return injectInputEvent(event, sync, true ); |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
@TestApi |
| 771 |
public boolean injectInputEvent(@NonNull InputEvent event, boolean sync, |
| 772 |
boolean waitForAnimations) { |
| 773 |
try { |
| 774 |
if (DEBUG) { |
| 775 |
Log.i(LOG_TAG, "Injecting: " + event + " sync: " + sync + " waitForAnimations: " |
| 776 |
+ waitForAnimations); |
| 777 |
} |
| 778 |
|
| 779 |
return mUiAutomationConnection.injectInputEvent(event, sync, waitForAnimations); |
| 780 |
} catch (RemoteException re) { |
| 781 |
Log.e(LOG_TAG, "Error while injecting input event!", re); |
| 782 |
} |
| 783 |
return false; |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
public void setAnimationScale(float scale) { |
| 802 |
final IAccessibilityServiceConnection connection = |
| 803 |
AccessibilityInteractionClient.getInstance().getConnection(mConnectionId); |
| 804 |
if (connection != null) { |
| 805 |
try { |
| 806 |
connection.setAnimationScale(scale); |
| 807 |
} catch (RemoteException re) { |
| 808 |
throw new RuntimeException(re); |
| 809 |
} |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
@TestApi |
| 820 |
public void syncInputTransactions() { |
| 821 |
try { |
| 822 |
|
| 823 |
mUiAutomationConnection.syncInputTransactions(true ); |
| 824 |
} catch (RemoteException re) { |
| 825 |
Log.e(LOG_TAG, "Error while syncing input transactions!", re); |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
@TestApi |
| 839 |
public void syncInputTransactions(boolean waitForAnimations) { |
| 840 |
try { |
| 841 |
|
| 842 |
mUiAutomationConnection.syncInputTransactions(waitForAnimations); |
| 843 |
} catch (RemoteException re) { |
| 844 |
Log.e(LOG_TAG, "Error while syncing input transactions!", re); |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
public boolean setRotation(int rotation) { |
| 865 |
switch (rotation) { |
| 866 |
case ROTATION_FREEZE_0: |
| 867 |
case ROTATION_FREEZE_90: |
| 868 |
case ROTATION_FREEZE_180: |
| 869 |
case ROTATION_FREEZE_270: |
| 870 |
case ROTATION_UNFREEZE: |
| 871 |
case ROTATION_FREEZE_CURRENT: { |
| 872 |
try { |
| 873 |
|
| 874 |
mUiAutomationConnection.setRotation(rotation); |
| 875 |
return true; |
| 876 |
} catch (RemoteException re) { |
| 877 |
Log.e(LOG_TAG, "Error while setting rotation!", re); |
| 878 |
} |
| 879 |
} return false; |
| 880 |
default: { |
| 881 |
throw new IllegalArgumentException("Invalid rotation."); |
| 882 |
} |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
public AccessibilityEvent executeAndWaitForEvent(Runnable command, |
| 904 |
AccessibilityEventFilter filter, long timeoutMillis) throws TimeoutException { |
| 905 |
|
| 906 |
synchronized (mLock) { |
| 907 |
throwIfNotConnectedLocked(); |
| 908 |
mEventQueue.clear(); |
| 909 |
|
| 910 |
mWaitingForEventDelivery = true; |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
final long executionStartTimeMillis = SystemClock.uptimeMillis(); |
| 919 |
|
| 920 |
command.run(); |
| 921 |
|
| 922 |
List<AccessibilityEvent> receivedEvents = new ArrayList<>(); |
| 923 |
|
| 924 |
|
| 925 |
try { |
| 926 |
|
| 927 |
final long startTimeMillis = SystemClock.uptimeMillis(); |
| 928 |
while (true) { |
| 929 |
List<AccessibilityEvent> localEvents = new ArrayList<>(); |
| 930 |
synchronized (mLock) { |
| 931 |
localEvents.addAll(mEventQueue); |
| 932 |
mEventQueue.clear(); |
| 933 |
} |
| 934 |
|
| 935 |
while (!localEvents.isEmpty()) { |
| 936 |
AccessibilityEvent event = localEvents.remove(0); |
| 937 |
|
| 938 |
if (event.getEventTime() < executionStartTimeMillis) { |
| 939 |
continue; |
| 940 |
} |
| 941 |
if (filter.accept(event)) { |
| 942 |
return event; |
| 943 |
} |
| 944 |
receivedEvents.add(event); |
| 945 |
} |
| 946 |
|
| 947 |
final long elapsedTimeMillis = SystemClock.uptimeMillis() - startTimeMillis; |
| 948 |
final long remainingTimeMillis = timeoutMillis - elapsedTimeMillis; |
| 949 |
if (remainingTimeMillis <= 0) { |
| 950 |
throw new TimeoutException("Expected event not received within: " |
| 951 |
+ timeoutMillis + " ms among: " + receivedEvents); |
| 952 |
} |
| 953 |
synchronized (mLock) { |
| 954 |
if (mEventQueue.isEmpty()) { |
| 955 |
try { |
| 956 |
mLock.wait(remainingTimeMillis); |
| 957 |
} catch (InterruptedException ie) { |
| 958 |
|
| 959 |
} |
| 960 |
} |
| 961 |
} |
| 962 |
} |
| 963 |
} finally { |
| 964 |
int size = receivedEvents.size(); |
| 965 |
for (int i = 0; i < size; i++) { |
| 966 |
receivedEvents.get(i).recycle(); |
| 967 |
} |
| 968 |
|
| 969 |
synchronized (mLock) { |
| 970 |
mWaitingForEventDelivery = false; |
| 971 |
mEventQueue.clear(); |
| 972 |
mLock.notifyAll(); |
| 973 |
} |
| 974 |
} |
| 975 |
} |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
public void waitForIdle(long idleTimeoutMillis, long globalTimeoutMillis) |
| 994 |
throws TimeoutException { |
| 995 |
synchronized (mLock) { |
| 996 |
throwIfNotConnectedLocked(); |
| 997 |
|
| 998 |
final long startTimeMillis = SystemClock.uptimeMillis(); |
| 999 |
if (mLastEventTimeMillis <= 0) { |
| 1000 |
mLastEventTimeMillis = startTimeMillis; |
| 1001 |
} |
| 1002 |
|
| 1003 |
while (true) { |
| 1004 |
final long currentTimeMillis = SystemClock.uptimeMillis(); |
| 1005 |
|
| 1006 |
final long elapsedGlobalTimeMillis = currentTimeMillis - startTimeMillis; |
| 1007 |
final long remainingGlobalTimeMillis = |
| 1008 |
globalTimeoutMillis - elapsedGlobalTimeMillis; |
| 1009 |
if (remainingGlobalTimeMillis <= 0) { |
| 1010 |
throw new TimeoutException("No idle state with idle timeout: " |
| 1011 |
+ idleTimeoutMillis + " within global timeout: " |
| 1012 |
+ globalTimeoutMillis); |
| 1013 |
} |
| 1014 |
|
| 1015 |
final long elapsedIdleTimeMillis = currentTimeMillis - mLastEventTimeMillis; |
| 1016 |
final long remainingIdleTimeMillis = idleTimeoutMillis - elapsedIdleTimeMillis; |
| 1017 |
if (remainingIdleTimeMillis <= 0) { |
| 1018 |
return; |
| 1019 |
} |
| 1020 |
try { |
| 1021 |
mLock.wait(remainingIdleTimeMillis); |
| 1022 |
} catch (InterruptedException ie) { |
| 1023 |
|
| 1024 |
} |
| 1025 |
} |
| 1026 |
} |
| 1027 |
} |
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
public Bitmap takeScreenshot() { |
| 1035 |
Display display = DisplayManagerGlobal.getInstance() |
| 1036 |
.getRealDisplay(Display.DEFAULT_DISPLAY); |
| 1037 |
Point displaySize = new Point(); |
| 1038 |
display.getRealSize(displaySize); |
| 1039 |
|
| 1040 |
int rotation = display.getRotation(); |
| 1041 |
|
| 1042 |
|
| 1043 |
Bitmap screenShot = null; |
| 1044 |
try { |
| 1045 |
|
| 1046 |
screenShot = mUiAutomationConnection.takeScreenshot( |
| 1047 |
new Rect(0, 0, displaySize.x, displaySize.y)); |
| 1048 |
if (screenShot == null) { |
| 1049 |
return null; |
| 1050 |
} |
| 1051 |
} catch (RemoteException re) { |
| 1052 |
Log.e(LOG_TAG, "Error while taking screenshot!", re); |
| 1053 |
return null; |
| 1054 |
} |
| 1055 |
|
| 1056 |
|
| 1057 |
screenShot.setHasAlpha(false); |
| 1058 |
|
| 1059 |
return screenShot; |
| 1060 |
} |
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
@TestApi |
| 1075 |
@Nullable |
| 1076 |
public Bitmap takeScreenshot(@NonNull Window window) { |
| 1077 |
if (window == null) { |
| 1078 |
return null; |
| 1079 |
} |
| 1080 |
|
| 1081 |
View decorView = window.peekDecorView(); |
| 1082 |
if (decorView == null) { |
| 1083 |
return null; |
| 1084 |
} |
| 1085 |
|
| 1086 |
ViewRootImpl viewRoot = decorView.getViewRootImpl(); |
| 1087 |
if (viewRoot == null) { |
| 1088 |
return null; |
| 1089 |
} |
| 1090 |
|
| 1091 |
SurfaceControl sc = viewRoot.getSurfaceControl(); |
| 1092 |
if (!sc.isValid()) { |
| 1093 |
return null; |
| 1094 |
} |
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
new SurfaceControl.Transaction().apply(true); |
| 1099 |
try { |
| 1100 |
return mUiAutomationConnection.takeSurfaceControlScreenshot(sc); |
| 1101 |
} catch (RemoteException re) { |
| 1102 |
Log.e(LOG_TAG, "Error while taking screenshot!", re); |
| 1103 |
return null; |
| 1104 |
} |
| 1105 |
} |
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
public void setRunAsMonkey(boolean enable) { |
| 1116 |
try { |
| 1117 |
ActivityManager.getService().setUserIsMonkey(enable); |
| 1118 |
} catch (RemoteException re) { |
| 1119 |
Log.e(LOG_TAG, "Error while setting run as monkey!", re); |
| 1120 |
} |
| 1121 |
} |
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
public boolean clearWindowContentFrameStats(int windowId) { |
| 1140 |
synchronized (mLock) { |
| 1141 |
throwIfNotConnectedLocked(); |
| 1142 |
} |
| 1143 |
try { |
| 1144 |
if (DEBUG) { |
| 1145 |
Log.i(LOG_TAG, "Clearing content frame stats for window: " + windowId); |
| 1146 |
} |
| 1147 |
|
| 1148 |
return mUiAutomationConnection.clearWindowContentFrameStats(windowId); |
| 1149 |
} catch (RemoteException re) { |
| 1150 |
Log.e(LOG_TAG, "Error clearing window content frame stats!", re); |
| 1151 |
} |
| 1152 |
return false; |
| 1153 |
} |
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
public WindowContentFrameStats getWindowContentFrameStats(int windowId) { |
| 1187 |
synchronized (mLock) { |
| 1188 |
throwIfNotConnectedLocked(); |
| 1189 |
} |
| 1190 |
try { |
| 1191 |
if (DEBUG) { |
| 1192 |
Log.i(LOG_TAG, "Getting content frame stats for window: " + windowId); |
| 1193 |
} |
| 1194 |
|
| 1195 |
return mUiAutomationConnection.getWindowContentFrameStats(windowId); |
| 1196 |
} catch (RemoteException re) { |
| 1197 |
Log.e(LOG_TAG, "Error getting window content frame stats!", re); |
| 1198 |
} |
| 1199 |
return null; |
| 1200 |
} |
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
@Deprecated |
| 1215 |
public void clearWindowAnimationFrameStats() { |
| 1216 |
try { |
| 1217 |
if (DEBUG) { |
| 1218 |
Log.i(LOG_TAG, "Clearing window animation frame stats"); |
| 1219 |
} |
| 1220 |
|
| 1221 |
mUiAutomationConnection.clearWindowAnimationFrameStats(); |
| 1222 |
} catch (RemoteException re) { |
| 1223 |
Log.e(LOG_TAG, "Error clearing window animation frame stats!", re); |
| 1224 |
} |
| 1225 |
} |
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
@Deprecated |
| 1256 |
public WindowAnimationFrameStats getWindowAnimationFrameStats() { |
| 1257 |
try { |
| 1258 |
if (DEBUG) { |
| 1259 |
Log.i(LOG_TAG, "Getting window animation frame stats"); |
| 1260 |
} |
| 1261 |
|
| 1262 |
return mUiAutomationConnection.getWindowAnimationFrameStats(); |
| 1263 |
} catch (RemoteException re) { |
| 1264 |
Log.e(LOG_TAG, "Error getting window animation frame stats!", re); |
| 1265 |
} |
| 1266 |
return null; |
| 1267 |
} |
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
public void grantRuntimePermission(String packageName, String permission) { |
| 1277 |
grantRuntimePermissionAsUser(packageName, permission, android.os.Process.myUserHandle()); |
| 1278 |
} |
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
@Deprecated |
| 1286 |
@TestApi |
| 1287 |
public boolean grantRuntimePermission(String packageName, String permission, |
| 1288 |
UserHandle userHandle) { |
| 1289 |
grantRuntimePermissionAsUser(packageName, permission, userHandle); |
| 1290 |
return true; |
| 1291 |
} |
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
public void grantRuntimePermissionAsUser(String packageName, String permission, |
| 1301 |
UserHandle userHandle) { |
| 1302 |
try { |
| 1303 |
if (DEBUG) { |
| 1304 |
Log.i(LOG_TAG, "Granting runtime permission"); |
| 1305 |
} |
| 1306 |
|
| 1307 |
mUiAutomationConnection.grantRuntimePermission(packageName, |
| 1308 |
permission, userHandle.getIdentifier()); |
| 1309 |
} catch (Exception e) { |
| 1310 |
throw new SecurityException("Error granting runtime permission", e); |
| 1311 |
} |
| 1312 |
} |
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
public void revokeRuntimePermission(String packageName, String permission) { |
| 1322 |
revokeRuntimePermissionAsUser(packageName, permission, android.os.Process.myUserHandle()); |
| 1323 |
} |
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
@Deprecated |
| 1331 |
@TestApi |
| 1332 |
public boolean revokeRuntimePermission(String packageName, String permission, |
| 1333 |
UserHandle userHandle) { |
| 1334 |
revokeRuntimePermissionAsUser(packageName, permission, userHandle); |
| 1335 |
return true; |
| 1336 |
} |
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
public void revokeRuntimePermissionAsUser(String packageName, String permission, |
| 1346 |
UserHandle userHandle) { |
| 1347 |
try { |
| 1348 |
if (DEBUG) { |
| 1349 |
Log.i(LOG_TAG, "Revoking runtime permission"); |
| 1350 |
} |
| 1351 |
|
| 1352 |
mUiAutomationConnection.revokeRuntimePermission(packageName, |
| 1353 |
permission, userHandle.getIdentifier()); |
| 1354 |
} catch (Exception e) { |
| 1355 |
throw new SecurityException("Error granting runtime permission", e); |
| 1356 |
} |
| 1357 |
} |
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
public ParcelFileDescriptor executeShellCommand(String command) { |
| 1374 |
warnIfBetterCommand(command); |
| 1375 |
|
| 1376 |
ParcelFileDescriptor source = null; |
| 1377 |
ParcelFileDescriptor sink = null; |
| 1378 |
|
| 1379 |
try { |
| 1380 |
ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe(); |
| 1381 |
source = pipe[0]; |
| 1382 |
sink = pipe[1]; |
| 1383 |
|
| 1384 |
|
| 1385 |
mUiAutomationConnection.executeShellCommand(command, sink, null); |
| 1386 |
} catch (IOException ioe) { |
| 1387 |
Log.e(LOG_TAG, "Error executing shell command!", ioe); |
| 1388 |
} catch (RemoteException re) { |
| 1389 |
Log.e(LOG_TAG, "Error executing shell command!", re); |
| 1390 |
} finally { |
| 1391 |
IoUtils.closeQuietly(sink); |
| 1392 |
} |
| 1393 |
|
| 1394 |
return source; |
| 1395 |
} |
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
@SuppressLint("ArrayReturn") |
| 1411 |
public @NonNull ParcelFileDescriptor[] executeShellCommandRw(@NonNull String command) { |
| 1412 |
return executeShellCommandInternal(command, false ); |
| 1413 |
} |
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
@TestApi |
| 1432 |
@SuppressLint("ArrayReturn") |
| 1433 |
public @NonNull ParcelFileDescriptor[] executeShellCommandRwe(@NonNull String command) { |
| 1434 |
return executeShellCommandInternal(command, true ); |
| 1435 |
} |
| 1436 |
|
| 1437 |
private ParcelFileDescriptor[] executeShellCommandInternal( |
| 1438 |
String command, boolean includeStderr) { |
| 1439 |
warnIfBetterCommand(command); |
| 1440 |
|
| 1441 |
ParcelFileDescriptor source_read = null; |
| 1442 |
ParcelFileDescriptor sink_read = null; |
| 1443 |
|
| 1444 |
ParcelFileDescriptor source_write = null; |
| 1445 |
ParcelFileDescriptor sink_write = null; |
| 1446 |
|
| 1447 |
ParcelFileDescriptor stderr_source_read = null; |
| 1448 |
ParcelFileDescriptor stderr_sink_read = null; |
| 1449 |
|
| 1450 |
try { |
| 1451 |
ParcelFileDescriptor[] pipe_read = ParcelFileDescriptor.createPipe(); |
| 1452 |
source_read = pipe_read[0]; |
| 1453 |
sink_read = pipe_read[1]; |
| 1454 |
|
| 1455 |
ParcelFileDescriptor[] pipe_write = ParcelFileDescriptor.createPipe(); |
| 1456 |
source_write = pipe_write[0]; |
| 1457 |
sink_write = pipe_write[1]; |
| 1458 |
|
| 1459 |
if (includeStderr) { |
| 1460 |
ParcelFileDescriptor[] stderr_read = ParcelFileDescriptor.createPipe(); |
| 1461 |
stderr_source_read = stderr_read[0]; |
| 1462 |
stderr_sink_read = stderr_read[1]; |
| 1463 |
} |
| 1464 |
|
| 1465 |
|
| 1466 |
mUiAutomationConnection.executeShellCommandWithStderr( |
| 1467 |
command, sink_read, source_write, stderr_sink_read); |
| 1468 |
} catch (IOException ioe) { |
| 1469 |
Log.e(LOG_TAG, "Error executing shell command!", ioe); |
| 1470 |
} catch (RemoteException re) { |
| 1471 |
Log.e(LOG_TAG, "Error executing shell command!", re); |
| 1472 |
} finally { |
| 1473 |
IoUtils.closeQuietly(sink_read); |
| 1474 |
IoUtils.closeQuietly(source_write); |
| 1475 |
IoUtils.closeQuietly(stderr_sink_read); |
| 1476 |
} |
| 1477 |
|
| 1478 |
ParcelFileDescriptor[] result = new ParcelFileDescriptor[includeStderr ? 3 : 2]; |
| 1479 |
result[0] = source_read; |
| 1480 |
result[1] = sink_write; |
| 1481 |
if (includeStderr) { |
| 1482 |
result[2] = stderr_source_read; |
| 1483 |
} |
| 1484 |
return result; |
| 1485 |
} |
| 1486 |
|
| 1487 |
@Override |
| 1488 |
public String toString() { |
| 1489 |
final StringBuilder stringBuilder = new StringBuilder(); |
| 1490 |
stringBuilder.append("UiAutomation@").append(Integer.toHexString(hashCode())); |
| 1491 |
stringBuilder.append("[id=").append(mConnectionId); |
| 1492 |
stringBuilder.append(", flags=").append(mFlags); |
| 1493 |
stringBuilder.append("]"); |
| 1494 |
return stringBuilder.toString(); |
| 1495 |
} |
| 1496 |
|
| 1497 |
@GuardedBy("mLock") |
| 1498 |
private void throwIfConnectedLocked() { |
| 1499 |
if (mConnectionState == ConnectionState.CONNECTED) { |
| 1500 |
throw new IllegalStateException("UiAutomation connected, " + this); |
| 1501 |
} |
| 1502 |
} |
| 1503 |
|
| 1504 |
@GuardedBy("mLock") |
| 1505 |
private void throwIfNotConnectedLocked() { |
| 1506 |
if (mConnectionState != ConnectionState.CONNECTED) { |
| 1507 |
final String msg = useAccessibility() |
| 1508 |
? "UiAutomation not connected, " |
| 1509 |
: "UiAutomation not connected: Accessibility-dependent method called with " |
| 1510 |
+ "FLAG_DONT_USE_ACCESSIBILITY set, "; |
| 1511 |
throw new IllegalStateException(msg + this); |
| 1512 |
} |
| 1513 |
} |
| 1514 |
|
| 1515 |
private void warnIfBetterCommand(String cmd) { |
| 1516 |
if (cmd.startsWith("pm grant ")) { |
| 1517 |
Log.w(LOG_TAG, "UiAutomation.grantRuntimePermission() " |
| 1518 |
+ "is more robust and should be used instead of 'pm grant'"); |
| 1519 |
} else if (cmd.startsWith("pm revoke ")) { |
| 1520 |
Log.w(LOG_TAG, "UiAutomation.revokeRuntimePermission() " |
| 1521 |
+ "is more robust and should be used instead of 'pm revoke'"); |
| 1522 |
} |
| 1523 |
} |
| 1524 |
|
| 1525 |
private boolean useAccessibility() { |
| 1526 |
return (mFlags & UiAutomation.FLAG_DONT_USE_ACCESSIBILITY) == 0; |
| 1527 |
} |
| 1528 |
|
| 1529 |
private class IAccessibilityServiceClientImpl extends IAccessibilityServiceClientWrapper { |
| 1530 |
|
| 1531 |
public IAccessibilityServiceClientImpl(Looper looper, int generationId) { |
| 1532 |
super(null, looper, new Callbacks() { |
| 1533 |
private final int mGenerationId = generationId; |
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
private boolean isGenerationChangedLocked() { |
| 1541 |
return mGenerationId != UiAutomation.this.mGenerationId; |
| 1542 |
} |
| 1543 |
|
| 1544 |
@Override |
| 1545 |
public void init(int connectionId, IBinder windowToken) { |
| 1546 |
synchronized (mLock) { |
| 1547 |
if (isGenerationChangedLocked()) { |
| 1548 |
return; |
| 1549 |
} |
| 1550 |
mConnectionState = ConnectionState.CONNECTED; |
| 1551 |
mConnectionId = connectionId; |
| 1552 |
mLock.notifyAll(); |
| 1553 |
} |
| 1554 |
if (Build.IS_DEBUGGABLE) { |
| 1555 |
Log.v(LOG_TAG, "Init " + UiAutomation.this); |
| 1556 |
} |
| 1557 |
} |
| 1558 |
|
| 1559 |
@Override |
| 1560 |
public void onServiceConnected() { |
| 1561 |
|
| 1562 |
} |
| 1563 |
|
| 1564 |
@Override |
| 1565 |
public void onInterrupt() { |
| 1566 |
|
| 1567 |
} |
| 1568 |
|
| 1569 |
@Override |
| 1570 |
public void onSystemActionsChanged() { |
| 1571 |
|
| 1572 |
} |
| 1573 |
|
| 1574 |
@Override |
| 1575 |
public void createImeSession(IAccessibilityInputMethodSessionCallback callback) { |
| 1576 |
|
| 1577 |
} |
| 1578 |
|
| 1579 |
@Override |
| 1580 |
public void startInput( |
| 1581 |
@Nullable RemoteAccessibilityInputConnection inputConnection, |
| 1582 |
@NonNull EditorInfo editorInfo, boolean restarting) { |
| 1583 |
} |
| 1584 |
|
| 1585 |
@Override |
| 1586 |
public boolean onGesture(AccessibilityGestureEvent gestureEvent) { |
| 1587 |
|
| 1588 |
return false; |
| 1589 |
} |
| 1590 |
|
| 1591 |
public void onMotionEvent(MotionEvent event) { |
| 1592 |
|
| 1593 |
} |
| 1594 |
|
| 1595 |
@Override |
| 1596 |
public void onTouchStateChanged(int displayId, int state) { |
| 1597 |
|
| 1598 |
} |
| 1599 |
|
| 1600 |
@Override |
| 1601 |
public void onAccessibilityEvent(AccessibilityEvent event) { |
| 1602 |
final OnAccessibilityEventListener listener; |
| 1603 |
synchronized (mLock) { |
| 1604 |
if (isGenerationChangedLocked()) { |
| 1605 |
return; |
| 1606 |
} |
| 1607 |
mLastEventTimeMillis = event.getEventTime(); |
| 1608 |
if (mWaitingForEventDelivery) { |
| 1609 |
mEventQueue.add(AccessibilityEvent.obtain(event)); |
| 1610 |
} |
| 1611 |
mLock.notifyAll(); |
| 1612 |
listener = mOnAccessibilityEventListener; |
| 1613 |
} |
| 1614 |
if (listener != null) { |
| 1615 |
|
| 1616 |
mLocalCallbackHandler.sendMessage(PooledLambda.obtainMessage( |
| 1617 |
OnAccessibilityEventListener::onAccessibilityEvent, |
| 1618 |
listener, AccessibilityEvent.obtain(event))); |
| 1619 |
} |
| 1620 |
} |
| 1621 |
|
| 1622 |
@Override |
| 1623 |
public boolean onKeyEvent(KeyEvent event) { |
| 1624 |
return false; |
| 1625 |
} |
| 1626 |
|
| 1627 |
@Override |
| 1628 |
public void onMagnificationChanged(int displayId, @NonNull Region region, |
| 1629 |
MagnificationConfig config) { |
| 1630 |
|
| 1631 |
} |
| 1632 |
|
| 1633 |
@Override |
| 1634 |
public void onSoftKeyboardShowModeChanged(int showMode) { |
| 1635 |
|
| 1636 |
} |
| 1637 |
|
| 1638 |
@Override |
| 1639 |
public void onPerformGestureResult(int sequence, boolean completedSuccessfully) { |
| 1640 |
|
| 1641 |
} |
| 1642 |
|
| 1643 |
@Override |
| 1644 |
public void onFingerprintCapturingGesturesChanged(boolean active) { |
| 1645 |
|
| 1646 |
} |
| 1647 |
|
| 1648 |
@Override |
| 1649 |
public void onFingerprintGesture(int gesture) { |
| 1650 |
|
| 1651 |
} |
| 1652 |
|
| 1653 |
@Override |
| 1654 |
public void onAccessibilityButtonClicked(int displayId) { |
| 1655 |
|
| 1656 |
} |
| 1657 |
|
| 1658 |
@Override |
| 1659 |
public void onAccessibilityButtonAvailabilityChanged(boolean available) { |
| 1660 |
|
| 1661 |
} |
| 1662 |
}); |
| 1663 |
} |
| 1664 |
} |
| 1665 |
} |