| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.compat.annotation.UnsupportedAppUsage; |
| 23 |
import android.content.ActivityNotFoundException; |
| 24 |
import android.content.ComponentName; |
| 25 |
import android.content.Context; |
| 26 |
import android.content.Intent; |
| 27 |
import android.content.IntentFilter; |
| 28 |
import android.content.pm.ActivityInfo; |
| 29 |
import android.content.res.Configuration; |
| 30 |
import android.hardware.input.InputManager; |
| 31 |
import android.net.Uri; |
| 32 |
import android.os.Build; |
| 33 |
import android.os.Bundle; |
| 34 |
import android.os.Debug; |
| 35 |
import android.os.IBinder; |
| 36 |
import android.os.Looper; |
| 37 |
import android.os.MessageQueue; |
| 38 |
import android.os.PerformanceCollector; |
| 39 |
import android.os.PersistableBundle; |
| 40 |
import android.os.Process; |
| 41 |
import android.os.RemoteException; |
| 42 |
import android.os.ServiceManager; |
| 43 |
import android.os.SystemClock; |
| 44 |
import android.os.TestLooperManager; |
| 45 |
import android.os.UserHandle; |
| 46 |
import android.util.AndroidRuntimeException; |
| 47 |
import android.util.Log; |
| 48 |
import android.view.IWindowManager; |
| 49 |
import android.view.InputDevice; |
| 50 |
import android.view.KeyCharacterMap; |
| 51 |
import android.view.KeyEvent; |
| 52 |
import android.view.MotionEvent; |
| 53 |
import android.view.SurfaceControl; |
| 54 |
import android.view.ViewConfiguration; |
| 55 |
import android.view.Window; |
| 56 |
import android.view.WindowManagerGlobal; |
| 57 |
|
| 58 |
import com.android.internal.content.ReferrerIntent; |
| 59 |
|
| 60 |
import java.io.File; |
| 61 |
import java.lang.annotation.Retention; |
| 62 |
import java.lang.annotation.RetentionPolicy; |
| 63 |
import java.util.ArrayList; |
| 64 |
import java.util.List; |
| 65 |
import java.util.concurrent.TimeoutException; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public class Instrumentation { |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public static final String REPORT_KEY_IDENTIFIER = "id"; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public static final String REPORT_KEY_STREAMRESULT = "stream"; |
| 91 |
|
| 92 |
private static final String TAG = "Instrumentation"; |
| 93 |
|
| 94 |
private static final long CONNECT_TIMEOUT_MILLIS = 5000; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
@Retention(RetentionPolicy.SOURCE) |
| 100 |
@IntDef({0, UiAutomation.FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES, |
| 101 |
UiAutomation.FLAG_DONT_USE_ACCESSIBILITY}) |
| 102 |
public @interface UiAutomationFlags {}; |
| 103 |
|
| 104 |
|
| 105 |
private final Object mSync = new Object(); |
| 106 |
private ActivityThread mThread = null; |
| 107 |
private MessageQueue mMessageQueue = null; |
| 108 |
private Context mInstrContext; |
| 109 |
private Context mAppContext; |
| 110 |
private ComponentName mComponent; |
| 111 |
private Thread mRunner; |
| 112 |
private List<ActivityWaiter> mWaitingActivities; |
| 113 |
private List<ActivityMonitor> mActivityMonitors; |
| 114 |
private IInstrumentationWatcher mWatcher; |
| 115 |
private IUiAutomationConnection mUiAutomationConnection; |
| 116 |
private boolean mAutomaticPerformanceSnapshots = false; |
| 117 |
private PerformanceCollector mPerformanceCollector; |
| 118 |
private Bundle mPerfMetrics = new Bundle(); |
| 119 |
private UiAutomation mUiAutomation; |
| 120 |
private final Object mAnimationCompleteLock = new Object(); |
| 121 |
|
| 122 |
public Instrumentation() { |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
private void checkInstrumenting(String method) { |
| 133 |
|
| 134 |
|
| 135 |
if (mInstrContext == null) { |
| 136 |
throw new RuntimeException(method + |
| 137 |
" cannot be called outside of instrumented processes"); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public boolean isInstrumenting() { |
| 147 |
|
| 148 |
|
| 149 |
if (mInstrContext == null) { |
| 150 |
return false; |
| 151 |
} |
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public void onCreate(Bundle arguments) { |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public void start() { |
| 179 |
if (mRunner != null) { |
| 180 |
throw new RuntimeException("Instrumentation already started"); |
| 181 |
} |
| 182 |
mRunner = new InstrumentationThread("Instr: " + getClass().getName()); |
| 183 |
mRunner.start(); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public void onStart() { |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public boolean onException(Object obj, Throwable e) { |
| 213 |
return false; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public void sendStatus(int resultCode, Bundle results) { |
| 223 |
if (mWatcher != null) { |
| 224 |
try { |
| 225 |
mWatcher.instrumentationStatus(mComponent, resultCode, results); |
| 226 |
} |
| 227 |
catch (RemoteException e) { |
| 228 |
mWatcher = null; |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public void addResults(Bundle results) { |
| 238 |
IActivityManager am = ActivityManager.getService(); |
| 239 |
try { |
| 240 |
am.addInstrumentationResults(mThread.getApplicationThread(), results); |
| 241 |
} catch (RemoteException ex) { |
| 242 |
throw ex.rethrowFromSystemServer(); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public void finish(int resultCode, Bundle results) { |
| 257 |
if (mAutomaticPerformanceSnapshots) { |
| 258 |
endPerformanceSnapshot(); |
| 259 |
} |
| 260 |
if (mPerfMetrics != null) { |
| 261 |
if (results == null) { |
| 262 |
results = new Bundle(); |
| 263 |
} |
| 264 |
results.putAll(mPerfMetrics); |
| 265 |
} |
| 266 |
if ((mUiAutomation != null) && !mUiAutomation.isDestroyed()) { |
| 267 |
mUiAutomation.disconnect(); |
| 268 |
mUiAutomation = null; |
| 269 |
} |
| 270 |
mThread.finishInstrumentation(resultCode, results); |
| 271 |
} |
| 272 |
|
| 273 |
public void setAutomaticPerformanceSnapshots() { |
| 274 |
mAutomaticPerformanceSnapshots = true; |
| 275 |
mPerformanceCollector = new PerformanceCollector(); |
| 276 |
} |
| 277 |
|
| 278 |
public void startPerformanceSnapshot() { |
| 279 |
if (!isProfiling()) { |
| 280 |
mPerformanceCollector.beginSnapshot(null); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
public void endPerformanceSnapshot() { |
| 285 |
if (!isProfiling()) { |
| 286 |
mPerfMetrics = mPerformanceCollector.endSnapshot(); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public void onDestroy() { |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
public Context getContext() { |
| 310 |
return mInstrContext; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
public ComponentName getComponentName() { |
| 319 |
return mComponent; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public Context getTargetContext() { |
| 334 |
return mAppContext; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public String getProcessName() { |
| 346 |
return mThread.getProcessName(); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
public boolean isProfiling() { |
| 355 |
return mThread.isProfiling(); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public void startProfiling() { |
| 364 |
if (mThread.isProfiling()) { |
| 365 |
File file = new File(mThread.getProfileFilePath()); |
| 366 |
file.getParentFile().mkdirs(); |
| 367 |
Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
public void stopProfiling() { |
| 375 |
if (mThread.isProfiling()) { |
| 376 |
Debug.stopMethodTracing(); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
public void setInTouchMode(boolean inTouch) { |
| 391 |
try { |
| 392 |
IWindowManager.Stub.asInterface( |
| 393 |
ServiceManager.getService("window")).setInTouchMode(inTouch); |
| 394 |
} catch (RemoteException e) { |
| 395 |
|
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
public void resetInTouchMode() { |
| 403 |
final boolean defaultInTouchMode = getContext().getResources().getBoolean( |
| 404 |
com.android.internal.R.bool.config_defaultInTouchMode); |
| 405 |
setInTouchMode(defaultInTouchMode); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
public void waitForIdle(Runnable recipient) { |
| 416 |
mMessageQueue.addIdleHandler(new Idler(recipient)); |
| 417 |
mThread.getHandler().post(new EmptyRunnable()); |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
public void waitForIdleSync() { |
| 426 |
validateNotAppThread(); |
| 427 |
Idler idler = new Idler(null); |
| 428 |
mMessageQueue.addIdleHandler(idler); |
| 429 |
mThread.getHandler().post(new EmptyRunnable()); |
| 430 |
idler.waitForIdle(); |
| 431 |
} |
| 432 |
|
| 433 |
private void waitForEnterAnimationComplete(Activity activity) { |
| 434 |
synchronized (mAnimationCompleteLock) { |
| 435 |
long timeout = 5000; |
| 436 |
try { |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
while (timeout > 0 && !activity.mEnterAnimationComplete) { |
| 441 |
long startTime = System.currentTimeMillis(); |
| 442 |
mAnimationCompleteLock.wait(timeout); |
| 443 |
long totalTime = System.currentTimeMillis() - startTime; |
| 444 |
timeout -= totalTime; |
| 445 |
} |
| 446 |
} catch (InterruptedException e) { |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
public void onEnterAnimationComplete() { |
| 453 |
synchronized (mAnimationCompleteLock) { |
| 454 |
mAnimationCompleteLock.notifyAll(); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
public void runOnMainSync(Runnable runner) { |
| 466 |
validateNotAppThread(); |
| 467 |
SyncRunnable sr = new SyncRunnable(runner); |
| 468 |
mThread.getHandler().post(sr); |
| 469 |
sr.waitForComplete(); |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
public Activity startActivitySync(Intent intent) { |
| 494 |
return startActivitySync(intent, null ); |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
@NonNull |
| 522 |
public Activity startActivitySync(@NonNull Intent intent, @Nullable Bundle options) { |
| 523 |
validateNotAppThread(); |
| 524 |
|
| 525 |
final Activity activity; |
| 526 |
synchronized (mSync) { |
| 527 |
intent = new Intent(intent); |
| 528 |
|
| 529 |
ActivityInfo ai = intent.resolveActivityInfo( |
| 530 |
getTargetContext().getPackageManager(), 0); |
| 531 |
if (ai == null) { |
| 532 |
throw new RuntimeException("Unable to resolve activity for: " + intent); |
| 533 |
} |
| 534 |
String myProc = mThread.getProcessName(); |
| 535 |
if (!ai.processName.equals(myProc)) { |
| 536 |
|
| 537 |
|
| 538 |
throw new RuntimeException("Intent in process " |
| 539 |
+ myProc + " resolved to different process " |
| 540 |
+ ai.processName + ": " + intent); |
| 541 |
} |
| 542 |
|
| 543 |
intent.setComponent(new ComponentName( |
| 544 |
ai.applicationInfo.packageName, ai.name)); |
| 545 |
final ActivityWaiter aw = new ActivityWaiter(intent); |
| 546 |
|
| 547 |
if (mWaitingActivities == null) { |
| 548 |
mWaitingActivities = new ArrayList(); |
| 549 |
} |
| 550 |
mWaitingActivities.add(aw); |
| 551 |
|
| 552 |
getTargetContext().startActivity(intent, options); |
| 553 |
|
| 554 |
do { |
| 555 |
try { |
| 556 |
mSync.wait(); |
| 557 |
} catch (InterruptedException e) { |
| 558 |
} |
| 559 |
} while (mWaitingActivities.contains(aw)); |
| 560 |
activity = aw.activity; |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
waitForEnterAnimationComplete(activity); |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
try (SurfaceControl.Transaction t = new SurfaceControl.Transaction()) { |
| 569 |
t.apply(true); |
| 570 |
} |
| 571 |
return activity; |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
public static class ActivityMonitor { |
| 587 |
private final IntentFilter mWhich; |
| 588 |
private final String mClass; |
| 589 |
private final ActivityResult mResult; |
| 590 |
private final boolean mBlock; |
| 591 |
private final boolean mIgnoreMatchingSpecificIntents; |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
int mHits = 0; |
| 596 |
|
| 597 |
|
| 598 |
Activity mLastActivity = null; |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
public ActivityMonitor( |
| 614 |
IntentFilter which, ActivityResult result, boolean block) { |
| 615 |
mWhich = which; |
| 616 |
mClass = null; |
| 617 |
mResult = result; |
| 618 |
mBlock = block; |
| 619 |
mIgnoreMatchingSpecificIntents = false; |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
public ActivityMonitor( |
| 636 |
String cls, ActivityResult result, boolean block) { |
| 637 |
mWhich = null; |
| 638 |
mClass = cls; |
| 639 |
mResult = result; |
| 640 |
mBlock = block; |
| 641 |
mIgnoreMatchingSpecificIntents = false; |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
public ActivityMonitor() { |
| 654 |
mWhich = null; |
| 655 |
mClass = null; |
| 656 |
mResult = null; |
| 657 |
mBlock = false; |
| 658 |
mIgnoreMatchingSpecificIntents = true; |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
final boolean ignoreMatchingSpecificIntents() { |
| 668 |
return mIgnoreMatchingSpecificIntents; |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
public final IntentFilter getFilter() { |
| 675 |
return mWhich; |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
public final ActivityResult getResult() { |
| 683 |
return mResult; |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
public final boolean isBlocking() { |
| 691 |
return mBlock; |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
public final int getHits() { |
| 698 |
return mHits; |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
public final Activity getLastActivity() { |
| 706 |
return mLastActivity; |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
public final Activity waitForActivity() { |
| 716 |
synchronized (this) { |
| 717 |
while (mLastActivity == null) { |
| 718 |
try { |
| 719 |
wait(); |
| 720 |
} catch (InterruptedException e) { |
| 721 |
} |
| 722 |
} |
| 723 |
Activity res = mLastActivity; |
| 724 |
mLastActivity = null; |
| 725 |
return res; |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
public final Activity waitForActivityWithTimeout(long timeOut) { |
| 739 |
synchronized (this) { |
| 740 |
if (mLastActivity == null) { |
| 741 |
try { |
| 742 |
wait(timeOut); |
| 743 |
} catch (InterruptedException e) { |
| 744 |
} |
| 745 |
} |
| 746 |
if (mLastActivity == null) { |
| 747 |
return null; |
| 748 |
} else { |
| 749 |
Activity res = mLastActivity; |
| 750 |
mLastActivity = null; |
| 751 |
return res; |
| 752 |
} |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
public ActivityResult onStartActivity(@NonNull Context who, @NonNull Intent intent, |
| 764 |
@NonNull Bundle options) { |
| 765 |
return onStartActivity(intent); |
| 766 |
} |
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
public ActivityResult onStartActivity(Intent intent) { |
| 783 |
return null; |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
public void onStartActivityResult(int result, @NonNull Bundle bOptions) {} |
| 796 |
|
| 797 |
final boolean match(Context who, |
| 798 |
Activity activity, |
| 799 |
Intent intent) { |
| 800 |
if (mIgnoreMatchingSpecificIntents) { |
| 801 |
return false; |
| 802 |
} |
| 803 |
synchronized (this) { |
| 804 |
if (mWhich != null |
| 805 |
&& mWhich.match(who.getContentResolver(), intent, |
| 806 |
true, "Instrumentation") < 0) { |
| 807 |
return false; |
| 808 |
} |
| 809 |
if (mClass != null) { |
| 810 |
String cls = null; |
| 811 |
if (activity != null) { |
| 812 |
cls = activity.getClass().getName(); |
| 813 |
} else if (intent.getComponent() != null) { |
| 814 |
cls = intent.getComponent().getClassName(); |
| 815 |
} |
| 816 |
if (cls == null || !mClass.equals(cls)) { |
| 817 |
return false; |
| 818 |
} |
| 819 |
} |
| 820 |
if (activity != null) { |
| 821 |
mLastActivity = activity; |
| 822 |
notifyAll(); |
| 823 |
} |
| 824 |
return true; |
| 825 |
} |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
public void addMonitor(ActivityMonitor monitor) { |
| 841 |
synchronized (mSync) { |
| 842 |
if (mActivityMonitors == null) { |
| 843 |
mActivityMonitors = new ArrayList(); |
| 844 |
} |
| 845 |
mActivityMonitors.add(monitor); |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
public ActivityMonitor addMonitor( |
| 867 |
IntentFilter filter, ActivityResult result, boolean block) { |
| 868 |
ActivityMonitor am = new ActivityMonitor(filter, result, block); |
| 869 |
addMonitor(am); |
| 870 |
return am; |
| 871 |
} |
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
public ActivityMonitor addMonitor( |
| 890 |
String cls, ActivityResult result, boolean block) { |
| 891 |
ActivityMonitor am = new ActivityMonitor(cls, result, block); |
| 892 |
addMonitor(am); |
| 893 |
return am; |
| 894 |
} |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
public boolean checkMonitorHit(ActivityMonitor monitor, int minHits) { |
| 910 |
waitForIdleSync(); |
| 911 |
synchronized (mSync) { |
| 912 |
if (monitor.getHits() < minHits) { |
| 913 |
return false; |
| 914 |
} |
| 915 |
mActivityMonitors.remove(monitor); |
| 916 |
} |
| 917 |
return true; |
| 918 |
} |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
public Activity waitForMonitor(ActivityMonitor monitor) { |
| 930 |
Activity activity = monitor.waitForActivity(); |
| 931 |
synchronized (mSync) { |
| 932 |
mActivityMonitors.remove(monitor); |
| 933 |
} |
| 934 |
return activity; |
| 935 |
} |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
public Activity waitForMonitorWithTimeout(ActivityMonitor monitor, long timeOut) { |
| 949 |
Activity activity = monitor.waitForActivityWithTimeout(timeOut); |
| 950 |
synchronized (mSync) { |
| 951 |
mActivityMonitors.remove(monitor); |
| 952 |
} |
| 953 |
return activity; |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
public void removeMonitor(ActivityMonitor monitor) { |
| 965 |
synchronized (mSync) { |
| 966 |
mActivityMonitors.remove(monitor); |
| 967 |
} |
| 968 |
} |
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
public boolean invokeMenuActionSync(Activity targetActivity, |
| 980 |
int id, int flag) { |
| 981 |
class MenuRunnable implements Runnable { |
| 982 |
private final Activity activity; |
| 983 |
private final int identifier; |
| 984 |
private final int flags; |
| 985 |
boolean returnValue; |
| 986 |
|
| 987 |
public MenuRunnable(Activity _activity, int _identifier, |
| 988 |
int _flags) { |
| 989 |
activity = _activity; |
| 990 |
identifier = _identifier; |
| 991 |
flags = _flags; |
| 992 |
} |
| 993 |
|
| 994 |
public void run() { |
| 995 |
Window win = activity.getWindow(); |
| 996 |
|
| 997 |
returnValue = win.performPanelIdentifierAction( |
| 998 |
Window.FEATURE_OPTIONS_PANEL, |
| 999 |
identifier, |
| 1000 |
flags); |
| 1001 |
} |
| 1002 |
|
| 1003 |
} |
| 1004 |
MenuRunnable mr = new MenuRunnable(targetActivity, id, flag); |
| 1005 |
runOnMainSync(mr); |
| 1006 |
return mr.returnValue; |
| 1007 |
} |
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
public boolean invokeContextMenuAction(Activity targetActivity, int id, int flag) { |
| 1020 |
validateNotAppThread(); |
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
final KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER); |
| 1027 |
sendKeySync(downEvent); |
| 1028 |
|
| 1029 |
|
| 1030 |
waitForIdleSync(); |
| 1031 |
try { |
| 1032 |
Thread.sleep(ViewConfiguration.getLongPressTimeout()); |
| 1033 |
} catch (InterruptedException e) { |
| 1034 |
Log.e(TAG, "Could not sleep for long press timeout", e); |
| 1035 |
return false; |
| 1036 |
} |
| 1037 |
|
| 1038 |
final KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER); |
| 1039 |
sendKeySync(upEvent); |
| 1040 |
|
| 1041 |
|
| 1042 |
waitForIdleSync(); |
| 1043 |
|
| 1044 |
class ContextMenuRunnable implements Runnable { |
| 1045 |
private final Activity activity; |
| 1046 |
private final int identifier; |
| 1047 |
private final int flags; |
| 1048 |
boolean returnValue; |
| 1049 |
|
| 1050 |
public ContextMenuRunnable(Activity _activity, int _identifier, |
| 1051 |
int _flags) { |
| 1052 |
activity = _activity; |
| 1053 |
identifier = _identifier; |
| 1054 |
flags = _flags; |
| 1055 |
} |
| 1056 |
|
| 1057 |
public void run() { |
| 1058 |
Window win = activity.getWindow(); |
| 1059 |
returnValue = win.performContextMenuIdentifierAction( |
| 1060 |
identifier, |
| 1061 |
flags); |
| 1062 |
} |
| 1063 |
|
| 1064 |
} |
| 1065 |
|
| 1066 |
ContextMenuRunnable cmr = new ContextMenuRunnable(targetActivity, id, flag); |
| 1067 |
runOnMainSync(cmr); |
| 1068 |
return cmr.returnValue; |
| 1069 |
} |
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
public void sendStringSync(String text) { |
| 1079 |
if (text == null) { |
| 1080 |
return; |
| 1081 |
} |
| 1082 |
KeyCharacterMap keyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD); |
| 1083 |
|
| 1084 |
KeyEvent[] events = keyCharacterMap.getEvents(text.toCharArray()); |
| 1085 |
|
| 1086 |
if (events != null) { |
| 1087 |
for (int i = 0; i < events.length; i++) { |
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
sendKeySync(KeyEvent.changeTimeRepeat(events[i], SystemClock.uptimeMillis(), 0)); |
| 1094 |
} |
| 1095 |
} |
| 1096 |
} |
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
public void sendKeySync(KeyEvent event) { |
| 1109 |
validateNotAppThread(); |
| 1110 |
|
| 1111 |
long downTime = event.getDownTime(); |
| 1112 |
long eventTime = event.getEventTime(); |
| 1113 |
int source = event.getSource(); |
| 1114 |
if (source == InputDevice.SOURCE_UNKNOWN) { |
| 1115 |
source = InputDevice.SOURCE_KEYBOARD; |
| 1116 |
} |
| 1117 |
if (eventTime == 0) { |
| 1118 |
eventTime = SystemClock.uptimeMillis(); |
| 1119 |
} |
| 1120 |
if (downTime == 0) { |
| 1121 |
downTime = eventTime; |
| 1122 |
} |
| 1123 |
KeyEvent newEvent = new KeyEvent(event); |
| 1124 |
newEvent.setTime(downTime, eventTime); |
| 1125 |
newEvent.setSource(source); |
| 1126 |
newEvent.setFlags(event.getFlags() | KeyEvent.FLAG_FROM_SYSTEM); |
| 1127 |
InputManager.getInstance().injectInputEvent(newEvent, |
| 1128 |
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH); |
| 1129 |
} |
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
public void sendKeyDownUpSync(int keyCode) { |
| 1139 |
sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, keyCode)); |
| 1140 |
sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, keyCode)); |
| 1141 |
} |
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
public void sendCharacterSync(int keyCode) { |
| 1153 |
sendKeyDownUpSync(keyCode); |
| 1154 |
} |
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
public void sendPointerSync(MotionEvent event) { |
| 1174 |
validateNotAppThread(); |
| 1175 |
if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) { |
| 1176 |
event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
| 1177 |
} |
| 1178 |
|
| 1179 |
syncInputTransactionsAndInjectEventIntoSelf(event); |
| 1180 |
} |
| 1181 |
|
| 1182 |
private void syncInputTransactionsAndInjectEventIntoSelf(MotionEvent event) { |
| 1183 |
final boolean syncBefore = event.getAction() == MotionEvent.ACTION_DOWN |
| 1184 |
|| event.isFromSource(InputDevice.SOURCE_MOUSE); |
| 1185 |
final boolean syncAfter = event.getAction() == MotionEvent.ACTION_UP; |
| 1186 |
|
| 1187 |
try { |
| 1188 |
if (syncBefore) { |
| 1189 |
WindowManagerGlobal.getWindowManagerService() |
| 1190 |
.syncInputTransactions(true ); |
| 1191 |
} |
| 1192 |
|
| 1193 |
|
| 1194 |
InputManager.getInstance().injectInputEvent( |
| 1195 |
event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH, Process.myUid()); |
| 1196 |
|
| 1197 |
if (syncAfter) { |
| 1198 |
WindowManagerGlobal.getWindowManagerService() |
| 1199 |
.syncInputTransactions(true ); |
| 1200 |
} |
| 1201 |
} catch (RemoteException e) { |
| 1202 |
e.rethrowFromSystemServer(); |
| 1203 |
} |
| 1204 |
} |
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
public void sendTrackballEventSync(MotionEvent event) { |
| 1220 |
validateNotAppThread(); |
| 1221 |
if (!event.isFromSource(InputDevice.SOURCE_CLASS_TRACKBALL)) { |
| 1222 |
event.setSource(InputDevice.SOURCE_TRACKBALL); |
| 1223 |
} |
| 1224 |
InputManager.getInstance().injectInputEvent(event, |
| 1225 |
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH); |
| 1226 |
} |
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
public Application newApplication(ClassLoader cl, String className, Context context) |
| 1240 |
throws InstantiationException, IllegalAccessException, |
| 1241 |
ClassNotFoundException { |
| 1242 |
Application app = getFactory(context.getPackageName()) |
| 1243 |
.instantiateApplication(cl, className); |
| 1244 |
app.attach(context); |
| 1245 |
return app; |
| 1246 |
} |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
static public Application newApplication(Class<?> clazz, Context context) |
| 1258 |
throws InstantiationException, IllegalAccessException, |
| 1259 |
ClassNotFoundException { |
| 1260 |
Application app = (Application)clazz.newInstance(); |
| 1261 |
app.attach(context); |
| 1262 |
return app; |
| 1263 |
} |
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
public void callApplicationOnCreate(Application app) { |
| 1277 |
app.onCreate(); |
| 1278 |
} |
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
public Activity newActivity(Class<?> clazz, Context context, |
| 1302 |
IBinder token, Application application, Intent intent, ActivityInfo info, |
| 1303 |
CharSequence title, Activity parent, String id, |
| 1304 |
Object lastNonConfigurationInstance) throws InstantiationException, |
| 1305 |
IllegalAccessException { |
| 1306 |
Activity activity = (Activity)clazz.newInstance(); |
| 1307 |
ActivityThread aThread = null; |
| 1308 |
|
| 1309 |
if (application == null) { |
| 1310 |
application = new Application(); |
| 1311 |
} |
| 1312 |
activity.attach(context, aThread, this, token, 0 , application, intent, |
| 1313 |
info, title, parent, id, |
| 1314 |
(Activity.NonConfigurationInstances)lastNonConfigurationInstance, |
| 1315 |
new Configuration(), null , null , |
| 1316 |
null , null , null , |
| 1317 |
null ); |
| 1318 |
return activity; |
| 1319 |
} |
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
public Activity newActivity(ClassLoader cl, String className, |
| 1334 |
Intent intent) |
| 1335 |
throws InstantiationException, IllegalAccessException, |
| 1336 |
ClassNotFoundException { |
| 1337 |
String pkg = intent != null && intent.getComponent() != null |
| 1338 |
? intent.getComponent().getPackageName() : null; |
| 1339 |
return getFactory(pkg).instantiateActivity(cl, className, intent); |
| 1340 |
} |
| 1341 |
|
| 1342 |
private AppComponentFactory getFactory(String pkg) { |
| 1343 |
if (pkg == null) { |
| 1344 |
Log.e(TAG, "No pkg specified, disabling AppComponentFactory"); |
| 1345 |
return AppComponentFactory.DEFAULT; |
| 1346 |
} |
| 1347 |
if (mThread == null) { |
| 1348 |
Log.e(TAG, "Uninitialized ActivityThread, likely app-created Instrumentation," |
| 1349 |
+ " disabling AppComponentFactory", new Throwable()); |
| 1350 |
return AppComponentFactory.DEFAULT; |
| 1351 |
} |
| 1352 |
LoadedApk apk = mThread.peekPackageInfo(pkg, true); |
| 1353 |
|
| 1354 |
if (apk == null) apk = mThread.getSystemContext().mPackageInfo; |
| 1355 |
return apk.getAppFactory(); |
| 1356 |
} |
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
private void notifyStartActivityResult(int result, @Nullable Bundle options) { |
| 1363 |
if (mActivityMonitors == null) { |
| 1364 |
return; |
| 1365 |
} |
| 1366 |
synchronized (mSync) { |
| 1367 |
final int size = mActivityMonitors.size(); |
| 1368 |
for (int i = 0; i < size; i++) { |
| 1369 |
final ActivityMonitor am = mActivityMonitors.get(i); |
| 1370 |
if (am.ignoreMatchingSpecificIntents()) { |
| 1371 |
if (options == null) { |
| 1372 |
options = ActivityOptions.makeBasic().toBundle(); |
| 1373 |
} |
| 1374 |
am.onStartActivityResult(result, options); |
| 1375 |
} |
| 1376 |
} |
| 1377 |
} |
| 1378 |
} |
| 1379 |
|
| 1380 |
private void prePerformCreate(Activity activity) { |
| 1381 |
if (mWaitingActivities != null) { |
| 1382 |
synchronized (mSync) { |
| 1383 |
final int N = mWaitingActivities.size(); |
| 1384 |
for (int i=0; i<N; i++) { |
| 1385 |
final ActivityWaiter aw = mWaitingActivities.get(i); |
| 1386 |
final Intent intent = aw.intent; |
| 1387 |
if (intent.filterEquals(activity.getIntent())) { |
| 1388 |
aw.activity = activity; |
| 1389 |
mMessageQueue.addIdleHandler(new ActivityGoing(aw)); |
| 1390 |
} |
| 1391 |
} |
| 1392 |
} |
| 1393 |
} |
| 1394 |
} |
| 1395 |
|
| 1396 |
private void postPerformCreate(Activity activity) { |
| 1397 |
if (mActivityMonitors != null) { |
| 1398 |
synchronized (mSync) { |
| 1399 |
final int N = mActivityMonitors.size(); |
| 1400 |
for (int i=0; i<N; i++) { |
| 1401 |
final ActivityMonitor am = mActivityMonitors.get(i); |
| 1402 |
am.match(activity, activity, activity.getIntent()); |
| 1403 |
} |
| 1404 |
} |
| 1405 |
} |
| 1406 |
} |
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
public void callActivityOnCreate(Activity activity, Bundle icicle) { |
| 1416 |
prePerformCreate(activity); |
| 1417 |
activity.performCreate(icicle); |
| 1418 |
postPerformCreate(activity); |
| 1419 |
} |
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
public void callActivityOnCreate(Activity activity, Bundle icicle, |
| 1429 |
PersistableBundle persistentState) { |
| 1430 |
prePerformCreate(activity); |
| 1431 |
activity.performCreate(icicle, persistentState); |
| 1432 |
postPerformCreate(activity); |
| 1433 |
} |
| 1434 |
|
| 1435 |
public void callActivityOnDestroy(Activity activity) { |
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
activity.performDestroy(); |
| 1453 |
} |
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
public void callActivityOnRestoreInstanceState(@NonNull Activity activity, |
| 1463 |
@NonNull Bundle savedInstanceState) { |
| 1464 |
activity.performRestoreInstanceState(savedInstanceState); |
| 1465 |
} |
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
public void callActivityOnRestoreInstanceState(@NonNull Activity activity, |
| 1476 |
@Nullable Bundle savedInstanceState, |
| 1477 |
@Nullable PersistableBundle persistentState) { |
| 1478 |
activity.performRestoreInstanceState(savedInstanceState, persistentState); |
| 1479 |
} |
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
public void callActivityOnPostCreate(@NonNull Activity activity, |
| 1490 |
@Nullable Bundle savedInstanceState) { |
| 1491 |
activity.onPostCreate(savedInstanceState); |
| 1492 |
} |
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
public void callActivityOnPostCreate(@NonNull Activity activity, |
| 1504 |
@Nullable Bundle savedInstanceState, |
| 1505 |
@Nullable PersistableBundle persistentState) { |
| 1506 |
activity.onPostCreate(savedInstanceState, persistentState); |
| 1507 |
} |
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
public void callActivityOnNewIntent(Activity activity, Intent intent) { |
| 1517 |
activity.performNewIntent(intent); |
| 1518 |
} |
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1524 |
public void callActivityOnNewIntent(Activity activity, ReferrerIntent intent) { |
| 1525 |
final String oldReferrer = activity.mReferrer; |
| 1526 |
try { |
| 1527 |
if (intent != null) { |
| 1528 |
activity.mReferrer = intent.mReferrer; |
| 1529 |
} |
| 1530 |
callActivityOnNewIntent(activity, intent != null ? new Intent(intent) : null); |
| 1531 |
} finally { |
| 1532 |
activity.mReferrer = oldReferrer; |
| 1533 |
} |
| 1534 |
} |
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
public void callActivityOnStart(Activity activity) { |
| 1543 |
activity.onStart(); |
| 1544 |
} |
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
public void callActivityOnRestart(Activity activity) { |
| 1553 |
activity.onRestart(); |
| 1554 |
} |
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
public void callActivityOnResume(Activity activity) { |
| 1563 |
activity.mResumed = true; |
| 1564 |
activity.onResume(); |
| 1565 |
|
| 1566 |
if (mActivityMonitors != null) { |
| 1567 |
synchronized (mSync) { |
| 1568 |
final int N = mActivityMonitors.size(); |
| 1569 |
for (int i=0; i<N; i++) { |
| 1570 |
final ActivityMonitor am = mActivityMonitors.get(i); |
| 1571 |
am.match(activity, activity, activity.getIntent()); |
| 1572 |
} |
| 1573 |
} |
| 1574 |
} |
| 1575 |
} |
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
public void callActivityOnStop(Activity activity) { |
| 1584 |
activity.onStop(); |
| 1585 |
} |
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
public void callActivityOnSaveInstanceState(@NonNull Activity activity, |
| 1595 |
@NonNull Bundle outState) { |
| 1596 |
activity.performSaveInstanceState(outState); |
| 1597 |
} |
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
public void callActivityOnSaveInstanceState(@NonNull Activity activity, |
| 1607 |
@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) { |
| 1608 |
activity.performSaveInstanceState(outState, outPersistentState); |
| 1609 |
} |
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
public void callActivityOnPause(Activity activity) { |
| 1618 |
activity.performPause(); |
| 1619 |
} |
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
public void callActivityOnUserLeaving(Activity activity) { |
| 1628 |
activity.performUserLeaving(); |
| 1629 |
} |
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
public void callActivityOnPictureInPictureRequested(@NonNull Activity activity) { |
| 1638 |
activity.onPictureInPictureRequested(); |
| 1639 |
} |
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
@Deprecated |
| 1647 |
public void startAllocCounting() { |
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
Runtime.getRuntime().gc(); |
| 1653 |
Runtime.getRuntime().runFinalization(); |
| 1654 |
Runtime.getRuntime().gc(); |
| 1655 |
|
| 1656 |
Debug.resetAllCounts(); |
| 1657 |
|
| 1658 |
|
| 1659 |
Debug.startAllocCounting(); |
| 1660 |
} |
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
@Deprecated |
| 1668 |
public void stopAllocCounting() { |
| 1669 |
Runtime.getRuntime().gc(); |
| 1670 |
Runtime.getRuntime().runFinalization(); |
| 1671 |
Runtime.getRuntime().gc(); |
| 1672 |
Debug.stopAllocCounting(); |
| 1673 |
} |
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
private void addValue(String key, int value, Bundle results) { |
| 1681 |
if (results.containsKey(key)) { |
| 1682 |
List<Integer> list = results.getIntegerArrayList(key); |
| 1683 |
if (list != null) { |
| 1684 |
list.add(value); |
| 1685 |
} |
| 1686 |
} else { |
| 1687 |
ArrayList<Integer> list = new ArrayList<Integer>(); |
| 1688 |
list.add(value); |
| 1689 |
results.putIntegerArrayList(key, list); |
| 1690 |
} |
| 1691 |
} |
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
public Bundle getAllocCounts() { |
| 1697 |
Bundle results = new Bundle(); |
| 1698 |
results.putLong("global_alloc_count", Debug.getGlobalAllocCount()); |
| 1699 |
results.putLong("global_alloc_size", Debug.getGlobalAllocSize()); |
| 1700 |
results.putLong("global_freed_count", Debug.getGlobalFreedCount()); |
| 1701 |
results.putLong("global_freed_size", Debug.getGlobalFreedSize()); |
| 1702 |
results.putLong("gc_invocation_count", Debug.getGlobalGcInvocationCount()); |
| 1703 |
return results; |
| 1704 |
} |
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
public Bundle getBinderCounts() { |
| 1711 |
Bundle results = new Bundle(); |
| 1712 |
results.putLong("sent_transactions", Debug.getBinderSentTransactions()); |
| 1713 |
results.putLong("received_transactions", Debug.getBinderReceivedTransactions()); |
| 1714 |
return results; |
| 1715 |
} |
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
public static final class ActivityResult { |
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
public ActivityResult(int resultCode, Intent resultData) { |
| 1732 |
mResultCode = resultCode; |
| 1733 |
mResultData = resultData; |
| 1734 |
} |
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
public int getResultCode() { |
| 1740 |
return mResultCode; |
| 1741 |
} |
| 1742 |
|
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
public Intent getResultData() { |
| 1747 |
return mResultData; |
| 1748 |
} |
| 1749 |
|
| 1750 |
private final int mResultCode; |
| 1751 |
private final Intent mResultData; |
| 1752 |
} |
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
@UnsupportedAppUsage |
| 1798 |
public ActivityResult execStartActivity( |
| 1799 |
Context who, IBinder contextThread, IBinder token, Activity target, |
| 1800 |
Intent intent, int requestCode, Bundle options) { |
| 1801 |
IApplicationThread whoThread = (IApplicationThread) contextThread; |
| 1802 |
Uri referrer = target != null ? target.onProvideReferrer() : null; |
| 1803 |
if (referrer != null) { |
| 1804 |
intent.putExtra(Intent.EXTRA_REFERRER, referrer); |
| 1805 |
} |
| 1806 |
if (mActivityMonitors != null) { |
| 1807 |
synchronized (mSync) { |
| 1808 |
final int N = mActivityMonitors.size(); |
| 1809 |
for (int i=0; i<N; i++) { |
| 1810 |
final ActivityMonitor am = mActivityMonitors.get(i); |
| 1811 |
ActivityResult result = null; |
| 1812 |
if (am.ignoreMatchingSpecificIntents()) { |
| 1813 |
if (options == null) { |
| 1814 |
options = ActivityOptions.makeBasic().toBundle(); |
| 1815 |
} |
| 1816 |
result = am.onStartActivity(who, intent, options); |
| 1817 |
} |
| 1818 |
if (result != null) { |
| 1819 |
am.mHits++; |
| 1820 |
return result; |
| 1821 |
} else if (am.match(who, null, intent)) { |
| 1822 |
am.mHits++; |
| 1823 |
if (am.isBlocking()) { |
| 1824 |
return requestCode >= 0 ? am.getResult() : null; |
| 1825 |
} |
| 1826 |
break; |
| 1827 |
} |
| 1828 |
} |
| 1829 |
} |
| 1830 |
} |
| 1831 |
try { |
| 1832 |
intent.migrateExtraStreamToClipData(who); |
| 1833 |
intent.prepareToLeaveProcess(who); |
| 1834 |
int result = ActivityTaskManager.getService().startActivity(whoThread, |
| 1835 |
who.getOpPackageName(), who.getAttributionTag(), intent, |
| 1836 |
intent.resolveTypeIfNeeded(who.getContentResolver()), token, |
| 1837 |
target != null ? target.mEmbeddedID : null, requestCode, 0, null, options); |
| 1838 |
notifyStartActivityResult(result, options); |
| 1839 |
checkStartActivityResult(result, intent); |
| 1840 |
} catch (RemoteException e) { |
| 1841 |
throw new RuntimeException("Failure from system", e); |
| 1842 |
} |
| 1843 |
return null; |
| 1844 |
} |
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
@UnsupportedAppUsage |
| 1855 |
public void execStartActivities(Context who, IBinder contextThread, |
| 1856 |
IBinder token, Activity target, Intent[] intents, Bundle options) { |
| 1857 |
execStartActivitiesAsUser(who, contextThread, token, target, intents, options, |
| 1858 |
who.getUserId()); |
| 1859 |
} |
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1874 |
public int execStartActivitiesAsUser(Context who, IBinder contextThread, |
| 1875 |
IBinder token, Activity target, Intent[] intents, Bundle options, |
| 1876 |
int userId) { |
| 1877 |
IApplicationThread whoThread = (IApplicationThread) contextThread; |
| 1878 |
if (mActivityMonitors != null) { |
| 1879 |
synchronized (mSync) { |
| 1880 |
final int N = mActivityMonitors.size(); |
| 1881 |
for (int i=0; i<N; i++) { |
| 1882 |
final ActivityMonitor am = mActivityMonitors.get(i); |
| 1883 |
ActivityResult result = null; |
| 1884 |
if (am.ignoreMatchingSpecificIntents()) { |
| 1885 |
if (options == null) { |
| 1886 |
options = ActivityOptions.makeBasic().toBundle(); |
| 1887 |
} |
| 1888 |
result = am.onStartActivity(who, intents[0], options); |
| 1889 |
} |
| 1890 |
if (result != null) { |
| 1891 |
am.mHits++; |
| 1892 |
return ActivityManager.START_CANCELED; |
| 1893 |
} else if (am.match(who, null, intents[0])) { |
| 1894 |
am.mHits++; |
| 1895 |
if (am.isBlocking()) { |
| 1896 |
return ActivityManager.START_CANCELED; |
| 1897 |
} |
| 1898 |
break; |
| 1899 |
} |
| 1900 |
} |
| 1901 |
} |
| 1902 |
} |
| 1903 |
try { |
| 1904 |
String[] resolvedTypes = new String[intents.length]; |
| 1905 |
for (int i=0; i<intents.length; i++) { |
| 1906 |
intents[i].migrateExtraStreamToClipData(who); |
| 1907 |
intents[i].prepareToLeaveProcess(who); |
| 1908 |
resolvedTypes[i] = intents[i].resolveTypeIfNeeded(who.getContentResolver()); |
| 1909 |
} |
| 1910 |
int result = ActivityTaskManager.getService().startActivities(whoThread, |
| 1911 |
who.getOpPackageName(), who.getAttributionTag(), intents, resolvedTypes, |
| 1912 |
token, options, userId); |
| 1913 |
notifyStartActivityResult(result, options); |
| 1914 |
checkStartActivityResult(result, intents[0]); |
| 1915 |
return result; |
| 1916 |
} catch (RemoteException e) { |
| 1917 |
throw new RuntimeException("Failure from system", e); |
| 1918 |
} |
| 1919 |
} |
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
|
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 1930 |
|
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
|
| 1936 |
|
| 1937 |
|
| 1938 |
|
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
@UnsupportedAppUsage |
| 1949 |
public ActivityResult execStartActivity( |
| 1950 |
Context who, IBinder contextThread, IBinder token, String target, |
| 1951 |
Intent intent, int requestCode, Bundle options) { |
| 1952 |
IApplicationThread whoThread = (IApplicationThread) contextThread; |
| 1953 |
if (mActivityMonitors != null) { |
| 1954 |
synchronized (mSync) { |
| 1955 |
final int N = mActivityMonitors.size(); |
| 1956 |
for (int i=0; i<N; i++) { |
| 1957 |
final ActivityMonitor am = mActivityMonitors.get(i); |
| 1958 |
ActivityResult result = null; |
| 1959 |
if (am.ignoreMatchingSpecificIntents()) { |
| 1960 |
if (options == null) { |
| 1961 |
options = ActivityOptions.makeBasic().toBundle(); |
| 1962 |
} |
| 1963 |
result = am.onStartActivity(who, intent, options); |
| 1964 |
} |
| 1965 |
if (result != null) { |
| 1966 |
am.mHits++; |
| 1967 |
return result; |
| 1968 |
} else if (am.match(who, null, intent)) { |
| 1969 |
am.mHits++; |
| 1970 |
if (am.isBlocking()) { |
| 1971 |
return requestCode >= 0 ? am.getResult() : null; |
| 1972 |
} |
| 1973 |
break; |
| 1974 |
} |
| 1975 |
} |
| 1976 |
} |
| 1977 |
} |
| 1978 |
try { |
| 1979 |
intent.migrateExtraStreamToClipData(who); |
| 1980 |
intent.prepareToLeaveProcess(who); |
| 1981 |
int result = ActivityTaskManager.getService().startActivity(whoThread, |
| 1982 |
who.getOpPackageName(), who.getAttributionTag(), intent, |
| 1983 |
intent.resolveTypeIfNeeded(who.getContentResolver()), token, target, |
| 1984 |
requestCode, 0, null, options); |
| 1985 |
notifyStartActivityResult(result, options); |
| 1986 |
checkStartActivityResult(result, intent); |
| 1987 |
} catch (RemoteException e) { |
| 1988 |
throw new RuntimeException("Failure from system", e); |
| 1989 |
} |
| 1990 |
return null; |
| 1991 |
} |
| 1992 |
|
| 1993 |
|
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
|
| 2007 |
|
| 2008 |
|
| 2009 |
|
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
@UnsupportedAppUsage |
| 2020 |
public ActivityResult execStartActivity( |
| 2021 |
Context who, IBinder contextThread, IBinder token, String resultWho, |
| 2022 |
Intent intent, int requestCode, Bundle options, UserHandle user) { |
| 2023 |
IApplicationThread whoThread = (IApplicationThread) contextThread; |
| 2024 |
if (mActivityMonitors != null) { |
| 2025 |
synchronized (mSync) { |
| 2026 |
final int N = mActivityMonitors.size(); |
| 2027 |
for (int i=0; i<N; i++) { |
| 2028 |
final ActivityMonitor am = mActivityMonitors.get(i); |
| 2029 |
ActivityResult result = null; |
| 2030 |
if (am.ignoreMatchingSpecificIntents()) { |
| 2031 |
if (options == null) { |
| 2032 |
options = ActivityOptions.makeBasic().toBundle(); |
| 2033 |
} |
| 2034 |
result = am.onStartActivity(who, intent, options); |
| 2035 |
} |
| 2036 |
if (result != null) { |
| 2037 |
am.mHits++; |
| 2038 |
return result; |
| 2039 |
} else if (am.match(who, null, intent)) { |
| 2040 |
am.mHits++; |
| 2041 |
if (am.isBlocking()) { |
| 2042 |
return requestCode >= 0 ? am.getResult() : null; |
| 2043 |
} |
| 2044 |
break; |
| 2045 |
} |
| 2046 |
} |
| 2047 |
} |
| 2048 |
} |
| 2049 |
try { |
| 2050 |
intent.migrateExtraStreamToClipData(who); |
| 2051 |
intent.prepareToLeaveProcess(who); |
| 2052 |
int result = ActivityTaskManager.getService().startActivityAsUser(whoThread, |
| 2053 |
who.getOpPackageName(), who.getAttributionTag(), intent, |
| 2054 |
intent.resolveTypeIfNeeded(who.getContentResolver()), token, resultWho, |
| 2055 |
requestCode, 0, null, options, user.getIdentifier()); |
| 2056 |
notifyStartActivityResult(result, options); |
| 2057 |
checkStartActivityResult(result, intent); |
| 2058 |
} catch (RemoteException e) { |
| 2059 |
throw new RuntimeException("Failure from system", e); |
| 2060 |
} |
| 2061 |
return null; |
| 2062 |
} |
| 2063 |
|
| 2064 |
|
| 2065 |
|
| 2066 |
|
| 2067 |
|
| 2068 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2069 |
public ActivityResult execStartActivityAsCaller( |
| 2070 |
Context who, IBinder contextThread, IBinder token, Activity target, |
| 2071 |
Intent intent, int requestCode, Bundle options, |
| 2072 |
boolean ignoreTargetSecurity, int userId) { |
| 2073 |
IApplicationThread whoThread = (IApplicationThread) contextThread; |
| 2074 |
if (mActivityMonitors != null) { |
| 2075 |
synchronized (mSync) { |
| 2076 |
final int N = mActivityMonitors.size(); |
| 2077 |
for (int i=0; i<N; i++) { |
| 2078 |
final ActivityMonitor am = mActivityMonitors.get(i); |
| 2079 |
ActivityResult result = null; |
| 2080 |
if (am.ignoreMatchingSpecificIntents()) { |
| 2081 |
if (options == null) { |
| 2082 |
options = ActivityOptions.makeBasic().toBundle(); |
| 2083 |
} |
| 2084 |
result = am.onStartActivity(who, intent, options); |
| 2085 |
} |
| 2086 |
if (result != null) { |
| 2087 |
am.mHits++; |
| 2088 |
return result; |
| 2089 |
} else if (am.match(who, null, intent)) { |
| 2090 |
am.mHits++; |
| 2091 |
if (am.isBlocking()) { |
| 2092 |
return requestCode >= 0 ? am.getResult() : null; |
| 2093 |
} |
| 2094 |
break; |
| 2095 |
} |
| 2096 |
} |
| 2097 |
} |
| 2098 |
} |
| 2099 |
try { |
| 2100 |
intent.migrateExtraStreamToClipData(who); |
| 2101 |
intent.prepareToLeaveProcess(who); |
| 2102 |
int result = ActivityTaskManager.getService() |
| 2103 |
.startActivityAsCaller(whoThread, who.getOpPackageName(), intent, |
| 2104 |
intent.resolveTypeIfNeeded(who.getContentResolver()), |
| 2105 |
token, target != null ? target.mEmbeddedID : null, |
| 2106 |
requestCode, 0, null, options, |
| 2107 |
ignoreTargetSecurity, userId); |
| 2108 |
notifyStartActivityResult(result, options); |
| 2109 |
checkStartActivityResult(result, intent); |
| 2110 |
} catch (RemoteException e) { |
| 2111 |
throw new RuntimeException("Failure from system", e); |
| 2112 |
} |
| 2113 |
return null; |
| 2114 |
} |
| 2115 |
|
| 2116 |
|
| 2117 |
|
| 2118 |
|
| 2119 |
|
| 2120 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2121 |
public void execStartActivityFromAppTask( |
| 2122 |
Context who, IBinder contextThread, IAppTask appTask, |
| 2123 |
Intent intent, Bundle options) { |
| 2124 |
IApplicationThread whoThread = (IApplicationThread) contextThread; |
| 2125 |
if (mActivityMonitors != null) { |
| 2126 |
synchronized (mSync) { |
| 2127 |
final int N = mActivityMonitors.size(); |
| 2128 |
for (int i=0; i<N; i++) { |
| 2129 |
final ActivityMonitor am = mActivityMonitors.get(i); |
| 2130 |
ActivityResult result = null; |
| 2131 |
if (am.ignoreMatchingSpecificIntents()) { |
| 2132 |
if (options == null) { |
| 2133 |
options = ActivityOptions.makeBasic().toBundle(); |
| 2134 |
} |
| 2135 |
result = am.onStartActivity(who, intent, options); |
| 2136 |
} |
| 2137 |
if (result != null) { |
| 2138 |
am.mHits++; |
| 2139 |
return; |
| 2140 |
} else if (am.match(who, null, intent)) { |
| 2141 |
am.mHits++; |
| 2142 |
if (am.isBlocking()) { |
| 2143 |
return; |
| 2144 |
} |
| 2145 |
break; |
| 2146 |
} |
| 2147 |
} |
| 2148 |
} |
| 2149 |
} |
| 2150 |
try { |
| 2151 |
intent.migrateExtraStreamToClipData(who); |
| 2152 |
intent.prepareToLeaveProcess(who); |
| 2153 |
int result = appTask.startActivity(whoThread.asBinder(), who.getOpPackageName(), |
| 2154 |
who.getAttributionTag(), intent, |
| 2155 |
intent.resolveTypeIfNeeded(who.getContentResolver()), options); |
| 2156 |
notifyStartActivityResult(result, options); |
| 2157 |
checkStartActivityResult(result, intent); |
| 2158 |
} catch (RemoteException e) { |
| 2159 |
throw new RuntimeException("Failure from system", e); |
| 2160 |
} |
| 2161 |
return; |
| 2162 |
} |
| 2163 |
|
| 2164 |
final void init(ActivityThread thread, |
| 2165 |
Context instrContext, Context appContext, ComponentName component, |
| 2166 |
IInstrumentationWatcher watcher, IUiAutomationConnection uiAutomationConnection) { |
| 2167 |
mThread = thread; |
| 2168 |
mMessageQueue = mThread.getLooper().myQueue(); |
| 2169 |
mInstrContext = instrContext; |
| 2170 |
mAppContext = appContext; |
| 2171 |
mComponent = component; |
| 2172 |
mWatcher = watcher; |
| 2173 |
mUiAutomationConnection = uiAutomationConnection; |
| 2174 |
} |
| 2175 |
|
| 2176 |
|
| 2177 |
|
| 2178 |
|
| 2179 |
|
| 2180 |
final void basicInit(ActivityThread thread) { |
| 2181 |
mThread = thread; |
| 2182 |
} |
| 2183 |
|
| 2184 |
|
| 2185 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 2186 |
public static void checkStartActivityResult(int res, Object intent) { |
| 2187 |
if (!ActivityManager.isStartResultFatalError(res)) { |
| 2188 |
return; |
| 2189 |
} |
| 2190 |
|
| 2191 |
switch (res) { |
| 2192 |
case ActivityManager.START_INTENT_NOT_RESOLVED: |
| 2193 |
case ActivityManager.START_CLASS_NOT_FOUND: |
| 2194 |
if (intent instanceof Intent && ((Intent)intent).getComponent() != null) |
| 2195 |
throw new ActivityNotFoundException( |
| 2196 |
"Unable to find explicit activity class " |
| 2197 |
+ ((Intent)intent).getComponent().toShortString() |
| 2198 |
+ "; have you declared this activity in your AndroidManifest.xml" |
| 2199 |
+ ", or does your intent not match its declared <intent-filter>?"); |
| 2200 |
throw new ActivityNotFoundException( |
| 2201 |
"No Activity found to handle " + intent); |
| 2202 |
case ActivityManager.START_PERMISSION_DENIED: |
| 2203 |
throw new SecurityException("Not allowed to start activity " |
| 2204 |
+ intent); |
| 2205 |
case ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT: |
| 2206 |
throw new AndroidRuntimeException( |
| 2207 |
"FORWARD_RESULT_FLAG used while also requesting a result"); |
| 2208 |
case ActivityManager.START_NOT_ACTIVITY: |
| 2209 |
throw new IllegalArgumentException( |
| 2210 |
"PendingIntent is not an activity"); |
| 2211 |
case ActivityManager.START_NOT_VOICE_COMPATIBLE: |
| 2212 |
throw new SecurityException( |
| 2213 |
"Starting under voice control not allowed for: " + intent); |
| 2214 |
case ActivityManager.START_VOICE_NOT_ACTIVE_SESSION: |
| 2215 |
throw new IllegalStateException( |
| 2216 |
"Session calling startVoiceActivity does not match active session"); |
| 2217 |
case ActivityManager.START_VOICE_HIDDEN_SESSION: |
| 2218 |
throw new IllegalStateException( |
| 2219 |
"Cannot start voice activity on a hidden session"); |
| 2220 |
case ActivityManager.START_ASSISTANT_NOT_ACTIVE_SESSION: |
| 2221 |
throw new IllegalStateException( |
| 2222 |
"Session calling startAssistantActivity does not match active session"); |
| 2223 |
case ActivityManager.START_ASSISTANT_HIDDEN_SESSION: |
| 2224 |
throw new IllegalStateException( |
| 2225 |
"Cannot start assistant activity on a hidden session"); |
| 2226 |
case ActivityManager.START_CANCELED: |
| 2227 |
throw new AndroidRuntimeException("Activity could not be started for " |
| 2228 |
+ intent); |
| 2229 |
default: |
| 2230 |
throw new AndroidRuntimeException("Unknown error code " |
| 2231 |
+ res + " when starting " + intent); |
| 2232 |
} |
| 2233 |
} |
| 2234 |
|
| 2235 |
private final void validateNotAppThread() { |
| 2236 |
if (Looper.myLooper() == Looper.getMainLooper()) { |
| 2237 |
throw new RuntimeException( |
| 2238 |
"This method can not be called from the main application thread"); |
| 2239 |
} |
| 2240 |
} |
| 2241 |
|
| 2242 |
|
| 2243 |
|
| 2244 |
|
| 2245 |
|
| 2246 |
|
| 2247 |
|
| 2248 |
|
| 2249 |
|
| 2250 |
|
| 2251 |
|
| 2252 |
|
| 2253 |
|
| 2254 |
|
| 2255 |
|
| 2256 |
|
| 2257 |
|
| 2258 |
|
| 2259 |
|
| 2260 |
|
| 2261 |
|
| 2262 |
|
| 2263 |
|
| 2264 |
|
| 2265 |
|
| 2266 |
|
| 2267 |
|
| 2268 |
|
| 2269 |
|
| 2270 |
|
| 2271 |
|
| 2272 |
public UiAutomation getUiAutomation() { |
| 2273 |
return getUiAutomation(0); |
| 2274 |
} |
| 2275 |
|
| 2276 |
|
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
|
| 2281 |
|
| 2282 |
|
| 2283 |
|
| 2284 |
|
| 2285 |
|
| 2286 |
|
| 2287 |
|
| 2288 |
|
| 2289 |
|
| 2290 |
|
| 2291 |
|
| 2292 |
|
| 2293 |
|
| 2294 |
|
| 2295 |
|
| 2296 |
|
| 2297 |
|
| 2298 |
|
| 2299 |
|
| 2300 |
|
| 2301 |
|
| 2302 |
|
| 2303 |
|
| 2304 |
|
| 2305 |
|
| 2306 |
|
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
public UiAutomation getUiAutomation(@UiAutomationFlags int flags) { |
| 2311 |
boolean mustCreateNewAutomation = (mUiAutomation == null) || (mUiAutomation.isDestroyed()); |
| 2312 |
|
| 2313 |
if (mUiAutomationConnection != null) { |
| 2314 |
if (!mustCreateNewAutomation && (mUiAutomation.getFlags() == flags)) { |
| 2315 |
return mUiAutomation; |
| 2316 |
} |
| 2317 |
if (mustCreateNewAutomation) { |
| 2318 |
mUiAutomation = new UiAutomation(getTargetContext().getMainLooper(), |
| 2319 |
mUiAutomationConnection); |
| 2320 |
} else { |
| 2321 |
mUiAutomation.disconnect(); |
| 2322 |
} |
| 2323 |
if (getTargetContext().getApplicationInfo().targetSdkVersion <= Build.VERSION_CODES.R) { |
| 2324 |
mUiAutomation.connect(flags); |
| 2325 |
return mUiAutomation; |
| 2326 |
} |
| 2327 |
try { |
| 2328 |
mUiAutomation.connectWithTimeout(flags, CONNECT_TIMEOUT_MILLIS); |
| 2329 |
return mUiAutomation; |
| 2330 |
} catch (TimeoutException e) { |
| 2331 |
mUiAutomation.destroy(); |
| 2332 |
mUiAutomation = null; |
| 2333 |
} |
| 2334 |
} |
| 2335 |
return null; |
| 2336 |
} |
| 2337 |
|
| 2338 |
|
| 2339 |
|
| 2340 |
|
| 2341 |
|
| 2342 |
public TestLooperManager acquireLooperManager(Looper looper) { |
| 2343 |
checkInstrumenting("acquireLooperManager"); |
| 2344 |
return new TestLooperManager(looper); |
| 2345 |
} |
| 2346 |
|
| 2347 |
private final class InstrumentationThread extends Thread { |
| 2348 |
public InstrumentationThread(String name) { |
| 2349 |
super(name); |
| 2350 |
} |
| 2351 |
public void run() { |
| 2352 |
try { |
| 2353 |
Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY); |
| 2354 |
} catch (RuntimeException e) { |
| 2355 |
Log.w(TAG, "Exception setting priority of instrumentation thread " |
| 2356 |
+ Process.myTid(), e); |
| 2357 |
} |
| 2358 |
if (mAutomaticPerformanceSnapshots) { |
| 2359 |
startPerformanceSnapshot(); |
| 2360 |
} |
| 2361 |
onStart(); |
| 2362 |
} |
| 2363 |
} |
| 2364 |
|
| 2365 |
private static final class EmptyRunnable implements Runnable { |
| 2366 |
public void run() { |
| 2367 |
} |
| 2368 |
} |
| 2369 |
|
| 2370 |
private static final class SyncRunnable implements Runnable { |
| 2371 |
private final Runnable mTarget; |
| 2372 |
private boolean mComplete; |
| 2373 |
|
| 2374 |
public SyncRunnable(Runnable target) { |
| 2375 |
mTarget = target; |
| 2376 |
} |
| 2377 |
|
| 2378 |
public void run() { |
| 2379 |
mTarget.run(); |
| 2380 |
synchronized (this) { |
| 2381 |
mComplete = true; |
| 2382 |
notifyAll(); |
| 2383 |
} |
| 2384 |
} |
| 2385 |
|
| 2386 |
public void waitForComplete() { |
| 2387 |
synchronized (this) { |
| 2388 |
while (!mComplete) { |
| 2389 |
try { |
| 2390 |
wait(); |
| 2391 |
} catch (InterruptedException e) { |
| 2392 |
} |
| 2393 |
} |
| 2394 |
} |
| 2395 |
} |
| 2396 |
} |
| 2397 |
|
| 2398 |
private static final class ActivityWaiter { |
| 2399 |
public final Intent intent; |
| 2400 |
public Activity activity; |
| 2401 |
|
| 2402 |
public ActivityWaiter(Intent _intent) { |
| 2403 |
intent = _intent; |
| 2404 |
} |
| 2405 |
} |
| 2406 |
|
| 2407 |
private final class ActivityGoing implements MessageQueue.IdleHandler { |
| 2408 |
private final ActivityWaiter mWaiter; |
| 2409 |
|
| 2410 |
public ActivityGoing(ActivityWaiter waiter) { |
| 2411 |
mWaiter = waiter; |
| 2412 |
} |
| 2413 |
|
| 2414 |
public final boolean queueIdle() { |
| 2415 |
synchronized (mSync) { |
| 2416 |
mWaitingActivities.remove(mWaiter); |
| 2417 |
mSync.notifyAll(); |
| 2418 |
} |
| 2419 |
return false; |
| 2420 |
} |
| 2421 |
} |
| 2422 |
|
| 2423 |
private static final class Idler implements MessageQueue.IdleHandler { |
| 2424 |
private final Runnable mCallback; |
| 2425 |
private boolean mIdle; |
| 2426 |
|
| 2427 |
public Idler(Runnable callback) { |
| 2428 |
mCallback = callback; |
| 2429 |
mIdle = false; |
| 2430 |
} |
| 2431 |
|
| 2432 |
public final boolean queueIdle() { |
| 2433 |
if (mCallback != null) { |
| 2434 |
mCallback.run(); |
| 2435 |
} |
| 2436 |
synchronized (this) { |
| 2437 |
mIdle = true; |
| 2438 |
notifyAll(); |
| 2439 |
} |
| 2440 |
return false; |
| 2441 |
} |
| 2442 |
|
| 2443 |
public void waitForIdle() { |
| 2444 |
synchronized (this) { |
| 2445 |
while (!mIdle) { |
| 2446 |
try { |
| 2447 |
wait(); |
| 2448 |
} catch (InterruptedException e) { |
| 2449 |
} |
| 2450 |
} |
| 2451 |
} |
| 2452 |
} |
| 2453 |
} |
| 2454 |
} |