blob: 809452c50bb838be780d9350c43b8e46b70267c8 [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 Hackborncee04b52013-07-03 17:01:28 -070039import android.os.Trace;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070040import com.android.internal.os.BatteryStatsImpl;
Craig Mautnerd74f7d72013-02-26 13:41:02 -080041import com.android.internal.util.Objects;
Kenny Rootadd58212013-05-07 09:47:34 -070042import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080043import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4b71aa12012-12-27 17:20:01 -080044import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080045import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070046import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070047
48import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070049import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070050import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070051import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080052import android.app.IActivityController;
Craig Mautnercae015f2013-02-08 14:31:27 -080053import android.app.IThumbnailReceiver;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080055import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.content.ComponentName;
57import android.content.Context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080062import android.content.res.Resources;
63import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080064import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070066import android.os.Bundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070067import android.os.Handler;
68import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090069import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070070import android.os.Message;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070071import android.os.RemoteException;
72import android.os.SystemClock;
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;
84
85/**
86 * State and management of a single stack of activities.
87 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070088final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080089
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070090 // Ticks during which we check progress while waiting for an app to launch.
91 static final int LAUNCH_TICK = 500;
92
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070093 // How long we wait until giving up on the last activity to pause. This
94 // is short because it directly impacts the responsiveness of starting the
95 // next activity.
96 static final int PAUSE_TIMEOUT = 500;
97
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070098 // How long we wait for the activity to tell us it has stopped before
99 // giving up. This is a good amount of time because we really need this
100 // from the application in order to get its saved state.
101 static final int STOP_TIMEOUT = 10*1000;
102
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700103 // How long we wait until giving up on an activity telling us it has
104 // finished destroying itself.
105 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800106
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700107 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800108 // disabled.
109 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800110
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700111 // How long between activity launches that we consider safe to not warn
112 // the user about an unexpected activity being launched on top.
113 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800114
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700115 // Set to false to disable the preview that is shown while a new activity
116 // is being started.
117 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800118
Craig Mautner5eda9b32013-07-02 11:58:16 -0700119 // How long to wait for all background Activities to redraw following a call to
120 // convertToTranslucent().
121 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
122
John Reck172e87c2013-10-02 16:55:16 -0700123 static final boolean SCREENSHOT_FORCE_565 = ActivityManager
124 .isLowRamDeviceStatic() ? true : false;
125
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700126 enum ActivityState {
127 INITIALIZING,
128 RESUMED,
129 PAUSING,
130 PAUSED,
131 STOPPING,
132 STOPPED,
133 FINISHING,
134 DESTROYING,
135 DESTROYED
136 }
137
138 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700139 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800140
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700141 final Context mContext;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800142
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700143 /**
144 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800145 * running) activities. It contains #TaskRecord objects.
146 */
147 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
148
149 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800150 * Used for validating app tokens with window manager.
151 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800152 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800153
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700154 /**
155 * List of running activities, sorted by recent usage.
156 * The first entry in the list is the least recently used.
157 * It contains HistoryRecord objects.
158 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700159 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700160
161 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700162 * Animations that for the current transition have requested not to
163 * be considered for the transition animation.
164 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700165 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700166
167 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700168 * When we are in the process of pausing an activity, before starting the
169 * next one, this variable holds the activity that is currently being paused.
170 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800171 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700172
173 /**
174 * This is the last activity that we put into the paused state. This is
175 * used to determine if we need to do an activity transition while sleeping,
176 * when we normally hold the top activity paused.
177 */
178 ActivityRecord mLastPausedActivity = null;
179
180 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700181 * Activities that specify No History must be removed once the user navigates away from them.
182 * If the device goes to sleep with such an activity in the paused state then we save it here
183 * and finish it later if another activity replaces it on wakeup.
184 */
185 ActivityRecord mLastNoHistoryActivity = null;
186
187 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700188 * Current activity that is resumed, or null if there is none.
189 */
190 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800191
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700192 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700193 * This is the last activity that has been started. It is only used to
194 * identify when multiple activities are started at once so that the user
195 * can be warned they may not be in the activity they think they are.
196 */
197 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800198
Craig Mautner5eda9b32013-07-02 11:58:16 -0700199 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
200 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
201 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
202 // Activity in mTranslucentActivityWaiting is notified via
203 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
204 // background activity being drawn then the same call will be made with a true value.
205 ActivityRecord mTranslucentActivityWaiting = null;
206 ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
207 new ArrayList<ActivityRecord>();
208
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700209 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700210 * Set when we know we are going to be calling updateConfiguration()
211 * soon, so want to skip intermediate config checks.
212 */
213 boolean mConfigWillChange;
214
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700215 long mLaunchStartTime = 0;
216 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800217
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800218 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800219 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
220 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
221 */
222 private ActivityRecord mLastScreenshotActivity = null;
223 private Bitmap mLastScreenshotBitmap = null;
224
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800225 int mThumbnailWidth = -1;
226 int mThumbnailHeight = -1;
227
Craig Mautner858d8a62013-04-23 17:08:34 -0700228 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700229
Craig Mautnerc00204b2013-03-05 15:02:14 -0800230 final int mStackId;
231
Craig Mautner27084302013-03-25 08:05:25 -0700232 /** Run all ActivityStacks through this */
233 final ActivityStackSupervisor mStackSupervisor;
234
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700235 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700236 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
237 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
238 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
239 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700240 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700241
242 static class ScheduleDestroyArgs {
243 final ProcessRecord mOwner;
244 final boolean mOomAdj;
245 final String mReason;
246 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
247 mOwner = owner;
248 mOomAdj = oomAdj;
249 mReason = reason;
250 }
251 }
252
Zoran Marcetaf958b322012-08-09 20:27:12 +0900253 final Handler mHandler;
254
255 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700256 //public Handler() {
257 // if (localLOGV) Slog.v(TAG, "Handler started!");
258 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700259 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900260 super(looper);
261 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700262
Zoran Marcetaf958b322012-08-09 20:27:12 +0900263 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700264 public void handleMessage(Message msg) {
265 switch (msg.what) {
266 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800267 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700268 // We don't at this point know if the activity is fullscreen,
269 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800270 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700271 synchronized (mService) {
272 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700273 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700274 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700275 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800276 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700277 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700278 case LAUNCH_TICK_MSG: {
279 ActivityRecord r = (ActivityRecord)msg.obj;
280 synchronized (mService) {
281 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700282 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700283 }
284 }
285 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700286 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800287 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700288 // We don't at this point know if the activity is fullscreen,
289 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800290 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800291 synchronized (mService) {
292 activityDestroyedLocked(r != null ? r.appToken : null);
293 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700294 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700295 case STOP_TIMEOUT_MSG: {
296 ActivityRecord r = (ActivityRecord)msg.obj;
297 // We don't at this point know if the activity is fullscreen,
298 // so we need to be conservative and assume it isn't.
299 Slog.w(TAG, "Activity stop timeout for " + r);
300 synchronized (mService) {
301 if (r.isInHistory()) {
302 activityStoppedLocked(r, null, null, null);
303 }
304 }
305 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700306 case DESTROY_ACTIVITIES_MSG: {
307 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
308 synchronized (mService) {
309 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
310 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700311 } break;
312 case TRANSLUCENT_TIMEOUT_MSG: {
313 synchronized (mService) {
314 notifyActivityDrawnLocked(null);
315 }
316 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700317 }
318 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800319 }
320
Craig Mautner000f0022013-02-26 15:04:29 -0800321 private int numActivities() {
322 int count = 0;
323 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
324 count += mTaskHistory.get(taskNdx).mActivities.size();
325 }
326 return count;
327 }
328
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700329 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900330 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700331 mService = service;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700332 mWindowManager = service.mWindowManager;
333 mStackSupervisor = service.mStackSupervisor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700334 mContext = context;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800335 mStackId = stackId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700336 mCurrentUser = service.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700337 }
Craig Mautner5962b122012-10-05 14:45:52 -0700338
Craig Mautner6b74cb52013-09-27 17:02:21 -0700339 boolean okToShow(ActivityRecord r) {
Craig Mautner5962b122012-10-05 14:45:52 -0700340 return r.userId == mCurrentUser
341 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
342 }
343
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700344 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800345 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700346 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
347 if (r != null) {
348 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800349 }
350 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700351 return null;
352 }
353
354 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800355 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
356 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800357 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800358 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
359 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800360 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
361 return r;
362 }
363 }
364 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700365 return null;
366 }
367
368 /**
369 * This is a simplified version of topRunningActivityLocked that provides a number of
370 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800371 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700372 * @param token If non-null, any history records matching this token will be skipped.
373 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800374 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700375 * @return Returns the HistoryRecord of the next activity on the stack.
376 */
377 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800378 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
379 TaskRecord task = mTaskHistory.get(taskNdx);
380 if (task.taskId == taskId) {
381 continue;
382 }
383 ArrayList<ActivityRecord> activities = task.mActivities;
384 for (int i = activities.size() - 1; i >= 0; --i) {
385 final ActivityRecord r = activities.get(i);
386 // Note: the taskId check depends on real taskId fields being non-zero
387 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
388 return r;
389 }
390 }
391 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700392 return null;
393 }
394
Craig Mautner8849a5e2013-04-02 16:41:03 -0700395 final ActivityRecord topActivity() {
396 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700397 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700398 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
399 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
400 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700401 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700402 }
403 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700404 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700405 }
406
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700407 final TaskRecord topTask() {
408 final int size = mTaskHistory.size();
409 if (size > 0) {
410 return mTaskHistory.get(size - 1);
411 }
412 return null;
413 }
414
Craig Mautnerd2328952013-03-05 12:46:26 -0800415 TaskRecord taskForIdLocked(int id) {
416 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
417 final TaskRecord task = mTaskHistory.get(taskNdx);
418 if (task.taskId == id) {
419 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800420 }
421 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700422 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700423 }
424
Craig Mautnerd2328952013-03-05 12:46:26 -0800425 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800426 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800427 if (r != null) {
428 final TaskRecord task = r.task;
429 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
430 if (task.stack != this) Slog.w(TAG,
431 "Illegal state! task does not point to stack it is in.");
432 return r;
433 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800434 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800435 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800436 }
437
Craig Mautnerf88c50f2013-04-18 19:25:12 -0700438 boolean containsApp(ProcessRecord app) {
439 if (app == null) {
440 return false;
441 }
442 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
443 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
444 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
445 final ActivityRecord r = activities.get(activityNdx);
446 if (r.finishing) {
447 continue;
448 }
449 if (r.app == app) {
450 return true;
451 }
452 }
453 }
454 return false;
455 }
456
Craig Mautner2420ead2013-04-01 17:13:20 -0700457 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700458 final boolean hadit = mLRUActivities.remove(r);
459 mLRUActivities.add(r);
460 return hadit;
461 }
462
Craig Mautnerde4ef022013-04-07 19:01:33 -0700463 final boolean isHomeStack() {
464 return mStackId == HOME_STACK_ID;
465 }
466
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700467 /**
468 * Returns the top activity in any existing task matching the given
469 * Intent. Returns null if no such task is found.
470 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700471 ActivityRecord findTaskLocked(ActivityRecord target) {
472 Intent intent = target.intent;
473 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700474 ComponentName cls = intent.getComponent();
475 if (info.targetActivity != null) {
476 cls = new ComponentName(info.packageName, info.targetActivity);
477 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700478 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800479
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700480 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800481 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
482 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700483 if (task.userId != userId) {
484 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700485 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700486 continue;
487 }
Craig Mautner000f0022013-02-26 15:04:29 -0800488 final ActivityRecord r = task.getTopActivity();
489 if (r == null || r.finishing || r.userId != userId ||
490 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700491 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800492 continue;
493 }
494
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700495 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
496 + r.task.intent.getComponent().flattenToShortString()
497 + "/aff=" + r.task.affinity + " to new cls="
498 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Craig Mautner000f0022013-02-26 15:04:29 -0800499 if (task.affinity != null) {
500 if (task.affinity.equals(info.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700501 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700502 return r;
503 }
Craig Mautner000f0022013-02-26 15:04:29 -0800504 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700505 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800506 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700507 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
508 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800509 return r;
510 } else if (task.affinityIntent != null
511 && task.affinityIntent.getComponent().equals(cls)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700512 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800513 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700514 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
515 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800516 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700517 } else if (DEBUG_TASKS) {
518 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700519 }
520 }
521
522 return null;
523 }
524
525 /**
526 * Returns the first activity (starting from the top of the stack) that
527 * is the same as the given activity. Returns null if no such activity
528 * is found.
529 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700530 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700531 ComponentName cls = intent.getComponent();
532 if (info.targetActivity != null) {
533 cls = new ComponentName(info.packageName, info.targetActivity);
534 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700535 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700536
Craig Mautner000f0022013-02-26 15:04:29 -0800537 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700538 TaskRecord task = mTaskHistory.get(taskNdx);
539 if (task.userId != mCurrentUser) {
540 return null;
541 }
542 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800543 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
544 ActivityRecord r = activities.get(activityNdx);
545 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700546 //Slog.i(TAG, "Found matching class!");
547 //dump();
548 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
549 return r;
550 }
551 }
552 }
553
554 return null;
555 }
556
Amith Yamasani742a6712011-05-04 14:49:28 -0700557 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700558 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700559 */
Craig Mautner93529a42013-10-04 15:03:13 -0700560 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800561 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700562 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800563 }
564 mCurrentUser = userId;
565
566 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800567 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800568 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700569 TaskRecord task = mTaskHistory.get(i);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800570 if (task.userId == userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700571 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
572 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800573 mTaskHistory.remove(i);
574 mTaskHistory.add(task);
575 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800576 // Use same value for i.
577 } else {
578 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800579 }
580 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700581 if (VALIDATE_TOKENS) {
582 validateAppTokensLocked();
583 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700584 }
585
Craig Mautner2420ead2013-04-01 17:13:20 -0700586 void minimalResumeActivityLocked(ActivityRecord r) {
587 r.state = ActivityState.RESUMED;
588 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
589 + " (starting new instance)");
590 r.stopped = false;
591 mResumedActivity = r;
592 r.task.touchActiveTime();
593 mService.addRecentTaskLocked(r.task);
594 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700595 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700596 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700597 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700598 }
599
Dianne Hackborncee04b52013-07-03 17:01:28 -0700600 private void startLaunchTraces() {
601 if (mFullyDrawnStartTime != 0) {
602 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
603 }
604 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
605 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
606 }
607
608 private void stopFullyDrawnTraceIfNeeded() {
609 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
610 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
611 mFullyDrawnStartTime = 0;
612 }
613 }
614
Craig Mautnere79d42682013-04-01 19:01:53 -0700615 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700616 if (r.displayStartTime == 0) {
617 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
618 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700619 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700620 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700621 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700622 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700623 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700624 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700625 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700626 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800627
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700628 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700629 // Make sure that there is no activity waiting for this to launch.
630 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
631 r.displayStartTime = r.fullyDrawnStartTime = 0;
632 } else {
633 mStackSupervisor.removeTimeoutsForActivityLocked(r);
634 mStackSupervisor.scheduleIdleTimeoutLocked(r);
635 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700636 }
637
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800638 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800639 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800640 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
641 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
642 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
643 activities.get(activityNdx).setSleeping(false);
644 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800645 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800646 }
647
Craig Mautner0eea92c2013-05-16 13:35:39 -0700648 /**
649 * @return true if something must be done before going to sleep.
650 */
651 boolean checkReadyForSleepLocked() {
652 if (mResumedActivity != null) {
653 // Still have something resumed; can't sleep until it is paused.
654 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
655 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
656 startPausingLocked(false, true);
657 return true;
658 }
659 if (mPausingActivity != null) {
660 // Still waiting for something to pause; can't sleep yet.
661 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
662 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800663 }
664
Craig Mautner0eea92c2013-05-16 13:35:39 -0700665 return false;
666 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800667
Craig Mautner0eea92c2013-05-16 13:35:39 -0700668 void goToSleep() {
669 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800670
Craig Mautner0eea92c2013-05-16 13:35:39 -0700671 // Make sure any stopped but visible activities are now sleeping.
672 // This ensures that the activity's onStop() is called.
673 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
674 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
675 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
676 final ActivityRecord r = activities.get(activityNdx);
677 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
678 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800679 }
680 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800681 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700682 }
Craig Mautner59c00972012-07-30 12:10:24 -0700683
Dianne Hackbornd2835932010-12-13 16:28:46 -0800684 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800685 if (who.noDisplay) {
686 return null;
687 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800688
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700689 TaskRecord tr = who.task;
Craig Mautner34740402013-10-11 11:05:35 -0700690 if (mService.getMostRecentTask() != tr && tr.intent != null &&
691 (tr.intent.getFlags() & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) != 0) {
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700692 // If this task is being excluded from recents, we don't want to take
693 // the expense of capturing a thumbnail, since we will never show it.
694 return null;
695 }
696
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800697 Resources res = mService.mContext.getResources();
698 int w = mThumbnailWidth;
699 int h = mThumbnailHeight;
700 if (w < 0) {
701 mThumbnailWidth = w =
702 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
703 mThumbnailHeight = h =
704 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
705 }
706
707 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800708 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700709 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800710 || mLastScreenshotBitmap.getWidth() != w
711 || mLastScreenshotBitmap.getHeight() != h) {
712 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700713 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700714 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800715 }
716 if (mLastScreenshotBitmap != null) {
John Reck172e87c2013-10-02 16:55:16 -0700717 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800718 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800719 }
720 return null;
721 }
722
Craig Mautnercf910b02013-04-23 11:23:27 -0700723 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800724 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800725 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700726 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800727 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700728 ActivityRecord prev = mResumedActivity;
729 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700730 Slog.e(TAG, "Trying to pause when nothing is resumed",
731 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700732 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700733 return;
734 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700735 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
736 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700737 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800738 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700739 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700740 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
741 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700742 prev.state = ActivityState.PAUSING;
743 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700744 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700745 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
746 if (next == null || next.task != prev.task) {
747 prev.updateThumbnail(screenshotActivities(prev), null);
748 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700749 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700750
751 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800752
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700753 if (prev.app != null && prev.app.thread != null) {
754 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
755 try {
756 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700757 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700758 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700759 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800760 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
761 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700762 } catch (Exception e) {
763 // Ignore exception, if process died other code will cleanup.
764 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800765 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700766 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700767 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700768 }
769 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800770 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700771 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700772 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700773 }
774
775 // If we are not going to sleep, we want to ensure the device is
776 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700777 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700778 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700779 }
780
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800781 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700782 // Have the window manager pause its key dispatching until the new
783 // activity has started. If we're pausing the activity just because
784 // the screen is being turned off and the UI is sleeping, don't interrupt
785 // key dispatch; the same activity will pick it up again on wakeup.
786 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800787 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700788 } else {
789 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
790 }
791
792 // Schedule a pause timeout in case the app doesn't respond.
793 // We don't give it much time because this directly impacts the
794 // responsiveness seen by the user.
795 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
796 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700797 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700798 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
799 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
800 } else {
801 // This activity failed to schedule the
802 // pause, so just treat it as being paused now.
803 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700804 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700805 }
806 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700807
Craig Mautnerd2328952013-03-05 12:46:26 -0800808 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700809 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800810 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700811
Craig Mautnerd2328952013-03-05 12:46:26 -0800812 final ActivityRecord r = isInStackLocked(token);
813 if (r != null) {
814 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
815 if (mPausingActivity == r) {
816 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
817 + (timeout ? " (due to timeout)" : " (pause complete)"));
818 r.state = ActivityState.PAUSED;
819 completePauseLocked();
820 } else {
821 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
822 r.userId, System.identityHashCode(r), r.shortComponentName,
823 mPausingActivity != null
824 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700825 }
826 }
827 }
828
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700829 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
830 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700831 if (r.state != ActivityState.STOPPING) {
832 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
833 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
834 return;
835 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700836 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700837 if (icicle != null) {
838 // If icicle is null, this is happening due to a timeout, so we
839 // haven't really saved the state.
840 r.icicle = icicle;
841 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800842 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700843 r.updateThumbnail(thumbnail, description);
844 }
845 if (!r.stopped) {
846 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
847 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
848 r.stopped = true;
849 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700850 if (r.finishing) {
851 r.clearOptionsLocked();
852 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700853 if (r.configDestroy) {
854 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700855 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700856 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700857 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800858 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700859 }
860 }
861 }
862
Craig Mautnerc8143c62013-09-03 12:15:57 -0700863 private void completePauseLocked() {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800864 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700865 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800866
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800867 if (prev != null) {
868 if (prev.finishing) {
869 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700870 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800871 } else if (prev.app != null) {
872 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
873 if (prev.waitingVisible) {
874 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700875 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800876 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
877 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800878 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800879 if (prev.configDestroy) {
880 // The previous is being paused because the configuration
881 // is changing, which means it is actually stopping...
882 // To juggle the fact that we are also starting a new
883 // instance right now, we need to first completely stop
884 // the current instance before starting the new one.
885 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
886 destroyActivityLocked(prev, true, false, "pause-config");
887 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700888 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700889 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
890 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800891 // If we already have a few activities waiting to stop,
892 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700893 // them out. Or if r is the last of activity of the last task the stack
894 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800895 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700896 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800897 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700898 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800899 }
900 }
901 } else {
902 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
903 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800904 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800905 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700906 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800907
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700908 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700909 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700910 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800911 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700912 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700913 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700914 if (top == null || (prev != null && top != prev)) {
915 // If there are no more activities available to run,
916 // do resume anyway to start something. Also if the top
917 // activity on the stack is not the just paused activity,
918 // we need to go ahead and resume it to ensure we complete
919 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700920 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700921 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700922 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800923
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800924 if (prev != null) {
925 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700926
Craig Mautner525f3d92013-05-07 14:01:50 -0700927 if (prev.app != null && prev.cpuTimeAtResume > 0
928 && mService.mBatteryStatsService.isOnBattery()) {
929 long diff;
Dianne Hackbornd2932242013-08-05 18:18:42 -0700930 synchronized (mService.mProcessCpuThread) {
931 diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
Craig Mautner525f3d92013-05-07 14:01:50 -0700932 - prev.cpuTimeAtResume;
933 }
934 if (diff > 0) {
935 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
936 synchronized (bsi) {
937 BatteryStatsImpl.Uid.Proc ps =
938 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -0700939 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -0700940 if (ps != null) {
941 ps.addForegroundTimeLocked(diff);
942 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700943 }
944 }
945 }
Craig Mautner525f3d92013-05-07 14:01:50 -0700946 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700947 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700948 }
949
950 /**
951 * Once we know that we have asked an application to put an activity in
952 * the resumed state (either by launching it or explicitly telling it),
953 * this function updates the rest of our state to match that fact.
954 */
Craig Mautner525f3d92013-05-07 14:01:50 -0700955 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700956 next.idle = false;
957 next.results = null;
958 next.newIntents = null;
Craig Mautner07566322013-09-26 16:42:55 -0700959 if (next.nowVisible) {
960 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
961 mStackSupervisor.dismissKeyguard();
962 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700963
964 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -0700965 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700966
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700967 mStackSupervisor.reportResumedActivityLocked(next);
968
969 next.resumeKeyDispatchingLocked();
970 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700971
972 // Mark the point when the activity is resuming
973 // TODO: To be more accurate, the mark should be before the onCreate,
974 // not after the onResume. But for subsequent starts, onResume is fine.
975 if (next.app != null) {
Dianne Hackbornd2932242013-08-05 18:18:42 -0700976 synchronized (mService.mProcessCpuThread) {
977 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700978 }
979 } else {
980 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
981 }
982 }
983
Craig Mautner580ea812013-04-25 12:58:38 -0700984 /**
Craig Mautner39e1c5a2013-10-23 15:14:22 -0700985 * Determine if home should be visible below the passed record.
986 * @param record activity we are querying for.
987 * @return true if home is visible below the passed activity, false otherwise.
988 */
989 boolean isActivityOverHome(ActivityRecord record) {
990 // Start at record and go down, look for either home or a visible fullscreen activity.
991 final TaskRecord recordTask = record.task;
992 for (int taskNdx = mTaskHistory.indexOf(recordTask); taskNdx >= 0; --taskNdx) {
993 TaskRecord task = mTaskHistory.get(taskNdx);
994 final ArrayList<ActivityRecord> activities = task.mActivities;
995 final int startNdx =
996 task == recordTask ? activities.indexOf(record) : activities.size() - 1;
997 for (int activityNdx = startNdx; activityNdx >= 0; --activityNdx) {
998 final ActivityRecord r = activities.get(activityNdx);
999 if (r.isHomeActivity()) {
1000 return true;
1001 }
Craig Mautner76be9d22013-11-04 16:01:22 -08001002 if (!r.finishing && r.fullscreen) {
1003 // Passed activity is over a fullscreen activity.
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001004 return false;
1005 }
1006 }
1007 if (task.mOnTopOfHome) {
1008 // Got to the bottom of a task on top of home without finding a visible fullscreen
1009 // activity. Home is visible.
1010 return true;
1011 }
1012 }
1013 // Got to the bottom of this stack and still don't know. If this is over the home stack
1014 // then record is over home. May not work if we ever get more than two layers.
1015 return mStackSupervisor.isFrontStack(this);
1016 }
1017
1018 /**
Craig Mautner580ea812013-04-25 12:58:38 -07001019 * Version of ensureActivitiesVisible that can easily be called anywhere.
1020 */
1021 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1022 return ensureActivitiesVisibleLocked(starting, configChanges, false);
1023 }
1024
1025 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1026 boolean forceHomeShown) {
1027 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -07001028 return r != null &&
1029 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -07001030 }
1031
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001032 /**
1033 * Make sure that all activities that need to be visible (that is, they
1034 * currently can be seen by the user) actually are.
1035 */
Craig Mautner580ea812013-04-25 12:58:38 -07001036 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
1037 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001038 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001039 TAG, "ensureActivitiesVisible behind " + top
1040 + " configChanges=0x" + Integer.toHexString(configChanges));
1041
Craig Mautner5eda9b32013-07-02 11:58:16 -07001042 if (mTranslucentActivityWaiting != top) {
1043 mUndrawnActivitiesBelowTopTranslucent.clear();
1044 if (mTranslucentActivityWaiting != null) {
1045 // Call the callback with a timeout indication.
1046 notifyActivityDrawnLocked(null);
1047 mTranslucentActivityWaiting = null;
1048 }
1049 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1050 }
1051
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001052 // If the top activity is not fullscreen, then we need to
1053 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001054 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -07001055 boolean showHomeBehindStack = false;
1056 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
1057 !(forceHomeShown && isHomeStack());
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001058 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001059 final TaskRecord task = mTaskHistory.get(taskNdx);
1060 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001061 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1062 final ActivityRecord r = activities.get(activityNdx);
1063 if (r.finishing) {
1064 continue;
1065 }
1066 if (aboveTop && r != top) {
1067 continue;
1068 }
1069 aboveTop = false;
1070 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001071 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001072 TAG, "Make visible? " + r + " finishing=" + r.finishing
1073 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001074
Craig Mautnerd44711d2013-02-23 11:24:36 -08001075 final boolean doThisProcess = onlyThisProcess == null
1076 || onlyThisProcess.equals(r.processName);
1077
1078 // First: if this is not the current activity being started, make
1079 // sure it matches the current configuration.
1080 if (r != starting && doThisProcess) {
1081 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001082 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001083
1084 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001085 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001086 // This activity needs to be visible, but isn't even
1087 // running... get it started, but don't resume it
1088 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001089 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001090 if (r != starting) {
1091 r.startFreezingScreenLocked(r.app, configChanges);
1092 }
1093 if (!r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001094 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001095 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001096 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001097 }
1098 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001099 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001100 }
1101 }
1102
1103 } else if (r.visible) {
1104 // If this activity is already visible, then there is nothing
1105 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001106 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001107 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001108
1109 } else if (onlyThisProcess == null) {
1110 // This activity is not currently visible, but is running.
1111 // Tell it to become visible.
1112 r.visible = true;
1113 if (r.state != ActivityState.RESUMED && r != starting) {
1114 // If this activity is paused, tell it
1115 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001116 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001117 TAG, "Making visible and scheduling visibility: " + r);
1118 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001119 if (mTranslucentActivityWaiting != null) {
1120 mUndrawnActivitiesBelowTopTranslucent.add(r);
1121 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001122 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001123 r.sleeping = false;
1124 r.app.pendingUiClean = true;
1125 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1126 r.stopFreezingScreenLocked(false);
1127 } catch (Exception e) {
1128 // Just skip on any failure; we'll make it
1129 // visible when it next restarts.
1130 Slog.w(TAG, "Exception thrown making visibile: "
1131 + r.intent.getComponent(), e);
1132 }
1133 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001134 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001135
Craig Mautnerd44711d2013-02-23 11:24:36 -08001136 // Aggregate current change flags.
1137 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001138
Craig Mautnerd44711d2013-02-23 11:24:36 -08001139 if (r.fullscreen) {
1140 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001141 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1142 behindFullscreen = true;
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001143 } else if (isActivityOverHome(r)) {
1144 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1145 showHomeBehindStack = true;
1146 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001147 }
1148 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001149 if (DEBUG_VISBILITY) Slog.v(
1150 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1151 + " state=" + r.state
1152 + " behindFullscreen=" + behindFullscreen);
1153 // Now for any activities that aren't visible to the user, make
1154 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001155 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001156 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001157 r.visible = false;
1158 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001159 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001160 switch (r.state) {
1161 case STOPPING:
1162 case STOPPED:
1163 if (r.app != null && r.app.thread != null) {
1164 if (DEBUG_VISBILITY) Slog.v(
1165 TAG, "Scheduling invisibility: " + r);
1166 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1167 }
1168 break;
1169
1170 case INITIALIZING:
1171 case RESUMED:
1172 case PAUSING:
1173 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001174 // This case created for transitioning activities from
1175 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautnere5273b42013-09-09 12:57:47 -07001176 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1177 mStackSupervisor.mStoppingActivities.add(r);
1178 }
1179 mStackSupervisor.scheduleIdleLocked();
Craig Mautner4addfc52013-06-25 08:05:45 -07001180 break;
1181
1182 default:
1183 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001184 }
1185 } catch (Exception e) {
1186 // Just skip on any failure; we'll make it
1187 // visible when it next restarts.
1188 Slog.w(TAG, "Exception thrown making hidden: "
1189 + r.intent.getComponent(), e);
1190 }
1191 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001192 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001193 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001194 }
1195 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001196 }
Craig Mautner580ea812013-04-25 12:58:38 -07001197 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001198 }
Craig Mautner58547802013-03-05 08:23:53 -08001199
Craig Mautner5eda9b32013-07-02 11:58:16 -07001200 void convertToTranslucent(ActivityRecord r) {
1201 mTranslucentActivityWaiting = r;
1202 mUndrawnActivitiesBelowTopTranslucent.clear();
1203 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1204 }
1205
1206 /**
1207 * Called as activities below the top translucent activity are redrawn. When the last one is
1208 * redrawn notify the top activity by calling
1209 * {@link Activity#onTranslucentConversionComplete}.
1210 *
1211 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1212 * occurred and the activity will be notified immediately.
1213 */
1214 void notifyActivityDrawnLocked(ActivityRecord r) {
1215 if ((r == null)
1216 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1217 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1218 // The last undrawn activity below the top has just been drawn. If there is an
1219 // opaque activity at the top, notify it that it can become translucent safely now.
1220 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1221 mTranslucentActivityWaiting = null;
1222 mUndrawnActivitiesBelowTopTranslucent.clear();
1223 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1224
1225 if (waitingActivity != null && waitingActivity.app != null &&
1226 waitingActivity.app.thread != null) {
1227 try {
1228 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1229 waitingActivity.appToken, r != null);
1230 } catch (RemoteException e) {
1231 }
1232 }
1233 }
1234 }
1235
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001236 /**
1237 * Ensure that the top activity in the stack is resumed.
1238 *
1239 * @param prev The previously resumed activity, for when in the process
1240 * of pausing; can be null to call from elsewhere.
1241 *
1242 * @return Returns true if something is being resumed, or false if
1243 * nothing happened.
1244 */
1245 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001246 return resumeTopActivityLocked(prev, null);
1247 }
1248
1249 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001250 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1251
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001252 // Find the first activity that is not finishing.
1253 ActivityRecord next = topRunningActivityLocked(null);
1254
1255 // Remember how we'll process this pause/resume situation, and ensure
1256 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001257 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1258 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001259
1260 if (next == null) {
1261 // There are no more activities! Let's just start up the
1262 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001263 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001264 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001265 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001266 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001267 }
1268
1269 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001270
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001271 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001272 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1273 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001274 // Make sure we have executed any pending transitions, since there
1275 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001276 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001277 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001278 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001279 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001280 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001281 return false;
1282 }
1283
Craig Mautner525f3d92013-05-07 14:01:50 -07001284 final TaskRecord nextTask = next.task;
1285 final TaskRecord prevTask = prev != null ? prev.task : null;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001286 if (prevTask != null && prevTask.mOnTopOfHome && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001287 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001288 if (prevTask == nextTask) {
1289 ArrayList<ActivityRecord> activities = prevTask.mActivities;
1290 final int numActivities = activities.size();
1291 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1292 final ActivityRecord r = activities.get(activityNdx);
1293 // r is usually the same as next, but what if two activities were launched
1294 // before prev finished?
1295 if (!r.finishing) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001296 r.frontOfTask = true;
1297 break;
1298 }
1299 }
1300 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001301 // This task is going away but it was supposed to return to the home task.
1302 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001303 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001304 mTaskHistory.get(taskNdx).mOnTopOfHome = true;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001305 } else {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001306 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Launching home next");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001307 return mStackSupervisor.resumeHomeActivity(prev);
1308 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001309 }
1310
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001311 // If we are sleeping, and there is no resumed activity, and the top
1312 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001313 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001314 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001315 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001316 // Make sure we have executed any pending transitions, since there
1317 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001318 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001319 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001320 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001321 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001322 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001323 return false;
1324 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001325
1326 // Make sure that the user who owns this activity is started. If not,
1327 // we will just leave it as is because someone should be bringing
1328 // another user's activities to the top of the stack.
1329 if (mService.mStartedUsers.get(next.userId) == null) {
1330 Slog.w(TAG, "Skipping resume of top activity " + next
1331 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001332 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001333 return false;
1334 }
1335
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001336 // The activity may be waiting for stop, but that is no longer
1337 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001338 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001339 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001340 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001341 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001342
Dianne Hackborn84375872012-06-01 19:03:50 -07001343 next.updateOptionsLocked(options);
1344
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001345 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1346
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001347 // If we are currently pausing an activity, then don't do anything
1348 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001349 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001350 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1351 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001352 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001353 return false;
1354 }
1355
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001356 // Okay we are now going to start a switch, to 'next'. We may first
1357 // have to pause the current activity, but this is an important point
1358 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001359 // XXX "App Redirected" dialog is getting too many false positives
1360 // at this point, so turn off for now.
1361 if (false) {
1362 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1363 long now = SystemClock.uptimeMillis();
1364 final boolean inTime = mLastStartedActivity.startTime != 0
1365 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1366 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1367 final int nextUid = next.info.applicationInfo.uid;
1368 if (inTime && lastUid != nextUid
1369 && lastUid != next.launchedFromUid
1370 && mService.checkPermission(
1371 android.Manifest.permission.STOP_APP_SWITCHES,
1372 -1, next.launchedFromUid)
1373 != PackageManager.PERMISSION_GRANTED) {
1374 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1375 } else {
1376 next.startTime = now;
1377 mLastStartedActivity = next;
1378 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001379 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001380 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001381 mLastStartedActivity = next;
1382 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001383 }
Craig Mautner58547802013-03-05 08:23:53 -08001384
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001385 // We need to start pausing the current activity so the top one
1386 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001387 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001388 if (mResumedActivity != null) {
1389 pausing = true;
1390 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001391 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001392 }
1393 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001394 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1395 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001396 // At this point we want to put the upcoming activity's process
1397 // at the top of the LRU list, since we know we will be needing it
1398 // very soon and it would be a waste to let it get killed if it
1399 // happens to be sitting towards the end.
1400 if (next.app != null && next.app.thread != null) {
1401 // No reason to do full oom adj update here; we'll let that
1402 // happen whenever it needs to later.
Dianne Hackborndb926082013-10-31 16:32:44 -07001403 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001404 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001405 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001406 return true;
1407 }
1408
Christopher Tated3f175c2012-06-14 14:16:54 -07001409 // If the most recent activity was noHistory but was only stopped rather
1410 // than stopped+finished because the device went to sleep, we need to make
1411 // sure to finish it as we're making a new activity topmost.
Craig Mautner0f922742013-08-06 08:44:42 -07001412 if (mService.mSleeping && mLastNoHistoryActivity != null &&
1413 !mLastNoHistoryActivity.finishing) {
1414 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1415 " on new resume");
1416 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1417 null, "no-history", false);
1418 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001419 }
1420
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001421 if (prev != null && prev != next) {
1422 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1423 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001424 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001425 if (DEBUG_SWITCH) Slog.v(
1426 TAG, "Resuming top, waiting visible to hide: " + prev);
1427 } else {
1428 // The next activity is already visible, so hide the previous
1429 // activity's windows right now so we can show the new one ASAP.
1430 // We only do this if the previous is finishing, which should mean
1431 // it is on top of the one being resumed so hiding it quickly
1432 // is good. Otherwise, we want to do the normal route of allowing
1433 // the resumed activity to be shown so we can decide if the
1434 // previous should actually be hidden depending on whether the
1435 // new one is found to be full-screen or not.
1436 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001437 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001438 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1439 + prev + ", waitingVisible="
1440 + (prev != null ? prev.waitingVisible : null)
1441 + ", nowVisible=" + next.nowVisible);
1442 } else {
1443 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1444 + prev + ", waitingVisible="
1445 + (prev != null ? prev.waitingVisible : null)
1446 + ", nowVisible=" + next.nowVisible);
1447 }
1448 }
1449 }
1450
Dianne Hackborne7f97212011-02-24 14:40:20 -08001451 // Launching this app's activity, make sure the app is no longer
1452 // considered stopped.
1453 try {
1454 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001455 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001456 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001457 } catch (IllegalArgumentException e) {
1458 Slog.w(TAG, "Failed trying to unstop package "
1459 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001460 }
1461
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001462 // We are starting up the next activity, so tell the window manager
1463 // that the previous one will be hidden soon. This way it can know
1464 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001465 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001466 if (prev != null) {
1467 if (prev.finishing) {
1468 if (DEBUG_TRANSITION) Slog.v(TAG,
1469 "Prepare close transition: prev=" + prev);
1470 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001471 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001472 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001473 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001474 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001475 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1476 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001477 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001478 mWindowManager.setAppWillBeHidden(prev.appToken);
1479 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001480 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001481 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001482 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001483 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001484 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001485 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001486 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001487 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1488 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001489 }
1490 }
1491 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001492 mWindowManager.setAppWillBeHidden(prev.appToken);
1493 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001494 }
Craig Mautner967212c2013-04-13 21:10:58 -07001495 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001496 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001497 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001498 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001499 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001500 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001501 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001502 }
1503 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001504 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001505 next.applyOptionsLocked();
1506 } else {
1507 next.clearOptionsLocked();
1508 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001509
Craig Mautnercf910b02013-04-23 11:23:27 -07001510 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001511 if (next.app != null && next.app.thread != null) {
1512 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1513
1514 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001515 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001516
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001517 // schedule launch ticks to collect information about slow apps.
1518 next.startLaunchTickingLocked();
1519
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001520 ActivityRecord lastResumedActivity =
1521 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001522 ActivityState lastState = next.state;
1523
1524 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001525
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001526 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001527 next.state = ActivityState.RESUMED;
1528 mResumedActivity = next;
1529 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001530 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001531 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001532 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001533 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001534
1535 // Have the window manager re-evaluate the orientation of
1536 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001537 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001538 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001539 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001540 mService.mConfiguration,
1541 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1542 if (config != null) {
1543 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001544 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001545 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001546 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001547
Craig Mautner525f3d92013-05-07 14:01:50 -07001548 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001549 // The configuration update wasn't able to keep the existing
1550 // instance of the activity, and instead started a new one.
1551 // We should be all done, but let's just make sure our activity
1552 // is still at the top and schedule another run if something
1553 // weird happened.
1554 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001555 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001556 "Activity config changed during resume: " + next
1557 + ", new next: " + nextNext);
1558 if (nextNext != next) {
1559 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001560 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001561 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001562 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001563 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001564 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001565 return true;
1566 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001567 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001568 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001569 }
Craig Mautner58547802013-03-05 08:23:53 -08001570
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001571 try {
1572 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001573 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001574 if (a != null) {
1575 final int N = a.size();
1576 if (!next.finishing && N > 0) {
1577 if (DEBUG_RESULTS) Slog.v(
1578 TAG, "Delivering results to " + next
1579 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001580 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001581 }
1582 }
1583
1584 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001585 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001586 }
1587
1588 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001589 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001590 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001591
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001592 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001593 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001594 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001595 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
1596 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001597 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001598
Craig Mautner0eea92c2013-05-16 13:35:39 -07001599 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001600
Craig Mautnerac6f8432013-07-17 13:24:59 -07001601 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001602 } catch (Exception e) {
1603 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001604 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1605 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001606 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001607 if (lastStack != null) {
1608 lastStack.mResumedActivity = lastResumedActivity;
1609 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001610 Slog.i(TAG, "Restarting because process died: " + next);
1611 if (!next.hasBeenLaunched) {
1612 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001613 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1614 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001615 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001616 next.appToken, next.packageName, next.theme,
1617 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001618 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1619 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001620 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001621 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001622 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001623 return true;
1624 }
1625
1626 // From this point on, if something goes wrong there is no way
1627 // to recover the activity.
1628 try {
1629 next.visible = true;
1630 completeResumeLocked(next);
1631 } catch (Exception e) {
1632 // If any exception gets thrown, toss away this
1633 // activity and try the next one.
1634 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001635 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001636 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001637 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001638 return true;
1639 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001640 next.stopped = false;
1641
1642 } else {
1643 // Whoops, need to restart this activity!
1644 if (!next.hasBeenLaunched) {
1645 next.hasBeenLaunched = true;
1646 } else {
1647 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001648 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001649 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001650 mService.compatibilityInfoForPackageLocked(
1651 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001652 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001653 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001654 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001655 }
1656 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1657 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001658 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
Craig Mautnere79d42682013-04-01 19:01:53 -07001659 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001660 }
1661
Craig Mautnercf910b02013-04-23 11:23:27 -07001662 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001663 return true;
1664 }
1665
Craig Mautnerac6f8432013-07-17 13:24:59 -07001666 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001667 // If this is being moved to the top by another activity or being launched from the home
1668 // activity, set mOnTopOfHome accordingly.
Craig Mautnerd99384d2013-10-14 07:09:18 -07001669 ActivityStack lastStack = mStackSupervisor.getLastStack();
1670 final boolean fromHome = lastStack == null ? true : lastStack.isHomeStack();
Craig Mautner9c85c202013-10-04 20:11:26 -07001671 if (!isHomeStack() && (fromHome || topTask() != task)) {
1672 task.mOnTopOfHome = fromHome;
1673 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001674
Craig Mautnerac6f8432013-07-17 13:24:59 -07001675 mTaskHistory.remove(task);
1676 // Now put task at top.
1677 int stackNdx = mTaskHistory.size();
1678 if (task.userId != mCurrentUser) {
1679 // Put non-current user tasks below current user tasks.
1680 while (--stackNdx >= 0) {
1681 if (mTaskHistory.get(stackNdx).userId != mCurrentUser) {
1682 break;
1683 }
1684 }
1685 ++stackNdx;
1686 }
1687 mTaskHistory.add(stackNdx, task);
1688 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001689
Craig Mautner8849a5e2013-04-02 16:41:03 -07001690 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001691 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001692 TaskRecord rTask = r.task;
1693 final int taskId = rTask.taskId;
1694 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001695 // Last activity in task had been removed or ActivityManagerService is reusing task.
1696 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001697 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001698 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001699 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001700 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001701 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001702 if (!newTask) {
1703 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001704 boolean startIt = true;
1705 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1706 task = mTaskHistory.get(taskNdx);
1707 if (task == r.task) {
1708 // Here it is! Now, if this is not yet visible to the
1709 // user, then just add it without starting; it will
1710 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001711 if (!startIt) {
1712 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1713 + task, new RuntimeException("here").fillInStackTrace());
1714 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001715 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001716 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1717 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001718 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
1719 r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001720 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001721 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001722 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001723 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001724 return;
1725 }
1726 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001727 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001728 startIt = false;
1729 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001730 }
1731 }
1732
1733 // Place a new activity at top of stack, so it is next to interact
1734 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001735
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001736 // If we are not placing the new activity frontmost, we do not want
1737 // to deliver the onUserLeaving callback to the actual frontmost
1738 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001739 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001740 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001741 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1742 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001743 }
Craig Mautner70a86932013-02-28 22:37:44 -08001744
1745 task = r.task;
1746
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001747 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001748 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001749 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001750 task.addActivityToTop(r);
1751
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001752 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001753 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001754 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001755 // We want to show the starting preview window if we are
1756 // switching to a new task, or the next activity's process is
1757 // not currently running.
1758 boolean showStartingIcon = newTask;
1759 ProcessRecord proc = r.app;
1760 if (proc == null) {
1761 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1762 }
1763 if (proc == null || proc.thread == null) {
1764 showStartingIcon = true;
1765 }
1766 if (DEBUG_TRANSITION) Slog.v(TAG,
1767 "Prepare open transition: starting " + r);
1768 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001769 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001770 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001771 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001772 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001773 ? AppTransition.TRANSIT_TASK_OPEN
1774 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001775 mNoAnimActivities.remove(r);
1776 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001777 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001778 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001779 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001780 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001781 boolean doShow = true;
1782 if (newTask) {
1783 // Even though this activity is starting fresh, we still need
1784 // to reset it to make sure we apply affinities to move any
1785 // existing activities from other tasks in to it.
1786 // If the caller has requested that the target task be
1787 // reset, then do so.
1788 if ((r.intent.getFlags()
1789 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1790 resetTaskIfNeededLocked(r, r);
1791 doShow = topRunningNonDelayedActivityLocked(null) == r;
1792 }
1793 }
1794 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1795 // Figure out if we are transitioning from another activity that is
1796 // "has the same starting icon" as the next one. This allows the
1797 // window manager to keep the previous window it had previously
1798 // created, if it still had one.
1799 ActivityRecord prev = mResumedActivity;
1800 if (prev != null) {
1801 // We don't want to reuse the previous starting preview if:
1802 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001803 if (prev.task != r.task) {
1804 prev = null;
1805 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001806 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001807 else if (prev.nowVisible) {
1808 prev = null;
1809 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001810 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001811 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001812 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001813 mService.compatibilityInfoForPackageLocked(
1814 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001815 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001816 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001817 }
1818 } else {
1819 // If this is the first activity, don't do any fancy animations,
1820 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001821 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001822 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001823 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001824 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001825 }
1826 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001827 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001828 }
1829
1830 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001831 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001832 }
1833 }
1834
Dianne Hackbornbe707852011-11-11 14:32:10 -08001835 final void validateAppTokensLocked() {
1836 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001837 mValidateAppTokens.ensureCapacity(numActivities());
1838 final int numTasks = mTaskHistory.size();
1839 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1840 TaskRecord task = mTaskHistory.get(taskNdx);
1841 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07001842 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08001843 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001844 }
Craig Mautner000f0022013-02-26 15:04:29 -08001845 TaskGroup group = new TaskGroup();
1846 group.taskId = task.taskId;
1847 mValidateAppTokens.add(group);
1848 final int numActivities = activities.size();
1849 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1850 final ActivityRecord r = activities.get(activityNdx);
1851 group.tokens.add(r.appToken);
1852 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001853 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001854 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001855 }
1856
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001857 /**
1858 * Perform a reset of the given task, if needed as part of launching it.
1859 * Returns the new HistoryRecord at the top of the task.
1860 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001861 /**
1862 * Helper method for #resetTaskIfNeededLocked.
1863 * We are inside of the task being reset... we'll either finish this activity, push it out
1864 * for another task, or leave it as-is.
1865 * @param task The task containing the Activity (taskTop) that might be reset.
1866 * @param forceReset
1867 * @return An ActivityOptions that needs to be processed.
1868 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001869 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001870 ActivityOptions topOptions = null;
1871
1872 int replyChainEnd = -1;
1873 boolean canMoveOptions = true;
1874
1875 // We only do this for activities that are not the root of the task (since if we finish
1876 // the root, we may no longer have the task!).
1877 final ArrayList<ActivityRecord> activities = task.mActivities;
1878 final int numActivities = activities.size();
1879 for (int i = numActivities - 1; i > 0; --i ) {
1880 ActivityRecord target = activities.get(i);
1881
1882 final int flags = target.info.flags;
1883 final boolean finishOnTaskLaunch =
1884 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1885 final boolean allowTaskReparenting =
1886 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1887 final boolean clearWhenTaskReset =
1888 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1889
1890 if (!finishOnTaskLaunch
1891 && !clearWhenTaskReset
1892 && target.resultTo != null) {
1893 // If this activity is sending a reply to a previous
1894 // activity, we can't do anything with it now until
1895 // we reach the start of the reply chain.
1896 // XXX note that we are assuming the result is always
1897 // to the previous activity, which is almost always
1898 // the case but we really shouldn't count on.
1899 if (replyChainEnd < 0) {
1900 replyChainEnd = i;
1901 }
1902 } else if (!finishOnTaskLaunch
1903 && !clearWhenTaskReset
1904 && allowTaskReparenting
1905 && target.taskAffinity != null
1906 && !target.taskAffinity.equals(task.affinity)) {
1907 // If this activity has an affinity for another
1908 // task, then we need to move it out of here. We will
1909 // move it as far out of the way as possible, to the
1910 // bottom of the activity stack. This also keeps it
1911 // correctly ordered with any activities we previously
1912 // moved.
Craig Mautnerdccb7702013-09-17 15:53:34 -07001913 final ActivityRecord bottom =
1914 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
1915 mTaskHistory.get(0).mActivities.get(0) : null;
1916 if (bottom != null && target.taskAffinity != null
1917 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001918 // If the activity currently at the bottom has the
1919 // same task affinity as the one we are moving,
1920 // then merge it into the same task.
Craig Mautnerdccb7702013-09-17 15:53:34 -07001921 target.setTask(bottom.task, bottom.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001922 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07001923 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001924 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001925 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001926 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001927 target.task.affinityIntent = target.intent;
1928 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1929 + " out to new task " + target.task);
1930 }
1931
1932 final TaskRecord targetTask = target.task;
1933 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001934 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001935
Craig Mautner525f3d92013-05-07 14:01:50 -07001936 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001937 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1938 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07001939 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001940 if (p.finishing) {
1941 continue;
1942 }
1943
Craig Mautner525f3d92013-05-07 14:01:50 -07001944 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001945 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001946 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001947 topOptions = p.takeOptionsLocked();
1948 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001949 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001950 }
1951 }
1952 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1953 + task + " adding to task=" + targetTask,
1954 new RuntimeException("here").fillInStackTrace());
1955 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1956 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001957 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001958 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001959
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001960 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001961 }
1962
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001963 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001964 if (VALIDATE_TOKENS) {
1965 validateAppTokensLocked();
1966 }
1967
1968 replyChainEnd = -1;
1969 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1970 // If the activity should just be removed -- either
1971 // because it asks for it, or the task should be
1972 // cleared -- then finish it and anything that is
1973 // part of its reply chain.
1974 int end;
1975 if (clearWhenTaskReset) {
1976 // In this case, we want to finish this activity
1977 // and everything above it, so be sneaky and pretend
1978 // like these are all in the reply chain.
1979 end = numActivities - 1;
1980 } else if (replyChainEnd < 0) {
1981 end = i;
1982 } else {
1983 end = replyChainEnd;
1984 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001985 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001986 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001987 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001988 if (p.finishing) {
1989 continue;
1990 }
1991 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001992 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001993 topOptions = p.takeOptionsLocked();
1994 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001995 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001996 }
1997 }
Craig Mautner58547802013-03-05 08:23:53 -08001998 if (DEBUG_TASKS) Slog.w(TAG,
1999 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002000 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002001 end--;
2002 srcPos--;
2003 }
2004 }
2005 replyChainEnd = -1;
2006 } else {
2007 // If we were in the middle of a chain, well the
2008 // activity that started it all doesn't want anything
2009 // special, so leave it all as-is.
2010 replyChainEnd = -1;
2011 }
2012 }
2013
2014 return topOptions;
2015 }
2016
2017 /**
2018 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2019 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2020 * @param affinityTask The task we are looking for an affinity to.
2021 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2022 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2023 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2024 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002025 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002026 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002027 int replyChainEnd = -1;
2028 final int taskId = task.taskId;
2029 final String taskAffinity = task.affinity;
2030
2031 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2032 final int numActivities = activities.size();
2033 // Do not operate on the root Activity.
2034 for (int i = numActivities - 1; i > 0; --i) {
2035 ActivityRecord target = activities.get(i);
2036
2037 final int flags = target.info.flags;
2038 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2039 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2040
2041 if (target.resultTo != null) {
2042 // If this activity is sending a reply to a previous
2043 // activity, we can't do anything with it now until
2044 // we reach the start of the reply chain.
2045 // XXX note that we are assuming the result is always
2046 // to the previous activity, which is almost always
2047 // the case but we really shouldn't count on.
2048 if (replyChainEnd < 0) {
2049 replyChainEnd = i;
2050 }
2051 } else if (topTaskIsHigher
2052 && allowTaskReparenting
2053 && taskAffinity != null
2054 && taskAffinity.equals(target.taskAffinity)) {
2055 // This activity has an affinity for our task. Either remove it if we are
2056 // clearing or move it over to our task. Note that
2057 // we currently punt on the case where we are resetting a
2058 // task that is not at the top but who has activities above
2059 // with an affinity to it... this is really not a normal
2060 // case, and we will need to later pull that task to the front
2061 // and usually at that point we will do the reset and pick
2062 // up those remaining activities. (This only happens if
2063 // someone starts an activity in a new task from an activity
2064 // in a task that is not currently on top.)
2065 if (forceReset || finishOnTaskLaunch) {
2066 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2067 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2068 for (int srcPos = start; srcPos >= i; --srcPos) {
2069 final ActivityRecord p = activities.get(srcPos);
2070 if (p.finishing) {
2071 continue;
2072 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002073 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002074 }
2075 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002076 if (taskInsertionPoint < 0) {
2077 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002078
Craig Mautner77878772013-03-04 19:46:24 -08002079 }
Craig Mautner77878772013-03-04 19:46:24 -08002080
2081 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2082 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2083 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2084 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002085 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002086 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002087 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002088
Craig Mautnere3a74d52013-02-22 14:14:58 -08002089 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2090 + " to stack at " + task,
2091 new RuntimeException("here").fillInStackTrace());
2092 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2093 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002094 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002095 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002096 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002097 if (VALIDATE_TOKENS) {
2098 validateAppTokensLocked();
2099 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002100
2101 // Now we've moved it in to place... but what if this is
2102 // a singleTop activity and we have put it on top of another
2103 // instance of the same activity? Then we drop the instance
2104 // below so it remains singleTop.
2105 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2106 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002107 int targetNdx = taskActivities.indexOf(target);
2108 if (targetNdx > 0) {
2109 ActivityRecord p = taskActivities.get(targetNdx - 1);
2110 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002111 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2112 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002113 }
2114 }
2115 }
2116 }
2117
2118 replyChainEnd = -1;
2119 }
2120 }
Craig Mautner77878772013-03-04 19:46:24 -08002121 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002122 }
2123
Craig Mautner8849a5e2013-04-02 16:41:03 -07002124 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002125 ActivityRecord newActivity) {
2126 boolean forceReset =
2127 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2128 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2129 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2130 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2131 forceReset = true;
2132 }
2133 }
2134
2135 final TaskRecord task = taskTop.task;
2136
2137 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2138 * for remaining tasks. Used for later tasks to reparent to task. */
2139 boolean taskFound = false;
2140
2141 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2142 ActivityOptions topOptions = null;
2143
Craig Mautner77878772013-03-04 19:46:24 -08002144 // Preserve the location for reparenting in the new task.
2145 int reparentInsertionPoint = -1;
2146
Craig Mautnere3a74d52013-02-22 14:14:58 -08002147 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2148 final TaskRecord targetTask = mTaskHistory.get(i);
2149
2150 if (targetTask == task) {
2151 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2152 taskFound = true;
2153 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002154 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2155 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002156 }
2157 }
2158
Craig Mautner70a86932013-02-28 22:37:44 -08002159 int taskNdx = mTaskHistory.indexOf(task);
2160 do {
2161 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2162 } while (taskTop == null && taskNdx >= 0);
2163
Craig Mautnere3a74d52013-02-22 14:14:58 -08002164 if (topOptions != null) {
2165 // If we got some ActivityOptions from an activity on top that
2166 // was removed from the task, propagate them to the new real top.
2167 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002168 taskTop.updateOptionsLocked(topOptions);
2169 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002170 topOptions.abort();
2171 }
2172 }
2173
2174 return taskTop;
2175 }
2176
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002177 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2178 String resultWho, int requestCode, int resultCode, Intent data) {
2179
2180 if (callingUid > 0) {
2181 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002182 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002183 }
2184
2185 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2186 + " : who=" + resultWho + " req=" + requestCode
2187 + " res=" + resultCode + " data=" + data);
2188 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2189 try {
2190 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2191 list.add(new ResultInfo(resultWho, requestCode,
2192 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002193 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002194 return;
2195 } catch (Exception e) {
2196 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2197 }
2198 }
2199
2200 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2201 }
2202
Craig Mautner04f0b702013-10-22 12:31:01 -07002203 private void adjustFocusedActivityLocked(ActivityRecord r) {
2204 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2205 ActivityRecord next = topRunningActivityLocked(null);
2206 if (next != r) {
2207 final TaskRecord task = r.task;
2208 if (r.frontOfTask && task == topTask() && task.mOnTopOfHome) {
2209 mStackSupervisor.moveHomeToTop();
2210 }
2211 }
2212 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
2213 }
2214 }
2215
Craig Mautnerf3333272013-04-22 10:55:53 -07002216 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002217 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2218 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2219 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2220 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002221 if (!mService.mSleeping) {
2222 if (DEBUG_STATES) {
2223 Slog.d(TAG, "no-history finish of " + r);
2224 }
2225 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002226 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002227 } else {
2228 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2229 + " on stop because we're just sleeping");
2230 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002231 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002232 }
2233
2234 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002235 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002236 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002237 try {
2238 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002239 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2240 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002241 r.state = ActivityState.STOPPING;
2242 if (DEBUG_VISBILITY) Slog.v(
2243 TAG, "Stopping visible=" + r.visible + " for " + r);
2244 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002245 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002246 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002247 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002248 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002249 r.setSleeping(true);
2250 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002251 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002252 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002253 } catch (Exception e) {
2254 // Maybe just ignore exceptions here... if the process
2255 // has crashed, our death notification will clean things
2256 // up.
2257 Slog.w(TAG, "Exception thrown during pause", e);
2258 // Just in case, assume it to be stopped.
2259 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002260 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002261 r.state = ActivityState.STOPPED;
2262 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002263 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002264 }
2265 }
2266 }
2267 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002268
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002269 /**
2270 * @return Returns true if the activity is being finished, false if for
2271 * some reason it is being left as-is.
2272 */
2273 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002274 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002275 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002276 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002277 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002278 + ", result=" + resultCode + ", data=" + resultData
2279 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002280 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002281 return false;
2282 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002283
Craig Mautnerd44711d2013-02-23 11:24:36 -08002284 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002285 return true;
2286 }
2287
Craig Mautnerd2328952013-03-05 12:46:26 -08002288 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002289 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2290 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2291 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2292 ActivityRecord r = activities.get(activityNdx);
2293 if (r.resultTo == self && r.requestCode == requestCode) {
2294 if ((r.resultWho == null && resultWho == null) ||
2295 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2296 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2297 false);
2298 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002299 }
2300 }
2301 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002302 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002303 }
2304
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002305 final void finishTopRunningActivityLocked(ProcessRecord app) {
2306 ActivityRecord r = topRunningActivityLocked(null);
2307 if (r != null && r.app == app) {
2308 // If the top running activity is from this crashing
2309 // process, then terminate it to avoid getting in a loop.
2310 Slog.w(TAG, " Force finishing activity "
2311 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002312 int taskNdx = mTaskHistory.indexOf(r.task);
2313 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002314 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002315 // Also terminate any activities below it that aren't yet
2316 // stopped, to avoid a situation where one will get
2317 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002318 --activityNdx;
2319 if (activityNdx < 0) {
2320 do {
2321 --taskNdx;
2322 if (taskNdx < 0) {
2323 break;
2324 }
2325 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2326 } while (activityNdx < 0);
2327 }
2328 if (activityNdx >= 0) {
2329 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002330 if (r.state == ActivityState.RESUMED
2331 || r.state == ActivityState.PAUSING
2332 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002333 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002334 Slog.w(TAG, " Force finishing activity "
2335 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002336 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002337 }
2338 }
2339 }
2340 }
2341 }
2342
Craig Mautnerd2328952013-03-05 12:46:26 -08002343 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002344 ArrayList<ActivityRecord> activities = r.task.mActivities;
2345 for (int index = activities.indexOf(r); index >= 0; --index) {
2346 ActivityRecord cur = activities.get(index);
2347 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002348 break;
2349 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002350 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002351 }
2352 return true;
2353 }
2354
Dianne Hackborn5c607432012-02-28 14:44:19 -08002355 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2356 // send the result
2357 ActivityRecord resultTo = r.resultTo;
2358 if (resultTo != null) {
2359 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2360 + " who=" + r.resultWho + " req=" + r.requestCode
2361 + " res=" + resultCode + " data=" + resultData);
2362 if (r.info.applicationInfo.uid > 0) {
2363 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2364 resultTo.packageName, resultData,
2365 resultTo.getUriPermissionsLocked());
2366 }
2367 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2368 resultData);
2369 r.resultTo = null;
2370 }
2371 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2372
2373 // Make sure this HistoryRecord is not holding on to other resources,
2374 // because clients have remote IPC references to this object so we
2375 // can't assume that will go away and want to avoid circular IPC refs.
2376 r.results = null;
2377 r.pendingResults = null;
2378 r.newIntents = null;
2379 r.icicle = null;
2380 }
2381
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002382 /**
2383 * @return Returns true if this activity has been removed from the history
2384 * list, or false if it is still in the list and will be removed later.
2385 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002386 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2387 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002388 if (r.finishing) {
2389 Slog.w(TAG, "Duplicate finish request for " + r);
2390 return false;
2391 }
2392
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002393 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002394 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002395 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002396 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002397 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2398 final int index = activities.indexOf(r);
2399 if (index < (activities.size() - 1)) {
2400 ActivityRecord next = activities.get(index+1);
2401 if (r.frontOfTask) {
2402 // The next activity is now the front of the task.
2403 next.frontOfTask = true;
2404 }
2405 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2406 // If the caller asked that this activity (and all above it)
2407 // be cleared when the task is reset, don't lose that information,
2408 // but propagate it up to the next activity.
2409 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002410 }
2411 }
2412
2413 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002414
2415 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002416
Dianne Hackborn5c607432012-02-28 14:44:19 -08002417 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002418
Craig Mautnerc8143c62013-09-03 12:15:57 -07002419 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002420 // There are clients waiting to receive thumbnails so, in case
2421 // this is an activity that someone is waiting for, add it
2422 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002423 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002424 }
2425
Craig Mautnerde4ef022013-04-07 19:01:33 -07002426 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002427 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002428 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002429 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002430 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002431 ? AppTransition.TRANSIT_TASK_CLOSE
2432 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002433
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002434 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002435 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002436
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002437 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002438 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2439 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2440 startPausingLocked(false, false);
2441 }
2442
2443 } else if (r.state != ActivityState.PAUSING) {
2444 // If the activity is PAUSING, we will complete the finish once
2445 // it is done pausing; else we can just directly finish it here.
2446 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002447 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002448 } else {
2449 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2450 }
2451
2452 return false;
2453 }
2454
Craig Mautnerf3333272013-04-22 10:55:53 -07002455 static final int FINISH_IMMEDIATELY = 0;
2456 static final int FINISH_AFTER_PAUSE = 1;
2457 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002458
Craig Mautnerf3333272013-04-22 10:55:53 -07002459 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002460 // First things first: if this activity is currently visible,
2461 // and the resumed activity is not yet visible, then hold off on
2462 // finishing until the resumed one becomes visible.
2463 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002464 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2465 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002466 if (mStackSupervisor.mStoppingActivities.size() > 3
2467 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002468 // If we already have a few activities waiting to stop,
2469 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002470 // them out. Or if r is the last of activity of the last task the stack
2471 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002472 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002473 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002474 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002475 }
2476 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002477 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2478 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002479 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002480 if (oomAdj) {
2481 mService.updateOomAdjLocked();
2482 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002483 return r;
2484 }
2485
2486 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002487 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002488 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002489 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002490 if (mResumedActivity == r) {
2491 mResumedActivity = null;
2492 }
2493 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002494 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002495 r.state = ActivityState.FINISHING;
2496
2497 if (mode == FINISH_IMMEDIATELY
2498 || prevState == ActivityState.STOPPED
2499 || prevState == ActivityState.INITIALIZING) {
2500 // If this activity is already stopped, we can just finish
2501 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002502 boolean activityRemoved = destroyActivityLocked(r, true,
2503 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002504 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002505 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002506 }
2507 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002508 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002509
2510 // Need to go through the full pause cycle to get this
2511 // activity into the stopped state and then finish it.
2512 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002513 mStackSupervisor.mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002514 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002515 return r;
2516 }
2517
Craig Mautnerd2328952013-03-05 12:46:26 -08002518 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002519 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002520 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002521 final TaskRecord task = srec.task;
2522 final ArrayList<ActivityRecord> activities = task.mActivities;
2523 final int start = activities.indexOf(srec);
2524 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002525 return false;
2526 }
2527 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002528 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002529 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002530 final ComponentName dest = destIntent.getComponent();
2531 if (start > 0 && dest != null) {
2532 for (int i = finishTo; i >= 0; i--) {
2533 ActivityRecord r = activities.get(i);
2534 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002535 r.info.name.equals(dest.getClassName())) {
2536 finishTo = i;
2537 parent = r;
2538 foundParentInTask = true;
2539 break;
2540 }
2541 }
2542 }
2543
2544 IActivityController controller = mService.mController;
2545 if (controller != null) {
2546 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2547 if (next != null) {
2548 // ask watcher if this is allowed
2549 boolean resumeOK = true;
2550 try {
2551 resumeOK = controller.activityResuming(next.packageName);
2552 } catch (RemoteException e) {
2553 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002554 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002555 }
2556
2557 if (!resumeOK) {
2558 return false;
2559 }
2560 }
2561 }
2562 final long origId = Binder.clearCallingIdentity();
2563 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002564 ActivityRecord r = activities.get(i);
2565 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002566 // Only return the supplied result for the first activity finished
2567 resultCode = Activity.RESULT_CANCELED;
2568 resultData = null;
2569 }
2570
2571 if (parent != null && foundParentInTask) {
2572 final int parentLaunchMode = parent.info.launchMode;
2573 final int destIntentFlags = destIntent.getFlags();
2574 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2575 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2576 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2577 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2578 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2579 } else {
2580 try {
2581 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2582 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002583 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002584 null, aInfo, parent.appToken, null,
2585 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2586 0, null, true, null);
2587 foundParentInTask = res == ActivityManager.START_SUCCESS;
2588 } catch (RemoteException e) {
2589 foundParentInTask = false;
2590 }
2591 requestFinishActivityLocked(parent.appToken, resultCode,
2592 resultData, "navigate-up", true);
2593 }
2594 }
2595 Binder.restoreCallingIdentity(origId);
2596 return foundParentInTask;
2597 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002598 /**
2599 * Perform the common clean-up of an activity record. This is called both
2600 * as part of destroyActivityLocked() (when destroying the client-side
2601 * representation) and cleaning things up as a result of its hosting
2602 * processing going away, in which case there is no remaining client-side
2603 * state to destroy so only the cleanup here is needed.
2604 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002605 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2606 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002607 if (mResumedActivity == r) {
2608 mResumedActivity = null;
2609 }
2610 if (mService.mFocusedActivity == r) {
2611 mService.mFocusedActivity = null;
2612 }
2613
2614 r.configDestroy = false;
2615 r.frozenBeforeDestroy = false;
2616
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002617 if (setState) {
2618 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2619 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002620 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002621 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002622 }
2623
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002624 // Make sure this record is no longer in the pending finishes list.
2625 // This could happen, for example, if we are trimming activities
2626 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002627 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002628 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002629
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002630 // Remove any pending results.
2631 if (r.finishing && r.pendingResults != null) {
2632 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2633 PendingIntentRecord rec = apr.get();
2634 if (rec != null) {
2635 mService.cancelIntentSenderLocked(rec, false);
2636 }
2637 }
2638 r.pendingResults = null;
2639 }
2640
2641 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002642 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002643 }
2644
Craig Mautnerc8143c62013-09-03 12:15:57 -07002645 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002646 // There are clients waiting to receive thumbnails so, in case
2647 // this is an activity that someone is waiting for, add it
2648 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002649 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002650 }
2651
2652 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002653 removeTimeoutsForActivityLocked(r);
2654 }
2655
2656 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002657 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002658 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002659 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002660 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002661 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002662 }
2663
Dianne Hackborn5c607432012-02-28 14:44:19 -08002664 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002665 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2666 r.makeFinishing();
2667 if (DEBUG_ADD_REMOVE) {
2668 RuntimeException here = new RuntimeException("here");
2669 here.fillInStackTrace();
2670 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002671 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002672 final TaskRecord task = r.task;
2673 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002674 if (DEBUG_STACK) Slog.i(TAG,
2675 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnere1db0dd2013-10-13 18:15:00 -07002676 if (mStackSupervisor.isFrontStack(this) && task == topTask() && task.mOnTopOfHome) {
Craig Mautner8e569572013-10-11 17:36:59 -07002677 mStackSupervisor.moveHomeToTop();
2678 }
Craig Mautner2219b752013-10-12 11:26:08 -07002679 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002680 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002681 r.takeFromHistory();
2682 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002683 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002684 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002685 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002686 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002687 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002688 if (VALIDATE_TOKENS) {
2689 validateAppTokensLocked();
2690 }
2691 cleanUpActivityServicesLocked(r);
2692 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002693 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002694
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002695 /**
2696 * Perform clean-up of service connections in an activity record.
2697 */
2698 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2699 // Throw away any services that have been bound by this activity.
2700 if (r.connections != null) {
2701 Iterator<ConnectionRecord> it = r.connections.iterator();
2702 while (it.hasNext()) {
2703 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002704 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002705 }
2706 r.connections = null;
2707 }
2708 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002709
2710 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2711 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2712 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2713 mHandler.sendMessage(msg);
2714 }
2715
Dianne Hackborn28695e02011-11-02 21:59:51 -07002716 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002717 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002718 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002719 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2720 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2721 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2722 final ActivityRecord r = activities.get(activityNdx);
2723 if (r.finishing) {
2724 continue;
2725 }
2726 if (r.fullscreen) {
2727 lastIsOpaque = true;
2728 }
2729 if (owner != null && r.app != owner) {
2730 continue;
2731 }
2732 if (!lastIsOpaque) {
2733 continue;
2734 }
2735 // We can destroy this one if we have its icicle saved and
2736 // it is not in the process of pausing/stopping/finishing.
2737 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2738 && r.haveState && !r.visible && r.stopped
2739 && r.state != ActivityState.DESTROYING
2740 && r.state != ActivityState.DESTROYED) {
2741 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2742 + " resumed=" + mResumedActivity
2743 + " pausing=" + mPausingActivity);
2744 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2745 activityRemoved = true;
2746 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002747 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002748 }
2749 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002750 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002751 mStackSupervisor.resumeTopActivitiesLocked();
2752
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002753 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002754 }
2755
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002756 /**
2757 * Destroy the current CLIENT SIDE instance of an activity. This may be
2758 * called both when actually finishing an activity, or when performing
2759 * a configuration switch where we destroy the current client-side object
2760 * but then create a new client-side object for this same HistoryRecord.
2761 */
2762 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002763 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002764 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002765 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002766 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2767 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002768 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002769 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002770
2771 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002772
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002773 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002774
2775 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002776
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002777 if (hadApp) {
2778 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002779 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002780 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2781 mService.mHeavyWeightProcess = null;
2782 mService.mHandler.sendEmptyMessage(
2783 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2784 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07002785 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07002786 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07002787 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002788 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002789 }
2790 }
2791
2792 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002793
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002794 try {
2795 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002796 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002797 r.configChangeFlags);
2798 } catch (Exception e) {
2799 // We can just ignore exceptions here... if the process
2800 // has crashed, our death notification will clean things
2801 // up.
2802 //Slog.w(TAG, "Exception thrown during finish", e);
2803 if (r.finishing) {
2804 removeActivityFromHistoryLocked(r);
2805 removedFromHistory = true;
2806 skipDestroy = true;
2807 }
2808 }
2809
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002810 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002811
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002812 // If the activity is finishing, we need to wait on removing it
2813 // from the list to give it a chance to do its cleanup. During
2814 // that time it may make calls back with its token so we need to
2815 // be able to find it on the list and so we don't want to remove
2816 // it from the list yet. Otherwise, we can just immediately put
2817 // it in the destroyed state since we are not removing it from the
2818 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002819 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002820 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2821 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002822 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002823 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002824 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2825 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002826 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002827 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002828 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002829 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002830 }
2831 } else {
2832 // remove this record from the history.
2833 if (r.finishing) {
2834 removeActivityFromHistoryLocked(r);
2835 removedFromHistory = true;
2836 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002837 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002838 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002839 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002840 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002841 }
2842 }
2843
2844 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002845
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002846 if (!mLRUActivities.remove(r) && hadApp) {
2847 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2848 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002849
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002850 return removedFromHistory;
2851 }
2852
Craig Mautnerd2328952013-03-05 12:46:26 -08002853 final void activityDestroyedLocked(IBinder token) {
2854 final long origId = Binder.clearCallingIdentity();
2855 try {
2856 ActivityRecord r = ActivityRecord.forToken(token);
2857 if (r != null) {
2858 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002859 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002860
2861 if (isInStackLocked(token) != null) {
2862 if (r.state == ActivityState.DESTROYING) {
2863 cleanUpActivityLocked(r, true, false);
2864 removeActivityFromHistoryLocked(r);
2865 }
2866 }
Craig Mautner05d29032013-05-03 13:40:13 -07002867 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002868 } finally {
2869 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002870 }
2871 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002872
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002873 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2874 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002875 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002876 if (DEBUG_CLEANUP) Slog.v(
2877 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002878 + " with " + i + " entries");
2879 while (i > 0) {
2880 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002881 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002882 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002883 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002884 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002885 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002886 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002887 }
2888 }
2889 }
2890
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002891 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2892 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002893 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2894 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002895 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2896 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002897 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002898 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002899 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2900 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002901
2902 boolean hasVisibleActivities = false;
2903
2904 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002905 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002906 if (DEBUG_CLEANUP) Slog.v(
2907 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002908 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2909 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2910 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2911 final ActivityRecord r = activities.get(activityNdx);
2912 --i;
2913 if (DEBUG_CLEANUP) Slog.v(
2914 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2915 if (r.app == app) {
2916 boolean remove;
2917 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2918 // Don't currently have state for the activity, or
2919 // it is finishing -- always remove it.
2920 remove = true;
2921 } else if (r.launchCount > 2 &&
2922 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2923 // We have launched this activity too many times since it was
2924 // able to run, so give up and remove it.
2925 remove = true;
2926 } else {
2927 // The process may be gone, but the activity lives on!
2928 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002929 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002930 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002931 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002932 RuntimeException here = new RuntimeException("here");
2933 here.fillInStackTrace();
2934 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2935 + ": haveState=" + r.haveState
2936 + " stateNotNeeded=" + r.stateNotNeeded
2937 + " finishing=" + r.finishing
2938 + " state=" + r.state, here);
2939 }
2940 if (!r.finishing) {
2941 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2942 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2943 r.userId, System.identityHashCode(r),
2944 r.task.taskId, r.shortComponentName,
2945 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07002946 if (r.state == ActivityState.RESUMED) {
2947 mService.updateUsageStats(r, false);
2948 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002949 }
2950 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002951
Craig Mautner0247fc82013-02-28 14:32:06 -08002952 } else {
2953 // We have the current state for this activity, so
2954 // it can be restarted later when needed.
2955 if (localLOGV) Slog.v(
2956 TAG, "Keeping entry, setting app to null");
2957 if (r.visible) {
2958 hasVisibleActivities = true;
2959 }
2960 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2961 + r);
2962 r.app = null;
2963 r.nowVisible = false;
2964 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002965 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08002966 "App died, clearing saved state of " + r);
2967 r.icicle = null;
2968 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002969 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002970
Craig Mautnerd2328952013-03-05 12:46:26 -08002971 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002972 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002973 }
2974 }
2975
2976 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002977 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002978
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002979 final void updateTransitLocked(int transit, Bundle options) {
2980 if (options != null) {
2981 ActivityRecord r = topRunningActivityLocked(null);
2982 if (r != null && r.state != ActivityState.RESUMED) {
2983 r.updateOptionsLocked(options);
2984 } else {
2985 ActivityOptions.abort(options);
2986 }
2987 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002988 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002989 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002990
Craig Mautnera82aa092013-09-13 15:34:08 -07002991 void moveHomeTaskToTop() {
2992 final int top = mTaskHistory.size() - 1;
2993 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
2994 final TaskRecord task = mTaskHistory.get(taskNdx);
2995 if (task.isHomeTask()) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002996 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG, "moveHomeTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07002997 mTaskHistory.remove(taskNdx);
2998 mTaskHistory.add(top, task);
2999 mWindowManager.moveTaskToTop(task.taskId);
3000 return;
3001 }
3002 }
3003 }
3004
Craig Mautnercae015f2013-02-08 14:31:27 -08003005 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003006 final TaskRecord task = taskForIdLocked(taskId);
3007 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003008 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003009 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003010 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003011 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
3012 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07003013 // we'll just indicate that this task returns to the home task.
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003014 task.mOnTopOfHome = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08003015 }
3016 moveTaskToFrontLocked(task, null, options);
3017 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003018 }
3019 return false;
3020 }
3021
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003022 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003023 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003024
Craig Mautner11bf9a52013-02-19 14:08:51 -08003025 final int numTasks = mTaskHistory.size();
3026 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003027 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003028 // nothing to do!
3029 if (reason != null &&
3030 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3031 ActivityOptions.abort(options);
3032 } else {
3033 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3034 }
3035 return;
3036 }
3037
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003038 mStackSupervisor.moveHomeStack(isHomeStack());
3039
Craig Mautner11bf9a52013-02-19 14:08:51 -08003040 // Shift all activities with this task up to the top
3041 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003042 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003043
3044 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003045 if (reason != null &&
3046 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003047 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003048 ActivityRecord r = topRunningActivityLocked(null);
3049 if (r != null) {
3050 mNoAnimActivities.add(r);
3051 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003052 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003053 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003054 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003055 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003056
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003057 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003058
Craig Mautner05d29032013-05-03 13:40:13 -07003059 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003060 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003061
3062 if (VALIDATE_TOKENS) {
3063 validateAppTokensLocked();
3064 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003065 }
3066
3067 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003068 * Worker method for rearranging history stack. Implements the function of moving all
3069 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003070 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003071 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003072 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3073 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003074 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003075 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003076 * @return Returns true if the move completed, false if not.
3077 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003078 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
3079 Slog.i(TAG, "moveTaskToBack: " + taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003080
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003081 // If we have a watcher, preflight the move before committing to it. First check
3082 // for *other* available tasks, but if none are available, then try again allowing the
3083 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003084 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003085 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003086 if (next == null) {
3087 next = topRunningActivityLocked(null, 0);
3088 }
3089 if (next != null) {
3090 // ask watcher if this is allowed
3091 boolean moveOK = true;
3092 try {
3093 moveOK = mService.mController.activityResuming(next.packageName);
3094 } catch (RemoteException e) {
3095 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003096 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003097 }
3098 if (!moveOK) {
3099 return false;
3100 }
3101 }
3102 }
3103
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003104 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003105 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003106
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003107 final TaskRecord tr = taskForIdLocked(taskId);
Craig Mautnerd2328952013-03-05 12:46:26 -08003108 if (tr == null) {
3109 return false;
3110 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003111
Craig Mautner11bf9a52013-02-19 14:08:51 -08003112 mTaskHistory.remove(tr);
3113 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003114
Craig Mautnerc8143c62013-09-03 12:15:57 -07003115 // There is an assumption that moving a task to the back moves it behind the home activity.
3116 // We make sure here that some activity in the stack will launch home.
3117 ActivityRecord lastActivity = null;
3118 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003119 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3120 final TaskRecord task = mTaskHistory.get(taskNdx);
3121 if (task.mOnTopOfHome) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003122 break;
3123 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003124 if (taskNdx == 1) {
3125 // Set the last task before tr to go to home.
3126 task.mOnTopOfHome = true;
3127 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003128 }
3129
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003130 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003131 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3132 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003133 ActivityRecord r = topRunningActivityLocked(null);
3134 if (r != null) {
3135 mNoAnimActivities.add(r);
3136 }
3137 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003138 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003139 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003140 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003141
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003142 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003143 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003144 }
3145
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003146 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
3147 if (task == tr && task.mOnTopOfHome || numTasks <= 1) {
3148 task.mOnTopOfHome = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003149 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003150 }
3151
Craig Mautner05d29032013-05-03 13:40:13 -07003152 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003153 return true;
3154 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003155
Craig Mautner8849a5e2013-04-02 16:41:03 -07003156 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003157 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003158 final Uri data = r.intent.getData();
3159 final String strData = data != null ? data.toSafeString() : null;
3160
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003161 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003162 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003163 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003164 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003165 }
3166
3167 /**
3168 * Make sure the given activity matches the current configuration. Returns
3169 * false if the activity had to be destroyed. Returns true if the
3170 * configuration is the same, or the activity will remain running as-is
3171 * for whatever reason. Ensures the HistoryRecord is updated with the
3172 * correct configuration and all other bookkeeping is handled.
3173 */
3174 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3175 int globalChanges) {
3176 if (mConfigWillChange) {
3177 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3178 "Skipping config check (will change): " + r);
3179 return true;
3180 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003181
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003182 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3183 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003184
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003185 // Short circuit: if the two configurations are the exact same
3186 // object (the common case), then there is nothing to do.
3187 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003188 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003189 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3190 "Configuration unchanged in " + r);
3191 return true;
3192 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003193
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003194 // We don't worry about activities that are finishing.
3195 if (r.finishing) {
3196 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3197 "Configuration doesn't matter in finishing " + r);
3198 r.stopFreezingScreenLocked(false);
3199 return true;
3200 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003201
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003202 // Okay we now are going to make this activity have the new config.
3203 // But then we need to figure out how it needs to deal with that.
3204 Configuration oldConfig = r.configuration;
3205 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003206
3207 // Determine what has changed. May be nothing, if this is a config
3208 // that has come back from the app after going idle. In that case
3209 // we just want to leave the official config object now in the
3210 // activity and do nothing else.
3211 final int changes = oldConfig.diff(newConfig);
3212 if (changes == 0 && !r.forceNewConfig) {
3213 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3214 "Configuration no differences in " + r);
3215 return true;
3216 }
3217
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003218 // If the activity isn't currently running, just leave the new
3219 // configuration and it will pick that up next time it starts.
3220 if (r.app == null || r.app.thread == null) {
3221 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3222 "Configuration doesn't matter not running " + r);
3223 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003224 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003225 return true;
3226 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003227
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003228 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003229 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3230 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3231 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003232 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003233 + ", newConfig=" + newConfig);
3234 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003235 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003236 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3237 r.configChangeFlags |= changes;
3238 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003239 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003240 if (r.app == null || r.app.thread == null) {
3241 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003242 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003243 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003244 } else if (r.state == ActivityState.PAUSING) {
3245 // A little annoying: we are waiting for this activity to
3246 // finish pausing. Let's not do anything now, but just
3247 // flag that it needs to be restarted when done pausing.
3248 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003249 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003250 r.configDestroy = true;
3251 return true;
3252 } else if (r.state == ActivityState.RESUMED) {
3253 // Try to optimize this case: the configuration is changing
3254 // and we need to restart the top, resumed activity.
3255 // Instead of doing the normal handshaking, just say
3256 // "restart!".
3257 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003258 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003259 relaunchActivityLocked(r, r.configChangeFlags, true);
3260 r.configChangeFlags = 0;
3261 } else {
3262 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003263 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003264 relaunchActivityLocked(r, r.configChangeFlags, false);
3265 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003266 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003267
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003268 // All done... tell the caller we weren't able to keep this
3269 // activity around.
3270 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003271 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003272
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003273 // Default case: the activity can handle this new configuration, so
3274 // hand it over. Note that we don't need to give it the new
3275 // configuration, since we always send configuration changes to all
3276 // process when they happen so it can just use whatever configuration
3277 // it last got.
3278 if (r.app != null && r.app.thread != null) {
3279 try {
3280 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003281 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003282 } catch (RemoteException e) {
3283 // If process died, whatever.
3284 }
3285 }
3286 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003287
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003288 return true;
3289 }
3290
Craig Mautnerc8143c62013-09-03 12:15:57 -07003291 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003292 int changes, boolean andResume) {
3293 List<ResultInfo> results = null;
3294 List<Intent> newIntents = null;
3295 if (andResume) {
3296 results = r.results;
3297 newIntents = r.newIntents;
3298 }
3299 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3300 + " with results=" + results + " newIntents=" + newIntents
3301 + " andResume=" + andResume);
3302 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003303 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003304 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003305
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003306 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003307
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003308 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003309 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3310 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3311 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003312 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003313 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003314 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003315 // Note: don't need to call pauseIfSleepingLocked() here, because
3316 // the caller will only pass in 'andResume' if this activity is
3317 // currently resumed, which implies we aren't sleeping.
3318 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003319 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003320 }
3321
3322 if (andResume) {
3323 r.results = null;
3324 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003325 r.state = ActivityState.RESUMED;
3326 } else {
3327 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3328 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003329 }
3330
3331 return true;
3332 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003333
3334 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003335 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3336 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3337 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3338 final ActivityRecord r = activities.get(activityNdx);
3339 if (r.appToken == token) {
3340 return true;
3341 }
3342 if (r.fullscreen && !r.finishing) {
3343 return false;
3344 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003345 }
3346 }
3347 return true;
3348 }
3349
3350 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003351 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3352 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3353 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3354 final ActivityRecord r = activities.get(activityNdx);
3355 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003356 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003357 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003358 }
3359 }
3360 }
3361
3362 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3363 boolean didSomething = false;
3364 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003365 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3366 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3367 int numActivities = activities.size();
3368 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3369 ActivityRecord r = activities.get(activityNdx);
3370 final boolean samePackage = r.packageName.equals(name)
3371 || (name == null && r.userId == userId);
3372 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3373 && (samePackage || r.task == lastTask)
3374 && (r.app == null || evenPersistent || !r.app.persistent)) {
3375 if (!doit) {
3376 if (r.finishing) {
3377 // If this activity is just finishing, then it is not
3378 // interesting as far as something to stop.
3379 continue;
3380 }
3381 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003382 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003383 didSomething = true;
3384 Slog.i(TAG, " Force finishing activity " + r);
3385 if (samePackage) {
3386 if (r.app != null) {
3387 r.app.removed = true;
3388 }
3389 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003390 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003391 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003392 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3393 true)) {
3394 // r has been deleted from mActivities, accommodate.
3395 --numActivities;
3396 --activityNdx;
3397 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003398 }
3399 }
3400 }
3401 return didSomething;
3402 }
3403
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003404 ActivityRecord getTasksLocked(IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003405 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003406 ActivityRecord topRecord = null;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003407 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003408 final TaskRecord task = mTaskHistory.get(taskNdx);
3409 ActivityRecord r = null;
3410 ActivityRecord top = null;
3411 int numActivities = 0;
3412 int numRunning = 0;
3413 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003414 if (activities.isEmpty()) {
3415 continue;
3416 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003417 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3418 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003419
Craig Mautneraab647e2013-02-28 16:31:36 -08003420 // Initialize state for next task if needed.
3421 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3422 top = r;
3423 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003424 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003425
3426 // Add 'r' into the current task.
3427 numActivities++;
3428 if (r.app != null && r.app.thread != null) {
3429 numRunning++;
3430 }
3431
3432 if (localLOGV) Slog.v(
3433 TAG, r.intent.getComponent().flattenToShortString()
3434 + ": task=" + r.task);
3435 }
3436
3437 RunningTaskInfo ci = new RunningTaskInfo();
3438 ci.id = task.taskId;
3439 ci.baseActivity = r.intent.getComponent();
3440 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003441 ci.lastActiveTime = task.lastActiveTime;
3442
Craig Mautneraab647e2013-02-28 16:31:36 -08003443 if (top.thumbHolder != null) {
3444 ci.description = top.thumbHolder.lastDescription;
3445 }
3446 ci.numActivities = numActivities;
3447 ci.numRunning = numRunning;
3448 //System.out.println(
3449 // "#" + maxNum + ": " + " descr=" + ci.description);
3450 if (receiver != null) {
3451 if (localLOGV) Slog.v(
3452 TAG, "State=" + top.state + "Idle=" + top.idle
3453 + " app=" + top.app
3454 + " thr=" + (top.app != null ? top.app.thread : null));
3455 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3456 if (top.idle && top.app != null && top.app.thread != null) {
3457 topRecord = top;
3458 } else {
3459 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003460 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003461 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003462 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003463 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003464 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003465 }
3466 return topRecord;
3467 }
3468
3469 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003470 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003471 if (DEBUG_SWITCH) Slog.d(
3472 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003473 if (top >= 0) {
3474 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3475 int activityTop = activities.size() - 1;
3476 if (activityTop > 0) {
3477 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3478 "unhandled-back", true);
3479 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003480 }
3481 }
3482
Craig Mautner6b74cb52013-09-27 17:02:21 -07003483 /**
3484 * Reset local parameters because an app's activity died.
3485 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003486 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003487 */
3488 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003489 if (mPausingActivity != null && mPausingActivity.app == app) {
3490 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3491 "App died while pausing: " + mPausingActivity);
3492 mPausingActivity = null;
3493 }
3494 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3495 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003496 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003497 }
3498
Craig Mautner19091252013-10-05 00:03:53 -07003499 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003500 }
3501
Craig Mautnercae015f2013-02-08 14:31:27 -08003502 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003503 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3504 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3505 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3506 final ActivityRecord r = activities.get(activityNdx);
3507 if (r.app == app) {
3508 Slog.w(TAG, " Force finishing activity "
3509 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003510 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003511 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003512 }
3513 }
3514 }
3515
Dianne Hackborn390517b2013-05-30 15:03:32 -07003516 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003517 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003518 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003519 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3520 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003521 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3522 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003523 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003524 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003525 if (printed) {
3526 header = null;
3527 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003528 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003529 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003530 }
3531
3532 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3533 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3534
3535 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003536 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3537 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003538 }
3539 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003540 final int top = mTaskHistory.size() - 1;
3541 if (top >= 0) {
3542 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3543 int listTop = list.size() - 1;
3544 if (listTop >= 0) {
3545 activities.add(list.get(listTop));
3546 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003547 }
3548 } else {
3549 ItemMatcher matcher = new ItemMatcher();
3550 matcher.build(name);
3551
Craig Mautneraab647e2013-02-28 16:31:36 -08003552 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3553 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3554 if (matcher.match(r1, r1.intent.getComponent())) {
3555 activities.add(r1);
3556 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003557 }
3558 }
3559 }
3560
3561 return activities;
3562 }
3563
3564 ActivityRecord restartPackage(String packageName) {
3565 ActivityRecord starting = topRunningActivityLocked(null);
3566
3567 // All activities that came from the package must be
3568 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003569 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3570 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3571 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3572 final ActivityRecord a = activities.get(activityNdx);
3573 if (a.info.packageName.equals(packageName)) {
3574 a.forceNewConfig = true;
3575 if (starting != null && a == starting && a.visible) {
3576 a.startFreezingScreenLocked(starting.app,
3577 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3578 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003579 }
3580 }
3581 }
3582
3583 return starting;
3584 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003585
Craig Mautnerde4ef022013-04-07 19:01:33 -07003586 boolean removeTask(TaskRecord task) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003587 final int taskNdx = mTaskHistory.indexOf(task);
3588 final int topTaskNdx = mTaskHistory.size() - 1;
3589 if (task.mOnTopOfHome && taskNdx < topTaskNdx) {
3590 mTaskHistory.get(taskNdx + 1).mOnTopOfHome = true;
3591 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003592 mTaskHistory.remove(task);
Craig Mautnerc8143c62013-09-03 12:15:57 -07003593 return mTaskHistory.isEmpty();
Craig Mautner0247fc82013-02-28 14:32:06 -08003594 }
3595
Craig Mautnerde4ef022013-04-07 19:01:33 -07003596 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003597 TaskRecord task = new TaskRecord(taskId, info, intent);
3598 addTask(task, toTop);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003599 return task;
3600 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003601
3602 ArrayList<TaskRecord> getAllTasks() {
3603 return new ArrayList<TaskRecord>(mTaskHistory);
3604 }
3605
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003606 void addTask(final TaskRecord task, final boolean toTop) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003607 task.stack = this;
3608 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003609 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003610 } else {
3611 mTaskHistory.add(0, task);
3612 }
3613 }
3614
3615 public int getStackId() {
3616 return mStackId;
3617 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003618
3619 @Override
3620 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07003621 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
3622 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07003623 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003624}