blob: b3041f2618e5691c04309554352078fc799d7b8f [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
Craig Mautner0eea92c2013-05-16 13:35:39 -070019import static com.android.server.am.ActivityManagerService.TAG;
20import static com.android.server.am.ActivityManagerService.localLOGV;
21import static com.android.server.am.ActivityManagerService.DEBUG_CLEANUP;
22import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
23import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
24import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
25import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
26import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
27import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
28import static com.android.server.am.ActivityManagerService.DEBUG_TRANSITION;
29import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
30import static com.android.server.am.ActivityManagerService.DEBUG_VISBILITY;
31import static com.android.server.am.ActivityManagerService.VALIDATE_TOKENS;
32
33import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE;
34import static com.android.server.am.ActivityStackSupervisor.DEBUG_APP;
35import static com.android.server.am.ActivityStackSupervisor.DEBUG_SAVED_STATE;
36import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
Craig Mautnerde4ef022013-04-07 19:01:33 -070037import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
38
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070039import com.android.internal.os.BatteryStatsImpl;
Craig Mautnerd74f7d72013-02-26 13:41:02 -080040import com.android.internal.util.Objects;
Kenny Rootadd58212013-05-07 09:47:34 -070041import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080042import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4b71aa12012-12-27 17:20:01 -080043import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080044import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070045import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070046
47import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070048import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070049import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070050import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080051import android.app.IActivityController;
Craig Mautnercae015f2013-02-08 14:31:27 -080052import android.app.IThumbnailReceiver;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080054import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055import android.content.ComponentName;
56import android.content.Context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080061import android.content.res.Resources;
62import android.graphics.Bitmap;
Craig Mautnerb12428a2012-12-20 16:07:06 -080063import android.graphics.Bitmap.Config;
Santos Cordon73ff7d82013-03-06 17:24:11 -080064import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070066import android.os.Bundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070067import android.os.Handler;
68import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090069import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070070import android.os.Message;
71import android.os.PowerManager;
72import android.os.RemoteException;
73import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070074import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070077import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078
Craig Mautnercae015f2013-02-08 14:31:27 -080079import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080080import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070081import java.lang.ref.WeakReference;
82import java.util.ArrayList;
83import java.util.Iterator;
84import java.util.List;
85
86/**
87 * State and management of a single stack of activities.
88 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070089final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080090
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070091 // Ticks during which we check progress while waiting for an app to launch.
92 static final int LAUNCH_TICK = 500;
93
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070094 // How long we wait until giving up on the last activity to pause. This
95 // is short because it directly impacts the responsiveness of starting the
96 // next activity.
97 static final int PAUSE_TIMEOUT = 500;
98
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070099 // How long we wait for the activity to tell us it has stopped before
100 // giving up. This is a good amount of time because we really need this
101 // from the application in order to get its saved state.
102 static final int STOP_TIMEOUT = 10*1000;
103
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700104 // How long we can hold the launch wake lock before giving up.
105 static final int LAUNCH_TIMEOUT = 10*1000;
106
107 // How long we wait until giving up on an activity telling us it has
108 // finished destroying itself.
109 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800110
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700111 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800112 // disabled.
113 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800114
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700115 // How long between activity launches that we consider safe to not warn
116 // the user about an unexpected activity being launched on top.
117 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800118
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700119 // Set to false to disable the preview that is shown while a new activity
120 // is being started.
121 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800122
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700123 enum ActivityState {
124 INITIALIZING,
125 RESUMED,
126 PAUSING,
127 PAUSED,
128 STOPPING,
129 STOPPED,
130 FINISHING,
131 DESTROYING,
132 DESTROYED
133 }
134
135 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700136 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800137
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700138 final Context mContext;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800139
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700140 /**
141 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800142 * running) activities. It contains #TaskRecord objects.
143 */
144 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
145
146 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800147 * Used for validating app tokens with window manager.
148 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800149 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800150
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700151 /**
152 * List of running activities, sorted by recent usage.
153 * The first entry in the list is the least recently used.
154 * It contains HistoryRecord objects.
155 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700156 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700157
158 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700159 * Animations that for the current transition have requested not to
160 * be considered for the transition animation.
161 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700162 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700163
164 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700165 * We don't want to allow the device to go to sleep while in the process
166 * of launching an activity. This is primarily to allow alarm intent
167 * receivers to launch an activity and get that to run before the device
168 * goes back to sleep.
169 */
170 final PowerManager.WakeLock mLaunchingActivity;
171
172 /**
173 * When we are in the process of pausing an activity, before starting the
174 * next one, this variable holds the activity that is currently being paused.
175 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800176 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700177
178 /**
179 * This is the last activity that we put into the paused state. This is
180 * used to determine if we need to do an activity transition while sleeping,
181 * when we normally hold the top activity paused.
182 */
183 ActivityRecord mLastPausedActivity = null;
184
185 /**
186 * Current activity that is resumed, or null if there is none.
187 */
188 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800189
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700190 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700191 * This is the last activity that has been started. It is only used to
192 * identify when multiple activities are started at once so that the user
193 * can be warned they may not be in the activity they think they are.
194 */
195 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800196
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700197 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700198 * Set when we know we are going to be calling updateConfiguration()
199 * soon, so want to skip intermediate config checks.
200 */
201 boolean mConfigWillChange;
202
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700203 long mInitialStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800204
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800205 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800206 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
207 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
208 */
209 private ActivityRecord mLastScreenshotActivity = null;
210 private Bitmap mLastScreenshotBitmap = null;
211
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800212 int mThumbnailWidth = -1;
213 int mThumbnailHeight = -1;
214
Craig Mautner858d8a62013-04-23 17:08:34 -0700215 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700216
Craig Mautnerc00204b2013-03-05 15:02:14 -0800217 final int mStackId;
218
Craig Mautner27084302013-03-25 08:05:25 -0700219 /** Run all ActivityStacks through this */
220 final ActivityStackSupervisor mStackSupervisor;
221
Craig Mautner0eea92c2013-05-16 13:35:39 -0700222 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG;
223 static final int LAUNCH_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
224 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
225 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
226 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
227 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700228
229 static class ScheduleDestroyArgs {
230 final ProcessRecord mOwner;
231 final boolean mOomAdj;
232 final String mReason;
233 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
234 mOwner = owner;
235 mOomAdj = oomAdj;
236 mReason = reason;
237 }
238 }
239
Zoran Marcetaf958b322012-08-09 20:27:12 +0900240 final Handler mHandler;
241
242 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700243 //public Handler() {
244 // if (localLOGV) Slog.v(TAG, "Handler started!");
245 //}
Zoran Marcetaf958b322012-08-09 20:27:12 +0900246 public ActivityStackHandler(Looper looper) {
247 super(looper);
248 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700249
Zoran Marcetaf958b322012-08-09 20:27:12 +0900250 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700251 public void handleMessage(Message msg) {
252 switch (msg.what) {
253 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800254 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700255 // We don't at this point know if the activity is fullscreen,
256 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800257 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700258 synchronized (mService) {
259 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700260 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700261 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700262 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800263 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700264 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700265 case LAUNCH_TICK_MSG: {
266 ActivityRecord r = (ActivityRecord)msg.obj;
267 synchronized (mService) {
268 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700269 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700270 }
271 }
272 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700273 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800274 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700275 // We don't at this point know if the activity is fullscreen,
276 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800277 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800278 synchronized (mService) {
279 activityDestroyedLocked(r != null ? r.appToken : null);
280 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700281 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700282 case LAUNCH_TIMEOUT_MSG: {
283 if (mService.mDidDexOpt) {
284 mService.mDidDexOpt = false;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700285 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700286 return;
287 }
288 synchronized (mService) {
289 if (mLaunchingActivity.isHeld()) {
290 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
291 mLaunchingActivity.release();
292 }
293 }
294 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700295 case STOP_TIMEOUT_MSG: {
296 ActivityRecord r = (ActivityRecord)msg.obj;
297 // We don't at this point know if the activity is fullscreen,
298 // so we need to be conservative and assume it isn't.
299 Slog.w(TAG, "Activity stop timeout for " + r);
300 synchronized (mService) {
301 if (r.isInHistory()) {
302 activityStoppedLocked(r, null, null, null);
303 }
304 }
305 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700306 case DESTROY_ACTIVITIES_MSG: {
307 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
308 synchronized (mService) {
309 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
310 }
311 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700312 }
313 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800314 }
315
Craig Mautner000f0022013-02-26 15:04:29 -0800316 private int numActivities() {
317 int count = 0;
318 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
319 count += mTaskHistory.get(taskNdx).mActivities.size();
320 }
321 return count;
322 }
323
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700324 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900325 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700326 mService = service;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700327 mWindowManager = service.mWindowManager;
328 mStackSupervisor = service.mStackSupervisor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700329 mContext = context;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700330 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700331 mLaunchingActivity =
332 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700333 mLaunchingActivity.setReferenceCounted(false);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800334 mStackId = stackId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700335 mCurrentUser = service.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700336 }
Craig Mautner5962b122012-10-05 14:45:52 -0700337
338 private boolean okToShow(ActivityRecord r) {
339 return r.userId == mCurrentUser
340 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
341 }
342
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700343 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800344 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
345 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800346 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800347 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
348 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800349 if (!r.finishing && r != notTop && okToShow(r)) {
350 return r;
351 }
352 }
353 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700354 return null;
355 }
356
357 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800358 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
359 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800360 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800361 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
362 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800363 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
364 return r;
365 }
366 }
367 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700368 return null;
369 }
370
371 /**
372 * This is a simplified version of topRunningActivityLocked that provides a number of
373 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800374 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700375 * @param token If non-null, any history records matching this token will be skipped.
376 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800377 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700378 * @return Returns the HistoryRecord of the next activity on the stack.
379 */
380 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800381 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
382 TaskRecord task = mTaskHistory.get(taskNdx);
383 if (task.taskId == taskId) {
384 continue;
385 }
386 ArrayList<ActivityRecord> activities = task.mActivities;
387 for (int i = activities.size() - 1; i >= 0; --i) {
388 final ActivityRecord r = activities.get(i);
389 // Note: the taskId check depends on real taskId fields being non-zero
390 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
391 return r;
392 }
393 }
394 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700395 return null;
396 }
397
Craig Mautner8849a5e2013-04-02 16:41:03 -0700398 final ActivityRecord topActivity() {
399 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700400 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700401 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
402 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
403 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700404 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700405 }
406 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700407 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700408 }
409
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700410 final TaskRecord topTask() {
411 final int size = mTaskHistory.size();
412 if (size > 0) {
413 return mTaskHistory.get(size - 1);
414 }
415 return null;
416 }
417
Craig Mautnerd2328952013-03-05 12:46:26 -0800418 TaskRecord taskForIdLocked(int id) {
419 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
420 final TaskRecord task = mTaskHistory.get(taskNdx);
421 if (task.taskId == id) {
422 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800423 }
424 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700425 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700426 }
427
Craig Mautnerd2328952013-03-05 12:46:26 -0800428 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800429 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800430 if (r != null) {
431 final TaskRecord task = r.task;
432 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
433 if (task.stack != this) Slog.w(TAG,
434 "Illegal state! task does not point to stack it is in.");
435 return r;
436 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800437 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800438 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800439 }
440
Craig Mautnerf88c50f2013-04-18 19:25:12 -0700441 boolean containsApp(ProcessRecord app) {
442 if (app == null) {
443 return false;
444 }
445 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
446 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
447 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
448 final ActivityRecord r = activities.get(activityNdx);
449 if (r.finishing) {
450 continue;
451 }
452 if (r.app == app) {
453 return true;
454 }
455 }
456 }
457 return false;
458 }
459
Craig Mautner2420ead2013-04-01 17:13:20 -0700460 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700461 final boolean hadit = mLRUActivities.remove(r);
462 mLRUActivities.add(r);
463 return hadit;
464 }
465
Craig Mautnerde4ef022013-04-07 19:01:33 -0700466 final boolean isHomeStack() {
467 return mStackId == HOME_STACK_ID;
468 }
469
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700470 /**
471 * Returns the top activity in any existing task matching the given
472 * Intent. Returns null if no such task is found.
473 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700474 ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700475 ComponentName cls = intent.getComponent();
476 if (info.targetActivity != null) {
477 cls = new ComponentName(info.packageName, info.targetActivity);
478 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700479 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800480
481 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
482 final TaskRecord task = mTaskHistory.get(taskNdx);
483 final ActivityRecord r = task.getTopActivity();
484 if (r == null || r.finishing || r.userId != userId ||
485 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
486 continue;
487 }
488
489 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
490 // + "/aff=" + r.task.affinity + " to new cls="
491 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
492 if (task.affinity != null) {
493 if (task.affinity.equals(info.taskAffinity)) {
494 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700495 return r;
496 }
Craig Mautner000f0022013-02-26 15:04:29 -0800497 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
498 //Slog.i(TAG, "Found matching class!");
499 //dump();
500 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
501 return r;
502 } else if (task.affinityIntent != null
503 && task.affinityIntent.getComponent().equals(cls)) {
504 //Slog.i(TAG, "Found matching class!");
505 //dump();
506 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
507 return r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700508 }
509 }
510
511 return null;
512 }
513
514 /**
515 * Returns the first activity (starting from the top of the stack) that
516 * is the same as the given activity. Returns null if no such activity
517 * is found.
518 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700519 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700520 ComponentName cls = intent.getComponent();
521 if (info.targetActivity != null) {
522 cls = new ComponentName(info.packageName, info.targetActivity);
523 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700524 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700525
Craig Mautner000f0022013-02-26 15:04:29 -0800526 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
527 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
528 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
529 ActivityRecord r = activities.get(activityNdx);
530 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700531 //Slog.i(TAG, "Found matching class!");
532 //dump();
533 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
534 return r;
535 }
536 }
537 }
538
539 return null;
540 }
541
Amith Yamasani742a6712011-05-04 14:49:28 -0700542 /*
Craig Mautner858d8a62013-04-23 17:08:34 -0700543 * Move the activities around in the stack to bring a user to the foreground. This only
544 * matters on the home stack. All other stacks are single user.
Amith Yamasani742a6712011-05-04 14:49:28 -0700545 * @return whether there are any activities for the specified user.
546 */
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700547 final boolean switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800548 if (VALIDATE_TOKENS) {
549 validateAppTokensLocked();
550 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800551 if (mCurrentUser == userId) {
552 return true;
553 }
554 mCurrentUser = userId;
555
556 // Move userId's tasks to the top.
557 boolean haveActivities = false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800558 int index = mTaskHistory.size();
559 for (int i = 0; i < index; ++i) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700560 TaskRecord task = mTaskHistory.get(i);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800561 if (task.userId == userId) {
562 haveActivities = true;
563 mTaskHistory.remove(i);
564 mTaskHistory.add(task);
565 --index;
566 }
567 }
568
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700569 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700570 }
571
Craig Mautner2420ead2013-04-01 17:13:20 -0700572 void minimalResumeActivityLocked(ActivityRecord r) {
573 r.state = ActivityState.RESUMED;
574 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
575 + " (starting new instance)");
576 r.stopped = false;
577 mResumedActivity = r;
578 r.task.touchActiveTime();
579 mService.addRecentTaskLocked(r.task);
580 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700581 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -0700582 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700583 }
584
Craig Mautnere79d42682013-04-01 19:01:53 -0700585 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700586 if (r.launchTime == 0) {
587 r.launchTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700588 if (mInitialStartTime == 0) {
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700589 mInitialStartTime = r.launchTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700590 }
591 } else if (mInitialStartTime == 0) {
592 mInitialStartTime = SystemClock.uptimeMillis();
593 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700594 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800595
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800596 void stopIfSleepingLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700597 if (mLaunchingActivity.isHeld()) {
598 mLaunchingActivity.release();
599 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800600 }
601 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700602
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800603 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800604 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800605 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
606 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
607 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
608 activities.get(activityNdx).setSleeping(false);
609 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800610 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800611 }
612
Craig Mautner0eea92c2013-05-16 13:35:39 -0700613 /**
614 * @return true if something must be done before going to sleep.
615 */
616 boolean checkReadyForSleepLocked() {
617 if (mResumedActivity != null) {
618 // Still have something resumed; can't sleep until it is paused.
619 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
620 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
621 startPausingLocked(false, true);
622 return true;
623 }
624 if (mPausingActivity != null) {
625 // Still waiting for something to pause; can't sleep yet.
626 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
627 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800628 }
629
Craig Mautner0eea92c2013-05-16 13:35:39 -0700630 return false;
631 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800632
Craig Mautner0eea92c2013-05-16 13:35:39 -0700633 void goToSleep() {
634 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800635
Craig Mautner0eea92c2013-05-16 13:35:39 -0700636 // Make sure any stopped but visible activities are now sleeping.
637 // This ensures that the activity's onStop() is called.
638 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
639 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
640 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
641 final ActivityRecord r = activities.get(activityNdx);
642 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
643 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800644 }
645 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800646 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700647 }
Craig Mautner59c00972012-07-30 12:10:24 -0700648
Dianne Hackbornd2835932010-12-13 16:28:46 -0800649 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800650 if (who.noDisplay) {
651 return null;
652 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800653
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800654 Resources res = mService.mContext.getResources();
655 int w = mThumbnailWidth;
656 int h = mThumbnailHeight;
657 if (w < 0) {
658 mThumbnailWidth = w =
659 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
660 mThumbnailHeight = h =
661 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
662 }
663
664 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800665 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
666 || mLastScreenshotBitmap.getWidth() != w
667 || mLastScreenshotBitmap.getHeight() != h) {
668 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700669 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
Craig Mautnerb12428a2012-12-20 16:07:06 -0800670 who.appToken, Display.DEFAULT_DISPLAY, w, h);
671 }
672 if (mLastScreenshotBitmap != null) {
673 return mLastScreenshotBitmap.copy(Config.ARGB_8888, true);
674 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800675 }
676 return null;
677 }
678
Craig Mautnercf910b02013-04-23 11:23:27 -0700679 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800680 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800681 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700682 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800683 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700684 ActivityRecord prev = mResumedActivity;
685 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700686 Slog.e(TAG, "Trying to pause when nothing is resumed",
687 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700688 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700689 return;
690 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700691 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
692 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700693 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800694 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700695 mLastPausedActivity = prev;
696 prev.state = ActivityState.PAUSING;
697 prev.task.touchActiveTime();
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700698 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700699
700 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800701
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700702 if (prev.app != null && prev.app.thread != null) {
703 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
704 try {
705 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700706 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700707 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700708 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800709 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
710 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700711 } catch (Exception e) {
712 // Ignore exception, if process died other code will cleanup.
713 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800714 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700715 mLastPausedActivity = null;
716 }
717 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800718 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700719 mLastPausedActivity = null;
720 }
721
722 // If we are not going to sleep, we want to ensure the device is
723 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700724 if (!mService.isSleepingOrShuttingDown()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700725 mLaunchingActivity.acquire();
726 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
727 // To be safe, don't allow the wake lock to be held for too long.
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700728 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700729 }
730 }
731
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800732 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700733 // Have the window manager pause its key dispatching until the new
734 // activity has started. If we're pausing the activity just because
735 // the screen is being turned off and the UI is sleeping, don't interrupt
736 // key dispatch; the same activity will pick it up again on wakeup.
737 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800738 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700739 } else {
740 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
741 }
742
743 // Schedule a pause timeout in case the app doesn't respond.
744 // We don't give it much time because this directly impacts the
745 // responsiveness seen by the user.
746 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
747 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700748 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700749 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
750 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
751 } else {
752 // This activity failed to schedule the
753 // pause, so just treat it as being paused now.
754 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700755 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700756 }
757 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700758
Craig Mautnerd2328952013-03-05 12:46:26 -0800759 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700760 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800761 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700762
Craig Mautnerd2328952013-03-05 12:46:26 -0800763 final ActivityRecord r = isInStackLocked(token);
764 if (r != null) {
765 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
766 if (mPausingActivity == r) {
767 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
768 + (timeout ? " (due to timeout)" : " (pause complete)"));
769 r.state = ActivityState.PAUSED;
770 completePauseLocked();
771 } else {
772 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
773 r.userId, System.identityHashCode(r), r.shortComponentName,
774 mPausingActivity != null
775 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700776 }
777 }
778 }
779
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700780 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
781 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700782 if (r.state != ActivityState.STOPPING) {
783 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
784 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
785 return;
786 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700787 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700788 if (icicle != null) {
789 // If icicle is null, this is happening due to a timeout, so we
790 // haven't really saved the state.
791 r.icicle = icicle;
792 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800793 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700794 r.updateThumbnail(thumbnail, description);
795 }
796 if (!r.stopped) {
797 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
798 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
799 r.stopped = true;
800 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700801 if (r.finishing) {
802 r.clearOptionsLocked();
803 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700804 if (r.configDestroy) {
805 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700806 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700807 } else {
808 // Now that this process has stopped, we may want to consider
809 // it to be the previous app to try to keep around in case
810 // the user wants to return to it.
811 ProcessRecord fgApp = null;
812 if (mResumedActivity != null) {
813 fgApp = mResumedActivity.app;
814 } else if (mPausingActivity != null) {
815 fgApp = mPausingActivity.app;
816 }
817 if (r.app != null && fgApp != null && r.app != fgApp
818 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
819 && r.app != mService.mHomeProcess) {
820 mService.mPreviousProcess = r.app;
821 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
822 }
Dianne Hackborn50685602011-12-01 12:23:37 -0800823 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700824 }
825 }
826 }
827
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800828 private final void completePauseLocked() {
829 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700830 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800831
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800832 if (prev != null) {
833 if (prev.finishing) {
834 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700835 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800836 } else if (prev.app != null) {
837 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
838 if (prev.waitingVisible) {
839 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700840 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800841 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
842 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800843 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800844 if (prev.configDestroy) {
845 // The previous is being paused because the configuration
846 // is changing, which means it is actually stopping...
847 // To juggle the fact that we are also starting a new
848 // instance right now, we need to first completely stop
849 // the current instance before starting the new one.
850 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
851 destroyActivityLocked(prev, true, false, "pause-config");
852 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700853 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700854 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
855 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800856 // If we already have a few activities waiting to stop,
857 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700858 // them out. Or if r is the last of activity of the last task the stack
859 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800860 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700861 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800862 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700863 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800864 }
865 }
866 } else {
867 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
868 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800869 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800870 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700871 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800872
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700873 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700874 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700875 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800876 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700877 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700878 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700879 if (top == null || (prev != null && top != prev)) {
880 // If there are no more activities available to run,
881 // do resume anyway to start something. Also if the top
882 // activity on the stack is not the just paused activity,
883 // we need to go ahead and resume it to ensure we complete
884 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700885 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700886 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700887 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800888
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800889 if (prev != null) {
890 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700891
Craig Mautner525f3d92013-05-07 14:01:50 -0700892 if (prev.app != null && prev.cpuTimeAtResume > 0
893 && mService.mBatteryStatsService.isOnBattery()) {
894 long diff;
895 synchronized (mService.mProcessStatsThread) {
896 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid)
897 - prev.cpuTimeAtResume;
898 }
899 if (diff > 0) {
900 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
901 synchronized (bsi) {
902 BatteryStatsImpl.Uid.Proc ps =
903 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
904 prev.info.packageName);
905 if (ps != null) {
906 ps.addForegroundTimeLocked(diff);
907 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700908 }
909 }
910 }
Craig Mautner525f3d92013-05-07 14:01:50 -0700911 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700912 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700913 }
914
915 /**
916 * Once we know that we have asked an application to put an activity in
917 * the resumed state (either by launching it or explicitly telling it),
918 * this function updates the rest of our state to match that fact.
919 */
Craig Mautner525f3d92013-05-07 14:01:50 -0700920 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700921 next.idle = false;
922 next.results = null;
923 next.newIntents = null;
924
925 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -0700926 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700927
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700928 mStackSupervisor.reportResumedActivityLocked(next);
929
930 next.resumeKeyDispatchingLocked();
931 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700932
933 // Mark the point when the activity is resuming
934 // TODO: To be more accurate, the mark should be before the onCreate,
935 // not after the onResume. But for subsequent starts, onResume is fine.
936 if (next.app != null) {
937 synchronized (mService.mProcessStatsThread) {
938 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid);
939 }
940 } else {
941 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
942 }
943 }
944
Craig Mautner580ea812013-04-25 12:58:38 -0700945 /**
946 * Version of ensureActivitiesVisible that can easily be called anywhere.
947 */
948 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
949 return ensureActivitiesVisibleLocked(starting, configChanges, false);
950 }
951
952 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
953 boolean forceHomeShown) {
954 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -0700955 return r != null &&
956 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -0700957 }
958
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700959 /**
960 * Make sure that all activities that need to be visible (that is, they
961 * currently can be seen by the user) actually are.
962 */
Craig Mautner580ea812013-04-25 12:58:38 -0700963 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
964 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700965 if (DEBUG_VISBILITY) Slog.v(
966 TAG, "ensureActivitiesVisible behind " + top
967 + " configChanges=0x" + Integer.toHexString(configChanges));
968
969 // If the top activity is not fullscreen, then we need to
970 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800971 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -0700972 boolean showHomeBehindStack = false;
973 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
974 !(forceHomeShown && isHomeStack());
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700975 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerd44711d2013-02-23 11:24:36 -0800976 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
977 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
978 final ActivityRecord r = activities.get(activityNdx);
979 if (r.finishing) {
980 continue;
981 }
982 if (aboveTop && r != top) {
983 continue;
984 }
985 aboveTop = false;
986 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700987 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -0800988 TAG, "Make visible? " + r + " finishing=" + r.finishing
989 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -0800990
Craig Mautnerd44711d2013-02-23 11:24:36 -0800991 final boolean doThisProcess = onlyThisProcess == null
992 || onlyThisProcess.equals(r.processName);
993
994 // First: if this is not the current activity being started, make
995 // sure it matches the current configuration.
996 if (r != starting && doThisProcess) {
997 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700998 }
Craig Mautnerd44711d2013-02-23 11:24:36 -0800999
1000 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001001 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001002 // This activity needs to be visible, but isn't even
1003 // running... get it started, but don't resume it
1004 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001005 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001006 if (r != starting) {
1007 r.startFreezingScreenLocked(r.app, configChanges);
1008 }
1009 if (!r.visible) {
1010 if (DEBUG_VISBILITY) Slog.v(
1011 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001012 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001013 }
1014 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001015 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001016 }
1017 }
1018
1019 } else if (r.visible) {
1020 // If this activity is already visible, then there is nothing
1021 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001022 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001023 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001024
1025 } else if (onlyThisProcess == null) {
1026 // This activity is not currently visible, but is running.
1027 // Tell it to become visible.
1028 r.visible = true;
1029 if (r.state != ActivityState.RESUMED && r != starting) {
1030 // If this activity is paused, tell it
1031 // to now show its window.
1032 if (DEBUG_VISBILITY) Slog.v(
1033 TAG, "Making visible and scheduling visibility: " + r);
1034 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001035 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001036 r.sleeping = false;
1037 r.app.pendingUiClean = true;
1038 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1039 r.stopFreezingScreenLocked(false);
1040 } catch (Exception e) {
1041 // Just skip on any failure; we'll make it
1042 // visible when it next restarts.
1043 Slog.w(TAG, "Exception thrown making visibile: "
1044 + r.intent.getComponent(), e);
1045 }
1046 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001047 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001048
Craig Mautnerd44711d2013-02-23 11:24:36 -08001049 // Aggregate current change flags.
1050 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001051
Craig Mautnerd44711d2013-02-23 11:24:36 -08001052 if (r.fullscreen) {
1053 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001054 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1055 behindFullscreen = true;
1056 } else if (r.mLaunchHomeTaskNext) {
1057 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1058 showHomeBehindStack = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001059 behindFullscreen = true;
1060 }
1061 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001062 if (DEBUG_VISBILITY) Slog.v(
1063 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1064 + " state=" + r.state
1065 + " behindFullscreen=" + behindFullscreen);
1066 // Now for any activities that aren't visible to the user, make
1067 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001068 if (r.visible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001069 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001070 r.visible = false;
1071 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001072 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001073 if ((r.state == ActivityState.STOPPING
1074 || r.state == ActivityState.STOPPED)
1075 && r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001076 if (DEBUG_VISBILITY) Slog.v(TAG, "Scheduling invisibility: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001077 r.app.thread.scheduleWindowVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001078 }
1079 } catch (Exception e) {
1080 // Just skip on any failure; we'll make it
1081 // visible when it next restarts.
1082 Slog.w(TAG, "Exception thrown making hidden: "
1083 + r.intent.getComponent(), e);
1084 }
1085 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001086 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001087 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001088 }
1089 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001090 }
Craig Mautner580ea812013-04-25 12:58:38 -07001091 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001092 }
Craig Mautner58547802013-03-05 08:23:53 -08001093
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001094 /**
1095 * Ensure that the top activity in the stack is resumed.
1096 *
1097 * @param prev The previously resumed activity, for when in the process
1098 * of pausing; can be null to call from elsewhere.
1099 *
1100 * @return Returns true if something is being resumed, or false if
1101 * nothing happened.
1102 */
1103 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001104 return resumeTopActivityLocked(prev, null);
1105 }
1106
1107 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001108 // Find the first activity that is not finishing.
1109 ActivityRecord next = topRunningActivityLocked(null);
1110
1111 // Remember how we'll process this pause/resume situation, and ensure
1112 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001113 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1114 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001115
1116 if (next == null) {
1117 // There are no more activities! Let's just start up the
1118 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001119 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001120 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001121 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001122 }
1123
1124 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001125
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001126 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001127 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1128 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001129 // Make sure we have executed any pending transitions, since there
1130 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001131 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001132 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001133 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001134 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001135 return false;
1136 }
1137
Craig Mautner525f3d92013-05-07 14:01:50 -07001138 final TaskRecord nextTask = next.task;
1139 final TaskRecord prevTask = prev != null ? prev.task : null;
1140 if (prevTask != null && prev.mLaunchHomeTaskNext && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001141 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001142 if (prevTask == nextTask) {
1143 ArrayList<ActivityRecord> activities = prevTask.mActivities;
1144 final int numActivities = activities.size();
1145 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1146 final ActivityRecord r = activities.get(activityNdx);
1147 // r is usually the same as next, but what if two activities were launched
1148 // before prev finished?
1149 if (!r.finishing) {
1150 r.mLaunchHomeTaskNext = true;
1151 r.frontOfTask = true;
1152 break;
1153 }
1154 }
1155 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001156 // This task is going away but it was supposed to return to the home task.
1157 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001158 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001159 mTaskHistory.get(taskNdx).mActivities.get(0).mLaunchHomeTaskNext = true;
1160 } else {
1161 return mStackSupervisor.resumeHomeActivity(prev);
1162 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001163 }
1164
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001165 // If we are sleeping, and there is no resumed activity, and the top
1166 // activity is paused, well that is the state we want.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001167 if ((mService.isSleepingOrShuttingDown())
p13451dbad2872012-04-18 11:39:23 +09001168 && mLastPausedActivity == next
Craig Mautnerde4ef022013-04-07 19:01:33 -07001169 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001170 // Make sure we have executed any pending transitions, since there
1171 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001172 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001173 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001174 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001175 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001176 return false;
1177 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001178
1179 // Make sure that the user who owns this activity is started. If not,
1180 // we will just leave it as is because someone should be bringing
1181 // another user's activities to the top of the stack.
1182 if (mService.mStartedUsers.get(next.userId) == null) {
1183 Slog.w(TAG, "Skipping resume of top activity " + next
1184 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001185 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001186 return false;
1187 }
1188
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001189 // The activity may be waiting for stop, but that is no longer
1190 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001191 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001192 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001193 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001194 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001195
Dianne Hackborn84375872012-06-01 19:03:50 -07001196 next.updateOptionsLocked(options);
1197
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001198 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1199
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001200 // If we are currently pausing an activity, then don't do anything
1201 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001202 if (!mStackSupervisor.allPausedActivitiesComplete()) {
1203 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG, "Skip resume: some activity pausing");
Craig Mautnercf910b02013-04-23 11:23:27 -07001204 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001205 return false;
1206 }
1207
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001208 // Okay we are now going to start a switch, to 'next'. We may first
1209 // have to pause the current activity, but this is an important point
1210 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001211 // XXX "App Redirected" dialog is getting too many false positives
1212 // at this point, so turn off for now.
1213 if (false) {
1214 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1215 long now = SystemClock.uptimeMillis();
1216 final boolean inTime = mLastStartedActivity.startTime != 0
1217 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1218 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1219 final int nextUid = next.info.applicationInfo.uid;
1220 if (inTime && lastUid != nextUid
1221 && lastUid != next.launchedFromUid
1222 && mService.checkPermission(
1223 android.Manifest.permission.STOP_APP_SWITCHES,
1224 -1, next.launchedFromUid)
1225 != PackageManager.PERMISSION_GRANTED) {
1226 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1227 } else {
1228 next.startTime = now;
1229 mLastStartedActivity = next;
1230 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001231 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001232 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001233 mLastStartedActivity = next;
1234 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001235 }
Craig Mautner58547802013-03-05 08:23:53 -08001236
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001237 // We need to start pausing the current activity so the top one
1238 // can be resumed...
Craig Mautnereb957862013-04-24 15:34:32 -07001239 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
1240 if (mResumedActivity != null) {
1241 pausing = true;
1242 startPausingLocked(userLeaving, false);
1243 }
1244 if (pausing) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001245 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001246 // At this point we want to put the upcoming activity's process
1247 // at the top of the LRU list, since we know we will be needing it
1248 // very soon and it would be a waste to let it get killed if it
1249 // happens to be sitting towards the end.
1250 if (next.app != null && next.app.thread != null) {
1251 // No reason to do full oom adj update here; we'll let that
1252 // happen whenever it needs to later.
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001253 mService.updateLruProcessLocked(next.app, false);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001254 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001255 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001256 return true;
1257 }
1258
Christopher Tated3f175c2012-06-14 14:16:54 -07001259 // If the most recent activity was noHistory but was only stopped rather
1260 // than stopped+finished because the device went to sleep, we need to make
1261 // sure to finish it as we're making a new activity topmost.
1262 final ActivityRecord last = mLastPausedActivity;
1263 if (mService.mSleeping && last != null && !last.finishing) {
1264 if ((last.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1265 || (last.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
1266 if (DEBUG_STATES) {
1267 Slog.d(TAG, "no-history finish of " + last + " on new resume");
1268 }
1269 requestFinishActivityLocked(last.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001270 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07001271 }
1272 }
1273
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001274 if (prev != null && prev != next) {
1275 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1276 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001277 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001278 if (DEBUG_SWITCH) Slog.v(
1279 TAG, "Resuming top, waiting visible to hide: " + prev);
1280 } else {
1281 // The next activity is already visible, so hide the previous
1282 // activity's windows right now so we can show the new one ASAP.
1283 // We only do this if the previous is finishing, which should mean
1284 // it is on top of the one being resumed so hiding it quickly
1285 // is good. Otherwise, we want to do the normal route of allowing
1286 // the resumed activity to be shown so we can decide if the
1287 // previous should actually be hidden depending on whether the
1288 // new one is found to be full-screen or not.
1289 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001290 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001291 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1292 + prev + ", waitingVisible="
1293 + (prev != null ? prev.waitingVisible : null)
1294 + ", nowVisible=" + next.nowVisible);
1295 } else {
1296 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1297 + prev + ", waitingVisible="
1298 + (prev != null ? prev.waitingVisible : null)
1299 + ", nowVisible=" + next.nowVisible);
1300 }
1301 }
1302 }
1303
Dianne Hackborne7f97212011-02-24 14:40:20 -08001304 // Launching this app's activity, make sure the app is no longer
1305 // considered stopped.
1306 try {
1307 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001308 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001309 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001310 } catch (IllegalArgumentException e) {
1311 Slog.w(TAG, "Failed trying to unstop package "
1312 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001313 }
1314
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001315 // We are starting up the next activity, so tell the window manager
1316 // that the previous one will be hidden soon. This way it can know
1317 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001318 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001319 if (prev != null) {
1320 if (prev.finishing) {
1321 if (DEBUG_TRANSITION) Slog.v(TAG,
1322 "Prepare close transition: prev=" + prev);
1323 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001324 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001325 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001326 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001327 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001328 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1329 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001330 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001331 mWindowManager.setAppWillBeHidden(prev.appToken);
1332 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001333 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001334 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001335 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001336 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001337 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001338 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001339 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001340 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1341 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001342 }
1343 }
1344 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001345 mWindowManager.setAppWillBeHidden(prev.appToken);
1346 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001347 }
Craig Mautner967212c2013-04-13 21:10:58 -07001348 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001349 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001350 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001351 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001352 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001353 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001354 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001355 }
1356 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001357 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001358 next.applyOptionsLocked();
1359 } else {
1360 next.clearOptionsLocked();
1361 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001362
Craig Mautnercf910b02013-04-23 11:23:27 -07001363 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001364 if (next.app != null && next.app.thread != null) {
1365 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1366
1367 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001368 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001369
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001370 // schedule launch ticks to collect information about slow apps.
1371 next.startLaunchTickingLocked();
1372
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001373 ActivityRecord lastResumedActivity =
1374 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001375 ActivityState lastState = next.state;
1376
1377 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001378
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001379 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001380 next.state = ActivityState.RESUMED;
1381 mResumedActivity = next;
1382 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001383 mService.addRecentTaskLocked(next.task);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001384 mService.updateLruProcessLocked(next.app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001385 updateLRUListLocked(next);
1386
1387 // Have the window manager re-evaluate the orientation of
1388 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001389 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001390 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001391 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001392 mService.mConfiguration,
1393 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1394 if (config != null) {
1395 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001396 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001397 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001398 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001399
Craig Mautner525f3d92013-05-07 14:01:50 -07001400 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001401 // The configuration update wasn't able to keep the existing
1402 // instance of the activity, and instead started a new one.
1403 // We should be all done, but let's just make sure our activity
1404 // is still at the top and schedule another run if something
1405 // weird happened.
1406 ActivityRecord nextNext = topRunningActivityLocked(null);
1407 if (DEBUG_SWITCH) Slog.i(TAG,
1408 "Activity config changed during resume: " + next
1409 + ", new next: " + nextNext);
1410 if (nextNext != next) {
1411 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001412 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001413 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001414 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001415 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001416 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001417 return true;
1418 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001419 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001420 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001421 }
Craig Mautner58547802013-03-05 08:23:53 -08001422
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001423 try {
1424 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001425 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001426 if (a != null) {
1427 final int N = a.size();
1428 if (!next.finishing && N > 0) {
1429 if (DEBUG_RESULTS) Slog.v(
1430 TAG, "Delivering results to " + next
1431 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001432 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001433 }
1434 }
1435
1436 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001437 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001438 }
1439
1440 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001441 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001442 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001443
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001444 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001445 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001446 next.app.pendingUiClean = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001447 next.app.thread.scheduleResumeActivity(next.appToken,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001448 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001449
Craig Mautner0eea92c2013-05-16 13:35:39 -07001450 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001451
1452 } catch (Exception e) {
1453 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001454 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1455 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001456 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001457 if (lastStack != null) {
1458 lastStack.mResumedActivity = lastResumedActivity;
1459 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001460 Slog.i(TAG, "Restarting because process died: " + next);
1461 if (!next.hasBeenLaunched) {
1462 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001463 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1464 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001465 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001466 next.appToken, next.packageName, next.theme,
1467 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
1468 next.nonLocalizedLabel, next.labelRes, next.icon, next.windowFlags,
1469 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001470 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001471 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001472 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001473 return true;
1474 }
1475
1476 // From this point on, if something goes wrong there is no way
1477 // to recover the activity.
1478 try {
1479 next.visible = true;
1480 completeResumeLocked(next);
1481 } catch (Exception e) {
1482 // If any exception gets thrown, toss away this
1483 // activity and try the next one.
1484 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001485 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001486 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001487 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001488 return true;
1489 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001490 next.stopped = false;
1491
1492 } else {
1493 // Whoops, need to restart this activity!
1494 if (!next.hasBeenLaunched) {
1495 next.hasBeenLaunched = true;
1496 } else {
1497 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001498 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001499 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001500 mService.compatibilityInfoForPackageLocked(
1501 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001502 next.nonLocalizedLabel,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001503 next.labelRes, next.icon, next.windowFlags,
1504 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001505 }
1506 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1507 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001508 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001509 }
1510
Craig Mautnercf910b02013-04-23 11:23:27 -07001511 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001512 return true;
1513 }
1514
Craig Mautner11bf9a52013-02-19 14:08:51 -08001515
Craig Mautner8849a5e2013-04-02 16:41:03 -07001516 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001517 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001518 TaskRecord rTask = r.task;
1519 final int taskId = rTask.taskId;
1520 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001521 // Last activity in task had been removed or ActivityManagerService is reusing task.
1522 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001523 // Might not even be in.
Craig Mautnerd2328952013-03-05 12:46:26 -08001524 mTaskHistory.remove(rTask);
Craig Mautner77878772013-03-04 19:46:24 -08001525 // Now put task at top.
Craig Mautnerd2328952013-03-05 12:46:26 -08001526 mTaskHistory.add(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001527 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001528 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001529 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001530 if (!newTask) {
1531 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001532 boolean startIt = true;
1533 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1534 task = mTaskHistory.get(taskNdx);
1535 if (task == r.task) {
1536 // Here it is! Now, if this is not yet visible to the
1537 // user, then just add it without starting; it will
1538 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001539 if (!startIt) {
1540 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1541 + task, new RuntimeException("here").fillInStackTrace());
1542 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001543 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001544 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1545 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001546 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001547 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001548 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001549 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001550 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001551 return;
1552 }
1553 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001554 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001555 startIt = false;
1556 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001557 }
1558 }
1559
1560 // Place a new activity at top of stack, so it is next to interact
1561 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001562
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001563 // If we are not placing the new activity frontmost, we do not want
1564 // to deliver the onUserLeaving callback to the actual frontmost
1565 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001566 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001567 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001568 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1569 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001570 }
Craig Mautner70a86932013-02-28 22:37:44 -08001571
1572 task = r.task;
1573
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001574 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001575 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001576 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001577 task.addActivityToTop(r);
1578
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001579 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001580 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001581 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001582 // We want to show the starting preview window if we are
1583 // switching to a new task, or the next activity's process is
1584 // not currently running.
1585 boolean showStartingIcon = newTask;
1586 ProcessRecord proc = r.app;
1587 if (proc == null) {
1588 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1589 }
1590 if (proc == null || proc.thread == null) {
1591 showStartingIcon = true;
1592 }
1593 if (DEBUG_TRANSITION) Slog.v(TAG,
1594 "Prepare open transition: starting " + r);
1595 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001596 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001597 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001598 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001599 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001600 ? AppTransition.TRANSIT_TASK_OPEN
1601 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001602 mNoAnimActivities.remove(r);
1603 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001604 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001605 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001606 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001607 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001608 boolean doShow = true;
1609 if (newTask) {
1610 // Even though this activity is starting fresh, we still need
1611 // to reset it to make sure we apply affinities to move any
1612 // existing activities from other tasks in to it.
1613 // If the caller has requested that the target task be
1614 // reset, then do so.
1615 if ((r.intent.getFlags()
1616 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1617 resetTaskIfNeededLocked(r, r);
1618 doShow = topRunningNonDelayedActivityLocked(null) == r;
1619 }
1620 }
1621 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1622 // Figure out if we are transitioning from another activity that is
1623 // "has the same starting icon" as the next one. This allows the
1624 // window manager to keep the previous window it had previously
1625 // created, if it still had one.
1626 ActivityRecord prev = mResumedActivity;
1627 if (prev != null) {
1628 // We don't want to reuse the previous starting preview if:
1629 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001630 if (prev.task != r.task) {
1631 prev = null;
1632 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001633 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001634 else if (prev.nowVisible) {
1635 prev = null;
1636 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001637 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001638 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001639 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001640 mService.compatibilityInfoForPackageLocked(
1641 r.info.applicationInfo), r.nonLocalizedLabel,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001642 r.labelRes, r.icon, r.windowFlags,
1643 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001644 }
1645 } else {
1646 // If this is the first activity, don't do any fancy animations,
1647 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001648 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001649 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001650 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001651 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001652 }
1653 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001654 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001655 }
1656
1657 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001658 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001659 }
1660 }
1661
Dianne Hackbornbe707852011-11-11 14:32:10 -08001662 final void validateAppTokensLocked() {
1663 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001664 mValidateAppTokens.ensureCapacity(numActivities());
1665 final int numTasks = mTaskHistory.size();
1666 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1667 TaskRecord task = mTaskHistory.get(taskNdx);
1668 final ArrayList<ActivityRecord> activities = task.mActivities;
1669 if (activities.size() == 0) {
1670 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001671 }
Craig Mautner000f0022013-02-26 15:04:29 -08001672 TaskGroup group = new TaskGroup();
1673 group.taskId = task.taskId;
1674 mValidateAppTokens.add(group);
1675 final int numActivities = activities.size();
1676 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1677 final ActivityRecord r = activities.get(activityNdx);
1678 group.tokens.add(r.appToken);
1679 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001680 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001681 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001682 }
1683
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001684 /**
1685 * Perform a reset of the given task, if needed as part of launching it.
1686 * Returns the new HistoryRecord at the top of the task.
1687 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001688 /**
1689 * Helper method for #resetTaskIfNeededLocked.
1690 * We are inside of the task being reset... we'll either finish this activity, push it out
1691 * for another task, or leave it as-is.
1692 * @param task The task containing the Activity (taskTop) that might be reset.
1693 * @param forceReset
1694 * @return An ActivityOptions that needs to be processed.
1695 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001696 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001697 ActivityOptions topOptions = null;
1698
1699 int replyChainEnd = -1;
1700 boolean canMoveOptions = true;
1701
1702 // We only do this for activities that are not the root of the task (since if we finish
1703 // the root, we may no longer have the task!).
1704 final ArrayList<ActivityRecord> activities = task.mActivities;
1705 final int numActivities = activities.size();
1706 for (int i = numActivities - 1; i > 0; --i ) {
1707 ActivityRecord target = activities.get(i);
1708
1709 final int flags = target.info.flags;
1710 final boolean finishOnTaskLaunch =
1711 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1712 final boolean allowTaskReparenting =
1713 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1714 final boolean clearWhenTaskReset =
1715 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1716
1717 if (!finishOnTaskLaunch
1718 && !clearWhenTaskReset
1719 && target.resultTo != null) {
1720 // If this activity is sending a reply to a previous
1721 // activity, we can't do anything with it now until
1722 // we reach the start of the reply chain.
1723 // XXX note that we are assuming the result is always
1724 // to the previous activity, which is almost always
1725 // the case but we really shouldn't count on.
1726 if (replyChainEnd < 0) {
1727 replyChainEnd = i;
1728 }
1729 } else if (!finishOnTaskLaunch
1730 && !clearWhenTaskReset
1731 && allowTaskReparenting
1732 && target.taskAffinity != null
1733 && !target.taskAffinity.equals(task.affinity)) {
1734 // If this activity has an affinity for another
1735 // task, then we need to move it out of here. We will
1736 // move it as far out of the way as possible, to the
1737 // bottom of the activity stack. This also keeps it
1738 // correctly ordered with any activities we previously
1739 // moved.
1740 TaskRecord bottomTask = mTaskHistory.get(0);
1741 ActivityRecord p = bottomTask.mActivities.get(0);
1742 if (target.taskAffinity != null
1743 && target.taskAffinity.equals(p.task.affinity)) {
1744 // If the activity currently at the bottom has the
1745 // same task affinity as the one we are moving,
1746 // then merge it into the same task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001747 target.setTask(p.task, p.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001748 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1749 + " out to bottom task " + p.task);
1750 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001751 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001752 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001753 target.task.affinityIntent = target.intent;
1754 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1755 + " out to new task " + target.task);
1756 }
1757
1758 final TaskRecord targetTask = target.task;
1759 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001760 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001761
Craig Mautner525f3d92013-05-07 14:01:50 -07001762 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001763 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1764 for (int srcPos = start; srcPos >= i; --srcPos) {
1765 p = activities.get(srcPos);
1766 if (p.finishing) {
1767 continue;
1768 }
1769
Craig Mautner525f3d92013-05-07 14:01:50 -07001770 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001771 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001772 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001773 topOptions = p.takeOptionsLocked();
1774 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001775 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001776 }
1777 }
1778 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1779 + task + " adding to task=" + targetTask,
1780 new RuntimeException("here").fillInStackTrace());
1781 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1782 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001783 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001784 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001785
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001786 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001787 }
1788
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001789 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001790 if (VALIDATE_TOKENS) {
1791 validateAppTokensLocked();
1792 }
1793
1794 replyChainEnd = -1;
1795 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1796 // If the activity should just be removed -- either
1797 // because it asks for it, or the task should be
1798 // cleared -- then finish it and anything that is
1799 // part of its reply chain.
1800 int end;
1801 if (clearWhenTaskReset) {
1802 // In this case, we want to finish this activity
1803 // and everything above it, so be sneaky and pretend
1804 // like these are all in the reply chain.
1805 end = numActivities - 1;
1806 } else if (replyChainEnd < 0) {
1807 end = i;
1808 } else {
1809 end = replyChainEnd;
1810 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001811 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001812 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001813 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001814 if (p.finishing) {
1815 continue;
1816 }
1817 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001818 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001819 topOptions = p.takeOptionsLocked();
1820 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001821 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001822 }
1823 }
Craig Mautner58547802013-03-05 08:23:53 -08001824 if (DEBUG_TASKS) Slog.w(TAG,
1825 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001826 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001827 end--;
1828 srcPos--;
1829 }
1830 }
1831 replyChainEnd = -1;
1832 } else {
1833 // If we were in the middle of a chain, well the
1834 // activity that started it all doesn't want anything
1835 // special, so leave it all as-is.
1836 replyChainEnd = -1;
1837 }
1838 }
1839
1840 return topOptions;
1841 }
1842
1843 /**
1844 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1845 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1846 * @param affinityTask The task we are looking for an affinity to.
1847 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1848 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1849 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1850 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001851 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08001852 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001853 int replyChainEnd = -1;
1854 final int taskId = task.taskId;
1855 final String taskAffinity = task.affinity;
1856
1857 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1858 final int numActivities = activities.size();
1859 // Do not operate on the root Activity.
1860 for (int i = numActivities - 1; i > 0; --i) {
1861 ActivityRecord target = activities.get(i);
1862
1863 final int flags = target.info.flags;
1864 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1865 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1866
1867 if (target.resultTo != null) {
1868 // If this activity is sending a reply to a previous
1869 // activity, we can't do anything with it now until
1870 // we reach the start of the reply chain.
1871 // XXX note that we are assuming the result is always
1872 // to the previous activity, which is almost always
1873 // the case but we really shouldn't count on.
1874 if (replyChainEnd < 0) {
1875 replyChainEnd = i;
1876 }
1877 } else if (topTaskIsHigher
1878 && allowTaskReparenting
1879 && taskAffinity != null
1880 && taskAffinity.equals(target.taskAffinity)) {
1881 // This activity has an affinity for our task. Either remove it if we are
1882 // clearing or move it over to our task. Note that
1883 // we currently punt on the case where we are resetting a
1884 // task that is not at the top but who has activities above
1885 // with an affinity to it... this is really not a normal
1886 // case, and we will need to later pull that task to the front
1887 // and usually at that point we will do the reset and pick
1888 // up those remaining activities. (This only happens if
1889 // someone starts an activity in a new task from an activity
1890 // in a task that is not currently on top.)
1891 if (forceReset || finishOnTaskLaunch) {
1892 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1893 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
1894 for (int srcPos = start; srcPos >= i; --srcPos) {
1895 final ActivityRecord p = activities.get(srcPos);
1896 if (p.finishing) {
1897 continue;
1898 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001899 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001900 }
1901 } else {
Craig Mautner77878772013-03-04 19:46:24 -08001902 if (taskInsertionPoint < 0) {
1903 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08001904
Craig Mautner77878772013-03-04 19:46:24 -08001905 }
Craig Mautner77878772013-03-04 19:46:24 -08001906
1907 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1908 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
1909 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
1910 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001911 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001912 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08001913 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001914
Craig Mautnere3a74d52013-02-22 14:14:58 -08001915 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
1916 + " to stack at " + task,
1917 new RuntimeException("here").fillInStackTrace());
1918 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
1919 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001920 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001921 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001922 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08001923 if (VALIDATE_TOKENS) {
1924 validateAppTokensLocked();
1925 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08001926
1927 // Now we've moved it in to place... but what if this is
1928 // a singleTop activity and we have put it on top of another
1929 // instance of the same activity? Then we drop the instance
1930 // below so it remains singleTop.
1931 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
1932 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001933 int targetNdx = taskActivities.indexOf(target);
1934 if (targetNdx > 0) {
1935 ActivityRecord p = taskActivities.get(targetNdx - 1);
1936 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08001937 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
1938 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001939 }
1940 }
1941 }
1942 }
1943
1944 replyChainEnd = -1;
1945 }
1946 }
Craig Mautner77878772013-03-04 19:46:24 -08001947 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001948 }
1949
Craig Mautner8849a5e2013-04-02 16:41:03 -07001950 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08001951 ActivityRecord newActivity) {
1952 boolean forceReset =
1953 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
1954 if (ACTIVITY_INACTIVE_RESET_TIME > 0
1955 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
1956 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
1957 forceReset = true;
1958 }
1959 }
1960
1961 final TaskRecord task = taskTop.task;
1962
1963 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
1964 * for remaining tasks. Used for later tasks to reparent to task. */
1965 boolean taskFound = false;
1966
1967 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
1968 ActivityOptions topOptions = null;
1969
Craig Mautner77878772013-03-04 19:46:24 -08001970 // Preserve the location for reparenting in the new task.
1971 int reparentInsertionPoint = -1;
1972
Craig Mautnere3a74d52013-02-22 14:14:58 -08001973 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1974 final TaskRecord targetTask = mTaskHistory.get(i);
1975
1976 if (targetTask == task) {
1977 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
1978 taskFound = true;
1979 } else {
Craig Mautner77878772013-03-04 19:46:24 -08001980 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
1981 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001982 }
1983 }
1984
Craig Mautner70a86932013-02-28 22:37:44 -08001985 int taskNdx = mTaskHistory.indexOf(task);
1986 do {
1987 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
1988 } while (taskTop == null && taskNdx >= 0);
1989
Craig Mautnere3a74d52013-02-22 14:14:58 -08001990 if (topOptions != null) {
1991 // If we got some ActivityOptions from an activity on top that
1992 // was removed from the task, propagate them to the new real top.
1993 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001994 taskTop.updateOptionsLocked(topOptions);
1995 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001996 topOptions.abort();
1997 }
1998 }
1999
2000 return taskTop;
2001 }
2002
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002003 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2004 String resultWho, int requestCode, int resultCode, Intent data) {
2005
2006 if (callingUid > 0) {
2007 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002008 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002009 }
2010
2011 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2012 + " : who=" + resultWho + " req=" + requestCode
2013 + " res=" + resultCode + " data=" + data);
2014 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2015 try {
2016 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2017 list.add(new ResultInfo(resultWho, requestCode,
2018 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002019 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002020 return;
2021 } catch (Exception e) {
2022 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2023 }
2024 }
2025
2026 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2027 }
2028
Craig Mautnerf3333272013-04-22 10:55:53 -07002029 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002030 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2031 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2032 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2033 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002034 if (!mService.mSleeping) {
2035 if (DEBUG_STATES) {
2036 Slog.d(TAG, "no-history finish of " + r);
2037 }
2038 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002039 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002040 } else {
2041 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2042 + " on stop because we're just sleeping");
2043 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002044 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002045 }
2046
2047 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002048 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002049 if (mService.mFocusedActivity == r) {
2050 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2051 }
2052 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002053 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002054 try {
2055 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002056 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2057 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002058 r.state = ActivityState.STOPPING;
2059 if (DEBUG_VISBILITY) Slog.v(
2060 TAG, "Stopping visible=" + r.visible + " for " + r);
2061 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002062 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002063 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002064 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002065 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002066 r.setSleeping(true);
2067 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002068 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002069 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002070 } catch (Exception e) {
2071 // Maybe just ignore exceptions here... if the process
2072 // has crashed, our death notification will clean things
2073 // up.
2074 Slog.w(TAG, "Exception thrown during pause", e);
2075 // Just in case, assume it to be stopped.
2076 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002077 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002078 r.state = ActivityState.STOPPED;
2079 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002080 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002081 }
2082 }
2083 }
2084 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002085
Craig Mautner525f3d92013-05-07 14:01:50 -07002086 final ActivityRecord activityIdleInternalLocked(final IBinder token) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002087 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2088
Craig Mautnere79d42682013-04-01 19:01:53 -07002089 // Get the activity record.
Craig Mautner525f3d92013-05-07 14:01:50 -07002090 ActivityRecord res = isInStackLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002091 if (res != null) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002092 // No longer need to keep the device awake.
Craig Mautnerf3333272013-04-22 10:55:53 -07002093 if (mResumedActivity == res && mLaunchingActivity.isHeld()) {
Craig Mautnere79d42682013-04-01 19:01:53 -07002094 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2095 mLaunchingActivity.release();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002096 }
2097
Craig Mautnere79d42682013-04-01 19:01:53 -07002098 // If this activity is fullscreen, set up to hide those under it.
Craig Mautnerf3333272013-04-22 10:55:53 -07002099 if (DEBUG_VISBILITY) Slog.v(TAG, "Idle activity for " + res);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002100 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002101 }
2102
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002103 return res;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002104 }
2105
2106 /**
2107 * @return Returns true if the activity is being finished, false if for
2108 * some reason it is being left as-is.
2109 */
2110 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002111 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002112 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002113 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002114 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002115 + ", result=" + resultCode + ", data=" + resultData
2116 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002117 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002118 return false;
2119 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002120
Craig Mautnerd44711d2013-02-23 11:24:36 -08002121 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002122 return true;
2123 }
2124
Craig Mautnerd2328952013-03-05 12:46:26 -08002125 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002126 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2127 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2128 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2129 ActivityRecord r = activities.get(activityNdx);
2130 if (r.resultTo == self && r.requestCode == requestCode) {
2131 if ((r.resultWho == null && resultWho == null) ||
2132 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2133 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2134 false);
2135 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002136 }
2137 }
2138 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002139 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002140 }
2141
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002142 final void finishTopRunningActivityLocked(ProcessRecord app) {
2143 ActivityRecord r = topRunningActivityLocked(null);
2144 if (r != null && r.app == app) {
2145 // If the top running activity is from this crashing
2146 // process, then terminate it to avoid getting in a loop.
2147 Slog.w(TAG, " Force finishing activity "
2148 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002149 int taskNdx = mTaskHistory.indexOf(r.task);
2150 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002151 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002152 // Also terminate any activities below it that aren't yet
2153 // stopped, to avoid a situation where one will get
2154 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002155 --activityNdx;
2156 if (activityNdx < 0) {
2157 do {
2158 --taskNdx;
2159 if (taskNdx < 0) {
2160 break;
2161 }
2162 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2163 } while (activityNdx < 0);
2164 }
2165 if (activityNdx >= 0) {
2166 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002167 if (r.state == ActivityState.RESUMED
2168 || r.state == ActivityState.PAUSING
2169 || r.state == ActivityState.PAUSED) {
Craig Mautner86d67a42013-05-14 10:34:38 -07002170 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002171 Slog.w(TAG, " Force finishing activity "
2172 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002173 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002174 }
2175 }
2176 }
2177 }
2178 }
2179
Craig Mautnerd2328952013-03-05 12:46:26 -08002180 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002181 ArrayList<ActivityRecord> activities = r.task.mActivities;
2182 for (int index = activities.indexOf(r); index >= 0; --index) {
2183 ActivityRecord cur = activities.get(index);
2184 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002185 break;
2186 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002187 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002188 }
2189 return true;
2190 }
2191
Dianne Hackborn5c607432012-02-28 14:44:19 -08002192 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2193 // send the result
2194 ActivityRecord resultTo = r.resultTo;
2195 if (resultTo != null) {
2196 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2197 + " who=" + r.resultWho + " req=" + r.requestCode
2198 + " res=" + resultCode + " data=" + resultData);
2199 if (r.info.applicationInfo.uid > 0) {
2200 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2201 resultTo.packageName, resultData,
2202 resultTo.getUriPermissionsLocked());
2203 }
2204 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2205 resultData);
2206 r.resultTo = null;
2207 }
2208 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2209
2210 // Make sure this HistoryRecord is not holding on to other resources,
2211 // because clients have remote IPC references to this object so we
2212 // can't assume that will go away and want to avoid circular IPC refs.
2213 r.results = null;
2214 r.pendingResults = null;
2215 r.newIntents = null;
2216 r.icicle = null;
2217 }
2218
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002219 /**
2220 * @return Returns true if this activity has been removed from the history
2221 * list, or false if it is still in the list and will be removed later.
2222 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002223 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2224 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002225 if (r.finishing) {
2226 Slog.w(TAG, "Duplicate finish request for " + r);
2227 return false;
2228 }
2229
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002230 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002231 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002232 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002233 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002234 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2235 final int index = activities.indexOf(r);
2236 if (index < (activities.size() - 1)) {
2237 ActivityRecord next = activities.get(index+1);
2238 if (r.frontOfTask) {
2239 // The next activity is now the front of the task.
2240 next.frontOfTask = true;
2241 }
2242 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2243 // If the caller asked that this activity (and all above it)
2244 // be cleared when the task is reset, don't lose that information,
2245 // but propagate it up to the next activity.
2246 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002247 }
2248 }
2249
2250 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002251 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002252 if (mService.mFocusedActivity == r) {
Craig Mautner29219d92013-04-16 20:19:12 -07002253 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002254 }
2255 }
2256
Dianne Hackborn5c607432012-02-28 14:44:19 -08002257 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002258
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002259 if (mService.mPendingThumbnails.size() > 0) {
2260 // There are clients waiting to receive thumbnails so, in case
2261 // this is an activity that someone is waiting for, add it
2262 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002263 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002264 }
2265
Craig Mautnerde4ef022013-04-07 19:01:33 -07002266 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002267 boolean endTask = index <= 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002268 if (DEBUG_TRANSITION) Slog.v(TAG,
2269 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002270 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002271 ? AppTransition.TRANSIT_TASK_CLOSE
2272 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002273
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002274 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002275 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002276
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002277 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002278 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2279 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2280 startPausingLocked(false, false);
2281 }
2282
2283 } else if (r.state != ActivityState.PAUSING) {
2284 // If the activity is PAUSING, we will complete the finish once
2285 // it is done pausing; else we can just directly finish it here.
2286 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002287 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002288 } else {
2289 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2290 }
2291
2292 return false;
2293 }
2294
Craig Mautnerf3333272013-04-22 10:55:53 -07002295 static final int FINISH_IMMEDIATELY = 0;
2296 static final int FINISH_AFTER_PAUSE = 1;
2297 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002298
Craig Mautnerf3333272013-04-22 10:55:53 -07002299 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002300 // First things first: if this activity is currently visible,
2301 // and the resumed activity is not yet visible, then hold off on
2302 // finishing until the resumed one becomes visible.
2303 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002304 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2305 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002306 if (mStackSupervisor.mStoppingActivities.size() > 3
2307 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002308 // If we already have a few activities waiting to stop,
2309 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002310 // them out. Or if r is the last of activity of the last task the stack
2311 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002312 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002313 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002314 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002315 }
2316 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002317 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2318 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002319 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002320 if (oomAdj) {
2321 mService.updateOomAdjLocked();
2322 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002323 return r;
2324 }
2325
2326 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002327 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002328 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002329 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002330 if (mResumedActivity == r) {
2331 mResumedActivity = null;
2332 }
2333 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002334 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002335 r.state = ActivityState.FINISHING;
2336
2337 if (mode == FINISH_IMMEDIATELY
2338 || prevState == ActivityState.STOPPED
2339 || prevState == ActivityState.INITIALIZING) {
2340 // If this activity is already stopped, we can just finish
2341 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002342 boolean activityRemoved = destroyActivityLocked(r, true,
2343 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002344 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002345 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002346 }
2347 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002348 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002349
2350 // Need to go through the full pause cycle to get this
2351 // activity into the stopped state and then finish it.
2352 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002353 mStackSupervisor.mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002354 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002355 return r;
2356 }
2357
Craig Mautnerd2328952013-03-05 12:46:26 -08002358 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002359 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002360 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002361 final TaskRecord task = srec.task;
2362 final ArrayList<ActivityRecord> activities = task.mActivities;
2363 final int start = activities.indexOf(srec);
2364 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002365 return false;
2366 }
2367 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002368 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002369 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002370 final ComponentName dest = destIntent.getComponent();
2371 if (start > 0 && dest != null) {
2372 for (int i = finishTo; i >= 0; i--) {
2373 ActivityRecord r = activities.get(i);
2374 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002375 r.info.name.equals(dest.getClassName())) {
2376 finishTo = i;
2377 parent = r;
2378 foundParentInTask = true;
2379 break;
2380 }
2381 }
2382 }
2383
2384 IActivityController controller = mService.mController;
2385 if (controller != null) {
2386 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2387 if (next != null) {
2388 // ask watcher if this is allowed
2389 boolean resumeOK = true;
2390 try {
2391 resumeOK = controller.activityResuming(next.packageName);
2392 } catch (RemoteException e) {
2393 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002394 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002395 }
2396
2397 if (!resumeOK) {
2398 return false;
2399 }
2400 }
2401 }
2402 final long origId = Binder.clearCallingIdentity();
2403 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002404 ActivityRecord r = activities.get(i);
2405 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002406 // Only return the supplied result for the first activity finished
2407 resultCode = Activity.RESULT_CANCELED;
2408 resultData = null;
2409 }
2410
2411 if (parent != null && foundParentInTask) {
2412 final int parentLaunchMode = parent.info.launchMode;
2413 final int destIntentFlags = destIntent.getFlags();
2414 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2415 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2416 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2417 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2418 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2419 } else {
2420 try {
2421 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2422 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002423 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002424 null, aInfo, parent.appToken, null,
2425 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2426 0, null, true, null);
2427 foundParentInTask = res == ActivityManager.START_SUCCESS;
2428 } catch (RemoteException e) {
2429 foundParentInTask = false;
2430 }
2431 requestFinishActivityLocked(parent.appToken, resultCode,
2432 resultData, "navigate-up", true);
2433 }
2434 }
2435 Binder.restoreCallingIdentity(origId);
2436 return foundParentInTask;
2437 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002438 /**
2439 * Perform the common clean-up of an activity record. This is called both
2440 * as part of destroyActivityLocked() (when destroying the client-side
2441 * representation) and cleaning things up as a result of its hosting
2442 * processing going away, in which case there is no remaining client-side
2443 * state to destroy so only the cleanup here is needed.
2444 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002445 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2446 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002447 if (mResumedActivity == r) {
2448 mResumedActivity = null;
2449 }
2450 if (mService.mFocusedActivity == r) {
2451 mService.mFocusedActivity = null;
2452 }
2453
2454 r.configDestroy = false;
2455 r.frozenBeforeDestroy = false;
2456
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002457 if (setState) {
2458 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2459 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002460 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002461 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002462 }
2463
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002464 // Make sure this record is no longer in the pending finishes list.
2465 // This could happen, for example, if we are trimming activities
2466 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002467 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002468 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002469
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002470 // Remove any pending results.
2471 if (r.finishing && r.pendingResults != null) {
2472 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2473 PendingIntentRecord rec = apr.get();
2474 if (rec != null) {
2475 mService.cancelIntentSenderLocked(rec, false);
2476 }
2477 }
2478 r.pendingResults = null;
2479 }
2480
2481 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002482 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002483 }
2484
2485 if (mService.mPendingThumbnails.size() > 0) {
2486 // There are clients waiting to receive thumbnails so, in case
2487 // this is an activity that someone is waiting for, add it
2488 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002489 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002490 }
2491
2492 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002493 removeTimeoutsForActivityLocked(r);
2494 }
2495
2496 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002497 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002498 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002499 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002500 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002501 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002502 }
2503
Dianne Hackborn5c607432012-02-28 14:44:19 -08002504 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002505 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2506 r.makeFinishing();
2507 if (DEBUG_ADD_REMOVE) {
2508 RuntimeException here = new RuntimeException("here");
2509 here.fillInStackTrace();
2510 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002511 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002512 final TaskRecord task = r.task;
2513 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002514 if (DEBUG_STACK) Slog.i(TAG,
2515 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002516 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002517 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002518 r.takeFromHistory();
2519 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002520 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002521 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002522 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002523 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002524 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002525 if (VALIDATE_TOKENS) {
2526 validateAppTokensLocked();
2527 }
2528 cleanUpActivityServicesLocked(r);
2529 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002530 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002531
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002532 /**
2533 * Perform clean-up of service connections in an activity record.
2534 */
2535 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2536 // Throw away any services that have been bound by this activity.
2537 if (r.connections != null) {
2538 Iterator<ConnectionRecord> it = r.connections.iterator();
2539 while (it.hasNext()) {
2540 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002541 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002542 }
2543 r.connections = null;
2544 }
2545 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002546
2547 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2548 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2549 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2550 mHandler.sendMessage(msg);
2551 }
2552
Dianne Hackborn28695e02011-11-02 21:59:51 -07002553 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002554 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002555 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002556 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2557 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2558 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2559 final ActivityRecord r = activities.get(activityNdx);
2560 if (r.finishing) {
2561 continue;
2562 }
2563 if (r.fullscreen) {
2564 lastIsOpaque = true;
2565 }
2566 if (owner != null && r.app != owner) {
2567 continue;
2568 }
2569 if (!lastIsOpaque) {
2570 continue;
2571 }
2572 // We can destroy this one if we have its icicle saved and
2573 // it is not in the process of pausing/stopping/finishing.
2574 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2575 && r.haveState && !r.visible && r.stopped
2576 && r.state != ActivityState.DESTROYING
2577 && r.state != ActivityState.DESTROYED) {
2578 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2579 + " resumed=" + mResumedActivity
2580 + " pausing=" + mPausingActivity);
2581 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2582 activityRemoved = true;
2583 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002584 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002585 }
2586 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002587 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002588 mStackSupervisor.resumeTopActivitiesLocked();
2589
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002590 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002591 }
2592
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002593 /**
2594 * Destroy the current CLIENT SIDE instance of an activity. This may be
2595 * called both when actually finishing an activity, or when performing
2596 * a configuration switch where we destroy the current client-side object
2597 * but then create a new client-side object for this same HistoryRecord.
2598 */
2599 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002600 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002601 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002602 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002603 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2604 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002605 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002606 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002607
2608 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002609
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002610 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002611
2612 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002613
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002614 if (hadApp) {
2615 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002616 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002617 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2618 mService.mHeavyWeightProcess = null;
2619 mService.mHandler.sendEmptyMessage(
2620 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2621 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002622 if (r.app.activities.size() == 0) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002623 // No longer have activities, so update oom adj.
2624 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002625 }
2626 }
2627
2628 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002629
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002630 try {
2631 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002632 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002633 r.configChangeFlags);
2634 } catch (Exception e) {
2635 // We can just ignore exceptions here... if the process
2636 // has crashed, our death notification will clean things
2637 // up.
2638 //Slog.w(TAG, "Exception thrown during finish", e);
2639 if (r.finishing) {
2640 removeActivityFromHistoryLocked(r);
2641 removedFromHistory = true;
2642 skipDestroy = true;
2643 }
2644 }
2645
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002646 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002647
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002648 // If the activity is finishing, we need to wait on removing it
2649 // from the list to give it a chance to do its cleanup. During
2650 // that time it may make calls back with its token so we need to
2651 // be able to find it on the list and so we don't want to remove
2652 // it from the list yet. Otherwise, we can just immediately put
2653 // it in the destroyed state since we are not removing it from the
2654 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002655 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002656 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2657 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002658 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002659 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002660 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2661 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002662 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002663 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002664 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002665 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002666 }
2667 } else {
2668 // remove this record from the history.
2669 if (r.finishing) {
2670 removeActivityFromHistoryLocked(r);
2671 removedFromHistory = true;
2672 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002673 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002674 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002675 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002676 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002677 }
2678 }
2679
2680 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002681
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002682 if (!mLRUActivities.remove(r) && hadApp) {
2683 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2684 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002685
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002686 return removedFromHistory;
2687 }
2688
Craig Mautnerd2328952013-03-05 12:46:26 -08002689 final void activityDestroyedLocked(IBinder token) {
2690 final long origId = Binder.clearCallingIdentity();
2691 try {
2692 ActivityRecord r = ActivityRecord.forToken(token);
2693 if (r != null) {
2694 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002695 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002696
2697 if (isInStackLocked(token) != null) {
2698 if (r.state == ActivityState.DESTROYING) {
2699 cleanUpActivityLocked(r, true, false);
2700 removeActivityFromHistoryLocked(r);
2701 }
2702 }
Craig Mautner05d29032013-05-03 13:40:13 -07002703 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002704 } finally {
2705 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002706 }
2707 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002708
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002709 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2710 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002711 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002712 if (DEBUG_CLEANUP) Slog.v(
2713 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002714 + " with " + i + " entries");
2715 while (i > 0) {
2716 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002717 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002718 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002719 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002720 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002721 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002722 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002723 }
2724 }
2725 }
2726
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002727 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2728 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002729 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2730 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002731 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2732 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002733 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002734 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002735 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2736 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002737
2738 boolean hasVisibleActivities = false;
2739
2740 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002741 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002742 if (DEBUG_CLEANUP) Slog.v(
2743 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002744 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2745 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2746 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2747 final ActivityRecord r = activities.get(activityNdx);
2748 --i;
2749 if (DEBUG_CLEANUP) Slog.v(
2750 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2751 if (r.app == app) {
2752 boolean remove;
2753 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2754 // Don't currently have state for the activity, or
2755 // it is finishing -- always remove it.
2756 remove = true;
2757 } else if (r.launchCount > 2 &&
2758 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2759 // We have launched this activity too many times since it was
2760 // able to run, so give up and remove it.
2761 remove = true;
2762 } else {
2763 // The process may be gone, but the activity lives on!
2764 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002765 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002766 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002767 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002768 RuntimeException here = new RuntimeException("here");
2769 here.fillInStackTrace();
2770 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2771 + ": haveState=" + r.haveState
2772 + " stateNotNeeded=" + r.stateNotNeeded
2773 + " finishing=" + r.finishing
2774 + " state=" + r.state, here);
2775 }
2776 if (!r.finishing) {
2777 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2778 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2779 r.userId, System.identityHashCode(r),
2780 r.task.taskId, r.shortComponentName,
2781 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07002782 if (r.state == ActivityState.RESUMED) {
2783 mService.updateUsageStats(r, false);
2784 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002785 }
2786 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002787
Craig Mautner0247fc82013-02-28 14:32:06 -08002788 } else {
2789 // We have the current state for this activity, so
2790 // it can be restarted later when needed.
2791 if (localLOGV) Slog.v(
2792 TAG, "Keeping entry, setting app to null");
2793 if (r.visible) {
2794 hasVisibleActivities = true;
2795 }
2796 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2797 + r);
2798 r.app = null;
2799 r.nowVisible = false;
2800 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002801 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08002802 "App died, clearing saved state of " + r);
2803 r.icicle = null;
2804 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002805 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002806
Craig Mautnerd2328952013-03-05 12:46:26 -08002807 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002808 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002809 }
2810 }
2811
2812 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002813 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002814
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002815 final void updateTransitLocked(int transit, Bundle options) {
2816 if (options != null) {
2817 ActivityRecord r = topRunningActivityLocked(null);
2818 if (r != null && r.state != ActivityState.RESUMED) {
2819 r.updateOptionsLocked(options);
2820 } else {
2821 ActivityOptions.abort(options);
2822 }
2823 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002824 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002825 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002826
Craig Mautnercae015f2013-02-08 14:31:27 -08002827 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002828 final TaskRecord task = taskForIdLocked(taskId);
2829 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08002830 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002831 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002832 }
Craig Mautneraab647e2013-02-28 16:31:36 -08002833 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2834 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07002835 // we'll just indicate that this task returns to the home task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002836 task.mActivities.get(0).mLaunchHomeTaskNext = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08002837 }
2838 moveTaskToFrontLocked(task, null, options);
2839 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002840 }
2841 return false;
2842 }
2843
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002844 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002845 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002846
Craig Mautner11bf9a52013-02-19 14:08:51 -08002847 final int numTasks = mTaskHistory.size();
2848 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07002849 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002850 // nothing to do!
2851 if (reason != null &&
2852 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2853 ActivityOptions.abort(options);
2854 } else {
2855 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
2856 }
2857 return;
2858 }
2859
Craig Mautner86d67a42013-05-14 10:34:38 -07002860 mStackSupervisor.moveHomeStack(isHomeStack());
2861
Craig Mautner11bf9a52013-02-19 14:08:51 -08002862 // Shift all activities with this task up to the top
2863 // of the stack, keeping them in the same internal order.
2864 mTaskHistory.remove(tr);
2865 mTaskHistory.add(tr);
2866
2867 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002868 if (reason != null &&
2869 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002870 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002871 ActivityRecord r = topRunningActivityLocked(null);
2872 if (r != null) {
2873 mNoAnimActivities.add(r);
2874 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002875 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002876 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08002877 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002878 }
Craig Mautner30e2d722013-02-12 11:30:16 -08002879
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002880 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002881
Craig Mautner05d29032013-05-03 13:40:13 -07002882 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08002883 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08002884
2885 if (VALIDATE_TOKENS) {
2886 validateAppTokensLocked();
2887 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002888 }
2889
2890 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002891 * Worker method for rearranging history stack. Implements the function of moving all
2892 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002893 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002894 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002895 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
2896 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002897 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002898 * @param task The taskId to collect and move to the bottom.
2899 * @return Returns true if the move completed, false if not.
2900 */
2901 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
2902 Slog.i(TAG, "moveTaskToBack: " + task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08002903
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002904 // If we have a watcher, preflight the move before committing to it. First check
2905 // for *other* available tasks, but if none are available, then try again allowing the
2906 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002907 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002908 ActivityRecord next = topRunningActivityLocked(null, task);
2909 if (next == null) {
2910 next = topRunningActivityLocked(null, 0);
2911 }
2912 if (next != null) {
2913 // ask watcher if this is allowed
2914 boolean moveOK = true;
2915 try {
2916 moveOK = mService.mController.activityResuming(next.packageName);
2917 } catch (RemoteException e) {
2918 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002919 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002920 }
2921 if (!moveOK) {
2922 return false;
2923 }
2924 }
2925 }
2926
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002927 if (DEBUG_TRANSITION) Slog.v(TAG,
2928 "Prepare to back transition: task=" + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002929
Craig Mautnerd2328952013-03-05 12:46:26 -08002930 final TaskRecord tr = taskForIdLocked(task);
2931 if (tr == null) {
2932 return false;
2933 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002934 mTaskHistory.remove(tr);
2935 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002936
2937 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002938 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2939 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002940 ActivityRecord r = topRunningActivityLocked(null);
2941 if (r != null) {
2942 mNoAnimActivities.add(r);
2943 }
2944 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002945 mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08002946 AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002947 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002948 mWindowManager.moveTaskToBottom(task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08002949
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002950 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002951 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002952 }
2953
Craig Mautnerde4ef022013-04-07 19:01:33 -07002954 if (mResumedActivity != null && mResumedActivity.task == tr &&
2955 mResumedActivity.mLaunchHomeTaskNext) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07002956 // TODO: Can we skip the next line and just pass mResumedAct. to resumeHomeAct.()?
Craig Mautnerde4ef022013-04-07 19:01:33 -07002957 mResumedActivity.mLaunchHomeTaskNext = false;
Craig Mautner69ada552013-04-18 13:51:51 -07002958 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002959 }
2960
Craig Mautner05d29032013-05-03 13:40:13 -07002961 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002962 return true;
2963 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07002964
Craig Mautner8849a5e2013-04-02 16:41:03 -07002965 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002966 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08002967 final Uri data = r.intent.getData();
2968 final String strData = data != null ? data.toSafeString() : null;
2969
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002970 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002971 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002972 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08002973 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002974 }
2975
2976 /**
2977 * Make sure the given activity matches the current configuration. Returns
2978 * false if the activity had to be destroyed. Returns true if the
2979 * configuration is the same, or the activity will remain running as-is
2980 * for whatever reason. Ensures the HistoryRecord is updated with the
2981 * correct configuration and all other bookkeeping is handled.
2982 */
2983 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
2984 int globalChanges) {
2985 if (mConfigWillChange) {
2986 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
2987 "Skipping config check (will change): " + r);
2988 return true;
2989 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002990
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002991 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
2992 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07002993
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002994 // Short circuit: if the two configurations are the exact same
2995 // object (the common case), then there is nothing to do.
2996 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002997 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002998 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
2999 "Configuration unchanged in " + r);
3000 return true;
3001 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003002
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003003 // We don't worry about activities that are finishing.
3004 if (r.finishing) {
3005 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3006 "Configuration doesn't matter in finishing " + r);
3007 r.stopFreezingScreenLocked(false);
3008 return true;
3009 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003010
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003011 // Okay we now are going to make this activity have the new config.
3012 // But then we need to figure out how it needs to deal with that.
3013 Configuration oldConfig = r.configuration;
3014 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003015
3016 // Determine what has changed. May be nothing, if this is a config
3017 // that has come back from the app after going idle. In that case
3018 // we just want to leave the official config object now in the
3019 // activity and do nothing else.
3020 final int changes = oldConfig.diff(newConfig);
3021 if (changes == 0 && !r.forceNewConfig) {
3022 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3023 "Configuration no differences in " + r);
3024 return true;
3025 }
3026
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003027 // If the activity isn't currently running, just leave the new
3028 // configuration and it will pick that up next time it starts.
3029 if (r.app == null || r.app.thread == null) {
3030 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3031 "Configuration doesn't matter not running " + r);
3032 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003033 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003034 return true;
3035 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003036
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003037 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003038 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3039 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3040 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003041 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003042 + ", newConfig=" + newConfig);
3043 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003044 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003045 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3046 r.configChangeFlags |= changes;
3047 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003048 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003049 if (r.app == null || r.app.thread == null) {
3050 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003051 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003052 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003053 } else if (r.state == ActivityState.PAUSING) {
3054 // A little annoying: we are waiting for this activity to
3055 // finish pausing. Let's not do anything now, but just
3056 // flag that it needs to be restarted when done pausing.
3057 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003058 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003059 r.configDestroy = true;
3060 return true;
3061 } else if (r.state == ActivityState.RESUMED) {
3062 // Try to optimize this case: the configuration is changing
3063 // and we need to restart the top, resumed activity.
3064 // Instead of doing the normal handshaking, just say
3065 // "restart!".
3066 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003067 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003068 relaunchActivityLocked(r, r.configChangeFlags, true);
3069 r.configChangeFlags = 0;
3070 } else {
3071 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003072 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003073 relaunchActivityLocked(r, r.configChangeFlags, false);
3074 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003075 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003076
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003077 // All done... tell the caller we weren't able to keep this
3078 // activity around.
3079 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003080 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003081
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003082 // Default case: the activity can handle this new configuration, so
3083 // hand it over. Note that we don't need to give it the new
3084 // configuration, since we always send configuration changes to all
3085 // process when they happen so it can just use whatever configuration
3086 // it last got.
3087 if (r.app != null && r.app.thread != null) {
3088 try {
3089 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003090 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003091 } catch (RemoteException e) {
3092 // If process died, whatever.
3093 }
3094 }
3095 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003096
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003097 return true;
3098 }
3099
3100 private final boolean relaunchActivityLocked(ActivityRecord r,
3101 int changes, boolean andResume) {
3102 List<ResultInfo> results = null;
3103 List<Intent> newIntents = null;
3104 if (andResume) {
3105 results = r.results;
3106 newIntents = r.newIntents;
3107 }
3108 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3109 + " with results=" + results + " newIntents=" + newIntents
3110 + " andResume=" + andResume);
3111 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003112 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003113 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003114
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003115 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003116
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003117 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003118 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3119 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3120 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003121 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003122 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003123 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003124 // Note: don't need to call pauseIfSleepingLocked() here, because
3125 // the caller will only pass in 'andResume' if this activity is
3126 // currently resumed, which implies we aren't sleeping.
3127 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003128 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003129 }
3130
3131 if (andResume) {
3132 r.results = null;
3133 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003134 r.state = ActivityState.RESUMED;
3135 } else {
3136 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3137 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003138 }
3139
3140 return true;
3141 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003142
3143 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003144 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3145 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3146 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3147 final ActivityRecord r = activities.get(activityNdx);
3148 if (r.appToken == token) {
3149 return true;
3150 }
3151 if (r.fullscreen && !r.finishing) {
3152 return false;
3153 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003154 }
3155 }
3156 return true;
3157 }
3158
3159 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003160 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3161 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3162 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3163 final ActivityRecord r = activities.get(activityNdx);
3164 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003165 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003166 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003167 }
3168 }
3169 }
3170
3171 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3172 boolean didSomething = false;
3173 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003174 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3175 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3176 int numActivities = activities.size();
3177 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3178 ActivityRecord r = activities.get(activityNdx);
3179 final boolean samePackage = r.packageName.equals(name)
3180 || (name == null && r.userId == userId);
3181 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3182 && (samePackage || r.task == lastTask)
3183 && (r.app == null || evenPersistent || !r.app.persistent)) {
3184 if (!doit) {
3185 if (r.finishing) {
3186 // If this activity is just finishing, then it is not
3187 // interesting as far as something to stop.
3188 continue;
3189 }
3190 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003191 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003192 didSomething = true;
3193 Slog.i(TAG, " Force finishing activity " + r);
3194 if (samePackage) {
3195 if (r.app != null) {
3196 r.app.removed = true;
3197 }
3198 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003199 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003200 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003201 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3202 true)) {
3203 // r has been deleted from mActivities, accommodate.
3204 --numActivities;
3205 --activityNdx;
3206 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003207 }
3208 }
3209 }
3210 return didSomething;
3211 }
3212
3213 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003214 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003215 ActivityRecord topRecord = null;
Craig Mautneraab647e2013-02-28 16:31:36 -08003216 for (int taskNdx = mTaskHistory.size() - 1; maxNum > 0 && taskNdx >= 0;
3217 --maxNum, --taskNdx) {
3218 final TaskRecord task = mTaskHistory.get(taskNdx);
3219 ActivityRecord r = null;
3220 ActivityRecord top = null;
3221 int numActivities = 0;
3222 int numRunning = 0;
3223 final ArrayList<ActivityRecord> activities = task.mActivities;
3224 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3225 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003226
Craig Mautneraab647e2013-02-28 16:31:36 -08003227 // Initialize state for next task if needed.
3228 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3229 top = r;
3230 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003231 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003232
3233 // Add 'r' into the current task.
3234 numActivities++;
3235 if (r.app != null && r.app.thread != null) {
3236 numRunning++;
3237 }
3238
3239 if (localLOGV) Slog.v(
3240 TAG, r.intent.getComponent().flattenToShortString()
3241 + ": task=" + r.task);
3242 }
3243
3244 RunningTaskInfo ci = new RunningTaskInfo();
3245 ci.id = task.taskId;
3246 ci.baseActivity = r.intent.getComponent();
3247 ci.topActivity = top.intent.getComponent();
3248 if (top.thumbHolder != null) {
3249 ci.description = top.thumbHolder.lastDescription;
3250 }
3251 ci.numActivities = numActivities;
3252 ci.numRunning = numRunning;
3253 //System.out.println(
3254 // "#" + maxNum + ": " + " descr=" + ci.description);
3255 if (receiver != null) {
3256 if (localLOGV) Slog.v(
3257 TAG, "State=" + top.state + "Idle=" + top.idle
3258 + " app=" + top.app
3259 + " thr=" + (top.app != null ? top.app.thread : null));
3260 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3261 if (top.idle && top.app != null && top.app.thread != null) {
3262 topRecord = top;
3263 } else {
3264 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003265 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003266 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003267 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003268 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003269 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003270 }
3271 return topRecord;
3272 }
3273
3274 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003275 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003276 if (DEBUG_SWITCH) Slog.d(
3277 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003278 if (top >= 0) {
3279 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3280 int activityTop = activities.size() - 1;
3281 if (activityTop > 0) {
3282 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3283 "unhandled-back", true);
3284 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003285 }
3286 }
3287
Craig Mautnere79d42682013-04-01 19:01:53 -07003288 void handleAppDiedLocked(ProcessRecord app, boolean restarting) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003289 if (!containsApp(app)) {
3290 return;
3291 }
3292 // TODO: handle the case where an app spans multiple stacks.
Craig Mautnere79d42682013-04-01 19:01:53 -07003293 if (mPausingActivity != null && mPausingActivity.app == app) {
3294 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3295 "App died while pausing: " + mPausingActivity);
3296 mPausingActivity = null;
3297 }
3298 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3299 mLastPausedActivity = null;
3300 }
3301
3302 // Remove this application's activities from active lists.
3303 boolean hasVisibleActivities = removeHistoryRecordsForAppLocked(app);
3304
3305 if (!restarting) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003306 ActivityStack stack = mStackSupervisor.getFocusedStack();
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003307 if (stack == null) {
3308 mStackSupervisor.resumeHomeActivity(null);
Craig Mautner05d29032013-05-03 13:40:13 -07003309 } else if (!mStackSupervisor.resumeTopActivitiesLocked(stack, null, null)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003310 // If there was nothing to resume, and we are not already
3311 // restarting this process, but there is a visible activity that
3312 // is hosted by the process... then make sure all visible
3313 // activities are running, taking care of restarting this
3314 // process.
3315 if (hasVisibleActivities) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003316 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
Craig Mautnere79d42682013-04-01 19:01:53 -07003317 }
3318 }
3319 }
3320 }
3321
Craig Mautnercae015f2013-02-08 14:31:27 -08003322 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003323 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3324 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3325 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3326 final ActivityRecord r = activities.get(activityNdx);
3327 if (r.app == app) {
3328 Slog.w(TAG, " Force finishing activity "
3329 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003330 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003331 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003332 }
3333 }
3334 }
3335
3336 void dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3337 boolean dumpClient, String dumpPackage) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003338 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3339 final TaskRecord task = mTaskHistory.get(taskNdx);
3340 pw.print(" Task "); pw.print(taskNdx); pw.print(": id #"); pw.println(task.taskId);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003341 ActivityStackSupervisor.dumpHistoryList(fd, pw, mTaskHistory.get(taskNdx).mActivities,
Craig Mautneraab647e2013-02-28 16:31:36 -08003342 " ", "Hist", true, !dumpAll, dumpClient, dumpPackage);
3343 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003344 }
3345
3346 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3347 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3348
3349 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003350 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3351 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003352 }
3353 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003354 final int top = mTaskHistory.size() - 1;
3355 if (top >= 0) {
3356 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3357 int listTop = list.size() - 1;
3358 if (listTop >= 0) {
3359 activities.add(list.get(listTop));
3360 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003361 }
3362 } else {
3363 ItemMatcher matcher = new ItemMatcher();
3364 matcher.build(name);
3365
Craig Mautneraab647e2013-02-28 16:31:36 -08003366 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3367 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3368 if (matcher.match(r1, r1.intent.getComponent())) {
3369 activities.add(r1);
3370 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003371 }
3372 }
3373 }
3374
3375 return activities;
3376 }
3377
3378 ActivityRecord restartPackage(String packageName) {
3379 ActivityRecord starting = topRunningActivityLocked(null);
3380
3381 // All activities that came from the package must be
3382 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003383 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3384 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3385 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3386 final ActivityRecord a = activities.get(activityNdx);
3387 if (a.info.packageName.equals(packageName)) {
3388 a.forceNewConfig = true;
3389 if (starting != null && a == starting && a.visible) {
3390 a.startFreezingScreenLocked(starting.app,
3391 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3392 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003393 }
3394 }
3395 }
3396
3397 return starting;
3398 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003399
Craig Mautnerde4ef022013-04-07 19:01:33 -07003400 boolean removeTask(TaskRecord task) {
3401 mTaskHistory.remove(task);
3402 return mTaskHistory.size() == 0;
Craig Mautner0247fc82013-02-28 14:32:06 -08003403 }
3404
Craig Mautnerde4ef022013-04-07 19:01:33 -07003405 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003406 TaskRecord task = new TaskRecord(taskId, info, intent, this);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003407 if (toTop) {
3408 mTaskHistory.add(task);
3409 } else {
3410 mTaskHistory.add(0, task);
3411 }
3412 return task;
3413 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003414
3415 ArrayList<TaskRecord> getAllTasks() {
3416 return new ArrayList<TaskRecord>(mTaskHistory);
3417 }
3418
3419 void moveTask(int taskId, boolean toTop) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003420 final TaskRecord task = mStackSupervisor.anyTaskForIdLocked(taskId);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003421 if (task == null) {
3422 return;
3423 }
3424 task.stack.mTaskHistory.remove(task);
3425 task.stack = this;
3426 if (toTop) {
3427 mTaskHistory.add(task);
3428 } else {
3429 mTaskHistory.add(0, task);
3430 }
3431 }
3432
3433 public int getStackId() {
3434 return mStackId;
3435 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003436
3437 @Override
3438 public String toString() {
3439 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3440 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003441}