blob: 526b24f052b3ceafb82561ca86aac0c65c6de04e [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;
Craig Mautner4b71aa12012-12-27 17:20:01 -080025import com.android.server.wm.AppTransition;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070026
27import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070028import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070029import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070030import android.app.AppGlobals;
31import android.app.IActivityManager;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070032import android.app.IThumbnailRetriever;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070033import android.app.IApplicationThread;
34import android.app.PendingIntent;
35import android.app.ResultInfo;
36import android.app.IActivityManager.WaitResult;
37import android.content.ComponentName;
38import android.content.Context;
39import android.content.IIntentSender;
40import android.content.Intent;
41import android.content.IntentSender;
42import android.content.pm.ActivityInfo;
43import android.content.pm.ApplicationInfo;
44import android.content.pm.PackageManager;
45import android.content.pm.ResolveInfo;
46import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080047import android.content.res.Resources;
48import android.graphics.Bitmap;
Craig Mautnerb12428a2012-12-20 16:07:06 -080049import android.graphics.Bitmap.Config;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070050import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070051import android.os.Bundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070052import android.os.Handler;
53import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090054import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055import android.os.Message;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070056import android.os.ParcelFileDescriptor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.os.PowerManager;
58import android.os.RemoteException;
59import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070060import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.util.EventLog;
62import android.util.Log;
63import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070064import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070066import java.io.IOException;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070067import java.lang.ref.WeakReference;
68import java.util.ArrayList;
69import java.util.Iterator;
70import java.util.List;
71
72/**
73 * State and management of a single stack of activities.
74 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070075final class ActivityStack {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076 static final String TAG = ActivityManagerService.TAG;
Dianne Hackbornb961cd22011-06-21 12:13:37 -070077 static final boolean localLOGV = ActivityManagerService.localLOGV;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078 static final boolean DEBUG_SWITCH = ActivityManagerService.DEBUG_SWITCH;
79 static final boolean DEBUG_PAUSE = ActivityManagerService.DEBUG_PAUSE;
80 static final boolean DEBUG_VISBILITY = ActivityManagerService.DEBUG_VISBILITY;
81 static final boolean DEBUG_USER_LEAVING = ActivityManagerService.DEBUG_USER_LEAVING;
82 static final boolean DEBUG_TRANSITION = ActivityManagerService.DEBUG_TRANSITION;
83 static final boolean DEBUG_RESULTS = ActivityManagerService.DEBUG_RESULTS;
84 static final boolean DEBUG_CONFIGURATION = ActivityManagerService.DEBUG_CONFIGURATION;
85 static final boolean DEBUG_TASKS = ActivityManagerService.DEBUG_TASKS;
Dianne Hackborncc5a0552012-10-01 16:32:39 -070086 static final boolean DEBUG_CLEANUP = ActivityManagerService.DEBUG_CLEANUP;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070087
Dianne Hackbornce86ba82011-07-13 19:33:41 -070088 static final boolean DEBUG_STATES = false;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070089 static final boolean DEBUG_ADD_REMOVE = false;
90 static final boolean DEBUG_SAVED_STATE = false;
Dianne Hackborn07981492013-01-28 11:36:23 -080091 static final boolean DEBUG_APP = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070092
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070093 static final boolean VALIDATE_TOKENS = ActivityManagerService.VALIDATE_TOKENS;
94
95 // How long we wait until giving up on the last activity telling us it
96 // is idle.
97 static final int IDLE_TIMEOUT = 10*1000;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070098
99 // Ticks during which we check progress while waiting for an app to launch.
100 static final int LAUNCH_TICK = 500;
101
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700102 // How long we wait until giving up on the last activity to pause. This
103 // is short because it directly impacts the responsiveness of starting the
104 // next activity.
105 static final int PAUSE_TIMEOUT = 500;
106
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700107 // How long we wait for the activity to tell us it has stopped before
108 // giving up. This is a good amount of time because we really need this
109 // from the application in order to get its saved state.
110 static final int STOP_TIMEOUT = 10*1000;
111
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800112 // How long we can hold the sleep wake lock before giving up.
113 static final int SLEEP_TIMEOUT = 5*1000;
114
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700115 // How long we can hold the launch wake lock before giving up.
116 static final int LAUNCH_TIMEOUT = 10*1000;
117
118 // How long we wait until giving up on an activity telling us it has
119 // finished destroying itself.
120 static final int DESTROY_TIMEOUT = 10*1000;
121
122 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800123 // disabled.
124 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700125
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700126 // How long between activity launches that we consider safe to not warn
127 // the user about an unexpected activity being launched on top.
128 static final long START_WARN_TIME = 5*1000;
129
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700130 // Set to false to disable the preview that is shown while a new activity
131 // is being started.
132 static final boolean SHOW_APP_STARTING_PREVIEW = true;
133
134 enum ActivityState {
135 INITIALIZING,
136 RESUMED,
137 PAUSING,
138 PAUSED,
139 STOPPING,
140 STOPPED,
141 FINISHING,
142 DESTROYING,
143 DESTROYED
144 }
145
146 final ActivityManagerService mService;
147 final boolean mMainStack;
148
149 final Context mContext;
150
151 /**
152 * The back history of all previous (and possibly still
153 * running) activities. It contains HistoryRecord objects.
154 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700155 final ArrayList<ActivityRecord> mHistory = new ArrayList<ActivityRecord>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800156
157 /**
158 * Used for validating app tokens with window manager.
159 */
160 final ArrayList<IBinder> mValidateAppTokens = new ArrayList<IBinder>();
161
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700162 /**
163 * List of running activities, sorted by recent usage.
164 * The first entry in the list is the least recently used.
165 * It contains HistoryRecord objects.
166 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700167 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700168
169 /**
170 * List of activities that are waiting for a new activity
171 * to become visible before completing whatever operation they are
172 * supposed to do.
173 */
174 final ArrayList<ActivityRecord> mWaitingVisibleActivities
175 = new ArrayList<ActivityRecord>();
176
177 /**
178 * List of activities that are ready to be stopped, but waiting
179 * for the next activity to settle down before doing so. It contains
180 * HistoryRecord objects.
181 */
182 final ArrayList<ActivityRecord> mStoppingActivities
183 = new ArrayList<ActivityRecord>();
184
185 /**
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800186 * List of activities that are in the process of going to sleep.
187 */
188 final ArrayList<ActivityRecord> mGoingToSleepActivities
189 = new ArrayList<ActivityRecord>();
190
191 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700192 * Animations that for the current transition have requested not to
193 * be considered for the transition animation.
194 */
195 final ArrayList<ActivityRecord> mNoAnimActivities
196 = new ArrayList<ActivityRecord>();
197
198 /**
199 * List of activities that are ready to be finished, but waiting
200 * for the previous activity to settle down before doing so. It contains
201 * HistoryRecord objects.
202 */
203 final ArrayList<ActivityRecord> mFinishingActivities
204 = new ArrayList<ActivityRecord>();
205
206 /**
207 * List of people waiting to find out about the next launched activity.
208 */
209 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched
210 = new ArrayList<IActivityManager.WaitResult>();
211
212 /**
213 * List of people waiting to find out about the next visible activity.
214 */
215 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible
216 = new ArrayList<IActivityManager.WaitResult>();
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700217
218 final ArrayList<UserStartedState> mStartingUsers
219 = new ArrayList<UserStartedState>();
220
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700221 /**
222 * Set when the system is going to sleep, until we have
223 * successfully paused the current activity and released our wake lock.
224 * At that point the system is allowed to actually sleep.
225 */
226 final PowerManager.WakeLock mGoingToSleep;
227
228 /**
229 * We don't want to allow the device to go to sleep while in the process
230 * of launching an activity. This is primarily to allow alarm intent
231 * receivers to launch an activity and get that to run before the device
232 * goes back to sleep.
233 */
234 final PowerManager.WakeLock mLaunchingActivity;
235
236 /**
237 * When we are in the process of pausing an activity, before starting the
238 * next one, this variable holds the activity that is currently being paused.
239 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800240 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700241
242 /**
243 * This is the last activity that we put into the paused state. This is
244 * used to determine if we need to do an activity transition while sleeping,
245 * when we normally hold the top activity paused.
246 */
247 ActivityRecord mLastPausedActivity = null;
248
249 /**
250 * Current activity that is resumed, or null if there is none.
251 */
252 ActivityRecord mResumedActivity = null;
253
254 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700255 * This is the last activity that has been started. It is only used to
256 * identify when multiple activities are started at once so that the user
257 * can be warned they may not be in the activity they think they are.
258 */
259 ActivityRecord mLastStartedActivity = null;
260
261 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700262 * Set when we know we are going to be calling updateConfiguration()
263 * soon, so want to skip intermediate config checks.
264 */
265 boolean mConfigWillChange;
266
267 /**
268 * Set to indicate whether to issue an onUserLeaving callback when a
269 * newly launched activity is being brought in front of us.
270 */
271 boolean mUserLeaving = false;
272
273 long mInitialStartTime = 0;
274
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800275 /**
276 * Set when we have taken too long waiting to go to sleep.
277 */
278 boolean mSleepTimeout = false;
279
Dianne Hackborn90c52de2011-09-23 12:57:44 -0700280 /**
281 * Dismiss the keyguard after the next activity is displayed?
282 */
283 boolean mDismissKeyguardOnNextActivity = false;
284
Craig Mautnerb12428a2012-12-20 16:07:06 -0800285 /**
286 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
287 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
288 */
289 private ActivityRecord mLastScreenshotActivity = null;
290 private Bitmap mLastScreenshotBitmap = null;
291
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800292 int mThumbnailWidth = -1;
293 int mThumbnailHeight = -1;
294
Amith Yamasani742a6712011-05-04 14:49:28 -0700295 private int mCurrentUser;
296
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800297 static final int SLEEP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG;
298 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
299 static final int IDLE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
300 static final int IDLE_NOW_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
301 static final int LAUNCH_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
302 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
303 static final int RESUME_TOP_ACTIVITY_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn29ba7e62012-03-16 15:03:36 -0700304 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700305 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 8;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700306 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 9;
307
308 static class ScheduleDestroyArgs {
309 final ProcessRecord mOwner;
310 final boolean mOomAdj;
311 final String mReason;
312 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
313 mOwner = owner;
314 mOomAdj = oomAdj;
315 mReason = reason;
316 }
317 }
318
Zoran Marcetaf958b322012-08-09 20:27:12 +0900319 final Handler mHandler;
320
321 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700322 //public Handler() {
323 // if (localLOGV) Slog.v(TAG, "Handler started!");
324 //}
Zoran Marcetaf958b322012-08-09 20:27:12 +0900325 public ActivityStackHandler(Looper looper) {
326 super(looper);
327 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700328
Zoran Marcetaf958b322012-08-09 20:27:12 +0900329 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700330 public void handleMessage(Message msg) {
331 switch (msg.what) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800332 case SLEEP_TIMEOUT_MSG: {
Dianne Hackborn8e8d65f2011-08-11 19:36:18 -0700333 synchronized (mService) {
334 if (mService.isSleeping()) {
335 Slog.w(TAG, "Sleep timeout! Sleeping now.");
336 mSleepTimeout = true;
337 checkReadyForSleepLocked();
338 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800339 }
340 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700341 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800342 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700343 // We don't at this point know if the activity is fullscreen,
344 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800345 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700346 synchronized (mService) {
347 if (r.app != null) {
348 mService.logAppTooSlow(r.app, r.pauseTime,
349 "pausing " + r);
350 }
351 }
352
Dianne Hackbornbe707852011-11-11 14:32:10 -0800353 activityPaused(r != null ? r.appToken : null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700354 } break;
355 case IDLE_TIMEOUT_MSG: {
356 if (mService.mDidDexOpt) {
357 mService.mDidDexOpt = false;
358 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
359 nmsg.obj = msg.obj;
360 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
361 return;
362 }
363 // We don't at this point know if the activity is fullscreen,
364 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800365 ActivityRecord r = (ActivityRecord)msg.obj;
366 Slog.w(TAG, "Activity idle timeout for " + r);
367 activityIdleInternal(r != null ? r.appToken : null, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700368 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700369 case LAUNCH_TICK_MSG: {
370 ActivityRecord r = (ActivityRecord)msg.obj;
371 synchronized (mService) {
372 if (r.continueLaunchTickingLocked()) {
373 mService.logAppTooSlow(r.app, r.launchTickTime,
374 "launching " + r);
375 }
376 }
377 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700378 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800379 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700380 // We don't at this point know if the activity is fullscreen,
381 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800382 Slog.w(TAG, "Activity destroy timeout for " + r);
383 activityDestroyed(r != null ? r.appToken : null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700384 } break;
385 case IDLE_NOW_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800386 ActivityRecord r = (ActivityRecord)msg.obj;
387 activityIdleInternal(r != null ? r.appToken : null, false, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700388 } break;
389 case LAUNCH_TIMEOUT_MSG: {
390 if (mService.mDidDexOpt) {
391 mService.mDidDexOpt = false;
392 Message nmsg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
393 mHandler.sendMessageDelayed(nmsg, LAUNCH_TIMEOUT);
394 return;
395 }
396 synchronized (mService) {
397 if (mLaunchingActivity.isHeld()) {
398 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
399 mLaunchingActivity.release();
400 }
401 }
402 } break;
403 case RESUME_TOP_ACTIVITY_MSG: {
404 synchronized (mService) {
405 resumeTopActivityLocked(null);
406 }
407 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700408 case STOP_TIMEOUT_MSG: {
409 ActivityRecord r = (ActivityRecord)msg.obj;
410 // We don't at this point know if the activity is fullscreen,
411 // so we need to be conservative and assume it isn't.
412 Slog.w(TAG, "Activity stop timeout for " + r);
413 synchronized (mService) {
414 if (r.isInHistory()) {
415 activityStoppedLocked(r, null, null, null);
416 }
417 }
418 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700419 case DESTROY_ACTIVITIES_MSG: {
420 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
421 synchronized (mService) {
422 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
423 }
424 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700425 }
426 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800427 }
428
Zoran Marcetaf958b322012-08-09 20:27:12 +0900429 ActivityStack(ActivityManagerService service, Context context, boolean mainStack, Looper looper) {
430 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700431 mService = service;
432 mContext = context;
433 mMainStack = mainStack;
434 PowerManager pm =
435 (PowerManager)context.getSystemService(Context.POWER_SERVICE);
436 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
437 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
438 mLaunchingActivity.setReferenceCounted(false);
439 }
Craig Mautner5962b122012-10-05 14:45:52 -0700440
441 private boolean okToShow(ActivityRecord r) {
442 return r.userId == mCurrentUser
443 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
444 }
445
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700446 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
447 int i = mHistory.size()-1;
448 while (i >= 0) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700449 ActivityRecord r = mHistory.get(i);
Craig Mautner5962b122012-10-05 14:45:52 -0700450 if (!r.finishing && r != notTop && okToShow(r)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700451 return r;
452 }
453 i--;
454 }
455 return null;
456 }
457
458 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
459 int i = mHistory.size()-1;
460 while (i >= 0) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700461 ActivityRecord r = mHistory.get(i);
Craig Mautner5962b122012-10-05 14:45:52 -0700462 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700463 return r;
464 }
465 i--;
466 }
467 return null;
468 }
469
470 /**
471 * This is a simplified version of topRunningActivityLocked that provides a number of
472 * optional skip-over modes. It is intended for use with the ActivityController hook only.
473 *
474 * @param token If non-null, any history records matching this token will be skipped.
475 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
476 *
477 * @return Returns the HistoryRecord of the next activity on the stack.
478 */
479 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
480 int i = mHistory.size()-1;
481 while (i >= 0) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700482 ActivityRecord r = mHistory.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700483 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani259d5e52012-08-31 15:11:01 -0700484 if (!r.finishing && (token != r.appToken) && (taskId != r.task.taskId)
Craig Mautner5962b122012-10-05 14:45:52 -0700485 && okToShow(r)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700486 return r;
487 }
488 i--;
489 }
490 return null;
491 }
492
493 final int indexOfTokenLocked(IBinder token) {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800494 return mHistory.indexOf(ActivityRecord.forToken(token));
495 }
496
497 final int indexOfActivityLocked(ActivityRecord r) {
498 return mHistory.indexOf(r);
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700499 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700500
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700501 final ActivityRecord isInStackLocked(IBinder token) {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800502 ActivityRecord r = ActivityRecord.forToken(token);
503 if (mHistory.contains(r)) {
504 return r;
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700505 }
506 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700507 }
508
509 private final boolean updateLRUListLocked(ActivityRecord r) {
510 final boolean hadit = mLRUActivities.remove(r);
511 mLRUActivities.add(r);
512 return hadit;
513 }
514
515 /**
516 * Returns the top activity in any existing task matching the given
517 * Intent. Returns null if no such task is found.
518 */
519 private ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) {
520 ComponentName cls = intent.getComponent();
521 if (info.targetActivity != null) {
522 cls = new ComponentName(info.packageName, info.targetActivity);
523 }
524
525 TaskRecord cp = null;
526
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700527 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700528 final int N = mHistory.size();
529 for (int i=(N-1); i>=0; i--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700530 ActivityRecord r = mHistory.get(i);
Amith Yamasani742a6712011-05-04 14:49:28 -0700531 if (!r.finishing && r.task != cp && r.userId == userId
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700532 && r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
533 cp = r.task;
534 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
535 // + "/aff=" + r.task.affinity + " to new cls="
536 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
537 if (r.task.affinity != null) {
538 if (r.task.affinity.equals(info.taskAffinity)) {
539 //Slog.i(TAG, "Found matching affinity!");
540 return r;
541 }
542 } else if (r.task.intent != null
543 && r.task.intent.getComponent().equals(cls)) {
544 //Slog.i(TAG, "Found matching class!");
545 //dump();
546 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
547 return r;
548 } else if (r.task.affinityIntent != null
549 && r.task.affinityIntent.getComponent().equals(cls)) {
550 //Slog.i(TAG, "Found matching class!");
551 //dump();
552 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
553 return r;
554 }
555 }
556 }
557
558 return null;
559 }
560
561 /**
562 * Returns the first activity (starting from the top of the stack) that
563 * is the same as the given activity. Returns null if no such activity
564 * is found.
565 */
566 private ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
567 ComponentName cls = intent.getComponent();
568 if (info.targetActivity != null) {
569 cls = new ComponentName(info.packageName, info.targetActivity);
570 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700571 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700572
573 final int N = mHistory.size();
574 for (int i=(N-1); i>=0; i--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700575 ActivityRecord r = mHistory.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700576 if (!r.finishing) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700577 if (r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700578 //Slog.i(TAG, "Found matching class!");
579 //dump();
580 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
581 return r;
582 }
583 }
584 }
585
586 return null;
587 }
588
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700589 final void showAskCompatModeDialogLocked(ActivityRecord r) {
590 Message msg = Message.obtain();
591 msg.what = ActivityManagerService.SHOW_COMPAT_MODE_DIALOG_MSG;
592 msg.obj = r.task.askedCompatMode ? null : r;
593 mService.mHandler.sendMessage(msg);
594 }
595
Amith Yamasani742a6712011-05-04 14:49:28 -0700596 /*
597 * Move the activities around in the stack to bring a user to the foreground.
598 * @return whether there are any activities for the specified user.
599 */
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700600 final boolean switchUserLocked(int userId, UserStartedState uss) {
601 mCurrentUser = userId;
602 mStartingUsers.add(uss);
Amith Yamasani742a6712011-05-04 14:49:28 -0700603
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700604 // Only one activity? Nothing to do...
605 if (mHistory.size() < 2)
606 return false;
Amith Yamasani742a6712011-05-04 14:49:28 -0700607
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700608 boolean haveActivities = false;
609 // Check if the top activity is from the new user.
610 ActivityRecord top = mHistory.get(mHistory.size() - 1);
611 if (top.userId == userId) return true;
612 // Otherwise, move the user's activities to the top.
613 int N = mHistory.size();
614 int i = 0;
615 while (i < N) {
616 ActivityRecord r = mHistory.get(i);
617 if (r.userId == userId) {
618 ActivityRecord moveToTop = mHistory.remove(i);
619 mHistory.add(moveToTop);
620 // No need to check the top one now
621 N--;
622 haveActivities = true;
623 } else {
624 i++;
Amith Yamasani742a6712011-05-04 14:49:28 -0700625 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700626 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700627 // Transition from the old top to the new top
628 resumeTopActivityLocked(top);
629 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700630 }
631
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700632 final boolean realStartActivityLocked(ActivityRecord r,
633 ProcessRecord app, boolean andResume, boolean checkConfig)
634 throws RemoteException {
635
636 r.startFreezingScreenLocked(app, 0);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800637 mService.mWindowManager.setAppVisibility(r.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700638
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700639 // schedule launch ticks to collect information about slow apps.
640 r.startLaunchTickingLocked();
641
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700642 // Have the window manager re-evaluate the orientation of
643 // the screen based on the new activity order. Note that
644 // as a result of this, it can call back into the activity
645 // manager with a new orientation. We don't care about that,
646 // because the activity is not currently running so we are
647 // just restarting it anyway.
648 if (checkConfig) {
649 Configuration config = mService.mWindowManager.updateOrientationFromAppTokens(
650 mService.mConfiguration,
Dianne Hackbornbe707852011-11-11 14:32:10 -0800651 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Dianne Hackborn813075a62011-11-14 17:45:19 -0800652 mService.updateConfigurationLocked(config, r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700653 }
654
655 r.app = app;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700656 app.waitingToKill = null;
Dianne Hackborn07981492013-01-28 11:36:23 -0800657 r.launchCount++;
658 r.lastLaunchTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700659
660 if (localLOGV) Slog.v(TAG, "Launching: " + r);
661
662 int idx = app.activities.indexOf(r);
663 if (idx < 0) {
664 app.activities.add(r);
665 }
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700666 mService.updateLruProcessLocked(app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700667
668 try {
669 if (app.thread == null) {
670 throw new RemoteException();
671 }
672 List<ResultInfo> results = null;
673 List<Intent> newIntents = null;
674 if (andResume) {
675 results = r.results;
676 newIntents = r.newIntents;
677 }
678 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
679 + " icicle=" + r.icicle
680 + " with results=" + results + " newIntents=" + newIntents
681 + " andResume=" + andResume);
682 if (andResume) {
683 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700684 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700685 r.task.taskId, r.shortComponentName);
686 }
687 if (r.isHomeActivity) {
688 mService.mHomeProcess = app;
689 }
690 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800691 r.sleeping = false;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400692 r.forceNewConfig = false;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700693 showAskCompatModeDialogLocked(r);
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700694 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700695 String profileFile = null;
696 ParcelFileDescriptor profileFd = null;
697 boolean profileAutoStop = false;
698 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
699 if (mService.mProfileProc == null || mService.mProfileProc == app) {
700 mService.mProfileProc = app;
701 profileFile = mService.mProfileFile;
702 profileFd = mService.mProfileFd;
703 profileAutoStop = mService.mAutoStopProfiler;
704 }
705 }
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -0700706 app.hasShownUi = true;
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700707 app.pendingUiClean = true;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700708 if (profileFd != null) {
709 try {
710 profileFd = profileFd.dup();
711 } catch (IOException e) {
712 profileFd = null;
713 }
714 }
Dianne Hackbornbe707852011-11-11 14:32:10 -0800715 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Dianne Hackborn813075a62011-11-14 17:45:19 -0800716 System.identityHashCode(r), r.info,
717 new Configuration(mService.mConfiguration),
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700718 r.compat, r.icicle, results, newIntents, !andResume,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700719 mService.isNextTransitionForward(), profileFile, profileFd,
720 profileAutoStop);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700721
Dianne Hackborn54e570f2010-10-04 18:32:32 -0700722 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700723 // This may be a heavy-weight process! Note that the package
724 // manager will ensure that only activity can run in the main
725 // process of the .apk, which is the only thing that will be
726 // considered heavy-weight.
727 if (app.processName.equals(app.info.packageName)) {
728 if (mService.mHeavyWeightProcess != null
729 && mService.mHeavyWeightProcess != app) {
730 Log.w(TAG, "Starting new heavy weight process " + app
731 + " when already running "
732 + mService.mHeavyWeightProcess);
733 }
734 mService.mHeavyWeightProcess = app;
735 Message msg = mService.mHandler.obtainMessage(
736 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
737 msg.obj = r;
738 mService.mHandler.sendMessage(msg);
739 }
740 }
741
742 } catch (RemoteException e) {
743 if (r.launchFailed) {
744 // This is the second time we failed -- finish activity
745 // and give up.
746 Slog.e(TAG, "Second failure launching "
747 + r.intent.getComponent().flattenToShortString()
748 + ", giving up", e);
749 mService.appDiedLocked(app, app.pid, app.thread);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800750 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700751 "2nd-crash", false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700752 return false;
753 }
754
755 // This is the first time we failed -- restart process and
756 // retry.
757 app.activities.remove(r);
758 throw e;
759 }
760
761 r.launchFailed = false;
762 if (updateLRUListLocked(r)) {
763 Slog.w(TAG, "Activity " + r
764 + " being launched, but already in LRU list");
765 }
766
767 if (andResume) {
768 // As part of the process of launching, ActivityThread also performs
769 // a resume.
770 r.state = ActivityState.RESUMED;
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700771 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
772 + " (starting new instance)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700773 r.stopped = false;
774 mResumedActivity = r;
775 r.task.touchActiveTime();
Dianne Hackborn88819b22010-12-21 18:18:02 -0800776 if (mMainStack) {
777 mService.addRecentTaskLocked(r.task);
778 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700779 completeResumeLocked(r);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800780 checkReadyForSleepLocked();
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700781 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700782 } else {
783 // This activity is not starting in the resumed state... which
784 // should look like we asked it to pause+stop (but remain visible),
785 // and it has done so and reported back the current icicle and
786 // other state.
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700787 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
788 + " (starting in stopped state)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700789 r.state = ActivityState.STOPPED;
790 r.stopped = true;
791 }
792
793 // Launch the new version setup screen if needed. We do this -after-
794 // launching the initial activity (that is, home), so that it can have
795 // a chance to initialize itself while in the background, making the
796 // switch back to it faster and look better.
797 if (mMainStack) {
798 mService.startSetupActivityLocked();
799 }
800
801 return true;
802 }
803
804 private final void startSpecificActivityLocked(ActivityRecord r,
805 boolean andResume, boolean checkConfig) {
806 // Is this activity's application already running?
807 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
808 r.info.applicationInfo.uid);
Dianne Hackborn07981492013-01-28 11:36:23 -0800809
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700810 if (r.launchTime == 0) {
811 r.launchTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700812 if (mInitialStartTime == 0) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700813 mInitialStartTime = r.launchTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700814 }
815 } else if (mInitialStartTime == 0) {
816 mInitialStartTime = SystemClock.uptimeMillis();
817 }
818
819 if (app != null && app.thread != null) {
820 try {
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700821 app.addPackage(r.info.packageName);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700822 realStartActivityLocked(r, app, andResume, checkConfig);
823 return;
824 } catch (RemoteException e) {
825 Slog.w(TAG, "Exception when starting activity "
826 + r.intent.getComponent().flattenToShortString(), e);
827 }
828
829 // If a dead object exception was thrown -- fall through to
830 // restart the application.
831 }
832
833 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800834 "activity", r.intent.getComponent(), false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700835 }
836
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800837 void stopIfSleepingLocked() {
838 if (mService.isSleeping()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700839 if (!mGoingToSleep.isHeld()) {
840 mGoingToSleep.acquire();
841 if (mLaunchingActivity.isHeld()) {
842 mLaunchingActivity.release();
843 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
844 }
845 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800846 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
847 Message msg = mHandler.obtainMessage(SLEEP_TIMEOUT_MSG);
848 mHandler.sendMessageDelayed(msg, SLEEP_TIMEOUT);
849 checkReadyForSleepLocked();
850 }
851 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700852
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800853 void awakeFromSleepingLocked() {
854 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
855 mSleepTimeout = false;
856 if (mGoingToSleep.isHeld()) {
857 mGoingToSleep.release();
858 }
859 // Ensure activities are no longer sleeping.
860 for (int i=mHistory.size()-1; i>=0; i--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700861 ActivityRecord r = mHistory.get(i);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800862 r.setSleeping(false);
863 }
864 mGoingToSleepActivities.clear();
865 }
866
867 void activitySleptLocked(ActivityRecord r) {
868 mGoingToSleepActivities.remove(r);
869 checkReadyForSleepLocked();
870 }
871
872 void checkReadyForSleepLocked() {
873 if (!mService.isSleeping()) {
874 // Do not care.
875 return;
876 }
877
878 if (!mSleepTimeout) {
879 if (mResumedActivity != null) {
880 // Still have something resumed; can't sleep until it is paused.
881 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700882 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
883 startPausingLocked(false, true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800884 return;
885 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800886 if (mPausingActivity != null) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800887 // Still waiting for something to pause; can't sleep yet.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800888 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800889 return;
890 }
891
892 if (mStoppingActivities.size() > 0) {
893 // Still need to tell some activities to stop; can't sleep yet.
894 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
895 + mStoppingActivities.size() + " activities");
Dianne Hackborn80a7ac12011-09-22 18:32:52 -0700896 scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800897 return;
898 }
899
900 ensureActivitiesVisibleLocked(null, 0);
901
902 // Make sure any stopped but visible activities are now sleeping.
903 // This ensures that the activity's onStop() is called.
904 for (int i=mHistory.size()-1; i>=0; i--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700905 ActivityRecord r = mHistory.get(i);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800906 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
907 r.setSleeping(true);
908 }
909 }
910
911 if (mGoingToSleepActivities.size() > 0) {
912 // Still need to tell some activities to sleep; can't sleep yet.
913 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
914 + mGoingToSleepActivities.size() + " activities");
915 return;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700916 }
917 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800918
919 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
920
921 if (mGoingToSleep.isHeld()) {
922 mGoingToSleep.release();
923 }
924 if (mService.mShuttingDown) {
925 mService.notifyAll();
926 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700927 }
Craig Mautner59c00972012-07-30 12:10:24 -0700928
Dianne Hackbornd2835932010-12-13 16:28:46 -0800929 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800930 if (who.noDisplay) {
931 return null;
932 }
933
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800934 Resources res = mService.mContext.getResources();
935 int w = mThumbnailWidth;
936 int h = mThumbnailHeight;
937 if (w < 0) {
938 mThumbnailWidth = w =
939 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
940 mThumbnailHeight = h =
941 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
942 }
943
944 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800945 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
946 || mLastScreenshotBitmap.getWidth() != w
947 || mLastScreenshotBitmap.getHeight() != h) {
948 mLastScreenshotActivity = who;
949 mLastScreenshotBitmap = mService.mWindowManager.screenshotApplications(
950 who.appToken, Display.DEFAULT_DISPLAY, w, h);
951 }
952 if (mLastScreenshotBitmap != null) {
953 return mLastScreenshotBitmap.copy(Config.ARGB_8888, true);
954 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800955 }
956 return null;
957 }
958
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700959 private final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800960 if (mPausingActivity != null) {
961 RuntimeException e = new RuntimeException();
962 Slog.e(TAG, "Trying to pause when pause is already pending for "
963 + mPausingActivity, e);
964 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700965 ActivityRecord prev = mResumedActivity;
966 if (prev == null) {
967 RuntimeException e = new RuntimeException();
968 Slog.e(TAG, "Trying to pause when nothing is resumed", e);
969 resumeTopActivityLocked(null);
970 return;
971 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700972 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
973 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700974 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800975 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700976 mLastPausedActivity = prev;
977 prev.state = ActivityState.PAUSING;
978 prev.task.touchActiveTime();
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700979 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700980
981 mService.updateCpuStats();
982
983 if (prev.app != null && prev.app.thread != null) {
984 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
985 try {
986 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700987 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700988 prev.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800989 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
990 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700991 if (mMainStack) {
992 mService.updateUsageStats(prev, false);
993 }
994 } catch (Exception e) {
995 // Ignore exception, if process died other code will cleanup.
996 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800997 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700998 mLastPausedActivity = null;
999 }
1000 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001001 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001002 mLastPausedActivity = null;
1003 }
1004
1005 // If we are not going to sleep, we want to ensure the device is
1006 // awake until the next activity is started.
1007 if (!mService.mSleeping && !mService.mShuttingDown) {
1008 mLaunchingActivity.acquire();
1009 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1010 // To be safe, don't allow the wake lock to be held for too long.
1011 Message msg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
1012 mHandler.sendMessageDelayed(msg, LAUNCH_TIMEOUT);
1013 }
1014 }
1015
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001016
1017 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001018 // Have the window manager pause its key dispatching until the new
1019 // activity has started. If we're pausing the activity just because
1020 // the screen is being turned off and the UI is sleeping, don't interrupt
1021 // key dispatch; the same activity will pick it up again on wakeup.
1022 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001023 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001024 } else {
1025 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
1026 }
1027
1028 // Schedule a pause timeout in case the app doesn't respond.
1029 // We don't give it much time because this directly impacts the
1030 // responsiveness seen by the user.
1031 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1032 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001033 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001034 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
1035 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
1036 } else {
1037 // This activity failed to schedule the
1038 // pause, so just treat it as being paused now.
1039 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001040 resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001041 }
1042 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001043
1044 final void activityResumed(IBinder token) {
1045 ActivityRecord r = null;
1046
1047 synchronized (mService) {
1048 int index = indexOfTokenLocked(token);
1049 if (index >= 0) {
1050 r = mHistory.get(index);
1051 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Resumed activity; dropping state of: " + r);
1052 r.icicle = null;
1053 r.haveState = false;
1054 }
1055 }
1056 }
1057
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001058 final void activityPaused(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001059 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001060 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001061
1062 ActivityRecord r = null;
1063
1064 synchronized (mService) {
1065 int index = indexOfTokenLocked(token);
1066 if (index >= 0) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001067 r = mHistory.get(index);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001068 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001069 if (mPausingActivity == r) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001070 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
1071 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001072 r.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001073 completePauseLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001074 } else {
1075 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001076 r.userId, System.identityHashCode(r), r.shortComponentName,
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001077 mPausingActivity != null
1078 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001079 }
1080 }
1081 }
1082 }
1083
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001084 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
1085 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001086 if (r.state != ActivityState.STOPPING) {
1087 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1088 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1089 return;
1090 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07001091 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001092 if (icicle != null) {
1093 // If icicle is null, this is happening due to a timeout, so we
1094 // haven't really saved the state.
1095 r.icicle = icicle;
1096 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001097 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001098 r.updateThumbnail(thumbnail, description);
1099 }
1100 if (!r.stopped) {
1101 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
1102 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1103 r.stopped = true;
1104 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001105 if (r.finishing) {
1106 r.clearOptionsLocked();
1107 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001108 if (r.configDestroy) {
1109 destroyActivityLocked(r, true, false, "stop-config");
1110 resumeTopActivityLocked(null);
1111 } else {
1112 // Now that this process has stopped, we may want to consider
1113 // it to be the previous app to try to keep around in case
1114 // the user wants to return to it.
1115 ProcessRecord fgApp = null;
1116 if (mResumedActivity != null) {
1117 fgApp = mResumedActivity.app;
1118 } else if (mPausingActivity != null) {
1119 fgApp = mPausingActivity.app;
1120 }
1121 if (r.app != null && fgApp != null && r.app != fgApp
1122 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
1123 && r.app != mService.mHomeProcess) {
1124 mService.mPreviousProcess = r.app;
1125 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
1126 }
Dianne Hackborn50685602011-12-01 12:23:37 -08001127 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001128 }
1129 }
1130 }
1131
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001132 private final void completePauseLocked() {
1133 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001134 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
1135
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001136 if (prev != null) {
1137 if (prev.finishing) {
1138 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001139 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001140 } else if (prev.app != null) {
1141 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
1142 if (prev.waitingVisible) {
1143 prev.waitingVisible = false;
1144 mWaitingVisibleActivities.remove(prev);
1145 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
1146 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001147 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001148 if (prev.configDestroy) {
1149 // The previous is being paused because the configuration
1150 // is changing, which means it is actually stopping...
1151 // To juggle the fact that we are also starting a new
1152 // instance right now, we need to first completely stop
1153 // the current instance before starting the new one.
1154 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
1155 destroyActivityLocked(prev, true, false, "pause-config");
1156 } else {
1157 mStoppingActivities.add(prev);
1158 if (mStoppingActivities.size() > 3) {
1159 // If we already have a few activities waiting to stop,
1160 // then give up on things going idle and start clearing
1161 // them out.
1162 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
1163 scheduleIdleLocked();
1164 } else {
1165 checkReadyForSleepLocked();
1166 }
1167 }
1168 } else {
1169 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
1170 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001171 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001172 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001173 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001174
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001175 if (!mService.isSleeping()) {
1176 resumeTopActivityLocked(prev);
1177 } else {
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001178 checkReadyForSleepLocked();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07001179 ActivityRecord top = topRunningActivityLocked(null);
1180 if (top == null || (prev != null && top != prev)) {
1181 // If there are no more activities available to run,
1182 // do resume anyway to start something. Also if the top
1183 // activity on the stack is not the just paused activity,
1184 // we need to go ahead and resume it to ensure we complete
1185 // an in-flight app switch.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001186 resumeTopActivityLocked(null);
1187 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001188 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001189
1190 if (prev != null) {
1191 prev.resumeKeyDispatchingLocked();
1192 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001193
1194 if (prev.app != null && prev.cpuTimeAtResume > 0
1195 && mService.mBatteryStatsService.isOnBattery()) {
1196 long diff = 0;
1197 synchronized (mService.mProcessStatsThread) {
1198 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid)
1199 - prev.cpuTimeAtResume;
1200 }
1201 if (diff > 0) {
1202 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1203 synchronized (bsi) {
1204 BatteryStatsImpl.Uid.Proc ps =
1205 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
1206 prev.info.packageName);
1207 if (ps != null) {
1208 ps.addForegroundTimeLocked(diff);
1209 }
1210 }
1211 }
1212 }
1213 prev.cpuTimeAtResume = 0; // reset it
1214 }
1215
1216 /**
1217 * Once we know that we have asked an application to put an activity in
1218 * the resumed state (either by launching it or explicitly telling it),
1219 * this function updates the rest of our state to match that fact.
1220 */
1221 private final void completeResumeLocked(ActivityRecord next) {
1222 next.idle = false;
1223 next.results = null;
1224 next.newIntents = null;
1225
1226 // schedule an idle timeout in case the app doesn't do it for us.
1227 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
1228 msg.obj = next;
1229 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
1230
1231 if (false) {
1232 // The activity was never told to pause, so just keep
1233 // things going as-is. To maintain our own state,
1234 // we need to emulate it coming back and saying it is
1235 // idle.
1236 msg = mHandler.obtainMessage(IDLE_NOW_MSG);
1237 msg.obj = next;
1238 mHandler.sendMessage(msg);
1239 }
1240
1241 if (mMainStack) {
1242 mService.reportResumedActivityLocked(next);
1243 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07001244
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001245 if (mMainStack) {
1246 mService.setFocusedActivityLocked(next);
1247 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001248 next.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001249 ensureActivitiesVisibleLocked(null, 0);
1250 mService.mWindowManager.executeAppTransition();
1251 mNoAnimActivities.clear();
1252
1253 // Mark the point when the activity is resuming
1254 // TODO: To be more accurate, the mark should be before the onCreate,
1255 // not after the onResume. But for subsequent starts, onResume is fine.
1256 if (next.app != null) {
1257 synchronized (mService.mProcessStatsThread) {
1258 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid);
1259 }
1260 } else {
1261 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1262 }
1263 }
1264
1265 /**
1266 * Make sure that all activities that need to be visible (that is, they
1267 * currently can be seen by the user) actually are.
1268 */
1269 final void ensureActivitiesVisibleLocked(ActivityRecord top,
1270 ActivityRecord starting, String onlyThisProcess, int configChanges) {
1271 if (DEBUG_VISBILITY) Slog.v(
1272 TAG, "ensureActivitiesVisible behind " + top
1273 + " configChanges=0x" + Integer.toHexString(configChanges));
1274
1275 // If the top activity is not fullscreen, then we need to
1276 // make sure any activities under it are now visible.
1277 final int count = mHistory.size();
1278 int i = count-1;
1279 while (mHistory.get(i) != top) {
1280 i--;
1281 }
1282 ActivityRecord r;
1283 boolean behindFullscreen = false;
1284 for (; i>=0; i--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001285 r = mHistory.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001286 if (DEBUG_VISBILITY) Slog.v(
1287 TAG, "Make visible? " + r + " finishing=" + r.finishing
1288 + " state=" + r.state);
1289 if (r.finishing) {
1290 continue;
1291 }
1292
1293 final boolean doThisProcess = onlyThisProcess == null
1294 || onlyThisProcess.equals(r.processName);
1295
1296 // First: if this is not the current activity being started, make
1297 // sure it matches the current configuration.
1298 if (r != starting && doThisProcess) {
1299 ensureActivityConfigurationLocked(r, 0);
1300 }
1301
1302 if (r.app == null || r.app.thread == null) {
1303 if (onlyThisProcess == null
1304 || onlyThisProcess.equals(r.processName)) {
1305 // This activity needs to be visible, but isn't even
1306 // running... get it started, but don't resume it
1307 // at this point.
1308 if (DEBUG_VISBILITY) Slog.v(
1309 TAG, "Start and freeze screen for " + r);
1310 if (r != starting) {
1311 r.startFreezingScreenLocked(r.app, configChanges);
1312 }
1313 if (!r.visible) {
1314 if (DEBUG_VISBILITY) Slog.v(
1315 TAG, "Starting and making visible: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001316 mService.mWindowManager.setAppVisibility(r.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001317 }
1318 if (r != starting) {
1319 startSpecificActivityLocked(r, false, false);
1320 }
1321 }
1322
1323 } else if (r.visible) {
1324 // If this activity is already visible, then there is nothing
1325 // else to do here.
1326 if (DEBUG_VISBILITY) Slog.v(
1327 TAG, "Skipping: already visible at " + r);
1328 r.stopFreezingScreenLocked(false);
1329
1330 } else if (onlyThisProcess == null) {
1331 // This activity is not currently visible, but is running.
1332 // Tell it to become visible.
1333 r.visible = true;
1334 if (r.state != ActivityState.RESUMED && r != starting) {
1335 // If this activity is paused, tell it
1336 // to now show its window.
1337 if (DEBUG_VISBILITY) Slog.v(
1338 TAG, "Making visible and scheduling visibility: " + r);
1339 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001340 mService.mWindowManager.setAppVisibility(r.appToken, true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001341 r.sleeping = false;
Dianne Hackborn905577f2011-09-07 18:31:28 -07001342 r.app.pendingUiClean = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001343 r.app.thread.scheduleWindowVisibility(r.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001344 r.stopFreezingScreenLocked(false);
1345 } catch (Exception e) {
1346 // Just skip on any failure; we'll make it
1347 // visible when it next restarts.
1348 Slog.w(TAG, "Exception thrown making visibile: "
1349 + r.intent.getComponent(), e);
1350 }
1351 }
1352 }
1353
1354 // Aggregate current change flags.
1355 configChanges |= r.configChangeFlags;
1356
1357 if (r.fullscreen) {
1358 // At this point, nothing else needs to be shown
1359 if (DEBUG_VISBILITY) Slog.v(
1360 TAG, "Stopping: fullscreen at " + r);
1361 behindFullscreen = true;
1362 i--;
1363 break;
1364 }
1365 }
1366
1367 // Now for any activities that aren't visible to the user, make
1368 // sure they no longer are keeping the screen frozen.
1369 while (i >= 0) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001370 r = mHistory.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001371 if (DEBUG_VISBILITY) Slog.v(
1372 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1373 + " state=" + r.state
1374 + " behindFullscreen=" + behindFullscreen);
1375 if (!r.finishing) {
1376 if (behindFullscreen) {
1377 if (r.visible) {
1378 if (DEBUG_VISBILITY) Slog.v(
1379 TAG, "Making invisible: " + r);
1380 r.visible = false;
1381 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001382 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001383 if ((r.state == ActivityState.STOPPING
1384 || r.state == ActivityState.STOPPED)
1385 && r.app != null && r.app.thread != null) {
1386 if (DEBUG_VISBILITY) Slog.v(
1387 TAG, "Scheduling invisibility: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001388 r.app.thread.scheduleWindowVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001389 }
1390 } catch (Exception e) {
1391 // Just skip on any failure; we'll make it
1392 // visible when it next restarts.
1393 Slog.w(TAG, "Exception thrown making hidden: "
1394 + r.intent.getComponent(), e);
1395 }
1396 } else {
1397 if (DEBUG_VISBILITY) Slog.v(
1398 TAG, "Already invisible: " + r);
1399 }
1400 } else if (r.fullscreen) {
1401 if (DEBUG_VISBILITY) Slog.v(
1402 TAG, "Now behindFullscreen: " + r);
1403 behindFullscreen = true;
1404 }
1405 }
1406 i--;
1407 }
1408 }
1409
1410 /**
1411 * Version of ensureActivitiesVisible that can easily be called anywhere.
1412 */
1413 final void ensureActivitiesVisibleLocked(ActivityRecord starting,
1414 int configChanges) {
1415 ActivityRecord r = topRunningActivityLocked(null);
1416 if (r != null) {
1417 ensureActivitiesVisibleLocked(r, starting, null, configChanges);
1418 }
1419 }
1420
1421 /**
1422 * Ensure that the top activity in the stack is resumed.
1423 *
1424 * @param prev The previously resumed activity, for when in the process
1425 * of pausing; can be null to call from elsewhere.
1426 *
1427 * @return Returns true if something is being resumed, or false if
1428 * nothing happened.
1429 */
1430 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001431 return resumeTopActivityLocked(prev, null);
1432 }
1433
1434 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001435 // Find the first activity that is not finishing.
1436 ActivityRecord next = topRunningActivityLocked(null);
1437
1438 // Remember how we'll process this pause/resume situation, and ensure
1439 // that the state is reset however we wind up proceeding.
1440 final boolean userLeaving = mUserLeaving;
1441 mUserLeaving = false;
1442
1443 if (next == null) {
1444 // There are no more activities! Let's just start up the
1445 // Launcher...
1446 if (mMainStack) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001447 ActivityOptions.abort(options);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001448 return mService.startHomeActivityLocked(mCurrentUser);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001449 }
1450 }
1451
1452 next.delayedResume = false;
1453
1454 // If the top activity is the resumed one, nothing to do.
1455 if (mResumedActivity == next && next.state == ActivityState.RESUMED) {
1456 // Make sure we have executed any pending transitions, since there
1457 // should be nothing left to do at this point.
1458 mService.mWindowManager.executeAppTransition();
1459 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001460 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001461 return false;
1462 }
1463
1464 // If we are sleeping, and there is no resumed activity, and the top
1465 // activity is paused, well that is the state we want.
1466 if ((mService.mSleeping || mService.mShuttingDown)
p13451dbad2872012-04-18 11:39:23 +09001467 && mLastPausedActivity == next
1468 && (next.state == ActivityState.PAUSED
1469 || next.state == ActivityState.STOPPED
1470 || next.state == ActivityState.STOPPING)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001471 // Make sure we have executed any pending transitions, since there
1472 // should be nothing left to do at this point.
1473 mService.mWindowManager.executeAppTransition();
1474 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001475 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001476 return false;
1477 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001478
1479 // Make sure that the user who owns this activity is started. If not,
1480 // we will just leave it as is because someone should be bringing
1481 // another user's activities to the top of the stack.
1482 if (mService.mStartedUsers.get(next.userId) == null) {
1483 Slog.w(TAG, "Skipping resume of top activity " + next
1484 + ": user " + next.userId + " is stopped");
1485 return false;
1486 }
1487
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001488 // The activity may be waiting for stop, but that is no longer
1489 // appropriate for it.
1490 mStoppingActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001491 mGoingToSleepActivities.remove(next);
1492 next.sleeping = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001493 mWaitingVisibleActivities.remove(next);
1494
Dianne Hackborn84375872012-06-01 19:03:50 -07001495 next.updateOptionsLocked(options);
1496
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001497 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1498
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001499 // If we are currently pausing an activity, then don't do anything
1500 // until that is done.
1501 if (mPausingActivity != null) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07001502 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG,
1503 "Skip resume: pausing=" + mPausingActivity);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001504 return false;
1505 }
1506
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001507 // Okay we are now going to start a switch, to 'next'. We may first
1508 // have to pause the current activity, but this is an important point
1509 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001510 // XXX "App Redirected" dialog is getting too many false positives
1511 // at this point, so turn off for now.
1512 if (false) {
1513 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1514 long now = SystemClock.uptimeMillis();
1515 final boolean inTime = mLastStartedActivity.startTime != 0
1516 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1517 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1518 final int nextUid = next.info.applicationInfo.uid;
1519 if (inTime && lastUid != nextUid
1520 && lastUid != next.launchedFromUid
1521 && mService.checkPermission(
1522 android.Manifest.permission.STOP_APP_SWITCHES,
1523 -1, next.launchedFromUid)
1524 != PackageManager.PERMISSION_GRANTED) {
1525 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1526 } else {
1527 next.startTime = now;
1528 mLastStartedActivity = next;
1529 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001530 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001531 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001532 mLastStartedActivity = next;
1533 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001534 }
1535
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001536 // We need to start pausing the current activity so the top one
1537 // can be resumed...
1538 if (mResumedActivity != null) {
1539 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001540 // At this point we want to put the upcoming activity's process
1541 // at the top of the LRU list, since we know we will be needing it
1542 // very soon and it would be a waste to let it get killed if it
1543 // happens to be sitting towards the end.
1544 if (next.app != null && next.app.thread != null) {
1545 // No reason to do full oom adj update here; we'll let that
1546 // happen whenever it needs to later.
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001547 mService.updateLruProcessLocked(next.app, false);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001548 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001549 startPausingLocked(userLeaving, false);
1550 return true;
1551 }
1552
Christopher Tated3f175c2012-06-14 14:16:54 -07001553 // If the most recent activity was noHistory but was only stopped rather
1554 // than stopped+finished because the device went to sleep, we need to make
1555 // sure to finish it as we're making a new activity topmost.
1556 final ActivityRecord last = mLastPausedActivity;
1557 if (mService.mSleeping && last != null && !last.finishing) {
1558 if ((last.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1559 || (last.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
1560 if (DEBUG_STATES) {
1561 Slog.d(TAG, "no-history finish of " + last + " on new resume");
1562 }
1563 requestFinishActivityLocked(last.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001564 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07001565 }
1566 }
1567
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001568 if (prev != null && prev != next) {
1569 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1570 prev.waitingVisible = true;
1571 mWaitingVisibleActivities.add(prev);
1572 if (DEBUG_SWITCH) Slog.v(
1573 TAG, "Resuming top, waiting visible to hide: " + prev);
1574 } else {
1575 // The next activity is already visible, so hide the previous
1576 // activity's windows right now so we can show the new one ASAP.
1577 // We only do this if the previous is finishing, which should mean
1578 // it is on top of the one being resumed so hiding it quickly
1579 // is good. Otherwise, we want to do the normal route of allowing
1580 // the resumed activity to be shown so we can decide if the
1581 // previous should actually be hidden depending on whether the
1582 // new one is found to be full-screen or not.
1583 if (prev.finishing) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001584 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001585 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1586 + prev + ", waitingVisible="
1587 + (prev != null ? prev.waitingVisible : null)
1588 + ", nowVisible=" + next.nowVisible);
1589 } else {
1590 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1591 + prev + ", waitingVisible="
1592 + (prev != null ? prev.waitingVisible : null)
1593 + ", nowVisible=" + next.nowVisible);
1594 }
1595 }
1596 }
1597
Dianne Hackborne7f97212011-02-24 14:40:20 -08001598 // Launching this app's activity, make sure the app is no longer
1599 // considered stopped.
1600 try {
1601 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001602 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001603 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001604 } catch (IllegalArgumentException e) {
1605 Slog.w(TAG, "Failed trying to unstop package "
1606 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001607 }
1608
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001609 // We are starting up the next activity, so tell the window manager
1610 // that the previous one will be hidden soon. This way it can know
1611 // to ignore it when computing the desired screen orientation.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001612 boolean noAnim = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001613 if (prev != null) {
1614 if (prev.finishing) {
1615 if (DEBUG_TRANSITION) Slog.v(TAG,
1616 "Prepare close transition: prev=" + prev);
1617 if (mNoAnimActivities.contains(prev)) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001618 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001619 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001620 } else {
1621 mService.mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001622 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1623 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001624 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001625 mService.mWindowManager.setAppWillBeHidden(prev.appToken);
1626 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001627 } else {
1628 if (DEBUG_TRANSITION) Slog.v(TAG,
1629 "Prepare open transition: prev=" + prev);
1630 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001631 noAnim = true;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001632 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001633 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001634 } else {
1635 mService.mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001636 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1637 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001638 }
1639 }
1640 if (false) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001641 mService.mWindowManager.setAppWillBeHidden(prev.appToken);
1642 mService.mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001643 }
1644 } else if (mHistory.size() > 1) {
1645 if (DEBUG_TRANSITION) Slog.v(TAG,
1646 "Prepare open transition: no previous");
1647 if (mNoAnimActivities.contains(next)) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001648 noAnim = true;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001649 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001650 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001651 } else {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001652 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001653 AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001654 }
1655 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001656 if (!noAnim) {
1657 next.applyOptionsLocked();
1658 } else {
1659 next.clearOptionsLocked();
1660 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001661
1662 if (next.app != null && next.app.thread != null) {
1663 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1664
1665 // This activity is now becoming visible.
Dianne Hackbornbe707852011-11-11 14:32:10 -08001666 mService.mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001667
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001668 // schedule launch ticks to collect information about slow apps.
1669 next.startLaunchTickingLocked();
1670
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001671 ActivityRecord lastResumedActivity = mResumedActivity;
1672 ActivityState lastState = next.state;
1673
1674 mService.updateCpuStats();
1675
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001676 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001677 next.state = ActivityState.RESUMED;
1678 mResumedActivity = next;
1679 next.task.touchActiveTime();
Dianne Hackborn88819b22010-12-21 18:18:02 -08001680 if (mMainStack) {
1681 mService.addRecentTaskLocked(next.task);
1682 }
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001683 mService.updateLruProcessLocked(next.app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001684 updateLRUListLocked(next);
1685
1686 // Have the window manager re-evaluate the orientation of
1687 // the screen based on the new activity order.
1688 boolean updated = false;
1689 if (mMainStack) {
1690 synchronized (mService) {
1691 Configuration config = mService.mWindowManager.updateOrientationFromAppTokens(
1692 mService.mConfiguration,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001693 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001694 if (config != null) {
1695 next.frozenBeforeDestroy = true;
1696 }
Dianne Hackborn813075a62011-11-14 17:45:19 -08001697 updated = mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001698 }
1699 }
1700 if (!updated) {
1701 // The configuration update wasn't able to keep the existing
1702 // instance of the activity, and instead started a new one.
1703 // We should be all done, but let's just make sure our activity
1704 // is still at the top and schedule another run if something
1705 // weird happened.
1706 ActivityRecord nextNext = topRunningActivityLocked(null);
1707 if (DEBUG_SWITCH) Slog.i(TAG,
1708 "Activity config changed during resume: " + next
1709 + ", new next: " + nextNext);
1710 if (nextNext != next) {
1711 // Do over!
1712 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
1713 }
1714 if (mMainStack) {
1715 mService.setFocusedActivityLocked(next);
1716 }
1717 ensureActivitiesVisibleLocked(null, 0);
1718 mService.mWindowManager.executeAppTransition();
1719 mNoAnimActivities.clear();
1720 return true;
1721 }
1722
1723 try {
1724 // Deliver all pending results.
1725 ArrayList a = next.results;
1726 if (a != null) {
1727 final int N = a.size();
1728 if (!next.finishing && N > 0) {
1729 if (DEBUG_RESULTS) Slog.v(
1730 TAG, "Delivering results to " + next
1731 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001732 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001733 }
1734 }
1735
1736 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001737 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001738 }
1739
1740 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001741 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001742 next.task.taskId, next.shortComponentName);
1743
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001744 next.sleeping = false;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07001745 showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001746 next.app.pendingUiClean = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001747 next.app.thread.scheduleResumeActivity(next.appToken,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001748 mService.isNextTransitionForward());
1749
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001750 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001751
1752 } catch (Exception e) {
1753 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001754 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1755 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001756 next.state = lastState;
1757 mResumedActivity = lastResumedActivity;
1758 Slog.i(TAG, "Restarting because process died: " + next);
1759 if (!next.hasBeenLaunched) {
1760 next.hasBeenLaunched = true;
1761 } else {
1762 if (SHOW_APP_STARTING_PREVIEW && mMainStack) {
1763 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001764 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001765 mService.compatibilityInfoForPackageLocked(
1766 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001767 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001768 next.labelRes, next.icon, next.windowFlags,
1769 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001770 }
1771 }
1772 startSpecificActivityLocked(next, true, false);
1773 return true;
1774 }
1775
1776 // From this point on, if something goes wrong there is no way
1777 // to recover the activity.
1778 try {
1779 next.visible = true;
1780 completeResumeLocked(next);
1781 } catch (Exception e) {
1782 // If any exception gets thrown, toss away this
1783 // activity and try the next one.
1784 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001785 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001786 "resume-exception", true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001787 return true;
1788 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001789 next.stopped = false;
1790
1791 } else {
1792 // Whoops, need to restart this activity!
1793 if (!next.hasBeenLaunched) {
1794 next.hasBeenLaunched = true;
1795 } else {
1796 if (SHOW_APP_STARTING_PREVIEW) {
1797 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001798 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001799 mService.compatibilityInfoForPackageLocked(
1800 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001801 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001802 next.labelRes, next.icon, next.windowFlags,
1803 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001804 }
1805 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1806 }
1807 startSpecificActivityLocked(next, true, true);
1808 }
1809
1810 return true;
1811 }
1812
1813 private final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001814 boolean doResume, boolean keepCurTransition, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001815 final int NH = mHistory.size();
1816
1817 int addPos = -1;
1818
1819 if (!newTask) {
1820 // If starting in an existing task, find where that is...
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001821 boolean startIt = true;
1822 for (int i = NH-1; i >= 0; i--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001823 ActivityRecord p = mHistory.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001824 if (p.finishing) {
1825 continue;
1826 }
1827 if (p.task == r.task) {
1828 // Here it is! Now, if this is not yet visible to the
1829 // user, then just add it without starting; it will
1830 // get started when the user navigates back to it.
1831 addPos = i+1;
1832 if (!startIt) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07001833 if (DEBUG_ADD_REMOVE) {
1834 RuntimeException here = new RuntimeException("here");
1835 here.fillInStackTrace();
1836 Slog.i(TAG, "Adding activity " + r + " to stack at " + addPos,
1837 here);
1838 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001839 mHistory.add(addPos, r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001840 r.putInHistory();
Dianne Hackbornbe707852011-11-11 14:32:10 -08001841 mService.mWindowManager.addAppToken(addPos, r.appToken, r.task.taskId,
Craig Mautner5962b122012-10-05 14:45:52 -07001842 r.info.screenOrientation, r.fullscreen,
1843 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001844 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001845 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001846 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001847 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001848 return;
1849 }
1850 break;
1851 }
1852 if (p.fullscreen) {
1853 startIt = false;
1854 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001855 }
1856 }
1857
1858 // Place a new activity at top of stack, so it is next to interact
1859 // with the user.
1860 if (addPos < 0) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001861 addPos = NH;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001862 }
1863
1864 // If we are not placing the new activity frontmost, we do not want
1865 // to deliver the onUserLeaving callback to the actual frontmost
1866 // activity
1867 if (addPos < NH) {
1868 mUserLeaving = false;
1869 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() behind front, mUserLeaving=false");
1870 }
1871
1872 // Slot the activity into the history stack and proceed
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07001873 if (DEBUG_ADD_REMOVE) {
1874 RuntimeException here = new RuntimeException("here");
1875 here.fillInStackTrace();
1876 Slog.i(TAG, "Adding activity " + r + " to stack at " + addPos, here);
1877 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001878 mHistory.add(addPos, r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001879 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001880 r.frontOfTask = newTask;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001881 if (NH > 0) {
1882 // We want to show the starting preview window if we are
1883 // switching to a new task, or the next activity's process is
1884 // not currently running.
1885 boolean showStartingIcon = newTask;
1886 ProcessRecord proc = r.app;
1887 if (proc == null) {
1888 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1889 }
1890 if (proc == null || proc.thread == null) {
1891 showStartingIcon = true;
1892 }
1893 if (DEBUG_TRANSITION) Slog.v(TAG,
1894 "Prepare open transition: starting " + r);
1895 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08001896 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08001897 AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001898 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001899 } else {
1900 mService.mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001901 ? AppTransition.TRANSIT_TASK_OPEN
1902 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001903 mNoAnimActivities.remove(r);
1904 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001905 r.updateOptionsLocked(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001906 mService.mWindowManager.addAppToken(
Craig Mautner5962b122012-10-05 14:45:52 -07001907 addPos, r.appToken, r.task.taskId, r.info.screenOrientation, r.fullscreen,
1908 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001909 boolean doShow = true;
1910 if (newTask) {
1911 // Even though this activity is starting fresh, we still need
1912 // to reset it to make sure we apply affinities to move any
1913 // existing activities from other tasks in to it.
1914 // If the caller has requested that the target task be
1915 // reset, then do so.
1916 if ((r.intent.getFlags()
1917 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1918 resetTaskIfNeededLocked(r, r);
1919 doShow = topRunningNonDelayedActivityLocked(null) == r;
1920 }
1921 }
1922 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1923 // Figure out if we are transitioning from another activity that is
1924 // "has the same starting icon" as the next one. This allows the
1925 // window manager to keep the previous window it had previously
1926 // created, if it still had one.
1927 ActivityRecord prev = mResumedActivity;
1928 if (prev != null) {
1929 // We don't want to reuse the previous starting preview if:
1930 // (1) The current activity is in a different task.
1931 if (prev.task != r.task) prev = null;
1932 // (2) The current activity is already displayed.
1933 else if (prev.nowVisible) prev = null;
1934 }
1935 mService.mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001936 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001937 mService.compatibilityInfoForPackageLocked(
1938 r.info.applicationInfo), r.nonLocalizedLabel,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001939 r.labelRes, r.icon, r.windowFlags,
1940 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001941 }
1942 } else {
1943 // If this is the first activity, don't do any fancy animations,
1944 // because there is nothing for it to animate on top of.
Dianne Hackbornbe707852011-11-11 14:32:10 -08001945 mService.mWindowManager.addAppToken(addPos, r.appToken, r.task.taskId,
Craig Mautner5962b122012-10-05 14:45:52 -07001946 r.info.screenOrientation, r.fullscreen,
1947 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001948 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001949 }
1950 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001951 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001952 }
1953
1954 if (doResume) {
1955 resumeTopActivityLocked(null);
1956 }
1957 }
1958
Dianne Hackbornbe707852011-11-11 14:32:10 -08001959 final void validateAppTokensLocked() {
1960 mValidateAppTokens.clear();
1961 mValidateAppTokens.ensureCapacity(mHistory.size());
1962 for (int i=0; i<mHistory.size(); i++) {
1963 mValidateAppTokens.add(mHistory.get(i).appToken);
1964 }
1965 mService.mWindowManager.validateAppTokens(mValidateAppTokens);
1966 }
1967
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001968 /**
1969 * Perform a reset of the given task, if needed as part of launching it.
1970 * Returns the new HistoryRecord at the top of the task.
1971 */
1972 private final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
1973 ActivityRecord newActivity) {
1974 boolean forceReset = (newActivity.info.flags
1975 &ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001976 if (ACTIVITY_INACTIVE_RESET_TIME > 0
1977 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001978 if ((newActivity.info.flags
1979 &ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
1980 forceReset = true;
1981 }
1982 }
1983
1984 final TaskRecord task = taskTop.task;
1985
1986 // We are going to move through the history list so that we can look
1987 // at each activity 'target' with 'below' either the interesting
1988 // activity immediately below it in the stack or null.
1989 ActivityRecord target = null;
1990 int targetI = 0;
1991 int taskTopI = -1;
1992 int replyChainEnd = -1;
1993 int lastReparentPos = -1;
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001994 ActivityOptions topOptions = null;
1995 boolean canMoveOptions = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001996 for (int i=mHistory.size()-1; i>=-1; i--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001997 ActivityRecord below = i >= 0 ? mHistory.get(i) : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001998
1999 if (below != null && below.finishing) {
2000 continue;
2001 }
Amith Yamasani04e0d262012-02-14 11:50:53 -08002002 // Don't check any lower in the stack if we're crossing a user boundary.
2003 if (below != null && below.userId != taskTop.userId) {
2004 break;
2005 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002006 if (target == null) {
2007 target = below;
2008 targetI = i;
2009 // If we were in the middle of a reply chain before this
2010 // task, it doesn't appear like the root of the chain wants
2011 // anything interesting, so drop it.
2012 replyChainEnd = -1;
2013 continue;
2014 }
2015
2016 final int flags = target.info.flags;
2017
2018 final boolean finishOnTaskLaunch =
2019 (flags&ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2020 final boolean allowTaskReparenting =
2021 (flags&ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2022
2023 if (target.task == task) {
2024 // We are inside of the task being reset... we'll either
2025 // finish this activity, push it out for another task,
2026 // or leave it as-is. We only do this
2027 // for activities that are not the root of the task (since
2028 // if we finish the root, we may no longer have the task!).
2029 if (taskTopI < 0) {
2030 taskTopI = targetI;
2031 }
2032 if (below != null && below.task == task) {
2033 final boolean clearWhenTaskReset =
2034 (target.intent.getFlags()
2035 &Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2036 if (!finishOnTaskLaunch && !clearWhenTaskReset && target.resultTo != null) {
2037 // If this activity is sending a reply to a previous
2038 // activity, we can't do anything with it now until
2039 // we reach the start of the reply chain.
2040 // XXX note that we are assuming the result is always
2041 // to the previous activity, which is almost always
2042 // the case but we really shouldn't count on.
2043 if (replyChainEnd < 0) {
2044 replyChainEnd = targetI;
2045 }
2046 } else if (!finishOnTaskLaunch && !clearWhenTaskReset && allowTaskReparenting
2047 && target.taskAffinity != null
2048 && !target.taskAffinity.equals(task.affinity)) {
2049 // If this activity has an affinity for another
2050 // task, then we need to move it out of here. We will
2051 // move it as far out of the way as possible, to the
2052 // bottom of the activity stack. This also keeps it
2053 // correctly ordered with any activities we previously
2054 // moved.
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002055 ActivityRecord p = mHistory.get(0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002056 if (target.taskAffinity != null
2057 && target.taskAffinity.equals(p.task.affinity)) {
2058 // If the activity currently at the bottom has the
2059 // same task affinity as the one we are moving,
2060 // then merge it into the same task.
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002061 target.setTask(p.task, p.thumbHolder, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002062 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2063 + " out to bottom task " + p.task);
2064 } else {
2065 mService.mCurTask++;
2066 if (mService.mCurTask <= 0) {
2067 mService.mCurTask = 1;
2068 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002069 target.setTask(new TaskRecord(mService.mCurTask, target.info, null),
2070 null, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002071 target.task.affinityIntent = target.intent;
2072 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2073 + " out to new task " + target.task);
2074 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002075 mService.mWindowManager.setAppGroupId(target.appToken, task.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002076 if (replyChainEnd < 0) {
2077 replyChainEnd = targetI;
2078 }
2079 int dstPos = 0;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002080 ThumbnailHolder curThumbHolder = target.thumbHolder;
Dianne Hackborn9622ca42012-10-23 18:56:33 -07002081 boolean gotOptions = !canMoveOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002082 for (int srcPos=targetI; srcPos<=replyChainEnd; srcPos++) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002083 p = mHistory.get(srcPos);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002084 if (p.finishing) {
2085 continue;
2086 }
2087 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2088 + " out to target's task " + target.task);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002089 p.setTask(target.task, curThumbHolder, false);
2090 curThumbHolder = p.thumbHolder;
Dianne Hackborn9622ca42012-10-23 18:56:33 -07002091 canMoveOptions = false;
2092 if (!gotOptions && topOptions == null) {
2093 topOptions = p.takeOptionsLocked();
2094 if (topOptions != null) {
2095 gotOptions = true;
2096 }
2097 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07002098 if (DEBUG_ADD_REMOVE) {
2099 RuntimeException here = new RuntimeException("here");
2100 here.fillInStackTrace();
2101 Slog.i(TAG, "Removing and adding activity " + p + " to stack at "
2102 + dstPos, here);
2103 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002104 mHistory.remove(srcPos);
2105 mHistory.add(dstPos, p);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002106 mService.mWindowManager.moveAppToken(dstPos, p.appToken);
2107 mService.mWindowManager.setAppGroupId(p.appToken, p.task.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002108 dstPos++;
2109 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002110 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002111 }
2112 i++;
2113 }
2114 if (taskTop == p) {
2115 taskTop = below;
2116 }
2117 if (taskTopI == replyChainEnd) {
2118 taskTopI = -1;
2119 }
2120 replyChainEnd = -1;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002121 } else if (forceReset || finishOnTaskLaunch
2122 || clearWhenTaskReset) {
2123 // If the activity should just be removed -- either
2124 // because it asks for it, or the task should be
2125 // cleared -- then finish it and anything that is
2126 // part of its reply chain.
2127 if (clearWhenTaskReset) {
2128 // In this case, we want to finish this activity
2129 // and everything above it, so be sneaky and pretend
2130 // like these are all in the reply chain.
2131 replyChainEnd = targetI+1;
2132 while (replyChainEnd < mHistory.size() &&
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002133 (mHistory.get(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002134 replyChainEnd)).task == task) {
2135 replyChainEnd++;
2136 }
2137 replyChainEnd--;
2138 } else if (replyChainEnd < 0) {
2139 replyChainEnd = targetI;
2140 }
2141 ActivityRecord p = null;
Dianne Hackborn9622ca42012-10-23 18:56:33 -07002142 boolean gotOptions = !canMoveOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002143 for (int srcPos=targetI; srcPos<=replyChainEnd; srcPos++) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002144 p = mHistory.get(srcPos);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002145 if (p.finishing) {
2146 continue;
2147 }
Dianne Hackborn9622ca42012-10-23 18:56:33 -07002148 canMoveOptions = false;
2149 if (!gotOptions && topOptions == null) {
2150 topOptions = p.takeOptionsLocked();
2151 if (topOptions != null) {
2152 gotOptions = true;
2153 }
2154 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002155 if (finishActivityLocked(p, srcPos,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002156 Activity.RESULT_CANCELED, null, "reset", false)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002157 replyChainEnd--;
2158 srcPos--;
2159 }
2160 }
2161 if (taskTop == p) {
2162 taskTop = below;
2163 }
2164 if (taskTopI == replyChainEnd) {
2165 taskTopI = -1;
2166 }
2167 replyChainEnd = -1;
2168 } else {
2169 // If we were in the middle of a chain, well the
2170 // activity that started it all doesn't want anything
2171 // special, so leave it all as-is.
2172 replyChainEnd = -1;
2173 }
2174 } else {
2175 // Reached the bottom of the task -- any reply chain
2176 // should be left as-is.
2177 replyChainEnd = -1;
2178 }
Dianne Hackbornae0a0a82011-12-07 14:03:01 -08002179
2180 } else if (target.resultTo != null && (below == null
2181 || below.task == target.task)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002182 // If this activity is sending a reply to a previous
2183 // activity, we can't do anything with it now until
2184 // we reach the start of the reply chain.
2185 // XXX note that we are assuming the result is always
2186 // to the previous activity, which is almost always
2187 // the case but we really shouldn't count on.
2188 if (replyChainEnd < 0) {
2189 replyChainEnd = targetI;
2190 }
2191
2192 } else if (taskTopI >= 0 && allowTaskReparenting
2193 && task.affinity != null
2194 && task.affinity.equals(target.taskAffinity)) {
2195 // We are inside of another task... if this activity has
2196 // an affinity for our task, then either remove it if we are
2197 // clearing or move it over to our task. Note that
2198 // we currently punt on the case where we are resetting a
2199 // task that is not at the top but who has activities above
2200 // with an affinity to it... this is really not a normal
2201 // case, and we will need to later pull that task to the front
2202 // and usually at that point we will do the reset and pick
2203 // up those remaining activities. (This only happens if
2204 // someone starts an activity in a new task from an activity
2205 // in a task that is not currently on top.)
2206 if (forceReset || finishOnTaskLaunch) {
2207 if (replyChainEnd < 0) {
2208 replyChainEnd = targetI;
2209 }
2210 ActivityRecord p = null;
Dianne Hackbornae0a0a82011-12-07 14:03:01 -08002211 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index "
2212 + targetI + " to " + replyChainEnd);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002213 for (int srcPos=targetI; srcPos<=replyChainEnd; srcPos++) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002214 p = mHistory.get(srcPos);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002215 if (p.finishing) {
2216 continue;
2217 }
2218 if (finishActivityLocked(p, srcPos,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002219 Activity.RESULT_CANCELED, null, "reset", false)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002220 taskTopI--;
2221 lastReparentPos--;
2222 replyChainEnd--;
2223 srcPos--;
2224 }
2225 }
2226 replyChainEnd = -1;
2227 } else {
2228 if (replyChainEnd < 0) {
2229 replyChainEnd = targetI;
2230 }
Dianne Hackbornae0a0a82011-12-07 14:03:01 -08002231 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting task at index "
2232 + targetI + " to " + replyChainEnd);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002233 for (int srcPos=replyChainEnd; srcPos>=targetI; srcPos--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002234 ActivityRecord p = mHistory.get(srcPos);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002235 if (p.finishing) {
2236 continue;
2237 }
2238 if (lastReparentPos < 0) {
2239 lastReparentPos = taskTopI;
2240 taskTop = p;
2241 } else {
2242 lastReparentPos--;
2243 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07002244 if (DEBUG_ADD_REMOVE) {
2245 RuntimeException here = new RuntimeException("here");
2246 here.fillInStackTrace();
2247 Slog.i(TAG, "Removing and adding activity " + p + " to stack at "
2248 + lastReparentPos, here);
2249 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002250 mHistory.remove(srcPos);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002251 p.setTask(task, null, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002252 mHistory.add(lastReparentPos, p);
2253 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p
Dianne Hackbornae0a0a82011-12-07 14:03:01 -08002254 + " from " + srcPos + " to " + lastReparentPos
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002255 + " in to resetting task " + task);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002256 mService.mWindowManager.moveAppToken(lastReparentPos, p.appToken);
2257 mService.mWindowManager.setAppGroupId(p.appToken, p.task.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002258 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002259 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002260 }
2261 }
2262 replyChainEnd = -1;
2263
2264 // Now we've moved it in to place... but what if this is
2265 // a singleTop activity and we have put it on top of another
2266 // instance of the same activity? Then we drop the instance
2267 // below so it remains singleTop.
2268 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2269 for (int j=lastReparentPos-1; j>=0; j--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002270 ActivityRecord p = mHistory.get(j);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002271 if (p.finishing) {
2272 continue;
2273 }
2274 if (p.intent.getComponent().equals(target.intent.getComponent())) {
2275 if (finishActivityLocked(p, j,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002276 Activity.RESULT_CANCELED, null, "replace", false)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002277 taskTopI--;
2278 lastReparentPos--;
2279 }
2280 }
2281 }
2282 }
2283 }
Dianne Hackbornae0a0a82011-12-07 14:03:01 -08002284
2285 } else if (below != null && below.task != target.task) {
2286 // We hit the botton of a task; the reply chain can't
2287 // pass through it.
2288 replyChainEnd = -1;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002289 }
2290
2291 target = below;
2292 targetI = i;
2293 }
Dianne Hackborn9622ca42012-10-23 18:56:33 -07002294
2295 if (topOptions != null) {
2296 // If we got some ActivityOptions from an activity on top that
2297 // was removed from the task, propagate them to the new real top.
2298 if (taskTop != null) {
2299 taskTop.updateOptionsLocked(topOptions);
2300 } else {
2301 topOptions.abort();
2302 }
2303 }
2304
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002305 return taskTop;
2306 }
2307
2308 /**
2309 * Perform clear operation as requested by
2310 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
2311 * stack to the given task, then look for
2312 * an instance of that activity in the stack and, if found, finish all
2313 * activities on top of it and return the instance.
2314 *
2315 * @param newR Description of the new activity being started.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002316 * @return Returns the old activity that should be continued to be used,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002317 * or null if none was found.
2318 */
2319 private final ActivityRecord performClearTaskLocked(int taskId,
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002320 ActivityRecord newR, int launchFlags) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002321 int i = mHistory.size();
2322
2323 // First find the requested task.
2324 while (i > 0) {
2325 i--;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002326 ActivityRecord r = mHistory.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002327 if (r.task.taskId == taskId) {
2328 i++;
2329 break;
2330 }
2331 }
2332
2333 // Now clear it.
2334 while (i > 0) {
2335 i--;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002336 ActivityRecord r = mHistory.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002337 if (r.finishing) {
2338 continue;
2339 }
2340 if (r.task.taskId != taskId) {
2341 return null;
2342 }
2343 if (r.realActivity.equals(newR.realActivity)) {
2344 // Here it is! Now finish everything in front...
2345 ActivityRecord ret = r;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002346 while (i < (mHistory.size()-1)) {
2347 i++;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002348 r = mHistory.get(i);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002349 if (r.task.taskId != taskId) {
2350 break;
2351 }
2352 if (r.finishing) {
2353 continue;
2354 }
Dianne Hackborn9622ca42012-10-23 18:56:33 -07002355 ActivityOptions opts = r.takeOptionsLocked();
2356 if (opts != null) {
2357 ret.updateOptionsLocked(opts);
2358 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002359 if (finishActivityLocked(r, i, Activity.RESULT_CANCELED,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002360 null, "clear", false)) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002361 i--;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002362 }
2363 }
2364
2365 // Finally, if this is a normal launch mode (that is, not
2366 // expecting onNewIntent()), then we will finish the current
2367 // instance of the activity so a new fresh one can be started.
2368 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
2369 && (launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0) {
2370 if (!ret.finishing) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002371 int index = indexOfTokenLocked(ret.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002372 if (index >= 0) {
2373 finishActivityLocked(ret, index, Activity.RESULT_CANCELED,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002374 null, "clear", false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002375 }
2376 return null;
2377 }
2378 }
2379
2380 return ret;
2381 }
2382 }
2383
2384 return null;
2385 }
2386
2387 /**
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002388 * Completely remove all activities associated with an existing
2389 * task starting at a specified index.
2390 */
2391 private final void performClearTaskAtIndexLocked(int taskId, int i) {
Dianne Hackborneabd3282011-10-13 16:26:49 -07002392 while (i < mHistory.size()) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002393 ActivityRecord r = mHistory.get(i);
2394 if (r.task.taskId != taskId) {
2395 // Whoops hit the end.
2396 return;
2397 }
2398 if (r.finishing) {
2399 i++;
2400 continue;
2401 }
2402 if (!finishActivityLocked(r, i, Activity.RESULT_CANCELED,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002403 null, "clear", false)) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002404 i++;
2405 }
2406 }
2407 }
2408
2409 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002410 * Completely remove all activities associated with an existing task.
2411 */
2412 private final void performClearTaskLocked(int taskId) {
2413 int i = mHistory.size();
2414
2415 // First find the requested task.
2416 while (i > 0) {
2417 i--;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002418 ActivityRecord r = mHistory.get(i);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002419 if (r.task.taskId == taskId) {
2420 i++;
2421 break;
2422 }
2423 }
2424
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002425 // Now find the start and clear it.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002426 while (i > 0) {
2427 i--;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002428 ActivityRecord r = mHistory.get(i);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002429 if (r.finishing) {
2430 continue;
2431 }
2432 if (r.task.taskId != taskId) {
2433 // We hit the bottom. Now finish it all...
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002434 performClearTaskAtIndexLocked(taskId, i+1);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002435 return;
2436 }
2437 }
2438 }
2439
2440 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002441 * Find the activity in the history stack within the given task. Returns
2442 * the index within the history at which it's found, or < 0 if not found.
2443 */
2444 private final int findActivityInHistoryLocked(ActivityRecord r, int task) {
2445 int i = mHistory.size();
2446 while (i > 0) {
2447 i--;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002448 ActivityRecord candidate = mHistory.get(i);
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07002449 if (candidate.finishing) {
2450 continue;
2451 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002452 if (candidate.task.taskId != task) {
2453 break;
2454 }
2455 if (candidate.realActivity.equals(r.realActivity)) {
2456 return i;
2457 }
2458 }
2459
2460 return -1;
2461 }
2462
2463 /**
2464 * Reorder the history stack so that the activity at the given index is
2465 * brought to the front.
2466 */
2467 private final ActivityRecord moveActivityToFrontLocked(int where) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002468 ActivityRecord newTop = mHistory.remove(where);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002469 int top = mHistory.size();
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002470 ActivityRecord oldTop = mHistory.get(top-1);
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07002471 if (DEBUG_ADD_REMOVE) {
2472 RuntimeException here = new RuntimeException("here");
2473 here.fillInStackTrace();
2474 Slog.i(TAG, "Removing and adding activity " + newTop + " to stack at "
2475 + top, here);
2476 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002477 mHistory.add(top, newTop);
2478 oldTop.frontOfTask = false;
2479 newTop.frontOfTask = true;
2480 return newTop;
2481 }
2482
2483 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002484 Intent intent, String resolvedType, ActivityInfo aInfo, IBinder resultTo,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002485 String resultWho, int requestCode,
Dianne Hackbornf265ea92013-01-31 15:00:51 -08002486 int callingPid, int callingUid, String callingPackage, int startFlags, Bundle options,
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002487 boolean componentSpecified, ActivityRecord[] outActivity) {
Dianne Hackbornefb58102010-10-14 16:47:34 -07002488
Dianne Hackborna4972e92012-03-14 10:38:05 -07002489 int err = ActivityManager.START_SUCCESS;
Dianne Hackbornefb58102010-10-14 16:47:34 -07002490
2491 ProcessRecord callerApp = null;
2492 if (caller != null) {
2493 callerApp = mService.getRecordForAppLocked(caller);
2494 if (callerApp != null) {
2495 callingPid = callerApp.pid;
2496 callingUid = callerApp.info.uid;
2497 } else {
2498 Slog.w(TAG, "Unable to find app for caller " + caller
2499 + " (pid=" + callingPid + ") when starting: "
2500 + intent.toString());
Dianne Hackborna4972e92012-03-14 10:38:05 -07002501 err = ActivityManager.START_PERMISSION_DENIED;
Dianne Hackbornefb58102010-10-14 16:47:34 -07002502 }
2503 }
2504
Dianne Hackborna4972e92012-03-14 10:38:05 -07002505 if (err == ActivityManager.START_SUCCESS) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002506 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -07002507 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
2508 + "} from pid " + (callerApp != null ? callerApp.pid : callingPid));
Dianne Hackbornefb58102010-10-14 16:47:34 -07002509 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002510
2511 ActivityRecord sourceRecord = null;
2512 ActivityRecord resultRecord = null;
2513 if (resultTo != null) {
2514 int index = indexOfTokenLocked(resultTo);
2515 if (DEBUG_RESULTS) Slog.v(
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07002516 TAG, "Will send result to " + resultTo + " (index " + index + ")");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002517 if (index >= 0) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002518 sourceRecord = mHistory.get(index);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002519 if (requestCode >= 0 && !sourceRecord.finishing) {
2520 resultRecord = sourceRecord;
2521 }
2522 }
2523 }
2524
2525 int launchFlags = intent.getFlags();
2526
2527 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
2528 && sourceRecord != null) {
2529 // Transfer the result target from the source activity to the new
2530 // one being started, including any failures.
2531 if (requestCode >= 0) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002532 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002533 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002534 }
2535 resultRecord = sourceRecord.resultTo;
2536 resultWho = sourceRecord.resultWho;
2537 requestCode = sourceRecord.requestCode;
2538 sourceRecord.resultTo = null;
2539 if (resultRecord != null) {
2540 resultRecord.removeResultsLocked(
2541 sourceRecord, resultWho, requestCode);
2542 }
2543 }
2544
Dianne Hackborna4972e92012-03-14 10:38:05 -07002545 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002546 // We couldn't find a class that can handle the given Intent.
2547 // That's the end of that!
Dianne Hackborna4972e92012-03-14 10:38:05 -07002548 err = ActivityManager.START_INTENT_NOT_RESOLVED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002549 }
2550
Dianne Hackborna4972e92012-03-14 10:38:05 -07002551 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002552 // We couldn't find the specific class specified in the Intent.
2553 // Also the end of the line.
Dianne Hackborna4972e92012-03-14 10:38:05 -07002554 err = ActivityManager.START_CLASS_NOT_FOUND;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002555 }
2556
Dianne Hackborna4972e92012-03-14 10:38:05 -07002557 if (err != ActivityManager.START_SUCCESS) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002558 if (resultRecord != null) {
2559 sendActivityResultLocked(-1,
2560 resultRecord, resultWho, requestCode,
2561 Activity.RESULT_CANCELED, null);
2562 }
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002563 mDismissKeyguardOnNextActivity = false;
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002564 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002565 return err;
2566 }
2567
Jeff Sharkey35be7562012-04-18 19:16:15 -07002568 final int startAnyPerm = mService.checkPermission(
2569 START_ANY_ACTIVITY, callingPid, callingUid);
2570 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08002571 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
Jeff Sharkey35be7562012-04-18 19:16:15 -07002572 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002573 if (resultRecord != null) {
2574 sendActivityResultLocked(-1,
2575 resultRecord, resultWho, requestCode,
2576 Activity.RESULT_CANCELED, null);
2577 }
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002578 mDismissKeyguardOnNextActivity = false;
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08002579 String msg;
2580 if (!aInfo.exported) {
2581 msg = "Permission Denial: starting " + intent.toString()
2582 + " from " + callerApp + " (pid=" + callingPid
2583 + ", uid=" + callingUid + ")"
2584 + " not exported from uid " + aInfo.applicationInfo.uid;
2585 } else {
2586 msg = "Permission Denial: starting " + intent.toString()
2587 + " from " + callerApp + " (pid=" + callingPid
2588 + ", uid=" + callingUid + ")"
2589 + " requires " + aInfo.permission;
2590 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002591 Slog.w(TAG, msg);
2592 throw new SecurityException(msg);
2593 }
2594
2595 if (mMainStack) {
2596 if (mService.mController != null) {
2597 boolean abort = false;
2598 try {
2599 // The Intent we give to the watcher has the extra data
2600 // stripped off, since it can contain private information.
2601 Intent watchIntent = intent.cloneFilter();
2602 abort = !mService.mController.activityStarting(watchIntent,
2603 aInfo.applicationInfo.packageName);
2604 } catch (RemoteException e) {
2605 mService.mController = null;
2606 }
2607
2608 if (abort) {
2609 if (resultRecord != null) {
2610 sendActivityResultLocked(-1,
2611 resultRecord, resultWho, requestCode,
2612 Activity.RESULT_CANCELED, null);
2613 }
2614 // We pretend to the caller that it was really started, but
2615 // they will just get a cancel result.
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002616 mDismissKeyguardOnNextActivity = false;
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002617 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002618 return ActivityManager.START_SUCCESS;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002619 }
2620 }
2621 }
Amith Yamasani742a6712011-05-04 14:49:28 -07002622
Dianne Hackbornf265ea92013-01-31 15:00:51 -08002623 ActivityRecord r = new ActivityRecord(mService, this, callerApp, callingUid, callingPackage,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002624 intent, resolvedType, aInfo, mService.mConfiguration,
2625 resultRecord, resultWho, requestCode, componentSpecified);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002626 if (outActivity != null) {
2627 outActivity[0] = r;
2628 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002629
2630 if (mMainStack) {
2631 if (mResumedActivity == null
2632 || mResumedActivity.info.applicationInfo.uid != callingUid) {
2633 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) {
2634 PendingActivityLaunch pal = new PendingActivityLaunch();
2635 pal.r = r;
2636 pal.sourceRecord = sourceRecord;
Dianne Hackborna4972e92012-03-14 10:38:05 -07002637 pal.startFlags = startFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002638 mService.mPendingActivityLaunches.add(pal);
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002639 mDismissKeyguardOnNextActivity = false;
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002640 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002641 return ActivityManager.START_SWITCHES_CANCELED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002642 }
2643 }
2644
2645 if (mService.mDidAppSwitch) {
2646 // This is the second allowed switch since we stopped switches,
2647 // so now just generally allow switches. Use case: user presses
2648 // home (switches disabled, switch to home, mDidAppSwitch now true);
2649 // user taps a home icon (coming from home so allowed, we hit here
2650 // and now allow anyone to switch again).
2651 mService.mAppSwitchesAllowedTime = 0;
2652 } else {
2653 mService.mDidAppSwitch = true;
2654 }
2655
2656 mService.doPendingActivityLaunchesLocked(false);
2657 }
2658
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002659 err = startActivityUncheckedLocked(r, sourceRecord,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002660 startFlags, true, options);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002661 if (mDismissKeyguardOnNextActivity && mPausingActivity == null) {
Dianne Hackborn90c52de2011-09-23 12:57:44 -07002662 // Someone asked to have the keyguard dismissed on the next
2663 // activity start, but we are not actually doing an activity
2664 // switch... just dismiss the keyguard now, because we
2665 // probably want to see whatever is behind it.
2666 mDismissKeyguardOnNextActivity = false;
2667 mService.mWindowManager.dismissKeyguard();
2668 }
2669 return err;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002670 }
2671
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002672 final void moveHomeToFrontFromLaunchLocked(int launchFlags) {
2673 if ((launchFlags &
2674 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
2675 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
2676 // Caller wants to appear on home activity, so before starting
2677 // their own activity we will bring home to the front.
2678 moveHomeToFrontLocked();
2679 }
2680 }
2681
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002682 final int startActivityUncheckedLocked(ActivityRecord r,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002683 ActivityRecord sourceRecord, int startFlags, boolean doResume,
2684 Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002685 final Intent intent = r.intent;
2686 final int callingUid = r.launchedFromUid;
Amith Yamasani742a6712011-05-04 14:49:28 -07002687
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002688 int launchFlags = intent.getFlags();
2689
2690 // We'll invoke onUserLeaving before onPause only if the launching
2691 // activity did not explicitly state that this is an automated launch.
2692 mUserLeaving = (launchFlags&Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
2693 if (DEBUG_USER_LEAVING) Slog.v(TAG,
2694 "startActivity() => mUserLeaving=" + mUserLeaving);
2695
2696 // If the caller has asked not to resume at this point, we make note
2697 // of this in the record so that we can skip it when trying to find
2698 // the top running activity.
2699 if (!doResume) {
2700 r.delayedResume = true;
2701 }
2702
2703 ActivityRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)
2704 != 0 ? r : null;
2705
2706 // If the onlyIfNeeded flag is set, then we can do this if the activity
2707 // being launched is the same as the one making the call... or, as
2708 // a special case, if we do not know the caller then we count the
2709 // current top activity as the caller.
Dianne Hackborna4972e92012-03-14 10:38:05 -07002710 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002711 ActivityRecord checkedCaller = sourceRecord;
2712 if (checkedCaller == null) {
2713 checkedCaller = topRunningNonDelayedActivityLocked(notTop);
2714 }
2715 if (!checkedCaller.realActivity.equals(r.realActivity)) {
2716 // Caller is not the same as launcher, so always needed.
Dianne Hackborna4972e92012-03-14 10:38:05 -07002717 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002718 }
2719 }
2720
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002721 if (sourceRecord == null) {
2722 // This activity is not being started from another... in this
2723 // case we -always- start a new task.
2724 if ((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2725 Slog.w(TAG, "startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: "
2726 + intent);
2727 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2728 }
2729 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2730 // The original activity who is starting us is running as a single
2731 // instance... this new activity it is starting must go on its
2732 // own task.
2733 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2734 } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
2735 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
2736 // The activity being started is a single instance... it always
2737 // gets launched into its own task.
2738 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2739 }
2740
2741 if (r.resultTo != null && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
2742 // For whatever reason this activity is being launched into a new
2743 // task... yet the caller has requested a result back. Well, that
2744 // is pretty messed up, so instead immediately send back a cancel
2745 // and let the new task continue launched as normal without a
2746 // dependency on its originator.
2747 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
2748 sendActivityResultLocked(-1,
2749 r.resultTo, r.resultWho, r.requestCode,
2750 Activity.RESULT_CANCELED, null);
2751 r.resultTo = null;
2752 }
2753
2754 boolean addingToTask = false;
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002755 boolean movedHome = false;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002756 TaskRecord reuseTask = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002757 if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2758 (launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
2759 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
2760 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2761 // If bring to front is requested, and no result is requested, and
2762 // we can find a task that was started with this same
2763 // component, then instead of launching bring that one to the front.
2764 if (r.resultTo == null) {
2765 // See if there is a task to bring to the front. If this is
2766 // a SINGLE_INSTANCE activity, there can be one and only one
2767 // instance of it in the history, and it is always in its own
2768 // unique task, so we do a special search.
2769 ActivityRecord taskTop = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
2770 ? findTaskLocked(intent, r.info)
2771 : findActivityLocked(intent, r.info);
2772 if (taskTop != null) {
2773 if (taskTop.task.intent == null) {
2774 // This task was started because of movement of
2775 // the activity based on affinity... now that we
2776 // are actually launching it, we can assign the
2777 // base intent.
2778 taskTop.task.setIntent(intent, r.info);
2779 }
2780 // If the target task is not in the front, then we need
2781 // to bring it to the front... except... well, with
2782 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2783 // to have the same behavior as if a new instance was
2784 // being started, which means not bringing it to the front
2785 // if the caller is not itself in the front.
2786 ActivityRecord curTop = topRunningNonDelayedActivityLocked(notTop);
Jean-Baptiste Queru66a5d692010-10-25 17:27:16 -07002787 if (curTop != null && curTop.task != taskTop.task) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002788 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
2789 boolean callerAtFront = sourceRecord == null
2790 || curTop.task == sourceRecord.task;
2791 if (callerAtFront) {
2792 // We really do want to push this one into the
2793 // user's face, right now.
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002794 movedHome = true;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002795 moveHomeToFrontFromLaunchLocked(launchFlags);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002796 moveTaskToFrontLocked(taskTop.task, r, options);
Dianne Hackborn84375872012-06-01 19:03:50 -07002797 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002798 }
2799 }
2800 // If the caller has requested that the target task be
2801 // reset, then do so.
2802 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2803 taskTop = resetTaskIfNeededLocked(taskTop, r);
2804 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002805 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002806 // We don't need to start a new activity, and
2807 // the client said not to do anything if that
2808 // is the case, so this is it! And for paranoia, make
2809 // sure we have correctly resumed the top activity.
2810 if (doResume) {
Dianne Hackborn84375872012-06-01 19:03:50 -07002811 resumeTopActivityLocked(null, options);
2812 } else {
2813 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002814 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002815 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002816 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002817 if ((launchFlags &
2818 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
2819 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
2820 // The caller has requested to completely replace any
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08002821 // existing task with its new activity. Well that should
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002822 // not be too hard...
2823 reuseTask = taskTop.task;
2824 performClearTaskLocked(taskTop.task.taskId);
2825 reuseTask.setIntent(r.intent, r.info);
2826 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002827 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
2828 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2829 // In this situation we want to remove all activities
2830 // from the task up to the one being started. In most
2831 // cases this means we are resetting the task to its
2832 // initial state.
2833 ActivityRecord top = performClearTaskLocked(
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002834 taskTop.task.taskId, r, launchFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002835 if (top != null) {
2836 if (top.frontOfTask) {
2837 // Activity aliases may mean we use different
2838 // intents for the top activity, so make sure
2839 // the task now has the identity of the new
2840 // intent.
2841 top.task.setIntent(r.intent, r.info);
2842 }
2843 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn39792d22010-08-19 18:01:52 -07002844 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002845 } else {
2846 // A special case: we need to
2847 // start the activity because it is not currently
2848 // running, and the caller has asked to clear the
2849 // current task to have this activity at the top.
2850 addingToTask = true;
2851 // Now pretend like this activity is being started
2852 // by the top of its task, so it is put in the
2853 // right place.
2854 sourceRecord = taskTop;
2855 }
2856 } else if (r.realActivity.equals(taskTop.task.realActivity)) {
2857 // In this case the top activity on the task is the
2858 // same as the one being launched, so we take that
2859 // as a request to bring the task to the foreground.
2860 // If the top activity in the task is the root
2861 // activity, deliver this new intent to it if it
2862 // desires.
Johan Viktorssonf363dfd2012-02-16 17:05:16 +01002863 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2864 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002865 && taskTop.realActivity.equals(r.realActivity)) {
2866 logStartActivity(EventLogTags.AM_NEW_INTENT, r, taskTop.task);
2867 if (taskTop.frontOfTask) {
2868 taskTop.task.setIntent(r.intent, r.info);
2869 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07002870 taskTop.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002871 } else if (!r.intent.filterEquals(taskTop.task.intent)) {
2872 // In this case we are launching the root activity
2873 // of the task, but with a different intent. We
2874 // should start a new instance on top.
2875 addingToTask = true;
2876 sourceRecord = taskTop;
2877 }
2878 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2879 // In this case an activity is being launched in to an
2880 // existing task, without resetting that task. This
2881 // is typically the situation of launching an activity
2882 // from a notification or shortcut. We want to place
2883 // the new activity on top of the current task.
2884 addingToTask = true;
2885 sourceRecord = taskTop;
2886 } else if (!taskTop.task.rootWasReset) {
2887 // In this case we are launching in to an existing task
2888 // that has not yet been started from its front door.
2889 // The current task has been brought to the front.
2890 // Ideally, we'd probably like to place this new task
2891 // at the bottom of its stack, but that's a little hard
2892 // to do with the current organization of the code so
2893 // for now we'll just drop it.
2894 taskTop.task.setIntent(r.intent, r.info);
2895 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002896 if (!addingToTask && reuseTask == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002897 // We didn't do anything... but it was needed (a.k.a., client
2898 // don't use that intent!) And for paranoia, make
2899 // sure we have correctly resumed the top activity.
2900 if (doResume) {
Dianne Hackborn84375872012-06-01 19:03:50 -07002901 resumeTopActivityLocked(null, options);
2902 } else {
2903 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002904 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002905 return ActivityManager.START_TASK_TO_FRONT;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002906 }
2907 }
2908 }
2909 }
2910
2911 //String uri = r.intent.toURI();
2912 //Intent intent2 = new Intent(uri);
2913 //Slog.i(TAG, "Given intent: " + r.intent);
2914 //Slog.i(TAG, "URI is: " + uri);
2915 //Slog.i(TAG, "To intent: " + intent2);
2916
2917 if (r.packageName != null) {
2918 // If the activity being launched is the same as the one currently
2919 // at the top, then we need to check if it should only be launched
2920 // once.
2921 ActivityRecord top = topRunningNonDelayedActivityLocked(notTop);
2922 if (top != null && r.resultTo == null) {
Amith Yamasani742a6712011-05-04 14:49:28 -07002923 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002924 if (top.app != null && top.app.thread != null) {
2925 if ((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2926 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP
2927 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
2928 logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2929 // For paranoia, make sure we have correctly
2930 // resumed the top activity.
2931 if (doResume) {
2932 resumeTopActivityLocked(null);
2933 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002934 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002935 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002936 // We don't need to start a new activity, and
2937 // the client said not to do anything if that
2938 // is the case, so this is it!
Dianne Hackborna4972e92012-03-14 10:38:05 -07002939 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002940 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07002941 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002942 return ActivityManager.START_DELIVERED_TO_TOP;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002943 }
2944 }
2945 }
2946 }
2947
2948 } else {
2949 if (r.resultTo != null) {
2950 sendActivityResultLocked(-1,
2951 r.resultTo, r.resultWho, r.requestCode,
2952 Activity.RESULT_CANCELED, null);
2953 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002954 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002955 return ActivityManager.START_CLASS_NOT_FOUND;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002956 }
2957
2958 boolean newTask = false;
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08002959 boolean keepCurTransition = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002960
2961 // Should this be considered a new task?
2962 if (r.resultTo == null && !addingToTask
2963 && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002964 if (reuseTask == null) {
2965 // todo: should do better management of integers.
2966 mService.mCurTask++;
2967 if (mService.mCurTask <= 0) {
2968 mService.mCurTask = 1;
2969 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002970 r.setTask(new TaskRecord(mService.mCurTask, r.info, intent), null, true);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002971 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2972 + " in new task " + r.task);
2973 } else {
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002974 r.setTask(reuseTask, reuseTask, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002975 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002976 newTask = true;
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002977 if (!movedHome) {
2978 moveHomeToFrontFromLaunchLocked(launchFlags);
2979 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002980
2981 } else if (sourceRecord != null) {
2982 if (!addingToTask &&
2983 (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2984 // In this case, we are adding the activity to an existing
2985 // task, but the caller has asked to clear that task if the
2986 // activity is already running.
2987 ActivityRecord top = performClearTaskLocked(
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002988 sourceRecord.task.taskId, r, launchFlags);
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08002989 keepCurTransition = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002990 if (top != null) {
2991 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn39792d22010-08-19 18:01:52 -07002992 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002993 // For paranoia, make sure we have correctly
2994 // resumed the top activity.
2995 if (doResume) {
2996 resumeTopActivityLocked(null);
2997 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002998 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002999 return ActivityManager.START_DELIVERED_TO_TOP;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003000 }
3001 } else if (!addingToTask &&
3002 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
3003 // In this case, we are launching an activity in our own task
3004 // that may already be running somewhere in the history, and
3005 // we want to shuffle it to the front of the stack if so.
3006 int where = findActivityInHistoryLocked(r, sourceRecord.task.taskId);
3007 if (where >= 0) {
3008 ActivityRecord top = moveActivityToFrontLocked(where);
3009 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07003010 top.updateOptionsLocked(options);
Dianne Hackborn39792d22010-08-19 18:01:52 -07003011 top.deliverNewIntentLocked(callingUid, r.intent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003012 if (doResume) {
3013 resumeTopActivityLocked(null);
3014 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07003015 return ActivityManager.START_DELIVERED_TO_TOP;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003016 }
3017 }
3018 // An existing activity is starting this new activity, so we want
3019 // to keep the new one in the same task as the one that is starting
3020 // it.
Dianne Hackbornf26fd992011-04-08 18:14:09 -07003021 r.setTask(sourceRecord.task, sourceRecord.thumbHolder, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003022 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
3023 + " in existing task " + r.task);
3024
3025 } else {
3026 // This not being started from an existing activity, and not part
3027 // of a new task... just put it in the top task, though these days
3028 // this case should never happen.
3029 final int N = mHistory.size();
3030 ActivityRecord prev =
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003031 N > 0 ? mHistory.get(N-1) : null;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07003032 r.setTask(prev != null
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003033 ? prev.task
Dianne Hackbornf26fd992011-04-08 18:14:09 -07003034 : new TaskRecord(mService.mCurTask, r.info, intent), null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003035 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
3036 + " in new guessed " + r.task);
3037 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07003038
Dianne Hackborn39792d22010-08-19 18:01:52 -07003039 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07003040 intent, r.getUriPermissionsLocked());
Dianne Hackborn39792d22010-08-19 18:01:52 -07003041
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003042 if (newTask) {
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003043 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003044 }
3045 logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07003046 startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07003047 return ActivityManager.START_SUCCESS;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003048 }
3049
Dianne Hackborna4972e92012-03-14 10:38:05 -07003050 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Amith Yamasani483f3b02012-03-13 16:08:00 -07003051 String profileFile, ParcelFileDescriptor profileFd, int userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003052 // Collect information about the target of the Intent.
3053 ActivityInfo aInfo;
3054 try {
3055 ResolveInfo rInfo =
3056 AppGlobals.getPackageManager().resolveIntent(
3057 intent, resolvedType,
3058 PackageManager.MATCH_DEFAULT_ONLY
Amith Yamasani483f3b02012-03-13 16:08:00 -07003059 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003060 aInfo = rInfo != null ? rInfo.activityInfo : null;
3061 } catch (RemoteException e) {
3062 aInfo = null;
3063 }
3064
3065 if (aInfo != null) {
3066 // Store the found target back into the intent, because now that
3067 // we have it we never want to do this again. For example, if the
3068 // user navigates back to this point in the history, we should
3069 // always restart the exact same activity.
3070 intent.setComponent(new ComponentName(
3071 aInfo.applicationInfo.packageName, aInfo.name));
3072
3073 // Don't debug things in the system process
Dianne Hackborna4972e92012-03-14 10:38:05 -07003074 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003075 if (!aInfo.processName.equals("system")) {
3076 mService.setDebugApp(aInfo.processName, true, false);
3077 }
3078 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003079
Dianne Hackborna4972e92012-03-14 10:38:05 -07003080 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
Siva Velusamy92a8b222012-03-09 16:24:04 -08003081 if (!aInfo.processName.equals("system")) {
3082 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
3083 }
3084 }
3085
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003086 if (profileFile != null) {
3087 if (!aInfo.processName.equals("system")) {
3088 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName,
Dianne Hackborna4972e92012-03-14 10:38:05 -07003089 profileFile, profileFd,
3090 (startFlags&ActivityManager.START_FLAG_AUTO_STOP_PROFILER) != 0);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003091 }
3092 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003093 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003094 return aInfo;
3095 }
3096
3097 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackbornf265ea92013-01-31 15:00:51 -08003098 String callingPackage, Intent intent, String resolvedType, IBinder resultTo,
Dianne Hackborna4972e92012-03-14 10:38:05 -07003099 String resultWho, int requestCode, int startFlags, String profileFile,
3100 ParcelFileDescriptor profileFd, WaitResult outResult, Configuration config,
3101 Bundle options, int userId) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003102 // Refuse possible leaked file descriptors
3103 if (intent != null && intent.hasFileDescriptors()) {
3104 throw new IllegalArgumentException("File descriptors passed in Intent");
3105 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003106 boolean componentSpecified = intent.getComponent() != null;
3107
3108 // Don't modify the client's object!
3109 intent = new Intent(intent);
3110
3111 // Collect information about the target of the Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07003112 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
Amith Yamasani483f3b02012-03-13 16:08:00 -07003113 profileFile, profileFd, userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003114
3115 synchronized (mService) {
3116 int callingPid;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003117 if (callingUid >= 0) {
3118 callingPid = -1;
3119 } else if (caller == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003120 callingPid = Binder.getCallingPid();
3121 callingUid = Binder.getCallingUid();
3122 } else {
3123 callingPid = callingUid = -1;
3124 }
3125
3126 mConfigWillChange = config != null
3127 && mService.mConfiguration.diff(config) != 0;
3128 if (DEBUG_CONFIGURATION) Slog.v(TAG,
3129 "Starting activity when config will change = " + mConfigWillChange);
3130
3131 final long origId = Binder.clearCallingIdentity();
3132
3133 if (mMainStack && aInfo != null &&
Dianne Hackborn54e570f2010-10-04 18:32:32 -07003134 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003135 // This may be a heavy-weight process! Check to see if we already
3136 // have another, different heavy-weight process running.
3137 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
3138 if (mService.mHeavyWeightProcess != null &&
3139 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
3140 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
3141 int realCallingPid = callingPid;
3142 int realCallingUid = callingUid;
3143 if (caller != null) {
3144 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
3145 if (callerApp != null) {
3146 realCallingPid = callerApp.pid;
3147 realCallingUid = callerApp.info.uid;
3148 } else {
3149 Slog.w(TAG, "Unable to find app for caller " + caller
3150 + " (pid=" + realCallingPid + ") when starting: "
3151 + intent.toString());
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003152 ActivityOptions.abort(options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07003153 return ActivityManager.START_PERMISSION_DENIED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003154 }
3155 }
3156
3157 IIntentSender target = mService.getIntentSenderLocked(
Dianne Hackborna4972e92012-03-14 10:38:05 -07003158 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn41203752012-08-31 14:05:51 -07003159 realCallingUid, userId, null, null, 0, new Intent[] { intent },
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003160 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07003161 | PendingIntent.FLAG_ONE_SHOT, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003162
3163 Intent newIntent = new Intent();
3164 if (requestCode >= 0) {
3165 // Caller is requesting a result.
3166 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
3167 }
3168 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
3169 new IntentSender(target));
3170 if (mService.mHeavyWeightProcess.activities.size() > 0) {
3171 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
3172 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
3173 hist.packageName);
3174 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
3175 hist.task.taskId);
3176 }
3177 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
3178 aInfo.packageName);
3179 newIntent.setFlags(intent.getFlags());
3180 newIntent.setClassName("android",
3181 HeavyWeightSwitcherActivity.class.getName());
3182 intent = newIntent;
3183 resolvedType = null;
3184 caller = null;
3185 callingUid = Binder.getCallingUid();
3186 callingPid = Binder.getCallingPid();
3187 componentSpecified = true;
3188 try {
3189 ResolveInfo rInfo =
3190 AppGlobals.getPackageManager().resolveIntent(
3191 intent, null,
3192 PackageManager.MATCH_DEFAULT_ONLY
Amith Yamasani483f3b02012-03-13 16:08:00 -07003193 | ActivityManagerService.STOCK_PM_FLAGS, userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003194 aInfo = rInfo != null ? rInfo.activityInfo : null;
Amith Yamasani742a6712011-05-04 14:49:28 -07003195 aInfo = mService.getActivityInfoForUser(aInfo, userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003196 } catch (RemoteException e) {
3197 aInfo = null;
3198 }
3199 }
3200 }
3201 }
3202
3203 int res = startActivityLocked(caller, intent, resolvedType,
Dianne Hackborna4972e92012-03-14 10:38:05 -07003204 aInfo, resultTo, resultWho, requestCode, callingPid, callingUid,
Dianne Hackbornf265ea92013-01-31 15:00:51 -08003205 callingPackage, startFlags, options, componentSpecified, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003206
3207 if (mConfigWillChange && mMainStack) {
3208 // If the caller also wants to switch to a new configuration,
3209 // do so now. This allows a clean switch, as we are waiting
3210 // for the current activity to pause (so we will not destroy
3211 // it), and have not yet started the next activity.
3212 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
3213 "updateConfiguration()");
3214 mConfigWillChange = false;
3215 if (DEBUG_CONFIGURATION) Slog.v(TAG,
3216 "Updating to new configuration after starting activity.");
Dianne Hackborn813075a62011-11-14 17:45:19 -08003217 mService.updateConfigurationLocked(config, null, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003218 }
3219
3220 Binder.restoreCallingIdentity(origId);
3221
3222 if (outResult != null) {
3223 outResult.result = res;
Dianne Hackborna4972e92012-03-14 10:38:05 -07003224 if (res == ActivityManager.START_SUCCESS) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003225 mWaitingActivityLaunched.add(outResult);
3226 do {
3227 try {
Dianne Hackbornba0492d2010-10-12 19:01:46 -07003228 mService.wait();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003229 } catch (InterruptedException e) {
3230 }
3231 } while (!outResult.timeout && outResult.who == null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07003232 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003233 ActivityRecord r = this.topRunningActivityLocked(null);
3234 if (r.nowVisible) {
3235 outResult.timeout = false;
3236 outResult.who = new ComponentName(r.info.packageName, r.info.name);
3237 outResult.totalTime = 0;
3238 outResult.thisTime = 0;
3239 } else {
3240 outResult.thisTime = SystemClock.uptimeMillis();
3241 mWaitingActivityVisible.add(outResult);
3242 do {
3243 try {
Dianne Hackbornba0492d2010-10-12 19:01:46 -07003244 mService.wait();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003245 } catch (InterruptedException e) {
3246 }
3247 } while (!outResult.timeout && outResult.who == null);
3248 }
3249 }
3250 }
3251
3252 return res;
3253 }
3254 }
3255
Dianne Hackbornf265ea92013-01-31 15:00:51 -08003256 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
Dianne Hackborna4972e92012-03-14 10:38:05 -07003257 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
3258 Bundle options, int userId) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003259 if (intents == null) {
3260 throw new NullPointerException("intents is null");
3261 }
3262 if (resolvedTypes == null) {
3263 throw new NullPointerException("resolvedTypes is null");
3264 }
3265 if (intents.length != resolvedTypes.length) {
3266 throw new IllegalArgumentException("intents are length different than resolvedTypes");
3267 }
3268
3269 ActivityRecord[] outActivity = new ActivityRecord[1];
3270
3271 int callingPid;
3272 if (callingUid >= 0) {
3273 callingPid = -1;
3274 } else if (caller == null) {
3275 callingPid = Binder.getCallingPid();
3276 callingUid = Binder.getCallingUid();
3277 } else {
3278 callingPid = callingUid = -1;
3279 }
3280 final long origId = Binder.clearCallingIdentity();
3281 try {
3282 synchronized (mService) {
3283
3284 for (int i=0; i<intents.length; i++) {
3285 Intent intent = intents[i];
3286 if (intent == null) {
3287 continue;
3288 }
3289
3290 // Refuse possible leaked file descriptors
3291 if (intent != null && intent.hasFileDescriptors()) {
3292 throw new IllegalArgumentException("File descriptors passed in Intent");
3293 }
3294
3295 boolean componentSpecified = intent.getComponent() != null;
3296
3297 // Don't modify the client's object!
3298 intent = new Intent(intent);
3299
3300 // Collect information about the target of the Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07003301 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i],
Amith Yamasani483f3b02012-03-13 16:08:00 -07003302 0, null, null, userId);
Amith Yamasani742a6712011-05-04 14:49:28 -07003303 // TODO: New, check if this is correct
3304 aInfo = mService.getActivityInfoForUser(aInfo, userId);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003305
3306 if (mMainStack && aInfo != null && (aInfo.applicationInfo.flags
3307 & ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
3308 throw new IllegalArgumentException(
3309 "FLAG_CANT_SAVE_STATE not supported here");
3310 }
3311
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07003312 Bundle theseOptions;
3313 if (options != null && i == intents.length-1) {
3314 theseOptions = options;
3315 } else {
3316 theseOptions = null;
3317 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003318 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackbornf265ea92013-01-31 15:00:51 -08003319 aInfo, resultTo, null, -1, callingPid, callingUid, callingPackage,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07003320 0, theseOptions, componentSpecified, outActivity);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003321 if (res < 0) {
3322 return res;
3323 }
3324
Dianne Hackbornbe707852011-11-11 14:32:10 -08003325 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003326 }
3327 }
3328 } finally {
3329 Binder.restoreCallingIdentity(origId);
3330 }
3331
Dianne Hackborna4972e92012-03-14 10:38:05 -07003332 return ActivityManager.START_SUCCESS;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003333 }
3334
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003335 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
3336 long thisTime, long totalTime) {
3337 for (int i=mWaitingActivityLaunched.size()-1; i>=0; i--) {
3338 WaitResult w = mWaitingActivityLaunched.get(i);
3339 w.timeout = timeout;
3340 if (r != null) {
3341 w.who = new ComponentName(r.info.packageName, r.info.name);
3342 }
3343 w.thisTime = thisTime;
3344 w.totalTime = totalTime;
3345 }
3346 mService.notifyAll();
3347 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003348
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003349 void reportActivityVisibleLocked(ActivityRecord r) {
3350 for (int i=mWaitingActivityVisible.size()-1; i>=0; i--) {
3351 WaitResult w = mWaitingActivityVisible.get(i);
3352 w.timeout = false;
3353 if (r != null) {
3354 w.who = new ComponentName(r.info.packageName, r.info.name);
3355 }
3356 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
3357 w.thisTime = w.totalTime;
3358 }
3359 mService.notifyAll();
Dianne Hackborn90c52de2011-09-23 12:57:44 -07003360
3361 if (mDismissKeyguardOnNextActivity) {
3362 mDismissKeyguardOnNextActivity = false;
3363 mService.mWindowManager.dismissKeyguard();
3364 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003365 }
3366
3367 void sendActivityResultLocked(int callingUid, ActivityRecord r,
3368 String resultWho, int requestCode, int resultCode, Intent data) {
3369
3370 if (callingUid > 0) {
3371 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07003372 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003373 }
3374
3375 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
3376 + " : who=" + resultWho + " req=" + requestCode
3377 + " res=" + resultCode + " data=" + data);
3378 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
3379 try {
3380 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
3381 list.add(new ResultInfo(resultWho, requestCode,
3382 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08003383 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003384 return;
3385 } catch (Exception e) {
3386 Slog.w(TAG, "Exception thrown sending result to " + r, e);
3387 }
3388 }
3389
3390 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
3391 }
3392
3393 private final void stopActivityLocked(ActivityRecord r) {
3394 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
3395 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3396 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3397 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07003398 if (!mService.mSleeping) {
3399 if (DEBUG_STATES) {
3400 Slog.d(TAG, "no-history finish of " + r);
3401 }
3402 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003403 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07003404 } else {
3405 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
3406 + " on stop because we're just sleeping");
3407 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003408 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003409 }
3410
3411 if (r.app != null && r.app.thread != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003412 if (mMainStack) {
3413 if (mService.mFocusedActivity == r) {
3414 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
3415 }
3416 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003417 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003418 try {
3419 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003420 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
3421 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003422 r.state = ActivityState.STOPPING;
3423 if (DEBUG_VISBILITY) Slog.v(
3424 TAG, "Stopping visible=" + r.visible + " for " + r);
3425 if (!r.visible) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003426 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003427 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08003428 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003429 if (mService.isSleeping()) {
3430 r.setSleeping(true);
3431 }
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003432 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG);
3433 msg.obj = r;
3434 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003435 } catch (Exception e) {
3436 // Maybe just ignore exceptions here... if the process
3437 // has crashed, our death notification will clean things
3438 // up.
3439 Slog.w(TAG, "Exception thrown during pause", e);
3440 // Just in case, assume it to be stopped.
3441 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003442 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003443 r.state = ActivityState.STOPPED;
3444 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07003445 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003446 }
3447 }
3448 }
3449 }
3450
3451 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(
3452 boolean remove) {
3453 int N = mStoppingActivities.size();
3454 if (N <= 0) return null;
3455
3456 ArrayList<ActivityRecord> stops = null;
3457
3458 final boolean nowVisible = mResumedActivity != null
3459 && mResumedActivity.nowVisible
3460 && !mResumedActivity.waitingVisible;
3461 for (int i=0; i<N; i++) {
3462 ActivityRecord s = mStoppingActivities.get(i);
3463 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
3464 + nowVisible + " waitingVisible=" + s.waitingVisible
3465 + " finishing=" + s.finishing);
3466 if (s.waitingVisible && nowVisible) {
3467 mWaitingVisibleActivities.remove(s);
3468 s.waitingVisible = false;
3469 if (s.finishing) {
3470 // If this activity is finishing, it is sitting on top of
3471 // everyone else but we now know it is no longer needed...
3472 // so get rid of it. Otherwise, we need to go through the
3473 // normal flow and hide it once we determine that it is
3474 // hidden by the activities in front of it.
3475 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003476 mService.mWindowManager.setAppVisibility(s.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003477 }
3478 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003479 if ((!s.waitingVisible || mService.isSleeping()) && remove) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003480 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
3481 if (stops == null) {
3482 stops = new ArrayList<ActivityRecord>();
3483 }
3484 stops.add(s);
3485 mStoppingActivities.remove(i);
3486 N--;
3487 i--;
3488 }
3489 }
3490
3491 return stops;
3492 }
3493
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07003494 final void scheduleIdleLocked() {
3495 Message msg = Message.obtain();
3496 msg.what = IDLE_NOW_MSG;
3497 mHandler.sendMessage(msg);
3498 }
3499
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003500 final ActivityRecord activityIdleInternal(IBinder token, boolean fromTimeout,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003501 Configuration config) {
3502 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
3503
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003504 ActivityRecord res = null;
3505
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003506 ArrayList<ActivityRecord> stops = null;
3507 ArrayList<ActivityRecord> finishes = null;
3508 ArrayList<ActivityRecord> thumbnails = null;
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003509 ArrayList<UserStartedState> startingUsers = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003510 int NS = 0;
3511 int NF = 0;
3512 int NT = 0;
3513 IApplicationThread sendThumbnail = null;
3514 boolean booting = false;
3515 boolean enableScreen = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003516 boolean activityRemoved = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003517
3518 synchronized (mService) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003519 ActivityRecord r = ActivityRecord.forToken(token);
3520 if (r != null) {
3521 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003522 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003523 }
3524
3525 // Get the activity record.
Dianne Hackbornbe707852011-11-11 14:32:10 -08003526 int index = indexOfActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003527 if (index >= 0) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003528 res = r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003529
3530 if (fromTimeout) {
3531 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
3532 }
3533
3534 // This is a hack to semi-deal with a race condition
3535 // in the client where it can be constructed with a
3536 // newer configuration from when we asked it to launch.
3537 // We'll update with whatever configuration it now says
3538 // it used to launch.
3539 if (config != null) {
3540 r.configuration = config;
3541 }
3542
3543 // No longer need to keep the device awake.
3544 if (mResumedActivity == r && mLaunchingActivity.isHeld()) {
3545 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
3546 mLaunchingActivity.release();
3547 }
3548
3549 // We are now idle. If someone is waiting for a thumbnail from
3550 // us, we can now deliver.
3551 r.idle = true;
3552 mService.scheduleAppGcsLocked();
3553 if (r.thumbnailNeeded && r.app != null && r.app.thread != null) {
3554 sendThumbnail = r.app.thread;
3555 r.thumbnailNeeded = false;
3556 }
3557
3558 // If this activity is fullscreen, set up to hide those under it.
3559
3560 if (DEBUG_VISBILITY) Slog.v(TAG, "Idle activity for " + r);
3561 ensureActivitiesVisibleLocked(null, 0);
3562
3563 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
3564 if (mMainStack) {
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07003565 if (!mService.mBooted) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003566 mService.mBooted = true;
3567 enableScreen = true;
3568 }
3569 }
3570
3571 } else if (fromTimeout) {
3572 reportActivityLaunchedLocked(fromTimeout, null, -1, -1);
3573 }
3574
3575 // Atomically retrieve all of the other things to do.
3576 stops = processStoppingActivitiesLocked(true);
3577 NS = stops != null ? stops.size() : 0;
3578 if ((NF=mFinishingActivities.size()) > 0) {
3579 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
3580 mFinishingActivities.clear();
3581 }
3582 if ((NT=mService.mCancelledThumbnails.size()) > 0) {
3583 thumbnails = new ArrayList<ActivityRecord>(mService.mCancelledThumbnails);
3584 mService.mCancelledThumbnails.clear();
3585 }
3586
3587 if (mMainStack) {
3588 booting = mService.mBooting;
3589 mService.mBooting = false;
3590 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003591 if (mStartingUsers.size() > 0) {
3592 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
3593 mStartingUsers.clear();
3594 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003595 }
3596
3597 int i;
3598
3599 // Send thumbnail if requested.
3600 if (sendThumbnail != null) {
3601 try {
3602 sendThumbnail.requestThumbnail(token);
3603 } catch (Exception e) {
3604 Slog.w(TAG, "Exception thrown when requesting thumbnail", e);
3605 mService.sendPendingThumbnail(null, token, null, null, true);
3606 }
3607 }
3608
3609 // Stop any activities that are scheduled to do so but have been
3610 // waiting for the next one to start.
3611 for (i=0; i<NS; i++) {
3612 ActivityRecord r = (ActivityRecord)stops.get(i);
3613 synchronized (mService) {
3614 if (r.finishing) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003615 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003616 } else {
3617 stopActivityLocked(r);
3618 }
3619 }
3620 }
3621
3622 // Finish any activities that are scheduled to do so but have been
3623 // waiting for the next one to start.
3624 for (i=0; i<NF; i++) {
3625 ActivityRecord r = (ActivityRecord)finishes.get(i);
3626 synchronized (mService) {
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003627 activityRemoved = destroyActivityLocked(r, true, false, "finish-idle");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003628 }
3629 }
3630
3631 // Report back to any thumbnail receivers.
3632 for (i=0; i<NT; i++) {
3633 ActivityRecord r = (ActivityRecord)thumbnails.get(i);
3634 mService.sendPendingThumbnail(r, null, null, null, true);
3635 }
3636
3637 if (booting) {
3638 mService.finishBooting();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003639 } else if (startingUsers != null) {
3640 for (i=0; i<startingUsers.size(); i++) {
3641 mService.finishUserSwitch(startingUsers.get(i));
3642 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003643 }
3644
3645 mService.trimApplications();
3646 //dump();
3647 //mWindowManager.dump();
3648
3649 if (enableScreen) {
3650 mService.enableScreenAfterBoot();
3651 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003652
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003653 if (activityRemoved) {
3654 resumeTopActivityLocked(null);
3655 }
3656
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003657 return res;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003658 }
3659
3660 /**
3661 * @return Returns true if the activity is being finished, false if for
3662 * some reason it is being left as-is.
3663 */
3664 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003665 Intent resultData, String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003666 int index = indexOfTokenLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07003667 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07003668 TAG, "Finishing activity @" + index + ": token=" + token
Christopher Tated3f175c2012-06-14 14:16:54 -07003669 + ", result=" + resultCode + ", data=" + resultData
3670 + ", reason=" + reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003671 if (index < 0) {
3672 return false;
3673 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003674 ActivityRecord r = mHistory.get(index);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003675
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003676 finishActivityLocked(r, index, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003677 return true;
3678 }
3679
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003680 final void finishSubActivityLocked(IBinder token, String resultWho, int requestCode) {
3681 ActivityRecord self = isInStackLocked(token);
3682 if (self == null) {
3683 return;
3684 }
3685
3686 int i;
3687 for (i=mHistory.size()-1; i>=0; i--) {
3688 ActivityRecord r = (ActivityRecord)mHistory.get(i);
3689 if (r.resultTo == self && r.requestCode == requestCode) {
3690 if ((r.resultWho == null && resultWho == null) ||
3691 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3692 finishActivityLocked(r, i,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003693 Activity.RESULT_CANCELED, null, "request-sub", false);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003694 }
3695 }
3696 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003697 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003698 }
3699
3700 final boolean finishActivityAffinityLocked(IBinder token) {
3701 int index = indexOfTokenLocked(token);
3702 if (DEBUG_RESULTS) Slog.v(
3703 TAG, "Finishing activity affinity @" + index + ": token=" + token);
3704 if (index < 0) {
3705 return false;
3706 }
3707 ActivityRecord r = mHistory.get(index);
3708
Amith Yamasanibfc1be12012-05-15 11:12:17 -07003709 while (index >= 0) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003710 ActivityRecord cur = mHistory.get(index);
3711 if (cur.task != r.task) {
3712 break;
3713 }
3714 if (cur.taskAffinity == null && r.taskAffinity != null) {
3715 break;
3716 }
3717 if (cur.taskAffinity != null && !cur.taskAffinity.equals(r.taskAffinity)) {
3718 break;
3719 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003720 finishActivityLocked(cur, index, Activity.RESULT_CANCELED, null,
3721 "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003722 index--;
3723 }
3724 return true;
3725 }
3726
Dianne Hackborn5c607432012-02-28 14:44:19 -08003727 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3728 // send the result
3729 ActivityRecord resultTo = r.resultTo;
3730 if (resultTo != null) {
3731 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
3732 + " who=" + r.resultWho + " req=" + r.requestCode
3733 + " res=" + resultCode + " data=" + resultData);
3734 if (r.info.applicationInfo.uid > 0) {
3735 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3736 resultTo.packageName, resultData,
3737 resultTo.getUriPermissionsLocked());
3738 }
3739 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3740 resultData);
3741 r.resultTo = null;
3742 }
3743 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
3744
3745 // Make sure this HistoryRecord is not holding on to other resources,
3746 // because clients have remote IPC references to this object so we
3747 // can't assume that will go away and want to avoid circular IPC refs.
3748 r.results = null;
3749 r.pendingResults = null;
3750 r.newIntents = null;
3751 r.icicle = null;
3752 }
3753
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003754 /**
3755 * @return Returns true if this activity has been removed from the history
3756 * list, or false if it is still in the list and will be removed later.
3757 */
3758 final boolean finishActivityLocked(ActivityRecord r, int index,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003759 int resultCode, Intent resultData, String reason, boolean oomAdj) {
3760 return finishActivityLocked(r, index, resultCode, resultData, reason, false, oomAdj);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003761 }
3762
3763 /**
3764 * @return Returns true if this activity has been removed from the history
3765 * list, or false if it is still in the list and will be removed later.
3766 */
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003767 final boolean finishActivityLocked(ActivityRecord r, int index, int resultCode,
3768 Intent resultData, String reason, boolean immediate, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003769 if (r.finishing) {
3770 Slog.w(TAG, "Duplicate finish request for " + r);
3771 return false;
3772 }
3773
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08003774 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003775 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003776 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003777 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003778 if (index < (mHistory.size()-1)) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003779 ActivityRecord next = mHistory.get(index+1);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003780 if (next.task == r.task) {
3781 if (r.frontOfTask) {
3782 // The next activity is now the front of the task.
3783 next.frontOfTask = true;
3784 }
3785 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
3786 // If the caller asked that this activity (and all above it)
3787 // be cleared when the task is reset, don't lose that information,
3788 // but propagate it up to the next activity.
3789 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
3790 }
3791 }
3792 }
3793
3794 r.pauseKeyDispatchingLocked();
3795 if (mMainStack) {
3796 if (mService.mFocusedActivity == r) {
3797 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
3798 }
3799 }
3800
Dianne Hackborn5c607432012-02-28 14:44:19 -08003801 finishActivityResultsLocked(r, resultCode, resultData);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003802
3803 if (mService.mPendingThumbnails.size() > 0) {
3804 // There are clients waiting to receive thumbnails so, in case
3805 // this is an activity that someone is waiting for, add it
3806 // to the pending list so we can correctly update the clients.
3807 mService.mCancelledThumbnails.add(r);
3808 }
3809
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003810 if (immediate) {
3811 return finishCurrentActivityLocked(r, index,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003812 FINISH_IMMEDIATELY, oomAdj) == null;
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003813 } else if (mResumedActivity == r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003814 boolean endTask = index <= 0
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003815 || (mHistory.get(index-1)).task != r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003816 if (DEBUG_TRANSITION) Slog.v(TAG,
3817 "Prepare close transition: finishing " + r);
3818 mService.mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003819 ? AppTransition.TRANSIT_TASK_CLOSE
3820 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003821
3822 // Tell window manager to prepare for this one to be removed.
Dianne Hackbornbe707852011-11-11 14:32:10 -08003823 mService.mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003824
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003825 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003826 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
3827 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
3828 startPausingLocked(false, false);
3829 }
3830
3831 } else if (r.state != ActivityState.PAUSING) {
3832 // If the activity is PAUSING, we will complete the finish once
3833 // it is done pausing; else we can just directly finish it here.
3834 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
3835 return finishCurrentActivityLocked(r, index,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003836 FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003837 } else {
3838 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
3839 }
3840
3841 return false;
3842 }
3843
3844 private static final int FINISH_IMMEDIATELY = 0;
3845 private static final int FINISH_AFTER_PAUSE = 1;
3846 private static final int FINISH_AFTER_VISIBLE = 2;
3847
3848 private final ActivityRecord finishCurrentActivityLocked(ActivityRecord r,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003849 int mode, boolean oomAdj) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003850 final int index = indexOfActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003851 if (index < 0) {
3852 return null;
3853 }
3854
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003855 return finishCurrentActivityLocked(r, index, mode, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003856 }
3857
3858 private final ActivityRecord finishCurrentActivityLocked(ActivityRecord r,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003859 int index, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003860 // First things first: if this activity is currently visible,
3861 // and the resumed activity is not yet visible, then hold off on
3862 // finishing until the resumed one becomes visible.
3863 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
3864 if (!mStoppingActivities.contains(r)) {
3865 mStoppingActivities.add(r);
3866 if (mStoppingActivities.size() > 3) {
3867 // If we already have a few activities waiting to stop,
3868 // then give up on things going idle and start clearing
3869 // them out.
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07003870 scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003871 } else {
3872 checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003873 }
3874 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003875 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
3876 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003877 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003878 if (oomAdj) {
3879 mService.updateOomAdjLocked();
3880 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003881 return r;
3882 }
3883
3884 // make sure the record is cleaned out of other places.
3885 mStoppingActivities.remove(r);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003886 mGoingToSleepActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003887 mWaitingVisibleActivities.remove(r);
3888 if (mResumedActivity == r) {
3889 mResumedActivity = null;
3890 }
3891 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003892 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003893 r.state = ActivityState.FINISHING;
3894
3895 if (mode == FINISH_IMMEDIATELY
3896 || prevState == ActivityState.STOPPED
3897 || prevState == ActivityState.INITIALIZING) {
3898 // If this activity is already stopped, we can just finish
3899 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003900 boolean activityRemoved = destroyActivityLocked(r, true,
3901 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003902 if (activityRemoved) {
3903 resumeTopActivityLocked(null);
3904 }
3905 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003906 } else {
3907 // Need to go through the full pause cycle to get this
3908 // activity into the stopped state and then finish it.
3909 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
3910 mFinishingActivities.add(r);
3911 resumeTopActivityLocked(null);
3912 }
3913 return r;
3914 }
3915
3916 /**
3917 * Perform the common clean-up of an activity record. This is called both
3918 * as part of destroyActivityLocked() (when destroying the client-side
3919 * representation) and cleaning things up as a result of its hosting
3920 * processing going away, in which case there is no remaining client-side
3921 * state to destroy so only the cleanup here is needed.
3922 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003923 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3924 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003925 if (mResumedActivity == r) {
3926 mResumedActivity = null;
3927 }
3928 if (mService.mFocusedActivity == r) {
3929 mService.mFocusedActivity = null;
3930 }
3931
3932 r.configDestroy = false;
3933 r.frozenBeforeDestroy = false;
3934
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003935 if (setState) {
3936 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
3937 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003938 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003939 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003940 }
3941
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003942 // Make sure this record is no longer in the pending finishes list.
3943 // This could happen, for example, if we are trimming activities
3944 // down to the max limit while they are still waiting to finish.
3945 mFinishingActivities.remove(r);
3946 mWaitingVisibleActivities.remove(r);
3947
3948 // Remove any pending results.
3949 if (r.finishing && r.pendingResults != null) {
3950 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3951 PendingIntentRecord rec = apr.get();
3952 if (rec != null) {
3953 mService.cancelIntentSenderLocked(rec, false);
3954 }
3955 }
3956 r.pendingResults = null;
3957 }
3958
3959 if (cleanServices) {
3960 cleanUpActivityServicesLocked(r);
3961 }
3962
3963 if (mService.mPendingThumbnails.size() > 0) {
3964 // There are clients waiting to receive thumbnails so, in case
3965 // this is an activity that someone is waiting for, add it
3966 // to the pending list so we can correctly update the clients.
3967 mService.mCancelledThumbnails.add(r);
3968 }
3969
3970 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003971 removeTimeoutsForActivityLocked(r);
3972 }
3973
3974 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003975 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003976 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003977 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003978 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003979 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003980 }
3981
Dianne Hackborn5c607432012-02-28 14:44:19 -08003982 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003983 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
3984 r.makeFinishing();
3985 if (DEBUG_ADD_REMOVE) {
3986 RuntimeException here = new RuntimeException("here");
3987 here.fillInStackTrace();
3988 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003989 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003990 mHistory.remove(r);
3991 r.takeFromHistory();
3992 removeTimeoutsForActivityLocked(r);
3993 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
3994 + " (removed from history)");
3995 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003996 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003997 r.app = null;
3998 mService.mWindowManager.removeAppToken(r.appToken);
3999 if (VALIDATE_TOKENS) {
4000 validateAppTokensLocked();
4001 }
4002 cleanUpActivityServicesLocked(r);
4003 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004004 }
4005
4006 /**
4007 * Perform clean-up of service connections in an activity record.
4008 */
4009 final void cleanUpActivityServicesLocked(ActivityRecord r) {
4010 // Throw away any services that have been bound by this activity.
4011 if (r.connections != null) {
4012 Iterator<ConnectionRecord> it = r.connections.iterator();
4013 while (it.hasNext()) {
4014 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07004015 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004016 }
4017 r.connections = null;
4018 }
4019 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004020
4021 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
4022 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
4023 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
4024 mHandler.sendMessage(msg);
4025 }
4026
Dianne Hackborn28695e02011-11-02 21:59:51 -07004027 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004028 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004029 boolean activityRemoved = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004030 for (int i=mHistory.size()-1; i>=0; i--) {
4031 ActivityRecord r = mHistory.get(i);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004032 if (r.finishing) {
4033 continue;
4034 }
4035 if (r.fullscreen) {
4036 lastIsOpaque = true;
4037 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004038 if (owner != null && r.app != owner) {
4039 continue;
4040 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004041 if (!lastIsOpaque) {
4042 continue;
4043 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004044 // We can destroy this one if we have its icicle saved and
4045 // it is not in the process of pausing/stopping/finishing.
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004046 if (r.app != null && r != mResumedActivity && r != mPausingActivity
4047 && r.haveState && !r.visible && r.stopped
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004048 && r.state != ActivityState.DESTROYING
4049 && r.state != ActivityState.DESTROYED) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004050 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
4051 + " resumed=" + mResumedActivity
4052 + " pausing=" + mPausingActivity);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004053 if (destroyActivityLocked(r, true, oomAdj, reason)) {
4054 activityRemoved = true;
4055 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004056 }
4057 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004058 if (activityRemoved) {
4059 resumeTopActivityLocked(null);
4060 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004061 }
4062
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004063 /**
4064 * Destroy the current CLIENT SIDE instance of an activity. This may be
4065 * called both when actually finishing an activity, or when performing
4066 * a configuration switch where we destroy the current client-side object
4067 * but then create a new client-side object for this same HistoryRecord.
4068 */
4069 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07004070 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004071 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004072 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004073 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
4074 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004075 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07004076 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004077
4078 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004079
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004080 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004081
4082 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004083
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004084 if (hadApp) {
4085 if (removeFromApp) {
4086 int idx = r.app.activities.indexOf(r);
4087 if (idx >= 0) {
4088 r.app.activities.remove(idx);
4089 }
4090 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
4091 mService.mHeavyWeightProcess = null;
4092 mService.mHandler.sendEmptyMessage(
4093 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
4094 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004095 if (r.app.activities.size() == 0) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07004096 // No longer have activities, so update oom adj.
4097 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004098 }
4099 }
4100
4101 boolean skipDestroy = false;
4102
4103 try {
4104 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08004105 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004106 r.configChangeFlags);
4107 } catch (Exception e) {
4108 // We can just ignore exceptions here... if the process
4109 // has crashed, our death notification will clean things
4110 // up.
4111 //Slog.w(TAG, "Exception thrown during finish", e);
4112 if (r.finishing) {
4113 removeActivityFromHistoryLocked(r);
4114 removedFromHistory = true;
4115 skipDestroy = true;
4116 }
4117 }
4118
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004119 r.nowVisible = false;
4120
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004121 // If the activity is finishing, we need to wait on removing it
4122 // from the list to give it a chance to do its cleanup. During
4123 // that time it may make calls back with its token so we need to
4124 // be able to find it on the list and so we don't want to remove
4125 // it from the list yet. Otherwise, we can just immediately put
4126 // it in the destroyed state since we are not removing it from the
4127 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004128 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004129 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
4130 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004131 r.state = ActivityState.DESTROYING;
4132 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG);
4133 msg.obj = r;
4134 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
4135 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004136 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
4137 + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004138 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08004139 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004140 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004141 }
4142 } else {
4143 // remove this record from the history.
4144 if (r.finishing) {
4145 removeActivityFromHistoryLocked(r);
4146 removedFromHistory = true;
4147 } else {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004148 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
4149 + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004150 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08004151 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004152 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004153 }
4154 }
4155
4156 r.configChangeFlags = 0;
4157
4158 if (!mLRUActivities.remove(r) && hadApp) {
4159 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
4160 }
4161
4162 return removedFromHistory;
4163 }
4164
4165 final void activityDestroyed(IBinder token) {
4166 synchronized (mService) {
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004167 final long origId = Binder.clearCallingIdentity();
4168 try {
4169 ActivityRecord r = ActivityRecord.forToken(token);
4170 if (r != null) {
4171 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004172 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004173
4174 int index = indexOfActivityLocked(r);
4175 if (index >= 0) {
4176 if (r.state == ActivityState.DESTROYING) {
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07004177 cleanUpActivityLocked(r, true, false);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004178 removeActivityFromHistoryLocked(r);
4179 }
4180 }
4181 resumeTopActivityLocked(null);
4182 } finally {
4183 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004184 }
4185 }
4186 }
4187
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004188 private void removeHistoryRecordsForAppLocked(ArrayList list, ProcessRecord app,
4189 String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004190 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004191 if (DEBUG_CLEANUP) Slog.v(
4192 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004193 + " with " + i + " entries");
4194 while (i > 0) {
4195 i--;
4196 ActivityRecord r = (ActivityRecord)list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004197 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004198 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004199 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004200 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004201 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004202 }
4203 }
4204 }
4205
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004206 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
4207 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
4208 removeHistoryRecordsForAppLocked(mStoppingActivities, app, "mStoppingActivities");
4209 removeHistoryRecordsForAppLocked(mGoingToSleepActivities, app, "mGoingToSleepActivities");
4210 removeHistoryRecordsForAppLocked(mWaitingVisibleActivities, app,
4211 "mWaitingVisibleActivities");
4212 removeHistoryRecordsForAppLocked(mFinishingActivities, app, "mFinishingActivities");
4213
4214 boolean hasVisibleActivities = false;
4215
4216 // Clean out the history list.
4217 int i = mHistory.size();
4218 if (DEBUG_CLEANUP) Slog.v(
4219 TAG, "Removing app " + app + " from history with " + i + " entries");
4220 while (i > 0) {
4221 i--;
4222 ActivityRecord r = (ActivityRecord)mHistory.get(i);
4223 if (DEBUG_CLEANUP) Slog.v(
4224 TAG, "Record #" + i + " " + r + ": app=" + r.app);
4225 if (r.app == app) {
Dianne Hackborn07981492013-01-28 11:36:23 -08004226 boolean remove;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004227 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
Dianne Hackborn07981492013-01-28 11:36:23 -08004228 // Don't currently have state for the activity, or
4229 // it is finishing -- always remove it.
4230 remove = true;
4231 } else if (r.launchCount > 2 &&
4232 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
4233 // We have launched this activity too many times since it was
4234 // able to run, so give up and remove it.
4235 remove = true;
4236 } else {
4237 // The process may be gone, but the activity lives on!
4238 remove = false;
4239 }
4240 if (remove) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004241 if (ActivityStack.DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
4242 RuntimeException here = new RuntimeException("here");
4243 here.fillInStackTrace();
4244 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
4245 + ": haveState=" + r.haveState
4246 + " stateNotNeeded=" + r.stateNotNeeded
4247 + " finishing=" + r.finishing
4248 + " state=" + r.state, here);
4249 }
4250 if (!r.finishing) {
4251 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
4252 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
4253 r.userId, System.identityHashCode(r),
4254 r.task.taskId, r.shortComponentName,
4255 "proc died without state saved");
4256 }
4257 removeActivityFromHistoryLocked(r);
4258
4259 } else {
4260 // We have the current state for this activity, so
4261 // it can be restarted later when needed.
4262 if (localLOGV) Slog.v(
4263 TAG, "Keeping entry, setting app to null");
4264 if (r.visible) {
4265 hasVisibleActivities = true;
4266 }
Dianne Hackborn07981492013-01-28 11:36:23 -08004267 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
4268 + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004269 r.app = null;
4270 r.nowVisible = false;
4271 if (!r.haveState) {
4272 if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG,
4273 "App died, clearing saved state of " + r);
4274 r.icicle = null;
4275 }
4276 }
4277
4278 r.stack.cleanUpActivityLocked(r, true, true);
4279 }
4280 }
4281
4282 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004283 }
4284
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004285 /**
4286 * Move the current home activity's task (if one exists) to the front
4287 * of the stack.
4288 */
4289 final void moveHomeToFrontLocked() {
4290 TaskRecord homeTask = null;
4291 for (int i=mHistory.size()-1; i>=0; i--) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004292 ActivityRecord hr = mHistory.get(i);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004293 if (hr.isHomeActivity) {
4294 homeTask = hr.task;
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08004295 break;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004296 }
4297 }
4298 if (homeTask != null) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004299 moveTaskToFrontLocked(homeTask, null, null);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004300 }
4301 }
4302
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004303 final void updateTransitLocked(int transit, Bundle options) {
4304 if (options != null) {
4305 ActivityRecord r = topRunningActivityLocked(null);
4306 if (r != null && r.state != ActivityState.RESUMED) {
4307 r.updateOptionsLocked(options);
4308 } else {
4309 ActivityOptions.abort(options);
4310 }
4311 }
4312 mService.mWindowManager.prepareAppTransition(transit, false);
4313 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004314
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004315 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004316 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
4317
4318 final int task = tr.taskId;
4319 int top = mHistory.size()-1;
4320
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004321 if (top < 0 || (mHistory.get(top)).task.taskId == task) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004322 // nothing to do!
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004323 if (reason != null &&
4324 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
4325 ActivityOptions.abort(options);
4326 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08004327 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004328 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004329 return;
4330 }
4331
Dianne Hackbornbe707852011-11-11 14:32:10 -08004332 ArrayList<IBinder> moved = new ArrayList<IBinder>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004333
4334 // Applying the affinities may have removed entries from the history,
4335 // so get the size again.
4336 top = mHistory.size()-1;
4337 int pos = top;
4338
4339 // Shift all activities with this task up to the top
4340 // of the stack, keeping them in the same internal order.
4341 while (pos >= 0) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004342 ActivityRecord r = mHistory.get(pos);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004343 if (localLOGV) Slog.v(
4344 TAG, "At " + pos + " ckp " + r.task + ": " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004345 if (r.task.taskId == task) {
4346 if (localLOGV) Slog.v(TAG, "Removing and adding at " + top);
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07004347 if (DEBUG_ADD_REMOVE) {
4348 RuntimeException here = new RuntimeException("here");
4349 here.fillInStackTrace();
4350 Slog.i(TAG, "Removing and adding activity " + r + " to stack at " + top, here);
4351 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004352 mHistory.remove(pos);
4353 mHistory.add(top, r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08004354 moved.add(0, r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004355 top--;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004356 }
4357 pos--;
4358 }
4359
4360 if (DEBUG_TRANSITION) Slog.v(TAG,
4361 "Prepare to front transition: task=" + tr);
4362 if (reason != null &&
4363 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08004364 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08004365 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004366 ActivityRecord r = topRunningActivityLocked(null);
4367 if (r != null) {
4368 mNoAnimActivities.add(r);
4369 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004370 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004371 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08004372 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004373 }
4374
4375 mService.mWindowManager.moveAppTokensToTop(moved);
4376 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004377 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004378 }
4379
4380 finishTaskMoveLocked(task);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004381 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004382 }
4383
4384 private final void finishTaskMoveLocked(int task) {
4385 resumeTopActivityLocked(null);
4386 }
4387
4388 /**
4389 * Worker method for rearranging history stack. Implements the function of moving all
4390 * activities for a specific task (gathering them if disjoint) into a single group at the
4391 * bottom of the stack.
4392 *
4393 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4394 * to premeptively cancel the move.
4395 *
4396 * @param task The taskId to collect and move to the bottom.
4397 * @return Returns true if the move completed, false if not.
4398 */
4399 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
4400 Slog.i(TAG, "moveTaskToBack: " + task);
4401
4402 // If we have a watcher, preflight the move before committing to it. First check
4403 // for *other* available tasks, but if none are available, then try again allowing the
4404 // current task to be selected.
4405 if (mMainStack && mService.mController != null) {
4406 ActivityRecord next = topRunningActivityLocked(null, task);
4407 if (next == null) {
4408 next = topRunningActivityLocked(null, 0);
4409 }
4410 if (next != null) {
4411 // ask watcher if this is allowed
4412 boolean moveOK = true;
4413 try {
4414 moveOK = mService.mController.activityResuming(next.packageName);
4415 } catch (RemoteException e) {
4416 mService.mController = null;
4417 }
4418 if (!moveOK) {
4419 return false;
4420 }
4421 }
4422 }
4423
Dianne Hackbornbe707852011-11-11 14:32:10 -08004424 ArrayList<IBinder> moved = new ArrayList<IBinder>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004425
4426 if (DEBUG_TRANSITION) Slog.v(TAG,
4427 "Prepare to back transition: task=" + task);
4428
4429 final int N = mHistory.size();
4430 int bottom = 0;
4431 int pos = 0;
4432
4433 // Shift all activities with this task down to the bottom
4434 // of the stack, keeping them in the same internal order.
4435 while (pos < N) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004436 ActivityRecord r = mHistory.get(pos);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004437 if (localLOGV) Slog.v(
4438 TAG, "At " + pos + " ckp " + r.task + ": " + r);
4439 if (r.task.taskId == task) {
4440 if (localLOGV) Slog.v(TAG, "Removing and adding at " + (N-1));
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07004441 if (DEBUG_ADD_REMOVE) {
4442 RuntimeException here = new RuntimeException("here");
4443 here.fillInStackTrace();
4444 Slog.i(TAG, "Removing and adding activity " + r + " to stack at "
4445 + bottom, here);
4446 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004447 mHistory.remove(pos);
4448 mHistory.add(bottom, r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08004449 moved.add(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004450 bottom++;
4451 }
4452 pos++;
4453 }
4454
4455 if (reason != null &&
4456 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08004457 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08004458 AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004459 ActivityRecord r = topRunningActivityLocked(null);
4460 if (r != null) {
4461 mNoAnimActivities.add(r);
4462 }
4463 } else {
Dianne Hackborn7da6ac32010-12-09 19:22:04 -08004464 mService.mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08004465 AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004466 }
4467 mService.mWindowManager.moveAppTokensToBottom(moved);
4468 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004469 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004470 }
4471
4472 finishTaskMoveLocked(task);
4473 return true;
4474 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004475
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004476 public ActivityManager.TaskThumbnails getTaskThumbnailsLocked(TaskRecord tr) {
4477 TaskAccessInfo info = getTaskAccessInfoLocked(tr.taskId, true);
4478 ActivityRecord resumed = mResumedActivity;
4479 if (resumed != null && resumed.thumbHolder == tr) {
4480 info.mainThumbnail = resumed.stack.screenshotActivities(resumed);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004481 }
Dianne Hackborn6a864952012-09-21 18:46:11 -07004482 if (info.mainThumbnail == null) {
4483 info.mainThumbnail = tr.lastThumbnail;
4484 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004485 return info;
4486 }
4487
Dianne Hackborn15491c62012-09-19 10:59:14 -07004488 public Bitmap getTaskTopThumbnailLocked(TaskRecord tr) {
4489 ActivityRecord resumed = mResumedActivity;
4490 if (resumed != null && resumed.task == tr) {
4491 // This task is the current resumed task, we just need to take
4492 // a screenshot of it and return that.
4493 return resumed.stack.screenshotActivities(resumed);
4494 }
4495 // Return the information about the task, to figure out the top
4496 // thumbnail to return.
4497 TaskAccessInfo info = getTaskAccessInfoLocked(tr.taskId, true);
4498 if (info.numSubThumbbails <= 0) {
Dianne Hackborn6a864952012-09-21 18:46:11 -07004499 return info.mainThumbnail != null ? info.mainThumbnail : tr.lastThumbnail;
Dianne Hackborn15491c62012-09-19 10:59:14 -07004500 } else {
4501 return info.subtasks.get(info.numSubThumbbails-1).holder.lastThumbnail;
4502 }
4503 }
4504
Dianne Hackborn9da2d402012-03-15 13:43:08 -07004505 public ActivityRecord removeTaskActivitiesLocked(int taskId, int subTaskIndex,
4506 boolean taskRequired) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004507 TaskAccessInfo info = getTaskAccessInfoLocked(taskId, false);
4508 if (info.root == null) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07004509 if (taskRequired) {
4510 Slog.w(TAG, "removeTaskLocked: unknown taskId " + taskId);
4511 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004512 return null;
4513 }
4514
4515 if (subTaskIndex < 0) {
4516 // Just remove the entire task.
4517 performClearTaskAtIndexLocked(taskId, info.rootIndex);
4518 return info.root;
4519 }
4520
4521 if (subTaskIndex >= info.subtasks.size()) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07004522 if (taskRequired) {
4523 Slog.w(TAG, "removeTaskLocked: unknown subTaskIndex " + subTaskIndex);
4524 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004525 return null;
4526 }
4527
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004528 // Remove all of this task's activities starting at the sub task.
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004529 TaskAccessInfo.SubTask subtask = info.subtasks.get(subTaskIndex);
4530 performClearTaskAtIndexLocked(taskId, subtask.index);
4531 return subtask.activity;
4532 }
4533
4534 public TaskAccessInfo getTaskAccessInfoLocked(int taskId, boolean inclThumbs) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004535 final TaskAccessInfo thumbs = new TaskAccessInfo();
4536 // How many different sub-thumbnails?
4537 final int NA = mHistory.size();
4538 int j = 0;
4539 ThumbnailHolder holder = null;
4540 while (j < NA) {
4541 ActivityRecord ar = mHistory.get(j);
4542 if (!ar.finishing && ar.task.taskId == taskId) {
Dianne Hackborn8da429e2012-09-23 12:52:19 -07004543 thumbs.root = ar;
4544 thumbs.rootIndex = j;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004545 holder = ar.thumbHolder;
Dianne Hackborn15491c62012-09-19 10:59:14 -07004546 if (holder != null) {
4547 thumbs.mainThumbnail = holder.lastThumbnail;
4548 }
4549 j++;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004550 break;
4551 }
4552 j++;
4553 }
4554
4555 if (j >= NA) {
4556 return thumbs;
4557 }
4558
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004559 ArrayList<TaskAccessInfo.SubTask> subtasks = new ArrayList<TaskAccessInfo.SubTask>();
4560 thumbs.subtasks = subtasks;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004561 while (j < NA) {
4562 ActivityRecord ar = mHistory.get(j);
4563 j++;
4564 if (ar.finishing) {
4565 continue;
4566 }
4567 if (ar.task.taskId != taskId) {
4568 break;
4569 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004570 if (ar.thumbHolder != holder && holder != null) {
4571 thumbs.numSubThumbbails++;
4572 holder = ar.thumbHolder;
4573 TaskAccessInfo.SubTask sub = new TaskAccessInfo.SubTask();
Dianne Hackborn15491c62012-09-19 10:59:14 -07004574 sub.holder = holder;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004575 sub.activity = ar;
4576 sub.index = j-1;
4577 subtasks.add(sub);
4578 }
4579 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004580 if (thumbs.numSubThumbbails > 0) {
4581 thumbs.retriever = new IThumbnailRetriever.Stub() {
4582 public Bitmap getThumbnail(int index) {
4583 if (index < 0 || index >= thumbs.subtasks.size()) {
4584 return null;
4585 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004586 TaskAccessInfo.SubTask sub = thumbs.subtasks.get(index);
4587 ActivityRecord resumed = mResumedActivity;
4588 if (resumed != null && resumed.thumbHolder == sub.holder) {
4589 return resumed.stack.screenshotActivities(resumed);
4590 }
4591 return sub.holder.lastThumbnail;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004592 }
4593 };
4594 }
4595 return thumbs;
4596 }
4597
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004598 private final void logStartActivity(int tag, ActivityRecord r,
4599 TaskRecord task) {
4600 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004601 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004602 r.shortComponentName, r.intent.getAction(),
4603 r.intent.getType(), r.intent.getDataString(),
4604 r.intent.getFlags());
4605 }
4606
4607 /**
4608 * Make sure the given activity matches the current configuration. Returns
4609 * false if the activity had to be destroyed. Returns true if the
4610 * configuration is the same, or the activity will remain running as-is
4611 * for whatever reason. Ensures the HistoryRecord is updated with the
4612 * correct configuration and all other bookkeeping is handled.
4613 */
4614 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
4615 int globalChanges) {
4616 if (mConfigWillChange) {
4617 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4618 "Skipping config check (will change): " + r);
4619 return true;
4620 }
4621
4622 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4623 "Ensuring correct configuration: " + r);
4624
4625 // Short circuit: if the two configurations are the exact same
4626 // object (the common case), then there is nothing to do.
4627 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004628 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004629 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4630 "Configuration unchanged in " + r);
4631 return true;
4632 }
4633
4634 // We don't worry about activities that are finishing.
4635 if (r.finishing) {
4636 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4637 "Configuration doesn't matter in finishing " + r);
4638 r.stopFreezingScreenLocked(false);
4639 return true;
4640 }
4641
4642 // Okay we now are going to make this activity have the new config.
4643 // But then we need to figure out how it needs to deal with that.
4644 Configuration oldConfig = r.configuration;
4645 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004646
4647 // Determine what has changed. May be nothing, if this is a config
4648 // that has come back from the app after going idle. In that case
4649 // we just want to leave the official config object now in the
4650 // activity and do nothing else.
4651 final int changes = oldConfig.diff(newConfig);
4652 if (changes == 0 && !r.forceNewConfig) {
4653 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4654 "Configuration no differences in " + r);
4655 return true;
4656 }
4657
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004658 // If the activity isn't currently running, just leave the new
4659 // configuration and it will pick that up next time it starts.
4660 if (r.app == null || r.app.thread == null) {
4661 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
4662 "Configuration doesn't matter not running " + r);
4663 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004664 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004665 return true;
4666 }
4667
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004668 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004669 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
4670 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
4671 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07004672 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004673 + ", newConfig=" + newConfig);
4674 }
Dianne Hackborne6676352011-06-01 16:51:20 -07004675 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004676 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4677 r.configChangeFlags |= changes;
4678 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004679 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004680 if (r.app == null || r.app.thread == null) {
4681 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004682 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07004683 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004684 } else if (r.state == ActivityState.PAUSING) {
4685 // A little annoying: we are waiting for this activity to
4686 // finish pausing. Let's not do anything now, but just
4687 // flag that it needs to be restarted when done pausing.
4688 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004689 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004690 r.configDestroy = true;
4691 return true;
4692 } else if (r.state == ActivityState.RESUMED) {
4693 // Try to optimize this case: the configuration is changing
4694 // and we need to restart the top, resumed activity.
4695 // Instead of doing the normal handshaking, just say
4696 // "restart!".
4697 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004698 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004699 relaunchActivityLocked(r, r.configChangeFlags, true);
4700 r.configChangeFlags = 0;
4701 } else {
4702 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004703 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004704 relaunchActivityLocked(r, r.configChangeFlags, false);
4705 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004706 }
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004707
4708 // All done... tell the caller we weren't able to keep this
4709 // activity around.
4710 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004711 }
4712
4713 // Default case: the activity can handle this new configuration, so
4714 // hand it over. Note that we don't need to give it the new
4715 // configuration, since we always send configuration changes to all
4716 // process when they happen so it can just use whatever configuration
4717 // it last got.
4718 if (r.app != null && r.app.thread != null) {
4719 try {
4720 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08004721 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004722 } catch (RemoteException e) {
4723 // If process died, whatever.
4724 }
4725 }
4726 r.stopFreezingScreenLocked(false);
4727
4728 return true;
4729 }
4730
4731 private final boolean relaunchActivityLocked(ActivityRecord r,
4732 int changes, boolean andResume) {
4733 List<ResultInfo> results = null;
4734 List<Intent> newIntents = null;
4735 if (andResume) {
4736 results = r.results;
4737 newIntents = r.newIntents;
4738 }
4739 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
4740 + " with results=" + results + " newIntents=" + newIntents
4741 + " andResume=" + andResume);
4742 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004743 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004744 r.task.taskId, r.shortComponentName);
4745
4746 r.startFreezingScreenLocked(r.app, 0);
4747
4748 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004749 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
4750 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
4751 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004752 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08004753 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08004754 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004755 // Note: don't need to call pauseIfSleepingLocked() here, because
4756 // the caller will only pass in 'andResume' if this activity is
4757 // currently resumed, which implies we aren't sleeping.
4758 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004759 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004760 }
4761
4762 if (andResume) {
4763 r.results = null;
4764 r.newIntents = null;
4765 if (mMainStack) {
4766 mService.reportResumedActivityLocked(r);
4767 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004768 r.state = ActivityState.RESUMED;
4769 } else {
4770 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4771 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004772 }
4773
4774 return true;
4775 }
Dianne Hackborn90c52de2011-09-23 12:57:44 -07004776
4777 public void dismissKeyguardOnNextActivityLocked() {
4778 mDismissKeyguardOnNextActivity = true;
4779 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004780}