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