blob: 7ecb35df9ddf3bc2432b8c3a16cfc990ed97d255 [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 wait until giving up on an activity telling us it has
105 // finished destroying itself.
106 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800107
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700108 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800109 // disabled.
110 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800111
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700112 // How long between activity launches that we consider safe to not warn
113 // the user about an unexpected activity being launched on top.
114 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800115
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700116 // Set to false to disable the preview that is shown while a new activity
117 // is being started.
118 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800119
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700120 enum ActivityState {
121 INITIALIZING,
122 RESUMED,
123 PAUSING,
124 PAUSED,
125 STOPPING,
126 STOPPED,
127 FINISHING,
128 DESTROYING,
129 DESTROYED
130 }
131
132 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700133 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800134
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700135 final Context mContext;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800136
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700137 /**
138 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800139 * running) activities. It contains #TaskRecord objects.
140 */
141 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
142
143 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800144 * Used for validating app tokens with window manager.
145 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800146 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800147
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700148 /**
149 * List of running activities, sorted by recent usage.
150 * The first entry in the list is the least recently used.
151 * It contains HistoryRecord objects.
152 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700153 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700154
155 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700156 * Animations that for the current transition have requested not to
157 * be considered for the transition animation.
158 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700159 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700160
161 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700162 * When we are in the process of pausing an activity, before starting the
163 * next one, this variable holds the activity that is currently being paused.
164 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800165 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700166
167 /**
168 * This is the last activity that we put into the paused state. This is
169 * used to determine if we need to do an activity transition while sleeping,
170 * when we normally hold the top activity paused.
171 */
172 ActivityRecord mLastPausedActivity = null;
173
174 /**
175 * Current activity that is resumed, or null if there is none.
176 */
177 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800178
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700179 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700180 * This is the last activity that has been started. It is only used to
181 * identify when multiple activities are started at once so that the user
182 * can be warned they may not be in the activity they think they are.
183 */
184 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800185
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700186 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700187 * Set when we know we are going to be calling updateConfiguration()
188 * soon, so want to skip intermediate config checks.
189 */
190 boolean mConfigWillChange;
191
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700192 long mLaunchStartTime = 0;
193 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800194
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800195 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800196 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
197 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
198 */
199 private ActivityRecord mLastScreenshotActivity = null;
200 private Bitmap mLastScreenshotBitmap = null;
201
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800202 int mThumbnailWidth = -1;
203 int mThumbnailHeight = -1;
204
Craig Mautner858d8a62013-04-23 17:08:34 -0700205 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700206
Craig Mautnerc00204b2013-03-05 15:02:14 -0800207 final int mStackId;
208
Craig Mautner27084302013-03-25 08:05:25 -0700209 /** Run all ActivityStacks through this */
210 final ActivityStackSupervisor mStackSupervisor;
211
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700212 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700213 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
214 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
215 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
216 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700217
218 static class ScheduleDestroyArgs {
219 final ProcessRecord mOwner;
220 final boolean mOomAdj;
221 final String mReason;
222 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
223 mOwner = owner;
224 mOomAdj = oomAdj;
225 mReason = reason;
226 }
227 }
228
Zoran Marcetaf958b322012-08-09 20:27:12 +0900229 final Handler mHandler;
230
231 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700232 //public Handler() {
233 // if (localLOGV) Slog.v(TAG, "Handler started!");
234 //}
Zoran Marcetaf958b322012-08-09 20:27:12 +0900235 public ActivityStackHandler(Looper looper) {
236 super(looper);
237 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700238
Zoran Marcetaf958b322012-08-09 20:27:12 +0900239 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700240 public void handleMessage(Message msg) {
241 switch (msg.what) {
242 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800243 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700244 // We don't at this point know if the activity is fullscreen,
245 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800246 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700247 synchronized (mService) {
248 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700249 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700250 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700251 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800252 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700253 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700254 case LAUNCH_TICK_MSG: {
255 ActivityRecord r = (ActivityRecord)msg.obj;
256 synchronized (mService) {
257 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700258 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700259 }
260 }
261 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700262 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800263 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700264 // We don't at this point know if the activity is fullscreen,
265 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800266 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800267 synchronized (mService) {
268 activityDestroyedLocked(r != null ? r.appToken : null);
269 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700270 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700271 case STOP_TIMEOUT_MSG: {
272 ActivityRecord r = (ActivityRecord)msg.obj;
273 // We don't at this point know if the activity is fullscreen,
274 // so we need to be conservative and assume it isn't.
275 Slog.w(TAG, "Activity stop timeout for " + r);
276 synchronized (mService) {
277 if (r.isInHistory()) {
278 activityStoppedLocked(r, null, null, null);
279 }
280 }
281 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700282 case DESTROY_ACTIVITIES_MSG: {
283 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
284 synchronized (mService) {
285 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
286 }
287 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700288 }
289 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800290 }
291
Craig Mautner000f0022013-02-26 15:04:29 -0800292 private int numActivities() {
293 int count = 0;
294 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
295 count += mTaskHistory.get(taskNdx).mActivities.size();
296 }
297 return count;
298 }
299
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700300 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900301 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700302 mService = service;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700303 mWindowManager = service.mWindowManager;
304 mStackSupervisor = service.mStackSupervisor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700305 mContext = context;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700306 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800307 mStackId = stackId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700308 mCurrentUser = service.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700309 }
Craig Mautner5962b122012-10-05 14:45:52 -0700310
311 private boolean okToShow(ActivityRecord r) {
312 return r.userId == mCurrentUser
313 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
314 }
315
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700316 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800317 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
318 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800319 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800320 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
321 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800322 if (!r.finishing && r != notTop && okToShow(r)) {
323 return r;
324 }
325 }
326 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700327 return null;
328 }
329
330 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800331 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
332 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800333 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800334 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
335 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800336 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
337 return r;
338 }
339 }
340 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700341 return null;
342 }
343
344 /**
345 * This is a simplified version of topRunningActivityLocked that provides a number of
346 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800347 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700348 * @param token If non-null, any history records matching this token will be skipped.
349 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800350 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700351 * @return Returns the HistoryRecord of the next activity on the stack.
352 */
353 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800354 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
355 TaskRecord task = mTaskHistory.get(taskNdx);
356 if (task.taskId == taskId) {
357 continue;
358 }
359 ArrayList<ActivityRecord> activities = task.mActivities;
360 for (int i = activities.size() - 1; i >= 0; --i) {
361 final ActivityRecord r = activities.get(i);
362 // Note: the taskId check depends on real taskId fields being non-zero
363 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
364 return r;
365 }
366 }
367 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700368 return null;
369 }
370
Craig Mautner8849a5e2013-04-02 16:41:03 -0700371 final ActivityRecord topActivity() {
372 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700373 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700374 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
375 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
376 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700377 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700378 }
379 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700380 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700381 }
382
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700383 final TaskRecord topTask() {
384 final int size = mTaskHistory.size();
385 if (size > 0) {
386 return mTaskHistory.get(size - 1);
387 }
388 return null;
389 }
390
Craig Mautnerd2328952013-03-05 12:46:26 -0800391 TaskRecord taskForIdLocked(int id) {
392 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
393 final TaskRecord task = mTaskHistory.get(taskNdx);
394 if (task.taskId == id) {
395 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800396 }
397 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700398 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700399 }
400
Craig Mautnerd2328952013-03-05 12:46:26 -0800401 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800402 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800403 if (r != null) {
404 final TaskRecord task = r.task;
405 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
406 if (task.stack != this) Slog.w(TAG,
407 "Illegal state! task does not point to stack it is in.");
408 return r;
409 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800410 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800411 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800412 }
413
Craig Mautnerf88c50f2013-04-18 19:25:12 -0700414 boolean containsApp(ProcessRecord app) {
415 if (app == null) {
416 return false;
417 }
418 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
419 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
420 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
421 final ActivityRecord r = activities.get(activityNdx);
422 if (r.finishing) {
423 continue;
424 }
425 if (r.app == app) {
426 return true;
427 }
428 }
429 }
430 return false;
431 }
432
Craig Mautner2420ead2013-04-01 17:13:20 -0700433 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700434 final boolean hadit = mLRUActivities.remove(r);
435 mLRUActivities.add(r);
436 return hadit;
437 }
438
Craig Mautnerde4ef022013-04-07 19:01:33 -0700439 final boolean isHomeStack() {
440 return mStackId == HOME_STACK_ID;
441 }
442
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700443 /**
444 * Returns the top activity in any existing task matching the given
445 * Intent. Returns null if no such task is found.
446 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700447 ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700448 ComponentName cls = intent.getComponent();
449 if (info.targetActivity != null) {
450 cls = new ComponentName(info.packageName, info.targetActivity);
451 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700452 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800453
454 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
455 final TaskRecord task = mTaskHistory.get(taskNdx);
456 final ActivityRecord r = task.getTopActivity();
457 if (r == null || r.finishing || r.userId != userId ||
458 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
459 continue;
460 }
461
462 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
463 // + "/aff=" + r.task.affinity + " to new cls="
464 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
465 if (task.affinity != null) {
466 if (task.affinity.equals(info.taskAffinity)) {
467 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700468 return r;
469 }
Craig Mautner000f0022013-02-26 15:04:29 -0800470 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
471 //Slog.i(TAG, "Found matching class!");
472 //dump();
473 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
474 return r;
475 } else if (task.affinityIntent != null
476 && task.affinityIntent.getComponent().equals(cls)) {
477 //Slog.i(TAG, "Found matching class!");
478 //dump();
479 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
480 return r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700481 }
482 }
483
484 return null;
485 }
486
487 /**
488 * Returns the first activity (starting from the top of the stack) that
489 * is the same as the given activity. Returns null if no such activity
490 * is found.
491 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700492 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700493 ComponentName cls = intent.getComponent();
494 if (info.targetActivity != null) {
495 cls = new ComponentName(info.packageName, info.targetActivity);
496 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700497 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700498
Craig Mautner000f0022013-02-26 15:04:29 -0800499 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
500 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
501 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
502 ActivityRecord r = activities.get(activityNdx);
503 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700504 //Slog.i(TAG, "Found matching class!");
505 //dump();
506 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
507 return r;
508 }
509 }
510 }
511
512 return null;
513 }
514
Amith Yamasani742a6712011-05-04 14:49:28 -0700515 /*
Craig Mautner858d8a62013-04-23 17:08:34 -0700516 * Move the activities around in the stack to bring a user to the foreground. This only
517 * matters on the home stack. All other stacks are single user.
Amith Yamasani742a6712011-05-04 14:49:28 -0700518 * @return whether there are any activities for the specified user.
519 */
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700520 final boolean switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800521 if (VALIDATE_TOKENS) {
522 validateAppTokensLocked();
523 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800524 if (mCurrentUser == userId) {
525 return true;
526 }
527 mCurrentUser = userId;
528
529 // Move userId's tasks to the top.
530 boolean haveActivities = false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800531 int index = mTaskHistory.size();
532 for (int i = 0; i < index; ++i) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700533 TaskRecord task = mTaskHistory.get(i);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800534 if (task.userId == userId) {
535 haveActivities = true;
536 mTaskHistory.remove(i);
537 mTaskHistory.add(task);
538 --index;
539 }
540 }
541
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700542 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700543 }
544
Craig Mautner2420ead2013-04-01 17:13:20 -0700545 void minimalResumeActivityLocked(ActivityRecord r) {
546 r.state = ActivityState.RESUMED;
547 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
548 + " (starting new instance)");
549 r.stopped = false;
550 mResumedActivity = r;
551 r.task.touchActiveTime();
552 mService.addRecentTaskLocked(r.task);
553 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700554 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -0700555 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700556 }
557
Craig Mautnere79d42682013-04-01 19:01:53 -0700558 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700559 if (r.displayStartTime == 0) {
560 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
561 if (mLaunchStartTime == 0) {
562 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700563 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700564 } else if (mLaunchStartTime == 0) {
565 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700566 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700567 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800568
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700569 void clearLaunchTime(ActivityRecord r) {
570 r.displayStartTime = r.fullyDrawnStartTime = 0;
571 }
572
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800573 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800574 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800575 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
576 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
577 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
578 activities.get(activityNdx).setSleeping(false);
579 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800580 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800581 }
582
Craig Mautner0eea92c2013-05-16 13:35:39 -0700583 /**
584 * @return true if something must be done before going to sleep.
585 */
586 boolean checkReadyForSleepLocked() {
587 if (mResumedActivity != null) {
588 // Still have something resumed; can't sleep until it is paused.
589 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
590 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
591 startPausingLocked(false, true);
592 return true;
593 }
594 if (mPausingActivity != null) {
595 // Still waiting for something to pause; can't sleep yet.
596 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
597 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800598 }
599
Craig Mautner0eea92c2013-05-16 13:35:39 -0700600 return false;
601 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800602
Craig Mautner0eea92c2013-05-16 13:35:39 -0700603 void goToSleep() {
604 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800605
Craig Mautner0eea92c2013-05-16 13:35:39 -0700606 // Make sure any stopped but visible activities are now sleeping.
607 // This ensures that the activity's onStop() is called.
608 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
609 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
610 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
611 final ActivityRecord r = activities.get(activityNdx);
612 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
613 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800614 }
615 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800616 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700617 }
Craig Mautner59c00972012-07-30 12:10:24 -0700618
Dianne Hackbornd2835932010-12-13 16:28:46 -0800619 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800620 if (who.noDisplay) {
621 return null;
622 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800623
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800624 Resources res = mService.mContext.getResources();
625 int w = mThumbnailWidth;
626 int h = mThumbnailHeight;
627 if (w < 0) {
628 mThumbnailWidth = w =
629 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
630 mThumbnailHeight = h =
631 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
632 }
633
634 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800635 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
636 || mLastScreenshotBitmap.getWidth() != w
637 || mLastScreenshotBitmap.getHeight() != h) {
638 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700639 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
Craig Mautnerb12428a2012-12-20 16:07:06 -0800640 who.appToken, Display.DEFAULT_DISPLAY, w, h);
641 }
642 if (mLastScreenshotBitmap != null) {
643 return mLastScreenshotBitmap.copy(Config.ARGB_8888, true);
644 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800645 }
646 return null;
647 }
648
Craig Mautnercf910b02013-04-23 11:23:27 -0700649 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800650 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800651 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700652 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800653 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700654 ActivityRecord prev = mResumedActivity;
655 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700656 Slog.e(TAG, "Trying to pause when nothing is resumed",
657 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700658 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700659 return;
660 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700661 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
662 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700663 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800664 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700665 mLastPausedActivity = prev;
666 prev.state = ActivityState.PAUSING;
667 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700668 clearLaunchTime(prev);
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700669 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700670
671 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800672
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700673 if (prev.app != null && prev.app.thread != null) {
674 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
675 try {
676 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700677 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700678 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700679 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800680 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
681 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700682 } catch (Exception e) {
683 // Ignore exception, if process died other code will cleanup.
684 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800685 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700686 mLastPausedActivity = null;
687 }
688 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800689 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700690 mLastPausedActivity = null;
691 }
692
693 // If we are not going to sleep, we want to ensure the device is
694 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700695 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700696 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700697 }
698
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800699 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700700 // Have the window manager pause its key dispatching until the new
701 // activity has started. If we're pausing the activity just because
702 // the screen is being turned off and the UI is sleeping, don't interrupt
703 // key dispatch; the same activity will pick it up again on wakeup.
704 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800705 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700706 } else {
707 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
708 }
709
710 // Schedule a pause timeout in case the app doesn't respond.
711 // We don't give it much time because this directly impacts the
712 // responsiveness seen by the user.
713 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
714 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700715 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700716 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
717 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
718 } else {
719 // This activity failed to schedule the
720 // pause, so just treat it as being paused now.
721 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700722 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700723 }
724 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700725
Craig Mautnerd2328952013-03-05 12:46:26 -0800726 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700727 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800728 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700729
Craig Mautnerd2328952013-03-05 12:46:26 -0800730 final ActivityRecord r = isInStackLocked(token);
731 if (r != null) {
732 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
733 if (mPausingActivity == r) {
734 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
735 + (timeout ? " (due to timeout)" : " (pause complete)"));
736 r.state = ActivityState.PAUSED;
737 completePauseLocked();
738 } else {
739 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
740 r.userId, System.identityHashCode(r), r.shortComponentName,
741 mPausingActivity != null
742 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700743 }
744 }
745 }
746
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700747 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
748 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700749 if (r.state != ActivityState.STOPPING) {
750 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
751 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
752 return;
753 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700754 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700755 if (icicle != null) {
756 // If icicle is null, this is happening due to a timeout, so we
757 // haven't really saved the state.
758 r.icicle = icicle;
759 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800760 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700761 r.updateThumbnail(thumbnail, description);
762 }
763 if (!r.stopped) {
764 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
765 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
766 r.stopped = true;
767 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700768 if (r.finishing) {
769 r.clearOptionsLocked();
770 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700771 if (r.configDestroy) {
772 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700773 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700774 } else {
775 // Now that this process has stopped, we may want to consider
776 // it to be the previous app to try to keep around in case
777 // the user wants to return to it.
778 ProcessRecord fgApp = null;
779 if (mResumedActivity != null) {
780 fgApp = mResumedActivity.app;
781 } else if (mPausingActivity != null) {
782 fgApp = mPausingActivity.app;
783 }
784 if (r.app != null && fgApp != null && r.app != fgApp
785 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
786 && r.app != mService.mHomeProcess) {
787 mService.mPreviousProcess = r.app;
788 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
789 }
Dianne Hackborn50685602011-12-01 12:23:37 -0800790 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700791 }
792 }
793 }
794
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800795 private final void completePauseLocked() {
796 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700797 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800798
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800799 if (prev != null) {
800 if (prev.finishing) {
801 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700802 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800803 } else if (prev.app != null) {
804 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
805 if (prev.waitingVisible) {
806 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700807 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800808 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
809 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800810 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800811 if (prev.configDestroy) {
812 // The previous is being paused because the configuration
813 // is changing, which means it is actually stopping...
814 // To juggle the fact that we are also starting a new
815 // instance right now, we need to first completely stop
816 // the current instance before starting the new one.
817 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
818 destroyActivityLocked(prev, true, false, "pause-config");
819 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700820 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700821 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
822 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800823 // If we already have a few activities waiting to stop,
824 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700825 // them out. Or if r is the last of activity of the last task the stack
826 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800827 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700828 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800829 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700830 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800831 }
832 }
833 } else {
834 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
835 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800836 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800837 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700838 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800839
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700840 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700841 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700842 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800843 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700844 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700845 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700846 if (top == null || (prev != null && top != prev)) {
847 // If there are no more activities available to run,
848 // do resume anyway to start something. Also if the top
849 // activity on the stack is not the just paused activity,
850 // we need to go ahead and resume it to ensure we complete
851 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700852 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700853 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700854 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800855
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800856 if (prev != null) {
857 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700858
Craig Mautner525f3d92013-05-07 14:01:50 -0700859 if (prev.app != null && prev.cpuTimeAtResume > 0
860 && mService.mBatteryStatsService.isOnBattery()) {
861 long diff;
862 synchronized (mService.mProcessStatsThread) {
863 diff = mService.mProcessStats.getCpuTimeForPid(prev.app.pid)
864 - prev.cpuTimeAtResume;
865 }
866 if (diff > 0) {
867 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
868 synchronized (bsi) {
869 BatteryStatsImpl.Uid.Proc ps =
870 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
871 prev.info.packageName);
872 if (ps != null) {
873 ps.addForegroundTimeLocked(diff);
874 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700875 }
876 }
877 }
Craig Mautner525f3d92013-05-07 14:01:50 -0700878 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700879 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700880 }
881
882 /**
883 * Once we know that we have asked an application to put an activity in
884 * the resumed state (either by launching it or explicitly telling it),
885 * this function updates the rest of our state to match that fact.
886 */
Craig Mautner525f3d92013-05-07 14:01:50 -0700887 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700888 next.idle = false;
889 next.results = null;
890 next.newIntents = null;
891
892 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -0700893 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700894
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700895 mStackSupervisor.reportResumedActivityLocked(next);
896
897 next.resumeKeyDispatchingLocked();
898 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700899
900 // Mark the point when the activity is resuming
901 // TODO: To be more accurate, the mark should be before the onCreate,
902 // not after the onResume. But for subsequent starts, onResume is fine.
903 if (next.app != null) {
904 synchronized (mService.mProcessStatsThread) {
905 next.cpuTimeAtResume = mService.mProcessStats.getCpuTimeForPid(next.app.pid);
906 }
907 } else {
908 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
909 }
910 }
911
Craig Mautner580ea812013-04-25 12:58:38 -0700912 /**
913 * Version of ensureActivitiesVisible that can easily be called anywhere.
914 */
915 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
916 return ensureActivitiesVisibleLocked(starting, configChanges, false);
917 }
918
919 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
920 boolean forceHomeShown) {
921 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -0700922 return r != null &&
923 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -0700924 }
925
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700926 /**
927 * Make sure that all activities that need to be visible (that is, they
928 * currently can be seen by the user) actually are.
929 */
Craig Mautner580ea812013-04-25 12:58:38 -0700930 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
931 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700932 if (DEBUG_VISBILITY) Slog.v(
933 TAG, "ensureActivitiesVisible behind " + top
934 + " configChanges=0x" + Integer.toHexString(configChanges));
935
936 // If the top activity is not fullscreen, then we need to
937 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800938 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -0700939 boolean showHomeBehindStack = false;
940 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
941 !(forceHomeShown && isHomeStack());
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700942 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerd44711d2013-02-23 11:24:36 -0800943 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
944 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
945 final ActivityRecord r = activities.get(activityNdx);
946 if (r.finishing) {
947 continue;
948 }
949 if (aboveTop && r != top) {
950 continue;
951 }
952 aboveTop = false;
953 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700954 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -0800955 TAG, "Make visible? " + r + " finishing=" + r.finishing
956 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -0800957
Craig Mautnerd44711d2013-02-23 11:24:36 -0800958 final boolean doThisProcess = onlyThisProcess == null
959 || onlyThisProcess.equals(r.processName);
960
961 // First: if this is not the current activity being started, make
962 // sure it matches the current configuration.
963 if (r != starting && doThisProcess) {
964 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700965 }
Craig Mautnerd44711d2013-02-23 11:24:36 -0800966
967 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700968 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -0800969 // This activity needs to be visible, but isn't even
970 // running... get it started, but don't resume it
971 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700972 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -0800973 if (r != starting) {
974 r.startFreezingScreenLocked(r.app, configChanges);
975 }
976 if (!r.visible) {
977 if (DEBUG_VISBILITY) Slog.v(
978 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700979 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -0800980 }
981 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -0700982 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -0800983 }
984 }
985
986 } else if (r.visible) {
987 // If this activity is already visible, then there is nothing
988 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700989 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700990 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -0800991
992 } else if (onlyThisProcess == null) {
993 // This activity is not currently visible, but is running.
994 // Tell it to become visible.
995 r.visible = true;
996 if (r.state != ActivityState.RESUMED && r != starting) {
997 // If this activity is paused, tell it
998 // to now show its window.
999 if (DEBUG_VISBILITY) Slog.v(
1000 TAG, "Making visible and scheduling visibility: " + r);
1001 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001002 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001003 r.sleeping = false;
1004 r.app.pendingUiClean = true;
1005 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1006 r.stopFreezingScreenLocked(false);
1007 } catch (Exception e) {
1008 // Just skip on any failure; we'll make it
1009 // visible when it next restarts.
1010 Slog.w(TAG, "Exception thrown making visibile: "
1011 + r.intent.getComponent(), e);
1012 }
1013 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001014 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001015
Craig Mautnerd44711d2013-02-23 11:24:36 -08001016 // Aggregate current change flags.
1017 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001018
Craig Mautnerd44711d2013-02-23 11:24:36 -08001019 if (r.fullscreen) {
1020 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001021 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1022 behindFullscreen = true;
1023 } else if (r.mLaunchHomeTaskNext) {
1024 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1025 showHomeBehindStack = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001026 behindFullscreen = true;
1027 }
1028 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001029 if (DEBUG_VISBILITY) Slog.v(
1030 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1031 + " state=" + r.state
1032 + " behindFullscreen=" + behindFullscreen);
1033 // Now for any activities that aren't visible to the user, make
1034 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001035 if (r.visible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001036 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001037 r.visible = false;
1038 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001039 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001040 switch (r.state) {
1041 case STOPPING:
1042 case STOPPED:
1043 if (r.app != null && r.app.thread != null) {
1044 if (DEBUG_VISBILITY) Slog.v(
1045 TAG, "Scheduling invisibility: " + r);
1046 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1047 }
1048 break;
1049
1050 case INITIALIZING:
1051 case RESUMED:
1052 case PAUSING:
1053 case PAUSED:
1054 stopActivityLocked(r);
1055 break;
1056
1057 default:
1058 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001059 }
1060 } catch (Exception e) {
1061 // Just skip on any failure; we'll make it
1062 // visible when it next restarts.
1063 Slog.w(TAG, "Exception thrown making hidden: "
1064 + r.intent.getComponent(), e);
1065 }
1066 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001067 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001068 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001069 }
1070 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001071 }
Craig Mautner580ea812013-04-25 12:58:38 -07001072 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001073 }
Craig Mautner58547802013-03-05 08:23:53 -08001074
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001075 /**
1076 * Ensure that the top activity in the stack is resumed.
1077 *
1078 * @param prev The previously resumed activity, for when in the process
1079 * of pausing; can be null to call from elsewhere.
1080 *
1081 * @return Returns true if something is being resumed, or false if
1082 * nothing happened.
1083 */
1084 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001085 return resumeTopActivityLocked(prev, null);
1086 }
1087
1088 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001089 // Find the first activity that is not finishing.
1090 ActivityRecord next = topRunningActivityLocked(null);
1091
1092 // Remember how we'll process this pause/resume situation, and ensure
1093 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001094 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1095 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001096
1097 if (next == null) {
1098 // There are no more activities! Let's just start up the
1099 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001100 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001101 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001102 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001103 }
1104
1105 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001106
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001107 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001108 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1109 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001110 // Make sure we have executed any pending transitions, since there
1111 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001112 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001113 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001114 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001115 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001116 return false;
1117 }
1118
Craig Mautner525f3d92013-05-07 14:01:50 -07001119 final TaskRecord nextTask = next.task;
1120 final TaskRecord prevTask = prev != null ? prev.task : null;
1121 if (prevTask != null && prev.mLaunchHomeTaskNext && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001122 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001123 if (prevTask == nextTask) {
1124 ArrayList<ActivityRecord> activities = prevTask.mActivities;
1125 final int numActivities = activities.size();
1126 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1127 final ActivityRecord r = activities.get(activityNdx);
1128 // r is usually the same as next, but what if two activities were launched
1129 // before prev finished?
1130 if (!r.finishing) {
1131 r.mLaunchHomeTaskNext = true;
1132 r.frontOfTask = true;
1133 break;
1134 }
1135 }
1136 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001137 // This task is going away but it was supposed to return to the home task.
1138 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001139 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001140 mTaskHistory.get(taskNdx).mActivities.get(0).mLaunchHomeTaskNext = true;
1141 } else {
1142 return mStackSupervisor.resumeHomeActivity(prev);
1143 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001144 }
1145
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001146 // If we are sleeping, and there is no resumed activity, and the top
1147 // activity is paused, well that is the state we want.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001148 if ((mService.isSleepingOrShuttingDown())
p13451dbad2872012-04-18 11:39:23 +09001149 && mLastPausedActivity == next
Craig Mautnerde4ef022013-04-07 19:01:33 -07001150 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001151 // Make sure we have executed any pending transitions, since there
1152 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001153 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001154 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001155 ActivityOptions.abort(options);
Craig Mautnercf910b02013-04-23 11:23:27 -07001156 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001157 return false;
1158 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001159
1160 // Make sure that the user who owns this activity is started. If not,
1161 // we will just leave it as is because someone should be bringing
1162 // another user's activities to the top of the stack.
1163 if (mService.mStartedUsers.get(next.userId) == null) {
1164 Slog.w(TAG, "Skipping resume of top activity " + next
1165 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001166 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001167 return false;
1168 }
1169
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001170 // The activity may be waiting for stop, but that is no longer
1171 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001172 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001173 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001174 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001175 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001176
Dianne Hackborn84375872012-06-01 19:03:50 -07001177 next.updateOptionsLocked(options);
1178
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001179 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1180
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001181 // If we are currently pausing an activity, then don't do anything
1182 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001183 if (!mStackSupervisor.allPausedActivitiesComplete()) {
1184 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG, "Skip resume: some activity pausing");
Craig Mautnercf910b02013-04-23 11:23:27 -07001185 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001186 return false;
1187 }
1188
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001189 // Okay we are now going to start a switch, to 'next'. We may first
1190 // have to pause the current activity, but this is an important point
1191 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001192 // XXX "App Redirected" dialog is getting too many false positives
1193 // at this point, so turn off for now.
1194 if (false) {
1195 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1196 long now = SystemClock.uptimeMillis();
1197 final boolean inTime = mLastStartedActivity.startTime != 0
1198 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1199 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1200 final int nextUid = next.info.applicationInfo.uid;
1201 if (inTime && lastUid != nextUid
1202 && lastUid != next.launchedFromUid
1203 && mService.checkPermission(
1204 android.Manifest.permission.STOP_APP_SWITCHES,
1205 -1, next.launchedFromUid)
1206 != PackageManager.PERMISSION_GRANTED) {
1207 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1208 } else {
1209 next.startTime = now;
1210 mLastStartedActivity = next;
1211 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001212 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001213 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001214 mLastStartedActivity = next;
1215 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001216 }
Craig Mautner58547802013-03-05 08:23:53 -08001217
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001218 // We need to start pausing the current activity so the top one
1219 // can be resumed...
Craig Mautnereb957862013-04-24 15:34:32 -07001220 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
1221 if (mResumedActivity != null) {
1222 pausing = true;
1223 startPausingLocked(userLeaving, false);
1224 }
1225 if (pausing) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001226 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001227 // At this point we want to put the upcoming activity's process
1228 // at the top of the LRU list, since we know we will be needing it
1229 // very soon and it would be a waste to let it get killed if it
1230 // happens to be sitting towards the end.
1231 if (next.app != null && next.app.thread != null) {
1232 // No reason to do full oom adj update here; we'll let that
1233 // happen whenever it needs to later.
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001234 mService.updateLruProcessLocked(next.app, false);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001235 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001236 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001237 return true;
1238 }
1239
Christopher Tated3f175c2012-06-14 14:16:54 -07001240 // If the most recent activity was noHistory but was only stopped rather
1241 // than stopped+finished because the device went to sleep, we need to make
1242 // sure to finish it as we're making a new activity topmost.
1243 final ActivityRecord last = mLastPausedActivity;
1244 if (mService.mSleeping && last != null && !last.finishing) {
1245 if ((last.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1246 || (last.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
1247 if (DEBUG_STATES) {
1248 Slog.d(TAG, "no-history finish of " + last + " on new resume");
1249 }
1250 requestFinishActivityLocked(last.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001251 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07001252 }
1253 }
1254
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001255 if (prev != null && prev != next) {
1256 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1257 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001258 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001259 if (DEBUG_SWITCH) Slog.v(
1260 TAG, "Resuming top, waiting visible to hide: " + prev);
1261 } else {
1262 // The next activity is already visible, so hide the previous
1263 // activity's windows right now so we can show the new one ASAP.
1264 // We only do this if the previous is finishing, which should mean
1265 // it is on top of the one being resumed so hiding it quickly
1266 // is good. Otherwise, we want to do the normal route of allowing
1267 // the resumed activity to be shown so we can decide if the
1268 // previous should actually be hidden depending on whether the
1269 // new one is found to be full-screen or not.
1270 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001271 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001272 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1273 + prev + ", waitingVisible="
1274 + (prev != null ? prev.waitingVisible : null)
1275 + ", nowVisible=" + next.nowVisible);
1276 } else {
1277 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1278 + prev + ", waitingVisible="
1279 + (prev != null ? prev.waitingVisible : null)
1280 + ", nowVisible=" + next.nowVisible);
1281 }
1282 }
1283 }
1284
Dianne Hackborne7f97212011-02-24 14:40:20 -08001285 // Launching this app's activity, make sure the app is no longer
1286 // considered stopped.
1287 try {
1288 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001289 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001290 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001291 } catch (IllegalArgumentException e) {
1292 Slog.w(TAG, "Failed trying to unstop package "
1293 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001294 }
1295
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001296 // We are starting up the next activity, so tell the window manager
1297 // that the previous one will be hidden soon. This way it can know
1298 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001299 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001300 if (prev != null) {
1301 if (prev.finishing) {
1302 if (DEBUG_TRANSITION) Slog.v(TAG,
1303 "Prepare close transition: prev=" + prev);
1304 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001305 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001306 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001307 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001308 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001309 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1310 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001311 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001312 mWindowManager.setAppWillBeHidden(prev.appToken);
1313 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001314 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001315 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001316 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001317 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001318 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001319 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001320 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001321 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1322 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001323 }
1324 }
1325 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001326 mWindowManager.setAppWillBeHidden(prev.appToken);
1327 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001328 }
Craig Mautner967212c2013-04-13 21:10:58 -07001329 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001330 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001331 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001332 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001333 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001334 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001335 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001336 }
1337 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001338 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001339 next.applyOptionsLocked();
1340 } else {
1341 next.clearOptionsLocked();
1342 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001343
Craig Mautnercf910b02013-04-23 11:23:27 -07001344 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001345 if (next.app != null && next.app.thread != null) {
1346 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1347
1348 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001349 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001350
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001351 // schedule launch ticks to collect information about slow apps.
1352 next.startLaunchTickingLocked();
1353
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001354 ActivityRecord lastResumedActivity =
1355 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001356 ActivityState lastState = next.state;
1357
1358 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001359
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001360 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001361 next.state = ActivityState.RESUMED;
1362 mResumedActivity = next;
1363 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001364 mService.addRecentTaskLocked(next.task);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001365 mService.updateLruProcessLocked(next.app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001366 updateLRUListLocked(next);
1367
1368 // Have the window manager re-evaluate the orientation of
1369 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001370 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001371 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001372 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001373 mService.mConfiguration,
1374 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1375 if (config != null) {
1376 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001377 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001378 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001379 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001380
Craig Mautner525f3d92013-05-07 14:01:50 -07001381 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001382 // The configuration update wasn't able to keep the existing
1383 // instance of the activity, and instead started a new one.
1384 // We should be all done, but let's just make sure our activity
1385 // is still at the top and schedule another run if something
1386 // weird happened.
1387 ActivityRecord nextNext = topRunningActivityLocked(null);
1388 if (DEBUG_SWITCH) Slog.i(TAG,
1389 "Activity config changed during resume: " + next
1390 + ", new next: " + nextNext);
1391 if (nextNext != next) {
1392 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001393 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001394 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001395 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001396 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001397 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001398 return true;
1399 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001400 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001401 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001402 }
Craig Mautner58547802013-03-05 08:23:53 -08001403
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001404 try {
1405 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001406 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001407 if (a != null) {
1408 final int N = a.size();
1409 if (!next.finishing && N > 0) {
1410 if (DEBUG_RESULTS) Slog.v(
1411 TAG, "Delivering results to " + next
1412 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001413 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001414 }
1415 }
1416
1417 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001418 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001419 }
1420
1421 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001422 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001423 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001424
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001425 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001426 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001427 next.app.pendingUiClean = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001428 next.app.thread.scheduleResumeActivity(next.appToken,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001429 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001430
Craig Mautner0eea92c2013-05-16 13:35:39 -07001431 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001432
1433 } catch (Exception e) {
1434 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001435 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1436 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001437 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001438 if (lastStack != null) {
1439 lastStack.mResumedActivity = lastResumedActivity;
1440 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001441 Slog.i(TAG, "Restarting because process died: " + next);
1442 if (!next.hasBeenLaunched) {
1443 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001444 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1445 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001446 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001447 next.appToken, next.packageName, next.theme,
1448 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001449 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1450 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001451 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001452 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001453 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001454 return true;
1455 }
1456
1457 // From this point on, if something goes wrong there is no way
1458 // to recover the activity.
1459 try {
1460 next.visible = true;
1461 completeResumeLocked(next);
1462 } catch (Exception e) {
1463 // If any exception gets thrown, toss away this
1464 // activity and try the next one.
1465 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001466 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001467 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001468 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001469 return true;
1470 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001471 next.stopped = false;
1472
1473 } else {
1474 // Whoops, need to restart this activity!
1475 if (!next.hasBeenLaunched) {
1476 next.hasBeenLaunched = true;
1477 } else {
1478 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001479 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001480 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001481 mService.compatibilityInfoForPackageLocked(
1482 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001483 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001484 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001485 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001486 }
1487 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1488 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001489 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001490 }
1491
Craig Mautnercf910b02013-04-23 11:23:27 -07001492 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001493 return true;
1494 }
1495
Craig Mautner11bf9a52013-02-19 14:08:51 -08001496
Craig Mautner8849a5e2013-04-02 16:41:03 -07001497 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001498 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001499 TaskRecord rTask = r.task;
1500 final int taskId = rTask.taskId;
1501 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001502 // Last activity in task had been removed or ActivityManagerService is reusing task.
1503 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001504 // Might not even be in.
Craig Mautnerd2328952013-03-05 12:46:26 -08001505 mTaskHistory.remove(rTask);
Craig Mautner77878772013-03-04 19:46:24 -08001506 // Now put task at top.
Craig Mautnerd2328952013-03-05 12:46:26 -08001507 mTaskHistory.add(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001508 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001509 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001510 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001511 if (!newTask) {
1512 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001513 boolean startIt = true;
1514 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1515 task = mTaskHistory.get(taskNdx);
1516 if (task == r.task) {
1517 // Here it is! Now, if this is not yet visible to the
1518 // user, then just add it without starting; it will
1519 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001520 if (!startIt) {
1521 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1522 + task, new RuntimeException("here").fillInStackTrace());
1523 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001524 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001525 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1526 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001527 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001528 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001529 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001530 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001531 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001532 return;
1533 }
1534 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001535 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001536 startIt = false;
1537 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001538 }
1539 }
1540
1541 // Place a new activity at top of stack, so it is next to interact
1542 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001543
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001544 // If we are not placing the new activity frontmost, we do not want
1545 // to deliver the onUserLeaving callback to the actual frontmost
1546 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001547 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001548 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001549 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1550 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001551 }
Craig Mautner70a86932013-02-28 22:37:44 -08001552
1553 task = r.task;
1554
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001555 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001556 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001557 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001558 task.addActivityToTop(r);
1559
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001560 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001561 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001562 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001563 // We want to show the starting preview window if we are
1564 // switching to a new task, or the next activity's process is
1565 // not currently running.
1566 boolean showStartingIcon = newTask;
1567 ProcessRecord proc = r.app;
1568 if (proc == null) {
1569 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1570 }
1571 if (proc == null || proc.thread == null) {
1572 showStartingIcon = true;
1573 }
1574 if (DEBUG_TRANSITION) Slog.v(TAG,
1575 "Prepare open transition: starting " + r);
1576 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001577 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001578 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001579 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001580 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001581 ? AppTransition.TRANSIT_TASK_OPEN
1582 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001583 mNoAnimActivities.remove(r);
1584 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001585 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001586 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001587 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001588 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001589 boolean doShow = true;
1590 if (newTask) {
1591 // Even though this activity is starting fresh, we still need
1592 // to reset it to make sure we apply affinities to move any
1593 // existing activities from other tasks in to it.
1594 // If the caller has requested that the target task be
1595 // reset, then do so.
1596 if ((r.intent.getFlags()
1597 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1598 resetTaskIfNeededLocked(r, r);
1599 doShow = topRunningNonDelayedActivityLocked(null) == r;
1600 }
1601 }
1602 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1603 // Figure out if we are transitioning from another activity that is
1604 // "has the same starting icon" as the next one. This allows the
1605 // window manager to keep the previous window it had previously
1606 // created, if it still had one.
1607 ActivityRecord prev = mResumedActivity;
1608 if (prev != null) {
1609 // We don't want to reuse the previous starting preview if:
1610 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001611 if (prev.task != r.task) {
1612 prev = null;
1613 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001614 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001615 else if (prev.nowVisible) {
1616 prev = null;
1617 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001618 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001619 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001620 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001621 mService.compatibilityInfoForPackageLocked(
1622 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001623 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001624 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001625 }
1626 } else {
1627 // If this is the first activity, don't do any fancy animations,
1628 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001629 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001630 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5962b122012-10-05 14:45:52 -07001631 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001632 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001633 }
1634 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001635 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001636 }
1637
1638 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001639 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001640 }
1641 }
1642
Dianne Hackbornbe707852011-11-11 14:32:10 -08001643 final void validateAppTokensLocked() {
1644 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001645 mValidateAppTokens.ensureCapacity(numActivities());
1646 final int numTasks = mTaskHistory.size();
1647 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1648 TaskRecord task = mTaskHistory.get(taskNdx);
1649 final ArrayList<ActivityRecord> activities = task.mActivities;
1650 if (activities.size() == 0) {
1651 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001652 }
Craig Mautner000f0022013-02-26 15:04:29 -08001653 TaskGroup group = new TaskGroup();
1654 group.taskId = task.taskId;
1655 mValidateAppTokens.add(group);
1656 final int numActivities = activities.size();
1657 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1658 final ActivityRecord r = activities.get(activityNdx);
1659 group.tokens.add(r.appToken);
1660 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001661 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001662 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001663 }
1664
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001665 /**
1666 * Perform a reset of the given task, if needed as part of launching it.
1667 * Returns the new HistoryRecord at the top of the task.
1668 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001669 /**
1670 * Helper method for #resetTaskIfNeededLocked.
1671 * We are inside of the task being reset... we'll either finish this activity, push it out
1672 * for another task, or leave it as-is.
1673 * @param task The task containing the Activity (taskTop) that might be reset.
1674 * @param forceReset
1675 * @return An ActivityOptions that needs to be processed.
1676 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001677 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001678 ActivityOptions topOptions = null;
1679
1680 int replyChainEnd = -1;
1681 boolean canMoveOptions = true;
1682
1683 // We only do this for activities that are not the root of the task (since if we finish
1684 // the root, we may no longer have the task!).
1685 final ArrayList<ActivityRecord> activities = task.mActivities;
1686 final int numActivities = activities.size();
1687 for (int i = numActivities - 1; i > 0; --i ) {
1688 ActivityRecord target = activities.get(i);
1689
1690 final int flags = target.info.flags;
1691 final boolean finishOnTaskLaunch =
1692 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1693 final boolean allowTaskReparenting =
1694 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1695 final boolean clearWhenTaskReset =
1696 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1697
1698 if (!finishOnTaskLaunch
1699 && !clearWhenTaskReset
1700 && target.resultTo != null) {
1701 // If this activity is sending a reply to a previous
1702 // activity, we can't do anything with it now until
1703 // we reach the start of the reply chain.
1704 // XXX note that we are assuming the result is always
1705 // to the previous activity, which is almost always
1706 // the case but we really shouldn't count on.
1707 if (replyChainEnd < 0) {
1708 replyChainEnd = i;
1709 }
1710 } else if (!finishOnTaskLaunch
1711 && !clearWhenTaskReset
1712 && allowTaskReparenting
1713 && target.taskAffinity != null
1714 && !target.taskAffinity.equals(task.affinity)) {
1715 // If this activity has an affinity for another
1716 // task, then we need to move it out of here. We will
1717 // move it as far out of the way as possible, to the
1718 // bottom of the activity stack. This also keeps it
1719 // correctly ordered with any activities we previously
1720 // moved.
1721 TaskRecord bottomTask = mTaskHistory.get(0);
1722 ActivityRecord p = bottomTask.mActivities.get(0);
1723 if (target.taskAffinity != null
1724 && target.taskAffinity.equals(p.task.affinity)) {
1725 // If the activity currently at the bottom has the
1726 // same task affinity as the one we are moving,
1727 // then merge it into the same task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001728 target.setTask(p.task, p.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001729 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1730 + " out to bottom task " + p.task);
1731 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001732 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001733 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001734 target.task.affinityIntent = target.intent;
1735 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1736 + " out to new task " + target.task);
1737 }
1738
1739 final TaskRecord targetTask = target.task;
1740 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001741 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001742
Craig Mautner525f3d92013-05-07 14:01:50 -07001743 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001744 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1745 for (int srcPos = start; srcPos >= i; --srcPos) {
1746 p = activities.get(srcPos);
1747 if (p.finishing) {
1748 continue;
1749 }
1750
Craig Mautner525f3d92013-05-07 14:01:50 -07001751 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001752 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001753 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001754 topOptions = p.takeOptionsLocked();
1755 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001756 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001757 }
1758 }
1759 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1760 + task + " adding to task=" + targetTask,
1761 new RuntimeException("here").fillInStackTrace());
1762 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1763 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001764 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001765 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001766
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001767 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001768 }
1769
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001770 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001771 if (VALIDATE_TOKENS) {
1772 validateAppTokensLocked();
1773 }
1774
1775 replyChainEnd = -1;
1776 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1777 // If the activity should just be removed -- either
1778 // because it asks for it, or the task should be
1779 // cleared -- then finish it and anything that is
1780 // part of its reply chain.
1781 int end;
1782 if (clearWhenTaskReset) {
1783 // In this case, we want to finish this activity
1784 // and everything above it, so be sneaky and pretend
1785 // like these are all in the reply chain.
1786 end = numActivities - 1;
1787 } else if (replyChainEnd < 0) {
1788 end = i;
1789 } else {
1790 end = replyChainEnd;
1791 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001792 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001793 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001794 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001795 if (p.finishing) {
1796 continue;
1797 }
1798 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001799 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001800 topOptions = p.takeOptionsLocked();
1801 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001802 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001803 }
1804 }
Craig Mautner58547802013-03-05 08:23:53 -08001805 if (DEBUG_TASKS) Slog.w(TAG,
1806 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001807 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001808 end--;
1809 srcPos--;
1810 }
1811 }
1812 replyChainEnd = -1;
1813 } else {
1814 // If we were in the middle of a chain, well the
1815 // activity that started it all doesn't want anything
1816 // special, so leave it all as-is.
1817 replyChainEnd = -1;
1818 }
1819 }
1820
1821 return topOptions;
1822 }
1823
1824 /**
1825 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1826 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1827 * @param affinityTask The task we are looking for an affinity to.
1828 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1829 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1830 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1831 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001832 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08001833 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001834 int replyChainEnd = -1;
1835 final int taskId = task.taskId;
1836 final String taskAffinity = task.affinity;
1837
1838 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1839 final int numActivities = activities.size();
1840 // Do not operate on the root Activity.
1841 for (int i = numActivities - 1; i > 0; --i) {
1842 ActivityRecord target = activities.get(i);
1843
1844 final int flags = target.info.flags;
1845 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1846 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1847
1848 if (target.resultTo != null) {
1849 // If this activity is sending a reply to a previous
1850 // activity, we can't do anything with it now until
1851 // we reach the start of the reply chain.
1852 // XXX note that we are assuming the result is always
1853 // to the previous activity, which is almost always
1854 // the case but we really shouldn't count on.
1855 if (replyChainEnd < 0) {
1856 replyChainEnd = i;
1857 }
1858 } else if (topTaskIsHigher
1859 && allowTaskReparenting
1860 && taskAffinity != null
1861 && taskAffinity.equals(target.taskAffinity)) {
1862 // This activity has an affinity for our task. Either remove it if we are
1863 // clearing or move it over to our task. Note that
1864 // we currently punt on the case where we are resetting a
1865 // task that is not at the top but who has activities above
1866 // with an affinity to it... this is really not a normal
1867 // case, and we will need to later pull that task to the front
1868 // and usually at that point we will do the reset and pick
1869 // up those remaining activities. (This only happens if
1870 // someone starts an activity in a new task from an activity
1871 // in a task that is not currently on top.)
1872 if (forceReset || finishOnTaskLaunch) {
1873 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1874 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
1875 for (int srcPos = start; srcPos >= i; --srcPos) {
1876 final ActivityRecord p = activities.get(srcPos);
1877 if (p.finishing) {
1878 continue;
1879 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001880 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001881 }
1882 } else {
Craig Mautner77878772013-03-04 19:46:24 -08001883 if (taskInsertionPoint < 0) {
1884 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08001885
Craig Mautner77878772013-03-04 19:46:24 -08001886 }
Craig Mautner77878772013-03-04 19:46:24 -08001887
1888 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1889 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
1890 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
1891 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001892 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001893 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08001894 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001895
Craig Mautnere3a74d52013-02-22 14:14:58 -08001896 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
1897 + " to stack at " + task,
1898 new RuntimeException("here").fillInStackTrace());
1899 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
1900 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001901 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001902 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001903 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08001904 if (VALIDATE_TOKENS) {
1905 validateAppTokensLocked();
1906 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08001907
1908 // Now we've moved it in to place... but what if this is
1909 // a singleTop activity and we have put it on top of another
1910 // instance of the same activity? Then we drop the instance
1911 // below so it remains singleTop.
1912 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
1913 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001914 int targetNdx = taskActivities.indexOf(target);
1915 if (targetNdx > 0) {
1916 ActivityRecord p = taskActivities.get(targetNdx - 1);
1917 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08001918 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
1919 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001920 }
1921 }
1922 }
1923 }
1924
1925 replyChainEnd = -1;
1926 }
1927 }
Craig Mautner77878772013-03-04 19:46:24 -08001928 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001929 }
1930
Craig Mautner8849a5e2013-04-02 16:41:03 -07001931 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08001932 ActivityRecord newActivity) {
1933 boolean forceReset =
1934 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
1935 if (ACTIVITY_INACTIVE_RESET_TIME > 0
1936 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
1937 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
1938 forceReset = true;
1939 }
1940 }
1941
1942 final TaskRecord task = taskTop.task;
1943
1944 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
1945 * for remaining tasks. Used for later tasks to reparent to task. */
1946 boolean taskFound = false;
1947
1948 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
1949 ActivityOptions topOptions = null;
1950
Craig Mautner77878772013-03-04 19:46:24 -08001951 // Preserve the location for reparenting in the new task.
1952 int reparentInsertionPoint = -1;
1953
Craig Mautnere3a74d52013-02-22 14:14:58 -08001954 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1955 final TaskRecord targetTask = mTaskHistory.get(i);
1956
1957 if (targetTask == task) {
1958 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
1959 taskFound = true;
1960 } else {
Craig Mautner77878772013-03-04 19:46:24 -08001961 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
1962 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001963 }
1964 }
1965
Craig Mautner70a86932013-02-28 22:37:44 -08001966 int taskNdx = mTaskHistory.indexOf(task);
1967 do {
1968 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
1969 } while (taskTop == null && taskNdx >= 0);
1970
Craig Mautnere3a74d52013-02-22 14:14:58 -08001971 if (topOptions != null) {
1972 // If we got some ActivityOptions from an activity on top that
1973 // was removed from the task, propagate them to the new real top.
1974 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001975 taskTop.updateOptionsLocked(topOptions);
1976 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001977 topOptions.abort();
1978 }
1979 }
1980
1981 return taskTop;
1982 }
1983
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001984 void sendActivityResultLocked(int callingUid, ActivityRecord r,
1985 String resultWho, int requestCode, int resultCode, Intent data) {
1986
1987 if (callingUid > 0) {
1988 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07001989 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001990 }
1991
1992 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
1993 + " : who=" + resultWho + " req=" + requestCode
1994 + " res=" + resultCode + " data=" + data);
1995 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
1996 try {
1997 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1998 list.add(new ResultInfo(resultWho, requestCode,
1999 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002000 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002001 return;
2002 } catch (Exception e) {
2003 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2004 }
2005 }
2006
2007 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2008 }
2009
Craig Mautnerf3333272013-04-22 10:55:53 -07002010 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002011 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2012 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2013 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2014 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002015 if (!mService.mSleeping) {
2016 if (DEBUG_STATES) {
2017 Slog.d(TAG, "no-history finish of " + r);
2018 }
2019 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002020 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002021 } else {
2022 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2023 + " on stop because we're just sleeping");
2024 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002025 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002026 }
2027
2028 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002029 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002030 if (mService.mFocusedActivity == r) {
2031 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2032 }
2033 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002034 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002035 try {
2036 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002037 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2038 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002039 r.state = ActivityState.STOPPING;
2040 if (DEBUG_VISBILITY) Slog.v(
2041 TAG, "Stopping visible=" + r.visible + " for " + r);
2042 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002043 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002044 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002045 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002046 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002047 r.setSleeping(true);
2048 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002049 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002050 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002051 } catch (Exception e) {
2052 // Maybe just ignore exceptions here... if the process
2053 // has crashed, our death notification will clean things
2054 // up.
2055 Slog.w(TAG, "Exception thrown during pause", e);
2056 // Just in case, assume it to be stopped.
2057 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002058 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002059 r.state = ActivityState.STOPPED;
2060 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002061 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002062 }
2063 }
2064 }
2065 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002066
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002067 /**
2068 * @return Returns true if the activity is being finished, false if for
2069 * some reason it is being left as-is.
2070 */
2071 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002072 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002073 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002074 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002075 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002076 + ", result=" + resultCode + ", data=" + resultData
2077 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002078 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002079 return false;
2080 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002081
Craig Mautnerd44711d2013-02-23 11:24:36 -08002082 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002083 return true;
2084 }
2085
Craig Mautnerd2328952013-03-05 12:46:26 -08002086 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002087 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2088 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2089 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2090 ActivityRecord r = activities.get(activityNdx);
2091 if (r.resultTo == self && r.requestCode == requestCode) {
2092 if ((r.resultWho == null && resultWho == null) ||
2093 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2094 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2095 false);
2096 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002097 }
2098 }
2099 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002100 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002101 }
2102
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002103 final void finishTopRunningActivityLocked(ProcessRecord app) {
2104 ActivityRecord r = topRunningActivityLocked(null);
2105 if (r != null && r.app == app) {
2106 // If the top running activity is from this crashing
2107 // process, then terminate it to avoid getting in a loop.
2108 Slog.w(TAG, " Force finishing activity "
2109 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002110 int taskNdx = mTaskHistory.indexOf(r.task);
2111 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002112 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002113 // Also terminate any activities below it that aren't yet
2114 // stopped, to avoid a situation where one will get
2115 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002116 --activityNdx;
2117 if (activityNdx < 0) {
2118 do {
2119 --taskNdx;
2120 if (taskNdx < 0) {
2121 break;
2122 }
2123 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2124 } while (activityNdx < 0);
2125 }
2126 if (activityNdx >= 0) {
2127 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002128 if (r.state == ActivityState.RESUMED
2129 || r.state == ActivityState.PAUSING
2130 || r.state == ActivityState.PAUSED) {
Craig Mautner86d67a42013-05-14 10:34:38 -07002131 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002132 Slog.w(TAG, " Force finishing activity "
2133 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002134 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002135 }
2136 }
2137 }
2138 }
2139 }
2140
Craig Mautnerd2328952013-03-05 12:46:26 -08002141 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002142 ArrayList<ActivityRecord> activities = r.task.mActivities;
2143 for (int index = activities.indexOf(r); index >= 0; --index) {
2144 ActivityRecord cur = activities.get(index);
2145 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002146 break;
2147 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002148 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002149 }
2150 return true;
2151 }
2152
Dianne Hackborn5c607432012-02-28 14:44:19 -08002153 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2154 // send the result
2155 ActivityRecord resultTo = r.resultTo;
2156 if (resultTo != null) {
2157 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2158 + " who=" + r.resultWho + " req=" + r.requestCode
2159 + " res=" + resultCode + " data=" + resultData);
2160 if (r.info.applicationInfo.uid > 0) {
2161 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2162 resultTo.packageName, resultData,
2163 resultTo.getUriPermissionsLocked());
2164 }
2165 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2166 resultData);
2167 r.resultTo = null;
2168 }
2169 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2170
2171 // Make sure this HistoryRecord is not holding on to other resources,
2172 // because clients have remote IPC references to this object so we
2173 // can't assume that will go away and want to avoid circular IPC refs.
2174 r.results = null;
2175 r.pendingResults = null;
2176 r.newIntents = null;
2177 r.icicle = null;
2178 }
2179
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002180 /**
2181 * @return Returns true if this activity has been removed from the history
2182 * list, or false if it is still in the list and will be removed later.
2183 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002184 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2185 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002186 if (r.finishing) {
2187 Slog.w(TAG, "Duplicate finish request for " + r);
2188 return false;
2189 }
2190
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002191 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002192 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002193 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002194 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002195 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2196 final int index = activities.indexOf(r);
2197 if (index < (activities.size() - 1)) {
2198 ActivityRecord next = activities.get(index+1);
2199 if (r.frontOfTask) {
2200 // The next activity is now the front of the task.
2201 next.frontOfTask = true;
2202 }
2203 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2204 // If the caller asked that this activity (and all above it)
2205 // be cleared when the task is reset, don't lose that information,
2206 // but propagate it up to the next activity.
2207 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002208 }
2209 }
2210
2211 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002212 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002213 if (mService.mFocusedActivity == r) {
Craig Mautner29219d92013-04-16 20:19:12 -07002214 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002215 }
2216 }
2217
Dianne Hackborn5c607432012-02-28 14:44:19 -08002218 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002219
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002220 if (mService.mPendingThumbnails.size() > 0) {
2221 // There are clients waiting to receive thumbnails so, in case
2222 // this is an activity that someone is waiting for, add it
2223 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002224 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002225 }
2226
Craig Mautnerde4ef022013-04-07 19:01:33 -07002227 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002228 boolean endTask = index <= 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002229 if (DEBUG_TRANSITION) Slog.v(TAG,
2230 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002231 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002232 ? AppTransition.TRANSIT_TASK_CLOSE
2233 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002234
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002235 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002236 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002237
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002238 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002239 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2240 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2241 startPausingLocked(false, false);
2242 }
2243
2244 } else if (r.state != ActivityState.PAUSING) {
2245 // If the activity is PAUSING, we will complete the finish once
2246 // it is done pausing; else we can just directly finish it here.
2247 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002248 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002249 } else {
2250 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2251 }
2252
2253 return false;
2254 }
2255
Craig Mautnerf3333272013-04-22 10:55:53 -07002256 static final int FINISH_IMMEDIATELY = 0;
2257 static final int FINISH_AFTER_PAUSE = 1;
2258 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002259
Craig Mautnerf3333272013-04-22 10:55:53 -07002260 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002261 // First things first: if this activity is currently visible,
2262 // and the resumed activity is not yet visible, then hold off on
2263 // finishing until the resumed one becomes visible.
2264 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002265 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2266 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002267 if (mStackSupervisor.mStoppingActivities.size() > 3
2268 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002269 // If we already have a few activities waiting to stop,
2270 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002271 // them out. Or if r is the last of activity of the last task the stack
2272 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002273 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002274 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002275 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002276 }
2277 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002278 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2279 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002280 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002281 if (oomAdj) {
2282 mService.updateOomAdjLocked();
2283 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002284 return r;
2285 }
2286
2287 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002288 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002289 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002290 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002291 if (mResumedActivity == r) {
2292 mResumedActivity = null;
2293 }
2294 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002295 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002296 r.state = ActivityState.FINISHING;
2297
2298 if (mode == FINISH_IMMEDIATELY
2299 || prevState == ActivityState.STOPPED
2300 || prevState == ActivityState.INITIALIZING) {
2301 // If this activity is already stopped, we can just finish
2302 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002303 boolean activityRemoved = destroyActivityLocked(r, true,
2304 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002305 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002306 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002307 }
2308 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002309 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002310
2311 // Need to go through the full pause cycle to get this
2312 // activity into the stopped state and then finish it.
2313 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002314 mStackSupervisor.mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002315 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002316 return r;
2317 }
2318
Craig Mautnerd2328952013-03-05 12:46:26 -08002319 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002320 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002321 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002322 final TaskRecord task = srec.task;
2323 final ArrayList<ActivityRecord> activities = task.mActivities;
2324 final int start = activities.indexOf(srec);
2325 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002326 return false;
2327 }
2328 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002329 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002330 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002331 final ComponentName dest = destIntent.getComponent();
2332 if (start > 0 && dest != null) {
2333 for (int i = finishTo; i >= 0; i--) {
2334 ActivityRecord r = activities.get(i);
2335 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002336 r.info.name.equals(dest.getClassName())) {
2337 finishTo = i;
2338 parent = r;
2339 foundParentInTask = true;
2340 break;
2341 }
2342 }
2343 }
2344
2345 IActivityController controller = mService.mController;
2346 if (controller != null) {
2347 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2348 if (next != null) {
2349 // ask watcher if this is allowed
2350 boolean resumeOK = true;
2351 try {
2352 resumeOK = controller.activityResuming(next.packageName);
2353 } catch (RemoteException e) {
2354 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002355 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002356 }
2357
2358 if (!resumeOK) {
2359 return false;
2360 }
2361 }
2362 }
2363 final long origId = Binder.clearCallingIdentity();
2364 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002365 ActivityRecord r = activities.get(i);
2366 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002367 // Only return the supplied result for the first activity finished
2368 resultCode = Activity.RESULT_CANCELED;
2369 resultData = null;
2370 }
2371
2372 if (parent != null && foundParentInTask) {
2373 final int parentLaunchMode = parent.info.launchMode;
2374 final int destIntentFlags = destIntent.getFlags();
2375 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2376 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2377 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2378 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2379 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2380 } else {
2381 try {
2382 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2383 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002384 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002385 null, aInfo, parent.appToken, null,
2386 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2387 0, null, true, null);
2388 foundParentInTask = res == ActivityManager.START_SUCCESS;
2389 } catch (RemoteException e) {
2390 foundParentInTask = false;
2391 }
2392 requestFinishActivityLocked(parent.appToken, resultCode,
2393 resultData, "navigate-up", true);
2394 }
2395 }
2396 Binder.restoreCallingIdentity(origId);
2397 return foundParentInTask;
2398 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002399 /**
2400 * Perform the common clean-up of an activity record. This is called both
2401 * as part of destroyActivityLocked() (when destroying the client-side
2402 * representation) and cleaning things up as a result of its hosting
2403 * processing going away, in which case there is no remaining client-side
2404 * state to destroy so only the cleanup here is needed.
2405 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002406 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2407 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002408 if (mResumedActivity == r) {
2409 mResumedActivity = null;
2410 }
2411 if (mService.mFocusedActivity == r) {
2412 mService.mFocusedActivity = null;
2413 }
2414
2415 r.configDestroy = false;
2416 r.frozenBeforeDestroy = false;
2417
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002418 if (setState) {
2419 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2420 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002421 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002422 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002423 }
2424
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002425 // Make sure this record is no longer in the pending finishes list.
2426 // This could happen, for example, if we are trimming activities
2427 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002428 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002429 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002430
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002431 // Remove any pending results.
2432 if (r.finishing && r.pendingResults != null) {
2433 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2434 PendingIntentRecord rec = apr.get();
2435 if (rec != null) {
2436 mService.cancelIntentSenderLocked(rec, false);
2437 }
2438 }
2439 r.pendingResults = null;
2440 }
2441
2442 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002443 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002444 }
2445
2446 if (mService.mPendingThumbnails.size() > 0) {
2447 // There are clients waiting to receive thumbnails so, in case
2448 // this is an activity that someone is waiting for, add it
2449 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002450 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002451 }
2452
2453 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002454 removeTimeoutsForActivityLocked(r);
2455 }
2456
2457 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002458 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002459 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002460 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002461 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002462 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002463 }
2464
Dianne Hackborn5c607432012-02-28 14:44:19 -08002465 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002466 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2467 r.makeFinishing();
2468 if (DEBUG_ADD_REMOVE) {
2469 RuntimeException here = new RuntimeException("here");
2470 here.fillInStackTrace();
2471 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002472 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002473 final TaskRecord task = r.task;
2474 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002475 if (DEBUG_STACK) Slog.i(TAG,
2476 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002477 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002478 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002479 r.takeFromHistory();
2480 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002481 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002482 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002483 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002484 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002485 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002486 if (VALIDATE_TOKENS) {
2487 validateAppTokensLocked();
2488 }
2489 cleanUpActivityServicesLocked(r);
2490 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002491 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002492
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002493 /**
2494 * Perform clean-up of service connections in an activity record.
2495 */
2496 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2497 // Throw away any services that have been bound by this activity.
2498 if (r.connections != null) {
2499 Iterator<ConnectionRecord> it = r.connections.iterator();
2500 while (it.hasNext()) {
2501 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002502 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002503 }
2504 r.connections = null;
2505 }
2506 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002507
2508 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2509 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2510 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2511 mHandler.sendMessage(msg);
2512 }
2513
Dianne Hackborn28695e02011-11-02 21:59:51 -07002514 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002515 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002516 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002517 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2518 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2519 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2520 final ActivityRecord r = activities.get(activityNdx);
2521 if (r.finishing) {
2522 continue;
2523 }
2524 if (r.fullscreen) {
2525 lastIsOpaque = true;
2526 }
2527 if (owner != null && r.app != owner) {
2528 continue;
2529 }
2530 if (!lastIsOpaque) {
2531 continue;
2532 }
2533 // We can destroy this one if we have its icicle saved and
2534 // it is not in the process of pausing/stopping/finishing.
2535 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2536 && r.haveState && !r.visible && r.stopped
2537 && r.state != ActivityState.DESTROYING
2538 && r.state != ActivityState.DESTROYED) {
2539 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2540 + " resumed=" + mResumedActivity
2541 + " pausing=" + mPausingActivity);
2542 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2543 activityRemoved = true;
2544 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002545 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002546 }
2547 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002548 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002549 mStackSupervisor.resumeTopActivitiesLocked();
2550
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002551 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002552 }
2553
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002554 /**
2555 * Destroy the current CLIENT SIDE instance of an activity. This may be
2556 * called both when actually finishing an activity, or when performing
2557 * a configuration switch where we destroy the current client-side object
2558 * but then create a new client-side object for this same HistoryRecord.
2559 */
2560 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002561 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002562 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002563 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002564 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2565 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002566 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002567 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002568
2569 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002570
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002571 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002572
2573 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002574
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002575 if (hadApp) {
2576 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002577 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002578 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2579 mService.mHeavyWeightProcess = null;
2580 mService.mHandler.sendEmptyMessage(
2581 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2582 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002583 if (r.app.activities.size() == 0) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002584 // No longer have activities, so update oom adj.
2585 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002586 }
2587 }
2588
2589 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002590
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002591 try {
2592 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002593 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002594 r.configChangeFlags);
2595 } catch (Exception e) {
2596 // We can just ignore exceptions here... if the process
2597 // has crashed, our death notification will clean things
2598 // up.
2599 //Slog.w(TAG, "Exception thrown during finish", e);
2600 if (r.finishing) {
2601 removeActivityFromHistoryLocked(r);
2602 removedFromHistory = true;
2603 skipDestroy = true;
2604 }
2605 }
2606
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002607 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002608
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002609 // If the activity is finishing, we need to wait on removing it
2610 // from the list to give it a chance to do its cleanup. During
2611 // that time it may make calls back with its token so we need to
2612 // be able to find it on the list and so we don't want to remove
2613 // it from the list yet. Otherwise, we can just immediately put
2614 // it in the destroyed state since we are not removing it from the
2615 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002616 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002617 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2618 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002619 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002620 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002621 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2622 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002623 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002624 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002625 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002626 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002627 }
2628 } else {
2629 // remove this record from the history.
2630 if (r.finishing) {
2631 removeActivityFromHistoryLocked(r);
2632 removedFromHistory = true;
2633 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002634 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002635 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002636 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002637 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002638 }
2639 }
2640
2641 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002642
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002643 if (!mLRUActivities.remove(r) && hadApp) {
2644 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2645 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002646
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002647 return removedFromHistory;
2648 }
2649
Craig Mautnerd2328952013-03-05 12:46:26 -08002650 final void activityDestroyedLocked(IBinder token) {
2651 final long origId = Binder.clearCallingIdentity();
2652 try {
2653 ActivityRecord r = ActivityRecord.forToken(token);
2654 if (r != null) {
2655 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002656 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002657
2658 if (isInStackLocked(token) != null) {
2659 if (r.state == ActivityState.DESTROYING) {
2660 cleanUpActivityLocked(r, true, false);
2661 removeActivityFromHistoryLocked(r);
2662 }
2663 }
Craig Mautner05d29032013-05-03 13:40:13 -07002664 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002665 } finally {
2666 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002667 }
2668 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002669
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002670 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2671 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002672 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002673 if (DEBUG_CLEANUP) Slog.v(
2674 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002675 + " with " + i + " entries");
2676 while (i > 0) {
2677 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002678 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002679 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002680 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002681 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002682 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002683 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002684 }
2685 }
2686 }
2687
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002688 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2689 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002690 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2691 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002692 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2693 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002694 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002695 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002696 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2697 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002698
2699 boolean hasVisibleActivities = false;
2700
2701 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002702 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002703 if (DEBUG_CLEANUP) Slog.v(
2704 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002705 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2706 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2707 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2708 final ActivityRecord r = activities.get(activityNdx);
2709 --i;
2710 if (DEBUG_CLEANUP) Slog.v(
2711 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2712 if (r.app == app) {
2713 boolean remove;
2714 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2715 // Don't currently have state for the activity, or
2716 // it is finishing -- always remove it.
2717 remove = true;
2718 } else if (r.launchCount > 2 &&
2719 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2720 // We have launched this activity too many times since it was
2721 // able to run, so give up and remove it.
2722 remove = true;
2723 } else {
2724 // The process may be gone, but the activity lives on!
2725 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002726 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002727 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002728 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002729 RuntimeException here = new RuntimeException("here");
2730 here.fillInStackTrace();
2731 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2732 + ": haveState=" + r.haveState
2733 + " stateNotNeeded=" + r.stateNotNeeded
2734 + " finishing=" + r.finishing
2735 + " state=" + r.state, here);
2736 }
2737 if (!r.finishing) {
2738 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2739 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2740 r.userId, System.identityHashCode(r),
2741 r.task.taskId, r.shortComponentName,
2742 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07002743 if (r.state == ActivityState.RESUMED) {
2744 mService.updateUsageStats(r, false);
2745 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002746 }
2747 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002748
Craig Mautner0247fc82013-02-28 14:32:06 -08002749 } else {
2750 // We have the current state for this activity, so
2751 // it can be restarted later when needed.
2752 if (localLOGV) Slog.v(
2753 TAG, "Keeping entry, setting app to null");
2754 if (r.visible) {
2755 hasVisibleActivities = true;
2756 }
2757 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2758 + r);
2759 r.app = null;
2760 r.nowVisible = false;
2761 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002762 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08002763 "App died, clearing saved state of " + r);
2764 r.icicle = null;
2765 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002766 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002767
Craig Mautnerd2328952013-03-05 12:46:26 -08002768 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002769 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002770 }
2771 }
2772
2773 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002774 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002775
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002776 final void updateTransitLocked(int transit, Bundle options) {
2777 if (options != null) {
2778 ActivityRecord r = topRunningActivityLocked(null);
2779 if (r != null && r.state != ActivityState.RESUMED) {
2780 r.updateOptionsLocked(options);
2781 } else {
2782 ActivityOptions.abort(options);
2783 }
2784 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002785 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002786 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002787
Craig Mautnercae015f2013-02-08 14:31:27 -08002788 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002789 final TaskRecord task = taskForIdLocked(taskId);
2790 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08002791 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002792 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002793 }
Craig Mautneraab647e2013-02-28 16:31:36 -08002794 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2795 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07002796 // we'll just indicate that this task returns to the home task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002797 task.mActivities.get(0).mLaunchHomeTaskNext = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08002798 }
2799 moveTaskToFrontLocked(task, null, options);
2800 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002801 }
2802 return false;
2803 }
2804
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002805 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002806 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002807
Craig Mautner11bf9a52013-02-19 14:08:51 -08002808 final int numTasks = mTaskHistory.size();
2809 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07002810 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002811 // nothing to do!
2812 if (reason != null &&
2813 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2814 ActivityOptions.abort(options);
2815 } else {
2816 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
2817 }
2818 return;
2819 }
2820
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07002821 mStackSupervisor.moveHomeStack(isHomeStack());
2822
Craig Mautner11bf9a52013-02-19 14:08:51 -08002823 // Shift all activities with this task up to the top
2824 // of the stack, keeping them in the same internal order.
2825 mTaskHistory.remove(tr);
2826 mTaskHistory.add(tr);
2827
2828 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002829 if (reason != null &&
2830 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002831 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002832 ActivityRecord r = topRunningActivityLocked(null);
2833 if (r != null) {
2834 mNoAnimActivities.add(r);
2835 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002836 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002837 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08002838 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002839 }
Craig Mautner30e2d722013-02-12 11:30:16 -08002840
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002841 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002842
Craig Mautner05d29032013-05-03 13:40:13 -07002843 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08002844 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08002845
2846 if (VALIDATE_TOKENS) {
2847 validateAppTokensLocked();
2848 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002849 }
2850
2851 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002852 * Worker method for rearranging history stack. Implements the function of moving all
2853 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002854 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002855 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002856 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
2857 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002858 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002859 * @param task The taskId to collect and move to the bottom.
2860 * @return Returns true if the move completed, false if not.
2861 */
2862 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
2863 Slog.i(TAG, "moveTaskToBack: " + task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08002864
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002865 // If we have a watcher, preflight the move before committing to it. First check
2866 // for *other* available tasks, but if none are available, then try again allowing the
2867 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002868 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002869 ActivityRecord next = topRunningActivityLocked(null, task);
2870 if (next == null) {
2871 next = topRunningActivityLocked(null, 0);
2872 }
2873 if (next != null) {
2874 // ask watcher if this is allowed
2875 boolean moveOK = true;
2876 try {
2877 moveOK = mService.mController.activityResuming(next.packageName);
2878 } catch (RemoteException e) {
2879 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002880 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002881 }
2882 if (!moveOK) {
2883 return false;
2884 }
2885 }
2886 }
2887
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002888 if (DEBUG_TRANSITION) Slog.v(TAG,
2889 "Prepare to back transition: task=" + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002890
Craig Mautnerd2328952013-03-05 12:46:26 -08002891 final TaskRecord tr = taskForIdLocked(task);
2892 if (tr == null) {
2893 return false;
2894 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002895 mTaskHistory.remove(tr);
2896 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002897
2898 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002899 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2900 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002901 ActivityRecord r = topRunningActivityLocked(null);
2902 if (r != null) {
2903 mNoAnimActivities.add(r);
2904 }
2905 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002906 mWindowManager.prepareAppTransition(
Craig Mautner4b71aa12012-12-27 17:20:01 -08002907 AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002908 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002909 mWindowManager.moveTaskToBottom(task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08002910
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002911 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002912 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002913 }
2914
Craig Mautnerde4ef022013-04-07 19:01:33 -07002915 if (mResumedActivity != null && mResumedActivity.task == tr &&
2916 mResumedActivity.mLaunchHomeTaskNext) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07002917 // TODO: Can we skip the next line and just pass mResumedAct. to resumeHomeAct.()?
Craig Mautnerde4ef022013-04-07 19:01:33 -07002918 mResumedActivity.mLaunchHomeTaskNext = false;
Craig Mautner69ada552013-04-18 13:51:51 -07002919 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002920 }
2921
Craig Mautner05d29032013-05-03 13:40:13 -07002922 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002923 return true;
2924 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07002925
Craig Mautner8849a5e2013-04-02 16:41:03 -07002926 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002927 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08002928 final Uri data = r.intent.getData();
2929 final String strData = data != null ? data.toSafeString() : null;
2930
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002931 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002932 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002933 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08002934 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002935 }
2936
2937 /**
2938 * Make sure the given activity matches the current configuration. Returns
2939 * false if the activity had to be destroyed. Returns true if the
2940 * configuration is the same, or the activity will remain running as-is
2941 * for whatever reason. Ensures the HistoryRecord is updated with the
2942 * correct configuration and all other bookkeeping is handled.
2943 */
2944 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
2945 int globalChanges) {
2946 if (mConfigWillChange) {
2947 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
2948 "Skipping config check (will change): " + r);
2949 return true;
2950 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002951
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002952 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
2953 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07002954
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002955 // Short circuit: if the two configurations are the exact same
2956 // object (the common case), then there is nothing to do.
2957 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002958 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002959 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
2960 "Configuration unchanged in " + r);
2961 return true;
2962 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002963
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002964 // We don't worry about activities that are finishing.
2965 if (r.finishing) {
2966 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
2967 "Configuration doesn't matter in finishing " + r);
2968 r.stopFreezingScreenLocked(false);
2969 return true;
2970 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002971
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002972 // Okay we now are going to make this activity have the new config.
2973 // But then we need to figure out how it needs to deal with that.
2974 Configuration oldConfig = r.configuration;
2975 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002976
2977 // Determine what has changed. May be nothing, if this is a config
2978 // that has come back from the app after going idle. In that case
2979 // we just want to leave the official config object now in the
2980 // activity and do nothing else.
2981 final int changes = oldConfig.diff(newConfig);
2982 if (changes == 0 && !r.forceNewConfig) {
2983 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
2984 "Configuration no differences in " + r);
2985 return true;
2986 }
2987
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002988 // If the activity isn't currently running, just leave the new
2989 // configuration and it will pick that up next time it starts.
2990 if (r.app == null || r.app.thread == null) {
2991 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
2992 "Configuration doesn't matter not running " + r);
2993 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002994 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002995 return true;
2996 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002997
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002998 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07002999 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3000 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3001 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003002 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003003 + ", newConfig=" + newConfig);
3004 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003005 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003006 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3007 r.configChangeFlags |= changes;
3008 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003009 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003010 if (r.app == null || r.app.thread == null) {
3011 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003012 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003013 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003014 } else if (r.state == ActivityState.PAUSING) {
3015 // A little annoying: we are waiting for this activity to
3016 // finish pausing. Let's not do anything now, but just
3017 // flag that it needs to be restarted when done pausing.
3018 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003019 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003020 r.configDestroy = true;
3021 return true;
3022 } else if (r.state == ActivityState.RESUMED) {
3023 // Try to optimize this case: the configuration is changing
3024 // and we need to restart the top, resumed activity.
3025 // Instead of doing the normal handshaking, just say
3026 // "restart!".
3027 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003028 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003029 relaunchActivityLocked(r, r.configChangeFlags, true);
3030 r.configChangeFlags = 0;
3031 } else {
3032 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003033 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003034 relaunchActivityLocked(r, r.configChangeFlags, false);
3035 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003036 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003037
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003038 // All done... tell the caller we weren't able to keep this
3039 // activity around.
3040 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003041 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003042
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003043 // Default case: the activity can handle this new configuration, so
3044 // hand it over. Note that we don't need to give it the new
3045 // configuration, since we always send configuration changes to all
3046 // process when they happen so it can just use whatever configuration
3047 // it last got.
3048 if (r.app != null && r.app.thread != null) {
3049 try {
3050 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003051 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003052 } catch (RemoteException e) {
3053 // If process died, whatever.
3054 }
3055 }
3056 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003057
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003058 return true;
3059 }
3060
3061 private final boolean relaunchActivityLocked(ActivityRecord r,
3062 int changes, boolean andResume) {
3063 List<ResultInfo> results = null;
3064 List<Intent> newIntents = null;
3065 if (andResume) {
3066 results = r.results;
3067 newIntents = r.newIntents;
3068 }
3069 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3070 + " with results=" + results + " newIntents=" + newIntents
3071 + " andResume=" + andResume);
3072 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003073 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003074 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003075
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003076 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003077
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003078 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003079 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3080 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3081 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003082 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003083 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003084 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003085 // Note: don't need to call pauseIfSleepingLocked() here, because
3086 // the caller will only pass in 'andResume' if this activity is
3087 // currently resumed, which implies we aren't sleeping.
3088 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003089 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003090 }
3091
3092 if (andResume) {
3093 r.results = null;
3094 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003095 r.state = ActivityState.RESUMED;
3096 } else {
3097 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3098 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003099 }
3100
3101 return true;
3102 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003103
3104 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003105 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3106 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3107 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3108 final ActivityRecord r = activities.get(activityNdx);
3109 if (r.appToken == token) {
3110 return true;
3111 }
3112 if (r.fullscreen && !r.finishing) {
3113 return false;
3114 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003115 }
3116 }
3117 return true;
3118 }
3119
3120 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003121 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3122 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3123 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3124 final ActivityRecord r = activities.get(activityNdx);
3125 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003126 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003127 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003128 }
3129 }
3130 }
3131
3132 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3133 boolean didSomething = false;
3134 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003135 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3136 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3137 int numActivities = activities.size();
3138 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3139 ActivityRecord r = activities.get(activityNdx);
3140 final boolean samePackage = r.packageName.equals(name)
3141 || (name == null && r.userId == userId);
3142 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3143 && (samePackage || r.task == lastTask)
3144 && (r.app == null || evenPersistent || !r.app.persistent)) {
3145 if (!doit) {
3146 if (r.finishing) {
3147 // If this activity is just finishing, then it is not
3148 // interesting as far as something to stop.
3149 continue;
3150 }
3151 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003152 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003153 didSomething = true;
3154 Slog.i(TAG, " Force finishing activity " + r);
3155 if (samePackage) {
3156 if (r.app != null) {
3157 r.app.removed = true;
3158 }
3159 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003160 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003161 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003162 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3163 true)) {
3164 // r has been deleted from mActivities, accommodate.
3165 --numActivities;
3166 --activityNdx;
3167 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003168 }
3169 }
3170 }
3171 return didSomething;
3172 }
3173
3174 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003175 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003176 ActivityRecord topRecord = null;
Craig Mautneraab647e2013-02-28 16:31:36 -08003177 for (int taskNdx = mTaskHistory.size() - 1; maxNum > 0 && taskNdx >= 0;
3178 --maxNum, --taskNdx) {
3179 final TaskRecord task = mTaskHistory.get(taskNdx);
3180 ActivityRecord r = null;
3181 ActivityRecord top = null;
3182 int numActivities = 0;
3183 int numRunning = 0;
3184 final ArrayList<ActivityRecord> activities = task.mActivities;
3185 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3186 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003187
Craig Mautneraab647e2013-02-28 16:31:36 -08003188 // Initialize state for next task if needed.
3189 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3190 top = r;
3191 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003192 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003193
3194 // Add 'r' into the current task.
3195 numActivities++;
3196 if (r.app != null && r.app.thread != null) {
3197 numRunning++;
3198 }
3199
3200 if (localLOGV) Slog.v(
3201 TAG, r.intent.getComponent().flattenToShortString()
3202 + ": task=" + r.task);
3203 }
3204
3205 RunningTaskInfo ci = new RunningTaskInfo();
3206 ci.id = task.taskId;
3207 ci.baseActivity = r.intent.getComponent();
3208 ci.topActivity = top.intent.getComponent();
3209 if (top.thumbHolder != null) {
3210 ci.description = top.thumbHolder.lastDescription;
3211 }
3212 ci.numActivities = numActivities;
3213 ci.numRunning = numRunning;
3214 //System.out.println(
3215 // "#" + maxNum + ": " + " descr=" + ci.description);
3216 if (receiver != null) {
3217 if (localLOGV) Slog.v(
3218 TAG, "State=" + top.state + "Idle=" + top.idle
3219 + " app=" + top.app
3220 + " thr=" + (top.app != null ? top.app.thread : null));
3221 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3222 if (top.idle && top.app != null && top.app.thread != null) {
3223 topRecord = top;
3224 } else {
3225 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003226 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003227 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003228 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003229 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003230 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003231 }
3232 return topRecord;
3233 }
3234
3235 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003236 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003237 if (DEBUG_SWITCH) Slog.d(
3238 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003239 if (top >= 0) {
3240 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3241 int activityTop = activities.size() - 1;
3242 if (activityTop > 0) {
3243 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3244 "unhandled-back", true);
3245 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003246 }
3247 }
3248
Craig Mautnere79d42682013-04-01 19:01:53 -07003249 void handleAppDiedLocked(ProcessRecord app, boolean restarting) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003250 if (!containsApp(app)) {
3251 return;
3252 }
3253 // TODO: handle the case where an app spans multiple stacks.
Craig Mautnere79d42682013-04-01 19:01:53 -07003254 if (mPausingActivity != null && mPausingActivity.app == app) {
3255 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3256 "App died while pausing: " + mPausingActivity);
3257 mPausingActivity = null;
3258 }
3259 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3260 mLastPausedActivity = null;
3261 }
Craig Mautner37f748d2013-06-24 16:19:55 -07003262 final ActivityRecord top = topRunningActivityLocked(null);
3263 final boolean launchHomeTaskNext =
3264 top != null && top.app == app && top.mLaunchHomeTaskNext;
Craig Mautnere79d42682013-04-01 19:01:53 -07003265
3266 // Remove this application's activities from active lists.
3267 boolean hasVisibleActivities = removeHistoryRecordsForAppLocked(app);
3268
3269 if (!restarting) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003270 ActivityStack stack = mStackSupervisor.getFocusedStack();
Craig Mautner37f748d2013-06-24 16:19:55 -07003271 if (stack == null || launchHomeTaskNext) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003272 mStackSupervisor.resumeHomeActivity(null);
Craig Mautner05d29032013-05-03 13:40:13 -07003273 } else if (!mStackSupervisor.resumeTopActivitiesLocked(stack, null, null)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003274 // If there was nothing to resume, and we are not already
3275 // restarting this process, but there is a visible activity that
3276 // is hosted by the process... then make sure all visible
3277 // activities are running, taking care of restarting this
3278 // process.
3279 if (hasVisibleActivities) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003280 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
Craig Mautnere79d42682013-04-01 19:01:53 -07003281 }
3282 }
3283 }
3284 }
3285
Craig Mautnercae015f2013-02-08 14:31:27 -08003286 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003287 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3288 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3289 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3290 final ActivityRecord r = activities.get(activityNdx);
3291 if (r.app == app) {
3292 Slog.w(TAG, " Force finishing activity "
3293 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003294 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003295 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003296 }
3297 }
3298 }
3299
Dianne Hackborn390517b2013-05-30 15:03:32 -07003300 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003301 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003302 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003303 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3304 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003305 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3306 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003307 dumpClient, dumpPackage, needSep, header,
3308 " Task " + taskNdx + ": id #" + task.taskId);
3309 if (printed) {
3310 header = null;
3311 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003312 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003313 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003314 }
3315
3316 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3317 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3318
3319 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003320 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3321 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003322 }
3323 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003324 final int top = mTaskHistory.size() - 1;
3325 if (top >= 0) {
3326 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3327 int listTop = list.size() - 1;
3328 if (listTop >= 0) {
3329 activities.add(list.get(listTop));
3330 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003331 }
3332 } else {
3333 ItemMatcher matcher = new ItemMatcher();
3334 matcher.build(name);
3335
Craig Mautneraab647e2013-02-28 16:31:36 -08003336 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3337 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3338 if (matcher.match(r1, r1.intent.getComponent())) {
3339 activities.add(r1);
3340 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003341 }
3342 }
3343 }
3344
3345 return activities;
3346 }
3347
3348 ActivityRecord restartPackage(String packageName) {
3349 ActivityRecord starting = topRunningActivityLocked(null);
3350
3351 // All activities that came from the package must be
3352 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003353 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3354 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3355 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3356 final ActivityRecord a = activities.get(activityNdx);
3357 if (a.info.packageName.equals(packageName)) {
3358 a.forceNewConfig = true;
3359 if (starting != null && a == starting && a.visible) {
3360 a.startFreezingScreenLocked(starting.app,
3361 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3362 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003363 }
3364 }
3365 }
3366
3367 return starting;
3368 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003369
Craig Mautnerde4ef022013-04-07 19:01:33 -07003370 boolean removeTask(TaskRecord task) {
3371 mTaskHistory.remove(task);
3372 return mTaskHistory.size() == 0;
Craig Mautner0247fc82013-02-28 14:32:06 -08003373 }
3374
Craig Mautnerde4ef022013-04-07 19:01:33 -07003375 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003376 TaskRecord task = new TaskRecord(taskId, info, intent, this);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003377 if (toTop) {
3378 mTaskHistory.add(task);
3379 } else {
3380 mTaskHistory.add(0, task);
3381 }
3382 return task;
3383 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003384
3385 ArrayList<TaskRecord> getAllTasks() {
3386 return new ArrayList<TaskRecord>(mTaskHistory);
3387 }
3388
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003389 void addTask(final TaskRecord task, final boolean toTop) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003390 task.stack = this;
3391 if (toTop) {
3392 mTaskHistory.add(task);
3393 } else {
3394 mTaskHistory.add(0, task);
3395 }
3396 }
3397
3398 public int getStackId() {
3399 return mStackId;
3400 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003401
3402 @Override
3403 public String toString() {
3404 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3405 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003406}