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