blob: e6f4d9184fa33debd3084af1b5a7d8e0146b1724 [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
Jeff Sharkey35be7562012-04-18 19:16:15 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
20import static android.content.pm.PackageManager.PERMISSION_GRANTED;
21
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070022import com.android.internal.app.HeavyWeightSwitcherActivity;
23import com.android.internal.os.BatteryStatsImpl;
24import com.android.server.am.ActivityManagerService.PendingActivityLaunch;
25
26import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070027import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070028import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070029import android.app.AppGlobals;
30import android.app.IActivityManager;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070031import android.app.IThumbnailRetriever;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070032import android.app.IApplicationThread;
33import android.app.PendingIntent;
34import android.app.ResultInfo;
35import android.app.IActivityManager.WaitResult;
36import android.content.ComponentName;
37import android.content.Context;
38import android.content.IIntentSender;
39import android.content.Intent;
40import android.content.IntentSender;
41import android.content.pm.ActivityInfo;
42import android.content.pm.ApplicationInfo;
43import android.content.pm.PackageManager;
44import android.content.pm.ResolveInfo;
45import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080046import android.content.res.Resources;
47import android.graphics.Bitmap;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070048import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070049import android.os.Bundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070050import android.os.Handler;
51import android.os.IBinder;
52import android.os.Message;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070053import android.os.ParcelFileDescriptor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054import android.os.PowerManager;
55import android.os.RemoteException;
56import android.os.SystemClock;
Amith Yamasani742a6712011-05-04 14:49:28 -070057import android.os.UserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.util.EventLog;
59import android.util.Log;
60import android.util.Slog;
61import android.view.WindowManagerPolicy;
62
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070063import java.io.IOException;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import java.lang.ref.WeakReference;
65import java.util.ArrayList;
66import java.util.Iterator;
67import java.util.List;
68
69/**
70 * State and management of a single stack of activities.
71 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070072final class ActivityStack {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070073 static final String TAG = ActivityManagerService.TAG;
Dianne Hackbornb961cd22011-06-21 12:13:37 -070074 static final boolean localLOGV = ActivityManagerService.localLOGV;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075 static final boolean DEBUG_SWITCH = ActivityManagerService.DEBUG_SWITCH;
76 static final boolean DEBUG_PAUSE = ActivityManagerService.DEBUG_PAUSE;
77 static final boolean DEBUG_VISBILITY = ActivityManagerService.DEBUG_VISBILITY;
78 static final boolean DEBUG_USER_LEAVING = ActivityManagerService.DEBUG_USER_LEAVING;
79 static final boolean DEBUG_TRANSITION = ActivityManagerService.DEBUG_TRANSITION;
80 static final boolean DEBUG_RESULTS = ActivityManagerService.DEBUG_RESULTS;
81 static final boolean DEBUG_CONFIGURATION = ActivityManagerService.DEBUG_CONFIGURATION;
82 static final boolean DEBUG_TASKS = ActivityManagerService.DEBUG_TASKS;
83
Dianne Hackbornce86ba82011-07-13 19:33:41 -070084 static final boolean DEBUG_STATES = false;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070085 static final boolean DEBUG_ADD_REMOVE = false;
86 static final boolean DEBUG_SAVED_STATE = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070087
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070088 static final boolean VALIDATE_TOKENS = ActivityManagerService.VALIDATE_TOKENS;
89
90 // How long we wait until giving up on the last activity telling us it
91 // is idle.
92 static final int IDLE_TIMEOUT = 10*1000;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070093
94 // Ticks during which we check progress while waiting for an app to launch.
95 static final int LAUNCH_TICK = 500;
96
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070097 // 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 Hackborn162bc0e2012-04-09 14:06:16 -0700102 // How long we wait for the activity to tell us it has stopped before
103 // giving up. This is a good amount of time because we really need this
104 // from the application in order to get its saved state.
105 static final int STOP_TIMEOUT = 10*1000;
106
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800107 // How long we can hold the sleep wake lock before giving up.
108 static final int SLEEP_TIMEOUT = 5*1000;
109
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700110 // How long we can hold the launch wake lock before giving up.
111 static final int LAUNCH_TIMEOUT = 10*1000;
112
113 // How long we wait until giving up on an activity telling us it has
114 // finished destroying itself.
115 static final int DESTROY_TIMEOUT = 10*1000;
116
117 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800118 // disabled.
119 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700120
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700121 // How long between activity launches that we consider safe to not warn
122 // the user about an unexpected activity being launched on top.
123 static final long START_WARN_TIME = 5*1000;
124
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700125 // Set to false to disable the preview that is shown while a new activity
126 // is being started.
127 static final boolean SHOW_APP_STARTING_PREVIEW = true;
128
129 enum ActivityState {
130 INITIALIZING,
131 RESUMED,
132 PAUSING,
133 PAUSED,
134 STOPPING,
135 STOPPED,
136 FINISHING,
137 DESTROYING,
138 DESTROYED
139 }
140
141 final ActivityManagerService mService;
142 final boolean mMainStack;
143
144 final Context mContext;
145
146 /**
147 * The back history of all previous (and possibly still
148 * running) activities. It contains HistoryRecord objects.
149 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700150 final ArrayList<ActivityRecord> mHistory = new ArrayList<ActivityRecord>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800151
152 /**
153 * Used for validating app tokens with window manager.
154 */
155 final ArrayList<IBinder> mValidateAppTokens = new ArrayList<IBinder>();
156
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700157 /**
158 * List of running activities, sorted by recent usage.
159 * The first entry in the list is the least recently used.
160 * It contains HistoryRecord objects.
161 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700162 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700163
164 /**
165 * List of activities that are waiting for a new activity
166 * to become visible before completing whatever operation they are
167 * supposed to do.
168 */
169 final ArrayList<ActivityRecord> mWaitingVisibleActivities
170 = new ArrayList<ActivityRecord>();
171
172 /**
173 * List of activities that are ready to be stopped, but waiting
174 * for the next activity to settle down before doing so. It contains
175 * HistoryRecord objects.
176 */
177 final ArrayList<ActivityRecord> mStoppingActivities
178 = new ArrayList<ActivityRecord>();
179
180 /**
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800181 * List of activities that are in the process of going to sleep.
182 */
183 final ArrayList<ActivityRecord> mGoingToSleepActivities
184 = new ArrayList<ActivityRecord>();
185
186 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700187 * Animations that for the current transition have requested not to
188 * be considered for the transition animation.
189 */
190 final ArrayList<ActivityRecord> mNoAnimActivities
191 = new ArrayList<ActivityRecord>();
192
193 /**
194 * List of activities that are ready to be finished, but waiting
195 * for the previous activity to settle down before doing so. It contains
196 * HistoryRecord objects.
197 */
198 final ArrayList<ActivityRecord> mFinishingActivities
199 = new ArrayList<ActivityRecord>();
200
201 /**
202 * List of people waiting to find out about the next launched activity.
203 */
204 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched
205 = new ArrayList<IActivityManager.WaitResult>();
206
207 /**
208 * List of people waiting to find out about the next visible activity.
209 */
210 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible
211 = new ArrayList<IActivityManager.WaitResult>();
212
213 /**
214 * Set when the system is going to sleep, until we have
215 * successfully paused the current activity and released our wake lock.
216 * At that point the system is allowed to actually sleep.
217 */
218 final PowerManager.WakeLock mGoingToSleep;
219
220 /**
221 * We don't want to allow the device to go to sleep while in the process
222 * of launching an activity. This is primarily to allow alarm intent
223 * receivers to launch an activity and get that to run before the device
224 * goes back to sleep.
225 */
226 final PowerManager.WakeLock mLaunchingActivity;
227
228 /**
229 * When we are in the process of pausing an activity, before starting the
230 * next one, this variable holds the activity that is currently being paused.
231 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800232 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700233
234 /**
235 * This is the last activity that we put into the paused state. This is
236 * used to determine if we need to do an activity transition while sleeping,
237 * when we normally hold the top activity paused.
238 */
239 ActivityRecord mLastPausedActivity = null;
240
241 /**
242 * Current activity that is resumed, or null if there is none.
243 */
244 ActivityRecord mResumedActivity = null;
245
246 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700247 * This is the last activity that has been started. It is only used to
248 * identify when multiple activities are started at once so that the user
249 * can be warned they may not be in the activity they think they are.
250 */
251 ActivityRecord mLastStartedActivity = null;
252
253 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700254 * Set when we know we are going to be calling updateConfiguration()
255 * soon, so want to skip intermediate config checks.
256 */
257 boolean mConfigWillChange;
258
259 /**
260 * Set to indicate whether to issue an onUserLeaving callback when a
261 * newly launched activity is being brought in front of us.
262 */
263 boolean mUserLeaving = false;
264
265 long mInitialStartTime = 0;
266
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800267 /**
268 * Set when we have taken too long waiting to go to sleep.
269 */
270 boolean mSleepTimeout = false;
271
Dianne Hackborn90c52de2011-09-23 12:57:44 -0700272 /**
273 * Dismiss the keyguard after the next activity is displayed?
274 */
275 boolean mDismissKeyguardOnNextActivity = false;
276
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800277 int mThumbnailWidth = -1;
278 int mThumbnailHeight = -1;
279
Amith Yamasani742a6712011-05-04 14:49:28 -0700280 private int mCurrentUser;
281
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800282 static final int SLEEP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG;
283 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
284 static final int IDLE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
285 static final int IDLE_NOW_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
286 static final int LAUNCH_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
287 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
288 static final int RESUME_TOP_ACTIVITY_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn29ba7e62012-03-16 15:03:36 -0700289 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700290 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 8;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700291 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 9;
292
293 static class ScheduleDestroyArgs {
294 final ProcessRecord mOwner;
295 final boolean mOomAdj;
296 final String mReason;
297 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
298 mOwner = owner;
299 mOomAdj = oomAdj;
300 mReason = reason;
301 }
302 }
303
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700304 final Handler mHandler = new Handler() {
305 //public Handler() {
306 // if (localLOGV) Slog.v(TAG, "Handler started!");
307 //}
308
309 public void handleMessage(Message msg) {
310 switch (msg.what) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800311 case SLEEP_TIMEOUT_MSG: {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700312 synchronized (mService) {
313 if (mService.isSleeping()) {
314 Slog.w(TAG, "Sleep timeout! Sleeping now.");
315 mSleepTimeout = true;
316 checkReadyForSleepLocked();
317 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800318 }
319 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700320 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800321 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700322 // We don't at this point know if the activity is fullscreen,
323 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800324 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700325 synchronized (mService) {
326 if (r.app != null) {
327 mService.logAppTooSlow(r.app, r.pauseTime,
328 "pausing " + r);
329 }
330 }
331
Dianne Hackbornbe707852011-11-11 14:32:10 -0800332 activityPaused(r != null ? r.appToken : null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700333 } break;
334 case IDLE_TIMEOUT_MSG: {
335 if (mService.mDidDexOpt) {
336 mService.mDidDexOpt = false;
337 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
338 nmsg.obj = msg.obj;
339 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
340 return;
341 }
342 // We don't at this point know if the activity is fullscreen,
343 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800344 ActivityRecord r = (ActivityRecord)msg.obj;
345 Slog.w(TAG, "Activity idle timeout for " + r);
346 activityIdleInternal(r != null ? r.appToken : null, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700347 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700348 case LAUNCH_TICK_MSG: {
349 ActivityRecord r = (ActivityRecord)msg.obj;
350 synchronized (mService) {
351 if (r.continueLaunchTickingLocked()) {
352 mService.logAppTooSlow(r.app, r.launchTickTime,
353 "launching " + r);
354 }
355 }
356 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700357 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800358 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700359 // We don't at this point know if the activity is fullscreen,
360 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800361 Slog.w(TAG, "Activity destroy timeout for " + r);
362 activityDestroyed(r != null ? r.appToken : null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700363 } break;
364 case IDLE_NOW_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800365 ActivityRecord r = (ActivityRecord)msg.obj;
366 activityIdleInternal(r != null ? r.appToken : null, false, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700367 } break;
368 case LAUNCH_TIMEOUT_MSG: {
369 if (mService.mDidDexOpt) {
370 mService.mDidDexOpt = false;
371 Message nmsg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
372 mHandler.sendMessageDelayed(nmsg, LAUNCH_TIMEOUT);
373 return;
374 }
375 synchronized (mService) {
376 if (mLaunchingActivity.isHeld()) {
377 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
378 mLaunchingActivity.release();
379 }
380 }
381 } break;
382 case RESUME_TOP_ACTIVITY_MSG: {
383 synchronized (mService) {
384 resumeTopActivityLocked(null);
385 }
386 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700387 case STOP_TIMEOUT_MSG: {
388 ActivityRecord r = (ActivityRecord)msg.obj;
389 // We don't at this point know if the activity is fullscreen,
390 // so we need to be conservative and assume it isn't.
391 Slog.w(TAG, "Activity stop timeout for " + r);
392 synchronized (mService) {
393 if (r.isInHistory()) {
394 activityStoppedLocked(r, null, null, null);
395 }
396 }
397 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700398 case DESTROY_ACTIVITIES_MSG: {
399 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
400 synchronized (mService) {
401 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
402 }
403 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700404 }
405 }
406 };
407
408 ActivityStack(ActivityManagerService service, Context context, boolean mainStack) {
409 mService = service;
410 mContext = context;
411 mMainStack = mainStack;
412 PowerManager pm =
413 (PowerManager)context.getSystemService(Context.POWER_SERVICE);
414 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
415 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
416 mLaunchingActivity.setReferenceCounted(false);
417 }
418
419 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700420 // TODO: Don't look for any tasks from other users
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700421 int i = mHistory.size()-1;
422 while (i >= 0) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700423 ActivityRecord r = mHistory.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700424 if (!r.finishing && r != notTop) {
425 return r;
426 }
427 i--;
428 }
429 return null;
430 }
431
432 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700433 // TODO: Don't look for any tasks from other users
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700434 int i = mHistory.size()-1;
435 while (i >= 0) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700436 ActivityRecord r = mHistory.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700437 if (!r.finishing && !r.delayedResume && r != notTop) {
438 return r;
439 }
440 i--;
441 }
442 return null;
443 }
444
445 /**
446 * This is a simplified version of topRunningActivityLocked that provides a number of
447 * optional skip-over modes. It is intended for use with the ActivityController hook only.
448 *
449 * @param token If non-null, any history records matching this token will be skipped.
450 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
451 *
452 * @return Returns the HistoryRecord of the next activity on the stack.
453 */
454 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700455 // TODO: Don't look for any tasks from other users
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700456 int i = mHistory.size()-1;
457 while (i >= 0) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700458 ActivityRecord r = mHistory.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700459 // Note: the taskId check depends on real taskId fields being non-zero
Dianne Hackbornbe707852011-11-11 14:32:10 -0800460 if (!r.finishing && (token != r.appToken) && (taskId != r.task.taskId)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700461 return r;
462 }
463 i--;
464 }
465 return null;
466 }
467
468 final int indexOfTokenLocked(IBinder token) {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800469 return mHistory.indexOf(ActivityRecord.forToken(token));
470 }
471
472 final int indexOfActivityLocked(ActivityRecord r) {
473 return mHistory.indexOf(r);
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700474 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700475
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700476 final ActivityRecord isInStackLocked(IBinder token) {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800477 ActivityRecord r = ActivityRecord.forToken(token);
478 if (mHistory.contains(r)) {
479 return r;
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700480 }
481 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700482 }
483
484 private final boolean updateLRUListLocked(ActivityRecord r) {
485 final boolean hadit = mLRUActivities.remove(r);
486 mLRUActivities.add(r);
487 return hadit;
488 }
489
490 /**
491 * Returns the top activity in any existing task matching the given
492 * Intent. Returns null if no such task is found.
493 */
494 private ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) {
495 ComponentName cls = intent.getComponent();
496 if (info.targetActivity != null) {
497 cls = new ComponentName(info.packageName, info.targetActivity);
498 }
499
500 TaskRecord cp = null;
501
Amith Yamasani742a6712011-05-04 14:49:28 -0700502 final int userId = UserId.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700503 final int N = mHistory.size();
504 for (int i=(N-1); i>=0; i--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700505 ActivityRecord r = mHistory.get(i);
Amith Yamasani742a6712011-05-04 14:49:28 -0700506 if (!r.finishing && r.task != cp && r.userId == userId
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700507 && r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
508 cp = r.task;
509 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
510 // + "/aff=" + r.task.affinity + " to new cls="
511 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
512 if (r.task.affinity != null) {
513 if (r.task.affinity.equals(info.taskAffinity)) {
514 //Slog.i(TAG, "Found matching affinity!");
515 return r;
516 }
517 } else if (r.task.intent != null
518 && r.task.intent.getComponent().equals(cls)) {
519 //Slog.i(TAG, "Found matching class!");
520 //dump();
521 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
522 return r;
523 } else if (r.task.affinityIntent != null
524 && r.task.affinityIntent.getComponent().equals(cls)) {
525 //Slog.i(TAG, "Found matching class!");
526 //dump();
527 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
528 return r;
529 }
530 }
531 }
532
533 return null;
534 }
535
536 /**
537 * Returns the first activity (starting from the top of the stack) that
538 * is the same as the given activity. Returns null if no such activity
539 * is found.
540 */
541 private ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
542 ComponentName cls = intent.getComponent();
543 if (info.targetActivity != null) {
544 cls = new ComponentName(info.packageName, info.targetActivity);
545 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700546 final int userId = UserId.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700547
548 final int N = mHistory.size();
549 for (int i=(N-1); i>=0; i--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700550 ActivityRecord r = mHistory.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700551 if (!r.finishing) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700552 if (r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700553 //Slog.i(TAG, "Found matching class!");
554 //dump();
555 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
556 return r;
557 }
558 }
559 }
560
561 return null;
562 }
563
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700564 final void showAskCompatModeDialogLocked(ActivityRecord r) {
565 Message msg = Message.obtain();
566 msg.what = ActivityManagerService.SHOW_COMPAT_MODE_DIALOG_MSG;
567 msg.obj = r.task.askedCompatMode ? null : r;
568 mService.mHandler.sendMessage(msg);
569 }
570
Amith Yamasani742a6712011-05-04 14:49:28 -0700571 /*
572 * Move the activities around in the stack to bring a user to the foreground.
573 * @return whether there are any activities for the specified user.
574 */
575 final boolean switchUser(int userId) {
576 synchronized (mService) {
577 mCurrentUser = userId;
578
579 // Only one activity? Nothing to do...
580 if (mHistory.size() < 2)
581 return false;
582
583 boolean haveActivities = false;
584 // Check if the top activity is from the new user.
585 ActivityRecord top = mHistory.get(mHistory.size() - 1);
586 if (top.userId == userId) return true;
587 // Otherwise, move the user's activities to the top.
588 int N = mHistory.size();
589 int i = 0;
590 while (i < N) {
591 ActivityRecord r = mHistory.get(i);
592 if (r.userId == userId) {
593 ActivityRecord moveToTop = mHistory.remove(i);
594 mHistory.add(moveToTop);
595 // No need to check the top one now
596 N--;
597 haveActivities = true;
598 } else {
599 i++;
600 }
601 }
602 // Transition from the old top to the new top
603 resumeTopActivityLocked(top);
604 return haveActivities;
605 }
606 }
607
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700608 final boolean realStartActivityLocked(ActivityRecord r,
609 ProcessRecord app, boolean andResume, boolean checkConfig)
610 throws RemoteException {
611
612 r.startFreezingScreenLocked(app, 0);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800613 mService.mWindowManager.setAppVisibility(r.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700614
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700615 // schedule launch ticks to collect information about slow apps.
616 r.startLaunchTickingLocked();
617
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700618 // Have the window manager re-evaluate the orientation of
619 // the screen based on the new activity order. Note that
620 // as a result of this, it can call back into the activity
621 // manager with a new orientation. We don't care about that,
622 // because the activity is not currently running so we are
623 // just restarting it anyway.
624 if (checkConfig) {
625 Configuration config = mService.mWindowManager.updateOrientationFromAppTokens(
626 mService.mConfiguration,
Dianne Hackbornbe707852011-11-11 14:32:10 -0800627 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Dianne Hackborn813075a62011-11-14 17:45:19 -0800628 mService.updateConfigurationLocked(config, r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700629 }
630
631 r.app = app;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700632 app.waitingToKill = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700633
634 if (localLOGV) Slog.v(TAG, "Launching: " + r);
635
636 int idx = app.activities.indexOf(r);
637 if (idx < 0) {
638 app.activities.add(r);
639 }
640 mService.updateLruProcessLocked(app, true, true);
641
642 try {
643 if (app.thread == null) {
644 throw new RemoteException();
645 }
646 List<ResultInfo> results = null;
647 List<Intent> newIntents = null;
648 if (andResume) {
649 results = r.results;
650 newIntents = r.newIntents;
651 }
652 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
653 + " icicle=" + r.icicle
654 + " with results=" + results + " newIntents=" + newIntents
655 + " andResume=" + andResume);
656 if (andResume) {
657 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
658 System.identityHashCode(r),
659 r.task.taskId, r.shortComponentName);
660 }
661 if (r.isHomeActivity) {
662 mService.mHomeProcess = app;
663 }
664 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800665 r.sleeping = false;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400666 r.forceNewConfig = false;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700667 showAskCompatModeDialogLocked(r);
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700668 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700669 String profileFile = null;
670 ParcelFileDescriptor profileFd = null;
671 boolean profileAutoStop = false;
672 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
673 if (mService.mProfileProc == null || mService.mProfileProc == app) {
674 mService.mProfileProc = app;
675 profileFile = mService.mProfileFile;
676 profileFd = mService.mProfileFd;
677 profileAutoStop = mService.mAutoStopProfiler;
678 }
679 }
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -0700680 app.hasShownUi = true;
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700681 app.pendingUiClean = true;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700682 if (profileFd != null) {
683 try {
684 profileFd = profileFd.dup();
685 } catch (IOException e) {
686 profileFd = null;
687 }
688 }
Dianne Hackbornbe707852011-11-11 14:32:10 -0800689 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Dianne Hackborn813075a62011-11-14 17:45:19 -0800690 System.identityHashCode(r), r.info,
691 new Configuration(mService.mConfiguration),
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700692 r.compat, r.icicle, results, newIntents, !andResume,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700693 mService.isNextTransitionForward(), profileFile, profileFd,
694 profileAutoStop);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700695
Dianne Hackborn54e570f2010-10-04 18:32:32 -0700696 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700697 // This may be a heavy-weight process! Note that the package
698 // manager will ensure that only activity can run in the main
699 // process of the .apk, which is the only thing that will be
700 // considered heavy-weight.
701 if (app.processName.equals(app.info.packageName)) {
702 if (mService.mHeavyWeightProcess != null
703 && mService.mHeavyWeightProcess != app) {
704 Log.w(TAG, "Starting new heavy weight process " + app
705 + " when already running "
706 + mService.mHeavyWeightProcess);
707 }
708 mService.mHeavyWeightProcess = app;
709 Message msg = mService.mHandler.obtainMessage(
710 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
711 msg.obj = r;
712 mService.mHandler.sendMessage(msg);
713 }
714 }
715
716 } catch (RemoteException e) {
717 if (r.launchFailed) {
718 // This is the second time we failed -- finish activity
719 // and give up.
720 Slog.e(TAG, "Second failure launching "
721 + r.intent.getComponent().flattenToShortString()
722 + ", giving up", e);
723 mService.appDiedLocked(app, app.pid, app.thread);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800724 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700725 "2nd-crash");
726 return false;
727 }
728
729 // This is the first time we failed -- restart process and
730 // retry.
731 app.activities.remove(r);
732 throw e;
733 }
734
735 r.launchFailed = false;
736 if (updateLRUListLocked(r)) {
737 Slog.w(TAG, "Activity " + r
738 + " being launched, but already in LRU list");
739 }
740
741 if (andResume) {
742 // As part of the process of launching, ActivityThread also performs
743 // a resume.
744 r.state = ActivityState.RESUMED;
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700745 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
746 + " (starting new instance)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700747 r.stopped = false;
748 mResumedActivity = r;
749 r.task.touchActiveTime();
Dianne Hackborn88819b22010-12-21 18:18:02 -0800750 if (mMainStack) {
751 mService.addRecentTaskLocked(r.task);
752 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700753 completeResumeLocked(r);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800754 checkReadyForSleepLocked();
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700755 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
756 r.icicle = null;
757 r.haveState = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700758 } else {
759 // This activity is not starting in the resumed state... which
760 // should look like we asked it to pause+stop (but remain visible),
761 // and it has done so and reported back the current icicle and
762 // other state.
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700763 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
764 + " (starting in stopped state)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700765 r.state = ActivityState.STOPPED;
766 r.stopped = true;
767 }
768
769 // Launch the new version setup screen if needed. We do this -after-
770 // launching the initial activity (that is, home), so that it can have
771 // a chance to initialize itself while in the background, making the
772 // switch back to it faster and look better.
773 if (mMainStack) {
774 mService.startSetupActivityLocked();
775 }
776
777 return true;
778 }
779
780 private final void startSpecificActivityLocked(ActivityRecord r,
781 boolean andResume, boolean checkConfig) {
782 // Is this activity's application already running?
783 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
784 r.info.applicationInfo.uid);
785
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700786 if (r.launchTime == 0) {
787 r.launchTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700788 if (mInitialStartTime == 0) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700789 mInitialStartTime = r.launchTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700790 }
791 } else if (mInitialStartTime == 0) {
792 mInitialStartTime = SystemClock.uptimeMillis();
793 }
794
795 if (app != null && app.thread != null) {
796 try {
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700797 app.addPackage(r.info.packageName);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700798 realStartActivityLocked(r, app, andResume, checkConfig);
799 return;
800 } catch (RemoteException e) {
801 Slog.w(TAG, "Exception when starting activity "
802 + r.intent.getComponent().flattenToShortString(), e);
803 }
804
805 // If a dead object exception was thrown -- fall through to
806 // restart the application.
807 }
808
809 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800810 "activity", r.intent.getComponent(), false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700811 }
812
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800813 void stopIfSleepingLocked() {
814 if (mService.isSleeping()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700815 if (!mGoingToSleep.isHeld()) {
816 mGoingToSleep.acquire();
817 if (mLaunchingActivity.isHeld()) {
818 mLaunchingActivity.release();
819 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
820 }
821 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800822 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
823 Message msg = mHandler.obtainMessage(SLEEP_TIMEOUT_MSG);
824 mHandler.sendMessageDelayed(msg, SLEEP_TIMEOUT);
825 checkReadyForSleepLocked();
826 }
827 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700828
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800829 void awakeFromSleepingLocked() {
830 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
831 mSleepTimeout = false;
832 if (mGoingToSleep.isHeld()) {
833 mGoingToSleep.release();
834 }
835 // Ensure activities are no longer sleeping.
836 for (int i=mHistory.size()-1; i>=0; i--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700837 ActivityRecord r = mHistory.get(i);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800838 r.setSleeping(false);
839 }
840 mGoingToSleepActivities.clear();
841 }
842
843 void activitySleptLocked(ActivityRecord r) {
844 mGoingToSleepActivities.remove(r);
845 checkReadyForSleepLocked();
846 }
847
848 void checkReadyForSleepLocked() {
849 if (!mService.isSleeping()) {
850 // Do not care.
851 return;
852 }
853
854 if (!mSleepTimeout) {
855 if (mResumedActivity != null) {
856 // Still have something resumed; can't sleep until it is paused.
857 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700858 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
859 startPausingLocked(false, true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800860 return;
861 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800862 if (mPausingActivity != null) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800863 // Still waiting for something to pause; can't sleep yet.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800864 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800865 return;
866 }
867
868 if (mStoppingActivities.size() > 0) {
869 // Still need to tell some activities to stop; can't sleep yet.
870 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
871 + mStoppingActivities.size() + " activities");
Dianne Hackborn80a7ac12011-09-22 18:32:52 -0700872 scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800873 return;
874 }
875
876 ensureActivitiesVisibleLocked(null, 0);
877
878 // Make sure any stopped but visible activities are now sleeping.
879 // This ensures that the activity's onStop() is called.
880 for (int i=mHistory.size()-1; i>=0; i--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700881 ActivityRecord r = mHistory.get(i);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800882 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
883 r.setSleeping(true);
884 }
885 }
886
887 if (mGoingToSleepActivities.size() > 0) {
888 // Still need to tell some activities to sleep; can't sleep yet.
889 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
890 + mGoingToSleepActivities.size() + " activities");
891 return;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700892 }
893 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800894
895 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
896
897 if (mGoingToSleep.isHeld()) {
898 mGoingToSleep.release();
899 }
900 if (mService.mShuttingDown) {
901 mService.notifyAll();
902 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700903 }
904
Dianne Hackbornd2835932010-12-13 16:28:46 -0800905 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800906 if (who.noDisplay) {
907 return null;
908 }
909
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800910 Resources res = mService.mContext.getResources();
911 int w = mThumbnailWidth;
912 int h = mThumbnailHeight;
913 if (w < 0) {
914 mThumbnailWidth = w =
915 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
916 mThumbnailHeight = h =
917 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
918 }
919
920 if (w > 0) {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800921 return mService.mWindowManager.screenshotApplications(who.appToken, w, h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800922 }
923 return null;
924 }
925
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700926 private final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800927 if (mPausingActivity != null) {
928 RuntimeException e = new RuntimeException();
929 Slog.e(TAG, "Trying to pause when pause is already pending for "
930 + mPausingActivity, e);
931 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700932 ActivityRecord prev = mResumedActivity;
933 if (prev == null) {
934 RuntimeException e = new RuntimeException();
935 Slog.e(TAG, "Trying to pause when nothing is resumed", e);
936 resumeTopActivityLocked(null);
937 return;
938 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700939 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
940 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700941 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800942 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700943 mLastPausedActivity = prev;
944 prev.state = ActivityState.PAUSING;
945 prev.task.touchActiveTime();
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700946 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700947
948 mService.updateCpuStats();
949
950 if (prev.app != null && prev.app.thread != null) {
951 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
952 try {
953 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
954 System.identityHashCode(prev),
955 prev.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800956 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
957 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700958 if (mMainStack) {
959 mService.updateUsageStats(prev, false);
960 }
961 } catch (Exception e) {
962 // Ignore exception, if process died other code will cleanup.
963 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800964 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700965 mLastPausedActivity = null;
966 }
967 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800968 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700969 mLastPausedActivity = null;
970 }
971
972 // If we are not going to sleep, we want to ensure the device is
973 // awake until the next activity is started.
974 if (!mService.mSleeping && !mService.mShuttingDown) {
975 mLaunchingActivity.acquire();
976 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
977 // To be safe, don't allow the wake lock to be held for too long.
978 Message msg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
979 mHandler.sendMessageDelayed(msg, LAUNCH_TIMEOUT);
980 }
981 }
982
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800983
984 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700985 // Have the window manager pause its key dispatching until the new
986 // activity has started. If we're pausing the activity just because
987 // the screen is being turned off and the UI is sleeping, don't interrupt
988 // key dispatch; the same activity will pick it up again on wakeup.
989 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800990 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700991 } else {
992 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
993 }
994
995 // Schedule a pause timeout in case the app doesn't respond.
996 // We don't give it much time because this directly impacts the
997 // responsiveness seen by the user.
998 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
999 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001000 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001001 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
1002 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
1003 } else {
1004 // This activity failed to schedule the
1005 // pause, so just treat it as being paused now.
1006 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001007 resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001008 }
1009 }
1010
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001011 final void activityPaused(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001012 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001013 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001014
1015 ActivityRecord r = null;
1016
1017 synchronized (mService) {
1018 int index = indexOfTokenLocked(token);
1019 if (index >= 0) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001020 r = mHistory.get(index);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001021 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001022 if (mPausingActivity == r) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001023 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
1024 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001025 r.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001026 completePauseLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001027 } else {
1028 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1029 System.identityHashCode(r), r.shortComponentName,
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001030 mPausingActivity != null
1031 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001032 }
1033 }
1034 }
1035 }
1036
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001037 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
1038 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001039 if (r.state != ActivityState.STOPPING) {
1040 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1041 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1042 return;
1043 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07001044 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001045 if (icicle != null) {
1046 // If icicle is null, this is happening due to a timeout, so we
1047 // haven't really saved the state.
1048 r.icicle = icicle;
1049 r.haveState = true;
1050 r.updateThumbnail(thumbnail, description);
1051 }
1052 if (!r.stopped) {
1053 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
1054 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1055 r.stopped = true;
1056 r.state = ActivityState.STOPPED;
1057 if (!r.finishing) {
1058 if (r.configDestroy) {
1059 destroyActivityLocked(r, true, false, "stop-config");
1060 resumeTopActivityLocked(null);
1061 } else {
1062 // Now that this process has stopped, we may want to consider
1063 // it to be the previous app to try to keep around in case
1064 // the user wants to return to it.
1065 ProcessRecord fgApp = null;
1066 if (mResumedActivity != null) {
1067 fgApp = mResumedActivity.app;
1068 } else if (mPausingActivity != null) {
1069 fgApp = mPausingActivity.app;
1070 }
1071 if (r.app != null && fgApp != null && r.app != fgApp
1072 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
1073 && r.app != mService.mHomeProcess) {
1074 mService.mPreviousProcess = r.app;
1075 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1076 }
Dianne Hackborn50685602011-12-01 12:23:37 -08001077 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001078 }
1079 }
1080 }
1081
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001082 private final void completePauseLocked() {
1083 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001084 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
1085
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001086 if (prev != null) {
1087 if (prev.finishing) {
1088 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
1089 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE);
1090 } else if (prev.app != null) {
1091 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
1092 if (prev.waitingVisible) {
1093 prev.waitingVisible = false;
1094 mWaitingVisibleActivities.remove(prev);
1095 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
1096 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001097 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001098 if (prev.configDestroy) {
1099 // The previous is being paused because the configuration
1100 // is changing, which means it is actually stopping...
1101 // To juggle the fact that we are also starting a new
1102 // instance right now, we need to first completely stop
1103 // the current instance before starting the new one.
1104 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
1105 destroyActivityLocked(prev, true, false, "pause-config");
1106 } else {
1107 mStoppingActivities.add(prev);
1108 if (mStoppingActivities.size() > 3) {
1109 // If we already have a few activities waiting to stop,
1110 // then give up on things going idle and start clearing
1111 // them out.
1112 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
1113 scheduleIdleLocked();
1114 } else {
1115 checkReadyForSleepLocked();
1116 }
1117 }
1118 } else {
1119 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
1120 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001121 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001122 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001123 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001124
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001125 if (!mService.isSleeping()) {
1126 resumeTopActivityLocked(prev);
1127 } else {
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001128 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001129 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001130
1131 if (prev != null) {
1132 prev.resumeKeyDispatchingLocked();
1133 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001134
1135 if (prev.app != null && prev.cpuTimeAtResume > 0
1136 && mService.mBatteryStatsService.isOnBattery()) {
1137 long diff = 0;
1138 synchronized (mService.mProcessStatsThread) {
1139 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid)
1140 - prev.cpuTimeAtResume;
1141 }
1142 if (diff > 0) {
1143 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1144 synchronized (bsi) {
1145 BatteryStatsImpl.Uid.Proc ps =
1146 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
1147 prev.info.packageName);
1148 if (ps != null) {
1149 ps.addForegroundTimeLocked(diff);
1150 }
1151 }
1152 }
1153 }
1154 prev.cpuTimeAtResume = 0; // reset it
1155 }
1156
1157 /**
1158 * Once we know that we have asked an application to put an activity in
1159 * the resumed state (either by launching it or explicitly telling it),
1160 * this function updates the rest of our state to match that fact.
1161 */
1162 private final void completeResumeLocked(ActivityRecord next) {
1163 next.idle = false;
1164 next.results = null;
1165 next.newIntents = null;
1166
1167 // schedule an idle timeout in case the app doesn't do it for us.
1168 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
1169 msg.obj = next;
1170 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
1171
1172 if (false) {
1173 // The activity was never told to pause, so just keep
1174 // things going as-is. To maintain our own state,
1175 // we need to emulate it coming back and saying it is
1176 // idle.
1177 msg = mHandler.obtainMessage(IDLE_NOW_MSG);
1178 msg.obj = next;
1179 mHandler.sendMessage(msg);
1180 }
1181
1182 if (mMainStack) {
1183 mService.reportResumedActivityLocked(next);
1184 }
1185
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001186 next.clearThumbnail();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001187 if (mMainStack) {
1188 mService.setFocusedActivityLocked(next);
1189 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001190 next.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001191 ensureActivitiesVisibleLocked(null, 0);
1192 mService.mWindowManager.executeAppTransition();
1193 mNoAnimActivities.clear();
1194
1195 // Mark the point when the activity is resuming
1196 // TODO: To be more accurate, the mark should be before the onCreate,
1197 // not after the onResume. But for subsequent starts, onResume is fine.
1198 if (next.app != null) {
1199 synchronized (mService.mProcessStatsThread) {
1200 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid);
1201 }
1202 } else {
1203 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1204 }
1205 }
1206
1207 /**
1208 * Make sure that all activities that need to be visible (that is, they
1209 * currently can be seen by the user) actually are.
1210 */
1211 final void ensureActivitiesVisibleLocked(ActivityRecord top,
1212 ActivityRecord starting, String onlyThisProcess, int configChanges) {
1213 if (DEBUG_VISBILITY) Slog.v(
1214 TAG, "ensureActivitiesVisible behind " + top
1215 + " configChanges=0x" + Integer.toHexString(configChanges));
1216
1217 // If the top activity is not fullscreen, then we need to
1218 // make sure any activities under it are now visible.
1219 final int count = mHistory.size();
1220 int i = count-1;
1221 while (mHistory.get(i) != top) {
1222 i--;
1223 }
1224 ActivityRecord r;
1225 boolean behindFullscreen = false;
1226 for (; i>=0; i--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001227 r = mHistory.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001228 if (DEBUG_VISBILITY) Slog.v(
1229 TAG, "Make visible? " + r + " finishing=" + r.finishing
1230 + " state=" + r.state);
1231 if (r.finishing) {
1232 continue;
1233 }
1234
1235 final boolean doThisProcess = onlyThisProcess == null
1236 || onlyThisProcess.equals(r.processName);
1237
1238 // First: if this is not the current activity being started, make
1239 // sure it matches the current configuration.
1240 if (r != starting && doThisProcess) {
1241 ensureActivityConfigurationLocked(r, 0);
1242 }
1243
1244 if (r.app == null || r.app.thread == null) {
1245 if (onlyThisProcess == null
1246 || onlyThisProcess.equals(r.processName)) {
1247 // This activity needs to be visible, but isn't even
1248 // running... get it started, but don't resume it
1249 // at this point.
1250 if (DEBUG_VISBILITY) Slog.v(
1251 TAG, "Start and freeze screen for " + r);
1252 if (r != starting) {
1253 r.startFreezingScreenLocked(r.app, configChanges);
1254 }
1255 if (!r.visible) {
1256 if (DEBUG_VISBILITY) Slog.v(
1257 TAG, "Starting and making visible: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001258 mService.mWindowManager.setAppVisibility(r.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001259 }
1260 if (r != starting) {
1261 startSpecificActivityLocked(r, false, false);
1262 }
1263 }
1264
1265 } else if (r.visible) {
1266 // If this activity is already visible, then there is nothing
1267 // else to do here.
1268 if (DEBUG_VISBILITY) Slog.v(
1269 TAG, "Skipping: already visible at " + r);
1270 r.stopFreezingScreenLocked(false);
1271
1272 } else if (onlyThisProcess == null) {
1273 // This activity is not currently visible, but is running.
1274 // Tell it to become visible.
1275 r.visible = true;
1276 if (r.state != ActivityState.RESUMED && r != starting) {
1277 // If this activity is paused, tell it
1278 // to now show its window.
1279 if (DEBUG_VISBILITY) Slog.v(
1280 TAG, "Making visible and scheduling visibility: " + r);
1281 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001282 mService.mWindowManager.setAppVisibility(r.appToken, true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001283 r.sleeping = false;
Dianne Hackborn905577f2011-09-07 18:31:28 -07001284 r.app.pendingUiClean = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001285 r.app.thread.scheduleWindowVisibility(r.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001286 r.stopFreezingScreenLocked(false);
1287 } catch (Exception e) {
1288 // Just skip on any failure; we'll make it
1289 // visible when it next restarts.
1290 Slog.w(TAG, "Exception thrown making visibile: "
1291 + r.intent.getComponent(), e);
1292 }
1293 }
1294 }
1295
1296 // Aggregate current change flags.
1297 configChanges |= r.configChangeFlags;
1298
1299 if (r.fullscreen) {
1300 // At this point, nothing else needs to be shown
1301 if (DEBUG_VISBILITY) Slog.v(
1302 TAG, "Stopping: fullscreen at " + r);
1303 behindFullscreen = true;
1304 i--;
1305 break;
1306 }
1307 }
1308
1309 // Now for any activities that aren't visible to the user, make
1310 // sure they no longer are keeping the screen frozen.
1311 while (i >= 0) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001312 r = mHistory.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001313 if (DEBUG_VISBILITY) Slog.v(
1314 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1315 + " state=" + r.state
1316 + " behindFullscreen=" + behindFullscreen);
1317 if (!r.finishing) {
1318 if (behindFullscreen) {
1319 if (r.visible) {
1320 if (DEBUG_VISBILITY) Slog.v(
1321 TAG, "Making invisible: " + r);
1322 r.visible = false;
1323 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001324 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001325 if ((r.state == ActivityState.STOPPING
1326 || r.state == ActivityState.STOPPED)
1327 && r.app != null && r.app.thread != null) {
1328 if (DEBUG_VISBILITY) Slog.v(
1329 TAG, "Scheduling invisibility: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001330 r.app.thread.scheduleWindowVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001331 }
1332 } catch (Exception e) {
1333 // Just skip on any failure; we'll make it
1334 // visible when it next restarts.
1335 Slog.w(TAG, "Exception thrown making hidden: "
1336 + r.intent.getComponent(), e);
1337 }
1338 } else {
1339 if (DEBUG_VISBILITY) Slog.v(
1340 TAG, "Already invisible: " + r);
1341 }
1342 } else if (r.fullscreen) {
1343 if (DEBUG_VISBILITY) Slog.v(
1344 TAG, "Now behindFullscreen: " + r);
1345 behindFullscreen = true;
1346 }
1347 }
1348 i--;
1349 }
1350 }
1351
1352 /**
1353 * Version of ensureActivitiesVisible that can easily be called anywhere.
1354 */
1355 final void ensureActivitiesVisibleLocked(ActivityRecord starting,
1356 int configChanges) {
1357 ActivityRecord r = topRunningActivityLocked(null);
1358 if (r != null) {
1359 ensureActivitiesVisibleLocked(r, starting, null, configChanges);
1360 }
1361 }
1362
1363 /**
1364 * Ensure that the top activity in the stack is resumed.
1365 *
1366 * @param prev The previously resumed activity, for when in the process
1367 * of pausing; can be null to call from elsewhere.
1368 *
1369 * @return Returns true if something is being resumed, or false if
1370 * nothing happened.
1371 */
1372 final boolean resumeTopActivityLocked(ActivityRecord prev) {
1373 // Find the first activity that is not finishing.
1374 ActivityRecord next = topRunningActivityLocked(null);
1375
1376 // Remember how we'll process this pause/resume situation, and ensure
1377 // that the state is reset however we wind up proceeding.
1378 final boolean userLeaving = mUserLeaving;
1379 mUserLeaving = false;
1380
1381 if (next == null) {
1382 // There are no more activities! Let's just start up the
1383 // Launcher...
1384 if (mMainStack) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001385 return mService.startHomeActivityLocked(0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001386 }
1387 }
1388
1389 next.delayedResume = false;
1390
1391 // If the top activity is the resumed one, nothing to do.
1392 if (mResumedActivity == next && next.state == ActivityState.RESUMED) {
1393 // Make sure we have executed any pending transitions, since there
1394 // should be nothing left to do at this point.
1395 mService.mWindowManager.executeAppTransition();
1396 mNoAnimActivities.clear();
1397 return false;
1398 }
1399
1400 // If we are sleeping, and there is no resumed activity, and the top
1401 // activity is paused, well that is the state we want.
1402 if ((mService.mSleeping || mService.mShuttingDown)
p13451dbad2872012-04-18 11:39:23 +09001403 && mLastPausedActivity == next
1404 && (next.state == ActivityState.PAUSED
1405 || next.state == ActivityState.STOPPED
1406 || next.state == ActivityState.STOPPING)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001407 // Make sure we have executed any pending transitions, since there
1408 // should be nothing left to do at this point.
1409 mService.mWindowManager.executeAppTransition();
1410 mNoAnimActivities.clear();
1411 return false;
1412 }
1413
1414 // The activity may be waiting for stop, but that is no longer
1415 // appropriate for it.
1416 mStoppingActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001417 mGoingToSleepActivities.remove(next);
1418 next.sleeping = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001419 mWaitingVisibleActivities.remove(next);
1420
1421 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1422
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001423 // If we are currently pausing an activity, then don't do anything
1424 // until that is done.
1425 if (mPausingActivity != null) {
1426 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: pausing=" + mPausingActivity);
1427 return false;
1428 }
1429
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001430 // Okay we are now going to start a switch, to 'next'. We may first
1431 // have to pause the current activity, but this is an important point
1432 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001433 // XXX "App Redirected" dialog is getting too many false positives
1434 // at this point, so turn off for now.
1435 if (false) {
1436 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1437 long now = SystemClock.uptimeMillis();
1438 final boolean inTime = mLastStartedActivity.startTime != 0
1439 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1440 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1441 final int nextUid = next.info.applicationInfo.uid;
1442 if (inTime && lastUid != nextUid
1443 && lastUid != next.launchedFromUid
1444 && mService.checkPermission(
1445 android.Manifest.permission.STOP_APP_SWITCHES,
1446 -1, next.launchedFromUid)
1447 != PackageManager.PERMISSION_GRANTED) {
1448 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1449 } else {
1450 next.startTime = now;
1451 mLastStartedActivity = next;
1452 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001453 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001454 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001455 mLastStartedActivity = next;
1456 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001457 }
1458
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001459 // We need to start pausing the current activity so the top one
1460 // can be resumed...
1461 if (mResumedActivity != null) {
1462 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing");
1463 startPausingLocked(userLeaving, false);
1464 return true;
1465 }
1466
1467 if (prev != null && prev != next) {
1468 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1469 prev.waitingVisible = true;
1470 mWaitingVisibleActivities.add(prev);
1471 if (DEBUG_SWITCH) Slog.v(
1472 TAG, "Resuming top, waiting visible to hide: " + prev);
1473 } else {
1474 // The next activity is already visible, so hide the previous
1475 // activity's windows right now so we can show the new one ASAP.
1476 // We only do this if the previous is finishing, which should mean
1477 // it is on top of the one being resumed so hiding it quickly
1478 // is good. Otherwise, we want to do the normal route of allowing
1479 // the resumed activity to be shown so we can decide if the
1480 // previous should actually be hidden depending on whether the
1481 // new one is found to be full-screen or not.
1482 if (prev.finishing) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001483 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001484 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1485 + prev + ", waitingVisible="
1486 + (prev != null ? prev.waitingVisible : null)
1487 + ", nowVisible=" + next.nowVisible);
1488 } else {
1489 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1490 + prev + ", waitingVisible="
1491 + (prev != null ? prev.waitingVisible : null)
1492 + ", nowVisible=" + next.nowVisible);
1493 }
1494 }
1495 }
1496
Dianne Hackborne7f97212011-02-24 14:40:20 -08001497 // Launching this app's activity, make sure the app is no longer
1498 // considered stopped.
1499 try {
1500 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001501 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001502 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001503 } catch (IllegalArgumentException e) {
1504 Slog.w(TAG, "Failed trying to unstop package "
1505 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001506 }
1507
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001508 // We are starting up the next activity, so tell the window manager
1509 // that the previous one will be hidden soon. This way it can know
1510 // to ignore it when computing the desired screen orientation.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001511 boolean noAnim = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001512 if (prev != null) {
1513 if (prev.finishing) {
1514 if (DEBUG_TRANSITION) Slog.v(TAG,
1515 "Prepare close transition: prev=" + prev);
1516 if (mNoAnimActivities.contains(prev)) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001517 mService.mWindowManager.prepareAppTransition(
1518 WindowManagerPolicy.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001519 } else {
1520 mService.mWindowManager.prepareAppTransition(prev.task == next.task
1521 ? WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001522 : WindowManagerPolicy.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001523 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001524 mService.mWindowManager.setAppWillBeHidden(prev.appToken);
1525 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001526 } else {
1527 if (DEBUG_TRANSITION) Slog.v(TAG,
1528 "Prepare open transition: prev=" + prev);
1529 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001530 noAnim = true;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001531 mService.mWindowManager.prepareAppTransition(
1532 WindowManagerPolicy.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001533 } else {
1534 mService.mWindowManager.prepareAppTransition(prev.task == next.task
1535 ? WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001536 : WindowManagerPolicy.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001537 }
1538 }
1539 if (false) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001540 mService.mWindowManager.setAppWillBeHidden(prev.appToken);
1541 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001542 }
1543 } else if (mHistory.size() > 1) {
1544 if (DEBUG_TRANSITION) Slog.v(TAG,
1545 "Prepare open transition: no previous");
1546 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001547 noAnim = true;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001548 mService.mWindowManager.prepareAppTransition(
1549 WindowManagerPolicy.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001550 } else {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001551 mService.mWindowManager.prepareAppTransition(
1552 WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001553 }
1554 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001555 if (!noAnim) {
1556 next.applyOptionsLocked();
1557 } else {
1558 next.clearOptionsLocked();
1559 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001560
1561 if (next.app != null && next.app.thread != null) {
1562 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1563
1564 // This activity is now becoming visible.
Dianne Hackbornbe707852011-11-11 14:32:10 -08001565 mService.mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001566
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001567 // schedule launch ticks to collect information about slow apps.
1568 next.startLaunchTickingLocked();
1569
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001570 ActivityRecord lastResumedActivity = mResumedActivity;
1571 ActivityState lastState = next.state;
1572
1573 mService.updateCpuStats();
1574
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001575 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001576 next.state = ActivityState.RESUMED;
1577 mResumedActivity = next;
1578 next.task.touchActiveTime();
Dianne Hackborn88819b22010-12-21 18:18:02 -08001579 if (mMainStack) {
1580 mService.addRecentTaskLocked(next.task);
1581 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001582 mService.updateLruProcessLocked(next.app, true, true);
1583 updateLRUListLocked(next);
1584
1585 // Have the window manager re-evaluate the orientation of
1586 // the screen based on the new activity order.
1587 boolean updated = false;
1588 if (mMainStack) {
1589 synchronized (mService) {
1590 Configuration config = mService.mWindowManager.updateOrientationFromAppTokens(
1591 mService.mConfiguration,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001592 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001593 if (config != null) {
1594 next.frozenBeforeDestroy = true;
1595 }
Dianne Hackborn813075a62011-11-14 17:45:19 -08001596 updated = mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001597 }
1598 }
1599 if (!updated) {
1600 // The configuration update wasn't able to keep the existing
1601 // instance of the activity, and instead started a new one.
1602 // We should be all done, but let's just make sure our activity
1603 // is still at the top and schedule another run if something
1604 // weird happened.
1605 ActivityRecord nextNext = topRunningActivityLocked(null);
1606 if (DEBUG_SWITCH) Slog.i(TAG,
1607 "Activity config changed during resume: " + next
1608 + ", new next: " + nextNext);
1609 if (nextNext != next) {
1610 // Do over!
1611 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
1612 }
1613 if (mMainStack) {
1614 mService.setFocusedActivityLocked(next);
1615 }
1616 ensureActivitiesVisibleLocked(null, 0);
1617 mService.mWindowManager.executeAppTransition();
1618 mNoAnimActivities.clear();
1619 return true;
1620 }
1621
1622 try {
1623 // Deliver all pending results.
1624 ArrayList a = next.results;
1625 if (a != null) {
1626 final int N = a.size();
1627 if (!next.finishing && N > 0) {
1628 if (DEBUG_RESULTS) Slog.v(
1629 TAG, "Delivering results to " + next
1630 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001631 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001632 }
1633 }
1634
1635 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001636 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001637 }
1638
1639 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
1640 System.identityHashCode(next),
1641 next.task.taskId, next.shortComponentName);
1642
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001643 next.sleeping = false;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07001644 showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001645 next.app.pendingUiClean = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001646 next.app.thread.scheduleResumeActivity(next.appToken,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001647 mService.isNextTransitionForward());
1648
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001649 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001650
1651 } catch (Exception e) {
1652 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001653 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1654 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001655 next.state = lastState;
1656 mResumedActivity = lastResumedActivity;
1657 Slog.i(TAG, "Restarting because process died: " + next);
1658 if (!next.hasBeenLaunched) {
1659 next.hasBeenLaunched = true;
1660 } else {
1661 if (SHOW_APP_STARTING_PREVIEW && mMainStack) {
1662 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001663 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001664 mService.compatibilityInfoForPackageLocked(
1665 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001666 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001667 next.labelRes, next.icon, next.windowFlags,
1668 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001669 }
1670 }
1671 startSpecificActivityLocked(next, true, false);
1672 return true;
1673 }
1674
1675 // From this point on, if something goes wrong there is no way
1676 // to recover the activity.
1677 try {
1678 next.visible = true;
1679 completeResumeLocked(next);
1680 } catch (Exception e) {
1681 // If any exception gets thrown, toss away this
1682 // activity and try the next one.
1683 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001684 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001685 "resume-exception");
1686 return true;
1687 }
1688
1689 // Didn't need to use the icicle, and it is now out of date.
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07001690 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Resumed activity; didn't need icicle of: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001691 next.icicle = null;
1692 next.haveState = false;
1693 next.stopped = false;
1694
1695 } else {
1696 // Whoops, need to restart this activity!
1697 if (!next.hasBeenLaunched) {
1698 next.hasBeenLaunched = true;
1699 } else {
1700 if (SHOW_APP_STARTING_PREVIEW) {
1701 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001702 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001703 mService.compatibilityInfoForPackageLocked(
1704 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001705 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001706 next.labelRes, next.icon, next.windowFlags,
1707 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001708 }
1709 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1710 }
1711 startSpecificActivityLocked(next, true, true);
1712 }
1713
1714 return true;
1715 }
1716
1717 private final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001718 boolean doResume, boolean keepCurTransition, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001719 final int NH = mHistory.size();
1720
1721 int addPos = -1;
1722
1723 if (!newTask) {
1724 // If starting in an existing task, find where that is...
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001725 boolean startIt = true;
1726 for (int i = NH-1; i >= 0; i--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001727 ActivityRecord p = mHistory.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001728 if (p.finishing) {
1729 continue;
1730 }
1731 if (p.task == r.task) {
1732 // Here it is! Now, if this is not yet visible to the
1733 // user, then just add it without starting; it will
1734 // get started when the user navigates back to it.
1735 addPos = i+1;
1736 if (!startIt) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07001737 if (DEBUG_ADD_REMOVE) {
1738 RuntimeException here = new RuntimeException("here");
1739 here.fillInStackTrace();
1740 Slog.i(TAG, "Adding activity " + r + " to stack at " + addPos,
1741 here);
1742 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001743 mHistory.add(addPos, r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001744 r.putInHistory();
Dianne Hackbornbe707852011-11-11 14:32:10 -08001745 mService.mWindowManager.addAppToken(addPos, r.appToken, r.task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001746 r.info.screenOrientation, r.fullscreen);
1747 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001748 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001749 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001750 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001751 return;
1752 }
1753 break;
1754 }
1755 if (p.fullscreen) {
1756 startIt = false;
1757 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001758 }
1759 }
1760
1761 // Place a new activity at top of stack, so it is next to interact
1762 // with the user.
1763 if (addPos < 0) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001764 addPos = NH;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001765 }
1766
1767 // If we are not placing the new activity frontmost, we do not want
1768 // to deliver the onUserLeaving callback to the actual frontmost
1769 // activity
1770 if (addPos < NH) {
1771 mUserLeaving = false;
1772 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() behind front, mUserLeaving=false");
1773 }
1774
1775 // Slot the activity into the history stack and proceed
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07001776 if (DEBUG_ADD_REMOVE) {
1777 RuntimeException here = new RuntimeException("here");
1778 here.fillInStackTrace();
1779 Slog.i(TAG, "Adding activity " + r + " to stack at " + addPos, here);
1780 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001781 mHistory.add(addPos, r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001782 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001783 r.frontOfTask = newTask;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001784 if (NH > 0) {
1785 // We want to show the starting preview window if we are
1786 // switching to a new task, or the next activity's process is
1787 // not currently running.
1788 boolean showStartingIcon = newTask;
1789 ProcessRecord proc = r.app;
1790 if (proc == null) {
1791 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1792 }
1793 if (proc == null || proc.thread == null) {
1794 showStartingIcon = true;
1795 }
1796 if (DEBUG_TRANSITION) Slog.v(TAG,
1797 "Prepare open transition: starting " + r);
1798 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001799 mService.mWindowManager.prepareAppTransition(
1800 WindowManagerPolicy.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001801 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001802 } else {
1803 mService.mWindowManager.prepareAppTransition(newTask
1804 ? WindowManagerPolicy.TRANSIT_TASK_OPEN
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001805 : WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001806 mNoAnimActivities.remove(r);
1807 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001808 r.updateOptionsLocked(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001809 mService.mWindowManager.addAppToken(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001810 addPos, r.appToken, r.task.taskId, r.info.screenOrientation, r.fullscreen);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001811 boolean doShow = true;
1812 if (newTask) {
1813 // Even though this activity is starting fresh, we still need
1814 // to reset it to make sure we apply affinities to move any
1815 // existing activities from other tasks in to it.
1816 // If the caller has requested that the target task be
1817 // reset, then do so.
1818 if ((r.intent.getFlags()
1819 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1820 resetTaskIfNeededLocked(r, r);
1821 doShow = topRunningNonDelayedActivityLocked(null) == r;
1822 }
1823 }
1824 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1825 // Figure out if we are transitioning from another activity that is
1826 // "has the same starting icon" as the next one. This allows the
1827 // window manager to keep the previous window it had previously
1828 // created, if it still had one.
1829 ActivityRecord prev = mResumedActivity;
1830 if (prev != null) {
1831 // We don't want to reuse the previous starting preview if:
1832 // (1) The current activity is in a different task.
1833 if (prev.task != r.task) prev = null;
1834 // (2) The current activity is already displayed.
1835 else if (prev.nowVisible) prev = null;
1836 }
1837 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001838 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001839 mService.compatibilityInfoForPackageLocked(
1840 r.info.applicationInfo), r.nonLocalizedLabel,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001841 r.labelRes, r.icon, r.windowFlags,
1842 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001843 }
1844 } else {
1845 // If this is the first activity, don't do any fancy animations,
1846 // because there is nothing for it to animate on top of.
Dianne Hackbornbe707852011-11-11 14:32:10 -08001847 mService.mWindowManager.addAppToken(addPos, r.appToken, r.task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001848 r.info.screenOrientation, r.fullscreen);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001849 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001850 }
1851 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001852 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001853 }
1854
1855 if (doResume) {
1856 resumeTopActivityLocked(null);
1857 }
1858 }
1859
Dianne Hackbornbe707852011-11-11 14:32:10 -08001860 final void validateAppTokensLocked() {
1861 mValidateAppTokens.clear();
1862 mValidateAppTokens.ensureCapacity(mHistory.size());
1863 for (int i=0; i<mHistory.size(); i++) {
1864 mValidateAppTokens.add(mHistory.get(i).appToken);
1865 }
1866 mService.mWindowManager.validateAppTokens(mValidateAppTokens);
1867 }
1868
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001869 /**
1870 * Perform a reset of the given task, if needed as part of launching it.
1871 * Returns the new HistoryRecord at the top of the task.
1872 */
1873 private final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
1874 ActivityRecord newActivity) {
1875 boolean forceReset = (newActivity.info.flags
1876 &ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001877 if (ACTIVITY_INACTIVE_RESET_TIME > 0
1878 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001879 if ((newActivity.info.flags
1880 &ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
1881 forceReset = true;
1882 }
1883 }
1884
1885 final TaskRecord task = taskTop.task;
1886
1887 // We are going to move through the history list so that we can look
1888 // at each activity 'target' with 'below' either the interesting
1889 // activity immediately below it in the stack or null.
1890 ActivityRecord target = null;
1891 int targetI = 0;
1892 int taskTopI = -1;
1893 int replyChainEnd = -1;
1894 int lastReparentPos = -1;
1895 for (int i=mHistory.size()-1; i>=-1; i--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001896 ActivityRecord below = i >= 0 ? mHistory.get(i) : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001897
1898 if (below != null && below.finishing) {
1899 continue;
1900 }
Amith Yamasani04e0d262012-02-14 11:50:53 -08001901 // Don't check any lower in the stack if we're crossing a user boundary.
1902 if (below != null && below.userId != taskTop.userId) {
1903 break;
1904 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001905 if (target == null) {
1906 target = below;
1907 targetI = i;
1908 // If we were in the middle of a reply chain before this
1909 // task, it doesn't appear like the root of the chain wants
1910 // anything interesting, so drop it.
1911 replyChainEnd = -1;
1912 continue;
1913 }
1914
1915 final int flags = target.info.flags;
1916
1917 final boolean finishOnTaskLaunch =
1918 (flags&ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1919 final boolean allowTaskReparenting =
1920 (flags&ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1921
1922 if (target.task == task) {
1923 // We are inside of the task being reset... we'll either
1924 // finish this activity, push it out for another task,
1925 // or leave it as-is. We only do this
1926 // for activities that are not the root of the task (since
1927 // if we finish the root, we may no longer have the task!).
1928 if (taskTopI < 0) {
1929 taskTopI = targetI;
1930 }
1931 if (below != null && below.task == task) {
1932 final boolean clearWhenTaskReset =
1933 (target.intent.getFlags()
1934 &Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1935 if (!finishOnTaskLaunch && !clearWhenTaskReset && target.resultTo != null) {
1936 // If this activity is sending a reply to a previous
1937 // activity, we can't do anything with it now until
1938 // we reach the start of the reply chain.
1939 // XXX note that we are assuming the result is always
1940 // to the previous activity, which is almost always
1941 // the case but we really shouldn't count on.
1942 if (replyChainEnd < 0) {
1943 replyChainEnd = targetI;
1944 }
1945 } else if (!finishOnTaskLaunch && !clearWhenTaskReset && allowTaskReparenting
1946 && target.taskAffinity != null
1947 && !target.taskAffinity.equals(task.affinity)) {
1948 // If this activity has an affinity for another
1949 // task, then we need to move it out of here. We will
1950 // move it as far out of the way as possible, to the
1951 // bottom of the activity stack. This also keeps it
1952 // correctly ordered with any activities we previously
1953 // moved.
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001954 ActivityRecord p = mHistory.get(0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001955 if (target.taskAffinity != null
1956 && target.taskAffinity.equals(p.task.affinity)) {
1957 // If the activity currently at the bottom has the
1958 // same task affinity as the one we are moving,
1959 // then merge it into the same task.
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001960 target.setTask(p.task, p.thumbHolder, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001961 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1962 + " out to bottom task " + p.task);
1963 } else {
1964 mService.mCurTask++;
1965 if (mService.mCurTask <= 0) {
1966 mService.mCurTask = 1;
1967 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001968 target.setTask(new TaskRecord(mService.mCurTask, target.info, null),
1969 null, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001970 target.task.affinityIntent = target.intent;
1971 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1972 + " out to new task " + target.task);
1973 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001974 mService.mWindowManager.setAppGroupId(target.appToken, task.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001975 if (replyChainEnd < 0) {
1976 replyChainEnd = targetI;
1977 }
1978 int dstPos = 0;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001979 ThumbnailHolder curThumbHolder = target.thumbHolder;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001980 for (int srcPos=targetI; srcPos<=replyChainEnd; srcPos++) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001981 p = mHistory.get(srcPos);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001982 if (p.finishing) {
1983 continue;
1984 }
1985 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1986 + " out to target's task " + target.task);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001987 p.setTask(target.task, curThumbHolder, false);
1988 curThumbHolder = p.thumbHolder;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07001989 if (DEBUG_ADD_REMOVE) {
1990 RuntimeException here = new RuntimeException("here");
1991 here.fillInStackTrace();
1992 Slog.i(TAG, "Removing and adding activity " + p + " to stack at "
1993 + dstPos, here);
1994 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001995 mHistory.remove(srcPos);
1996 mHistory.add(dstPos, p);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001997 mService.mWindowManager.moveAppToken(dstPos, p.appToken);
1998 mService.mWindowManager.setAppGroupId(p.appToken, p.task.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001999 dstPos++;
2000 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002001 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002002 }
2003 i++;
2004 }
2005 if (taskTop == p) {
2006 taskTop = below;
2007 }
2008 if (taskTopI == replyChainEnd) {
2009 taskTopI = -1;
2010 }
2011 replyChainEnd = -1;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002012 } else if (forceReset || finishOnTaskLaunch
2013 || clearWhenTaskReset) {
2014 // If the activity should just be removed -- either
2015 // because it asks for it, or the task should be
2016 // cleared -- then finish it and anything that is
2017 // part of its reply chain.
2018 if (clearWhenTaskReset) {
2019 // In this case, we want to finish this activity
2020 // and everything above it, so be sneaky and pretend
2021 // like these are all in the reply chain.
2022 replyChainEnd = targetI+1;
2023 while (replyChainEnd < mHistory.size() &&
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002024 (mHistory.get(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002025 replyChainEnd)).task == task) {
2026 replyChainEnd++;
2027 }
2028 replyChainEnd--;
2029 } else if (replyChainEnd < 0) {
2030 replyChainEnd = targetI;
2031 }
2032 ActivityRecord p = null;
2033 for (int srcPos=targetI; srcPos<=replyChainEnd; srcPos++) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002034 p = mHistory.get(srcPos);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002035 if (p.finishing) {
2036 continue;
2037 }
2038 if (finishActivityLocked(p, srcPos,
2039 Activity.RESULT_CANCELED, null, "reset")) {
2040 replyChainEnd--;
2041 srcPos--;
2042 }
2043 }
2044 if (taskTop == p) {
2045 taskTop = below;
2046 }
2047 if (taskTopI == replyChainEnd) {
2048 taskTopI = -1;
2049 }
2050 replyChainEnd = -1;
2051 } else {
2052 // If we were in the middle of a chain, well the
2053 // activity that started it all doesn't want anything
2054 // special, so leave it all as-is.
2055 replyChainEnd = -1;
2056 }
2057 } else {
2058 // Reached the bottom of the task -- any reply chain
2059 // should be left as-is.
2060 replyChainEnd = -1;
2061 }
Dianne Hackbornae0a0a82011-12-07 14:03:01 -08002062
2063 } else if (target.resultTo != null && (below == null
2064 || below.task == target.task)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002065 // If this activity is sending a reply to a previous
2066 // activity, we can't do anything with it now until
2067 // we reach the start of the reply chain.
2068 // XXX note that we are assuming the result is always
2069 // to the previous activity, which is almost always
2070 // the case but we really shouldn't count on.
2071 if (replyChainEnd < 0) {
2072 replyChainEnd = targetI;
2073 }
2074
2075 } else if (taskTopI >= 0 && allowTaskReparenting
2076 && task.affinity != null
2077 && task.affinity.equals(target.taskAffinity)) {
2078 // We are inside of another task... if this activity has
2079 // an affinity for our task, then either remove it if we are
2080 // clearing or move it over to our task. Note that
2081 // we currently punt on the case where we are resetting a
2082 // task that is not at the top but who has activities above
2083 // with an affinity to it... this is really not a normal
2084 // case, and we will need to later pull that task to the front
2085 // and usually at that point we will do the reset and pick
2086 // up those remaining activities. (This only happens if
2087 // someone starts an activity in a new task from an activity
2088 // in a task that is not currently on top.)
2089 if (forceReset || finishOnTaskLaunch) {
2090 if (replyChainEnd < 0) {
2091 replyChainEnd = targetI;
2092 }
2093 ActivityRecord p = null;
Dianne Hackbornae0a0a82011-12-07 14:03:01 -08002094 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index "
2095 + targetI + " to " + replyChainEnd);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002096 for (int srcPos=targetI; srcPos<=replyChainEnd; srcPos++) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002097 p = mHistory.get(srcPos);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002098 if (p.finishing) {
2099 continue;
2100 }
2101 if (finishActivityLocked(p, srcPos,
2102 Activity.RESULT_CANCELED, null, "reset")) {
2103 taskTopI--;
2104 lastReparentPos--;
2105 replyChainEnd--;
2106 srcPos--;
2107 }
2108 }
2109 replyChainEnd = -1;
2110 } else {
2111 if (replyChainEnd < 0) {
2112 replyChainEnd = targetI;
2113 }
Dianne Hackbornae0a0a82011-12-07 14:03:01 -08002114 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting task at index "
2115 + targetI + " to " + replyChainEnd);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002116 for (int srcPos=replyChainEnd; srcPos>=targetI; srcPos--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002117 ActivityRecord p = mHistory.get(srcPos);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002118 if (p.finishing) {
2119 continue;
2120 }
2121 if (lastReparentPos < 0) {
2122 lastReparentPos = taskTopI;
2123 taskTop = p;
2124 } else {
2125 lastReparentPos--;
2126 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07002127 if (DEBUG_ADD_REMOVE) {
2128 RuntimeException here = new RuntimeException("here");
2129 here.fillInStackTrace();
2130 Slog.i(TAG, "Removing and adding activity " + p + " to stack at "
2131 + lastReparentPos, here);
2132 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002133 mHistory.remove(srcPos);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002134 p.setTask(task, null, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002135 mHistory.add(lastReparentPos, p);
2136 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p
Dianne Hackbornae0a0a82011-12-07 14:03:01 -08002137 + " from " + srcPos + " to " + lastReparentPos
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002138 + " in to resetting task " + task);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002139 mService.mWindowManager.moveAppToken(lastReparentPos, p.appToken);
2140 mService.mWindowManager.setAppGroupId(p.appToken, p.task.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002141 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002142 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002143 }
2144 }
2145 replyChainEnd = -1;
2146
2147 // Now we've moved it in to place... but what if this is
2148 // a singleTop activity and we have put it on top of another
2149 // instance of the same activity? Then we drop the instance
2150 // below so it remains singleTop.
2151 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2152 for (int j=lastReparentPos-1; j>=0; j--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002153 ActivityRecord p = mHistory.get(j);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002154 if (p.finishing) {
2155 continue;
2156 }
2157 if (p.intent.getComponent().equals(target.intent.getComponent())) {
2158 if (finishActivityLocked(p, j,
2159 Activity.RESULT_CANCELED, null, "replace")) {
2160 taskTopI--;
2161 lastReparentPos--;
2162 }
2163 }
2164 }
2165 }
2166 }
Dianne Hackbornae0a0a82011-12-07 14:03:01 -08002167
2168 } else if (below != null && below.task != target.task) {
2169 // We hit the botton of a task; the reply chain can't
2170 // pass through it.
2171 replyChainEnd = -1;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002172 }
2173
2174 target = below;
2175 targetI = i;
2176 }
2177
2178 return taskTop;
2179 }
2180
2181 /**
2182 * Perform clear operation as requested by
2183 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
2184 * stack to the given task, then look for
2185 * an instance of that activity in the stack and, if found, finish all
2186 * activities on top of it and return the instance.
2187 *
2188 * @param newR Description of the new activity being started.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002189 * @return Returns the old activity that should be continued to be used,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002190 * or null if none was found.
2191 */
2192 private final ActivityRecord performClearTaskLocked(int taskId,
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002193 ActivityRecord newR, int launchFlags) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002194 int i = mHistory.size();
2195
2196 // First find the requested task.
2197 while (i > 0) {
2198 i--;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002199 ActivityRecord r = mHistory.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002200 if (r.task.taskId == taskId) {
2201 i++;
2202 break;
2203 }
2204 }
2205
2206 // Now clear it.
2207 while (i > 0) {
2208 i--;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002209 ActivityRecord r = mHistory.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002210 if (r.finishing) {
2211 continue;
2212 }
2213 if (r.task.taskId != taskId) {
2214 return null;
2215 }
2216 if (r.realActivity.equals(newR.realActivity)) {
2217 // Here it is! Now finish everything in front...
2218 ActivityRecord ret = r;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002219 while (i < (mHistory.size()-1)) {
2220 i++;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002221 r = mHistory.get(i);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002222 if (r.task.taskId != taskId) {
2223 break;
2224 }
2225 if (r.finishing) {
2226 continue;
2227 }
2228 if (finishActivityLocked(r, i, Activity.RESULT_CANCELED,
2229 null, "clear")) {
2230 i--;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002231 }
2232 }
2233
2234 // Finally, if this is a normal launch mode (that is, not
2235 // expecting onNewIntent()), then we will finish the current
2236 // instance of the activity so a new fresh one can be started.
2237 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
2238 && (launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0) {
2239 if (!ret.finishing) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002240 int index = indexOfTokenLocked(ret.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002241 if (index >= 0) {
2242 finishActivityLocked(ret, index, Activity.RESULT_CANCELED,
2243 null, "clear");
2244 }
2245 return null;
2246 }
2247 }
2248
2249 return ret;
2250 }
2251 }
2252
2253 return null;
2254 }
2255
2256 /**
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002257 * Completely remove all activities associated with an existing
2258 * task starting at a specified index.
2259 */
2260 private final void performClearTaskAtIndexLocked(int taskId, int i) {
Dianne Hackborneabd3282011-10-13 16:26:49 -07002261 while (i < mHistory.size()) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002262 ActivityRecord r = mHistory.get(i);
2263 if (r.task.taskId != taskId) {
2264 // Whoops hit the end.
2265 return;
2266 }
2267 if (r.finishing) {
2268 i++;
2269 continue;
2270 }
2271 if (!finishActivityLocked(r, i, Activity.RESULT_CANCELED,
2272 null, "clear")) {
2273 i++;
2274 }
2275 }
2276 }
2277
2278 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002279 * Completely remove all activities associated with an existing task.
2280 */
2281 private final void performClearTaskLocked(int taskId) {
2282 int i = mHistory.size();
2283
2284 // First find the requested task.
2285 while (i > 0) {
2286 i--;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002287 ActivityRecord r = mHistory.get(i);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002288 if (r.task.taskId == taskId) {
2289 i++;
2290 break;
2291 }
2292 }
2293
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002294 // Now find the start and clear it.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002295 while (i > 0) {
2296 i--;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002297 ActivityRecord r = mHistory.get(i);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002298 if (r.finishing) {
2299 continue;
2300 }
2301 if (r.task.taskId != taskId) {
2302 // We hit the bottom. Now finish it all...
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002303 performClearTaskAtIndexLocked(taskId, i+1);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002304 return;
2305 }
2306 }
2307 }
2308
2309 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002310 * Find the activity in the history stack within the given task. Returns
2311 * the index within the history at which it's found, or < 0 if not found.
2312 */
2313 private final int findActivityInHistoryLocked(ActivityRecord r, int task) {
2314 int i = mHistory.size();
2315 while (i > 0) {
2316 i--;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002317 ActivityRecord candidate = mHistory.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002318 if (candidate.task.taskId != task) {
2319 break;
2320 }
2321 if (candidate.realActivity.equals(r.realActivity)) {
2322 return i;
2323 }
2324 }
2325
2326 return -1;
2327 }
2328
2329 /**
2330 * Reorder the history stack so that the activity at the given index is
2331 * brought to the front.
2332 */
2333 private final ActivityRecord moveActivityToFrontLocked(int where) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002334 ActivityRecord newTop = mHistory.remove(where);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002335 int top = mHistory.size();
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002336 ActivityRecord oldTop = mHistory.get(top-1);
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07002337 if (DEBUG_ADD_REMOVE) {
2338 RuntimeException here = new RuntimeException("here");
2339 here.fillInStackTrace();
2340 Slog.i(TAG, "Removing and adding activity " + newTop + " to stack at "
2341 + top, here);
2342 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002343 mHistory.add(top, newTop);
2344 oldTop.frontOfTask = false;
2345 newTop.frontOfTask = true;
2346 return newTop;
2347 }
2348
2349 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002350 Intent intent, String resolvedType, ActivityInfo aInfo, IBinder resultTo,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002351 String resultWho, int requestCode,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002352 int callingPid, int callingUid, int startFlags, Bundle options,
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002353 boolean componentSpecified, ActivityRecord[] outActivity) {
Dianne Hackbornefb58102010-10-14 16:47:34 -07002354
Dianne Hackborna4972e92012-03-14 10:38:05 -07002355 int err = ActivityManager.START_SUCCESS;
Dianne Hackbornefb58102010-10-14 16:47:34 -07002356
2357 ProcessRecord callerApp = null;
2358 if (caller != null) {
2359 callerApp = mService.getRecordForAppLocked(caller);
2360 if (callerApp != null) {
2361 callingPid = callerApp.pid;
2362 callingUid = callerApp.info.uid;
2363 } else {
2364 Slog.w(TAG, "Unable to find app for caller " + caller
2365 + " (pid=" + callingPid + ") when starting: "
2366 + intent.toString());
Dianne Hackborna4972e92012-03-14 10:38:05 -07002367 err = ActivityManager.START_PERMISSION_DENIED;
Dianne Hackbornefb58102010-10-14 16:47:34 -07002368 }
2369 }
2370
Dianne Hackborna4972e92012-03-14 10:38:05 -07002371 if (err == ActivityManager.START_SUCCESS) {
Amith Yamasania4a54e22012-04-16 15:44:19 -07002372 final int userId = aInfo != null ? UserId.getUserId(aInfo.applicationInfo.uid) : 0;
Dianne Hackborn21c241e2012-03-08 13:57:23 -08002373 Slog.i(TAG, "START {" + intent.toShortString(true, true, true, false)
Amith Yamasania4a54e22012-04-16 15:44:19 -07002374 + " u=" + userId + "} from pid " + (callerApp != null ? callerApp.pid : callingPid));
Dianne Hackbornefb58102010-10-14 16:47:34 -07002375 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002376
2377 ActivityRecord sourceRecord = null;
2378 ActivityRecord resultRecord = null;
2379 if (resultTo != null) {
2380 int index = indexOfTokenLocked(resultTo);
2381 if (DEBUG_RESULTS) Slog.v(
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07002382 TAG, "Will send result to " + resultTo + " (index " + index + ")");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002383 if (index >= 0) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002384 sourceRecord = mHistory.get(index);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002385 if (requestCode >= 0 && !sourceRecord.finishing) {
2386 resultRecord = sourceRecord;
2387 }
2388 }
2389 }
2390
2391 int launchFlags = intent.getFlags();
2392
2393 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
2394 && sourceRecord != null) {
2395 // Transfer the result target from the source activity to the new
2396 // one being started, including any failures.
2397 if (requestCode >= 0) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002398 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002399 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002400 }
2401 resultRecord = sourceRecord.resultTo;
2402 resultWho = sourceRecord.resultWho;
2403 requestCode = sourceRecord.requestCode;
2404 sourceRecord.resultTo = null;
2405 if (resultRecord != null) {
2406 resultRecord.removeResultsLocked(
2407 sourceRecord, resultWho, requestCode);
2408 }
2409 }
2410
Dianne Hackborna4972e92012-03-14 10:38:05 -07002411 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002412 // We couldn't find a class that can handle the given Intent.
2413 // That's the end of that!
Dianne Hackborna4972e92012-03-14 10:38:05 -07002414 err = ActivityManager.START_INTENT_NOT_RESOLVED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002415 }
2416
Dianne Hackborna4972e92012-03-14 10:38:05 -07002417 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002418 // We couldn't find the specific class specified in the Intent.
2419 // Also the end of the line.
Dianne Hackborna4972e92012-03-14 10:38:05 -07002420 err = ActivityManager.START_CLASS_NOT_FOUND;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002421 }
2422
Dianne Hackborna4972e92012-03-14 10:38:05 -07002423 if (err != ActivityManager.START_SUCCESS) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002424 if (resultRecord != null) {
2425 sendActivityResultLocked(-1,
2426 resultRecord, resultWho, requestCode,
2427 Activity.RESULT_CANCELED, null);
2428 }
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002429 mDismissKeyguardOnNextActivity = false;
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002430 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002431 return err;
2432 }
2433
Jeff Sharkey35be7562012-04-18 19:16:15 -07002434 final int startAnyPerm = mService.checkPermission(
2435 START_ANY_ACTIVITY, callingPid, callingUid);
2436 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08002437 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
Jeff Sharkey35be7562012-04-18 19:16:15 -07002438 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002439 if (resultRecord != null) {
2440 sendActivityResultLocked(-1,
2441 resultRecord, resultWho, requestCode,
2442 Activity.RESULT_CANCELED, null);
2443 }
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002444 mDismissKeyguardOnNextActivity = false;
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08002445 String msg;
2446 if (!aInfo.exported) {
2447 msg = "Permission Denial: starting " + intent.toString()
2448 + " from " + callerApp + " (pid=" + callingPid
2449 + ", uid=" + callingUid + ")"
2450 + " not exported from uid " + aInfo.applicationInfo.uid;
2451 } else {
2452 msg = "Permission Denial: starting " + intent.toString()
2453 + " from " + callerApp + " (pid=" + callingPid
2454 + ", uid=" + callingUid + ")"
2455 + " requires " + aInfo.permission;
2456 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002457 Slog.w(TAG, msg);
2458 throw new SecurityException(msg);
2459 }
2460
2461 if (mMainStack) {
2462 if (mService.mController != null) {
2463 boolean abort = false;
2464 try {
2465 // The Intent we give to the watcher has the extra data
2466 // stripped off, since it can contain private information.
2467 Intent watchIntent = intent.cloneFilter();
2468 abort = !mService.mController.activityStarting(watchIntent,
2469 aInfo.applicationInfo.packageName);
2470 } catch (RemoteException e) {
2471 mService.mController = null;
2472 }
2473
2474 if (abort) {
2475 if (resultRecord != null) {
2476 sendActivityResultLocked(-1,
2477 resultRecord, resultWho, requestCode,
2478 Activity.RESULT_CANCELED, null);
2479 }
2480 // We pretend to the caller that it was really started, but
2481 // they will just get a cancel result.
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002482 mDismissKeyguardOnNextActivity = false;
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002483 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002484 return ActivityManager.START_SUCCESS;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002485 }
2486 }
2487 }
Amith Yamasani742a6712011-05-04 14:49:28 -07002488
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002489 ActivityRecord r = new ActivityRecord(mService, this, callerApp, callingUid,
2490 intent, resolvedType, aInfo, mService.mConfiguration,
2491 resultRecord, resultWho, requestCode, componentSpecified);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002492 if (outActivity != null) {
2493 outActivity[0] = r;
2494 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002495
2496 if (mMainStack) {
2497 if (mResumedActivity == null
2498 || mResumedActivity.info.applicationInfo.uid != callingUid) {
2499 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) {
2500 PendingActivityLaunch pal = new PendingActivityLaunch();
2501 pal.r = r;
2502 pal.sourceRecord = sourceRecord;
Dianne Hackborna4972e92012-03-14 10:38:05 -07002503 pal.startFlags = startFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002504 mService.mPendingActivityLaunches.add(pal);
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002505 mDismissKeyguardOnNextActivity = false;
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002506 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002507 return ActivityManager.START_SWITCHES_CANCELED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002508 }
2509 }
2510
2511 if (mService.mDidAppSwitch) {
2512 // This is the second allowed switch since we stopped switches,
2513 // so now just generally allow switches. Use case: user presses
2514 // home (switches disabled, switch to home, mDidAppSwitch now true);
2515 // user taps a home icon (coming from home so allowed, we hit here
2516 // and now allow anyone to switch again).
2517 mService.mAppSwitchesAllowedTime = 0;
2518 } else {
2519 mService.mDidAppSwitch = true;
2520 }
2521
2522 mService.doPendingActivityLaunchesLocked(false);
2523 }
2524
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002525 err = startActivityUncheckedLocked(r, sourceRecord,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002526 startFlags, true, options);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002527 if (mDismissKeyguardOnNextActivity && mPausingActivity == null) {
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002528 // Someone asked to have the keyguard dismissed on the next
2529 // activity start, but we are not actually doing an activity
2530 // switch... just dismiss the keyguard now, because we
2531 // probably want to see whatever is behind it.
2532 mDismissKeyguardOnNextActivity = false;
2533 mService.mWindowManager.dismissKeyguard();
2534 }
2535 return err;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002536 }
2537
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002538 final void moveHomeToFrontFromLaunchLocked(int launchFlags) {
2539 if ((launchFlags &
2540 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
2541 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
2542 // Caller wants to appear on home activity, so before starting
2543 // their own activity we will bring home to the front.
2544 moveHomeToFrontLocked();
2545 }
2546 }
2547
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002548 final int startActivityUncheckedLocked(ActivityRecord r,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002549 ActivityRecord sourceRecord, int startFlags, boolean doResume,
2550 Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002551 final Intent intent = r.intent;
2552 final int callingUid = r.launchedFromUid;
Amith Yamasani742a6712011-05-04 14:49:28 -07002553 final int userId = r.userId;
2554
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002555 int launchFlags = intent.getFlags();
2556
2557 // We'll invoke onUserLeaving before onPause only if the launching
2558 // activity did not explicitly state that this is an automated launch.
2559 mUserLeaving = (launchFlags&Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
2560 if (DEBUG_USER_LEAVING) Slog.v(TAG,
2561 "startActivity() => mUserLeaving=" + mUserLeaving);
2562
2563 // If the caller has asked not to resume at this point, we make note
2564 // of this in the record so that we can skip it when trying to find
2565 // the top running activity.
2566 if (!doResume) {
2567 r.delayedResume = true;
2568 }
2569
2570 ActivityRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)
2571 != 0 ? r : null;
2572
2573 // If the onlyIfNeeded flag is set, then we can do this if the activity
2574 // being launched is the same as the one making the call... or, as
2575 // a special case, if we do not know the caller then we count the
2576 // current top activity as the caller.
Dianne Hackborna4972e92012-03-14 10:38:05 -07002577 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002578 ActivityRecord checkedCaller = sourceRecord;
2579 if (checkedCaller == null) {
2580 checkedCaller = topRunningNonDelayedActivityLocked(notTop);
2581 }
2582 if (!checkedCaller.realActivity.equals(r.realActivity)) {
2583 // Caller is not the same as launcher, so always needed.
Dianne Hackborna4972e92012-03-14 10:38:05 -07002584 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002585 }
2586 }
2587
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002588 if (sourceRecord == null) {
2589 // This activity is not being started from another... in this
2590 // case we -always- start a new task.
2591 if ((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2592 Slog.w(TAG, "startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: "
2593 + intent);
2594 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2595 }
2596 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2597 // The original activity who is starting us is running as a single
2598 // instance... this new activity it is starting must go on its
2599 // own task.
2600 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2601 } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
2602 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
2603 // The activity being started is a single instance... it always
2604 // gets launched into its own task.
2605 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2606 }
2607
2608 if (r.resultTo != null && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
2609 // For whatever reason this activity is being launched into a new
2610 // task... yet the caller has requested a result back. Well, that
2611 // is pretty messed up, so instead immediately send back a cancel
2612 // and let the new task continue launched as normal without a
2613 // dependency on its originator.
2614 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
2615 sendActivityResultLocked(-1,
2616 r.resultTo, r.resultWho, r.requestCode,
2617 Activity.RESULT_CANCELED, null);
2618 r.resultTo = null;
2619 }
2620
2621 boolean addingToTask = false;
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002622 boolean movedHome = false;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002623 TaskRecord reuseTask = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002624 if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2625 (launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
2626 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
2627 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2628 // If bring to front is requested, and no result is requested, and
2629 // we can find a task that was started with this same
2630 // component, then instead of launching bring that one to the front.
2631 if (r.resultTo == null) {
2632 // See if there is a task to bring to the front. If this is
2633 // a SINGLE_INSTANCE activity, there can be one and only one
2634 // instance of it in the history, and it is always in its own
2635 // unique task, so we do a special search.
2636 ActivityRecord taskTop = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
2637 ? findTaskLocked(intent, r.info)
2638 : findActivityLocked(intent, r.info);
2639 if (taskTop != null) {
2640 if (taskTop.task.intent == null) {
2641 // This task was started because of movement of
2642 // the activity based on affinity... now that we
2643 // are actually launching it, we can assign the
2644 // base intent.
2645 taskTop.task.setIntent(intent, r.info);
2646 }
2647 // If the target task is not in the front, then we need
2648 // to bring it to the front... except... well, with
2649 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2650 // to have the same behavior as if a new instance was
2651 // being started, which means not bringing it to the front
2652 // if the caller is not itself in the front.
2653 ActivityRecord curTop = topRunningNonDelayedActivityLocked(notTop);
Jean-Baptiste Queru66a5d692010-10-25 17:27:16 -07002654 if (curTop != null && curTop.task != taskTop.task) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002655 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
2656 boolean callerAtFront = sourceRecord == null
2657 || curTop.task == sourceRecord.task;
2658 if (callerAtFront) {
2659 // We really do want to push this one into the
2660 // user's face, right now.
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002661 movedHome = true;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002662 moveHomeToFrontFromLaunchLocked(launchFlags);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002663 moveTaskToFrontLocked(taskTop.task, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002664 }
2665 }
2666 // If the caller has requested that the target task be
2667 // reset, then do so.
2668 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2669 taskTop = resetTaskIfNeededLocked(taskTop, r);
2670 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002671 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002672 // We don't need to start a new activity, and
2673 // the client said not to do anything if that
2674 // is the case, so this is it! And for paranoia, make
2675 // sure we have correctly resumed the top activity.
2676 if (doResume) {
2677 resumeTopActivityLocked(null);
2678 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002679 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002680 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002681 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002682 if ((launchFlags &
2683 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
2684 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
2685 // The caller has requested to completely replace any
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08002686 // existing task with its new activity. Well that should
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002687 // not be too hard...
2688 reuseTask = taskTop.task;
2689 performClearTaskLocked(taskTop.task.taskId);
2690 reuseTask.setIntent(r.intent, r.info);
2691 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002692 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
2693 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2694 // In this situation we want to remove all activities
2695 // from the task up to the one being started. In most
2696 // cases this means we are resetting the task to its
2697 // initial state.
2698 ActivityRecord top = performClearTaskLocked(
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002699 taskTop.task.taskId, r, launchFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002700 if (top != null) {
2701 if (top.frontOfTask) {
2702 // Activity aliases may mean we use different
2703 // intents for the top activity, so make sure
2704 // the task now has the identity of the new
2705 // intent.
2706 top.task.setIntent(r.intent, r.info);
2707 }
2708 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn39792d22010-08-19 18:01:52 -07002709 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002710 } else {
2711 // A special case: we need to
2712 // start the activity because it is not currently
2713 // running, and the caller has asked to clear the
2714 // current task to have this activity at the top.
2715 addingToTask = true;
2716 // Now pretend like this activity is being started
2717 // by the top of its task, so it is put in the
2718 // right place.
2719 sourceRecord = taskTop;
2720 }
2721 } else if (r.realActivity.equals(taskTop.task.realActivity)) {
2722 // In this case the top activity on the task is the
2723 // same as the one being launched, so we take that
2724 // as a request to bring the task to the foreground.
2725 // If the top activity in the task is the root
2726 // activity, deliver this new intent to it if it
2727 // desires.
2728 if ((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2729 && taskTop.realActivity.equals(r.realActivity)) {
2730 logStartActivity(EventLogTags.AM_NEW_INTENT, r, taskTop.task);
2731 if (taskTop.frontOfTask) {
2732 taskTop.task.setIntent(r.intent, r.info);
2733 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07002734 taskTop.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002735 } else if (!r.intent.filterEquals(taskTop.task.intent)) {
2736 // In this case we are launching the root activity
2737 // of the task, but with a different intent. We
2738 // should start a new instance on top.
2739 addingToTask = true;
2740 sourceRecord = taskTop;
2741 }
2742 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2743 // In this case an activity is being launched in to an
2744 // existing task, without resetting that task. This
2745 // is typically the situation of launching an activity
2746 // from a notification or shortcut. We want to place
2747 // the new activity on top of the current task.
2748 addingToTask = true;
2749 sourceRecord = taskTop;
2750 } else if (!taskTop.task.rootWasReset) {
2751 // In this case we are launching in to an existing task
2752 // that has not yet been started from its front door.
2753 // The current task has been brought to the front.
2754 // Ideally, we'd probably like to place this new task
2755 // at the bottom of its stack, but that's a little hard
2756 // to do with the current organization of the code so
2757 // for now we'll just drop it.
2758 taskTop.task.setIntent(r.intent, r.info);
2759 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002760 if (!addingToTask && reuseTask == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002761 // We didn't do anything... but it was needed (a.k.a., client
2762 // don't use that intent!) And for paranoia, make
2763 // sure we have correctly resumed the top activity.
2764 if (doResume) {
2765 resumeTopActivityLocked(null);
2766 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002767 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002768 return ActivityManager.START_TASK_TO_FRONT;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002769 }
2770 }
2771 }
2772 }
2773
2774 //String uri = r.intent.toURI();
2775 //Intent intent2 = new Intent(uri);
2776 //Slog.i(TAG, "Given intent: " + r.intent);
2777 //Slog.i(TAG, "URI is: " + uri);
2778 //Slog.i(TAG, "To intent: " + intent2);
2779
2780 if (r.packageName != null) {
2781 // If the activity being launched is the same as the one currently
2782 // at the top, then we need to check if it should only be launched
2783 // once.
2784 ActivityRecord top = topRunningNonDelayedActivityLocked(notTop);
2785 if (top != null && r.resultTo == null) {
Amith Yamasani742a6712011-05-04 14:49:28 -07002786 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002787 if (top.app != null && top.app.thread != null) {
2788 if ((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2789 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP
2790 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
2791 logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2792 // For paranoia, make sure we have correctly
2793 // resumed the top activity.
2794 if (doResume) {
2795 resumeTopActivityLocked(null);
2796 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002797 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002798 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002799 // We don't need to start a new activity, and
2800 // the client said not to do anything if that
2801 // is the case, so this is it!
Dianne Hackborna4972e92012-03-14 10:38:05 -07002802 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002803 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07002804 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002805 return ActivityManager.START_DELIVERED_TO_TOP;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002806 }
2807 }
2808 }
2809 }
2810
2811 } else {
2812 if (r.resultTo != null) {
2813 sendActivityResultLocked(-1,
2814 r.resultTo, r.resultWho, r.requestCode,
2815 Activity.RESULT_CANCELED, null);
2816 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002817 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002818 return ActivityManager.START_CLASS_NOT_FOUND;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002819 }
2820
2821 boolean newTask = false;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08002822 boolean keepCurTransition = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002823
2824 // Should this be considered a new task?
2825 if (r.resultTo == null && !addingToTask
2826 && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002827 if (reuseTask == null) {
2828 // todo: should do better management of integers.
2829 mService.mCurTask++;
2830 if (mService.mCurTask <= 0) {
2831 mService.mCurTask = 1;
2832 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002833 r.setTask(new TaskRecord(mService.mCurTask, r.info, intent), null, true);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002834 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2835 + " in new task " + r.task);
2836 } else {
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002837 r.setTask(reuseTask, reuseTask, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002838 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002839 newTask = true;
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002840 if (!movedHome) {
2841 moveHomeToFrontFromLaunchLocked(launchFlags);
2842 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002843
2844 } else if (sourceRecord != null) {
2845 if (!addingToTask &&
2846 (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2847 // In this case, we are adding the activity to an existing
2848 // task, but the caller has asked to clear that task if the
2849 // activity is already running.
2850 ActivityRecord top = performClearTaskLocked(
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002851 sourceRecord.task.taskId, r, launchFlags);
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08002852 keepCurTransition = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002853 if (top != null) {
2854 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn39792d22010-08-19 18:01:52 -07002855 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002856 // For paranoia, make sure we have correctly
2857 // resumed the top activity.
2858 if (doResume) {
2859 resumeTopActivityLocked(null);
2860 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002861 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002862 return ActivityManager.START_DELIVERED_TO_TOP;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002863 }
2864 } else if (!addingToTask &&
2865 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2866 // In this case, we are launching an activity in our own task
2867 // that may already be running somewhere in the history, and
2868 // we want to shuffle it to the front of the stack if so.
2869 int where = findActivityInHistoryLocked(r, sourceRecord.task.taskId);
2870 if (where >= 0) {
2871 ActivityRecord top = moveActivityToFrontLocked(where);
2872 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002873 top.updateOptionsLocked(options);
Dianne Hackborn39792d22010-08-19 18:01:52 -07002874 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002875 if (doResume) {
2876 resumeTopActivityLocked(null);
2877 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002878 return ActivityManager.START_DELIVERED_TO_TOP;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002879 }
2880 }
2881 // An existing activity is starting this new activity, so we want
2882 // to keep the new one in the same task as the one that is starting
2883 // it.
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002884 r.setTask(sourceRecord.task, sourceRecord.thumbHolder, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002885 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2886 + " in existing task " + r.task);
2887
2888 } else {
2889 // This not being started from an existing activity, and not part
2890 // of a new task... just put it in the top task, though these days
2891 // this case should never happen.
2892 final int N = mHistory.size();
2893 ActivityRecord prev =
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002894 N > 0 ? mHistory.get(N-1) : null;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002895 r.setTask(prev != null
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002896 ? prev.task
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002897 : new TaskRecord(mService.mCurTask, r.info, intent), null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002898 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2899 + " in new guessed " + r.task);
2900 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07002901
Dianne Hackborn39792d22010-08-19 18:01:52 -07002902 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002903 intent, r.getUriPermissionsLocked());
Dianne Hackborn39792d22010-08-19 18:01:52 -07002904
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002905 if (newTask) {
2906 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.task.taskId);
2907 }
2908 logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002909 startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002910 return ActivityManager.START_SUCCESS;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002911 }
2912
Dianne Hackborna4972e92012-03-14 10:38:05 -07002913 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Amith Yamasani483f3b02012-03-13 16:08:00 -07002914 String profileFile, ParcelFileDescriptor profileFd, int userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002915 // Collect information about the target of the Intent.
2916 ActivityInfo aInfo;
2917 try {
2918 ResolveInfo rInfo =
2919 AppGlobals.getPackageManager().resolveIntent(
2920 intent, resolvedType,
2921 PackageManager.MATCH_DEFAULT_ONLY
Amith Yamasani483f3b02012-03-13 16:08:00 -07002922 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002923 aInfo = rInfo != null ? rInfo.activityInfo : null;
2924 } catch (RemoteException e) {
2925 aInfo = null;
2926 }
2927
2928 if (aInfo != null) {
2929 // Store the found target back into the intent, because now that
2930 // we have it we never want to do this again. For example, if the
2931 // user navigates back to this point in the history, we should
2932 // always restart the exact same activity.
2933 intent.setComponent(new ComponentName(
2934 aInfo.applicationInfo.packageName, aInfo.name));
2935
2936 // Don't debug things in the system process
Dianne Hackborna4972e92012-03-14 10:38:05 -07002937 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002938 if (!aInfo.processName.equals("system")) {
2939 mService.setDebugApp(aInfo.processName, true, false);
2940 }
2941 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002942
Dianne Hackborna4972e92012-03-14 10:38:05 -07002943 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
Siva Velusamy92a8b222012-03-09 16:24:04 -08002944 if (!aInfo.processName.equals("system")) {
2945 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
2946 }
2947 }
2948
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002949 if (profileFile != null) {
2950 if (!aInfo.processName.equals("system")) {
2951 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002952 profileFile, profileFd,
2953 (startFlags&ActivityManager.START_FLAG_AUTO_STOP_PROFILER) != 0);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002954 }
2955 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002956 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002957 return aInfo;
2958 }
2959
2960 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002961 Intent intent, String resolvedType, IBinder resultTo,
2962 String resultWho, int requestCode, int startFlags, String profileFile,
2963 ParcelFileDescriptor profileFd, WaitResult outResult, Configuration config,
2964 Bundle options, int userId) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002965 // Refuse possible leaked file descriptors
2966 if (intent != null && intent.hasFileDescriptors()) {
2967 throw new IllegalArgumentException("File descriptors passed in Intent");
2968 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002969 boolean componentSpecified = intent.getComponent() != null;
2970
2971 // Don't modify the client's object!
2972 intent = new Intent(intent);
2973
2974 // Collect information about the target of the Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07002975 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
Amith Yamasani483f3b02012-03-13 16:08:00 -07002976 profileFile, profileFd, userId);
Amith Yamasani95a6a962012-04-18 09:54:43 -07002977 if (aInfo != null && mService.isSingleton(aInfo.processName, aInfo.applicationInfo)) {
Amith Yamasania4a54e22012-04-16 15:44:19 -07002978 userId = 0;
2979 }
Amith Yamasani742a6712011-05-04 14:49:28 -07002980 aInfo = mService.getActivityInfoForUser(aInfo, userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002981
2982 synchronized (mService) {
2983 int callingPid;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002984 if (callingUid >= 0) {
2985 callingPid = -1;
2986 } else if (caller == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002987 callingPid = Binder.getCallingPid();
2988 callingUid = Binder.getCallingUid();
2989 } else {
2990 callingPid = callingUid = -1;
2991 }
2992
2993 mConfigWillChange = config != null
2994 && mService.mConfiguration.diff(config) != 0;
2995 if (DEBUG_CONFIGURATION) Slog.v(TAG,
2996 "Starting activity when config will change = " + mConfigWillChange);
2997
2998 final long origId = Binder.clearCallingIdentity();
2999
3000 if (mMainStack && aInfo != null &&
Dianne Hackborn54e570f2010-10-04 18:32:32 -07003001 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003002 // This may be a heavy-weight process! Check to see if we already
3003 // have another, different heavy-weight process running.
3004 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
3005 if (mService.mHeavyWeightProcess != null &&
3006 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
3007 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
3008 int realCallingPid = callingPid;
3009 int realCallingUid = callingUid;
3010 if (caller != null) {
3011 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
3012 if (callerApp != null) {
3013 realCallingPid = callerApp.pid;
3014 realCallingUid = callerApp.info.uid;
3015 } else {
3016 Slog.w(TAG, "Unable to find app for caller " + caller
3017 + " (pid=" + realCallingPid + ") when starting: "
3018 + intent.toString());
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003019 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07003020 return ActivityManager.START_PERMISSION_DENIED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003021 }
3022 }
3023
3024 IIntentSender target = mService.getIntentSenderLocked(
Dianne Hackborna4972e92012-03-14 10:38:05 -07003025 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003026 realCallingUid, null, null, 0, new Intent[] { intent },
3027 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07003028 | PendingIntent.FLAG_ONE_SHOT, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003029
3030 Intent newIntent = new Intent();
3031 if (requestCode >= 0) {
3032 // Caller is requesting a result.
3033 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
3034 }
3035 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
3036 new IntentSender(target));
3037 if (mService.mHeavyWeightProcess.activities.size() > 0) {
3038 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
3039 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
3040 hist.packageName);
3041 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
3042 hist.task.taskId);
3043 }
3044 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
3045 aInfo.packageName);
3046 newIntent.setFlags(intent.getFlags());
3047 newIntent.setClassName("android",
3048 HeavyWeightSwitcherActivity.class.getName());
3049 intent = newIntent;
3050 resolvedType = null;
3051 caller = null;
3052 callingUid = Binder.getCallingUid();
3053 callingPid = Binder.getCallingPid();
3054 componentSpecified = true;
3055 try {
3056 ResolveInfo rInfo =
3057 AppGlobals.getPackageManager().resolveIntent(
3058 intent, null,
3059 PackageManager.MATCH_DEFAULT_ONLY
Amith Yamasani483f3b02012-03-13 16:08:00 -07003060 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003061 aInfo = rInfo != null ? rInfo.activityInfo : null;
Amith Yamasani742a6712011-05-04 14:49:28 -07003062 aInfo = mService.getActivityInfoForUser(aInfo, userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003063 } catch (RemoteException e) {
3064 aInfo = null;
3065 }
3066 }
3067 }
3068 }
3069
3070 int res = startActivityLocked(caller, intent, resolvedType,
Dianne Hackborna4972e92012-03-14 10:38:05 -07003071 aInfo, resultTo, resultWho, requestCode, callingPid, callingUid,
3072 startFlags, options, componentSpecified, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003073
3074 if (mConfigWillChange && mMainStack) {
3075 // If the caller also wants to switch to a new configuration,
3076 // do so now. This allows a clean switch, as we are waiting
3077 // for the current activity to pause (so we will not destroy
3078 // it), and have not yet started the next activity.
3079 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
3080 "updateConfiguration()");
3081 mConfigWillChange = false;
3082 if (DEBUG_CONFIGURATION) Slog.v(TAG,
3083 "Updating to new configuration after starting activity.");
Dianne Hackborn813075a62011-11-14 17:45:19 -08003084 mService.updateConfigurationLocked(config, null, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003085 }
3086
3087 Binder.restoreCallingIdentity(origId);
3088
3089 if (outResult != null) {
3090 outResult.result = res;
Dianne Hackborna4972e92012-03-14 10:38:05 -07003091 if (res == ActivityManager.START_SUCCESS) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003092 mWaitingActivityLaunched.add(outResult);
3093 do {
3094 try {
Dianne Hackbornba0492d2010-10-12 19:01:46 -07003095 mService.wait();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003096 } catch (InterruptedException e) {
3097 }
3098 } while (!outResult.timeout && outResult.who == null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07003099 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003100 ActivityRecord r = this.topRunningActivityLocked(null);
3101 if (r.nowVisible) {
3102 outResult.timeout = false;
3103 outResult.who = new ComponentName(r.info.packageName, r.info.name);
3104 outResult.totalTime = 0;
3105 outResult.thisTime = 0;
3106 } else {
3107 outResult.thisTime = SystemClock.uptimeMillis();
3108 mWaitingActivityVisible.add(outResult);
3109 do {
3110 try {
Dianne Hackbornba0492d2010-10-12 19:01:46 -07003111 mService.wait();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003112 } catch (InterruptedException e) {
3113 }
3114 } while (!outResult.timeout && outResult.who == null);
3115 }
3116 }
3117 }
3118
3119 return res;
3120 }
3121 }
3122
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003123 final int startActivities(IApplicationThread caller, int callingUid,
Dianne Hackborna4972e92012-03-14 10:38:05 -07003124 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
3125 Bundle options, int userId) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003126 if (intents == null) {
3127 throw new NullPointerException("intents is null");
3128 }
3129 if (resolvedTypes == null) {
3130 throw new NullPointerException("resolvedTypes is null");
3131 }
3132 if (intents.length != resolvedTypes.length) {
3133 throw new IllegalArgumentException("intents are length different than resolvedTypes");
3134 }
3135
3136 ActivityRecord[] outActivity = new ActivityRecord[1];
3137
3138 int callingPid;
3139 if (callingUid >= 0) {
3140 callingPid = -1;
3141 } else if (caller == null) {
3142 callingPid = Binder.getCallingPid();
3143 callingUid = Binder.getCallingUid();
3144 } else {
3145 callingPid = callingUid = -1;
3146 }
3147 final long origId = Binder.clearCallingIdentity();
3148 try {
3149 synchronized (mService) {
3150
3151 for (int i=0; i<intents.length; i++) {
3152 Intent intent = intents[i];
3153 if (intent == null) {
3154 continue;
3155 }
3156
3157 // Refuse possible leaked file descriptors
3158 if (intent != null && intent.hasFileDescriptors()) {
3159 throw new IllegalArgumentException("File descriptors passed in Intent");
3160 }
3161
3162 boolean componentSpecified = intent.getComponent() != null;
3163
3164 // Don't modify the client's object!
3165 intent = new Intent(intent);
3166
3167 // Collect information about the target of the Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07003168 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i],
Amith Yamasani483f3b02012-03-13 16:08:00 -07003169 0, null, null, userId);
Amith Yamasani742a6712011-05-04 14:49:28 -07003170 // TODO: New, check if this is correct
3171 aInfo = mService.getActivityInfoForUser(aInfo, userId);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003172
3173 if (mMainStack && aInfo != null && (aInfo.applicationInfo.flags
3174 & ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
3175 throw new IllegalArgumentException(
3176 "FLAG_CANT_SAVE_STATE not supported here");
3177 }
3178
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07003179 Bundle theseOptions;
3180 if (options != null && i == intents.length-1) {
3181 theseOptions = options;
3182 } else {
3183 theseOptions = null;
3184 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003185 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborna4972e92012-03-14 10:38:05 -07003186 aInfo, resultTo, null, -1, callingPid, callingUid,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07003187 0, theseOptions, componentSpecified, outActivity);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003188 if (res < 0) {
3189 return res;
3190 }
3191
Dianne Hackbornbe707852011-11-11 14:32:10 -08003192 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003193 }
3194 }
3195 } finally {
3196 Binder.restoreCallingIdentity(origId);
3197 }
3198
Dianne Hackborna4972e92012-03-14 10:38:05 -07003199 return ActivityManager.START_SUCCESS;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003200 }
3201
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003202 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
3203 long thisTime, long totalTime) {
3204 for (int i=mWaitingActivityLaunched.size()-1; i>=0; i--) {
3205 WaitResult w = mWaitingActivityLaunched.get(i);
3206 w.timeout = timeout;
3207 if (r != null) {
3208 w.who = new ComponentName(r.info.packageName, r.info.name);
3209 }
3210 w.thisTime = thisTime;
3211 w.totalTime = totalTime;
3212 }
3213 mService.notifyAll();
3214 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003215
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003216 void reportActivityVisibleLocked(ActivityRecord r) {
3217 for (int i=mWaitingActivityVisible.size()-1; i>=0; i--) {
3218 WaitResult w = mWaitingActivityVisible.get(i);
3219 w.timeout = false;
3220 if (r != null) {
3221 w.who = new ComponentName(r.info.packageName, r.info.name);
3222 }
3223 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
3224 w.thisTime = w.totalTime;
3225 }
3226 mService.notifyAll();
Dianne Hackborn90c52de2011-09-23 12:57:44 -07003227
3228 if (mDismissKeyguardOnNextActivity) {
3229 mDismissKeyguardOnNextActivity = false;
3230 mService.mWindowManager.dismissKeyguard();
3231 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003232 }
3233
3234 void sendActivityResultLocked(int callingUid, ActivityRecord r,
3235 String resultWho, int requestCode, int resultCode, Intent data) {
3236
3237 if (callingUid > 0) {
3238 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07003239 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003240 }
3241
3242 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
3243 + " : who=" + resultWho + " req=" + requestCode
3244 + " res=" + resultCode + " data=" + data);
3245 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
3246 try {
3247 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
3248 list.add(new ResultInfo(resultWho, requestCode,
3249 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08003250 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003251 return;
3252 } catch (Exception e) {
3253 Slog.w(TAG, "Exception thrown sending result to " + r, e);
3254 }
3255 }
3256
3257 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
3258 }
3259
3260 private final void stopActivityLocked(ActivityRecord r) {
3261 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
3262 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3263 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3264 if (!r.finishing) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003265 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003266 "no-history");
3267 }
3268 } else if (r.app != null && r.app.thread != null) {
3269 if (mMainStack) {
3270 if (mService.mFocusedActivity == r) {
3271 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
3272 }
3273 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003274 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003275 try {
3276 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003277 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
3278 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003279 r.state = ActivityState.STOPPING;
3280 if (DEBUG_VISBILITY) Slog.v(
3281 TAG, "Stopping visible=" + r.visible + " for " + r);
3282 if (!r.visible) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003283 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003284 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08003285 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003286 if (mService.isSleeping()) {
3287 r.setSleeping(true);
3288 }
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003289 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG);
3290 msg.obj = r;
3291 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003292 } catch (Exception e) {
3293 // Maybe just ignore exceptions here... if the process
3294 // has crashed, our death notification will clean things
3295 // up.
3296 Slog.w(TAG, "Exception thrown during pause", e);
3297 // Just in case, assume it to be stopped.
3298 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003299 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003300 r.state = ActivityState.STOPPED;
3301 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07003302 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003303 }
3304 }
3305 }
3306 }
3307
3308 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(
3309 boolean remove) {
3310 int N = mStoppingActivities.size();
3311 if (N <= 0) return null;
3312
3313 ArrayList<ActivityRecord> stops = null;
3314
3315 final boolean nowVisible = mResumedActivity != null
3316 && mResumedActivity.nowVisible
3317 && !mResumedActivity.waitingVisible;
3318 for (int i=0; i<N; i++) {
3319 ActivityRecord s = mStoppingActivities.get(i);
3320 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
3321 + nowVisible + " waitingVisible=" + s.waitingVisible
3322 + " finishing=" + s.finishing);
3323 if (s.waitingVisible && nowVisible) {
3324 mWaitingVisibleActivities.remove(s);
3325 s.waitingVisible = false;
3326 if (s.finishing) {
3327 // If this activity is finishing, it is sitting on top of
3328 // everyone else but we now know it is no longer needed...
3329 // so get rid of it. Otherwise, we need to go through the
3330 // normal flow and hide it once we determine that it is
3331 // hidden by the activities in front of it.
3332 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003333 mService.mWindowManager.setAppVisibility(s.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003334 }
3335 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003336 if ((!s.waitingVisible || mService.isSleeping()) && remove) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003337 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
3338 if (stops == null) {
3339 stops = new ArrayList<ActivityRecord>();
3340 }
3341 stops.add(s);
3342 mStoppingActivities.remove(i);
3343 N--;
3344 i--;
3345 }
3346 }
3347
3348 return stops;
3349 }
3350
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07003351 final void scheduleIdleLocked() {
3352 Message msg = Message.obtain();
3353 msg.what = IDLE_NOW_MSG;
3354 mHandler.sendMessage(msg);
3355 }
3356
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003357 final ActivityRecord activityIdleInternal(IBinder token, boolean fromTimeout,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003358 Configuration config) {
3359 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
3360
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003361 ActivityRecord res = null;
3362
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003363 ArrayList<ActivityRecord> stops = null;
3364 ArrayList<ActivityRecord> finishes = null;
3365 ArrayList<ActivityRecord> thumbnails = null;
3366 int NS = 0;
3367 int NF = 0;
3368 int NT = 0;
3369 IApplicationThread sendThumbnail = null;
3370 boolean booting = false;
3371 boolean enableScreen = false;
3372
3373 synchronized (mService) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003374 ActivityRecord r = ActivityRecord.forToken(token);
3375 if (r != null) {
3376 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003377 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003378 }
3379
3380 // Get the activity record.
Dianne Hackbornbe707852011-11-11 14:32:10 -08003381 int index = indexOfActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003382 if (index >= 0) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003383 res = r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003384
3385 if (fromTimeout) {
3386 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
3387 }
3388
3389 // This is a hack to semi-deal with a race condition
3390 // in the client where it can be constructed with a
3391 // newer configuration from when we asked it to launch.
3392 // We'll update with whatever configuration it now says
3393 // it used to launch.
3394 if (config != null) {
3395 r.configuration = config;
3396 }
3397
3398 // No longer need to keep the device awake.
3399 if (mResumedActivity == r && mLaunchingActivity.isHeld()) {
3400 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
3401 mLaunchingActivity.release();
3402 }
3403
3404 // We are now idle. If someone is waiting for a thumbnail from
3405 // us, we can now deliver.
3406 r.idle = true;
3407 mService.scheduleAppGcsLocked();
3408 if (r.thumbnailNeeded && r.app != null && r.app.thread != null) {
3409 sendThumbnail = r.app.thread;
3410 r.thumbnailNeeded = false;
3411 }
3412
3413 // If this activity is fullscreen, set up to hide those under it.
3414
3415 if (DEBUG_VISBILITY) Slog.v(TAG, "Idle activity for " + r);
3416 ensureActivitiesVisibleLocked(null, 0);
3417
3418 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
3419 if (mMainStack) {
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07003420 if (!mService.mBooted) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003421 mService.mBooted = true;
3422 enableScreen = true;
3423 }
3424 }
3425
3426 } else if (fromTimeout) {
3427 reportActivityLaunchedLocked(fromTimeout, null, -1, -1);
3428 }
3429
3430 // Atomically retrieve all of the other things to do.
3431 stops = processStoppingActivitiesLocked(true);
3432 NS = stops != null ? stops.size() : 0;
3433 if ((NF=mFinishingActivities.size()) > 0) {
3434 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
3435 mFinishingActivities.clear();
3436 }
3437 if ((NT=mService.mCancelledThumbnails.size()) > 0) {
3438 thumbnails = new ArrayList<ActivityRecord>(mService.mCancelledThumbnails);
3439 mService.mCancelledThumbnails.clear();
3440 }
3441
3442 if (mMainStack) {
3443 booting = mService.mBooting;
3444 mService.mBooting = false;
3445 }
3446 }
3447
3448 int i;
3449
3450 // Send thumbnail if requested.
3451 if (sendThumbnail != null) {
3452 try {
3453 sendThumbnail.requestThumbnail(token);
3454 } catch (Exception e) {
3455 Slog.w(TAG, "Exception thrown when requesting thumbnail", e);
3456 mService.sendPendingThumbnail(null, token, null, null, true);
3457 }
3458 }
3459
3460 // Stop any activities that are scheduled to do so but have been
3461 // waiting for the next one to start.
3462 for (i=0; i<NS; i++) {
3463 ActivityRecord r = (ActivityRecord)stops.get(i);
3464 synchronized (mService) {
3465 if (r.finishing) {
3466 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY);
3467 } else {
3468 stopActivityLocked(r);
3469 }
3470 }
3471 }
3472
3473 // Finish any activities that are scheduled to do so but have been
3474 // waiting for the next one to start.
3475 for (i=0; i<NF; i++) {
3476 ActivityRecord r = (ActivityRecord)finishes.get(i);
3477 synchronized (mService) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07003478 destroyActivityLocked(r, true, false, "finish-idle");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003479 }
3480 }
3481
3482 // Report back to any thumbnail receivers.
3483 for (i=0; i<NT; i++) {
3484 ActivityRecord r = (ActivityRecord)thumbnails.get(i);
3485 mService.sendPendingThumbnail(r, null, null, null, true);
3486 }
3487
3488 if (booting) {
3489 mService.finishBooting();
3490 }
3491
3492 mService.trimApplications();
3493 //dump();
3494 //mWindowManager.dump();
3495
3496 if (enableScreen) {
3497 mService.enableScreenAfterBoot();
3498 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003499
3500 return res;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003501 }
3502
3503 /**
3504 * @return Returns true if the activity is being finished, false if for
3505 * some reason it is being left as-is.
3506 */
3507 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
3508 Intent resultData, String reason) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003509 int index = indexOfTokenLocked(token);
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07003510 if (DEBUG_RESULTS) Slog.v(
3511 TAG, "Finishing activity @" + index + ": token=" + token
3512 + ", result=" + resultCode + ", data=" + resultData);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003513 if (index < 0) {
3514 return false;
3515 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003516 ActivityRecord r = mHistory.get(index);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003517
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003518 finishActivityLocked(r, index, resultCode, resultData, reason);
3519 return true;
3520 }
3521
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003522 final void finishSubActivityLocked(IBinder token, String resultWho, int requestCode) {
3523 ActivityRecord self = isInStackLocked(token);
3524 if (self == null) {
3525 return;
3526 }
3527
3528 int i;
3529 for (i=mHistory.size()-1; i>=0; i--) {
3530 ActivityRecord r = (ActivityRecord)mHistory.get(i);
3531 if (r.resultTo == self && r.requestCode == requestCode) {
3532 if ((r.resultWho == null && resultWho == null) ||
3533 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3534 finishActivityLocked(r, i,
3535 Activity.RESULT_CANCELED, null, "request-sub");
3536 }
3537 }
3538 }
3539 }
3540
3541 final boolean finishActivityAffinityLocked(IBinder token) {
3542 int index = indexOfTokenLocked(token);
3543 if (DEBUG_RESULTS) Slog.v(
3544 TAG, "Finishing activity affinity @" + index + ": token=" + token);
3545 if (index < 0) {
3546 return false;
3547 }
3548 ActivityRecord r = mHistory.get(index);
3549
Amith Yamasanibfc1be12012-05-15 11:12:17 -07003550 while (index >= 0) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003551 ActivityRecord cur = mHistory.get(index);
3552 if (cur.task != r.task) {
3553 break;
3554 }
3555 if (cur.taskAffinity == null && r.taskAffinity != null) {
3556 break;
3557 }
3558 if (cur.taskAffinity != null && !cur.taskAffinity.equals(r.taskAffinity)) {
3559 break;
3560 }
3561 finishActivityLocked(cur, index, Activity.RESULT_CANCELED, null, "request-affinity");
3562 index--;
3563 }
3564 return true;
3565 }
3566
Dianne Hackborn5c607432012-02-28 14:44:19 -08003567 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3568 // send the result
3569 ActivityRecord resultTo = r.resultTo;
3570 if (resultTo != null) {
3571 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
3572 + " who=" + r.resultWho + " req=" + r.requestCode
3573 + " res=" + resultCode + " data=" + resultData);
3574 if (r.info.applicationInfo.uid > 0) {
3575 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3576 resultTo.packageName, resultData,
3577 resultTo.getUriPermissionsLocked());
3578 }
3579 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3580 resultData);
3581 r.resultTo = null;
3582 }
3583 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
3584
3585 // Make sure this HistoryRecord is not holding on to other resources,
3586 // because clients have remote IPC references to this object so we
3587 // can't assume that will go away and want to avoid circular IPC refs.
3588 r.results = null;
3589 r.pendingResults = null;
3590 r.newIntents = null;
3591 r.icicle = null;
3592 }
3593
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003594 /**
3595 * @return Returns true if this activity has been removed from the history
3596 * list, or false if it is still in the list and will be removed later.
3597 */
3598 final boolean finishActivityLocked(ActivityRecord r, int index,
3599 int resultCode, Intent resultData, String reason) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003600 return finishActivityLocked(r, index, resultCode, resultData, reason, false);
3601 }
3602
3603 /**
3604 * @return Returns true if this activity has been removed from the history
3605 * list, or false if it is still in the list and will be removed later.
3606 */
3607 final boolean finishActivityLocked(ActivityRecord r, int index,
3608 int resultCode, Intent resultData, String reason, boolean immediate) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003609 if (r.finishing) {
3610 Slog.w(TAG, "Duplicate finish request for " + r);
3611 return false;
3612 }
3613
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08003614 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003615 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3616 System.identityHashCode(r),
3617 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003618 if (index < (mHistory.size()-1)) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003619 ActivityRecord next = mHistory.get(index+1);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003620 if (next.task == r.task) {
3621 if (r.frontOfTask) {
3622 // The next activity is now the front of the task.
3623 next.frontOfTask = true;
3624 }
3625 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
3626 // If the caller asked that this activity (and all above it)
3627 // be cleared when the task is reset, don't lose that information,
3628 // but propagate it up to the next activity.
3629 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
3630 }
3631 }
3632 }
3633
3634 r.pauseKeyDispatchingLocked();
3635 if (mMainStack) {
3636 if (mService.mFocusedActivity == r) {
3637 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
3638 }
3639 }
3640
Dianne Hackborn5c607432012-02-28 14:44:19 -08003641 finishActivityResultsLocked(r, resultCode, resultData);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003642
3643 if (mService.mPendingThumbnails.size() > 0) {
3644 // There are clients waiting to receive thumbnails so, in case
3645 // this is an activity that someone is waiting for, add it
3646 // to the pending list so we can correctly update the clients.
3647 mService.mCancelledThumbnails.add(r);
3648 }
3649
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003650 if (immediate) {
3651 return finishCurrentActivityLocked(r, index,
3652 FINISH_IMMEDIATELY) == null;
3653 } else if (mResumedActivity == r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003654 boolean endTask = index <= 0
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003655 || (mHistory.get(index-1)).task != r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003656 if (DEBUG_TRANSITION) Slog.v(TAG,
3657 "Prepare close transition: finishing " + r);
3658 mService.mWindowManager.prepareAppTransition(endTask
3659 ? WindowManagerPolicy.TRANSIT_TASK_CLOSE
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08003660 : WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003661
3662 // Tell window manager to prepare for this one to be removed.
Dianne Hackbornbe707852011-11-11 14:32:10 -08003663 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003664
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003665 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003666 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
3667 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
3668 startPausingLocked(false, false);
3669 }
3670
3671 } else if (r.state != ActivityState.PAUSING) {
3672 // If the activity is PAUSING, we will complete the finish once
3673 // it is done pausing; else we can just directly finish it here.
3674 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
3675 return finishCurrentActivityLocked(r, index,
3676 FINISH_AFTER_PAUSE) == null;
3677 } else {
3678 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
3679 }
3680
3681 return false;
3682 }
3683
3684 private static final int FINISH_IMMEDIATELY = 0;
3685 private static final int FINISH_AFTER_PAUSE = 1;
3686 private static final int FINISH_AFTER_VISIBLE = 2;
3687
3688 private final ActivityRecord finishCurrentActivityLocked(ActivityRecord r,
3689 int mode) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003690 final int index = indexOfActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003691 if (index < 0) {
3692 return null;
3693 }
3694
3695 return finishCurrentActivityLocked(r, index, mode);
3696 }
3697
3698 private final ActivityRecord finishCurrentActivityLocked(ActivityRecord r,
3699 int index, int mode) {
3700 // First things first: if this activity is currently visible,
3701 // and the resumed activity is not yet visible, then hold off on
3702 // finishing until the resumed one becomes visible.
3703 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
3704 if (!mStoppingActivities.contains(r)) {
3705 mStoppingActivities.add(r);
3706 if (mStoppingActivities.size() > 3) {
3707 // If we already have a few activities waiting to stop,
3708 // then give up on things going idle and start clearing
3709 // them out.
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07003710 scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003711 } else {
3712 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003713 }
3714 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003715 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
3716 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003717 r.state = ActivityState.STOPPING;
3718 mService.updateOomAdjLocked();
3719 return r;
3720 }
3721
3722 // make sure the record is cleaned out of other places.
3723 mStoppingActivities.remove(r);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003724 mGoingToSleepActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003725 mWaitingVisibleActivities.remove(r);
3726 if (mResumedActivity == r) {
3727 mResumedActivity = null;
3728 }
3729 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003730 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003731 r.state = ActivityState.FINISHING;
3732
3733 if (mode == FINISH_IMMEDIATELY
3734 || prevState == ActivityState.STOPPED
3735 || prevState == ActivityState.INITIALIZING) {
3736 // If this activity is already stopped, we can just finish
3737 // it right now.
Dianne Hackborn28695e02011-11-02 21:59:51 -07003738 return destroyActivityLocked(r, true, true, "finish-imm") ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003739 } else {
3740 // Need to go through the full pause cycle to get this
3741 // activity into the stopped state and then finish it.
3742 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
3743 mFinishingActivities.add(r);
3744 resumeTopActivityLocked(null);
3745 }
3746 return r;
3747 }
3748
3749 /**
3750 * Perform the common clean-up of an activity record. This is called both
3751 * as part of destroyActivityLocked() (when destroying the client-side
3752 * representation) and cleaning things up as a result of its hosting
3753 * processing going away, in which case there is no remaining client-side
3754 * state to destroy so only the cleanup here is needed.
3755 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003756 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3757 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003758 if (mResumedActivity == r) {
3759 mResumedActivity = null;
3760 }
3761 if (mService.mFocusedActivity == r) {
3762 mService.mFocusedActivity = null;
3763 }
3764
3765 r.configDestroy = false;
3766 r.frozenBeforeDestroy = false;
3767
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003768 if (setState) {
3769 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
3770 r.state = ActivityState.DESTROYED;
3771 }
3772
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003773 // Make sure this record is no longer in the pending finishes list.
3774 // This could happen, for example, if we are trimming activities
3775 // down to the max limit while they are still waiting to finish.
3776 mFinishingActivities.remove(r);
3777 mWaitingVisibleActivities.remove(r);
3778
3779 // Remove any pending results.
3780 if (r.finishing && r.pendingResults != null) {
3781 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3782 PendingIntentRecord rec = apr.get();
3783 if (rec != null) {
3784 mService.cancelIntentSenderLocked(rec, false);
3785 }
3786 }
3787 r.pendingResults = null;
3788 }
3789
3790 if (cleanServices) {
3791 cleanUpActivityServicesLocked(r);
3792 }
3793
3794 if (mService.mPendingThumbnails.size() > 0) {
3795 // There are clients waiting to receive thumbnails so, in case
3796 // this is an activity that someone is waiting for, add it
3797 // to the pending list so we can correctly update the clients.
3798 mService.mCancelledThumbnails.add(r);
3799 }
3800
3801 // Get rid of any pending idle timeouts.
3802 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003803 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003804 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003805 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003806 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003807 }
3808
Dianne Hackborn5c607432012-02-28 14:44:19 -08003809 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003810 if (r.state != ActivityState.DESTROYED) {
Dianne Hackborn5c607432012-02-28 14:44:19 -08003811 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08003812 r.makeFinishing();
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07003813 if (DEBUG_ADD_REMOVE) {
3814 RuntimeException here = new RuntimeException("here");
3815 here.fillInStackTrace();
3816 Slog.i(TAG, "Removing activity " + r + " from stack");
3817 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003818 mHistory.remove(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07003819 r.takeFromHistory();
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003820 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
3821 + " (removed from history)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003822 r.state = ActivityState.DESTROYED;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003823 mService.mWindowManager.removeAppToken(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003824 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003825 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003826 }
3827 cleanUpActivityServicesLocked(r);
3828 r.removeUriPermissionsLocked();
3829 }
3830 }
3831
3832 /**
3833 * Perform clean-up of service connections in an activity record.
3834 */
3835 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3836 // Throw away any services that have been bound by this activity.
3837 if (r.connections != null) {
3838 Iterator<ConnectionRecord> it = r.connections.iterator();
3839 while (it.hasNext()) {
3840 ConnectionRecord c = it.next();
3841 mService.removeConnectionLocked(c, null, r);
3842 }
3843 r.connections = null;
3844 }
3845 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003846
3847 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
3848 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
3849 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
3850 mHandler.sendMessage(msg);
3851 }
3852
Dianne Hackborn28695e02011-11-02 21:59:51 -07003853 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003854 boolean lastIsOpaque = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003855 for (int i=mHistory.size()-1; i>=0; i--) {
3856 ActivityRecord r = mHistory.get(i);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003857 if (r.finishing) {
3858 continue;
3859 }
3860 if (r.fullscreen) {
3861 lastIsOpaque = true;
3862 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003863 if (owner != null && r.app != owner) {
3864 continue;
3865 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003866 if (!lastIsOpaque) {
3867 continue;
3868 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003869 // We can destroy this one if we have its icicle saved and
3870 // it is not in the process of pausing/stopping/finishing.
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003871 if (r.app != null && r != mResumedActivity && r != mPausingActivity
3872 && r.haveState && !r.visible && r.stopped
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003873 && r.state != ActivityState.DESTROYING
3874 && r.state != ActivityState.DESTROYED) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003875 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3876 + " resumed=" + mResumedActivity
3877 + " pausing=" + mPausingActivity);
3878 destroyActivityLocked(r, true, oomAdj, reason);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003879 }
3880 }
3881 }
3882
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003883 /**
3884 * Destroy the current CLIENT SIDE instance of an activity. This may be
3885 * called both when actually finishing an activity, or when performing
3886 * a configuration switch where we destroy the current client-side object
3887 * but then create a new client-side object for this same HistoryRecord.
3888 */
3889 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07003890 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003891 if (DEBUG_SWITCH) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003892 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003893 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3894 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
3895 System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003896 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003897
3898 boolean removedFromHistory = false;
3899
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003900 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003901
3902 final boolean hadApp = r.app != null;
3903
3904 if (hadApp) {
3905 if (removeFromApp) {
3906 int idx = r.app.activities.indexOf(r);
3907 if (idx >= 0) {
3908 r.app.activities.remove(idx);
3909 }
3910 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3911 mService.mHeavyWeightProcess = null;
3912 mService.mHandler.sendEmptyMessage(
3913 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3914 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003915 if (r.app.activities.size() == 0) {
3916 // No longer have activities, so update location in
3917 // LRU list.
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003918 mService.updateLruProcessLocked(r.app, oomAdj, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003919 }
3920 }
3921
3922 boolean skipDestroy = false;
3923
3924 try {
3925 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003926 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003927 r.configChangeFlags);
3928 } catch (Exception e) {
3929 // We can just ignore exceptions here... if the process
3930 // has crashed, our death notification will clean things
3931 // up.
3932 //Slog.w(TAG, "Exception thrown during finish", e);
3933 if (r.finishing) {
3934 removeActivityFromHistoryLocked(r);
3935 removedFromHistory = true;
3936 skipDestroy = true;
3937 }
3938 }
3939
3940 r.app = null;
3941 r.nowVisible = false;
3942
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003943 // If the activity is finishing, we need to wait on removing it
3944 // from the list to give it a chance to do its cleanup. During
3945 // that time it may make calls back with its token so we need to
3946 // be able to find it on the list and so we don't want to remove
3947 // it from the list yet. Otherwise, we can just immediately put
3948 // it in the destroyed state since we are not removing it from the
3949 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003950 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003951 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3952 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003953 r.state = ActivityState.DESTROYING;
3954 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG);
3955 msg.obj = r;
3956 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3957 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003958 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
3959 + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003960 r.state = ActivityState.DESTROYED;
3961 }
3962 } else {
3963 // remove this record from the history.
3964 if (r.finishing) {
3965 removeActivityFromHistoryLocked(r);
3966 removedFromHistory = true;
3967 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003968 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
3969 + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003970 r.state = ActivityState.DESTROYED;
3971 }
3972 }
3973
3974 r.configChangeFlags = 0;
3975
3976 if (!mLRUActivities.remove(r) && hadApp) {
3977 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3978 }
3979
3980 return removedFromHistory;
3981 }
3982
3983 final void activityDestroyed(IBinder token) {
3984 synchronized (mService) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003985 ActivityRecord r = ActivityRecord.forToken(token);
3986 if (r != null) {
3987 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
3988 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003989
Dianne Hackbornbe707852011-11-11 14:32:10 -08003990 int index = indexOfActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003991 if (index >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003992 if (r.state == ActivityState.DESTROYING) {
3993 final long origId = Binder.clearCallingIdentity();
3994 removeActivityFromHistoryLocked(r);
3995 Binder.restoreCallingIdentity(origId);
3996 }
3997 }
3998 }
3999 }
4000
4001 private static void removeHistoryRecordsForAppLocked(ArrayList list, ProcessRecord app) {
4002 int i = list.size();
4003 if (localLOGV) Slog.v(
4004 TAG, "Removing app " + app + " from list " + list
4005 + " with " + i + " entries");
4006 while (i > 0) {
4007 i--;
4008 ActivityRecord r = (ActivityRecord)list.get(i);
4009 if (localLOGV) Slog.v(
4010 TAG, "Record #" + i + " " + r + ": app=" + r.app);
4011 if (r.app == app) {
4012 if (localLOGV) Slog.v(TAG, "Removing this entry!");
4013 list.remove(i);
4014 }
4015 }
4016 }
4017
4018 void removeHistoryRecordsForAppLocked(ProcessRecord app) {
4019 removeHistoryRecordsForAppLocked(mLRUActivities, app);
4020 removeHistoryRecordsForAppLocked(mStoppingActivities, app);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004021 removeHistoryRecordsForAppLocked(mGoingToSleepActivities, app);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004022 removeHistoryRecordsForAppLocked(mWaitingVisibleActivities, app);
4023 removeHistoryRecordsForAppLocked(mFinishingActivities, app);
4024 }
4025
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004026 /**
4027 * Move the current home activity's task (if one exists) to the front
4028 * of the stack.
4029 */
4030 final void moveHomeToFrontLocked() {
4031 TaskRecord homeTask = null;
4032 for (int i=mHistory.size()-1; i>=0; i--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004033 ActivityRecord hr = mHistory.get(i);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004034 if (hr.isHomeActivity) {
4035 homeTask = hr.task;
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08004036 break;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004037 }
4038 }
4039 if (homeTask != null) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004040 moveTaskToFrontLocked(homeTask, null, null);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004041 }
4042 }
4043
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004044 final void updateTransitLocked(int transit, Bundle options) {
4045 if (options != null) {
4046 ActivityRecord r = topRunningActivityLocked(null);
4047 if (r != null && r.state != ActivityState.RESUMED) {
4048 r.updateOptionsLocked(options);
4049 } else {
4050 ActivityOptions.abort(options);
4051 }
4052 }
4053 mService.mWindowManager.prepareAppTransition(transit, false);
4054 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004055
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004056 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004057 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
4058
4059 final int task = tr.taskId;
4060 int top = mHistory.size()-1;
4061
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004062 if (top < 0 || (mHistory.get(top)).task.taskId == task) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004063 // nothing to do!
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004064 if (reason != null &&
4065 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
4066 ActivityOptions.abort(options);
4067 } else {
4068 updateTransitLocked(WindowManagerPolicy.TRANSIT_TASK_TO_FRONT, options);
4069 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004070 return;
4071 }
4072
Dianne Hackbornbe707852011-11-11 14:32:10 -08004073 ArrayList<IBinder> moved = new ArrayList<IBinder>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004074
4075 // Applying the affinities may have removed entries from the history,
4076 // so get the size again.
4077 top = mHistory.size()-1;
4078 int pos = top;
4079
4080 // Shift all activities with this task up to the top
4081 // of the stack, keeping them in the same internal order.
4082 while (pos >= 0) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004083 ActivityRecord r = mHistory.get(pos);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004084 if (localLOGV) Slog.v(
4085 TAG, "At " + pos + " ckp " + r.task + ": " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004086 if (r.task.taskId == task) {
4087 if (localLOGV) Slog.v(TAG, "Removing and adding at " + top);
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07004088 if (DEBUG_ADD_REMOVE) {
4089 RuntimeException here = new RuntimeException("here");
4090 here.fillInStackTrace();
4091 Slog.i(TAG, "Removing and adding activity " + r + " to stack at " + top, here);
4092 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004093 mHistory.remove(pos);
4094 mHistory.add(top, r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08004095 moved.add(0, r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004096 top--;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004097 }
4098 pos--;
4099 }
4100
4101 if (DEBUG_TRANSITION) Slog.v(TAG,
4102 "Prepare to front transition: task=" + tr);
4103 if (reason != null &&
4104 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08004105 mService.mWindowManager.prepareAppTransition(
4106 WindowManagerPolicy.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004107 ActivityRecord r = topRunningActivityLocked(null);
4108 if (r != null) {
4109 mNoAnimActivities.add(r);
4110 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004111 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004112 } else {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004113 updateTransitLocked(WindowManagerPolicy.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004114 }
4115
4116 mService.mWindowManager.moveAppTokensToTop(moved);
4117 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004118 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004119 }
4120
4121 finishTaskMoveLocked(task);
4122 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, task);
4123 }
4124
4125 private final void finishTaskMoveLocked(int task) {
4126 resumeTopActivityLocked(null);
4127 }
4128
4129 /**
4130 * Worker method for rearranging history stack. Implements the function of moving all
4131 * activities for a specific task (gathering them if disjoint) into a single group at the
4132 * bottom of the stack.
4133 *
4134 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4135 * to premeptively cancel the move.
4136 *
4137 * @param task The taskId to collect and move to the bottom.
4138 * @return Returns true if the move completed, false if not.
4139 */
4140 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
4141 Slog.i(TAG, "moveTaskToBack: " + task);
4142
4143 // If we have a watcher, preflight the move before committing to it. First check
4144 // for *other* available tasks, but if none are available, then try again allowing the
4145 // current task to be selected.
4146 if (mMainStack && mService.mController != null) {
4147 ActivityRecord next = topRunningActivityLocked(null, task);
4148 if (next == null) {
4149 next = topRunningActivityLocked(null, 0);
4150 }
4151 if (next != null) {
4152 // ask watcher if this is allowed
4153 boolean moveOK = true;
4154 try {
4155 moveOK = mService.mController.activityResuming(next.packageName);
4156 } catch (RemoteException e) {
4157 mService.mController = null;
4158 }
4159 if (!moveOK) {
4160 return false;
4161 }
4162 }
4163 }
4164
Dianne Hackbornbe707852011-11-11 14:32:10 -08004165 ArrayList<IBinder> moved = new ArrayList<IBinder>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004166
4167 if (DEBUG_TRANSITION) Slog.v(TAG,
4168 "Prepare to back transition: task=" + task);
4169
4170 final int N = mHistory.size();
4171 int bottom = 0;
4172 int pos = 0;
4173
4174 // Shift all activities with this task down to the bottom
4175 // of the stack, keeping them in the same internal order.
4176 while (pos < N) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004177 ActivityRecord r = mHistory.get(pos);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004178 if (localLOGV) Slog.v(
4179 TAG, "At " + pos + " ckp " + r.task + ": " + r);
4180 if (r.task.taskId == task) {
4181 if (localLOGV) Slog.v(TAG, "Removing and adding at " + (N-1));
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07004182 if (DEBUG_ADD_REMOVE) {
4183 RuntimeException here = new RuntimeException("here");
4184 here.fillInStackTrace();
4185 Slog.i(TAG, "Removing and adding activity " + r + " to stack at "
4186 + bottom, here);
4187 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004188 mHistory.remove(pos);
4189 mHistory.add(bottom, r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08004190 moved.add(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004191 bottom++;
4192 }
4193 pos++;
4194 }
4195
4196 if (reason != null &&
4197 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08004198 mService.mWindowManager.prepareAppTransition(
4199 WindowManagerPolicy.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004200 ActivityRecord r = topRunningActivityLocked(null);
4201 if (r != null) {
4202 mNoAnimActivities.add(r);
4203 }
4204 } else {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08004205 mService.mWindowManager.prepareAppTransition(
4206 WindowManagerPolicy.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004207 }
4208 mService.mWindowManager.moveAppTokensToBottom(moved);
4209 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004210 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004211 }
4212
4213 finishTaskMoveLocked(task);
4214 return true;
4215 }
4216
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004217 public ActivityManager.TaskThumbnails getTaskThumbnailsLocked(TaskRecord tr) {
4218 TaskAccessInfo info = getTaskAccessInfoLocked(tr.taskId, true);
4219 ActivityRecord resumed = mResumedActivity;
4220 if (resumed != null && resumed.thumbHolder == tr) {
4221 info.mainThumbnail = resumed.stack.screenshotActivities(resumed);
4222 } else {
4223 info.mainThumbnail = tr.lastThumbnail;
4224 }
4225 return info;
4226 }
4227
Dianne Hackborn9da2d402012-03-15 13:43:08 -07004228 public ActivityRecord removeTaskActivitiesLocked(int taskId, int subTaskIndex,
4229 boolean taskRequired) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004230 TaskAccessInfo info = getTaskAccessInfoLocked(taskId, false);
4231 if (info.root == null) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07004232 if (taskRequired) {
4233 Slog.w(TAG, "removeTaskLocked: unknown taskId " + taskId);
4234 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004235 return null;
4236 }
4237
4238 if (subTaskIndex < 0) {
4239 // Just remove the entire task.
4240 performClearTaskAtIndexLocked(taskId, info.rootIndex);
4241 return info.root;
4242 }
4243
4244 if (subTaskIndex >= info.subtasks.size()) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07004245 if (taskRequired) {
4246 Slog.w(TAG, "removeTaskLocked: unknown subTaskIndex " + subTaskIndex);
4247 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004248 return null;
4249 }
4250
4251 // Remove all of this task's activies starting at the sub task.
4252 TaskAccessInfo.SubTask subtask = info.subtasks.get(subTaskIndex);
4253 performClearTaskAtIndexLocked(taskId, subtask.index);
4254 return subtask.activity;
4255 }
4256
4257 public TaskAccessInfo getTaskAccessInfoLocked(int taskId, boolean inclThumbs) {
4258 ActivityRecord resumed = mResumedActivity;
4259 final TaskAccessInfo thumbs = new TaskAccessInfo();
4260 // How many different sub-thumbnails?
4261 final int NA = mHistory.size();
4262 int j = 0;
4263 ThumbnailHolder holder = null;
4264 while (j < NA) {
4265 ActivityRecord ar = mHistory.get(j);
4266 if (!ar.finishing && ar.task.taskId == taskId) {
4267 holder = ar.thumbHolder;
4268 break;
4269 }
4270 j++;
4271 }
4272
4273 if (j >= NA) {
4274 return thumbs;
4275 }
4276
4277 thumbs.root = mHistory.get(j);
4278 thumbs.rootIndex = j;
4279
4280 ArrayList<TaskAccessInfo.SubTask> subtasks = new ArrayList<TaskAccessInfo.SubTask>();
4281 thumbs.subtasks = subtasks;
4282 ActivityRecord lastActivity = null;
4283 while (j < NA) {
4284 ActivityRecord ar = mHistory.get(j);
4285 j++;
4286 if (ar.finishing) {
4287 continue;
4288 }
4289 if (ar.task.taskId != taskId) {
4290 break;
4291 }
4292 lastActivity = ar;
4293 if (ar.thumbHolder != holder && holder != null) {
4294 thumbs.numSubThumbbails++;
4295 holder = ar.thumbHolder;
4296 TaskAccessInfo.SubTask sub = new TaskAccessInfo.SubTask();
4297 sub.thumbnail = holder.lastThumbnail;
4298 sub.activity = ar;
4299 sub.index = j-1;
4300 subtasks.add(sub);
4301 }
4302 }
4303 if (lastActivity != null && subtasks.size() > 0) {
4304 if (resumed == lastActivity) {
4305 TaskAccessInfo.SubTask sub = subtasks.get(subtasks.size()-1);
4306 sub.thumbnail = lastActivity.stack.screenshotActivities(lastActivity);
4307 }
4308 }
4309 if (thumbs.numSubThumbbails > 0) {
4310 thumbs.retriever = new IThumbnailRetriever.Stub() {
4311 public Bitmap getThumbnail(int index) {
4312 if (index < 0 || index >= thumbs.subtasks.size()) {
4313 return null;
4314 }
4315 return thumbs.subtasks.get(index).thumbnail;
4316 }
4317 };
4318 }
4319 return thumbs;
4320 }
4321
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004322 private final void logStartActivity(int tag, ActivityRecord r,
4323 TaskRecord task) {
4324 EventLog.writeEvent(tag,
4325 System.identityHashCode(r), task.taskId,
4326 r.shortComponentName, r.intent.getAction(),
4327 r.intent.getType(), r.intent.getDataString(),
4328 r.intent.getFlags());
4329 }
4330
4331 /**
4332 * Make sure the given activity matches the current configuration. Returns
4333 * false if the activity had to be destroyed. Returns true if the
4334 * configuration is the same, or the activity will remain running as-is
4335 * for whatever reason. Ensures the HistoryRecord is updated with the
4336 * correct configuration and all other bookkeeping is handled.
4337 */
4338 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
4339 int globalChanges) {
4340 if (mConfigWillChange) {
4341 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4342 "Skipping config check (will change): " + r);
4343 return true;
4344 }
4345
4346 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4347 "Ensuring correct configuration: " + r);
4348
4349 // Short circuit: if the two configurations are the exact same
4350 // object (the common case), then there is nothing to do.
4351 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004352 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004353 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4354 "Configuration unchanged in " + r);
4355 return true;
4356 }
4357
4358 // We don't worry about activities that are finishing.
4359 if (r.finishing) {
4360 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4361 "Configuration doesn't matter in finishing " + r);
4362 r.stopFreezingScreenLocked(false);
4363 return true;
4364 }
4365
4366 // Okay we now are going to make this activity have the new config.
4367 // But then we need to figure out how it needs to deal with that.
4368 Configuration oldConfig = r.configuration;
4369 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004370
4371 // Determine what has changed. May be nothing, if this is a config
4372 // that has come back from the app after going idle. In that case
4373 // we just want to leave the official config object now in the
4374 // activity and do nothing else.
4375 final int changes = oldConfig.diff(newConfig);
4376 if (changes == 0 && !r.forceNewConfig) {
4377 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4378 "Configuration no differences in " + r);
4379 return true;
4380 }
4381
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004382 // If the activity isn't currently running, just leave the new
4383 // configuration and it will pick that up next time it starts.
4384 if (r.app == null || r.app.thread == null) {
4385 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4386 "Configuration doesn't matter not running " + r);
4387 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004388 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004389 return true;
4390 }
4391
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004392 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004393 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
4394 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
4395 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07004396 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004397 + ", newConfig=" + newConfig);
4398 }
Dianne Hackborne6676352011-06-01 16:51:20 -07004399 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004400 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4401 r.configChangeFlags |= changes;
4402 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004403 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004404 if (r.app == null || r.app.thread == null) {
4405 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004406 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07004407 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004408 } else if (r.state == ActivityState.PAUSING) {
4409 // A little annoying: we are waiting for this activity to
4410 // finish pausing. Let's not do anything now, but just
4411 // flag that it needs to be restarted when done pausing.
4412 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004413 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004414 r.configDestroy = true;
4415 return true;
4416 } else if (r.state == ActivityState.RESUMED) {
4417 // Try to optimize this case: the configuration is changing
4418 // and we need to restart the top, resumed activity.
4419 // Instead of doing the normal handshaking, just say
4420 // "restart!".
4421 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004422 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004423 relaunchActivityLocked(r, r.configChangeFlags, true);
4424 r.configChangeFlags = 0;
4425 } else {
4426 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004427 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004428 relaunchActivityLocked(r, r.configChangeFlags, false);
4429 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004430 }
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004431
4432 // All done... tell the caller we weren't able to keep this
4433 // activity around.
4434 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004435 }
4436
4437 // Default case: the activity can handle this new configuration, so
4438 // hand it over. Note that we don't need to give it the new
4439 // configuration, since we always send configuration changes to all
4440 // process when they happen so it can just use whatever configuration
4441 // it last got.
4442 if (r.app != null && r.app.thread != null) {
4443 try {
4444 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08004445 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004446 } catch (RemoteException e) {
4447 // If process died, whatever.
4448 }
4449 }
4450 r.stopFreezingScreenLocked(false);
4451
4452 return true;
4453 }
4454
4455 private final boolean relaunchActivityLocked(ActivityRecord r,
4456 int changes, boolean andResume) {
4457 List<ResultInfo> results = null;
4458 List<Intent> newIntents = null;
4459 if (andResume) {
4460 results = r.results;
4461 newIntents = r.newIntents;
4462 }
4463 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
4464 + " with results=" + results + " newIntents=" + newIntents
4465 + " andResume=" + andResume);
4466 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
4467 : EventLogTags.AM_RELAUNCH_ACTIVITY, System.identityHashCode(r),
4468 r.task.taskId, r.shortComponentName);
4469
4470 r.startFreezingScreenLocked(r.app, 0);
4471
4472 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004473 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
4474 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
4475 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004476 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08004477 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08004478 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004479 // Note: don't need to call pauseIfSleepingLocked() here, because
4480 // the caller will only pass in 'andResume' if this activity is
4481 // currently resumed, which implies we aren't sleeping.
4482 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004483 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004484 }
4485
4486 if (andResume) {
4487 r.results = null;
4488 r.newIntents = null;
4489 if (mMainStack) {
4490 mService.reportResumedActivityLocked(r);
4491 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004492 r.state = ActivityState.RESUMED;
4493 } else {
4494 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4495 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004496 }
4497
4498 return true;
4499 }
Dianne Hackborn90c52de2011-09-23 12:57:44 -07004500
4501 public void dismissKeyguardOnNextActivityLocked() {
4502 mDismissKeyguardOnNextActivity = true;
4503 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004504}