blob: 690574cc7368f15d76b45760bc6a54cb0756fc6f [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;
Kenny Rootadd58212013-05-07 09:47:34 -070040import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080041import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080042import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
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;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080060import android.content.res.Resources;
61import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080062import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070064import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080065import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.os.Handler;
67import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090068import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070069import android.os.Message;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070070import android.os.RemoteException;
71import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080072import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070073import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070074import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070076import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077
Craig Mautnercae015f2013-02-08 14:31:27 -080078import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080079import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070080import java.lang.ref.WeakReference;
81import java.util.ArrayList;
82import java.util.Iterator;
83import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080084import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070085
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
Craig Mautner5eda9b32013-07-02 11:58:16 -0700120 // How long to wait for all background Activities to redraw following a call to
121 // convertToTranslucent().
122 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
123
John Reck172e87c2013-10-02 16:55:16 -0700124 static final boolean SCREENSHOT_FORCE_565 = ActivityManager
125 .isLowRamDeviceStatic() ? true : false;
126
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700127 enum ActivityState {
128 INITIALIZING,
129 RESUMED,
130 PAUSING,
131 PAUSED,
132 STOPPING,
133 STOPPED,
134 FINISHING,
135 DESTROYING,
136 DESTROYED
137 }
138
139 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700140 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800141
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700142 /**
143 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800144 * running) activities. It contains #TaskRecord objects.
145 */
146 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
147
148 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800149 * Used for validating app tokens with window manager.
150 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800151 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800152
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700153 /**
154 * List of running activities, sorted by recent usage.
155 * The first entry in the list is the least recently used.
156 * It contains HistoryRecord objects.
157 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700158 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700159
160 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700161 * Animations that for the current transition have requested not to
162 * be considered for the transition animation.
163 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700164 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700165
166 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700167 * When we are in the process of pausing an activity, before starting the
168 * next one, this variable holds the activity that is currently being paused.
169 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800170 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700171
172 /**
173 * This is the last activity that we put into the paused state. This is
174 * used to determine if we need to do an activity transition while sleeping,
175 * when we normally hold the top activity paused.
176 */
177 ActivityRecord mLastPausedActivity = null;
178
179 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700180 * Activities that specify No History must be removed once the user navigates away from them.
181 * If the device goes to sleep with such an activity in the paused state then we save it here
182 * and finish it later if another activity replaces it on wakeup.
183 */
184 ActivityRecord mLastNoHistoryActivity = null;
185
186 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700187 * Current activity that is resumed, or null if there is none.
188 */
189 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800190
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700191 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700192 * This is the last activity that has been started. It is only used to
193 * identify when multiple activities are started at once so that the user
194 * can be warned they may not be in the activity they think they are.
195 */
196 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800197
Craig Mautner5eda9b32013-07-02 11:58:16 -0700198 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
199 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
200 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
201 // Activity in mTranslucentActivityWaiting is notified via
202 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
203 // background activity being drawn then the same call will be made with a true value.
204 ActivityRecord mTranslucentActivityWaiting = null;
205 ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
206 new ArrayList<ActivityRecord>();
207
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700208 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700209 * Set when we know we are going to be calling updateConfiguration()
210 * soon, so want to skip intermediate config checks.
211 */
212 boolean mConfigWillChange;
213
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700214 long mLaunchStartTime = 0;
215 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800216
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800217 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800218 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
219 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
220 */
221 private ActivityRecord mLastScreenshotActivity = null;
222 private Bitmap mLastScreenshotBitmap = null;
223
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800224 int mThumbnailWidth = -1;
225 int mThumbnailHeight = -1;
226
Craig Mautner858d8a62013-04-23 17:08:34 -0700227 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700228
Craig Mautnerc00204b2013-03-05 15:02:14 -0800229 final int mStackId;
230
Craig Mautner4a1cb222013-12-04 16:14:06 -0800231 final ActivityContainer mActivityContainer;
232
Craig Mautner27084302013-03-25 08:05:25 -0700233 /** Run all ActivityStacks through this */
234 final ActivityStackSupervisor mStackSupervisor;
235
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700236 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700237 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
238 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
239 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
240 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700241 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700242
243 static class ScheduleDestroyArgs {
244 final ProcessRecord mOwner;
245 final boolean mOomAdj;
246 final String mReason;
247 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
248 mOwner = owner;
249 mOomAdj = oomAdj;
250 mReason = reason;
251 }
252 }
253
Zoran Marcetaf958b322012-08-09 20:27:12 +0900254 final Handler mHandler;
255
256 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700257 //public Handler() {
258 // if (localLOGV) Slog.v(TAG, "Handler started!");
259 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700260 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900261 super(looper);
262 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700263
Zoran Marcetaf958b322012-08-09 20:27:12 +0900264 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700265 public void handleMessage(Message msg) {
266 switch (msg.what) {
267 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800268 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700269 // We don't at this point know if the activity is fullscreen,
270 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800271 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700272 synchronized (mService) {
273 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700274 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700275 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700276 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800277 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700278 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700279 case LAUNCH_TICK_MSG: {
280 ActivityRecord r = (ActivityRecord)msg.obj;
281 synchronized (mService) {
282 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700283 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700284 }
285 }
286 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700287 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800288 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700289 // We don't at this point know if the activity is fullscreen,
290 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800291 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800292 synchronized (mService) {
293 activityDestroyedLocked(r != null ? r.appToken : null);
294 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700295 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700296 case STOP_TIMEOUT_MSG: {
297 ActivityRecord r = (ActivityRecord)msg.obj;
298 // We don't at this point know if the activity is fullscreen,
299 // so we need to be conservative and assume it isn't.
300 Slog.w(TAG, "Activity stop timeout for " + r);
301 synchronized (mService) {
302 if (r.isInHistory()) {
303 activityStoppedLocked(r, null, null, null);
304 }
305 }
306 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700307 case DESTROY_ACTIVITIES_MSG: {
308 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
309 synchronized (mService) {
310 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
311 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700312 } break;
313 case TRANSLUCENT_TIMEOUT_MSG: {
314 synchronized (mService) {
315 notifyActivityDrawnLocked(null);
316 }
317 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700318 }
319 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800320 }
321
Craig Mautner000f0022013-02-26 15:04:29 -0800322 private int numActivities() {
323 int count = 0;
324 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
325 count += mTaskHistory.get(taskNdx).mActivities.size();
326 }
327 return count;
328 }
329
Craig Mautner4a1cb222013-12-04 16:14:06 -0800330 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer) {
331 mActivityContainer = activityContainer;
332 mStackSupervisor = activityContainer.getOuter();
333 mService = mStackSupervisor.mService;
334 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
335 mWindowManager = mService.mWindowManager;
336 mStackId = activityContainer.mStackId;
337 mCurrentUser = mService.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700338 }
Craig Mautner5962b122012-10-05 14:45:52 -0700339
Craig Mautner6b74cb52013-09-27 17:02:21 -0700340 boolean okToShow(ActivityRecord r) {
Craig Mautner5962b122012-10-05 14:45:52 -0700341 return r.userId == mCurrentUser
342 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
343 }
344
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700345 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800346 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700347 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
348 if (r != null) {
349 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800350 }
351 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700352 return null;
353 }
354
355 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800356 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
357 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800358 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800359 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
360 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800361 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
362 return r;
363 }
364 }
365 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700366 return null;
367 }
368
369 /**
370 * This is a simplified version of topRunningActivityLocked that provides a number of
371 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800372 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700373 * @param token If non-null, any history records matching this token will be skipped.
374 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800375 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700376 * @return Returns the HistoryRecord of the next activity on the stack.
377 */
378 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800379 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
380 TaskRecord task = mTaskHistory.get(taskNdx);
381 if (task.taskId == taskId) {
382 continue;
383 }
384 ArrayList<ActivityRecord> activities = task.mActivities;
385 for (int i = activities.size() - 1; i >= 0; --i) {
386 final ActivityRecord r = activities.get(i);
387 // Note: the taskId check depends on real taskId fields being non-zero
388 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
389 return r;
390 }
391 }
392 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700393 return null;
394 }
395
Craig Mautner8849a5e2013-04-02 16:41:03 -0700396 final ActivityRecord topActivity() {
397 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700398 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700399 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
400 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
401 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700402 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700403 }
404 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700405 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700406 }
407
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700408 final TaskRecord topTask() {
409 final int size = mTaskHistory.size();
410 if (size > 0) {
411 return mTaskHistory.get(size - 1);
412 }
413 return null;
414 }
415
Craig Mautnerd2328952013-03-05 12:46:26 -0800416 TaskRecord taskForIdLocked(int id) {
417 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
418 final TaskRecord task = mTaskHistory.get(taskNdx);
419 if (task.taskId == id) {
420 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800421 }
422 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700423 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700424 }
425
Craig Mautnerd2328952013-03-05 12:46:26 -0800426 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800427 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800428 if (r != null) {
429 final TaskRecord task = r.task;
430 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
431 if (task.stack != this) Slog.w(TAG,
432 "Illegal state! task does not point to stack it is in.");
433 return r;
434 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800435 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800436 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800437 }
438
Craig Mautner2420ead2013-04-01 17:13:20 -0700439 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700440 final boolean hadit = mLRUActivities.remove(r);
441 mLRUActivities.add(r);
442 return hadit;
443 }
444
Craig Mautnerde4ef022013-04-07 19:01:33 -0700445 final boolean isHomeStack() {
446 return mStackId == HOME_STACK_ID;
447 }
448
Craig Mautner4a1cb222013-12-04 16:14:06 -0800449 ArrayList<ActivityStack> getStacksLocked() {
450 if (mActivityContainer.isAttached()) {
451 return mActivityContainer.mActivityDisplayInfo.stacks;
452 }
453 return null;
454 }
455
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700456 /**
457 * Returns the top activity in any existing task matching the given
458 * Intent. Returns null if no such task is found.
459 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700460 ActivityRecord findTaskLocked(ActivityRecord target) {
461 Intent intent = target.intent;
462 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700463 ComponentName cls = intent.getComponent();
464 if (info.targetActivity != null) {
465 cls = new ComponentName(info.packageName, info.targetActivity);
466 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700467 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800468
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700469 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800470 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
471 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700472 if (task.userId != userId) {
473 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700474 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700475 continue;
476 }
Craig Mautner000f0022013-02-26 15:04:29 -0800477 final ActivityRecord r = task.getTopActivity();
478 if (r == null || r.finishing || r.userId != userId ||
479 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700480 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800481 continue;
482 }
483
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700484 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
485 + r.task.intent.getComponent().flattenToShortString()
486 + "/aff=" + r.task.affinity + " to new cls="
487 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Craig Mautner000f0022013-02-26 15:04:29 -0800488 if (task.affinity != null) {
489 if (task.affinity.equals(info.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700490 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700491 return r;
492 }
Craig Mautner000f0022013-02-26 15:04:29 -0800493 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700494 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800495 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700496 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
497 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800498 return r;
499 } else if (task.affinityIntent != null
500 && task.affinityIntent.getComponent().equals(cls)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700501 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800502 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700503 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
504 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800505 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700506 } else if (DEBUG_TASKS) {
507 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700508 }
509 }
510
511 return null;
512 }
513
514 /**
515 * Returns the first activity (starting from the top of the stack) that
516 * is the same as the given activity. Returns null if no such activity
517 * is found.
518 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700519 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700520 ComponentName cls = intent.getComponent();
521 if (info.targetActivity != null) {
522 cls = new ComponentName(info.packageName, info.targetActivity);
523 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700524 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700525
Craig Mautner000f0022013-02-26 15:04:29 -0800526 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700527 TaskRecord task = mTaskHistory.get(taskNdx);
528 if (task.userId != mCurrentUser) {
529 return null;
530 }
531 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800532 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
533 ActivityRecord r = activities.get(activityNdx);
534 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700535 //Slog.i(TAG, "Found matching class!");
536 //dump();
537 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
538 return r;
539 }
540 }
541 }
542
543 return null;
544 }
545
Amith Yamasani742a6712011-05-04 14:49:28 -0700546 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700547 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700548 */
Craig Mautner93529a42013-10-04 15:03:13 -0700549 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800550 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700551 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800552 }
553 mCurrentUser = userId;
554
555 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800556 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800557 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700558 TaskRecord task = mTaskHistory.get(i);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800559 if (task.userId == userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700560 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
561 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800562 mTaskHistory.remove(i);
563 mTaskHistory.add(task);
564 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800565 // Use same value for i.
566 } else {
567 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800568 }
569 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700570 if (VALIDATE_TOKENS) {
571 validateAppTokensLocked();
572 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700573 }
574
Craig Mautner2420ead2013-04-01 17:13:20 -0700575 void minimalResumeActivityLocked(ActivityRecord r) {
576 r.state = ActivityState.RESUMED;
577 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
578 + " (starting new instance)");
579 r.stopped = false;
580 mResumedActivity = r;
581 r.task.touchActiveTime();
582 mService.addRecentTaskLocked(r.task);
583 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700584 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700585 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700586 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700587 }
588
Dianne Hackborncee04b52013-07-03 17:01:28 -0700589 private void startLaunchTraces() {
590 if (mFullyDrawnStartTime != 0) {
591 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
592 }
593 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
594 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
595 }
596
597 private void stopFullyDrawnTraceIfNeeded() {
598 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
599 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
600 mFullyDrawnStartTime = 0;
601 }
602 }
603
Craig Mautnere79d42682013-04-01 19:01:53 -0700604 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700605 if (r.displayStartTime == 0) {
606 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
607 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700608 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700609 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700610 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700611 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700612 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700613 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700614 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700615 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800616
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700617 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700618 // Make sure that there is no activity waiting for this to launch.
619 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
620 r.displayStartTime = r.fullyDrawnStartTime = 0;
621 } else {
622 mStackSupervisor.removeTimeoutsForActivityLocked(r);
623 mStackSupervisor.scheduleIdleTimeoutLocked(r);
624 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700625 }
626
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800627 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800628 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800629 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
630 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
631 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
632 activities.get(activityNdx).setSleeping(false);
633 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800634 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800635 }
636
Craig Mautner0eea92c2013-05-16 13:35:39 -0700637 /**
638 * @return true if something must be done before going to sleep.
639 */
640 boolean checkReadyForSleepLocked() {
641 if (mResumedActivity != null) {
642 // Still have something resumed; can't sleep until it is paused.
643 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
644 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
645 startPausingLocked(false, true);
646 return true;
647 }
648 if (mPausingActivity != null) {
649 // Still waiting for something to pause; can't sleep yet.
650 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
651 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800652 }
653
Craig Mautner0eea92c2013-05-16 13:35:39 -0700654 return false;
655 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800656
Craig Mautner0eea92c2013-05-16 13:35:39 -0700657 void goToSleep() {
658 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800659
Craig Mautner0eea92c2013-05-16 13:35:39 -0700660 // Make sure any stopped but visible activities are now sleeping.
661 // This ensures that the activity's onStop() is called.
662 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
663 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
664 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
665 final ActivityRecord r = activities.get(activityNdx);
666 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
667 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800668 }
669 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800670 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700671 }
Craig Mautner59c00972012-07-30 12:10:24 -0700672
Dianne Hackbornd2835932010-12-13 16:28:46 -0800673 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800674 if (who.noDisplay) {
675 return null;
676 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800677
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700678 TaskRecord tr = who.task;
Craig Mautner34740402013-10-11 11:05:35 -0700679 if (mService.getMostRecentTask() != tr && tr.intent != null &&
680 (tr.intent.getFlags() & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) != 0) {
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700681 // If this task is being excluded from recents, we don't want to take
682 // the expense of capturing a thumbnail, since we will never show it.
683 return null;
684 }
685
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800686 Resources res = mService.mContext.getResources();
687 int w = mThumbnailWidth;
688 int h = mThumbnailHeight;
689 if (w < 0) {
690 mThumbnailWidth = w =
691 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
692 mThumbnailHeight = h =
693 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
694 }
695
696 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800697 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700698 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800699 || mLastScreenshotBitmap.getWidth() != w
700 || mLastScreenshotBitmap.getHeight() != h) {
701 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700702 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700703 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800704 }
705 if (mLastScreenshotBitmap != null) {
John Reck172e87c2013-10-02 16:55:16 -0700706 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800707 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800708 }
709 return null;
710 }
711
Craig Mautnercf910b02013-04-23 11:23:27 -0700712 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800713 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800714 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700715 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800716 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700717 ActivityRecord prev = mResumedActivity;
718 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700719 Slog.e(TAG, "Trying to pause when nothing is resumed",
720 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700721 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700722 return;
723 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700724 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
725 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700726 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800727 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700728 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700729 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
730 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700731 prev.state = ActivityState.PAUSING;
732 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700733 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700734 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
735 if (next == null || next.task != prev.task) {
736 prev.updateThumbnail(screenshotActivities(prev), null);
737 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700738 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700739
740 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800741
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700742 if (prev.app != null && prev.app.thread != null) {
743 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
744 try {
745 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700746 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700747 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700748 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800749 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
750 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700751 } catch (Exception e) {
752 // Ignore exception, if process died other code will cleanup.
753 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800754 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700755 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700756 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700757 }
758 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800759 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700760 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700761 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700762 }
763
764 // If we are not going to sleep, we want to ensure the device is
765 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700766 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700767 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700768 }
769
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800770 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700771 // Have the window manager pause its key dispatching until the new
772 // activity has started. If we're pausing the activity just because
773 // the screen is being turned off and the UI is sleeping, don't interrupt
774 // key dispatch; the same activity will pick it up again on wakeup.
775 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800776 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700777 } else {
778 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
779 }
780
781 // Schedule a pause timeout in case the app doesn't respond.
782 // We don't give it much time because this directly impacts the
783 // responsiveness seen by the user.
784 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
785 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700786 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700787 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
788 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
789 } else {
790 // This activity failed to schedule the
791 // pause, so just treat it as being paused now.
792 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700793 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700794 }
795 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700796
Craig Mautnerd2328952013-03-05 12:46:26 -0800797 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700798 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800799 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700800
Craig Mautnerd2328952013-03-05 12:46:26 -0800801 final ActivityRecord r = isInStackLocked(token);
802 if (r != null) {
803 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
804 if (mPausingActivity == r) {
805 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
806 + (timeout ? " (due to timeout)" : " (pause complete)"));
807 r.state = ActivityState.PAUSED;
808 completePauseLocked();
809 } else {
810 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
811 r.userId, System.identityHashCode(r), r.shortComponentName,
812 mPausingActivity != null
813 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700814 }
815 }
816 }
817
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700818 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
819 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700820 if (r.state != ActivityState.STOPPING) {
821 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
822 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
823 return;
824 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700825 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700826 if (icicle != null) {
827 // If icicle is null, this is happening due to a timeout, so we
828 // haven't really saved the state.
829 r.icicle = icicle;
830 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800831 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700832 r.updateThumbnail(thumbnail, description);
833 }
834 if (!r.stopped) {
835 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
836 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
837 r.stopped = true;
838 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700839 if (r.finishing) {
840 r.clearOptionsLocked();
841 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700842 if (r.configDestroy) {
843 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700844 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700845 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700846 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800847 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700848 }
849 }
850 }
851
Craig Mautnerc8143c62013-09-03 12:15:57 -0700852 private void completePauseLocked() {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800853 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700854 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800855
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800856 if (prev != null) {
857 if (prev.finishing) {
858 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700859 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800860 } else if (prev.app != null) {
861 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
862 if (prev.waitingVisible) {
863 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700864 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800865 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
866 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800867 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800868 if (prev.configDestroy) {
869 // The previous is being paused because the configuration
870 // is changing, which means it is actually stopping...
871 // To juggle the fact that we are also starting a new
872 // instance right now, we need to first completely stop
873 // the current instance before starting the new one.
874 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
875 destroyActivityLocked(prev, true, false, "pause-config");
876 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700877 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700878 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
879 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800880 // If we already have a few activities waiting to stop,
881 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700882 // them out. Or if r is the last of activity of the last task the stack
883 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800884 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700885 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800886 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700887 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800888 }
889 }
890 } else {
891 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
892 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800893 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800894 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700895 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800896
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700897 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700898 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700899 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800900 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700901 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700902 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700903 if (top == null || (prev != null && top != prev)) {
904 // If there are no more activities available to run,
905 // do resume anyway to start something. Also if the top
906 // activity on the stack is not the just paused activity,
907 // we need to go ahead and resume it to ensure we complete
908 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700909 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700910 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700911 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800912
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800913 if (prev != null) {
914 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700915
Craig Mautner525f3d92013-05-07 14:01:50 -0700916 if (prev.app != null && prev.cpuTimeAtResume > 0
917 && mService.mBatteryStatsService.isOnBattery()) {
918 long diff;
Dianne Hackbornd2932242013-08-05 18:18:42 -0700919 synchronized (mService.mProcessCpuThread) {
920 diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
Craig Mautner525f3d92013-05-07 14:01:50 -0700921 - prev.cpuTimeAtResume;
922 }
923 if (diff > 0) {
924 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
925 synchronized (bsi) {
926 BatteryStatsImpl.Uid.Proc ps =
927 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -0700928 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -0700929 if (ps != null) {
930 ps.addForegroundTimeLocked(diff);
931 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700932 }
933 }
934 }
Craig Mautner525f3d92013-05-07 14:01:50 -0700935 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700936 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700937 }
938
939 /**
940 * Once we know that we have asked an application to put an activity in
941 * the resumed state (either by launching it or explicitly telling it),
942 * this function updates the rest of our state to match that fact.
943 */
Craig Mautner525f3d92013-05-07 14:01:50 -0700944 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700945 next.idle = false;
946 next.results = null;
947 next.newIntents = null;
Craig Mautner07566322013-09-26 16:42:55 -0700948 if (next.nowVisible) {
949 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
950 mStackSupervisor.dismissKeyguard();
951 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700952
953 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -0700954 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700955
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700956 mStackSupervisor.reportResumedActivityLocked(next);
957
958 next.resumeKeyDispatchingLocked();
959 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700960
961 // Mark the point when the activity is resuming
962 // TODO: To be more accurate, the mark should be before the onCreate,
963 // not after the onResume. But for subsequent starts, onResume is fine.
964 if (next.app != null) {
Dianne Hackbornd2932242013-08-05 18:18:42 -0700965 synchronized (mService.mProcessCpuThread) {
966 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700967 }
968 } else {
969 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
970 }
971 }
972
Craig Mautner580ea812013-04-25 12:58:38 -0700973 /**
Craig Mautner39e1c5a2013-10-23 15:14:22 -0700974 * Determine if home should be visible below the passed record.
975 * @param record activity we are querying for.
976 * @return true if home is visible below the passed activity, false otherwise.
977 */
978 boolean isActivityOverHome(ActivityRecord record) {
979 // Start at record and go down, look for either home or a visible fullscreen activity.
980 final TaskRecord recordTask = record.task;
981 for (int taskNdx = mTaskHistory.indexOf(recordTask); taskNdx >= 0; --taskNdx) {
982 TaskRecord task = mTaskHistory.get(taskNdx);
983 final ArrayList<ActivityRecord> activities = task.mActivities;
984 final int startNdx =
985 task == recordTask ? activities.indexOf(record) : activities.size() - 1;
986 for (int activityNdx = startNdx; activityNdx >= 0; --activityNdx) {
987 final ActivityRecord r = activities.get(activityNdx);
988 if (r.isHomeActivity()) {
989 return true;
990 }
Craig Mautner76be9d22013-11-04 16:01:22 -0800991 if (!r.finishing && r.fullscreen) {
992 // Passed activity is over a fullscreen activity.
Craig Mautner39e1c5a2013-10-23 15:14:22 -0700993 return false;
994 }
995 }
996 if (task.mOnTopOfHome) {
997 // Got to the bottom of a task on top of home without finding a visible fullscreen
998 // activity. Home is visible.
999 return true;
1000 }
1001 }
1002 // Got to the bottom of this stack and still don't know. If this is over the home stack
1003 // then record is over home. May not work if we ever get more than two layers.
1004 return mStackSupervisor.isFrontStack(this);
1005 }
1006
1007 /**
Craig Mautner580ea812013-04-25 12:58:38 -07001008 * Version of ensureActivitiesVisible that can easily be called anywhere.
1009 */
1010 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1011 return ensureActivitiesVisibleLocked(starting, configChanges, false);
1012 }
1013
1014 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1015 boolean forceHomeShown) {
1016 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -07001017 return r != null &&
1018 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -07001019 }
1020
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001021 /**
1022 * Make sure that all activities that need to be visible (that is, they
1023 * currently can be seen by the user) actually are.
1024 */
Craig Mautner580ea812013-04-25 12:58:38 -07001025 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
1026 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001027 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001028 TAG, "ensureActivitiesVisible behind " + top
1029 + " configChanges=0x" + Integer.toHexString(configChanges));
1030
Craig Mautner5eda9b32013-07-02 11:58:16 -07001031 if (mTranslucentActivityWaiting != top) {
1032 mUndrawnActivitiesBelowTopTranslucent.clear();
1033 if (mTranslucentActivityWaiting != null) {
1034 // Call the callback with a timeout indication.
1035 notifyActivityDrawnLocked(null);
1036 mTranslucentActivityWaiting = null;
1037 }
1038 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1039 }
1040
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001041 // If the top activity is not fullscreen, then we need to
1042 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001043 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -07001044 boolean showHomeBehindStack = false;
1045 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
1046 !(forceHomeShown && isHomeStack());
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001047 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001048 final TaskRecord task = mTaskHistory.get(taskNdx);
1049 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001050 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1051 final ActivityRecord r = activities.get(activityNdx);
1052 if (r.finishing) {
1053 continue;
1054 }
1055 if (aboveTop && r != top) {
1056 continue;
1057 }
1058 aboveTop = false;
1059 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001060 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001061 TAG, "Make visible? " + r + " finishing=" + r.finishing
1062 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001063
Craig Mautnerd44711d2013-02-23 11:24:36 -08001064 final boolean doThisProcess = onlyThisProcess == null
1065 || onlyThisProcess.equals(r.processName);
1066
1067 // First: if this is not the current activity being started, make
1068 // sure it matches the current configuration.
1069 if (r != starting && doThisProcess) {
1070 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001071 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001072
1073 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001074 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001075 // This activity needs to be visible, but isn't even
1076 // running... get it started, but don't resume it
1077 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001078 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001079 if (r != starting) {
1080 r.startFreezingScreenLocked(r.app, configChanges);
1081 }
1082 if (!r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001083 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001084 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001085 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001086 }
1087 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001088 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001089 }
1090 }
1091
1092 } else if (r.visible) {
1093 // If this activity is already visible, then there is nothing
1094 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001095 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001096 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001097
1098 } else if (onlyThisProcess == null) {
1099 // This activity is not currently visible, but is running.
1100 // Tell it to become visible.
1101 r.visible = true;
1102 if (r.state != ActivityState.RESUMED && r != starting) {
1103 // If this activity is paused, tell it
1104 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001105 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001106 TAG, "Making visible and scheduling visibility: " + r);
1107 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001108 if (mTranslucentActivityWaiting != null) {
1109 mUndrawnActivitiesBelowTopTranslucent.add(r);
1110 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001111 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001112 r.sleeping = false;
1113 r.app.pendingUiClean = true;
1114 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1115 r.stopFreezingScreenLocked(false);
1116 } catch (Exception e) {
1117 // Just skip on any failure; we'll make it
1118 // visible when it next restarts.
1119 Slog.w(TAG, "Exception thrown making visibile: "
1120 + r.intent.getComponent(), e);
1121 }
1122 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001123 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001124
Craig Mautnerd44711d2013-02-23 11:24:36 -08001125 // Aggregate current change flags.
1126 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001127
Craig Mautnerd44711d2013-02-23 11:24:36 -08001128 if (r.fullscreen) {
1129 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001130 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1131 behindFullscreen = true;
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001132 } else if (isActivityOverHome(r)) {
1133 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1134 showHomeBehindStack = true;
Craig Mautnerf41bcd42013-11-07 11:51:29 -08001135 behindFullscreen = !isHomeStack();
Craig Mautnerd44711d2013-02-23 11:24:36 -08001136 }
1137 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001138 if (DEBUG_VISBILITY) Slog.v(
1139 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1140 + " state=" + r.state
1141 + " behindFullscreen=" + behindFullscreen);
1142 // Now for any activities that aren't visible to the user, make
1143 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001144 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001145 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001146 r.visible = false;
1147 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001148 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001149 switch (r.state) {
1150 case STOPPING:
1151 case STOPPED:
1152 if (r.app != null && r.app.thread != null) {
1153 if (DEBUG_VISBILITY) Slog.v(
1154 TAG, "Scheduling invisibility: " + r);
1155 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1156 }
1157 break;
1158
1159 case INITIALIZING:
1160 case RESUMED:
1161 case PAUSING:
1162 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001163 // This case created for transitioning activities from
1164 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautnere5273b42013-09-09 12:57:47 -07001165 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1166 mStackSupervisor.mStoppingActivities.add(r);
1167 }
1168 mStackSupervisor.scheduleIdleLocked();
Craig Mautner4addfc52013-06-25 08:05:45 -07001169 break;
1170
1171 default:
1172 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001173 }
1174 } catch (Exception e) {
1175 // Just skip on any failure; we'll make it
1176 // visible when it next restarts.
1177 Slog.w(TAG, "Exception thrown making hidden: "
1178 + r.intent.getComponent(), e);
1179 }
1180 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001181 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001182 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001183 }
1184 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001185 }
Craig Mautner580ea812013-04-25 12:58:38 -07001186 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001187 }
Craig Mautner58547802013-03-05 08:23:53 -08001188
Craig Mautner5eda9b32013-07-02 11:58:16 -07001189 void convertToTranslucent(ActivityRecord r) {
1190 mTranslucentActivityWaiting = r;
1191 mUndrawnActivitiesBelowTopTranslucent.clear();
1192 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1193 }
1194
1195 /**
1196 * Called as activities below the top translucent activity are redrawn. When the last one is
1197 * redrawn notify the top activity by calling
1198 * {@link Activity#onTranslucentConversionComplete}.
1199 *
1200 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1201 * occurred and the activity will be notified immediately.
1202 */
1203 void notifyActivityDrawnLocked(ActivityRecord r) {
1204 if ((r == null)
1205 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1206 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1207 // The last undrawn activity below the top has just been drawn. If there is an
1208 // opaque activity at the top, notify it that it can become translucent safely now.
1209 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1210 mTranslucentActivityWaiting = null;
1211 mUndrawnActivitiesBelowTopTranslucent.clear();
1212 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1213
1214 if (waitingActivity != null && waitingActivity.app != null &&
1215 waitingActivity.app.thread != null) {
1216 try {
1217 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1218 waitingActivity.appToken, r != null);
1219 } catch (RemoteException e) {
1220 }
1221 }
1222 }
1223 }
1224
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001225 /**
1226 * Ensure that the top activity in the stack is resumed.
1227 *
1228 * @param prev The previously resumed activity, for when in the process
1229 * of pausing; can be null to call from elsewhere.
1230 *
1231 * @return Returns true if something is being resumed, or false if
1232 * nothing happened.
1233 */
1234 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001235 return resumeTopActivityLocked(prev, null);
1236 }
1237
1238 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001239 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1240
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001241 // Find the first activity that is not finishing.
1242 ActivityRecord next = topRunningActivityLocked(null);
1243
1244 // Remember how we'll process this pause/resume situation, and ensure
1245 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001246 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1247 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001248
1249 if (next == null) {
1250 // There are no more activities! Let's just start up the
1251 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001252 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001253 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001254 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001255 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001256 }
1257
1258 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001259
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001260 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001261 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1262 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001263 // Make sure we have executed any pending transitions, since there
1264 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001265 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001266 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001267 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001268 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001269 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001270 return false;
1271 }
1272
Craig Mautner525f3d92013-05-07 14:01:50 -07001273 final TaskRecord nextTask = next.task;
1274 final TaskRecord prevTask = prev != null ? prev.task : null;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001275 if (prevTask != null && prevTask.mOnTopOfHome && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001276 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001277 if (prevTask == nextTask) {
Craig Mautner3b475fe2013-12-16 15:58:31 -08001278 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001279 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001280 // This task is going away but it was supposed to return to the home task.
1281 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001282 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001283 mTaskHistory.get(taskNdx).mOnTopOfHome = true;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001284 } else {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001285 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Launching home next");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001286 return mStackSupervisor.resumeHomeActivity(prev);
1287 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001288 }
1289
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001290 // If we are sleeping, and there is no resumed activity, and the top
1291 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001292 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001293 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001294 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001295 // Make sure we have executed any pending transitions, since there
1296 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001297 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001298 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001299 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001300 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001301 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001302 return false;
1303 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001304
1305 // Make sure that the user who owns this activity is started. If not,
1306 // we will just leave it as is because someone should be bringing
1307 // another user's activities to the top of the stack.
1308 if (mService.mStartedUsers.get(next.userId) == null) {
1309 Slog.w(TAG, "Skipping resume of top activity " + next
1310 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001311 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001312 return false;
1313 }
1314
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001315 // The activity may be waiting for stop, but that is no longer
1316 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001317 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001318 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001319 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001320 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001321
Dianne Hackborn84375872012-06-01 19:03:50 -07001322 next.updateOptionsLocked(options);
1323
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001324 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1325
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001326 // If we are currently pausing an activity, then don't do anything
1327 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001328 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001329 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1330 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001331 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001332 return false;
1333 }
1334
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001335 // Okay we are now going to start a switch, to 'next'. We may first
1336 // have to pause the current activity, but this is an important point
1337 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001338 // XXX "App Redirected" dialog is getting too many false positives
1339 // at this point, so turn off for now.
1340 if (false) {
1341 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1342 long now = SystemClock.uptimeMillis();
1343 final boolean inTime = mLastStartedActivity.startTime != 0
1344 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1345 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1346 final int nextUid = next.info.applicationInfo.uid;
1347 if (inTime && lastUid != nextUid
1348 && lastUid != next.launchedFromUid
1349 && mService.checkPermission(
1350 android.Manifest.permission.STOP_APP_SWITCHES,
1351 -1, next.launchedFromUid)
1352 != PackageManager.PERMISSION_GRANTED) {
1353 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1354 } else {
1355 next.startTime = now;
1356 mLastStartedActivity = next;
1357 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001358 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001359 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001360 mLastStartedActivity = next;
1361 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001362 }
Craig Mautner58547802013-03-05 08:23:53 -08001363
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001364 // We need to start pausing the current activity so the top one
1365 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001366 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001367 if (mResumedActivity != null) {
1368 pausing = true;
1369 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001370 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001371 }
1372 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001373 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1374 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001375 // At this point we want to put the upcoming activity's process
1376 // at the top of the LRU list, since we know we will be needing it
1377 // very soon and it would be a waste to let it get killed if it
1378 // happens to be sitting towards the end.
1379 if (next.app != null && next.app.thread != null) {
1380 // No reason to do full oom adj update here; we'll let that
1381 // happen whenever it needs to later.
Dianne Hackborndb926082013-10-31 16:32:44 -07001382 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001383 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001384 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001385 return true;
1386 }
1387
Christopher Tated3f175c2012-06-14 14:16:54 -07001388 // If the most recent activity was noHistory but was only stopped rather
1389 // than stopped+finished because the device went to sleep, we need to make
1390 // sure to finish it as we're making a new activity topmost.
Craig Mautner0f922742013-08-06 08:44:42 -07001391 if (mService.mSleeping && mLastNoHistoryActivity != null &&
1392 !mLastNoHistoryActivity.finishing) {
1393 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1394 " on new resume");
1395 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1396 null, "no-history", false);
1397 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001398 }
1399
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001400 if (prev != null && prev != next) {
1401 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1402 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001403 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001404 if (DEBUG_SWITCH) Slog.v(
1405 TAG, "Resuming top, waiting visible to hide: " + prev);
1406 } else {
1407 // The next activity is already visible, so hide the previous
1408 // activity's windows right now so we can show the new one ASAP.
1409 // We only do this if the previous is finishing, which should mean
1410 // it is on top of the one being resumed so hiding it quickly
1411 // is good. Otherwise, we want to do the normal route of allowing
1412 // the resumed activity to be shown so we can decide if the
1413 // previous should actually be hidden depending on whether the
1414 // new one is found to be full-screen or not.
1415 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001416 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001417 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1418 + prev + ", waitingVisible="
1419 + (prev != null ? prev.waitingVisible : null)
1420 + ", nowVisible=" + next.nowVisible);
1421 } else {
1422 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1423 + prev + ", waitingVisible="
1424 + (prev != null ? prev.waitingVisible : null)
1425 + ", nowVisible=" + next.nowVisible);
1426 }
1427 }
1428 }
1429
Dianne Hackborne7f97212011-02-24 14:40:20 -08001430 // Launching this app's activity, make sure the app is no longer
1431 // considered stopped.
1432 try {
1433 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001434 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001435 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001436 } catch (IllegalArgumentException e) {
1437 Slog.w(TAG, "Failed trying to unstop package "
1438 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001439 }
1440
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001441 // We are starting up the next activity, so tell the window manager
1442 // that the previous one will be hidden soon. This way it can know
1443 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001444 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001445 if (prev != null) {
1446 if (prev.finishing) {
1447 if (DEBUG_TRANSITION) Slog.v(TAG,
1448 "Prepare close transition: prev=" + prev);
1449 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001450 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001451 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001452 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001453 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001454 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1455 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001456 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001457 mWindowManager.setAppWillBeHidden(prev.appToken);
1458 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001459 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001460 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001461 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001462 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001463 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001464 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001465 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001466 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1467 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001468 }
1469 }
1470 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001471 mWindowManager.setAppWillBeHidden(prev.appToken);
1472 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001473 }
Craig Mautner967212c2013-04-13 21:10:58 -07001474 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001475 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001476 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001477 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001478 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001479 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001480 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001481 }
1482 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001483 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001484 next.applyOptionsLocked();
1485 } else {
1486 next.clearOptionsLocked();
1487 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001488
Craig Mautnercf910b02013-04-23 11:23:27 -07001489 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001490 if (next.app != null && next.app.thread != null) {
1491 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1492
1493 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001494 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001495
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001496 // schedule launch ticks to collect information about slow apps.
1497 next.startLaunchTickingLocked();
1498
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001499 ActivityRecord lastResumedActivity =
1500 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001501 ActivityState lastState = next.state;
1502
1503 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001504
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001505 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001506 next.state = ActivityState.RESUMED;
1507 mResumedActivity = next;
1508 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001509 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001510 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001511 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001512 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001513
1514 // Have the window manager re-evaluate the orientation of
1515 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001516 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001517 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001518 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001519 mService.mConfiguration,
1520 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1521 if (config != null) {
1522 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001523 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001524 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001525 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001526
Craig Mautner525f3d92013-05-07 14:01:50 -07001527 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001528 // The configuration update wasn't able to keep the existing
1529 // instance of the activity, and instead started a new one.
1530 // We should be all done, but let's just make sure our activity
1531 // is still at the top and schedule another run if something
1532 // weird happened.
1533 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001534 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001535 "Activity config changed during resume: " + next
1536 + ", new next: " + nextNext);
1537 if (nextNext != next) {
1538 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001539 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001540 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001541 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001542 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001543 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001544 return true;
1545 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001546 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001547 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001548 }
Craig Mautner58547802013-03-05 08:23:53 -08001549
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001550 try {
1551 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001552 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001553 if (a != null) {
1554 final int N = a.size();
1555 if (!next.finishing && N > 0) {
1556 if (DEBUG_RESULTS) Slog.v(
1557 TAG, "Delivering results to " + next
1558 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001559 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001560 }
1561 }
1562
1563 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001564 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001565 }
1566
1567 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001568 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001569 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001570
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001571 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001572 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001573 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001574 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
1575 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001576 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001577
Craig Mautner0eea92c2013-05-16 13:35:39 -07001578 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001579
Craig Mautnerac6f8432013-07-17 13:24:59 -07001580 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001581 } catch (Exception e) {
1582 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001583 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1584 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001585 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001586 if (lastStack != null) {
1587 lastStack.mResumedActivity = lastResumedActivity;
1588 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001589 Slog.i(TAG, "Restarting because process died: " + next);
1590 if (!next.hasBeenLaunched) {
1591 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001592 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1593 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001594 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001595 next.appToken, next.packageName, next.theme,
1596 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001597 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1598 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001599 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001600 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001601 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001602 return true;
1603 }
1604
1605 // From this point on, if something goes wrong there is no way
1606 // to recover the activity.
1607 try {
1608 next.visible = true;
1609 completeResumeLocked(next);
1610 } catch (Exception e) {
1611 // If any exception gets thrown, toss away this
1612 // activity and try the next one.
1613 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001614 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001615 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001616 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001617 return true;
1618 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001619 next.stopped = false;
1620
1621 } else {
1622 // Whoops, need to restart this activity!
1623 if (!next.hasBeenLaunched) {
1624 next.hasBeenLaunched = true;
1625 } else {
1626 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001627 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001628 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001629 mService.compatibilityInfoForPackageLocked(
1630 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001631 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001632 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001633 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001634 }
1635 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1636 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001637 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
Craig Mautnere79d42682013-04-01 19:01:53 -07001638 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001639 }
1640
Craig Mautnercf910b02013-04-23 11:23:27 -07001641 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001642 return true;
1643 }
1644
Craig Mautnerac6f8432013-07-17 13:24:59 -07001645 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001646 // If this is being moved to the top by another activity or being launched from the home
1647 // activity, set mOnTopOfHome accordingly.
Craig Mautnerd99384d2013-10-14 07:09:18 -07001648 ActivityStack lastStack = mStackSupervisor.getLastStack();
1649 final boolean fromHome = lastStack == null ? true : lastStack.isHomeStack();
Craig Mautner9c85c202013-10-04 20:11:26 -07001650 if (!isHomeStack() && (fromHome || topTask() != task)) {
1651 task.mOnTopOfHome = fromHome;
1652 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001653
Craig Mautnerac6f8432013-07-17 13:24:59 -07001654 mTaskHistory.remove(task);
1655 // Now put task at top.
1656 int stackNdx = mTaskHistory.size();
1657 if (task.userId != mCurrentUser) {
1658 // Put non-current user tasks below current user tasks.
1659 while (--stackNdx >= 0) {
1660 if (mTaskHistory.get(stackNdx).userId != mCurrentUser) {
1661 break;
1662 }
1663 }
1664 ++stackNdx;
1665 }
1666 mTaskHistory.add(stackNdx, task);
1667 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001668
Craig Mautner8849a5e2013-04-02 16:41:03 -07001669 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001670 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001671 TaskRecord rTask = r.task;
1672 final int taskId = rTask.taskId;
1673 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001674 // Last activity in task had been removed or ActivityManagerService is reusing task.
1675 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001676 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001677 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001678 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001679 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001680 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001681 if (!newTask) {
1682 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001683 boolean startIt = true;
1684 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1685 task = mTaskHistory.get(taskNdx);
1686 if (task == r.task) {
1687 // Here it is! Now, if this is not yet visible to the
1688 // user, then just add it without starting; it will
1689 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001690 if (!startIt) {
1691 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1692 + task, new RuntimeException("here").fillInStackTrace());
1693 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001694 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001695 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1696 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001697 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001698 r.userId, r.info.configChanges);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001699 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001700 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001701 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001702 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001703 return;
1704 }
1705 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001706 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001707 startIt = false;
1708 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001709 }
1710 }
1711
1712 // Place a new activity at top of stack, so it is next to interact
1713 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001714
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001715 // If we are not placing the new activity frontmost, we do not want
1716 // to deliver the onUserLeaving callback to the actual frontmost
1717 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001718 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001719 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001720 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1721 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001722 }
Craig Mautner70a86932013-02-28 22:37:44 -08001723
1724 task = r.task;
1725
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001726 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001727 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001728 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001729 task.addActivityToTop(r);
Craig Mautner3b475fe2013-12-16 15:58:31 -08001730 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08001731
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001732 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001733 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001734 // We want to show the starting preview window if we are
1735 // switching to a new task, or the next activity's process is
1736 // not currently running.
1737 boolean showStartingIcon = newTask;
1738 ProcessRecord proc = r.app;
1739 if (proc == null) {
1740 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1741 }
1742 if (proc == null || proc.thread == null) {
1743 showStartingIcon = true;
1744 }
1745 if (DEBUG_TRANSITION) Slog.v(TAG,
1746 "Prepare open transition: starting " + r);
1747 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001748 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001749 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001750 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001751 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001752 ? AppTransition.TRANSIT_TASK_OPEN
1753 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001754 mNoAnimActivities.remove(r);
1755 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001756 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001757 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001758 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001759 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
1760 r.info.configChanges);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001761 boolean doShow = true;
1762 if (newTask) {
1763 // Even though this activity is starting fresh, we still need
1764 // to reset it to make sure we apply affinities to move any
1765 // existing activities from other tasks in to it.
1766 // If the caller has requested that the target task be
1767 // reset, then do so.
1768 if ((r.intent.getFlags()
1769 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1770 resetTaskIfNeededLocked(r, r);
1771 doShow = topRunningNonDelayedActivityLocked(null) == r;
1772 }
1773 }
1774 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1775 // Figure out if we are transitioning from another activity that is
1776 // "has the same starting icon" as the next one. This allows the
1777 // window manager to keep the previous window it had previously
1778 // created, if it still had one.
1779 ActivityRecord prev = mResumedActivity;
1780 if (prev != null) {
1781 // We don't want to reuse the previous starting preview if:
1782 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001783 if (prev.task != r.task) {
1784 prev = null;
1785 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001786 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001787 else if (prev.nowVisible) {
1788 prev = null;
1789 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001790 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001791 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001792 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001793 mService.compatibilityInfoForPackageLocked(
1794 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001795 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001796 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001797 }
1798 } else {
1799 // If this is the first activity, don't do any fancy animations,
1800 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001801 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001802 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001803 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
1804 r.info.configChanges);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001805 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001806 }
1807 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001808 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001809 }
1810
1811 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001812 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001813 }
1814 }
1815
Dianne Hackbornbe707852011-11-11 14:32:10 -08001816 final void validateAppTokensLocked() {
1817 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001818 mValidateAppTokens.ensureCapacity(numActivities());
1819 final int numTasks = mTaskHistory.size();
1820 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1821 TaskRecord task = mTaskHistory.get(taskNdx);
1822 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07001823 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08001824 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001825 }
Craig Mautner000f0022013-02-26 15:04:29 -08001826 TaskGroup group = new TaskGroup();
1827 group.taskId = task.taskId;
1828 mValidateAppTokens.add(group);
1829 final int numActivities = activities.size();
1830 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1831 final ActivityRecord r = activities.get(activityNdx);
1832 group.tokens.add(r.appToken);
1833 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001834 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001835 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001836 }
1837
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001838 /**
1839 * Perform a reset of the given task, if needed as part of launching it.
1840 * Returns the new HistoryRecord at the top of the task.
1841 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001842 /**
1843 * Helper method for #resetTaskIfNeededLocked.
1844 * We are inside of the task being reset... we'll either finish this activity, push it out
1845 * for another task, or leave it as-is.
1846 * @param task The task containing the Activity (taskTop) that might be reset.
1847 * @param forceReset
1848 * @return An ActivityOptions that needs to be processed.
1849 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001850 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001851 ActivityOptions topOptions = null;
1852
1853 int replyChainEnd = -1;
1854 boolean canMoveOptions = true;
1855
1856 // We only do this for activities that are not the root of the task (since if we finish
1857 // the root, we may no longer have the task!).
1858 final ArrayList<ActivityRecord> activities = task.mActivities;
1859 final int numActivities = activities.size();
1860 for (int i = numActivities - 1; i > 0; --i ) {
1861 ActivityRecord target = activities.get(i);
1862
1863 final int flags = target.info.flags;
1864 final boolean finishOnTaskLaunch =
1865 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1866 final boolean allowTaskReparenting =
1867 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1868 final boolean clearWhenTaskReset =
1869 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1870
1871 if (!finishOnTaskLaunch
1872 && !clearWhenTaskReset
1873 && target.resultTo != null) {
1874 // If this activity is sending a reply to a previous
1875 // activity, we can't do anything with it now until
1876 // we reach the start of the reply chain.
1877 // XXX note that we are assuming the result is always
1878 // to the previous activity, which is almost always
1879 // the case but we really shouldn't count on.
1880 if (replyChainEnd < 0) {
1881 replyChainEnd = i;
1882 }
1883 } else if (!finishOnTaskLaunch
1884 && !clearWhenTaskReset
1885 && allowTaskReparenting
1886 && target.taskAffinity != null
1887 && !target.taskAffinity.equals(task.affinity)) {
1888 // If this activity has an affinity for another
1889 // task, then we need to move it out of here. We will
1890 // move it as far out of the way as possible, to the
1891 // bottom of the activity stack. This also keeps it
1892 // correctly ordered with any activities we previously
1893 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08001894 final ThumbnailHolder newThumbHolder;
1895 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07001896 final ActivityRecord bottom =
1897 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08001898 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07001899 if (bottom != null && target.taskAffinity != null
1900 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001901 // If the activity currently at the bottom has the
1902 // same task affinity as the one we are moving,
1903 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08001904 targetTask = bottom.task;
1905 newThumbHolder = bottom.thumbHolder == null ? targetTask : bottom.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001906 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07001907 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001908 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08001909 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
1910 null, false);
1911 newThumbHolder = targetTask;
1912 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001913 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1914 + " out to new task " + target.task);
1915 }
1916
Craig Mautner329f4122013-11-07 09:10:42 -08001917 if (clearWhenTaskReset) {
1918 // This is the start of a new sub-task.
1919 if (target.thumbHolder == null) {
1920 target.thumbHolder = new ThumbnailHolder();
1921 }
1922 } else {
1923 target.thumbHolder = newThumbHolder;
1924 }
1925
Craig Mautnere3a74d52013-02-22 14:14:58 -08001926 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001927 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001928
Craig Mautner525f3d92013-05-07 14:01:50 -07001929 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001930 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1931 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07001932 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001933 if (p.finishing) {
1934 continue;
1935 }
1936
Craig Mautner525f3d92013-05-07 14:01:50 -07001937 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001938 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001939 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001940 topOptions = p.takeOptionsLocked();
1941 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001942 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001943 }
1944 }
1945 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08001946 + task + " adding to task=" + targetTask
1947 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08001948 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1949 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001950 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001951 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001952
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001953 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001954 }
1955
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001956 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001957 if (VALIDATE_TOKENS) {
1958 validateAppTokensLocked();
1959 }
1960
1961 replyChainEnd = -1;
1962 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1963 // If the activity should just be removed -- either
1964 // because it asks for it, or the task should be
1965 // cleared -- then finish it and anything that is
1966 // part of its reply chain.
1967 int end;
1968 if (clearWhenTaskReset) {
1969 // In this case, we want to finish this activity
1970 // and everything above it, so be sneaky and pretend
1971 // like these are all in the reply chain.
1972 end = numActivities - 1;
1973 } else if (replyChainEnd < 0) {
1974 end = i;
1975 } else {
1976 end = replyChainEnd;
1977 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001978 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001979 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001980 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001981 if (p.finishing) {
1982 continue;
1983 }
1984 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001985 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001986 topOptions = p.takeOptionsLocked();
1987 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001988 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001989 }
1990 }
Craig Mautner58547802013-03-05 08:23:53 -08001991 if (DEBUG_TASKS) Slog.w(TAG,
1992 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001993 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001994 end--;
1995 srcPos--;
1996 }
1997 }
1998 replyChainEnd = -1;
1999 } else {
2000 // If we were in the middle of a chain, well the
2001 // activity that started it all doesn't want anything
2002 // special, so leave it all as-is.
2003 replyChainEnd = -1;
2004 }
2005 }
2006
2007 return topOptions;
2008 }
2009
2010 /**
2011 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2012 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2013 * @param affinityTask The task we are looking for an affinity to.
2014 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2015 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2016 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2017 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002018 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002019 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002020 int replyChainEnd = -1;
2021 final int taskId = task.taskId;
2022 final String taskAffinity = task.affinity;
2023
2024 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2025 final int numActivities = activities.size();
2026 // Do not operate on the root Activity.
2027 for (int i = numActivities - 1; i > 0; --i) {
2028 ActivityRecord target = activities.get(i);
2029
2030 final int flags = target.info.flags;
2031 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2032 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2033
2034 if (target.resultTo != null) {
2035 // If this activity is sending a reply to a previous
2036 // activity, we can't do anything with it now until
2037 // we reach the start of the reply chain.
2038 // XXX note that we are assuming the result is always
2039 // to the previous activity, which is almost always
2040 // the case but we really shouldn't count on.
2041 if (replyChainEnd < 0) {
2042 replyChainEnd = i;
2043 }
2044 } else if (topTaskIsHigher
2045 && allowTaskReparenting
2046 && taskAffinity != null
2047 && taskAffinity.equals(target.taskAffinity)) {
2048 // This activity has an affinity for our task. Either remove it if we are
2049 // clearing or move it over to our task. Note that
2050 // we currently punt on the case where we are resetting a
2051 // task that is not at the top but who has activities above
2052 // with an affinity to it... this is really not a normal
2053 // case, and we will need to later pull that task to the front
2054 // and usually at that point we will do the reset and pick
2055 // up those remaining activities. (This only happens if
2056 // someone starts an activity in a new task from an activity
2057 // in a task that is not currently on top.)
2058 if (forceReset || finishOnTaskLaunch) {
2059 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2060 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2061 for (int srcPos = start; srcPos >= i; --srcPos) {
2062 final ActivityRecord p = activities.get(srcPos);
2063 if (p.finishing) {
2064 continue;
2065 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002066 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002067 }
2068 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002069 if (taskInsertionPoint < 0) {
2070 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002071
Craig Mautner77878772013-03-04 19:46:24 -08002072 }
Craig Mautner77878772013-03-04 19:46:24 -08002073
2074 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2075 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2076 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2077 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002078 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002079 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002080 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002081
Craig Mautnere3a74d52013-02-22 14:14:58 -08002082 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2083 + " to stack at " + task,
2084 new RuntimeException("here").fillInStackTrace());
2085 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2086 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002087 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002088 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002089 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002090 if (VALIDATE_TOKENS) {
2091 validateAppTokensLocked();
2092 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002093
2094 // Now we've moved it in to place... but what if this is
2095 // a singleTop activity and we have put it on top of another
2096 // instance of the same activity? Then we drop the instance
2097 // below so it remains singleTop.
2098 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2099 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002100 int targetNdx = taskActivities.indexOf(target);
2101 if (targetNdx > 0) {
2102 ActivityRecord p = taskActivities.get(targetNdx - 1);
2103 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002104 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2105 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002106 }
2107 }
2108 }
2109 }
2110
2111 replyChainEnd = -1;
2112 }
2113 }
Craig Mautner77878772013-03-04 19:46:24 -08002114 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002115 }
2116
Craig Mautner8849a5e2013-04-02 16:41:03 -07002117 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002118 ActivityRecord newActivity) {
2119 boolean forceReset =
2120 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2121 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2122 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2123 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2124 forceReset = true;
2125 }
2126 }
2127
2128 final TaskRecord task = taskTop.task;
2129
2130 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2131 * for remaining tasks. Used for later tasks to reparent to task. */
2132 boolean taskFound = false;
2133
2134 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2135 ActivityOptions topOptions = null;
2136
Craig Mautner77878772013-03-04 19:46:24 -08002137 // Preserve the location for reparenting in the new task.
2138 int reparentInsertionPoint = -1;
2139
Craig Mautnere3a74d52013-02-22 14:14:58 -08002140 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2141 final TaskRecord targetTask = mTaskHistory.get(i);
2142
2143 if (targetTask == task) {
2144 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2145 taskFound = true;
2146 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002147 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2148 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002149 }
2150 }
2151
Craig Mautner70a86932013-02-28 22:37:44 -08002152 int taskNdx = mTaskHistory.indexOf(task);
2153 do {
2154 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2155 } while (taskTop == null && taskNdx >= 0);
2156
Craig Mautnere3a74d52013-02-22 14:14:58 -08002157 if (topOptions != null) {
2158 // If we got some ActivityOptions from an activity on top that
2159 // was removed from the task, propagate them to the new real top.
2160 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002161 taskTop.updateOptionsLocked(topOptions);
2162 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002163 topOptions.abort();
2164 }
2165 }
2166
2167 return taskTop;
2168 }
2169
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002170 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2171 String resultWho, int requestCode, int resultCode, Intent data) {
2172
2173 if (callingUid > 0) {
2174 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002175 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002176 }
2177
2178 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2179 + " : who=" + resultWho + " req=" + requestCode
2180 + " res=" + resultCode + " data=" + data);
2181 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2182 try {
2183 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2184 list.add(new ResultInfo(resultWho, requestCode,
2185 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002186 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002187 return;
2188 } catch (Exception e) {
2189 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2190 }
2191 }
2192
2193 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2194 }
2195
Craig Mautner04f0b702013-10-22 12:31:01 -07002196 private void adjustFocusedActivityLocked(ActivityRecord r) {
2197 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2198 ActivityRecord next = topRunningActivityLocked(null);
2199 if (next != r) {
2200 final TaskRecord task = r.task;
2201 if (r.frontOfTask && task == topTask() && task.mOnTopOfHome) {
2202 mStackSupervisor.moveHomeToTop();
2203 }
2204 }
2205 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
2206 }
2207 }
2208
Craig Mautnerf3333272013-04-22 10:55:53 -07002209 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002210 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2211 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2212 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2213 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002214 if (!mService.mSleeping) {
2215 if (DEBUG_STATES) {
2216 Slog.d(TAG, "no-history finish of " + r);
2217 }
2218 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002219 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002220 } else {
2221 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2222 + " on stop because we're just sleeping");
2223 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002224 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002225 }
2226
2227 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002228 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002229 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002230 try {
2231 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002232 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2233 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002234 r.state = ActivityState.STOPPING;
2235 if (DEBUG_VISBILITY) Slog.v(
2236 TAG, "Stopping visible=" + r.visible + " for " + r);
2237 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002238 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002239 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002240 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002241 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002242 r.setSleeping(true);
2243 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002244 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002245 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002246 } catch (Exception e) {
2247 // Maybe just ignore exceptions here... if the process
2248 // has crashed, our death notification will clean things
2249 // up.
2250 Slog.w(TAG, "Exception thrown during pause", e);
2251 // Just in case, assume it to be stopped.
2252 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002253 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002254 r.state = ActivityState.STOPPED;
2255 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002256 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002257 }
2258 }
2259 }
2260 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002261
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002262 /**
2263 * @return Returns true if the activity is being finished, false if for
2264 * some reason it is being left as-is.
2265 */
2266 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002267 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002268 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002269 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002270 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002271 + ", result=" + resultCode + ", data=" + resultData
2272 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002273 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002274 return false;
2275 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002276
Craig Mautnerd44711d2013-02-23 11:24:36 -08002277 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002278 return true;
2279 }
2280
Craig Mautnerd2328952013-03-05 12:46:26 -08002281 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002282 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2283 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2284 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2285 ActivityRecord r = activities.get(activityNdx);
2286 if (r.resultTo == self && r.requestCode == requestCode) {
2287 if ((r.resultWho == null && resultWho == null) ||
2288 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2289 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2290 false);
2291 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002292 }
2293 }
2294 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002295 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002296 }
2297
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002298 final void finishTopRunningActivityLocked(ProcessRecord app) {
2299 ActivityRecord r = topRunningActivityLocked(null);
2300 if (r != null && r.app == app) {
2301 // If the top running activity is from this crashing
2302 // process, then terminate it to avoid getting in a loop.
2303 Slog.w(TAG, " Force finishing activity "
2304 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002305 int taskNdx = mTaskHistory.indexOf(r.task);
2306 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002307 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002308 // Also terminate any activities below it that aren't yet
2309 // stopped, to avoid a situation where one will get
2310 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002311 --activityNdx;
2312 if (activityNdx < 0) {
2313 do {
2314 --taskNdx;
2315 if (taskNdx < 0) {
2316 break;
2317 }
2318 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2319 } while (activityNdx < 0);
2320 }
2321 if (activityNdx >= 0) {
2322 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002323 if (r.state == ActivityState.RESUMED
2324 || r.state == ActivityState.PAUSING
2325 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002326 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002327 Slog.w(TAG, " Force finishing activity "
2328 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002329 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002330 }
2331 }
2332 }
2333 }
2334 }
2335
Craig Mautnerd2328952013-03-05 12:46:26 -08002336 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002337 ArrayList<ActivityRecord> activities = r.task.mActivities;
2338 for (int index = activities.indexOf(r); index >= 0; --index) {
2339 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002340 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002341 break;
2342 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002343 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002344 }
2345 return true;
2346 }
2347
Dianne Hackborn5c607432012-02-28 14:44:19 -08002348 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2349 // send the result
2350 ActivityRecord resultTo = r.resultTo;
2351 if (resultTo != null) {
2352 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2353 + " who=" + r.resultWho + " req=" + r.requestCode
2354 + " res=" + resultCode + " data=" + resultData);
2355 if (r.info.applicationInfo.uid > 0) {
2356 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2357 resultTo.packageName, resultData,
2358 resultTo.getUriPermissionsLocked());
2359 }
2360 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2361 resultData);
2362 r.resultTo = null;
2363 }
2364 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2365
2366 // Make sure this HistoryRecord is not holding on to other resources,
2367 // because clients have remote IPC references to this object so we
2368 // can't assume that will go away and want to avoid circular IPC refs.
2369 r.results = null;
2370 r.pendingResults = null;
2371 r.newIntents = null;
2372 r.icicle = null;
2373 }
2374
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002375 /**
2376 * @return Returns true if this activity has been removed from the history
2377 * list, or false if it is still in the list and will be removed later.
2378 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002379 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2380 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002381 if (r.finishing) {
2382 Slog.w(TAG, "Duplicate finish request for " + r);
2383 return false;
2384 }
2385
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002386 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002387 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002388 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002389 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002390 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2391 final int index = activities.indexOf(r);
2392 if (index < (activities.size() - 1)) {
Craig Mautner3b475fe2013-12-16 15:58:31 -08002393 r.task.setFrontOfTask();
Craig Mautnerd44711d2013-02-23 11:24:36 -08002394 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2395 // If the caller asked that this activity (and all above it)
2396 // be cleared when the task is reset, don't lose that information,
2397 // but propagate it up to the next activity.
Craig Mautner3b475fe2013-12-16 15:58:31 -08002398 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002399 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002400 }
2401 }
2402
2403 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002404
2405 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002406
Dianne Hackborn5c607432012-02-28 14:44:19 -08002407 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002408
Craig Mautnerc8143c62013-09-03 12:15:57 -07002409 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002410 // There are clients waiting to receive thumbnails so, in case
2411 // this is an activity that someone is waiting for, add it
2412 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002413 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002414 }
2415
Craig Mautnerde4ef022013-04-07 19:01:33 -07002416 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002417 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002418 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002419 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002420 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002421 ? AppTransition.TRANSIT_TASK_CLOSE
2422 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002423
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002424 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002425 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002426
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002427 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002428 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2429 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2430 startPausingLocked(false, false);
2431 }
2432
2433 } else if (r.state != ActivityState.PAUSING) {
2434 // If the activity is PAUSING, we will complete the finish once
2435 // it is done pausing; else we can just directly finish it here.
2436 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002437 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002438 } else {
2439 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2440 }
2441
2442 return false;
2443 }
2444
Craig Mautnerf3333272013-04-22 10:55:53 -07002445 static final int FINISH_IMMEDIATELY = 0;
2446 static final int FINISH_AFTER_PAUSE = 1;
2447 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002448
Craig Mautnerf3333272013-04-22 10:55:53 -07002449 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002450 // First things first: if this activity is currently visible,
2451 // and the resumed activity is not yet visible, then hold off on
2452 // finishing until the resumed one becomes visible.
2453 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002454 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2455 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002456 if (mStackSupervisor.mStoppingActivities.size() > 3
2457 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002458 // If we already have a few activities waiting to stop,
2459 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002460 // them out. Or if r is the last of activity of the last task the stack
2461 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002462 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002463 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002464 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002465 }
2466 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002467 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2468 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002469 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002470 if (oomAdj) {
2471 mService.updateOomAdjLocked();
2472 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002473 return r;
2474 }
2475
2476 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002477 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002478 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002479 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002480 if (mResumedActivity == r) {
2481 mResumedActivity = null;
2482 }
2483 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002484 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002485 r.state = ActivityState.FINISHING;
2486
2487 if (mode == FINISH_IMMEDIATELY
2488 || prevState == ActivityState.STOPPED
2489 || prevState == ActivityState.INITIALIZING) {
2490 // If this activity is already stopped, we can just finish
2491 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002492 boolean activityRemoved = destroyActivityLocked(r, true,
2493 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002494 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002495 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002496 }
2497 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002498 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002499
2500 // Need to go through the full pause cycle to get this
2501 // activity into the stopped state and then finish it.
2502 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002503 mStackSupervisor.mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002504 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002505 return r;
2506 }
2507
Craig Mautnerd2328952013-03-05 12:46:26 -08002508 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002509 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002510 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002511 final TaskRecord task = srec.task;
2512 final ArrayList<ActivityRecord> activities = task.mActivities;
2513 final int start = activities.indexOf(srec);
2514 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002515 return false;
2516 }
2517 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002518 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002519 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002520 final ComponentName dest = destIntent.getComponent();
2521 if (start > 0 && dest != null) {
2522 for (int i = finishTo; i >= 0; i--) {
2523 ActivityRecord r = activities.get(i);
2524 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002525 r.info.name.equals(dest.getClassName())) {
2526 finishTo = i;
2527 parent = r;
2528 foundParentInTask = true;
2529 break;
2530 }
2531 }
2532 }
2533
2534 IActivityController controller = mService.mController;
2535 if (controller != null) {
2536 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2537 if (next != null) {
2538 // ask watcher if this is allowed
2539 boolean resumeOK = true;
2540 try {
2541 resumeOK = controller.activityResuming(next.packageName);
2542 } catch (RemoteException e) {
2543 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002544 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002545 }
2546
2547 if (!resumeOK) {
2548 return false;
2549 }
2550 }
2551 }
2552 final long origId = Binder.clearCallingIdentity();
2553 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002554 ActivityRecord r = activities.get(i);
2555 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002556 // Only return the supplied result for the first activity finished
2557 resultCode = Activity.RESULT_CANCELED;
2558 resultData = null;
2559 }
2560
2561 if (parent != null && foundParentInTask) {
2562 final int parentLaunchMode = parent.info.launchMode;
2563 final int destIntentFlags = destIntent.getFlags();
2564 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2565 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2566 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2567 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2568 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2569 } else {
2570 try {
2571 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2572 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002573 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002574 null, aInfo, parent.appToken, null,
2575 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2576 0, null, true, null);
2577 foundParentInTask = res == ActivityManager.START_SUCCESS;
2578 } catch (RemoteException e) {
2579 foundParentInTask = false;
2580 }
2581 requestFinishActivityLocked(parent.appToken, resultCode,
2582 resultData, "navigate-up", true);
2583 }
2584 }
2585 Binder.restoreCallingIdentity(origId);
2586 return foundParentInTask;
2587 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002588 /**
2589 * Perform the common clean-up of an activity record. This is called both
2590 * as part of destroyActivityLocked() (when destroying the client-side
2591 * representation) and cleaning things up as a result of its hosting
2592 * processing going away, in which case there is no remaining client-side
2593 * state to destroy so only the cleanup here is needed.
2594 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002595 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2596 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002597 if (mResumedActivity == r) {
2598 mResumedActivity = null;
2599 }
2600 if (mService.mFocusedActivity == r) {
2601 mService.mFocusedActivity = null;
2602 }
2603
2604 r.configDestroy = false;
2605 r.frozenBeforeDestroy = false;
2606
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002607 if (setState) {
2608 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2609 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002610 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002611 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002612 }
2613
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002614 // Make sure this record is no longer in the pending finishes list.
2615 // This could happen, for example, if we are trimming activities
2616 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002617 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002618 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002619
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002620 // Remove any pending results.
2621 if (r.finishing && r.pendingResults != null) {
2622 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2623 PendingIntentRecord rec = apr.get();
2624 if (rec != null) {
2625 mService.cancelIntentSenderLocked(rec, false);
2626 }
2627 }
2628 r.pendingResults = null;
2629 }
2630
2631 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002632 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002633 }
2634
Craig Mautnerc8143c62013-09-03 12:15:57 -07002635 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002636 // There are clients waiting to receive thumbnails so, in case
2637 // this is an activity that someone is waiting for, add it
2638 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002639 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002640 }
2641
2642 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002643 removeTimeoutsForActivityLocked(r);
2644 }
2645
2646 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002647 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002648 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002649 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002650 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002651 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002652 }
2653
Dianne Hackborn5c607432012-02-28 14:44:19 -08002654 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002655 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2656 r.makeFinishing();
2657 if (DEBUG_ADD_REMOVE) {
2658 RuntimeException here = new RuntimeException("here");
2659 here.fillInStackTrace();
2660 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002661 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002662 final TaskRecord task = r.task;
2663 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002664 if (DEBUG_STACK) Slog.i(TAG,
2665 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnere1db0dd2013-10-13 18:15:00 -07002666 if (mStackSupervisor.isFrontStack(this) && task == topTask() && task.mOnTopOfHome) {
Craig Mautner8e569572013-10-11 17:36:59 -07002667 mStackSupervisor.moveHomeToTop();
2668 }
Craig Mautner2219b752013-10-12 11:26:08 -07002669 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002670 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002671 r.takeFromHistory();
2672 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002673 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002674 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002675 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002676 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002677 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002678 if (VALIDATE_TOKENS) {
2679 validateAppTokensLocked();
2680 }
2681 cleanUpActivityServicesLocked(r);
2682 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002683 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002684
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002685 /**
2686 * Perform clean-up of service connections in an activity record.
2687 */
2688 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2689 // Throw away any services that have been bound by this activity.
2690 if (r.connections != null) {
2691 Iterator<ConnectionRecord> it = r.connections.iterator();
2692 while (it.hasNext()) {
2693 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002694 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002695 }
2696 r.connections = null;
2697 }
2698 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002699
2700 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2701 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2702 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2703 mHandler.sendMessage(msg);
2704 }
2705
Dianne Hackborn28695e02011-11-02 21:59:51 -07002706 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002707 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002708 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002709 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2710 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2711 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2712 final ActivityRecord r = activities.get(activityNdx);
2713 if (r.finishing) {
2714 continue;
2715 }
2716 if (r.fullscreen) {
2717 lastIsOpaque = true;
2718 }
2719 if (owner != null && r.app != owner) {
2720 continue;
2721 }
2722 if (!lastIsOpaque) {
2723 continue;
2724 }
2725 // We can destroy this one if we have its icicle saved and
2726 // it is not in the process of pausing/stopping/finishing.
2727 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2728 && r.haveState && !r.visible && r.stopped
2729 && r.state != ActivityState.DESTROYING
2730 && r.state != ActivityState.DESTROYED) {
2731 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2732 + " resumed=" + mResumedActivity
2733 + " pausing=" + mPausingActivity);
2734 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2735 activityRemoved = true;
2736 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002737 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002738 }
2739 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002740 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002741 mStackSupervisor.resumeTopActivitiesLocked();
2742
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002743 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002744 }
2745
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002746 /**
2747 * Destroy the current CLIENT SIDE instance of an activity. This may be
2748 * called both when actually finishing an activity, or when performing
2749 * a configuration switch where we destroy the current client-side object
2750 * but then create a new client-side object for this same HistoryRecord.
2751 */
2752 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002753 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002754 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002755 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002756 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2757 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002758 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002759 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002760
2761 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002762
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002763 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002764
2765 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002766
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002767 if (hadApp) {
2768 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002769 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002770 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2771 mService.mHeavyWeightProcess = null;
2772 mService.mHandler.sendEmptyMessage(
2773 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2774 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07002775 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07002776 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07002777 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002778 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002779 }
2780 }
2781
2782 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002783
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002784 try {
2785 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002786 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002787 r.configChangeFlags);
2788 } catch (Exception e) {
2789 // We can just ignore exceptions here... if the process
2790 // has crashed, our death notification will clean things
2791 // up.
2792 //Slog.w(TAG, "Exception thrown during finish", e);
2793 if (r.finishing) {
2794 removeActivityFromHistoryLocked(r);
2795 removedFromHistory = true;
2796 skipDestroy = true;
2797 }
2798 }
2799
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002800 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002801
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002802 // If the activity is finishing, we need to wait on removing it
2803 // from the list to give it a chance to do its cleanup. During
2804 // that time it may make calls back with its token so we need to
2805 // be able to find it on the list and so we don't want to remove
2806 // it from the list yet. Otherwise, we can just immediately put
2807 // it in the destroyed state since we are not removing it from the
2808 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002809 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002810 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2811 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002812 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002813 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002814 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2815 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002816 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002817 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002818 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002819 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002820 }
2821 } else {
2822 // remove this record from the history.
2823 if (r.finishing) {
2824 removeActivityFromHistoryLocked(r);
2825 removedFromHistory = true;
2826 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002827 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002828 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002829 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002830 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002831 }
2832 }
2833
2834 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002835
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002836 if (!mLRUActivities.remove(r) && hadApp) {
2837 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2838 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002839
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002840 return removedFromHistory;
2841 }
2842
Craig Mautnerd2328952013-03-05 12:46:26 -08002843 final void activityDestroyedLocked(IBinder token) {
2844 final long origId = Binder.clearCallingIdentity();
2845 try {
2846 ActivityRecord r = ActivityRecord.forToken(token);
2847 if (r != null) {
2848 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002849 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002850
2851 if (isInStackLocked(token) != null) {
2852 if (r.state == ActivityState.DESTROYING) {
2853 cleanUpActivityLocked(r, true, false);
2854 removeActivityFromHistoryLocked(r);
2855 }
2856 }
Craig Mautner05d29032013-05-03 13:40:13 -07002857 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002858 } finally {
2859 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002860 }
2861 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002862
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002863 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2864 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002865 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002866 if (DEBUG_CLEANUP) Slog.v(
2867 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002868 + " with " + i + " entries");
2869 while (i > 0) {
2870 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002871 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002872 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002873 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002874 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002875 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002876 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002877 }
2878 }
2879 }
2880
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002881 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2882 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002883 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2884 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002885 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2886 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002887 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002888 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002889 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2890 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002891
2892 boolean hasVisibleActivities = false;
2893
2894 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002895 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002896 if (DEBUG_CLEANUP) Slog.v(
2897 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002898 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2899 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2900 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2901 final ActivityRecord r = activities.get(activityNdx);
2902 --i;
2903 if (DEBUG_CLEANUP) Slog.v(
2904 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2905 if (r.app == app) {
2906 boolean remove;
2907 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2908 // Don't currently have state for the activity, or
2909 // it is finishing -- always remove it.
2910 remove = true;
2911 } else if (r.launchCount > 2 &&
2912 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2913 // We have launched this activity too many times since it was
2914 // able to run, so give up and remove it.
2915 remove = true;
2916 } else {
2917 // The process may be gone, but the activity lives on!
2918 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002919 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002920 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002921 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002922 RuntimeException here = new RuntimeException("here");
2923 here.fillInStackTrace();
2924 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2925 + ": haveState=" + r.haveState
2926 + " stateNotNeeded=" + r.stateNotNeeded
2927 + " finishing=" + r.finishing
2928 + " state=" + r.state, here);
2929 }
2930 if (!r.finishing) {
2931 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2932 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2933 r.userId, System.identityHashCode(r),
2934 r.task.taskId, r.shortComponentName,
2935 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07002936 if (r.state == ActivityState.RESUMED) {
2937 mService.updateUsageStats(r, false);
2938 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002939 }
2940 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002941
Craig Mautner0247fc82013-02-28 14:32:06 -08002942 } else {
2943 // We have the current state for this activity, so
2944 // it can be restarted later when needed.
2945 if (localLOGV) Slog.v(
2946 TAG, "Keeping entry, setting app to null");
2947 if (r.visible) {
2948 hasVisibleActivities = true;
2949 }
2950 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2951 + r);
2952 r.app = null;
2953 r.nowVisible = false;
2954 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002955 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08002956 "App died, clearing saved state of " + r);
2957 r.icicle = null;
2958 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002959 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002960
Craig Mautnerd2328952013-03-05 12:46:26 -08002961 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002962 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002963 }
2964 }
2965
2966 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002967 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002968
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002969 final void updateTransitLocked(int transit, Bundle options) {
2970 if (options != null) {
2971 ActivityRecord r = topRunningActivityLocked(null);
2972 if (r != null && r.state != ActivityState.RESUMED) {
2973 r.updateOptionsLocked(options);
2974 } else {
2975 ActivityOptions.abort(options);
2976 }
2977 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002978 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002979 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002980
Craig Mautnera82aa092013-09-13 15:34:08 -07002981 void moveHomeTaskToTop() {
2982 final int top = mTaskHistory.size() - 1;
2983 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
2984 final TaskRecord task = mTaskHistory.get(taskNdx);
2985 if (task.isHomeTask()) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002986 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG, "moveHomeTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07002987 mTaskHistory.remove(taskNdx);
2988 mTaskHistory.add(top, task);
2989 mWindowManager.moveTaskToTop(task.taskId);
2990 return;
2991 }
2992 }
2993 }
2994
Craig Mautnercae015f2013-02-08 14:31:27 -08002995 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002996 final TaskRecord task = taskForIdLocked(taskId);
2997 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08002998 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002999 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003000 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003001 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
3002 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07003003 // we'll just indicate that this task returns to the home task.
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003004 task.mOnTopOfHome = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08003005 }
3006 moveTaskToFrontLocked(task, null, options);
3007 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003008 }
3009 return false;
3010 }
3011
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003012 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003013 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003014
Craig Mautner11bf9a52013-02-19 14:08:51 -08003015 final int numTasks = mTaskHistory.size();
3016 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003017 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003018 // nothing to do!
3019 if (reason != null &&
3020 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3021 ActivityOptions.abort(options);
3022 } else {
3023 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3024 }
3025 return;
3026 }
3027
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003028 mStackSupervisor.moveHomeStack(isHomeStack());
3029
Craig Mautner11bf9a52013-02-19 14:08:51 -08003030 // Shift all activities with this task up to the top
3031 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003032 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003033
3034 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003035 if (reason != null &&
3036 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003037 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003038 ActivityRecord r = topRunningActivityLocked(null);
3039 if (r != null) {
3040 mNoAnimActivities.add(r);
3041 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003042 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003043 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003044 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003045 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003046
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003047 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003048
Craig Mautner05d29032013-05-03 13:40:13 -07003049 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003050 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003051
3052 if (VALIDATE_TOKENS) {
3053 validateAppTokensLocked();
3054 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003055 }
3056
3057 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003058 * Worker method for rearranging history stack. Implements the function of moving all
3059 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003060 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003061 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003062 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3063 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003064 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003065 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003066 * @return Returns true if the move completed, false if not.
3067 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003068 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
3069 Slog.i(TAG, "moveTaskToBack: " + taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003070
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003071 // If we have a watcher, preflight the move before committing to it. First check
3072 // for *other* available tasks, but if none are available, then try again allowing the
3073 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003074 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003075 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003076 if (next == null) {
3077 next = topRunningActivityLocked(null, 0);
3078 }
3079 if (next != null) {
3080 // ask watcher if this is allowed
3081 boolean moveOK = true;
3082 try {
3083 moveOK = mService.mController.activityResuming(next.packageName);
3084 } catch (RemoteException e) {
3085 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003086 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003087 }
3088 if (!moveOK) {
3089 return false;
3090 }
3091 }
3092 }
3093
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003094 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003095 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003096
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003097 final TaskRecord tr = taskForIdLocked(taskId);
Craig Mautnerd2328952013-03-05 12:46:26 -08003098 if (tr == null) {
3099 return false;
3100 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003101
Craig Mautner11bf9a52013-02-19 14:08:51 -08003102 mTaskHistory.remove(tr);
3103 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003104
Craig Mautnerc8143c62013-09-03 12:15:57 -07003105 // There is an assumption that moving a task to the back moves it behind the home activity.
3106 // We make sure here that some activity in the stack will launch home.
3107 ActivityRecord lastActivity = null;
3108 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003109 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3110 final TaskRecord task = mTaskHistory.get(taskNdx);
3111 if (task.mOnTopOfHome) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003112 break;
3113 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003114 if (taskNdx == 1) {
3115 // Set the last task before tr to go to home.
3116 task.mOnTopOfHome = true;
3117 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003118 }
3119
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003120 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003121 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3122 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003123 ActivityRecord r = topRunningActivityLocked(null);
3124 if (r != null) {
3125 mNoAnimActivities.add(r);
3126 }
3127 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003128 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003129 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003130 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003131
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003132 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003133 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003134 }
3135
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003136 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
3137 if (task == tr && task.mOnTopOfHome || numTasks <= 1) {
Daniel 2 Olofsson9cdf9e52013-12-16 13:24:25 +01003138 tr.mOnTopOfHome = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003139 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003140 }
3141
Craig Mautner05d29032013-05-03 13:40:13 -07003142 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003143 return true;
3144 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003145
Craig Mautner8849a5e2013-04-02 16:41:03 -07003146 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003147 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003148 final Uri data = r.intent.getData();
3149 final String strData = data != null ? data.toSafeString() : null;
3150
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003151 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003152 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003153 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003154 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003155 }
3156
3157 /**
3158 * Make sure the given activity matches the current configuration. Returns
3159 * false if the activity had to be destroyed. Returns true if the
3160 * configuration is the same, or the activity will remain running as-is
3161 * for whatever reason. Ensures the HistoryRecord is updated with the
3162 * correct configuration and all other bookkeeping is handled.
3163 */
3164 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3165 int globalChanges) {
3166 if (mConfigWillChange) {
3167 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3168 "Skipping config check (will change): " + r);
3169 return true;
3170 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003171
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003172 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3173 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003174
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003175 // Short circuit: if the two configurations are the exact same
3176 // object (the common case), then there is nothing to do.
3177 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003178 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003179 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3180 "Configuration unchanged in " + r);
3181 return true;
3182 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003183
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003184 // We don't worry about activities that are finishing.
3185 if (r.finishing) {
3186 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3187 "Configuration doesn't matter in finishing " + r);
3188 r.stopFreezingScreenLocked(false);
3189 return true;
3190 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003191
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003192 // Okay we now are going to make this activity have the new config.
3193 // But then we need to figure out how it needs to deal with that.
3194 Configuration oldConfig = r.configuration;
3195 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003196
3197 // Determine what has changed. May be nothing, if this is a config
3198 // that has come back from the app after going idle. In that case
3199 // we just want to leave the official config object now in the
3200 // activity and do nothing else.
3201 final int changes = oldConfig.diff(newConfig);
3202 if (changes == 0 && !r.forceNewConfig) {
3203 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3204 "Configuration no differences in " + r);
3205 return true;
3206 }
3207
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003208 // If the activity isn't currently running, just leave the new
3209 // configuration and it will pick that up next time it starts.
3210 if (r.app == null || r.app.thread == null) {
3211 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3212 "Configuration doesn't matter not running " + r);
3213 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003214 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003215 return true;
3216 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003217
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003218 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003219 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3220 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3221 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003222 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003223 + ", newConfig=" + newConfig);
3224 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003225 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003226 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3227 r.configChangeFlags |= changes;
3228 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003229 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003230 if (r.app == null || r.app.thread == null) {
3231 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003232 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003233 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003234 } else if (r.state == ActivityState.PAUSING) {
3235 // A little annoying: we are waiting for this activity to
3236 // finish pausing. Let's not do anything now, but just
3237 // flag that it needs to be restarted when done pausing.
3238 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003239 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003240 r.configDestroy = true;
3241 return true;
3242 } else if (r.state == ActivityState.RESUMED) {
3243 // Try to optimize this case: the configuration is changing
3244 // and we need to restart the top, resumed activity.
3245 // Instead of doing the normal handshaking, just say
3246 // "restart!".
3247 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003248 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003249 relaunchActivityLocked(r, r.configChangeFlags, true);
3250 r.configChangeFlags = 0;
3251 } else {
3252 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003253 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003254 relaunchActivityLocked(r, r.configChangeFlags, false);
3255 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003256 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003257
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003258 // All done... tell the caller we weren't able to keep this
3259 // activity around.
3260 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003261 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003262
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003263 // Default case: the activity can handle this new configuration, so
3264 // hand it over. Note that we don't need to give it the new
3265 // configuration, since we always send configuration changes to all
3266 // process when they happen so it can just use whatever configuration
3267 // it last got.
3268 if (r.app != null && r.app.thread != null) {
3269 try {
3270 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003271 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003272 } catch (RemoteException e) {
3273 // If process died, whatever.
3274 }
3275 }
3276 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003277
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003278 return true;
3279 }
3280
Craig Mautnerc8143c62013-09-03 12:15:57 -07003281 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003282 int changes, boolean andResume) {
3283 List<ResultInfo> results = null;
3284 List<Intent> newIntents = null;
3285 if (andResume) {
3286 results = r.results;
3287 newIntents = r.newIntents;
3288 }
3289 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3290 + " with results=" + results + " newIntents=" + newIntents
3291 + " andResume=" + andResume);
3292 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003293 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003294 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003295
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003296 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003297
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003298 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003299 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3300 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3301 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003302 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003303 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003304 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003305 // Note: don't need to call pauseIfSleepingLocked() here, because
3306 // the caller will only pass in 'andResume' if this activity is
3307 // currently resumed, which implies we aren't sleeping.
3308 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003309 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003310 }
3311
3312 if (andResume) {
3313 r.results = null;
3314 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003315 r.state = ActivityState.RESUMED;
3316 } else {
3317 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3318 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003319 }
3320
3321 return true;
3322 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003323
3324 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003325 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3326 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3327 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3328 final ActivityRecord r = activities.get(activityNdx);
3329 if (r.appToken == token) {
3330 return true;
3331 }
3332 if (r.fullscreen && !r.finishing) {
3333 return false;
3334 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003335 }
3336 }
3337 return true;
3338 }
3339
3340 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003341 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3342 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3343 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3344 final ActivityRecord r = activities.get(activityNdx);
3345 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003346 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003347 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003348 }
3349 }
3350 }
3351
3352 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3353 boolean didSomething = false;
3354 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003355 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3356 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3357 int numActivities = activities.size();
3358 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3359 ActivityRecord r = activities.get(activityNdx);
3360 final boolean samePackage = r.packageName.equals(name)
3361 || (name == null && r.userId == userId);
3362 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3363 && (samePackage || r.task == lastTask)
3364 && (r.app == null || evenPersistent || !r.app.persistent)) {
3365 if (!doit) {
3366 if (r.finishing) {
3367 // If this activity is just finishing, then it is not
3368 // interesting as far as something to stop.
3369 continue;
3370 }
3371 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003372 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003373 didSomething = true;
3374 Slog.i(TAG, " Force finishing activity " + r);
3375 if (samePackage) {
3376 if (r.app != null) {
3377 r.app.removed = true;
3378 }
3379 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003380 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003381 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003382 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3383 true)) {
3384 // r has been deleted from mActivities, accommodate.
3385 --numActivities;
3386 --activityNdx;
3387 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003388 }
3389 }
3390 }
3391 return didSomething;
3392 }
3393
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003394 ActivityRecord getTasksLocked(IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003395 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003396 ActivityRecord topRecord = null;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003397 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003398 final TaskRecord task = mTaskHistory.get(taskNdx);
3399 ActivityRecord r = null;
3400 ActivityRecord top = null;
3401 int numActivities = 0;
3402 int numRunning = 0;
3403 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003404 if (activities.isEmpty()) {
3405 continue;
3406 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003407 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3408 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003409
Craig Mautneraab647e2013-02-28 16:31:36 -08003410 // Initialize state for next task if needed.
3411 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3412 top = r;
3413 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003414 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003415
3416 // Add 'r' into the current task.
3417 numActivities++;
3418 if (r.app != null && r.app.thread != null) {
3419 numRunning++;
3420 }
3421
3422 if (localLOGV) Slog.v(
3423 TAG, r.intent.getComponent().flattenToShortString()
3424 + ": task=" + r.task);
3425 }
3426
3427 RunningTaskInfo ci = new RunningTaskInfo();
3428 ci.id = task.taskId;
3429 ci.baseActivity = r.intent.getComponent();
3430 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003431 ci.lastActiveTime = task.lastActiveTime;
3432
Craig Mautneraab647e2013-02-28 16:31:36 -08003433 if (top.thumbHolder != null) {
3434 ci.description = top.thumbHolder.lastDescription;
3435 }
3436 ci.numActivities = numActivities;
3437 ci.numRunning = numRunning;
3438 //System.out.println(
3439 // "#" + maxNum + ": " + " descr=" + ci.description);
3440 if (receiver != null) {
3441 if (localLOGV) Slog.v(
3442 TAG, "State=" + top.state + "Idle=" + top.idle
3443 + " app=" + top.app
3444 + " thr=" + (top.app != null ? top.app.thread : null));
3445 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3446 if (top.idle && top.app != null && top.app.thread != null) {
3447 topRecord = top;
3448 } else {
3449 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003450 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003451 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003452 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003453 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003454 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003455 }
3456 return topRecord;
3457 }
3458
3459 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003460 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003461 if (DEBUG_SWITCH) Slog.d(
3462 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003463 if (top >= 0) {
3464 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3465 int activityTop = activities.size() - 1;
3466 if (activityTop > 0) {
3467 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3468 "unhandled-back", true);
3469 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003470 }
3471 }
3472
Craig Mautner6b74cb52013-09-27 17:02:21 -07003473 /**
3474 * Reset local parameters because an app's activity died.
3475 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003476 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003477 */
3478 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003479 if (mPausingActivity != null && mPausingActivity.app == app) {
3480 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3481 "App died while pausing: " + mPausingActivity);
3482 mPausingActivity = null;
3483 }
3484 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3485 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003486 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003487 }
3488
Craig Mautner19091252013-10-05 00:03:53 -07003489 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003490 }
3491
Craig Mautnercae015f2013-02-08 14:31:27 -08003492 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003493 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3494 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3495 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3496 final ActivityRecord r = activities.get(activityNdx);
3497 if (r.app == app) {
3498 Slog.w(TAG, " Force finishing activity "
3499 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003500 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003501 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003502 }
3503 }
3504 }
3505
Dianne Hackborn390517b2013-05-30 15:03:32 -07003506 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003507 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003508 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003509 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3510 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003511 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3512 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003513 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003514 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003515 if (printed) {
3516 header = null;
3517 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003518 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003519 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003520 }
3521
3522 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3523 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3524
3525 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003526 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3527 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003528 }
3529 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003530 final int top = mTaskHistory.size() - 1;
3531 if (top >= 0) {
3532 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3533 int listTop = list.size() - 1;
3534 if (listTop >= 0) {
3535 activities.add(list.get(listTop));
3536 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003537 }
3538 } else {
3539 ItemMatcher matcher = new ItemMatcher();
3540 matcher.build(name);
3541
Craig Mautneraab647e2013-02-28 16:31:36 -08003542 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3543 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3544 if (matcher.match(r1, r1.intent.getComponent())) {
3545 activities.add(r1);
3546 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003547 }
3548 }
3549 }
3550
3551 return activities;
3552 }
3553
3554 ActivityRecord restartPackage(String packageName) {
3555 ActivityRecord starting = topRunningActivityLocked(null);
3556
3557 // All activities that came from the package must be
3558 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003559 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3560 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3561 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3562 final ActivityRecord a = activities.get(activityNdx);
3563 if (a.info.packageName.equals(packageName)) {
3564 a.forceNewConfig = true;
3565 if (starting != null && a == starting && a.visible) {
3566 a.startFreezingScreenLocked(starting.app,
3567 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3568 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003569 }
3570 }
3571 }
3572
3573 return starting;
3574 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003575
Craig Mautnerde4ef022013-04-07 19:01:33 -07003576 boolean removeTask(TaskRecord task) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003577 final int taskNdx = mTaskHistory.indexOf(task);
3578 final int topTaskNdx = mTaskHistory.size() - 1;
3579 if (task.mOnTopOfHome && taskNdx < topTaskNdx) {
3580 mTaskHistory.get(taskNdx + 1).mOnTopOfHome = true;
3581 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003582 mTaskHistory.remove(task);
Craig Mautnerc8143c62013-09-03 12:15:57 -07003583 return mTaskHistory.isEmpty();
Craig Mautner0247fc82013-02-28 14:32:06 -08003584 }
3585
Craig Mautnerde4ef022013-04-07 19:01:33 -07003586 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003587 TaskRecord task = new TaskRecord(taskId, info, intent);
3588 addTask(task, toTop);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003589 return task;
3590 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003591
3592 ArrayList<TaskRecord> getAllTasks() {
3593 return new ArrayList<TaskRecord>(mTaskHistory);
3594 }
3595
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003596 void addTask(final TaskRecord task, final boolean toTop) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003597 task.stack = this;
3598 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003599 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003600 } else {
3601 mTaskHistory.add(0, task);
3602 }
3603 }
3604
3605 public int getStackId() {
3606 return mStackId;
3607 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003608
3609 @Override
3610 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07003611 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
3612 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07003613 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003614}