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