ActivityStack.java revision be70785f9b2dbd2c52fd8676d99bc64b22784201
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.server.am; 18 19import com.android.internal.app.HeavyWeightSwitcherActivity; 20import com.android.internal.os.BatteryStatsImpl; 21import com.android.server.am.ActivityManagerService.PendingActivityLaunch; 22 23import android.app.Activity; 24import android.app.ActivityManager; 25import android.app.AppGlobals; 26import android.app.IActivityManager; 27import android.app.IThumbnailRetriever; 28import static android.app.IActivityManager.START_CLASS_NOT_FOUND; 29import static android.app.IActivityManager.START_DELIVERED_TO_TOP; 30import static android.app.IActivityManager.START_FORWARD_AND_REQUEST_CONFLICT; 31import static android.app.IActivityManager.START_INTENT_NOT_RESOLVED; 32import static android.app.IActivityManager.START_PERMISSION_DENIED; 33import static android.app.IActivityManager.START_RETURN_INTENT_TO_CALLER; 34import static android.app.IActivityManager.START_SUCCESS; 35import static android.app.IActivityManager.START_SWITCHES_CANCELED; 36import static android.app.IActivityManager.START_TASK_TO_FRONT; 37import android.app.IApplicationThread; 38import android.app.PendingIntent; 39import android.app.ResultInfo; 40import android.app.IActivityManager.WaitResult; 41import android.content.ComponentName; 42import android.content.Context; 43import android.content.IIntentSender; 44import android.content.Intent; 45import android.content.IntentSender; 46import android.content.pm.ActivityInfo; 47import android.content.pm.ApplicationInfo; 48import android.content.pm.PackageManager; 49import android.content.pm.ResolveInfo; 50import android.content.res.Configuration; 51import android.content.res.Resources; 52import android.graphics.Bitmap; 53import android.net.Uri; 54import android.os.Binder; 55import android.os.Bundle; 56import android.os.Handler; 57import android.os.IBinder; 58import android.os.Message; 59import android.os.ParcelFileDescriptor; 60import android.os.PowerManager; 61import android.os.RemoteException; 62import android.os.SystemClock; 63import android.util.EventLog; 64import android.util.Log; 65import android.util.Slog; 66import android.view.WindowManagerPolicy; 67 68import java.io.IOException; 69import java.lang.ref.WeakReference; 70import java.util.ArrayList; 71import java.util.Iterator; 72import java.util.List; 73 74/** 75 * State and management of a single stack of activities. 76 */ 77final class ActivityStack { 78 static final String TAG = ActivityManagerService.TAG; 79 static final boolean localLOGV = ActivityManagerService.localLOGV; 80 static final boolean DEBUG_SWITCH = ActivityManagerService.DEBUG_SWITCH; 81 static final boolean DEBUG_PAUSE = ActivityManagerService.DEBUG_PAUSE; 82 static final boolean DEBUG_VISBILITY = ActivityManagerService.DEBUG_VISBILITY; 83 static final boolean DEBUG_USER_LEAVING = ActivityManagerService.DEBUG_USER_LEAVING; 84 static final boolean DEBUG_TRANSITION = ActivityManagerService.DEBUG_TRANSITION; 85 static final boolean DEBUG_RESULTS = ActivityManagerService.DEBUG_RESULTS; 86 static final boolean DEBUG_CONFIGURATION = ActivityManagerService.DEBUG_CONFIGURATION; 87 static final boolean DEBUG_TASKS = ActivityManagerService.DEBUG_TASKS; 88 89 static final boolean DEBUG_STATES = false; 90 static final boolean DEBUG_ADD_REMOVE = false; 91 static final boolean DEBUG_SAVED_STATE = false; 92 93 static final boolean VALIDATE_TOKENS = ActivityManagerService.VALIDATE_TOKENS; 94 95 // How long we wait until giving up on the last activity telling us it 96 // is idle. 97 static final int IDLE_TIMEOUT = 10*1000; 98 99 // How long we wait until giving up on the last activity to pause. This 100 // is short because it directly impacts the responsiveness of starting the 101 // next activity. 102 static final int PAUSE_TIMEOUT = 500; 103 104 // How long we can hold the sleep wake lock before giving up. 105 static final int SLEEP_TIMEOUT = 5*1000; 106 107 // How long we can hold the launch wake lock before giving up. 108 static final int LAUNCH_TIMEOUT = 10*1000; 109 110 // How long we wait until giving up on an activity telling us it has 111 // finished destroying itself. 112 static final int DESTROY_TIMEOUT = 10*1000; 113 114 // How long until we reset a task when the user returns to it. Currently 115 // disabled. 116 static final long ACTIVITY_INACTIVE_RESET_TIME = 0; 117 118 // How long between activity launches that we consider safe to not warn 119 // the user about an unexpected activity being launched on top. 120 static final long START_WARN_TIME = 5*1000; 121 122 // Set to false to disable the preview that is shown while a new activity 123 // is being started. 124 static final boolean SHOW_APP_STARTING_PREVIEW = true; 125 126 enum ActivityState { 127 INITIALIZING, 128 RESUMED, 129 PAUSING, 130 PAUSED, 131 STOPPING, 132 STOPPED, 133 FINISHING, 134 DESTROYING, 135 DESTROYED 136 } 137 138 final ActivityManagerService mService; 139 final boolean mMainStack; 140 141 final Context mContext; 142 143 /** 144 * The back history of all previous (and possibly still 145 * running) activities. It contains HistoryRecord objects. 146 */ 147 final ArrayList<ActivityRecord> mHistory = new ArrayList<ActivityRecord>(); 148 149 /** 150 * Used for validating app tokens with window manager. 151 */ 152 final ArrayList<IBinder> mValidateAppTokens = new ArrayList<IBinder>(); 153 154 /** 155 * List of running activities, sorted by recent usage. 156 * The first entry in the list is the least recently used. 157 * It contains HistoryRecord objects. 158 */ 159 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>(); 160 161 /** 162 * List of activities that are waiting for a new activity 163 * to become visible before completing whatever operation they are 164 * supposed to do. 165 */ 166 final ArrayList<ActivityRecord> mWaitingVisibleActivities 167 = new ArrayList<ActivityRecord>(); 168 169 /** 170 * List of activities that are ready to be stopped, but waiting 171 * for the next activity to settle down before doing so. It contains 172 * HistoryRecord objects. 173 */ 174 final ArrayList<ActivityRecord> mStoppingActivities 175 = new ArrayList<ActivityRecord>(); 176 177 /** 178 * List of activities that are in the process of going to sleep. 179 */ 180 final ArrayList<ActivityRecord> mGoingToSleepActivities 181 = new ArrayList<ActivityRecord>(); 182 183 /** 184 * Animations that for the current transition have requested not to 185 * be considered for the transition animation. 186 */ 187 final ArrayList<ActivityRecord> mNoAnimActivities 188 = new ArrayList<ActivityRecord>(); 189 190 /** 191 * List of activities that are ready to be finished, but waiting 192 * for the previous activity to settle down before doing so. It contains 193 * HistoryRecord objects. 194 */ 195 final ArrayList<ActivityRecord> mFinishingActivities 196 = new ArrayList<ActivityRecord>(); 197 198 /** 199 * List of people waiting to find out about the next launched activity. 200 */ 201 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched 202 = new ArrayList<IActivityManager.WaitResult>(); 203 204 /** 205 * List of people waiting to find out about the next visible activity. 206 */ 207 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible 208 = new ArrayList<IActivityManager.WaitResult>(); 209 210 /** 211 * Set when the system is going to sleep, until we have 212 * successfully paused the current activity and released our wake lock. 213 * At that point the system is allowed to actually sleep. 214 */ 215 final PowerManager.WakeLock mGoingToSleep; 216 217 /** 218 * We don't want to allow the device to go to sleep while in the process 219 * of launching an activity. This is primarily to allow alarm intent 220 * receivers to launch an activity and get that to run before the device 221 * goes back to sleep. 222 */ 223 final PowerManager.WakeLock mLaunchingActivity; 224 225 /** 226 * When we are in the process of pausing an activity, before starting the 227 * next one, this variable holds the activity that is currently being paused. 228 */ 229 ActivityRecord mPausingActivity = null; 230 231 /** 232 * This is the last activity that we put into the paused state. This is 233 * used to determine if we need to do an activity transition while sleeping, 234 * when we normally hold the top activity paused. 235 */ 236 ActivityRecord mLastPausedActivity = null; 237 238 /** 239 * Current activity that is resumed, or null if there is none. 240 */ 241 ActivityRecord mResumedActivity = null; 242 243 /** 244 * This is the last activity that has been started. It is only used to 245 * identify when multiple activities are started at once so that the user 246 * can be warned they may not be in the activity they think they are. 247 */ 248 ActivityRecord mLastStartedActivity = null; 249 250 /** 251 * Set when we know we are going to be calling updateConfiguration() 252 * soon, so want to skip intermediate config checks. 253 */ 254 boolean mConfigWillChange; 255 256 /** 257 * Set to indicate whether to issue an onUserLeaving callback when a 258 * newly launched activity is being brought in front of us. 259 */ 260 boolean mUserLeaving = false; 261 262 long mInitialStartTime = 0; 263 264 /** 265 * Set when we have taken too long waiting to go to sleep. 266 */ 267 boolean mSleepTimeout = false; 268 269 /** 270 * Dismiss the keyguard after the next activity is displayed? 271 */ 272 boolean mDismissKeyguardOnNextActivity = false; 273 274 int mThumbnailWidth = -1; 275 int mThumbnailHeight = -1; 276 277 static final int SLEEP_TIMEOUT_MSG = 8; 278 static final int PAUSE_TIMEOUT_MSG = 9; 279 static final int IDLE_TIMEOUT_MSG = 10; 280 static final int IDLE_NOW_MSG = 11; 281 static final int LAUNCH_TIMEOUT_MSG = 16; 282 static final int DESTROY_TIMEOUT_MSG = 17; 283 static final int RESUME_TOP_ACTIVITY_MSG = 19; 284 285 final Handler mHandler = new Handler() { 286 //public Handler() { 287 // if (localLOGV) Slog.v(TAG, "Handler started!"); 288 //} 289 290 public void handleMessage(Message msg) { 291 switch (msg.what) { 292 case SLEEP_TIMEOUT_MSG: { 293 synchronized (mService) { 294 if (mService.isSleeping()) { 295 Slog.w(TAG, "Sleep timeout! Sleeping now."); 296 mSleepTimeout = true; 297 checkReadyForSleepLocked(); 298 } 299 } 300 } break; 301 case PAUSE_TIMEOUT_MSG: { 302 ActivityRecord r = (ActivityRecord)msg.obj; 303 // We don't at this point know if the activity is fullscreen, 304 // so we need to be conservative and assume it isn't. 305 Slog.w(TAG, "Activity pause timeout for " + r); 306 activityPaused(r != null ? r.appToken : null, true); 307 } break; 308 case IDLE_TIMEOUT_MSG: { 309 if (mService.mDidDexOpt) { 310 mService.mDidDexOpt = false; 311 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG); 312 nmsg.obj = msg.obj; 313 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT); 314 return; 315 } 316 // We don't at this point know if the activity is fullscreen, 317 // so we need to be conservative and assume it isn't. 318 ActivityRecord r = (ActivityRecord)msg.obj; 319 Slog.w(TAG, "Activity idle timeout for " + r); 320 activityIdleInternal(r != null ? r.appToken : null, true, null); 321 } break; 322 case DESTROY_TIMEOUT_MSG: { 323 ActivityRecord r = (ActivityRecord)msg.obj; 324 // We don't at this point know if the activity is fullscreen, 325 // so we need to be conservative and assume it isn't. 326 Slog.w(TAG, "Activity destroy timeout for " + r); 327 activityDestroyed(r != null ? r.appToken : null); 328 } break; 329 case IDLE_NOW_MSG: { 330 ActivityRecord r = (ActivityRecord)msg.obj; 331 activityIdleInternal(r != null ? r.appToken : null, false, null); 332 } break; 333 case LAUNCH_TIMEOUT_MSG: { 334 if (mService.mDidDexOpt) { 335 mService.mDidDexOpt = false; 336 Message nmsg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG); 337 mHandler.sendMessageDelayed(nmsg, LAUNCH_TIMEOUT); 338 return; 339 } 340 synchronized (mService) { 341 if (mLaunchingActivity.isHeld()) { 342 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!"); 343 mLaunchingActivity.release(); 344 } 345 } 346 } break; 347 case RESUME_TOP_ACTIVITY_MSG: { 348 synchronized (mService) { 349 resumeTopActivityLocked(null); 350 } 351 } break; 352 } 353 } 354 }; 355 356 ActivityStack(ActivityManagerService service, Context context, boolean mainStack) { 357 mService = service; 358 mContext = context; 359 mMainStack = mainStack; 360 PowerManager pm = 361 (PowerManager)context.getSystemService(Context.POWER_SERVICE); 362 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep"); 363 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch"); 364 mLaunchingActivity.setReferenceCounted(false); 365 } 366 367 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) { 368 int i = mHistory.size()-1; 369 while (i >= 0) { 370 ActivityRecord r = mHistory.get(i); 371 if (!r.finishing && r != notTop) { 372 return r; 373 } 374 i--; 375 } 376 return null; 377 } 378 379 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) { 380 int i = mHistory.size()-1; 381 while (i >= 0) { 382 ActivityRecord r = mHistory.get(i); 383 if (!r.finishing && !r.delayedResume && r != notTop) { 384 return r; 385 } 386 i--; 387 } 388 return null; 389 } 390 391 /** 392 * This is a simplified version of topRunningActivityLocked that provides a number of 393 * optional skip-over modes. It is intended for use with the ActivityController hook only. 394 * 395 * @param token If non-null, any history records matching this token will be skipped. 396 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID. 397 * 398 * @return Returns the HistoryRecord of the next activity on the stack. 399 */ 400 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) { 401 int i = mHistory.size()-1; 402 while (i >= 0) { 403 ActivityRecord r = mHistory.get(i); 404 // Note: the taskId check depends on real taskId fields being non-zero 405 if (!r.finishing && (token != r.appToken) && (taskId != r.task.taskId)) { 406 return r; 407 } 408 i--; 409 } 410 return null; 411 } 412 413 final int indexOfTokenLocked(IBinder token) { 414 return mHistory.indexOf(ActivityRecord.forToken(token)); 415 } 416 417 final int indexOfActivityLocked(ActivityRecord r) { 418 return mHistory.indexOf(r); 419 } 420 421 final ActivityRecord isInStackLocked(IBinder token) { 422 ActivityRecord r = ActivityRecord.forToken(token); 423 if (mHistory.contains(r)) { 424 return r; 425 } 426 return null; 427 } 428 429 private final boolean updateLRUListLocked(ActivityRecord r) { 430 final boolean hadit = mLRUActivities.remove(r); 431 mLRUActivities.add(r); 432 return hadit; 433 } 434 435 /** 436 * Returns the top activity in any existing task matching the given 437 * Intent. Returns null if no such task is found. 438 */ 439 private ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) { 440 ComponentName cls = intent.getComponent(); 441 if (info.targetActivity != null) { 442 cls = new ComponentName(info.packageName, info.targetActivity); 443 } 444 445 TaskRecord cp = null; 446 447 final int N = mHistory.size(); 448 for (int i=(N-1); i>=0; i--) { 449 ActivityRecord r = mHistory.get(i); 450 if (!r.finishing && r.task != cp 451 && r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE) { 452 cp = r.task; 453 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString() 454 // + "/aff=" + r.task.affinity + " to new cls=" 455 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity); 456 if (r.task.affinity != null) { 457 if (r.task.affinity.equals(info.taskAffinity)) { 458 //Slog.i(TAG, "Found matching affinity!"); 459 return r; 460 } 461 } else if (r.task.intent != null 462 && r.task.intent.getComponent().equals(cls)) { 463 //Slog.i(TAG, "Found matching class!"); 464 //dump(); 465 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent); 466 return r; 467 } else if (r.task.affinityIntent != null 468 && r.task.affinityIntent.getComponent().equals(cls)) { 469 //Slog.i(TAG, "Found matching class!"); 470 //dump(); 471 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent); 472 return r; 473 } 474 } 475 } 476 477 return null; 478 } 479 480 /** 481 * Returns the first activity (starting from the top of the stack) that 482 * is the same as the given activity. Returns null if no such activity 483 * is found. 484 */ 485 private ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) { 486 ComponentName cls = intent.getComponent(); 487 if (info.targetActivity != null) { 488 cls = new ComponentName(info.packageName, info.targetActivity); 489 } 490 491 final int N = mHistory.size(); 492 for (int i=(N-1); i>=0; i--) { 493 ActivityRecord r = mHistory.get(i); 494 if (!r.finishing) { 495 if (r.intent.getComponent().equals(cls)) { 496 //Slog.i(TAG, "Found matching class!"); 497 //dump(); 498 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent); 499 return r; 500 } 501 } 502 } 503 504 return null; 505 } 506 507 final void showAskCompatModeDialogLocked(ActivityRecord r) { 508 Message msg = Message.obtain(); 509 msg.what = ActivityManagerService.SHOW_COMPAT_MODE_DIALOG_MSG; 510 msg.obj = r.task.askedCompatMode ? null : r; 511 mService.mHandler.sendMessage(msg); 512 } 513 514 final boolean realStartActivityLocked(ActivityRecord r, 515 ProcessRecord app, boolean andResume, boolean checkConfig) 516 throws RemoteException { 517 518 r.startFreezingScreenLocked(app, 0); 519 mService.mWindowManager.setAppVisibility(r.appToken, true); 520 521 // Have the window manager re-evaluate the orientation of 522 // the screen based on the new activity order. Note that 523 // as a result of this, it can call back into the activity 524 // manager with a new orientation. We don't care about that, 525 // because the activity is not currently running so we are 526 // just restarting it anyway. 527 if (checkConfig) { 528 Configuration config = mService.mWindowManager.updateOrientationFromAppTokens( 529 mService.mConfiguration, 530 r.mayFreezeScreenLocked(app) ? r.appToken : null); 531 mService.updateConfigurationLocked(config, r, false); 532 } 533 534 r.app = app; 535 app.waitingToKill = null; 536 537 if (localLOGV) Slog.v(TAG, "Launching: " + r); 538 539 int idx = app.activities.indexOf(r); 540 if (idx < 0) { 541 app.activities.add(r); 542 } 543 mService.updateLruProcessLocked(app, true, true); 544 545 try { 546 if (app.thread == null) { 547 throw new RemoteException(); 548 } 549 List<ResultInfo> results = null; 550 List<Intent> newIntents = null; 551 if (andResume) { 552 results = r.results; 553 newIntents = r.newIntents; 554 } 555 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r 556 + " icicle=" + r.icicle 557 + " with results=" + results + " newIntents=" + newIntents 558 + " andResume=" + andResume); 559 if (andResume) { 560 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY, 561 System.identityHashCode(r), 562 r.task.taskId, r.shortComponentName); 563 } 564 if (r.isHomeActivity) { 565 mService.mHomeProcess = app; 566 } 567 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName()); 568 r.sleeping = false; 569 r.forceNewConfig = false; 570 showAskCompatModeDialogLocked(r); 571 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo); 572 String profileFile = null; 573 ParcelFileDescriptor profileFd = null; 574 boolean profileAutoStop = false; 575 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) { 576 if (mService.mProfileProc == null || mService.mProfileProc == app) { 577 mService.mProfileProc = app; 578 profileFile = mService.mProfileFile; 579 profileFd = mService.mProfileFd; 580 profileAutoStop = mService.mAutoStopProfiler; 581 } 582 } 583 app.hasShownUi = true; 584 app.pendingUiClean = true; 585 if (profileFd != null) { 586 try { 587 profileFd = profileFd.dup(); 588 } catch (IOException e) { 589 profileFd = null; 590 } 591 } 592 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken, 593 System.identityHashCode(r), r.info, mService.mConfiguration, 594 r.compat, r.icicle, results, newIntents, !andResume, 595 mService.isNextTransitionForward(), profileFile, profileFd, 596 profileAutoStop); 597 598 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) { 599 // This may be a heavy-weight process! Note that the package 600 // manager will ensure that only activity can run in the main 601 // process of the .apk, which is the only thing that will be 602 // considered heavy-weight. 603 if (app.processName.equals(app.info.packageName)) { 604 if (mService.mHeavyWeightProcess != null 605 && mService.mHeavyWeightProcess != app) { 606 Log.w(TAG, "Starting new heavy weight process " + app 607 + " when already running " 608 + mService.mHeavyWeightProcess); 609 } 610 mService.mHeavyWeightProcess = app; 611 Message msg = mService.mHandler.obtainMessage( 612 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG); 613 msg.obj = r; 614 mService.mHandler.sendMessage(msg); 615 } 616 } 617 618 } catch (RemoteException e) { 619 if (r.launchFailed) { 620 // This is the second time we failed -- finish activity 621 // and give up. 622 Slog.e(TAG, "Second failure launching " 623 + r.intent.getComponent().flattenToShortString() 624 + ", giving up", e); 625 mService.appDiedLocked(app, app.pid, app.thread); 626 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null, 627 "2nd-crash"); 628 return false; 629 } 630 631 // This is the first time we failed -- restart process and 632 // retry. 633 app.activities.remove(r); 634 throw e; 635 } 636 637 r.launchFailed = false; 638 if (updateLRUListLocked(r)) { 639 Slog.w(TAG, "Activity " + r 640 + " being launched, but already in LRU list"); 641 } 642 643 if (andResume) { 644 // As part of the process of launching, ActivityThread also performs 645 // a resume. 646 r.state = ActivityState.RESUMED; 647 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r 648 + " (starting new instance)"); 649 r.stopped = false; 650 mResumedActivity = r; 651 r.task.touchActiveTime(); 652 if (mMainStack) { 653 mService.addRecentTaskLocked(r.task); 654 } 655 completeResumeLocked(r); 656 checkReadyForSleepLocked(); 657 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle); 658 r.icicle = null; 659 r.haveState = false; 660 } else { 661 // This activity is not starting in the resumed state... which 662 // should look like we asked it to pause+stop (but remain visible), 663 // and it has done so and reported back the current icicle and 664 // other state. 665 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r 666 + " (starting in stopped state)"); 667 r.state = ActivityState.STOPPED; 668 r.stopped = true; 669 } 670 671 // Launch the new version setup screen if needed. We do this -after- 672 // launching the initial activity (that is, home), so that it can have 673 // a chance to initialize itself while in the background, making the 674 // switch back to it faster and look better. 675 if (mMainStack) { 676 mService.startSetupActivityLocked(); 677 } 678 679 return true; 680 } 681 682 private final void startSpecificActivityLocked(ActivityRecord r, 683 boolean andResume, boolean checkConfig) { 684 // Is this activity's application already running? 685 ProcessRecord app = mService.getProcessRecordLocked(r.processName, 686 r.info.applicationInfo.uid); 687 688 if (r.launchTime == 0) { 689 r.launchTime = SystemClock.uptimeMillis(); 690 if (mInitialStartTime == 0) { 691 mInitialStartTime = r.launchTime; 692 } 693 } else if (mInitialStartTime == 0) { 694 mInitialStartTime = SystemClock.uptimeMillis(); 695 } 696 697 if (app != null && app.thread != null) { 698 try { 699 app.addPackage(r.info.packageName); 700 realStartActivityLocked(r, app, andResume, checkConfig); 701 return; 702 } catch (RemoteException e) { 703 Slog.w(TAG, "Exception when starting activity " 704 + r.intent.getComponent().flattenToShortString(), e); 705 } 706 707 // If a dead object exception was thrown -- fall through to 708 // restart the application. 709 } 710 711 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0, 712 "activity", r.intent.getComponent(), false); 713 } 714 715 void stopIfSleepingLocked() { 716 if (mService.isSleeping()) { 717 if (!mGoingToSleep.isHeld()) { 718 mGoingToSleep.acquire(); 719 if (mLaunchingActivity.isHeld()) { 720 mLaunchingActivity.release(); 721 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG); 722 } 723 } 724 mHandler.removeMessages(SLEEP_TIMEOUT_MSG); 725 Message msg = mHandler.obtainMessage(SLEEP_TIMEOUT_MSG); 726 mHandler.sendMessageDelayed(msg, SLEEP_TIMEOUT); 727 checkReadyForSleepLocked(); 728 } 729 } 730 731 void awakeFromSleepingLocked() { 732 mHandler.removeMessages(SLEEP_TIMEOUT_MSG); 733 mSleepTimeout = false; 734 if (mGoingToSleep.isHeld()) { 735 mGoingToSleep.release(); 736 } 737 // Ensure activities are no longer sleeping. 738 for (int i=mHistory.size()-1; i>=0; i--) { 739 ActivityRecord r = mHistory.get(i); 740 r.setSleeping(false); 741 } 742 mGoingToSleepActivities.clear(); 743 } 744 745 void activitySleptLocked(ActivityRecord r) { 746 mGoingToSleepActivities.remove(r); 747 checkReadyForSleepLocked(); 748 } 749 750 void checkReadyForSleepLocked() { 751 if (!mService.isSleeping()) { 752 // Do not care. 753 return; 754 } 755 756 if (!mSleepTimeout) { 757 if (mResumedActivity != null) { 758 // Still have something resumed; can't sleep until it is paused. 759 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity); 760 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false"); 761 startPausingLocked(false, true); 762 return; 763 } 764 if (mPausingActivity != null) { 765 // Still waiting for something to pause; can't sleep yet. 766 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity); 767 return; 768 } 769 770 if (mStoppingActivities.size() > 0) { 771 // Still need to tell some activities to stop; can't sleep yet. 772 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop " 773 + mStoppingActivities.size() + " activities"); 774 scheduleIdleLocked(); 775 return; 776 } 777 778 ensureActivitiesVisibleLocked(null, 0); 779 780 // Make sure any stopped but visible activities are now sleeping. 781 // This ensures that the activity's onStop() is called. 782 for (int i=mHistory.size()-1; i>=0; i--) { 783 ActivityRecord r = mHistory.get(i); 784 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) { 785 r.setSleeping(true); 786 } 787 } 788 789 if (mGoingToSleepActivities.size() > 0) { 790 // Still need to tell some activities to sleep; can't sleep yet. 791 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep " 792 + mGoingToSleepActivities.size() + " activities"); 793 return; 794 } 795 } 796 797 mHandler.removeMessages(SLEEP_TIMEOUT_MSG); 798 799 if (mGoingToSleep.isHeld()) { 800 mGoingToSleep.release(); 801 } 802 if (mService.mShuttingDown) { 803 mService.notifyAll(); 804 } 805 } 806 807 public final Bitmap screenshotActivities(ActivityRecord who) { 808 if (who.noDisplay) { 809 return null; 810 } 811 812 Resources res = mService.mContext.getResources(); 813 int w = mThumbnailWidth; 814 int h = mThumbnailHeight; 815 if (w < 0) { 816 mThumbnailWidth = w = 817 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width); 818 mThumbnailHeight = h = 819 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height); 820 } 821 822 if (w > 0) { 823 return mService.mWindowManager.screenshotApplications(who.appToken, w, h); 824 } 825 return null; 826 } 827 828 private final void startPausingLocked(boolean userLeaving, boolean uiSleeping) { 829 if (mPausingActivity != null) { 830 RuntimeException e = new RuntimeException(); 831 Slog.e(TAG, "Trying to pause when pause is already pending for " 832 + mPausingActivity, e); 833 } 834 ActivityRecord prev = mResumedActivity; 835 if (prev == null) { 836 RuntimeException e = new RuntimeException(); 837 Slog.e(TAG, "Trying to pause when nothing is resumed", e); 838 resumeTopActivityLocked(null); 839 return; 840 } 841 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev); 842 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev); 843 mResumedActivity = null; 844 mPausingActivity = prev; 845 mLastPausedActivity = prev; 846 prev.state = ActivityState.PAUSING; 847 prev.task.touchActiveTime(); 848 prev.updateThumbnail(screenshotActivities(prev), null); 849 850 mService.updateCpuStats(); 851 852 if (prev.app != null && prev.app.thread != null) { 853 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev); 854 try { 855 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY, 856 System.identityHashCode(prev), 857 prev.shortComponentName); 858 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing, 859 userLeaving, prev.configChangeFlags); 860 if (mMainStack) { 861 mService.updateUsageStats(prev, false); 862 } 863 } catch (Exception e) { 864 // Ignore exception, if process died other code will cleanup. 865 Slog.w(TAG, "Exception thrown during pause", e); 866 mPausingActivity = null; 867 mLastPausedActivity = null; 868 } 869 } else { 870 mPausingActivity = null; 871 mLastPausedActivity = null; 872 } 873 874 // If we are not going to sleep, we want to ensure the device is 875 // awake until the next activity is started. 876 if (!mService.mSleeping && !mService.mShuttingDown) { 877 mLaunchingActivity.acquire(); 878 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) { 879 // To be safe, don't allow the wake lock to be held for too long. 880 Message msg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG); 881 mHandler.sendMessageDelayed(msg, LAUNCH_TIMEOUT); 882 } 883 } 884 885 886 if (mPausingActivity != null) { 887 // Have the window manager pause its key dispatching until the new 888 // activity has started. If we're pausing the activity just because 889 // the screen is being turned off and the UI is sleeping, don't interrupt 890 // key dispatch; the same activity will pick it up again on wakeup. 891 if (!uiSleeping) { 892 prev.pauseKeyDispatchingLocked(); 893 } else { 894 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off"); 895 } 896 897 // Schedule a pause timeout in case the app doesn't respond. 898 // We don't give it much time because this directly impacts the 899 // responsiveness seen by the user. 900 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG); 901 msg.obj = prev; 902 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT); 903 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete..."); 904 } else { 905 // This activity failed to schedule the 906 // pause, so just treat it as being paused now. 907 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next."); 908 resumeTopActivityLocked(null); 909 } 910 } 911 912 final void activityPaused(IBinder token, boolean timeout) { 913 if (DEBUG_PAUSE) Slog.v( 914 TAG, "Activity paused: token=" + token + ", timeout=" + timeout); 915 916 ActivityRecord r = null; 917 918 synchronized (mService) { 919 int index = indexOfTokenLocked(token); 920 if (index >= 0) { 921 r = mHistory.get(index); 922 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r); 923 if (mPausingActivity == r) { 924 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r 925 + (timeout ? " (due to timeout)" : " (pause complete)")); 926 r.state = ActivityState.PAUSED; 927 completePauseLocked(); 928 } else { 929 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE, 930 System.identityHashCode(r), r.shortComponentName, 931 mPausingActivity != null 932 ? mPausingActivity.shortComponentName : "(none)"); 933 } 934 } 935 } 936 } 937 938 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail, 939 CharSequence description) { 940 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle); 941 r.icicle = icicle; 942 r.haveState = true; 943 r.updateThumbnail(thumbnail, description); 944 r.stopped = true; 945 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)"); 946 r.state = ActivityState.STOPPED; 947 if (!r.finishing) { 948 if (r.configDestroy) { 949 destroyActivityLocked(r, true, false, "stop-config"); 950 resumeTopActivityLocked(null); 951 } 952 } 953 } 954 955 private final void completePauseLocked() { 956 ActivityRecord prev = mPausingActivity; 957 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev); 958 959 if (prev != null) { 960 if (prev.finishing) { 961 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev); 962 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE); 963 } else if (prev.app != null) { 964 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev); 965 if (prev.waitingVisible) { 966 prev.waitingVisible = false; 967 mWaitingVisibleActivities.remove(prev); 968 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v( 969 TAG, "Complete pause, no longer waiting: " + prev); 970 } 971 if (prev.configDestroy) { 972 // The previous is being paused because the configuration 973 // is changing, which means it is actually stopping... 974 // To juggle the fact that we are also starting a new 975 // instance right now, we need to first completely stop 976 // the current instance before starting the new one. 977 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev); 978 destroyActivityLocked(prev, true, false, "pause-config"); 979 } else { 980 mStoppingActivities.add(prev); 981 if (mStoppingActivities.size() > 3) { 982 // If we already have a few activities waiting to stop, 983 // then give up on things going idle and start clearing 984 // them out. 985 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle"); 986 scheduleIdleLocked(); 987 } else { 988 checkReadyForSleepLocked(); 989 } 990 } 991 } else { 992 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev); 993 prev = null; 994 } 995 mPausingActivity = null; 996 } 997 998 if (!mService.isSleeping()) { 999 resumeTopActivityLocked(prev); 1000 } else { 1001 checkReadyForSleepLocked(); 1002 } 1003 1004 if (prev != null) { 1005 prev.resumeKeyDispatchingLocked(); 1006 } 1007 1008 if (prev.app != null && prev.cpuTimeAtResume > 0 1009 && mService.mBatteryStatsService.isOnBattery()) { 1010 long diff = 0; 1011 synchronized (mService.mProcessStatsThread) { 1012 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid) 1013 - prev.cpuTimeAtResume; 1014 } 1015 if (diff > 0) { 1016 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics(); 1017 synchronized (bsi) { 1018 BatteryStatsImpl.Uid.Proc ps = 1019 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid, 1020 prev.info.packageName); 1021 if (ps != null) { 1022 ps.addForegroundTimeLocked(diff); 1023 } 1024 } 1025 } 1026 } 1027 prev.cpuTimeAtResume = 0; // reset it 1028 } 1029 1030 /** 1031 * Once we know that we have asked an application to put an activity in 1032 * the resumed state (either by launching it or explicitly telling it), 1033 * this function updates the rest of our state to match that fact. 1034 */ 1035 private final void completeResumeLocked(ActivityRecord next) { 1036 next.idle = false; 1037 next.results = null; 1038 next.newIntents = null; 1039 1040 // schedule an idle timeout in case the app doesn't do it for us. 1041 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG); 1042 msg.obj = next; 1043 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT); 1044 1045 if (false) { 1046 // The activity was never told to pause, so just keep 1047 // things going as-is. To maintain our own state, 1048 // we need to emulate it coming back and saying it is 1049 // idle. 1050 msg = mHandler.obtainMessage(IDLE_NOW_MSG); 1051 msg.obj = next; 1052 mHandler.sendMessage(msg); 1053 } 1054 1055 if (mMainStack) { 1056 mService.reportResumedActivityLocked(next); 1057 } 1058 1059 next.clearThumbnail(); 1060 if (mMainStack) { 1061 mService.setFocusedActivityLocked(next); 1062 } 1063 next.resumeKeyDispatchingLocked(); 1064 ensureActivitiesVisibleLocked(null, 0); 1065 mService.mWindowManager.executeAppTransition(); 1066 mNoAnimActivities.clear(); 1067 1068 // Mark the point when the activity is resuming 1069 // TODO: To be more accurate, the mark should be before the onCreate, 1070 // not after the onResume. But for subsequent starts, onResume is fine. 1071 if (next.app != null) { 1072 synchronized (mService.mProcessStatsThread) { 1073 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid); 1074 } 1075 } else { 1076 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process 1077 } 1078 } 1079 1080 /** 1081 * Make sure that all activities that need to be visible (that is, they 1082 * currently can be seen by the user) actually are. 1083 */ 1084 final void ensureActivitiesVisibleLocked(ActivityRecord top, 1085 ActivityRecord starting, String onlyThisProcess, int configChanges) { 1086 if (DEBUG_VISBILITY) Slog.v( 1087 TAG, "ensureActivitiesVisible behind " + top 1088 + " configChanges=0x" + Integer.toHexString(configChanges)); 1089 1090 // If the top activity is not fullscreen, then we need to 1091 // make sure any activities under it are now visible. 1092 final int count = mHistory.size(); 1093 int i = count-1; 1094 while (mHistory.get(i) != top) { 1095 i--; 1096 } 1097 ActivityRecord r; 1098 boolean behindFullscreen = false; 1099 for (; i>=0; i--) { 1100 r = mHistory.get(i); 1101 if (DEBUG_VISBILITY) Slog.v( 1102 TAG, "Make visible? " + r + " finishing=" + r.finishing 1103 + " state=" + r.state); 1104 if (r.finishing) { 1105 continue; 1106 } 1107 1108 final boolean doThisProcess = onlyThisProcess == null 1109 || onlyThisProcess.equals(r.processName); 1110 1111 // First: if this is not the current activity being started, make 1112 // sure it matches the current configuration. 1113 if (r != starting && doThisProcess) { 1114 ensureActivityConfigurationLocked(r, 0); 1115 } 1116 1117 if (r.app == null || r.app.thread == null) { 1118 if (onlyThisProcess == null 1119 || onlyThisProcess.equals(r.processName)) { 1120 // This activity needs to be visible, but isn't even 1121 // running... get it started, but don't resume it 1122 // at this point. 1123 if (DEBUG_VISBILITY) Slog.v( 1124 TAG, "Start and freeze screen for " + r); 1125 if (r != starting) { 1126 r.startFreezingScreenLocked(r.app, configChanges); 1127 } 1128 if (!r.visible) { 1129 if (DEBUG_VISBILITY) Slog.v( 1130 TAG, "Starting and making visible: " + r); 1131 mService.mWindowManager.setAppVisibility(r.appToken, true); 1132 } 1133 if (r != starting) { 1134 startSpecificActivityLocked(r, false, false); 1135 } 1136 } 1137 1138 } else if (r.visible) { 1139 // If this activity is already visible, then there is nothing 1140 // else to do here. 1141 if (DEBUG_VISBILITY) Slog.v( 1142 TAG, "Skipping: already visible at " + r); 1143 r.stopFreezingScreenLocked(false); 1144 1145 } else if (onlyThisProcess == null) { 1146 // This activity is not currently visible, but is running. 1147 // Tell it to become visible. 1148 r.visible = true; 1149 if (r.state != ActivityState.RESUMED && r != starting) { 1150 // If this activity is paused, tell it 1151 // to now show its window. 1152 if (DEBUG_VISBILITY) Slog.v( 1153 TAG, "Making visible and scheduling visibility: " + r); 1154 try { 1155 mService.mWindowManager.setAppVisibility(r.appToken, true); 1156 r.sleeping = false; 1157 r.app.pendingUiClean = true; 1158 r.app.thread.scheduleWindowVisibility(r.appToken, true); 1159 r.stopFreezingScreenLocked(false); 1160 } catch (Exception e) { 1161 // Just skip on any failure; we'll make it 1162 // visible when it next restarts. 1163 Slog.w(TAG, "Exception thrown making visibile: " 1164 + r.intent.getComponent(), e); 1165 } 1166 } 1167 } 1168 1169 // Aggregate current change flags. 1170 configChanges |= r.configChangeFlags; 1171 1172 if (r.fullscreen) { 1173 // At this point, nothing else needs to be shown 1174 if (DEBUG_VISBILITY) Slog.v( 1175 TAG, "Stopping: fullscreen at " + r); 1176 behindFullscreen = true; 1177 i--; 1178 break; 1179 } 1180 } 1181 1182 // Now for any activities that aren't visible to the user, make 1183 // sure they no longer are keeping the screen frozen. 1184 while (i >= 0) { 1185 r = mHistory.get(i); 1186 if (DEBUG_VISBILITY) Slog.v( 1187 TAG, "Make invisible? " + r + " finishing=" + r.finishing 1188 + " state=" + r.state 1189 + " behindFullscreen=" + behindFullscreen); 1190 if (!r.finishing) { 1191 if (behindFullscreen) { 1192 if (r.visible) { 1193 if (DEBUG_VISBILITY) Slog.v( 1194 TAG, "Making invisible: " + r); 1195 r.visible = false; 1196 try { 1197 mService.mWindowManager.setAppVisibility(r.appToken, false); 1198 if ((r.state == ActivityState.STOPPING 1199 || r.state == ActivityState.STOPPED) 1200 && r.app != null && r.app.thread != null) { 1201 if (DEBUG_VISBILITY) Slog.v( 1202 TAG, "Scheduling invisibility: " + r); 1203 r.app.thread.scheduleWindowVisibility(r.appToken, false); 1204 } 1205 } catch (Exception e) { 1206 // Just skip on any failure; we'll make it 1207 // visible when it next restarts. 1208 Slog.w(TAG, "Exception thrown making hidden: " 1209 + r.intent.getComponent(), e); 1210 } 1211 } else { 1212 if (DEBUG_VISBILITY) Slog.v( 1213 TAG, "Already invisible: " + r); 1214 } 1215 } else if (r.fullscreen) { 1216 if (DEBUG_VISBILITY) Slog.v( 1217 TAG, "Now behindFullscreen: " + r); 1218 behindFullscreen = true; 1219 } 1220 } 1221 i--; 1222 } 1223 } 1224 1225 /** 1226 * Version of ensureActivitiesVisible that can easily be called anywhere. 1227 */ 1228 final void ensureActivitiesVisibleLocked(ActivityRecord starting, 1229 int configChanges) { 1230 ActivityRecord r = topRunningActivityLocked(null); 1231 if (r != null) { 1232 ensureActivitiesVisibleLocked(r, starting, null, configChanges); 1233 } 1234 } 1235 1236 /** 1237 * Ensure that the top activity in the stack is resumed. 1238 * 1239 * @param prev The previously resumed activity, for when in the process 1240 * of pausing; can be null to call from elsewhere. 1241 * 1242 * @return Returns true if something is being resumed, or false if 1243 * nothing happened. 1244 */ 1245 final boolean resumeTopActivityLocked(ActivityRecord prev) { 1246 // Find the first activity that is not finishing. 1247 ActivityRecord next = topRunningActivityLocked(null); 1248 1249 // Remember how we'll process this pause/resume situation, and ensure 1250 // that the state is reset however we wind up proceeding. 1251 final boolean userLeaving = mUserLeaving; 1252 mUserLeaving = false; 1253 1254 if (next == null) { 1255 // There are no more activities! Let's just start up the 1256 // Launcher... 1257 if (mMainStack) { 1258 return mService.startHomeActivityLocked(); 1259 } 1260 } 1261 1262 next.delayedResume = false; 1263 1264 // If the top activity is the resumed one, nothing to do. 1265 if (mResumedActivity == next && next.state == ActivityState.RESUMED) { 1266 // Make sure we have executed any pending transitions, since there 1267 // should be nothing left to do at this point. 1268 mService.mWindowManager.executeAppTransition(); 1269 mNoAnimActivities.clear(); 1270 return false; 1271 } 1272 1273 // If we are sleeping, and there is no resumed activity, and the top 1274 // activity is paused, well that is the state we want. 1275 if ((mService.mSleeping || mService.mShuttingDown) 1276 && mLastPausedActivity == next && next.state == ActivityState.PAUSED) { 1277 // Make sure we have executed any pending transitions, since there 1278 // should be nothing left to do at this point. 1279 mService.mWindowManager.executeAppTransition(); 1280 mNoAnimActivities.clear(); 1281 return false; 1282 } 1283 1284 // The activity may be waiting for stop, but that is no longer 1285 // appropriate for it. 1286 mStoppingActivities.remove(next); 1287 mGoingToSleepActivities.remove(next); 1288 next.sleeping = false; 1289 mWaitingVisibleActivities.remove(next); 1290 1291 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next); 1292 1293 // If we are currently pausing an activity, then don't do anything 1294 // until that is done. 1295 if (mPausingActivity != null) { 1296 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: pausing=" + mPausingActivity); 1297 return false; 1298 } 1299 1300 // Okay we are now going to start a switch, to 'next'. We may first 1301 // have to pause the current activity, but this is an important point 1302 // where we have decided to go to 'next' so keep track of that. 1303 // XXX "App Redirected" dialog is getting too many false positives 1304 // at this point, so turn off for now. 1305 if (false) { 1306 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) { 1307 long now = SystemClock.uptimeMillis(); 1308 final boolean inTime = mLastStartedActivity.startTime != 0 1309 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now; 1310 final int lastUid = mLastStartedActivity.info.applicationInfo.uid; 1311 final int nextUid = next.info.applicationInfo.uid; 1312 if (inTime && lastUid != nextUid 1313 && lastUid != next.launchedFromUid 1314 && mService.checkPermission( 1315 android.Manifest.permission.STOP_APP_SWITCHES, 1316 -1, next.launchedFromUid) 1317 != PackageManager.PERMISSION_GRANTED) { 1318 mService.showLaunchWarningLocked(mLastStartedActivity, next); 1319 } else { 1320 next.startTime = now; 1321 mLastStartedActivity = next; 1322 } 1323 } else { 1324 next.startTime = SystemClock.uptimeMillis(); 1325 mLastStartedActivity = next; 1326 } 1327 } 1328 1329 // We need to start pausing the current activity so the top one 1330 // can be resumed... 1331 if (mResumedActivity != null) { 1332 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing"); 1333 startPausingLocked(userLeaving, false); 1334 return true; 1335 } 1336 1337 if (prev != null && prev != next) { 1338 if (!prev.waitingVisible && next != null && !next.nowVisible) { 1339 prev.waitingVisible = true; 1340 mWaitingVisibleActivities.add(prev); 1341 if (DEBUG_SWITCH) Slog.v( 1342 TAG, "Resuming top, waiting visible to hide: " + prev); 1343 } else { 1344 // The next activity is already visible, so hide the previous 1345 // activity's windows right now so we can show the new one ASAP. 1346 // We only do this if the previous is finishing, which should mean 1347 // it is on top of the one being resumed so hiding it quickly 1348 // is good. Otherwise, we want to do the normal route of allowing 1349 // the resumed activity to be shown so we can decide if the 1350 // previous should actually be hidden depending on whether the 1351 // new one is found to be full-screen or not. 1352 if (prev.finishing) { 1353 mService.mWindowManager.setAppVisibility(prev.appToken, false); 1354 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: " 1355 + prev + ", waitingVisible=" 1356 + (prev != null ? prev.waitingVisible : null) 1357 + ", nowVisible=" + next.nowVisible); 1358 } else { 1359 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: " 1360 + prev + ", waitingVisible=" 1361 + (prev != null ? prev.waitingVisible : null) 1362 + ", nowVisible=" + next.nowVisible); 1363 } 1364 } 1365 1366 if (!prev.finishing && prev.app != null && prev.app != next.app 1367 && prev.app != mService.mHomeProcess) { 1368 // We are switching to a new activity that is in a different 1369 // process than the previous one. Note the previous process, 1370 // so we can try to keep it around. 1371 mService.mPreviousProcess = prev.app; 1372 } 1373 } 1374 1375 // Launching this app's activity, make sure the app is no longer 1376 // considered stopped. 1377 try { 1378 AppGlobals.getPackageManager().setPackageStoppedState( 1379 next.packageName, false); 1380 } catch (RemoteException e1) { 1381 } catch (IllegalArgumentException e) { 1382 Slog.w(TAG, "Failed trying to unstop package " 1383 + next.packageName + ": " + e); 1384 } 1385 1386 // We are starting up the next activity, so tell the window manager 1387 // that the previous one will be hidden soon. This way it can know 1388 // to ignore it when computing the desired screen orientation. 1389 if (prev != null) { 1390 if (prev.finishing) { 1391 if (DEBUG_TRANSITION) Slog.v(TAG, 1392 "Prepare close transition: prev=" + prev); 1393 if (mNoAnimActivities.contains(prev)) { 1394 mService.mWindowManager.prepareAppTransition( 1395 WindowManagerPolicy.TRANSIT_NONE, false); 1396 } else { 1397 mService.mWindowManager.prepareAppTransition(prev.task == next.task 1398 ? WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE 1399 : WindowManagerPolicy.TRANSIT_TASK_CLOSE, false); 1400 } 1401 mService.mWindowManager.setAppWillBeHidden(prev.appToken); 1402 mService.mWindowManager.setAppVisibility(prev.appToken, false); 1403 } else { 1404 if (DEBUG_TRANSITION) Slog.v(TAG, 1405 "Prepare open transition: prev=" + prev); 1406 if (mNoAnimActivities.contains(next)) { 1407 mService.mWindowManager.prepareAppTransition( 1408 WindowManagerPolicy.TRANSIT_NONE, false); 1409 } else { 1410 mService.mWindowManager.prepareAppTransition(prev.task == next.task 1411 ? WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN 1412 : WindowManagerPolicy.TRANSIT_TASK_OPEN, false); 1413 } 1414 } 1415 if (false) { 1416 mService.mWindowManager.setAppWillBeHidden(prev.appToken); 1417 mService.mWindowManager.setAppVisibility(prev.appToken, false); 1418 } 1419 } else if (mHistory.size() > 1) { 1420 if (DEBUG_TRANSITION) Slog.v(TAG, 1421 "Prepare open transition: no previous"); 1422 if (mNoAnimActivities.contains(next)) { 1423 mService.mWindowManager.prepareAppTransition( 1424 WindowManagerPolicy.TRANSIT_NONE, false); 1425 } else { 1426 mService.mWindowManager.prepareAppTransition( 1427 WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN, false); 1428 } 1429 } 1430 1431 if (next.app != null && next.app.thread != null) { 1432 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next); 1433 1434 // This activity is now becoming visible. 1435 mService.mWindowManager.setAppVisibility(next.appToken, true); 1436 1437 ActivityRecord lastResumedActivity = mResumedActivity; 1438 ActivityState lastState = next.state; 1439 1440 mService.updateCpuStats(); 1441 1442 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)"); 1443 next.state = ActivityState.RESUMED; 1444 mResumedActivity = next; 1445 next.task.touchActiveTime(); 1446 if (mMainStack) { 1447 mService.addRecentTaskLocked(next.task); 1448 } 1449 mService.updateLruProcessLocked(next.app, true, true); 1450 updateLRUListLocked(next); 1451 1452 // Have the window manager re-evaluate the orientation of 1453 // the screen based on the new activity order. 1454 boolean updated = false; 1455 if (mMainStack) { 1456 synchronized (mService) { 1457 Configuration config = mService.mWindowManager.updateOrientationFromAppTokens( 1458 mService.mConfiguration, 1459 next.mayFreezeScreenLocked(next.app) ? next.appToken : null); 1460 if (config != null) { 1461 next.frozenBeforeDestroy = true; 1462 } 1463 updated = mService.updateConfigurationLocked(config, next, false); 1464 } 1465 } 1466 if (!updated) { 1467 // The configuration update wasn't able to keep the existing 1468 // instance of the activity, and instead started a new one. 1469 // We should be all done, but let's just make sure our activity 1470 // is still at the top and schedule another run if something 1471 // weird happened. 1472 ActivityRecord nextNext = topRunningActivityLocked(null); 1473 if (DEBUG_SWITCH) Slog.i(TAG, 1474 "Activity config changed during resume: " + next 1475 + ", new next: " + nextNext); 1476 if (nextNext != next) { 1477 // Do over! 1478 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG); 1479 } 1480 if (mMainStack) { 1481 mService.setFocusedActivityLocked(next); 1482 } 1483 ensureActivitiesVisibleLocked(null, 0); 1484 mService.mWindowManager.executeAppTransition(); 1485 mNoAnimActivities.clear(); 1486 return true; 1487 } 1488 1489 try { 1490 // Deliver all pending results. 1491 ArrayList a = next.results; 1492 if (a != null) { 1493 final int N = a.size(); 1494 if (!next.finishing && N > 0) { 1495 if (DEBUG_RESULTS) Slog.v( 1496 TAG, "Delivering results to " + next 1497 + ": " + a); 1498 next.app.thread.scheduleSendResult(next.appToken, a); 1499 } 1500 } 1501 1502 if (next.newIntents != null) { 1503 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken); 1504 } 1505 1506 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, 1507 System.identityHashCode(next), 1508 next.task.taskId, next.shortComponentName); 1509 1510 next.sleeping = false; 1511 showAskCompatModeDialogLocked(next); 1512 next.app.pendingUiClean = true; 1513 next.app.thread.scheduleResumeActivity(next.appToken, 1514 mService.isNextTransitionForward()); 1515 1516 checkReadyForSleepLocked(); 1517 1518 } catch (Exception e) { 1519 // Whoops, need to restart this activity! 1520 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to " 1521 + lastState + ": " + next); 1522 next.state = lastState; 1523 mResumedActivity = lastResumedActivity; 1524 Slog.i(TAG, "Restarting because process died: " + next); 1525 if (!next.hasBeenLaunched) { 1526 next.hasBeenLaunched = true; 1527 } else { 1528 if (SHOW_APP_STARTING_PREVIEW && mMainStack) { 1529 mService.mWindowManager.setAppStartingWindow( 1530 next.appToken, next.packageName, next.theme, 1531 mService.compatibilityInfoForPackageLocked( 1532 next.info.applicationInfo), 1533 next.nonLocalizedLabel, 1534 next.labelRes, next.icon, next.windowFlags, 1535 null, true); 1536 } 1537 } 1538 startSpecificActivityLocked(next, true, false); 1539 return true; 1540 } 1541 1542 // From this point on, if something goes wrong there is no way 1543 // to recover the activity. 1544 try { 1545 next.visible = true; 1546 completeResumeLocked(next); 1547 } catch (Exception e) { 1548 // If any exception gets thrown, toss away this 1549 // activity and try the next one. 1550 Slog.w(TAG, "Exception thrown during resume of " + next, e); 1551 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null, 1552 "resume-exception"); 1553 return true; 1554 } 1555 1556 // Didn't need to use the icicle, and it is now out of date. 1557 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Resumed activity; didn't need icicle of: " + next); 1558 next.icicle = null; 1559 next.haveState = false; 1560 next.stopped = false; 1561 1562 } else { 1563 // Whoops, need to restart this activity! 1564 if (!next.hasBeenLaunched) { 1565 next.hasBeenLaunched = true; 1566 } else { 1567 if (SHOW_APP_STARTING_PREVIEW) { 1568 mService.mWindowManager.setAppStartingWindow( 1569 next.appToken, next.packageName, next.theme, 1570 mService.compatibilityInfoForPackageLocked( 1571 next.info.applicationInfo), 1572 next.nonLocalizedLabel, 1573 next.labelRes, next.icon, next.windowFlags, 1574 null, true); 1575 } 1576 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next); 1577 } 1578 startSpecificActivityLocked(next, true, true); 1579 } 1580 1581 return true; 1582 } 1583 1584 private final void startActivityLocked(ActivityRecord r, boolean newTask, 1585 boolean doResume, boolean keepCurTransition) { 1586 final int NH = mHistory.size(); 1587 1588 int addPos = -1; 1589 1590 if (!newTask) { 1591 // If starting in an existing task, find where that is... 1592 boolean startIt = true; 1593 for (int i = NH-1; i >= 0; i--) { 1594 ActivityRecord p = mHistory.get(i); 1595 if (p.finishing) { 1596 continue; 1597 } 1598 if (p.task == r.task) { 1599 // Here it is! Now, if this is not yet visible to the 1600 // user, then just add it without starting; it will 1601 // get started when the user navigates back to it. 1602 addPos = i+1; 1603 if (!startIt) { 1604 if (DEBUG_ADD_REMOVE) { 1605 RuntimeException here = new RuntimeException("here"); 1606 here.fillInStackTrace(); 1607 Slog.i(TAG, "Adding activity " + r + " to stack at " + addPos, 1608 here); 1609 } 1610 mHistory.add(addPos, r); 1611 r.putInHistory(); 1612 mService.mWindowManager.addAppToken(addPos, r.appToken, r.task.taskId, 1613 r.info.screenOrientation, r.fullscreen); 1614 if (VALIDATE_TOKENS) { 1615 validateAppTokensLocked(); 1616 } 1617 return; 1618 } 1619 break; 1620 } 1621 if (p.fullscreen) { 1622 startIt = false; 1623 } 1624 } 1625 } 1626 1627 // Place a new activity at top of stack, so it is next to interact 1628 // with the user. 1629 if (addPos < 0) { 1630 addPos = NH; 1631 } 1632 1633 // If we are not placing the new activity frontmost, we do not want 1634 // to deliver the onUserLeaving callback to the actual frontmost 1635 // activity 1636 if (addPos < NH) { 1637 mUserLeaving = false; 1638 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() behind front, mUserLeaving=false"); 1639 } 1640 1641 // Slot the activity into the history stack and proceed 1642 if (DEBUG_ADD_REMOVE) { 1643 RuntimeException here = new RuntimeException("here"); 1644 here.fillInStackTrace(); 1645 Slog.i(TAG, "Adding activity " + r + " to stack at " + addPos, here); 1646 } 1647 mHistory.add(addPos, r); 1648 r.putInHistory(); 1649 r.frontOfTask = newTask; 1650 if (NH > 0) { 1651 // We want to show the starting preview window if we are 1652 // switching to a new task, or the next activity's process is 1653 // not currently running. 1654 boolean showStartingIcon = newTask; 1655 ProcessRecord proc = r.app; 1656 if (proc == null) { 1657 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid); 1658 } 1659 if (proc == null || proc.thread == null) { 1660 showStartingIcon = true; 1661 } 1662 if (DEBUG_TRANSITION) Slog.v(TAG, 1663 "Prepare open transition: starting " + r); 1664 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) { 1665 mService.mWindowManager.prepareAppTransition( 1666 WindowManagerPolicy.TRANSIT_NONE, keepCurTransition); 1667 mNoAnimActivities.add(r); 1668 } else if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) { 1669 mService.mWindowManager.prepareAppTransition( 1670 WindowManagerPolicy.TRANSIT_TASK_OPEN, keepCurTransition); 1671 mNoAnimActivities.remove(r); 1672 } else { 1673 mService.mWindowManager.prepareAppTransition(newTask 1674 ? WindowManagerPolicy.TRANSIT_TASK_OPEN 1675 : WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN, keepCurTransition); 1676 mNoAnimActivities.remove(r); 1677 } 1678 mService.mWindowManager.addAppToken( 1679 addPos, r.appToken, r.task.taskId, r.info.screenOrientation, r.fullscreen); 1680 boolean doShow = true; 1681 if (newTask) { 1682 // Even though this activity is starting fresh, we still need 1683 // to reset it to make sure we apply affinities to move any 1684 // existing activities from other tasks in to it. 1685 // If the caller has requested that the target task be 1686 // reset, then do so. 1687 if ((r.intent.getFlags() 1688 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) { 1689 resetTaskIfNeededLocked(r, r); 1690 doShow = topRunningNonDelayedActivityLocked(null) == r; 1691 } 1692 } 1693 if (SHOW_APP_STARTING_PREVIEW && doShow) { 1694 // Figure out if we are transitioning from another activity that is 1695 // "has the same starting icon" as the next one. This allows the 1696 // window manager to keep the previous window it had previously 1697 // created, if it still had one. 1698 ActivityRecord prev = mResumedActivity; 1699 if (prev != null) { 1700 // We don't want to reuse the previous starting preview if: 1701 // (1) The current activity is in a different task. 1702 if (prev.task != r.task) prev = null; 1703 // (2) The current activity is already displayed. 1704 else if (prev.nowVisible) prev = null; 1705 } 1706 mService.mWindowManager.setAppStartingWindow( 1707 r.appToken, r.packageName, r.theme, 1708 mService.compatibilityInfoForPackageLocked( 1709 r.info.applicationInfo), r.nonLocalizedLabel, 1710 r.labelRes, r.icon, r.windowFlags, 1711 prev != null ? prev.appToken : null, showStartingIcon); 1712 } 1713 } else { 1714 // If this is the first activity, don't do any fancy animations, 1715 // because there is nothing for it to animate on top of. 1716 mService.mWindowManager.addAppToken(addPos, r.appToken, r.task.taskId, 1717 r.info.screenOrientation, r.fullscreen); 1718 } 1719 if (VALIDATE_TOKENS) { 1720 validateAppTokensLocked(); 1721 } 1722 1723 if (doResume) { 1724 resumeTopActivityLocked(null); 1725 } 1726 } 1727 1728 final void validateAppTokensLocked() { 1729 mValidateAppTokens.clear(); 1730 mValidateAppTokens.ensureCapacity(mHistory.size()); 1731 for (int i=0; i<mHistory.size(); i++) { 1732 mValidateAppTokens.add(mHistory.get(i).appToken); 1733 } 1734 mService.mWindowManager.validateAppTokens(mValidateAppTokens); 1735 } 1736 1737 /** 1738 * Perform a reset of the given task, if needed as part of launching it. 1739 * Returns the new HistoryRecord at the top of the task. 1740 */ 1741 private final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop, 1742 ActivityRecord newActivity) { 1743 boolean forceReset = (newActivity.info.flags 1744 &ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0; 1745 if (ACTIVITY_INACTIVE_RESET_TIME > 0 1746 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) { 1747 if ((newActivity.info.flags 1748 &ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) { 1749 forceReset = true; 1750 } 1751 } 1752 1753 final TaskRecord task = taskTop.task; 1754 1755 // We are going to move through the history list so that we can look 1756 // at each activity 'target' with 'below' either the interesting 1757 // activity immediately below it in the stack or null. 1758 ActivityRecord target = null; 1759 int targetI = 0; 1760 int taskTopI = -1; 1761 int replyChainEnd = -1; 1762 int lastReparentPos = -1; 1763 for (int i=mHistory.size()-1; i>=-1; i--) { 1764 ActivityRecord below = i >= 0 ? mHistory.get(i) : null; 1765 1766 if (below != null && below.finishing) { 1767 continue; 1768 } 1769 if (target == null) { 1770 target = below; 1771 targetI = i; 1772 // If we were in the middle of a reply chain before this 1773 // task, it doesn't appear like the root of the chain wants 1774 // anything interesting, so drop it. 1775 replyChainEnd = -1; 1776 continue; 1777 } 1778 1779 final int flags = target.info.flags; 1780 1781 final boolean finishOnTaskLaunch = 1782 (flags&ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0; 1783 final boolean allowTaskReparenting = 1784 (flags&ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0; 1785 1786 if (target.task == task) { 1787 // We are inside of the task being reset... we'll either 1788 // finish this activity, push it out for another task, 1789 // or leave it as-is. We only do this 1790 // for activities that are not the root of the task (since 1791 // if we finish the root, we may no longer have the task!). 1792 if (taskTopI < 0) { 1793 taskTopI = targetI; 1794 } 1795 if (below != null && below.task == task) { 1796 final boolean clearWhenTaskReset = 1797 (target.intent.getFlags() 1798 &Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0; 1799 if (!finishOnTaskLaunch && !clearWhenTaskReset && target.resultTo != null) { 1800 // If this activity is sending a reply to a previous 1801 // activity, we can't do anything with it now until 1802 // we reach the start of the reply chain. 1803 // XXX note that we are assuming the result is always 1804 // to the previous activity, which is almost always 1805 // the case but we really shouldn't count on. 1806 if (replyChainEnd < 0) { 1807 replyChainEnd = targetI; 1808 } 1809 } else if (!finishOnTaskLaunch && !clearWhenTaskReset && allowTaskReparenting 1810 && target.taskAffinity != null 1811 && !target.taskAffinity.equals(task.affinity)) { 1812 // If this activity has an affinity for another 1813 // task, then we need to move it out of here. We will 1814 // move it as far out of the way as possible, to the 1815 // bottom of the activity stack. This also keeps it 1816 // correctly ordered with any activities we previously 1817 // moved. 1818 ActivityRecord p = mHistory.get(0); 1819 if (target.taskAffinity != null 1820 && target.taskAffinity.equals(p.task.affinity)) { 1821 // If the activity currently at the bottom has the 1822 // same task affinity as the one we are moving, 1823 // then merge it into the same task. 1824 target.setTask(p.task, p.thumbHolder, false); 1825 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target 1826 + " out to bottom task " + p.task); 1827 } else { 1828 mService.mCurTask++; 1829 if (mService.mCurTask <= 0) { 1830 mService.mCurTask = 1; 1831 } 1832 target.setTask(new TaskRecord(mService.mCurTask, target.info, null), 1833 null, false); 1834 target.task.affinityIntent = target.intent; 1835 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target 1836 + " out to new task " + target.task); 1837 } 1838 mService.mWindowManager.setAppGroupId(target.appToken, task.taskId); 1839 if (replyChainEnd < 0) { 1840 replyChainEnd = targetI; 1841 } 1842 int dstPos = 0; 1843 ThumbnailHolder curThumbHolder = target.thumbHolder; 1844 for (int srcPos=targetI; srcPos<=replyChainEnd; srcPos++) { 1845 p = mHistory.get(srcPos); 1846 if (p.finishing) { 1847 continue; 1848 } 1849 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p 1850 + " out to target's task " + target.task); 1851 p.setTask(target.task, curThumbHolder, false); 1852 curThumbHolder = p.thumbHolder; 1853 if (DEBUG_ADD_REMOVE) { 1854 RuntimeException here = new RuntimeException("here"); 1855 here.fillInStackTrace(); 1856 Slog.i(TAG, "Removing and adding activity " + p + " to stack at " 1857 + dstPos, here); 1858 } 1859 mHistory.remove(srcPos); 1860 mHistory.add(dstPos, p); 1861 mService.mWindowManager.moveAppToken(dstPos, p.appToken); 1862 mService.mWindowManager.setAppGroupId(p.appToken, p.task.taskId); 1863 dstPos++; 1864 if (VALIDATE_TOKENS) { 1865 validateAppTokensLocked(); 1866 } 1867 i++; 1868 } 1869 if (taskTop == p) { 1870 taskTop = below; 1871 } 1872 if (taskTopI == replyChainEnd) { 1873 taskTopI = -1; 1874 } 1875 replyChainEnd = -1; 1876 } else if (forceReset || finishOnTaskLaunch 1877 || clearWhenTaskReset) { 1878 // If the activity should just be removed -- either 1879 // because it asks for it, or the task should be 1880 // cleared -- then finish it and anything that is 1881 // part of its reply chain. 1882 if (clearWhenTaskReset) { 1883 // In this case, we want to finish this activity 1884 // and everything above it, so be sneaky and pretend 1885 // like these are all in the reply chain. 1886 replyChainEnd = targetI+1; 1887 while (replyChainEnd < mHistory.size() && 1888 (mHistory.get( 1889 replyChainEnd)).task == task) { 1890 replyChainEnd++; 1891 } 1892 replyChainEnd--; 1893 } else if (replyChainEnd < 0) { 1894 replyChainEnd = targetI; 1895 } 1896 ActivityRecord p = null; 1897 for (int srcPos=targetI; srcPos<=replyChainEnd; srcPos++) { 1898 p = mHistory.get(srcPos); 1899 if (p.finishing) { 1900 continue; 1901 } 1902 if (finishActivityLocked(p, srcPos, 1903 Activity.RESULT_CANCELED, null, "reset")) { 1904 replyChainEnd--; 1905 srcPos--; 1906 } 1907 } 1908 if (taskTop == p) { 1909 taskTop = below; 1910 } 1911 if (taskTopI == replyChainEnd) { 1912 taskTopI = -1; 1913 } 1914 replyChainEnd = -1; 1915 } else { 1916 // If we were in the middle of a chain, well the 1917 // activity that started it all doesn't want anything 1918 // special, so leave it all as-is. 1919 replyChainEnd = -1; 1920 } 1921 } else { 1922 // Reached the bottom of the task -- any reply chain 1923 // should be left as-is. 1924 replyChainEnd = -1; 1925 } 1926 1927 } else if (target.resultTo != null) { 1928 // If this activity is sending a reply to a previous 1929 // activity, we can't do anything with it now until 1930 // we reach the start of the reply chain. 1931 // XXX note that we are assuming the result is always 1932 // to the previous activity, which is almost always 1933 // the case but we really shouldn't count on. 1934 if (replyChainEnd < 0) { 1935 replyChainEnd = targetI; 1936 } 1937 1938 } else if (taskTopI >= 0 && allowTaskReparenting 1939 && task.affinity != null 1940 && task.affinity.equals(target.taskAffinity)) { 1941 // We are inside of another task... if this activity has 1942 // an affinity for our task, then either remove it if we are 1943 // clearing or move it over to our task. Note that 1944 // we currently punt on the case where we are resetting a 1945 // task that is not at the top but who has activities above 1946 // with an affinity to it... this is really not a normal 1947 // case, and we will need to later pull that task to the front 1948 // and usually at that point we will do the reset and pick 1949 // up those remaining activities. (This only happens if 1950 // someone starts an activity in a new task from an activity 1951 // in a task that is not currently on top.) 1952 if (forceReset || finishOnTaskLaunch) { 1953 if (replyChainEnd < 0) { 1954 replyChainEnd = targetI; 1955 } 1956 ActivityRecord p = null; 1957 for (int srcPos=targetI; srcPos<=replyChainEnd; srcPos++) { 1958 p = mHistory.get(srcPos); 1959 if (p.finishing) { 1960 continue; 1961 } 1962 if (finishActivityLocked(p, srcPos, 1963 Activity.RESULT_CANCELED, null, "reset")) { 1964 taskTopI--; 1965 lastReparentPos--; 1966 replyChainEnd--; 1967 srcPos--; 1968 } 1969 } 1970 replyChainEnd = -1; 1971 } else { 1972 if (replyChainEnd < 0) { 1973 replyChainEnd = targetI; 1974 } 1975 for (int srcPos=replyChainEnd; srcPos>=targetI; srcPos--) { 1976 ActivityRecord p = mHistory.get(srcPos); 1977 if (p.finishing) { 1978 continue; 1979 } 1980 if (lastReparentPos < 0) { 1981 lastReparentPos = taskTopI; 1982 taskTop = p; 1983 } else { 1984 lastReparentPos--; 1985 } 1986 if (DEBUG_ADD_REMOVE) { 1987 RuntimeException here = new RuntimeException("here"); 1988 here.fillInStackTrace(); 1989 Slog.i(TAG, "Removing and adding activity " + p + " to stack at " 1990 + lastReparentPos, here); 1991 } 1992 mHistory.remove(srcPos); 1993 p.setTask(task, null, false); 1994 mHistory.add(lastReparentPos, p); 1995 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p 1996 + " in to resetting task " + task); 1997 mService.mWindowManager.moveAppToken(lastReparentPos, p.appToken); 1998 mService.mWindowManager.setAppGroupId(p.appToken, p.task.taskId); 1999 if (VALIDATE_TOKENS) { 2000 validateAppTokensLocked(); 2001 } 2002 } 2003 replyChainEnd = -1; 2004 2005 // Now we've moved it in to place... but what if this is 2006 // a singleTop activity and we have put it on top of another 2007 // instance of the same activity? Then we drop the instance 2008 // below so it remains singleTop. 2009 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) { 2010 for (int j=lastReparentPos-1; j>=0; j--) { 2011 ActivityRecord p = mHistory.get(j); 2012 if (p.finishing) { 2013 continue; 2014 } 2015 if (p.intent.getComponent().equals(target.intent.getComponent())) { 2016 if (finishActivityLocked(p, j, 2017 Activity.RESULT_CANCELED, null, "replace")) { 2018 taskTopI--; 2019 lastReparentPos--; 2020 } 2021 } 2022 } 2023 } 2024 } 2025 } 2026 2027 target = below; 2028 targetI = i; 2029 } 2030 2031 return taskTop; 2032 } 2033 2034 /** 2035 * Perform clear operation as requested by 2036 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the 2037 * stack to the given task, then look for 2038 * an instance of that activity in the stack and, if found, finish all 2039 * activities on top of it and return the instance. 2040 * 2041 * @param newR Description of the new activity being started. 2042 * @return Returns the old activity that should be continued to be used, 2043 * or null if none was found. 2044 */ 2045 private final ActivityRecord performClearTaskLocked(int taskId, 2046 ActivityRecord newR, int launchFlags) { 2047 int i = mHistory.size(); 2048 2049 // First find the requested task. 2050 while (i > 0) { 2051 i--; 2052 ActivityRecord r = mHistory.get(i); 2053 if (r.task.taskId == taskId) { 2054 i++; 2055 break; 2056 } 2057 } 2058 2059 // Now clear it. 2060 while (i > 0) { 2061 i--; 2062 ActivityRecord r = mHistory.get(i); 2063 if (r.finishing) { 2064 continue; 2065 } 2066 if (r.task.taskId != taskId) { 2067 return null; 2068 } 2069 if (r.realActivity.equals(newR.realActivity)) { 2070 // Here it is! Now finish everything in front... 2071 ActivityRecord ret = r; 2072 while (i < (mHistory.size()-1)) { 2073 i++; 2074 r = mHistory.get(i); 2075 if (r.task.taskId != taskId) { 2076 break; 2077 } 2078 if (r.finishing) { 2079 continue; 2080 } 2081 if (finishActivityLocked(r, i, Activity.RESULT_CANCELED, 2082 null, "clear")) { 2083 i--; 2084 } 2085 } 2086 2087 // Finally, if this is a normal launch mode (that is, not 2088 // expecting onNewIntent()), then we will finish the current 2089 // instance of the activity so a new fresh one can be started. 2090 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE 2091 && (launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0) { 2092 if (!ret.finishing) { 2093 int index = indexOfTokenLocked(ret.appToken); 2094 if (index >= 0) { 2095 finishActivityLocked(ret, index, Activity.RESULT_CANCELED, 2096 null, "clear"); 2097 } 2098 return null; 2099 } 2100 } 2101 2102 return ret; 2103 } 2104 } 2105 2106 return null; 2107 } 2108 2109 /** 2110 * Completely remove all activities associated with an existing 2111 * task starting at a specified index. 2112 */ 2113 private final void performClearTaskAtIndexLocked(int taskId, int i) { 2114 while (i < mHistory.size()) { 2115 ActivityRecord r = mHistory.get(i); 2116 if (r.task.taskId != taskId) { 2117 // Whoops hit the end. 2118 return; 2119 } 2120 if (r.finishing) { 2121 i++; 2122 continue; 2123 } 2124 if (!finishActivityLocked(r, i, Activity.RESULT_CANCELED, 2125 null, "clear")) { 2126 i++; 2127 } 2128 } 2129 } 2130 2131 /** 2132 * Completely remove all activities associated with an existing task. 2133 */ 2134 private final void performClearTaskLocked(int taskId) { 2135 int i = mHistory.size(); 2136 2137 // First find the requested task. 2138 while (i > 0) { 2139 i--; 2140 ActivityRecord r = mHistory.get(i); 2141 if (r.task.taskId == taskId) { 2142 i++; 2143 break; 2144 } 2145 } 2146 2147 // Now find the start and clear it. 2148 while (i > 0) { 2149 i--; 2150 ActivityRecord r = mHistory.get(i); 2151 if (r.finishing) { 2152 continue; 2153 } 2154 if (r.task.taskId != taskId) { 2155 // We hit the bottom. Now finish it all... 2156 performClearTaskAtIndexLocked(taskId, i+1); 2157 return; 2158 } 2159 } 2160 } 2161 2162 /** 2163 * Find the activity in the history stack within the given task. Returns 2164 * the index within the history at which it's found, or < 0 if not found. 2165 */ 2166 private final int findActivityInHistoryLocked(ActivityRecord r, int task) { 2167 int i = mHistory.size(); 2168 while (i > 0) { 2169 i--; 2170 ActivityRecord candidate = mHistory.get(i); 2171 if (candidate.task.taskId != task) { 2172 break; 2173 } 2174 if (candidate.realActivity.equals(r.realActivity)) { 2175 return i; 2176 } 2177 } 2178 2179 return -1; 2180 } 2181 2182 /** 2183 * Reorder the history stack so that the activity at the given index is 2184 * brought to the front. 2185 */ 2186 private final ActivityRecord moveActivityToFrontLocked(int where) { 2187 ActivityRecord newTop = mHistory.remove(where); 2188 int top = mHistory.size(); 2189 ActivityRecord oldTop = mHistory.get(top-1); 2190 if (DEBUG_ADD_REMOVE) { 2191 RuntimeException here = new RuntimeException("here"); 2192 here.fillInStackTrace(); 2193 Slog.i(TAG, "Removing and adding activity " + newTop + " to stack at " 2194 + top, here); 2195 } 2196 mHistory.add(top, newTop); 2197 oldTop.frontOfTask = false; 2198 newTop.frontOfTask = true; 2199 return newTop; 2200 } 2201 2202 final int startActivityLocked(IApplicationThread caller, 2203 Intent intent, String resolvedType, 2204 Uri[] grantedUriPermissions, 2205 int grantedMode, ActivityInfo aInfo, IBinder resultTo, 2206 String resultWho, int requestCode, 2207 int callingPid, int callingUid, boolean onlyIfNeeded, 2208 boolean componentSpecified, ActivityRecord[] outActivity) { 2209 2210 int err = START_SUCCESS; 2211 2212 ProcessRecord callerApp = null; 2213 if (caller != null) { 2214 callerApp = mService.getRecordForAppLocked(caller); 2215 if (callerApp != null) { 2216 callingPid = callerApp.pid; 2217 callingUid = callerApp.info.uid; 2218 } else { 2219 Slog.w(TAG, "Unable to find app for caller " + caller 2220 + " (pid=" + callingPid + ") when starting: " 2221 + intent.toString()); 2222 err = START_PERMISSION_DENIED; 2223 } 2224 } 2225 2226 if (err == START_SUCCESS) { 2227 Slog.i(TAG, "START {" + intent.toShortString(true, true, true) + "} from pid " 2228 + (callerApp != null ? callerApp.pid : callingPid)); 2229 } 2230 2231 ActivityRecord sourceRecord = null; 2232 ActivityRecord resultRecord = null; 2233 if (resultTo != null) { 2234 int index = indexOfTokenLocked(resultTo); 2235 if (DEBUG_RESULTS) Slog.v( 2236 TAG, "Will send result to " + resultTo + " (index " + index + ")"); 2237 if (index >= 0) { 2238 sourceRecord = mHistory.get(index); 2239 if (requestCode >= 0 && !sourceRecord.finishing) { 2240 resultRecord = sourceRecord; 2241 } 2242 } 2243 } 2244 2245 int launchFlags = intent.getFlags(); 2246 2247 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 2248 && sourceRecord != null) { 2249 // Transfer the result target from the source activity to the new 2250 // one being started, including any failures. 2251 if (requestCode >= 0) { 2252 return START_FORWARD_AND_REQUEST_CONFLICT; 2253 } 2254 resultRecord = sourceRecord.resultTo; 2255 resultWho = sourceRecord.resultWho; 2256 requestCode = sourceRecord.requestCode; 2257 sourceRecord.resultTo = null; 2258 if (resultRecord != null) { 2259 resultRecord.removeResultsLocked( 2260 sourceRecord, resultWho, requestCode); 2261 } 2262 } 2263 2264 if (err == START_SUCCESS && intent.getComponent() == null) { 2265 // We couldn't find a class that can handle the given Intent. 2266 // That's the end of that! 2267 err = START_INTENT_NOT_RESOLVED; 2268 } 2269 2270 if (err == START_SUCCESS && aInfo == null) { 2271 // We couldn't find the specific class specified in the Intent. 2272 // Also the end of the line. 2273 err = START_CLASS_NOT_FOUND; 2274 } 2275 2276 if (err != START_SUCCESS) { 2277 if (resultRecord != null) { 2278 sendActivityResultLocked(-1, 2279 resultRecord, resultWho, requestCode, 2280 Activity.RESULT_CANCELED, null); 2281 } 2282 mDismissKeyguardOnNextActivity = false; 2283 return err; 2284 } 2285 2286 final int perm = mService.checkComponentPermission(aInfo.permission, callingPid, 2287 callingUid, aInfo.applicationInfo.uid, aInfo.exported); 2288 if (perm != PackageManager.PERMISSION_GRANTED) { 2289 if (resultRecord != null) { 2290 sendActivityResultLocked(-1, 2291 resultRecord, resultWho, requestCode, 2292 Activity.RESULT_CANCELED, null); 2293 } 2294 mDismissKeyguardOnNextActivity = false; 2295 String msg; 2296 if (!aInfo.exported) { 2297 msg = "Permission Denial: starting " + intent.toString() 2298 + " from " + callerApp + " (pid=" + callingPid 2299 + ", uid=" + callingUid + ")" 2300 + " not exported from uid " + aInfo.applicationInfo.uid; 2301 } else { 2302 msg = "Permission Denial: starting " + intent.toString() 2303 + " from " + callerApp + " (pid=" + callingPid 2304 + ", uid=" + callingUid + ")" 2305 + " requires " + aInfo.permission; 2306 } 2307 Slog.w(TAG, msg); 2308 throw new SecurityException(msg); 2309 } 2310 2311 if (mMainStack) { 2312 if (mService.mController != null) { 2313 boolean abort = false; 2314 try { 2315 // The Intent we give to the watcher has the extra data 2316 // stripped off, since it can contain private information. 2317 Intent watchIntent = intent.cloneFilter(); 2318 abort = !mService.mController.activityStarting(watchIntent, 2319 aInfo.applicationInfo.packageName); 2320 } catch (RemoteException e) { 2321 mService.mController = null; 2322 } 2323 2324 if (abort) { 2325 if (resultRecord != null) { 2326 sendActivityResultLocked(-1, 2327 resultRecord, resultWho, requestCode, 2328 Activity.RESULT_CANCELED, null); 2329 } 2330 // We pretend to the caller that it was really started, but 2331 // they will just get a cancel result. 2332 mDismissKeyguardOnNextActivity = false; 2333 return START_SUCCESS; 2334 } 2335 } 2336 } 2337 2338 ActivityRecord r = new ActivityRecord(mService, this, callerApp, callingUid, 2339 intent, resolvedType, aInfo, mService.mConfiguration, 2340 resultRecord, resultWho, requestCode, componentSpecified); 2341 if (outActivity != null) { 2342 outActivity[0] = r; 2343 } 2344 2345 if (mMainStack) { 2346 if (mResumedActivity == null 2347 || mResumedActivity.info.applicationInfo.uid != callingUid) { 2348 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) { 2349 PendingActivityLaunch pal = new PendingActivityLaunch(); 2350 pal.r = r; 2351 pal.sourceRecord = sourceRecord; 2352 pal.grantedUriPermissions = grantedUriPermissions; 2353 pal.grantedMode = grantedMode; 2354 pal.onlyIfNeeded = onlyIfNeeded; 2355 mService.mPendingActivityLaunches.add(pal); 2356 mDismissKeyguardOnNextActivity = false; 2357 return START_SWITCHES_CANCELED; 2358 } 2359 } 2360 2361 if (mService.mDidAppSwitch) { 2362 // This is the second allowed switch since we stopped switches, 2363 // so now just generally allow switches. Use case: user presses 2364 // home (switches disabled, switch to home, mDidAppSwitch now true); 2365 // user taps a home icon (coming from home so allowed, we hit here 2366 // and now allow anyone to switch again). 2367 mService.mAppSwitchesAllowedTime = 0; 2368 } else { 2369 mService.mDidAppSwitch = true; 2370 } 2371 2372 mService.doPendingActivityLaunchesLocked(false); 2373 } 2374 2375 err = startActivityUncheckedLocked(r, sourceRecord, 2376 grantedUriPermissions, grantedMode, onlyIfNeeded, true); 2377 if (mDismissKeyguardOnNextActivity && mPausingActivity == null) { 2378 // Someone asked to have the keyguard dismissed on the next 2379 // activity start, but we are not actually doing an activity 2380 // switch... just dismiss the keyguard now, because we 2381 // probably want to see whatever is behind it. 2382 mDismissKeyguardOnNextActivity = false; 2383 mService.mWindowManager.dismissKeyguard(); 2384 } 2385 return err; 2386 } 2387 2388 final void moveHomeToFrontFromLaunchLocked(int launchFlags) { 2389 if ((launchFlags & 2390 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) 2391 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) { 2392 // Caller wants to appear on home activity, so before starting 2393 // their own activity we will bring home to the front. 2394 moveHomeToFrontLocked(); 2395 } 2396 } 2397 2398 final int startActivityUncheckedLocked(ActivityRecord r, 2399 ActivityRecord sourceRecord, Uri[] grantedUriPermissions, 2400 int grantedMode, boolean onlyIfNeeded, boolean doResume) { 2401 final Intent intent = r.intent; 2402 final int callingUid = r.launchedFromUid; 2403 2404 int launchFlags = intent.getFlags(); 2405 2406 // We'll invoke onUserLeaving before onPause only if the launching 2407 // activity did not explicitly state that this is an automated launch. 2408 mUserLeaving = (launchFlags&Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0; 2409 if (DEBUG_USER_LEAVING) Slog.v(TAG, 2410 "startActivity() => mUserLeaving=" + mUserLeaving); 2411 2412 // If the caller has asked not to resume at this point, we make note 2413 // of this in the record so that we can skip it when trying to find 2414 // the top running activity. 2415 if (!doResume) { 2416 r.delayedResume = true; 2417 } 2418 2419 ActivityRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) 2420 != 0 ? r : null; 2421 2422 // If the onlyIfNeeded flag is set, then we can do this if the activity 2423 // being launched is the same as the one making the call... or, as 2424 // a special case, if we do not know the caller then we count the 2425 // current top activity as the caller. 2426 if (onlyIfNeeded) { 2427 ActivityRecord checkedCaller = sourceRecord; 2428 if (checkedCaller == null) { 2429 checkedCaller = topRunningNonDelayedActivityLocked(notTop); 2430 } 2431 if (!checkedCaller.realActivity.equals(r.realActivity)) { 2432 // Caller is not the same as launcher, so always needed. 2433 onlyIfNeeded = false; 2434 } 2435 } 2436 2437 if (sourceRecord == null) { 2438 // This activity is not being started from another... in this 2439 // case we -always- start a new task. 2440 if ((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) { 2441 Slog.w(TAG, "startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: " 2442 + intent); 2443 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK; 2444 } 2445 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) { 2446 // The original activity who is starting us is running as a single 2447 // instance... this new activity it is starting must go on its 2448 // own task. 2449 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK; 2450 } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE 2451 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) { 2452 // The activity being started is a single instance... it always 2453 // gets launched into its own task. 2454 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK; 2455 } 2456 2457 if (r.resultTo != null && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) { 2458 // For whatever reason this activity is being launched into a new 2459 // task... yet the caller has requested a result back. Well, that 2460 // is pretty messed up, so instead immediately send back a cancel 2461 // and let the new task continue launched as normal without a 2462 // dependency on its originator. 2463 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result."); 2464 sendActivityResultLocked(-1, 2465 r.resultTo, r.resultWho, r.requestCode, 2466 Activity.RESULT_CANCELED, null); 2467 r.resultTo = null; 2468 } 2469 2470 boolean addingToTask = false; 2471 TaskRecord reuseTask = null; 2472 if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 && 2473 (launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0) 2474 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK 2475 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) { 2476 // If bring to front is requested, and no result is requested, and 2477 // we can find a task that was started with this same 2478 // component, then instead of launching bring that one to the front. 2479 if (r.resultTo == null) { 2480 // See if there is a task to bring to the front. If this is 2481 // a SINGLE_INSTANCE activity, there can be one and only one 2482 // instance of it in the history, and it is always in its own 2483 // unique task, so we do a special search. 2484 ActivityRecord taskTop = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE 2485 ? findTaskLocked(intent, r.info) 2486 : findActivityLocked(intent, r.info); 2487 if (taskTop != null) { 2488 if (taskTop.task.intent == null) { 2489 // This task was started because of movement of 2490 // the activity based on affinity... now that we 2491 // are actually launching it, we can assign the 2492 // base intent. 2493 taskTop.task.setIntent(intent, r.info); 2494 } 2495 // If the target task is not in the front, then we need 2496 // to bring it to the front... except... well, with 2497 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like 2498 // to have the same behavior as if a new instance was 2499 // being started, which means not bringing it to the front 2500 // if the caller is not itself in the front. 2501 ActivityRecord curTop = topRunningNonDelayedActivityLocked(notTop); 2502 if (curTop != null && curTop.task != taskTop.task) { 2503 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); 2504 boolean callerAtFront = sourceRecord == null 2505 || curTop.task == sourceRecord.task; 2506 if (callerAtFront) { 2507 // We really do want to push this one into the 2508 // user's face, right now. 2509 moveHomeToFrontFromLaunchLocked(launchFlags); 2510 moveTaskToFrontLocked(taskTop.task, r); 2511 } 2512 } 2513 // If the caller has requested that the target task be 2514 // reset, then do so. 2515 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) { 2516 taskTop = resetTaskIfNeededLocked(taskTop, r); 2517 } 2518 if (onlyIfNeeded) { 2519 // We don't need to start a new activity, and 2520 // the client said not to do anything if that 2521 // is the case, so this is it! And for paranoia, make 2522 // sure we have correctly resumed the top activity. 2523 if (doResume) { 2524 resumeTopActivityLocked(null); 2525 } 2526 return START_RETURN_INTENT_TO_CALLER; 2527 } 2528 if ((launchFlags & 2529 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) 2530 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) { 2531 // The caller has requested to completely replace any 2532 // existing task with its new activity. Well that should 2533 // not be too hard... 2534 reuseTask = taskTop.task; 2535 performClearTaskLocked(taskTop.task.taskId); 2536 reuseTask.setIntent(r.intent, r.info); 2537 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0 2538 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK 2539 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) { 2540 // In this situation we want to remove all activities 2541 // from the task up to the one being started. In most 2542 // cases this means we are resetting the task to its 2543 // initial state. 2544 ActivityRecord top = performClearTaskLocked( 2545 taskTop.task.taskId, r, launchFlags); 2546 if (top != null) { 2547 if (top.frontOfTask) { 2548 // Activity aliases may mean we use different 2549 // intents for the top activity, so make sure 2550 // the task now has the identity of the new 2551 // intent. 2552 top.task.setIntent(r.intent, r.info); 2553 } 2554 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task); 2555 top.deliverNewIntentLocked(callingUid, r.intent); 2556 } else { 2557 // A special case: we need to 2558 // start the activity because it is not currently 2559 // running, and the caller has asked to clear the 2560 // current task to have this activity at the top. 2561 addingToTask = true; 2562 // Now pretend like this activity is being started 2563 // by the top of its task, so it is put in the 2564 // right place. 2565 sourceRecord = taskTop; 2566 } 2567 } else if (r.realActivity.equals(taskTop.task.realActivity)) { 2568 // In this case the top activity on the task is the 2569 // same as the one being launched, so we take that 2570 // as a request to bring the task to the foreground. 2571 // If the top activity in the task is the root 2572 // activity, deliver this new intent to it if it 2573 // desires. 2574 if ((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 2575 && taskTop.realActivity.equals(r.realActivity)) { 2576 logStartActivity(EventLogTags.AM_NEW_INTENT, r, taskTop.task); 2577 if (taskTop.frontOfTask) { 2578 taskTop.task.setIntent(r.intent, r.info); 2579 } 2580 taskTop.deliverNewIntentLocked(callingUid, r.intent); 2581 } else if (!r.intent.filterEquals(taskTop.task.intent)) { 2582 // In this case we are launching the root activity 2583 // of the task, but with a different intent. We 2584 // should start a new instance on top. 2585 addingToTask = true; 2586 sourceRecord = taskTop; 2587 } 2588 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) { 2589 // In this case an activity is being launched in to an 2590 // existing task, without resetting that task. This 2591 // is typically the situation of launching an activity 2592 // from a notification or shortcut. We want to place 2593 // the new activity on top of the current task. 2594 addingToTask = true; 2595 sourceRecord = taskTop; 2596 } else if (!taskTop.task.rootWasReset) { 2597 // In this case we are launching in to an existing task 2598 // that has not yet been started from its front door. 2599 // The current task has been brought to the front. 2600 // Ideally, we'd probably like to place this new task 2601 // at the bottom of its stack, but that's a little hard 2602 // to do with the current organization of the code so 2603 // for now we'll just drop it. 2604 taskTop.task.setIntent(r.intent, r.info); 2605 } 2606 if (!addingToTask && reuseTask == null) { 2607 // We didn't do anything... but it was needed (a.k.a., client 2608 // don't use that intent!) And for paranoia, make 2609 // sure we have correctly resumed the top activity. 2610 if (doResume) { 2611 resumeTopActivityLocked(null); 2612 } 2613 return START_TASK_TO_FRONT; 2614 } 2615 } 2616 } 2617 } 2618 2619 //String uri = r.intent.toURI(); 2620 //Intent intent2 = new Intent(uri); 2621 //Slog.i(TAG, "Given intent: " + r.intent); 2622 //Slog.i(TAG, "URI is: " + uri); 2623 //Slog.i(TAG, "To intent: " + intent2); 2624 2625 if (r.packageName != null) { 2626 // If the activity being launched is the same as the one currently 2627 // at the top, then we need to check if it should only be launched 2628 // once. 2629 ActivityRecord top = topRunningNonDelayedActivityLocked(notTop); 2630 if (top != null && r.resultTo == null) { 2631 if (top.realActivity.equals(r.realActivity)) { 2632 if (top.app != null && top.app.thread != null) { 2633 if ((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 2634 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP 2635 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) { 2636 logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task); 2637 // For paranoia, make sure we have correctly 2638 // resumed the top activity. 2639 if (doResume) { 2640 resumeTopActivityLocked(null); 2641 } 2642 if (onlyIfNeeded) { 2643 // We don't need to start a new activity, and 2644 // the client said not to do anything if that 2645 // is the case, so this is it! 2646 return START_RETURN_INTENT_TO_CALLER; 2647 } 2648 top.deliverNewIntentLocked(callingUid, r.intent); 2649 return START_DELIVERED_TO_TOP; 2650 } 2651 } 2652 } 2653 } 2654 2655 } else { 2656 if (r.resultTo != null) { 2657 sendActivityResultLocked(-1, 2658 r.resultTo, r.resultWho, r.requestCode, 2659 Activity.RESULT_CANCELED, null); 2660 } 2661 return START_CLASS_NOT_FOUND; 2662 } 2663 2664 boolean newTask = false; 2665 boolean keepCurTransition = false; 2666 2667 // Should this be considered a new task? 2668 if (r.resultTo == null && !addingToTask 2669 && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) { 2670 if (reuseTask == null) { 2671 // todo: should do better management of integers. 2672 mService.mCurTask++; 2673 if (mService.mCurTask <= 0) { 2674 mService.mCurTask = 1; 2675 } 2676 r.setTask(new TaskRecord(mService.mCurTask, r.info, intent), null, true); 2677 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r 2678 + " in new task " + r.task); 2679 } else { 2680 r.setTask(reuseTask, reuseTask, true); 2681 } 2682 newTask = true; 2683 moveHomeToFrontFromLaunchLocked(launchFlags); 2684 2685 } else if (sourceRecord != null) { 2686 if (!addingToTask && 2687 (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) { 2688 // In this case, we are adding the activity to an existing 2689 // task, but the caller has asked to clear that task if the 2690 // activity is already running. 2691 ActivityRecord top = performClearTaskLocked( 2692 sourceRecord.task.taskId, r, launchFlags); 2693 keepCurTransition = true; 2694 if (top != null) { 2695 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task); 2696 top.deliverNewIntentLocked(callingUid, r.intent); 2697 // For paranoia, make sure we have correctly 2698 // resumed the top activity. 2699 if (doResume) { 2700 resumeTopActivityLocked(null); 2701 } 2702 return START_DELIVERED_TO_TOP; 2703 } 2704 } else if (!addingToTask && 2705 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) { 2706 // In this case, we are launching an activity in our own task 2707 // that may already be running somewhere in the history, and 2708 // we want to shuffle it to the front of the stack if so. 2709 int where = findActivityInHistoryLocked(r, sourceRecord.task.taskId); 2710 if (where >= 0) { 2711 ActivityRecord top = moveActivityToFrontLocked(where); 2712 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task); 2713 top.deliverNewIntentLocked(callingUid, r.intent); 2714 if (doResume) { 2715 resumeTopActivityLocked(null); 2716 } 2717 return START_DELIVERED_TO_TOP; 2718 } 2719 } 2720 // An existing activity is starting this new activity, so we want 2721 // to keep the new one in the same task as the one that is starting 2722 // it. 2723 r.setTask(sourceRecord.task, sourceRecord.thumbHolder, false); 2724 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r 2725 + " in existing task " + r.task); 2726 2727 } else { 2728 // This not being started from an existing activity, and not part 2729 // of a new task... just put it in the top task, though these days 2730 // this case should never happen. 2731 final int N = mHistory.size(); 2732 ActivityRecord prev = 2733 N > 0 ? mHistory.get(N-1) : null; 2734 r.setTask(prev != null 2735 ? prev.task 2736 : new TaskRecord(mService.mCurTask, r.info, intent), null, true); 2737 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r 2738 + " in new guessed " + r.task); 2739 } 2740 2741 if (grantedUriPermissions != null && callingUid > 0) { 2742 for (int i=0; i<grantedUriPermissions.length; i++) { 2743 mService.grantUriPermissionLocked(callingUid, r.packageName, 2744 grantedUriPermissions[i], grantedMode, r.getUriPermissionsLocked()); 2745 } 2746 } 2747 2748 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName, 2749 intent, r.getUriPermissionsLocked()); 2750 2751 if (newTask) { 2752 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.task.taskId); 2753 } 2754 logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task); 2755 startActivityLocked(r, newTask, doResume, keepCurTransition); 2756 return START_SUCCESS; 2757 } 2758 2759 ActivityInfo resolveActivity(Intent intent, String resolvedType, boolean debug, 2760 String profileFile, ParcelFileDescriptor profileFd, boolean autoStopProfiler) { 2761 // Collect information about the target of the Intent. 2762 ActivityInfo aInfo; 2763 try { 2764 ResolveInfo rInfo = 2765 AppGlobals.getPackageManager().resolveIntent( 2766 intent, resolvedType, 2767 PackageManager.MATCH_DEFAULT_ONLY 2768 | ActivityManagerService.STOCK_PM_FLAGS); 2769 aInfo = rInfo != null ? rInfo.activityInfo : null; 2770 } catch (RemoteException e) { 2771 aInfo = null; 2772 } 2773 2774 if (aInfo != null) { 2775 // Store the found target back into the intent, because now that 2776 // we have it we never want to do this again. For example, if the 2777 // user navigates back to this point in the history, we should 2778 // always restart the exact same activity. 2779 intent.setComponent(new ComponentName( 2780 aInfo.applicationInfo.packageName, aInfo.name)); 2781 2782 // Don't debug things in the system process 2783 if (debug) { 2784 if (!aInfo.processName.equals("system")) { 2785 mService.setDebugApp(aInfo.processName, true, false); 2786 } 2787 } 2788 2789 if (profileFile != null) { 2790 if (!aInfo.processName.equals("system")) { 2791 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, 2792 profileFile, profileFd, autoStopProfiler); 2793 } 2794 } 2795 } 2796 return aInfo; 2797 } 2798 2799 final int startActivityMayWait(IApplicationThread caller, int callingUid, 2800 Intent intent, String resolvedType, Uri[] grantedUriPermissions, 2801 int grantedMode, IBinder resultTo, 2802 String resultWho, int requestCode, boolean onlyIfNeeded, 2803 boolean debug, String profileFile, ParcelFileDescriptor profileFd, 2804 boolean autoStopProfiler, WaitResult outResult, Configuration config) { 2805 // Refuse possible leaked file descriptors 2806 if (intent != null && intent.hasFileDescriptors()) { 2807 throw new IllegalArgumentException("File descriptors passed in Intent"); 2808 } 2809 2810 boolean componentSpecified = intent.getComponent() != null; 2811 2812 // Don't modify the client's object! 2813 intent = new Intent(intent); 2814 2815 // Collect information about the target of the Intent. 2816 ActivityInfo aInfo = resolveActivity(intent, resolvedType, debug, 2817 profileFile, profileFd, autoStopProfiler); 2818 2819 synchronized (mService) { 2820 int callingPid; 2821 if (callingUid >= 0) { 2822 callingPid = -1; 2823 } else if (caller == null) { 2824 callingPid = Binder.getCallingPid(); 2825 callingUid = Binder.getCallingUid(); 2826 } else { 2827 callingPid = callingUid = -1; 2828 } 2829 2830 mConfigWillChange = config != null 2831 && mService.mConfiguration.diff(config) != 0; 2832 if (DEBUG_CONFIGURATION) Slog.v(TAG, 2833 "Starting activity when config will change = " + mConfigWillChange); 2834 2835 final long origId = Binder.clearCallingIdentity(); 2836 2837 if (mMainStack && aInfo != null && 2838 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) { 2839 // This may be a heavy-weight process! Check to see if we already 2840 // have another, different heavy-weight process running. 2841 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) { 2842 if (mService.mHeavyWeightProcess != null && 2843 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid || 2844 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) { 2845 int realCallingPid = callingPid; 2846 int realCallingUid = callingUid; 2847 if (caller != null) { 2848 ProcessRecord callerApp = mService.getRecordForAppLocked(caller); 2849 if (callerApp != null) { 2850 realCallingPid = callerApp.pid; 2851 realCallingUid = callerApp.info.uid; 2852 } else { 2853 Slog.w(TAG, "Unable to find app for caller " + caller 2854 + " (pid=" + realCallingPid + ") when starting: " 2855 + intent.toString()); 2856 return START_PERMISSION_DENIED; 2857 } 2858 } 2859 2860 IIntentSender target = mService.getIntentSenderLocked( 2861 IActivityManager.INTENT_SENDER_ACTIVITY, "android", 2862 realCallingUid, null, null, 0, new Intent[] { intent }, 2863 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT 2864 | PendingIntent.FLAG_ONE_SHOT); 2865 2866 Intent newIntent = new Intent(); 2867 if (requestCode >= 0) { 2868 // Caller is requesting a result. 2869 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true); 2870 } 2871 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT, 2872 new IntentSender(target)); 2873 if (mService.mHeavyWeightProcess.activities.size() > 0) { 2874 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0); 2875 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP, 2876 hist.packageName); 2877 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK, 2878 hist.task.taskId); 2879 } 2880 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP, 2881 aInfo.packageName); 2882 newIntent.setFlags(intent.getFlags()); 2883 newIntent.setClassName("android", 2884 HeavyWeightSwitcherActivity.class.getName()); 2885 intent = newIntent; 2886 resolvedType = null; 2887 caller = null; 2888 callingUid = Binder.getCallingUid(); 2889 callingPid = Binder.getCallingPid(); 2890 componentSpecified = true; 2891 try { 2892 ResolveInfo rInfo = 2893 AppGlobals.getPackageManager().resolveIntent( 2894 intent, null, 2895 PackageManager.MATCH_DEFAULT_ONLY 2896 | ActivityManagerService.STOCK_PM_FLAGS); 2897 aInfo = rInfo != null ? rInfo.activityInfo : null; 2898 } catch (RemoteException e) { 2899 aInfo = null; 2900 } 2901 } 2902 } 2903 } 2904 2905 int res = startActivityLocked(caller, intent, resolvedType, 2906 grantedUriPermissions, grantedMode, aInfo, 2907 resultTo, resultWho, requestCode, callingPid, callingUid, 2908 onlyIfNeeded, componentSpecified, null); 2909 2910 if (mConfigWillChange && mMainStack) { 2911 // If the caller also wants to switch to a new configuration, 2912 // do so now. This allows a clean switch, as we are waiting 2913 // for the current activity to pause (so we will not destroy 2914 // it), and have not yet started the next activity. 2915 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, 2916 "updateConfiguration()"); 2917 mConfigWillChange = false; 2918 if (DEBUG_CONFIGURATION) Slog.v(TAG, 2919 "Updating to new configuration after starting activity."); 2920 mService.updateConfigurationLocked(config, null, false); 2921 } 2922 2923 Binder.restoreCallingIdentity(origId); 2924 2925 if (outResult != null) { 2926 outResult.result = res; 2927 if (res == IActivityManager.START_SUCCESS) { 2928 mWaitingActivityLaunched.add(outResult); 2929 do { 2930 try { 2931 mService.wait(); 2932 } catch (InterruptedException e) { 2933 } 2934 } while (!outResult.timeout && outResult.who == null); 2935 } else if (res == IActivityManager.START_TASK_TO_FRONT) { 2936 ActivityRecord r = this.topRunningActivityLocked(null); 2937 if (r.nowVisible) { 2938 outResult.timeout = false; 2939 outResult.who = new ComponentName(r.info.packageName, r.info.name); 2940 outResult.totalTime = 0; 2941 outResult.thisTime = 0; 2942 } else { 2943 outResult.thisTime = SystemClock.uptimeMillis(); 2944 mWaitingActivityVisible.add(outResult); 2945 do { 2946 try { 2947 mService.wait(); 2948 } catch (InterruptedException e) { 2949 } 2950 } while (!outResult.timeout && outResult.who == null); 2951 } 2952 } 2953 } 2954 2955 return res; 2956 } 2957 } 2958 2959 final int startActivities(IApplicationThread caller, int callingUid, 2960 Intent[] intents, String[] resolvedTypes, IBinder resultTo) { 2961 if (intents == null) { 2962 throw new NullPointerException("intents is null"); 2963 } 2964 if (resolvedTypes == null) { 2965 throw new NullPointerException("resolvedTypes is null"); 2966 } 2967 if (intents.length != resolvedTypes.length) { 2968 throw new IllegalArgumentException("intents are length different than resolvedTypes"); 2969 } 2970 2971 ActivityRecord[] outActivity = new ActivityRecord[1]; 2972 2973 int callingPid; 2974 if (callingUid >= 0) { 2975 callingPid = -1; 2976 } else if (caller == null) { 2977 callingPid = Binder.getCallingPid(); 2978 callingUid = Binder.getCallingUid(); 2979 } else { 2980 callingPid = callingUid = -1; 2981 } 2982 final long origId = Binder.clearCallingIdentity(); 2983 try { 2984 synchronized (mService) { 2985 2986 for (int i=0; i<intents.length; i++) { 2987 Intent intent = intents[i]; 2988 if (intent == null) { 2989 continue; 2990 } 2991 2992 // Refuse possible leaked file descriptors 2993 if (intent != null && intent.hasFileDescriptors()) { 2994 throw new IllegalArgumentException("File descriptors passed in Intent"); 2995 } 2996 2997 boolean componentSpecified = intent.getComponent() != null; 2998 2999 // Don't modify the client's object! 3000 intent = new Intent(intent); 3001 3002 // Collect information about the target of the Intent. 3003 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], false, 3004 null, null, false); 3005 3006 if (mMainStack && aInfo != null && (aInfo.applicationInfo.flags 3007 & ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) { 3008 throw new IllegalArgumentException( 3009 "FLAG_CANT_SAVE_STATE not supported here"); 3010 } 3011 3012 int res = startActivityLocked(caller, intent, resolvedTypes[i], 3013 null, 0, aInfo, resultTo, null, -1, callingPid, callingUid, 3014 false, componentSpecified, outActivity); 3015 if (res < 0) { 3016 return res; 3017 } 3018 3019 resultTo = outActivity[0] != null ? outActivity[0].appToken : null; 3020 } 3021 } 3022 } finally { 3023 Binder.restoreCallingIdentity(origId); 3024 } 3025 3026 return IActivityManager.START_SUCCESS; 3027 } 3028 3029 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r, 3030 long thisTime, long totalTime) { 3031 for (int i=mWaitingActivityLaunched.size()-1; i>=0; i--) { 3032 WaitResult w = mWaitingActivityLaunched.get(i); 3033 w.timeout = timeout; 3034 if (r != null) { 3035 w.who = new ComponentName(r.info.packageName, r.info.name); 3036 } 3037 w.thisTime = thisTime; 3038 w.totalTime = totalTime; 3039 } 3040 mService.notifyAll(); 3041 } 3042 3043 void reportActivityVisibleLocked(ActivityRecord r) { 3044 for (int i=mWaitingActivityVisible.size()-1; i>=0; i--) { 3045 WaitResult w = mWaitingActivityVisible.get(i); 3046 w.timeout = false; 3047 if (r != null) { 3048 w.who = new ComponentName(r.info.packageName, r.info.name); 3049 } 3050 w.totalTime = SystemClock.uptimeMillis() - w.thisTime; 3051 w.thisTime = w.totalTime; 3052 } 3053 mService.notifyAll(); 3054 3055 if (mDismissKeyguardOnNextActivity) { 3056 mDismissKeyguardOnNextActivity = false; 3057 mService.mWindowManager.dismissKeyguard(); 3058 } 3059 } 3060 3061 void sendActivityResultLocked(int callingUid, ActivityRecord r, 3062 String resultWho, int requestCode, int resultCode, Intent data) { 3063 3064 if (callingUid > 0) { 3065 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName, 3066 data, r.getUriPermissionsLocked()); 3067 } 3068 3069 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r 3070 + " : who=" + resultWho + " req=" + requestCode 3071 + " res=" + resultCode + " data=" + data); 3072 if (mResumedActivity == r && r.app != null && r.app.thread != null) { 3073 try { 3074 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>(); 3075 list.add(new ResultInfo(resultWho, requestCode, 3076 resultCode, data)); 3077 r.app.thread.scheduleSendResult(r.appToken, list); 3078 return; 3079 } catch (Exception e) { 3080 Slog.w(TAG, "Exception thrown sending result to " + r, e); 3081 } 3082 } 3083 3084 r.addResultLocked(null, resultWho, requestCode, resultCode, data); 3085 } 3086 3087 private final void stopActivityLocked(ActivityRecord r) { 3088 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r); 3089 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0 3090 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) { 3091 if (!r.finishing) { 3092 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null, 3093 "no-history"); 3094 } 3095 } else if (r.app != null && r.app.thread != null) { 3096 if (mMainStack) { 3097 if (mService.mFocusedActivity == r) { 3098 mService.setFocusedActivityLocked(topRunningActivityLocked(null)); 3099 } 3100 } 3101 r.resumeKeyDispatchingLocked(); 3102 try { 3103 r.stopped = false; 3104 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r 3105 + " (stop requested)"); 3106 r.state = ActivityState.STOPPING; 3107 if (DEBUG_VISBILITY) Slog.v( 3108 TAG, "Stopping visible=" + r.visible + " for " + r); 3109 if (!r.visible) { 3110 mService.mWindowManager.setAppVisibility(r.appToken, false); 3111 } 3112 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags); 3113 if (mService.isSleeping()) { 3114 r.setSleeping(true); 3115 } 3116 } catch (Exception e) { 3117 // Maybe just ignore exceptions here... if the process 3118 // has crashed, our death notification will clean things 3119 // up. 3120 Slog.w(TAG, "Exception thrown during pause", e); 3121 // Just in case, assume it to be stopped. 3122 r.stopped = true; 3123 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r); 3124 r.state = ActivityState.STOPPED; 3125 if (r.configDestroy) { 3126 destroyActivityLocked(r, true, false, "stop-except"); 3127 } 3128 } 3129 } 3130 } 3131 3132 final ArrayList<ActivityRecord> processStoppingActivitiesLocked( 3133 boolean remove) { 3134 int N = mStoppingActivities.size(); 3135 if (N <= 0) return null; 3136 3137 ArrayList<ActivityRecord> stops = null; 3138 3139 final boolean nowVisible = mResumedActivity != null 3140 && mResumedActivity.nowVisible 3141 && !mResumedActivity.waitingVisible; 3142 for (int i=0; i<N; i++) { 3143 ActivityRecord s = mStoppingActivities.get(i); 3144 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible=" 3145 + nowVisible + " waitingVisible=" + s.waitingVisible 3146 + " finishing=" + s.finishing); 3147 if (s.waitingVisible && nowVisible) { 3148 mWaitingVisibleActivities.remove(s); 3149 s.waitingVisible = false; 3150 if (s.finishing) { 3151 // If this activity is finishing, it is sitting on top of 3152 // everyone else but we now know it is no longer needed... 3153 // so get rid of it. Otherwise, we need to go through the 3154 // normal flow and hide it once we determine that it is 3155 // hidden by the activities in front of it. 3156 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s); 3157 mService.mWindowManager.setAppVisibility(s.appToken, false); 3158 } 3159 } 3160 if ((!s.waitingVisible || mService.isSleeping()) && remove) { 3161 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s); 3162 if (stops == null) { 3163 stops = new ArrayList<ActivityRecord>(); 3164 } 3165 stops.add(s); 3166 mStoppingActivities.remove(i); 3167 N--; 3168 i--; 3169 } 3170 } 3171 3172 return stops; 3173 } 3174 3175 final void scheduleIdleLocked() { 3176 Message msg = Message.obtain(); 3177 msg.what = IDLE_NOW_MSG; 3178 mHandler.sendMessage(msg); 3179 } 3180 3181 final ActivityRecord activityIdleInternal(IBinder token, boolean fromTimeout, 3182 Configuration config) { 3183 if (localLOGV) Slog.v(TAG, "Activity idle: " + token); 3184 3185 ActivityRecord res = null; 3186 3187 ArrayList<ActivityRecord> stops = null; 3188 ArrayList<ActivityRecord> finishes = null; 3189 ArrayList<ActivityRecord> thumbnails = null; 3190 int NS = 0; 3191 int NF = 0; 3192 int NT = 0; 3193 IApplicationThread sendThumbnail = null; 3194 boolean booting = false; 3195 boolean enableScreen = false; 3196 3197 synchronized (mService) { 3198 ActivityRecord r = ActivityRecord.forToken(token); 3199 if (r != null) { 3200 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r); 3201 } 3202 3203 // Get the activity record. 3204 int index = indexOfActivityLocked(r); 3205 if (index >= 0) { 3206 res = r; 3207 3208 if (fromTimeout) { 3209 reportActivityLaunchedLocked(fromTimeout, r, -1, -1); 3210 } 3211 3212 // This is a hack to semi-deal with a race condition 3213 // in the client where it can be constructed with a 3214 // newer configuration from when we asked it to launch. 3215 // We'll update with whatever configuration it now says 3216 // it used to launch. 3217 if (config != null) { 3218 r.configuration = config; 3219 } 3220 3221 // No longer need to keep the device awake. 3222 if (mResumedActivity == r && mLaunchingActivity.isHeld()) { 3223 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG); 3224 mLaunchingActivity.release(); 3225 } 3226 3227 // We are now idle. If someone is waiting for a thumbnail from 3228 // us, we can now deliver. 3229 r.idle = true; 3230 mService.scheduleAppGcsLocked(); 3231 if (r.thumbnailNeeded && r.app != null && r.app.thread != null) { 3232 sendThumbnail = r.app.thread; 3233 r.thumbnailNeeded = false; 3234 } 3235 3236 // If this activity is fullscreen, set up to hide those under it. 3237 3238 if (DEBUG_VISBILITY) Slog.v(TAG, "Idle activity for " + r); 3239 ensureActivitiesVisibleLocked(null, 0); 3240 3241 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout); 3242 if (mMainStack) { 3243 if (!mService.mBooted) { 3244 mService.mBooted = true; 3245 enableScreen = true; 3246 } 3247 } 3248 3249 } else if (fromTimeout) { 3250 reportActivityLaunchedLocked(fromTimeout, null, -1, -1); 3251 } 3252 3253 // Atomically retrieve all of the other things to do. 3254 stops = processStoppingActivitiesLocked(true); 3255 NS = stops != null ? stops.size() : 0; 3256 if ((NF=mFinishingActivities.size()) > 0) { 3257 finishes = new ArrayList<ActivityRecord>(mFinishingActivities); 3258 mFinishingActivities.clear(); 3259 } 3260 if ((NT=mService.mCancelledThumbnails.size()) > 0) { 3261 thumbnails = new ArrayList<ActivityRecord>(mService.mCancelledThumbnails); 3262 mService.mCancelledThumbnails.clear(); 3263 } 3264 3265 if (mMainStack) { 3266 booting = mService.mBooting; 3267 mService.mBooting = false; 3268 } 3269 } 3270 3271 int i; 3272 3273 // Send thumbnail if requested. 3274 if (sendThumbnail != null) { 3275 try { 3276 sendThumbnail.requestThumbnail(token); 3277 } catch (Exception e) { 3278 Slog.w(TAG, "Exception thrown when requesting thumbnail", e); 3279 mService.sendPendingThumbnail(null, token, null, null, true); 3280 } 3281 } 3282 3283 // Stop any activities that are scheduled to do so but have been 3284 // waiting for the next one to start. 3285 for (i=0; i<NS; i++) { 3286 ActivityRecord r = (ActivityRecord)stops.get(i); 3287 synchronized (mService) { 3288 if (r.finishing) { 3289 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY); 3290 } else { 3291 stopActivityLocked(r); 3292 } 3293 } 3294 } 3295 3296 // Finish any activities that are scheduled to do so but have been 3297 // waiting for the next one to start. 3298 for (i=0; i<NF; i++) { 3299 ActivityRecord r = (ActivityRecord)finishes.get(i); 3300 synchronized (mService) { 3301 destroyActivityLocked(r, true, false, "finish-idle"); 3302 } 3303 } 3304 3305 // Report back to any thumbnail receivers. 3306 for (i=0; i<NT; i++) { 3307 ActivityRecord r = (ActivityRecord)thumbnails.get(i); 3308 mService.sendPendingThumbnail(r, null, null, null, true); 3309 } 3310 3311 if (booting) { 3312 mService.finishBooting(); 3313 } 3314 3315 mService.trimApplications(); 3316 //dump(); 3317 //mWindowManager.dump(); 3318 3319 if (enableScreen) { 3320 mService.enableScreenAfterBoot(); 3321 } 3322 3323 return res; 3324 } 3325 3326 /** 3327 * @return Returns true if the activity is being finished, false if for 3328 * some reason it is being left as-is. 3329 */ 3330 final boolean requestFinishActivityLocked(IBinder token, int resultCode, 3331 Intent resultData, String reason) { 3332 int index = indexOfTokenLocked(token); 3333 if (DEBUG_RESULTS) Slog.v( 3334 TAG, "Finishing activity @" + index + ": token=" + token 3335 + ", result=" + resultCode + ", data=" + resultData); 3336 if (index < 0) { 3337 return false; 3338 } 3339 ActivityRecord r = mHistory.get(index); 3340 3341 finishActivityLocked(r, index, resultCode, resultData, reason); 3342 return true; 3343 } 3344 3345 /** 3346 * @return Returns true if this activity has been removed from the history 3347 * list, or false if it is still in the list and will be removed later. 3348 */ 3349 final boolean finishActivityLocked(ActivityRecord r, int index, 3350 int resultCode, Intent resultData, String reason) { 3351 if (r.finishing) { 3352 Slog.w(TAG, "Duplicate finish request for " + r); 3353 return false; 3354 } 3355 3356 r.makeFinishing(); 3357 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY, 3358 System.identityHashCode(r), 3359 r.task.taskId, r.shortComponentName, reason); 3360 if (index < (mHistory.size()-1)) { 3361 ActivityRecord next = mHistory.get(index+1); 3362 if (next.task == r.task) { 3363 if (r.frontOfTask) { 3364 // The next activity is now the front of the task. 3365 next.frontOfTask = true; 3366 } 3367 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) { 3368 // If the caller asked that this activity (and all above it) 3369 // be cleared when the task is reset, don't lose that information, 3370 // but propagate it up to the next activity. 3371 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 3372 } 3373 } 3374 } 3375 3376 r.pauseKeyDispatchingLocked(); 3377 if (mMainStack) { 3378 if (mService.mFocusedActivity == r) { 3379 mService.setFocusedActivityLocked(topRunningActivityLocked(null)); 3380 } 3381 } 3382 3383 // send the result 3384 ActivityRecord resultTo = r.resultTo; 3385 if (resultTo != null) { 3386 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo 3387 + " who=" + r.resultWho + " req=" + r.requestCode 3388 + " res=" + resultCode + " data=" + resultData); 3389 if (r.info.applicationInfo.uid > 0) { 3390 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid, 3391 resultTo.packageName, resultData, 3392 resultTo.getUriPermissionsLocked()); 3393 } 3394 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode, 3395 resultData); 3396 r.resultTo = null; 3397 } 3398 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r); 3399 3400 // Make sure this HistoryRecord is not holding on to other resources, 3401 // because clients have remote IPC references to this object so we 3402 // can't assume that will go away and want to avoid circular IPC refs. 3403 r.results = null; 3404 r.pendingResults = null; 3405 r.newIntents = null; 3406 r.icicle = null; 3407 3408 if (mService.mPendingThumbnails.size() > 0) { 3409 // There are clients waiting to receive thumbnails so, in case 3410 // this is an activity that someone is waiting for, add it 3411 // to the pending list so we can correctly update the clients. 3412 mService.mCancelledThumbnails.add(r); 3413 } 3414 3415 if (mResumedActivity == r) { 3416 boolean endTask = index <= 0 3417 || (mHistory.get(index-1)).task != r.task; 3418 if (DEBUG_TRANSITION) Slog.v(TAG, 3419 "Prepare close transition: finishing " + r); 3420 mService.mWindowManager.prepareAppTransition(endTask 3421 ? WindowManagerPolicy.TRANSIT_TASK_CLOSE 3422 : WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE, false); 3423 3424 // Tell window manager to prepare for this one to be removed. 3425 mService.mWindowManager.setAppVisibility(r.appToken, false); 3426 3427 if (mPausingActivity == null) { 3428 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r); 3429 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false"); 3430 startPausingLocked(false, false); 3431 } 3432 3433 } else if (r.state != ActivityState.PAUSING) { 3434 // If the activity is PAUSING, we will complete the finish once 3435 // it is done pausing; else we can just directly finish it here. 3436 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r); 3437 return finishCurrentActivityLocked(r, index, 3438 FINISH_AFTER_PAUSE) == null; 3439 } else { 3440 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r); 3441 } 3442 3443 return false; 3444 } 3445 3446 private static final int FINISH_IMMEDIATELY = 0; 3447 private static final int FINISH_AFTER_PAUSE = 1; 3448 private static final int FINISH_AFTER_VISIBLE = 2; 3449 3450 private final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, 3451 int mode) { 3452 final int index = indexOfActivityLocked(r); 3453 if (index < 0) { 3454 return null; 3455 } 3456 3457 return finishCurrentActivityLocked(r, index, mode); 3458 } 3459 3460 private final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, 3461 int index, int mode) { 3462 // First things first: if this activity is currently visible, 3463 // and the resumed activity is not yet visible, then hold off on 3464 // finishing until the resumed one becomes visible. 3465 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) { 3466 if (!mStoppingActivities.contains(r)) { 3467 mStoppingActivities.add(r); 3468 if (mStoppingActivities.size() > 3) { 3469 // If we already have a few activities waiting to stop, 3470 // then give up on things going idle and start clearing 3471 // them out. 3472 scheduleIdleLocked(); 3473 } else { 3474 checkReadyForSleepLocked(); 3475 } 3476 } 3477 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r 3478 + " (finish requested)"); 3479 r.state = ActivityState.STOPPING; 3480 mService.updateOomAdjLocked(); 3481 return r; 3482 } 3483 3484 // make sure the record is cleaned out of other places. 3485 mStoppingActivities.remove(r); 3486 mGoingToSleepActivities.remove(r); 3487 mWaitingVisibleActivities.remove(r); 3488 if (mResumedActivity == r) { 3489 mResumedActivity = null; 3490 } 3491 final ActivityState prevState = r.state; 3492 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r); 3493 r.state = ActivityState.FINISHING; 3494 3495 if (mode == FINISH_IMMEDIATELY 3496 || prevState == ActivityState.STOPPED 3497 || prevState == ActivityState.INITIALIZING) { 3498 // If this activity is already stopped, we can just finish 3499 // it right now. 3500 return destroyActivityLocked(r, true, true, "finish-imm") ? null : r; 3501 } else { 3502 // Need to go through the full pause cycle to get this 3503 // activity into the stopped state and then finish it. 3504 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r); 3505 mFinishingActivities.add(r); 3506 resumeTopActivityLocked(null); 3507 } 3508 return r; 3509 } 3510 3511 /** 3512 * Perform the common clean-up of an activity record. This is called both 3513 * as part of destroyActivityLocked() (when destroying the client-side 3514 * representation) and cleaning things up as a result of its hosting 3515 * processing going away, in which case there is no remaining client-side 3516 * state to destroy so only the cleanup here is needed. 3517 */ 3518 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices, 3519 boolean setState) { 3520 if (mResumedActivity == r) { 3521 mResumedActivity = null; 3522 } 3523 if (mService.mFocusedActivity == r) { 3524 mService.mFocusedActivity = null; 3525 } 3526 3527 r.configDestroy = false; 3528 r.frozenBeforeDestroy = false; 3529 3530 if (setState) { 3531 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)"); 3532 r.state = ActivityState.DESTROYED; 3533 } 3534 3535 // Make sure this record is no longer in the pending finishes list. 3536 // This could happen, for example, if we are trimming activities 3537 // down to the max limit while they are still waiting to finish. 3538 mFinishingActivities.remove(r); 3539 mWaitingVisibleActivities.remove(r); 3540 3541 // Remove any pending results. 3542 if (r.finishing && r.pendingResults != null) { 3543 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) { 3544 PendingIntentRecord rec = apr.get(); 3545 if (rec != null) { 3546 mService.cancelIntentSenderLocked(rec, false); 3547 } 3548 } 3549 r.pendingResults = null; 3550 } 3551 3552 if (cleanServices) { 3553 cleanUpActivityServicesLocked(r); 3554 } 3555 3556 if (mService.mPendingThumbnails.size() > 0) { 3557 // There are clients waiting to receive thumbnails so, in case 3558 // this is an activity that someone is waiting for, add it 3559 // to the pending list so we can correctly update the clients. 3560 mService.mCancelledThumbnails.add(r); 3561 } 3562 3563 // Get rid of any pending idle timeouts. 3564 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r); 3565 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r); 3566 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r); 3567 } 3568 3569 private final void removeActivityFromHistoryLocked(ActivityRecord r) { 3570 if (r.state != ActivityState.DESTROYED) { 3571 r.makeFinishing(); 3572 if (DEBUG_ADD_REMOVE) { 3573 RuntimeException here = new RuntimeException("here"); 3574 here.fillInStackTrace(); 3575 Slog.i(TAG, "Removing activity " + r + " from stack"); 3576 } 3577 mHistory.remove(r); 3578 r.takeFromHistory(); 3579 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r 3580 + " (removed from history)"); 3581 r.state = ActivityState.DESTROYED; 3582 mService.mWindowManager.removeAppToken(r.appToken); 3583 if (VALIDATE_TOKENS) { 3584 validateAppTokensLocked(); 3585 } 3586 cleanUpActivityServicesLocked(r); 3587 r.removeUriPermissionsLocked(); 3588 } 3589 } 3590 3591 /** 3592 * Perform clean-up of service connections in an activity record. 3593 */ 3594 final void cleanUpActivityServicesLocked(ActivityRecord r) { 3595 // Throw away any services that have been bound by this activity. 3596 if (r.connections != null) { 3597 Iterator<ConnectionRecord> it = r.connections.iterator(); 3598 while (it.hasNext()) { 3599 ConnectionRecord c = it.next(); 3600 mService.removeConnectionLocked(c, null, r); 3601 } 3602 r.connections = null; 3603 } 3604 } 3605 3606 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) { 3607 for (int i=mHistory.size()-1; i>=0; i--) { 3608 ActivityRecord r = mHistory.get(i); 3609 if (owner != null && r.app != owner) { 3610 continue; 3611 } 3612 // We can destroy this one if we have its icicle saved and 3613 // it is not in the process of pausing/stopping/finishing. 3614 if (r.app != null && r.haveState && !r.visible && r.stopped && !r.finishing 3615 && r.state != ActivityState.DESTROYING 3616 && r.state != ActivityState.DESTROYED) { 3617 destroyActivityLocked(r, true, oomAdj, "trim"); 3618 } 3619 } 3620 } 3621 3622 /** 3623 * Destroy the current CLIENT SIDE instance of an activity. This may be 3624 * called both when actually finishing an activity, or when performing 3625 * a configuration switch where we destroy the current client-side object 3626 * but then create a new client-side object for this same HistoryRecord. 3627 */ 3628 final boolean destroyActivityLocked(ActivityRecord r, 3629 boolean removeFromApp, boolean oomAdj, String reason) { 3630 if (DEBUG_SWITCH) Slog.v( 3631 TAG, "Removing activity: token=" + r 3632 + ", app=" + (r.app != null ? r.app.processName : "(null)")); 3633 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY, 3634 System.identityHashCode(r), 3635 r.task.taskId, r.shortComponentName, reason); 3636 3637 boolean removedFromHistory = false; 3638 3639 cleanUpActivityLocked(r, false, false); 3640 3641 final boolean hadApp = r.app != null; 3642 3643 if (hadApp) { 3644 if (removeFromApp) { 3645 int idx = r.app.activities.indexOf(r); 3646 if (idx >= 0) { 3647 r.app.activities.remove(idx); 3648 } 3649 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) { 3650 mService.mHeavyWeightProcess = null; 3651 mService.mHandler.sendEmptyMessage( 3652 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG); 3653 } 3654 if (r.app.activities.size() == 0) { 3655 // No longer have activities, so update location in 3656 // LRU list. 3657 mService.updateLruProcessLocked(r.app, oomAdj, false); 3658 } 3659 } 3660 3661 boolean skipDestroy = false; 3662 3663 try { 3664 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r); 3665 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing, 3666 r.configChangeFlags); 3667 } catch (Exception e) { 3668 // We can just ignore exceptions here... if the process 3669 // has crashed, our death notification will clean things 3670 // up. 3671 //Slog.w(TAG, "Exception thrown during finish", e); 3672 if (r.finishing) { 3673 removeActivityFromHistoryLocked(r); 3674 removedFromHistory = true; 3675 skipDestroy = true; 3676 } 3677 } 3678 3679 r.app = null; 3680 r.nowVisible = false; 3681 3682 // If the activity is finishing, we need to wait on removing it 3683 // from the list to give it a chance to do its cleanup. During 3684 // that time it may make calls back with its token so we need to 3685 // be able to find it on the list and so we don't want to remove 3686 // it from the list yet. Otherwise, we can just immediately put 3687 // it in the destroyed state since we are not removing it from the 3688 // list. 3689 if (r.finishing && !skipDestroy) { 3690 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r 3691 + " (destroy requested)"); 3692 r.state = ActivityState.DESTROYING; 3693 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG); 3694 msg.obj = r; 3695 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT); 3696 } else { 3697 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r 3698 + " (destroy skipped)"); 3699 r.state = ActivityState.DESTROYED; 3700 } 3701 } else { 3702 // remove this record from the history. 3703 if (r.finishing) { 3704 removeActivityFromHistoryLocked(r); 3705 removedFromHistory = true; 3706 } else { 3707 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r 3708 + " (no app)"); 3709 r.state = ActivityState.DESTROYED; 3710 } 3711 } 3712 3713 r.configChangeFlags = 0; 3714 3715 if (!mLRUActivities.remove(r) && hadApp) { 3716 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list"); 3717 } 3718 3719 return removedFromHistory; 3720 } 3721 3722 final void activityDestroyed(IBinder token) { 3723 synchronized (mService) { 3724 ActivityRecord r = ActivityRecord.forToken(token); 3725 if (r != null) { 3726 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r); 3727 } 3728 3729 int index = indexOfActivityLocked(r); 3730 if (index >= 0) { 3731 if (r.state == ActivityState.DESTROYING) { 3732 final long origId = Binder.clearCallingIdentity(); 3733 removeActivityFromHistoryLocked(r); 3734 Binder.restoreCallingIdentity(origId); 3735 } 3736 } 3737 } 3738 } 3739 3740 private static void removeHistoryRecordsForAppLocked(ArrayList list, ProcessRecord app) { 3741 int i = list.size(); 3742 if (localLOGV) Slog.v( 3743 TAG, "Removing app " + app + " from list " + list 3744 + " with " + i + " entries"); 3745 while (i > 0) { 3746 i--; 3747 ActivityRecord r = (ActivityRecord)list.get(i); 3748 if (localLOGV) Slog.v( 3749 TAG, "Record #" + i + " " + r + ": app=" + r.app); 3750 if (r.app == app) { 3751 if (localLOGV) Slog.v(TAG, "Removing this entry!"); 3752 list.remove(i); 3753 } 3754 } 3755 } 3756 3757 void removeHistoryRecordsForAppLocked(ProcessRecord app) { 3758 removeHistoryRecordsForAppLocked(mLRUActivities, app); 3759 removeHistoryRecordsForAppLocked(mStoppingActivities, app); 3760 removeHistoryRecordsForAppLocked(mGoingToSleepActivities, app); 3761 removeHistoryRecordsForAppLocked(mWaitingVisibleActivities, app); 3762 removeHistoryRecordsForAppLocked(mFinishingActivities, app); 3763 } 3764 3765 /** 3766 * Move the current home activity's task (if one exists) to the front 3767 * of the stack. 3768 */ 3769 final void moveHomeToFrontLocked() { 3770 TaskRecord homeTask = null; 3771 for (int i=mHistory.size()-1; i>=0; i--) { 3772 ActivityRecord hr = mHistory.get(i); 3773 if (hr.isHomeActivity) { 3774 homeTask = hr.task; 3775 break; 3776 } 3777 } 3778 if (homeTask != null) { 3779 moveTaskToFrontLocked(homeTask, null); 3780 } 3781 } 3782 3783 3784 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason) { 3785 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr); 3786 3787 final int task = tr.taskId; 3788 int top = mHistory.size()-1; 3789 3790 if (top < 0 || (mHistory.get(top)).task.taskId == task) { 3791 // nothing to do! 3792 return; 3793 } 3794 3795 ArrayList<IBinder> moved = new ArrayList<IBinder>(); 3796 3797 // Applying the affinities may have removed entries from the history, 3798 // so get the size again. 3799 top = mHistory.size()-1; 3800 int pos = top; 3801 3802 // Shift all activities with this task up to the top 3803 // of the stack, keeping them in the same internal order. 3804 while (pos >= 0) { 3805 ActivityRecord r = mHistory.get(pos); 3806 if (localLOGV) Slog.v( 3807 TAG, "At " + pos + " ckp " + r.task + ": " + r); 3808 if (r.task.taskId == task) { 3809 if (localLOGV) Slog.v(TAG, "Removing and adding at " + top); 3810 if (DEBUG_ADD_REMOVE) { 3811 RuntimeException here = new RuntimeException("here"); 3812 here.fillInStackTrace(); 3813 Slog.i(TAG, "Removing and adding activity " + r + " to stack at " + top, here); 3814 } 3815 mHistory.remove(pos); 3816 mHistory.add(top, r); 3817 moved.add(0, r.appToken); 3818 top--; 3819 } 3820 pos--; 3821 } 3822 3823 if (DEBUG_TRANSITION) Slog.v(TAG, 3824 "Prepare to front transition: task=" + tr); 3825 if (reason != null && 3826 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) { 3827 mService.mWindowManager.prepareAppTransition( 3828 WindowManagerPolicy.TRANSIT_NONE, false); 3829 ActivityRecord r = topRunningActivityLocked(null); 3830 if (r != null) { 3831 mNoAnimActivities.add(r); 3832 } 3833 } else { 3834 mService.mWindowManager.prepareAppTransition( 3835 WindowManagerPolicy.TRANSIT_TASK_TO_FRONT, false); 3836 } 3837 3838 mService.mWindowManager.moveAppTokensToTop(moved); 3839 if (VALIDATE_TOKENS) { 3840 validateAppTokensLocked(); 3841 } 3842 3843 finishTaskMoveLocked(task); 3844 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, task); 3845 } 3846 3847 private final void finishTaskMoveLocked(int task) { 3848 resumeTopActivityLocked(null); 3849 } 3850 3851 /** 3852 * Worker method for rearranging history stack. Implements the function of moving all 3853 * activities for a specific task (gathering them if disjoint) into a single group at the 3854 * bottom of the stack. 3855 * 3856 * If a watcher is installed, the action is preflighted and the watcher has an opportunity 3857 * to premeptively cancel the move. 3858 * 3859 * @param task The taskId to collect and move to the bottom. 3860 * @return Returns true if the move completed, false if not. 3861 */ 3862 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) { 3863 Slog.i(TAG, "moveTaskToBack: " + task); 3864 3865 // If we have a watcher, preflight the move before committing to it. First check 3866 // for *other* available tasks, but if none are available, then try again allowing the 3867 // current task to be selected. 3868 if (mMainStack && mService.mController != null) { 3869 ActivityRecord next = topRunningActivityLocked(null, task); 3870 if (next == null) { 3871 next = topRunningActivityLocked(null, 0); 3872 } 3873 if (next != null) { 3874 // ask watcher if this is allowed 3875 boolean moveOK = true; 3876 try { 3877 moveOK = mService.mController.activityResuming(next.packageName); 3878 } catch (RemoteException e) { 3879 mService.mController = null; 3880 } 3881 if (!moveOK) { 3882 return false; 3883 } 3884 } 3885 } 3886 3887 ArrayList<IBinder> moved = new ArrayList<IBinder>(); 3888 3889 if (DEBUG_TRANSITION) Slog.v(TAG, 3890 "Prepare to back transition: task=" + task); 3891 3892 final int N = mHistory.size(); 3893 int bottom = 0; 3894 int pos = 0; 3895 3896 // Shift all activities with this task down to the bottom 3897 // of the stack, keeping them in the same internal order. 3898 while (pos < N) { 3899 ActivityRecord r = mHistory.get(pos); 3900 if (localLOGV) Slog.v( 3901 TAG, "At " + pos + " ckp " + r.task + ": " + r); 3902 if (r.task.taskId == task) { 3903 if (localLOGV) Slog.v(TAG, "Removing and adding at " + (N-1)); 3904 if (DEBUG_ADD_REMOVE) { 3905 RuntimeException here = new RuntimeException("here"); 3906 here.fillInStackTrace(); 3907 Slog.i(TAG, "Removing and adding activity " + r + " to stack at " 3908 + bottom, here); 3909 } 3910 mHistory.remove(pos); 3911 mHistory.add(bottom, r); 3912 moved.add(r.appToken); 3913 bottom++; 3914 } 3915 pos++; 3916 } 3917 3918 if (reason != null && 3919 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) { 3920 mService.mWindowManager.prepareAppTransition( 3921 WindowManagerPolicy.TRANSIT_NONE, false); 3922 ActivityRecord r = topRunningActivityLocked(null); 3923 if (r != null) { 3924 mNoAnimActivities.add(r); 3925 } 3926 } else { 3927 mService.mWindowManager.prepareAppTransition( 3928 WindowManagerPolicy.TRANSIT_TASK_TO_BACK, false); 3929 } 3930 mService.mWindowManager.moveAppTokensToBottom(moved); 3931 if (VALIDATE_TOKENS) { 3932 validateAppTokensLocked(); 3933 } 3934 3935 finishTaskMoveLocked(task); 3936 return true; 3937 } 3938 3939 public ActivityManager.TaskThumbnails getTaskThumbnailsLocked(TaskRecord tr) { 3940 TaskAccessInfo info = getTaskAccessInfoLocked(tr.taskId, true); 3941 ActivityRecord resumed = mResumedActivity; 3942 if (resumed != null && resumed.thumbHolder == tr) { 3943 info.mainThumbnail = resumed.stack.screenshotActivities(resumed); 3944 } else { 3945 info.mainThumbnail = tr.lastThumbnail; 3946 } 3947 return info; 3948 } 3949 3950 public ActivityRecord removeTaskActivitiesLocked(int taskId, int subTaskIndex) { 3951 TaskAccessInfo info = getTaskAccessInfoLocked(taskId, false); 3952 if (info.root == null) { 3953 Slog.w(TAG, "removeTaskLocked: unknown taskId " + taskId); 3954 return null; 3955 } 3956 3957 if (subTaskIndex < 0) { 3958 // Just remove the entire task. 3959 performClearTaskAtIndexLocked(taskId, info.rootIndex); 3960 return info.root; 3961 } 3962 3963 if (subTaskIndex >= info.subtasks.size()) { 3964 Slog.w(TAG, "removeTaskLocked: unknown subTaskIndex " + subTaskIndex); 3965 return null; 3966 } 3967 3968 // Remove all of this task's activies starting at the sub task. 3969 TaskAccessInfo.SubTask subtask = info.subtasks.get(subTaskIndex); 3970 performClearTaskAtIndexLocked(taskId, subtask.index); 3971 return subtask.activity; 3972 } 3973 3974 public TaskAccessInfo getTaskAccessInfoLocked(int taskId, boolean inclThumbs) { 3975 ActivityRecord resumed = mResumedActivity; 3976 final TaskAccessInfo thumbs = new TaskAccessInfo(); 3977 // How many different sub-thumbnails? 3978 final int NA = mHistory.size(); 3979 int j = 0; 3980 ThumbnailHolder holder = null; 3981 while (j < NA) { 3982 ActivityRecord ar = mHistory.get(j); 3983 if (!ar.finishing && ar.task.taskId == taskId) { 3984 holder = ar.thumbHolder; 3985 break; 3986 } 3987 j++; 3988 } 3989 3990 if (j >= NA) { 3991 return thumbs; 3992 } 3993 3994 thumbs.root = mHistory.get(j); 3995 thumbs.rootIndex = j; 3996 3997 ArrayList<TaskAccessInfo.SubTask> subtasks = new ArrayList<TaskAccessInfo.SubTask>(); 3998 thumbs.subtasks = subtasks; 3999 ActivityRecord lastActivity = null; 4000 while (j < NA) { 4001 ActivityRecord ar = mHistory.get(j); 4002 j++; 4003 if (ar.finishing) { 4004 continue; 4005 } 4006 if (ar.task.taskId != taskId) { 4007 break; 4008 } 4009 lastActivity = ar; 4010 if (ar.thumbHolder != holder && holder != null) { 4011 thumbs.numSubThumbbails++; 4012 holder = ar.thumbHolder; 4013 TaskAccessInfo.SubTask sub = new TaskAccessInfo.SubTask(); 4014 sub.thumbnail = holder.lastThumbnail; 4015 sub.activity = ar; 4016 sub.index = j-1; 4017 subtasks.add(sub); 4018 } 4019 } 4020 if (lastActivity != null && subtasks.size() > 0) { 4021 if (resumed == lastActivity) { 4022 TaskAccessInfo.SubTask sub = subtasks.get(subtasks.size()-1); 4023 sub.thumbnail = lastActivity.stack.screenshotActivities(lastActivity); 4024 } 4025 } 4026 if (thumbs.numSubThumbbails > 0) { 4027 thumbs.retriever = new IThumbnailRetriever.Stub() { 4028 public Bitmap getThumbnail(int index) { 4029 if (index < 0 || index >= thumbs.subtasks.size()) { 4030 return null; 4031 } 4032 return thumbs.subtasks.get(index).thumbnail; 4033 } 4034 }; 4035 } 4036 return thumbs; 4037 } 4038 4039 private final void logStartActivity(int tag, ActivityRecord r, 4040 TaskRecord task) { 4041 EventLog.writeEvent(tag, 4042 System.identityHashCode(r), task.taskId, 4043 r.shortComponentName, r.intent.getAction(), 4044 r.intent.getType(), r.intent.getDataString(), 4045 r.intent.getFlags()); 4046 } 4047 4048 /** 4049 * Make sure the given activity matches the current configuration. Returns 4050 * false if the activity had to be destroyed. Returns true if the 4051 * configuration is the same, or the activity will remain running as-is 4052 * for whatever reason. Ensures the HistoryRecord is updated with the 4053 * correct configuration and all other bookkeeping is handled. 4054 */ 4055 final boolean ensureActivityConfigurationLocked(ActivityRecord r, 4056 int globalChanges) { 4057 if (mConfigWillChange) { 4058 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, 4059 "Skipping config check (will change): " + r); 4060 return true; 4061 } 4062 4063 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, 4064 "Ensuring correct configuration: " + r); 4065 4066 // Short circuit: if the two configurations are the exact same 4067 // object (the common case), then there is nothing to do. 4068 Configuration newConfig = mService.mConfiguration; 4069 if (r.configuration == newConfig && !r.forceNewConfig) { 4070 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, 4071 "Configuration unchanged in " + r); 4072 return true; 4073 } 4074 4075 // We don't worry about activities that are finishing. 4076 if (r.finishing) { 4077 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, 4078 "Configuration doesn't matter in finishing " + r); 4079 r.stopFreezingScreenLocked(false); 4080 return true; 4081 } 4082 4083 // Okay we now are going to make this activity have the new config. 4084 // But then we need to figure out how it needs to deal with that. 4085 Configuration oldConfig = r.configuration; 4086 r.configuration = newConfig; 4087 4088 // Determine what has changed. May be nothing, if this is a config 4089 // that has come back from the app after going idle. In that case 4090 // we just want to leave the official config object now in the 4091 // activity and do nothing else. 4092 final int changes = oldConfig.diff(newConfig); 4093 if (changes == 0 && !r.forceNewConfig) { 4094 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, 4095 "Configuration no differences in " + r); 4096 return true; 4097 } 4098 4099 // If the activity isn't currently running, just leave the new 4100 // configuration and it will pick that up next time it starts. 4101 if (r.app == null || r.app.thread == null) { 4102 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, 4103 "Configuration doesn't matter not running " + r); 4104 r.stopFreezingScreenLocked(false); 4105 r.forceNewConfig = false; 4106 return true; 4107 } 4108 4109 // Figure out how to handle the changes between the configurations. 4110 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) { 4111 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x" 4112 + Integer.toHexString(changes) + ", handles=0x" 4113 + Integer.toHexString(r.info.getRealConfigChanged()) 4114 + ", newConfig=" + newConfig); 4115 } 4116 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) { 4117 // Aha, the activity isn't handling the change, so DIE DIE DIE. 4118 r.configChangeFlags |= changes; 4119 r.startFreezingScreenLocked(r.app, globalChanges); 4120 r.forceNewConfig = false; 4121 if (r.app == null || r.app.thread == null) { 4122 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, 4123 "Switch is destroying non-running " + r); 4124 destroyActivityLocked(r, true, false, "config"); 4125 } else if (r.state == ActivityState.PAUSING) { 4126 // A little annoying: we are waiting for this activity to 4127 // finish pausing. Let's not do anything now, but just 4128 // flag that it needs to be restarted when done pausing. 4129 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, 4130 "Switch is skipping already pausing " + r); 4131 r.configDestroy = true; 4132 return true; 4133 } else if (r.state == ActivityState.RESUMED) { 4134 // Try to optimize this case: the configuration is changing 4135 // and we need to restart the top, resumed activity. 4136 // Instead of doing the normal handshaking, just say 4137 // "restart!". 4138 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, 4139 "Switch is restarting resumed " + r); 4140 relaunchActivityLocked(r, r.configChangeFlags, true); 4141 r.configChangeFlags = 0; 4142 } else { 4143 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG, 4144 "Switch is restarting non-resumed " + r); 4145 relaunchActivityLocked(r, r.configChangeFlags, false); 4146 r.configChangeFlags = 0; 4147 } 4148 4149 // All done... tell the caller we weren't able to keep this 4150 // activity around. 4151 return false; 4152 } 4153 4154 // Default case: the activity can handle this new configuration, so 4155 // hand it over. Note that we don't need to give it the new 4156 // configuration, since we always send configuration changes to all 4157 // process when they happen so it can just use whatever configuration 4158 // it last got. 4159 if (r.app != null && r.app.thread != null) { 4160 try { 4161 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r); 4162 r.app.thread.scheduleActivityConfigurationChanged(r.appToken); 4163 } catch (RemoteException e) { 4164 // If process died, whatever. 4165 } 4166 } 4167 r.stopFreezingScreenLocked(false); 4168 4169 return true; 4170 } 4171 4172 private final boolean relaunchActivityLocked(ActivityRecord r, 4173 int changes, boolean andResume) { 4174 List<ResultInfo> results = null; 4175 List<Intent> newIntents = null; 4176 if (andResume) { 4177 results = r.results; 4178 newIntents = r.newIntents; 4179 } 4180 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r 4181 + " with results=" + results + " newIntents=" + newIntents 4182 + " andResume=" + andResume); 4183 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY 4184 : EventLogTags.AM_RELAUNCH_ACTIVITY, System.identityHashCode(r), 4185 r.task.taskId, r.shortComponentName); 4186 4187 r.startFreezingScreenLocked(r.app, 0); 4188 4189 try { 4190 if (DEBUG_SWITCH) Slog.i(TAG, "Switch is restarting resumed " + r); 4191 r.forceNewConfig = false; 4192 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, 4193 changes, !andResume, mService.mConfiguration); 4194 // Note: don't need to call pauseIfSleepingLocked() here, because 4195 // the caller will only pass in 'andResume' if this activity is 4196 // currently resumed, which implies we aren't sleeping. 4197 } catch (RemoteException e) { 4198 return false; 4199 } 4200 4201 if (andResume) { 4202 r.results = null; 4203 r.newIntents = null; 4204 if (mMainStack) { 4205 mService.reportResumedActivityLocked(r); 4206 } 4207 } 4208 4209 return true; 4210 } 4211 4212 public void dismissKeyguardOnNextActivityLocked() { 4213 mDismissKeyguardOnNextActivity = true; 4214 } 4215} 4216