blob: ed047242fb14d63e9396c7218ba31441d6992414 [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();
568 for (int i = 0; i < index; ++i) {
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;
576 }
577 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700578 if (VALIDATE_TOKENS) {
579 validateAppTokensLocked();
580 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700581 }
582
Craig Mautner2420ead2013-04-01 17:13:20 -0700583 void minimalResumeActivityLocked(ActivityRecord r) {
584 r.state = ActivityState.RESUMED;
585 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
586 + " (starting new instance)");
587 r.stopped = false;
588 mResumedActivity = r;
589 r.task.touchActiveTime();
590 mService.addRecentTaskLocked(r.task);
591 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700592 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700593 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700594 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700595 }
596
Dianne Hackborncee04b52013-07-03 17:01:28 -0700597 private void startLaunchTraces() {
598 if (mFullyDrawnStartTime != 0) {
599 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
600 }
601 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
602 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
603 }
604
605 private void stopFullyDrawnTraceIfNeeded() {
606 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
607 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
608 mFullyDrawnStartTime = 0;
609 }
610 }
611
Craig Mautnere79d42682013-04-01 19:01:53 -0700612 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700613 if (r.displayStartTime == 0) {
614 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
615 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700616 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700617 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700618 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700619 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700620 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700621 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700622 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700623 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800624
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700625 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700626 // Make sure that there is no activity waiting for this to launch.
627 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
628 r.displayStartTime = r.fullyDrawnStartTime = 0;
629 } else {
630 mStackSupervisor.removeTimeoutsForActivityLocked(r);
631 mStackSupervisor.scheduleIdleTimeoutLocked(r);
632 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700633 }
634
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800635 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800636 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800637 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
638 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
639 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
640 activities.get(activityNdx).setSleeping(false);
641 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800642 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800643 }
644
Craig Mautner0eea92c2013-05-16 13:35:39 -0700645 /**
646 * @return true if something must be done before going to sleep.
647 */
648 boolean checkReadyForSleepLocked() {
649 if (mResumedActivity != null) {
650 // Still have something resumed; can't sleep until it is paused.
651 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
652 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
653 startPausingLocked(false, true);
654 return true;
655 }
656 if (mPausingActivity != null) {
657 // Still waiting for something to pause; can't sleep yet.
658 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
659 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800660 }
661
Craig Mautner0eea92c2013-05-16 13:35:39 -0700662 return false;
663 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800664
Craig Mautner0eea92c2013-05-16 13:35:39 -0700665 void goToSleep() {
666 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800667
Craig Mautner0eea92c2013-05-16 13:35:39 -0700668 // Make sure any stopped but visible activities are now sleeping.
669 // This ensures that the activity's onStop() is called.
670 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
671 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
672 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
673 final ActivityRecord r = activities.get(activityNdx);
674 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
675 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800676 }
677 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800678 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700679 }
Craig Mautner59c00972012-07-30 12:10:24 -0700680
Dianne Hackbornd2835932010-12-13 16:28:46 -0800681 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800682 if (who.noDisplay) {
683 return null;
684 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800685
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700686 TaskRecord tr = who.task;
Craig Mautner34740402013-10-11 11:05:35 -0700687 if (mService.getMostRecentTask() != tr && tr.intent != null &&
688 (tr.intent.getFlags() & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) != 0) {
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700689 // If this task is being excluded from recents, we don't want to take
690 // the expense of capturing a thumbnail, since we will never show it.
691 return null;
692 }
693
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800694 Resources res = mService.mContext.getResources();
695 int w = mThumbnailWidth;
696 int h = mThumbnailHeight;
697 if (w < 0) {
698 mThumbnailWidth = w =
699 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
700 mThumbnailHeight = h =
701 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
702 }
703
704 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800705 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700706 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800707 || mLastScreenshotBitmap.getWidth() != w
708 || mLastScreenshotBitmap.getHeight() != h) {
709 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700710 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700711 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800712 }
713 if (mLastScreenshotBitmap != null) {
John Reck172e87c2013-10-02 16:55:16 -0700714 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800715 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800716 }
717 return null;
718 }
719
Craig Mautnercf910b02013-04-23 11:23:27 -0700720 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800721 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800722 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700723 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800724 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700725 ActivityRecord prev = mResumedActivity;
726 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700727 Slog.e(TAG, "Trying to pause when nothing is resumed",
728 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700729 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700730 return;
731 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700732 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
733 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700734 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800735 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700736 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700737 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
738 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700739 prev.state = ActivityState.PAUSING;
740 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700741 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700742 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
743 if (next == null || next.task != prev.task) {
744 prev.updateThumbnail(screenshotActivities(prev), null);
745 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700746 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700747
748 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800749
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700750 if (prev.app != null && prev.app.thread != null) {
751 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
752 try {
753 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700754 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700755 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700756 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800757 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
758 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700759 } catch (Exception e) {
760 // Ignore exception, if process died other code will cleanup.
761 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800762 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700763 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700764 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700765 }
766 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800767 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700768 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700769 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700770 }
771
772 // If we are not going to sleep, we want to ensure the device is
773 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700774 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700775 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700776 }
777
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800778 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700779 // Have the window manager pause its key dispatching until the new
780 // activity has started. If we're pausing the activity just because
781 // the screen is being turned off and the UI is sleeping, don't interrupt
782 // key dispatch; the same activity will pick it up again on wakeup.
783 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800784 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700785 } else {
786 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
787 }
788
789 // Schedule a pause timeout in case the app doesn't respond.
790 // We don't give it much time because this directly impacts the
791 // responsiveness seen by the user.
792 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
793 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700794 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700795 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
796 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
797 } else {
798 // This activity failed to schedule the
799 // pause, so just treat it as being paused now.
800 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700801 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700802 }
803 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700804
Craig Mautnerd2328952013-03-05 12:46:26 -0800805 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700806 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800807 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700808
Craig Mautnerd2328952013-03-05 12:46:26 -0800809 final ActivityRecord r = isInStackLocked(token);
810 if (r != null) {
811 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
812 if (mPausingActivity == r) {
813 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
814 + (timeout ? " (due to timeout)" : " (pause complete)"));
815 r.state = ActivityState.PAUSED;
816 completePauseLocked();
817 } else {
818 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
819 r.userId, System.identityHashCode(r), r.shortComponentName,
820 mPausingActivity != null
821 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700822 }
823 }
824 }
825
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700826 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
827 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700828 if (r.state != ActivityState.STOPPING) {
829 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
830 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
831 return;
832 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700833 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700834 if (icicle != null) {
835 // If icicle is null, this is happening due to a timeout, so we
836 // haven't really saved the state.
837 r.icicle = icicle;
838 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800839 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700840 r.updateThumbnail(thumbnail, description);
841 }
842 if (!r.stopped) {
843 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
844 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
845 r.stopped = true;
846 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700847 if (r.finishing) {
848 r.clearOptionsLocked();
849 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700850 if (r.configDestroy) {
851 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700852 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700853 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700854 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800855 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700856 }
857 }
858 }
859
Craig Mautnerc8143c62013-09-03 12:15:57 -0700860 private void completePauseLocked() {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800861 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700862 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800863
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800864 if (prev != null) {
865 if (prev.finishing) {
866 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700867 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800868 } else if (prev.app != null) {
869 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
870 if (prev.waitingVisible) {
871 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700872 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800873 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
874 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800875 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800876 if (prev.configDestroy) {
877 // The previous is being paused because the configuration
878 // is changing, which means it is actually stopping...
879 // To juggle the fact that we are also starting a new
880 // instance right now, we need to first completely stop
881 // the current instance before starting the new one.
882 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
883 destroyActivityLocked(prev, true, false, "pause-config");
884 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700885 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700886 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
887 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800888 // If we already have a few activities waiting to stop,
889 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700890 // them out. Or if r is the last of activity of the last task the stack
891 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800892 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700893 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800894 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700895 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800896 }
897 }
898 } else {
899 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
900 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800901 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800902 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700903 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800904
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700905 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700906 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700907 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800908 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700909 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700910 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700911 if (top == null || (prev != null && top != prev)) {
912 // If there are no more activities available to run,
913 // do resume anyway to start something. Also if the top
914 // activity on the stack is not the just paused activity,
915 // we need to go ahead and resume it to ensure we complete
916 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700917 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700918 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700919 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800920
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800921 if (prev != null) {
922 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700923
Craig Mautner525f3d92013-05-07 14:01:50 -0700924 if (prev.app != null && prev.cpuTimeAtResume > 0
925 && mService.mBatteryStatsService.isOnBattery()) {
926 long diff;
Dianne Hackbornd2932242013-08-05 18:18:42 -0700927 synchronized (mService.mProcessCpuThread) {
928 diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
Craig Mautner525f3d92013-05-07 14:01:50 -0700929 - prev.cpuTimeAtResume;
930 }
931 if (diff > 0) {
932 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
933 synchronized (bsi) {
934 BatteryStatsImpl.Uid.Proc ps =
935 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -0700936 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -0700937 if (ps != null) {
938 ps.addForegroundTimeLocked(diff);
939 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700940 }
941 }
942 }
Craig Mautner525f3d92013-05-07 14:01:50 -0700943 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700944 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700945 }
946
947 /**
948 * Once we know that we have asked an application to put an activity in
949 * the resumed state (either by launching it or explicitly telling it),
950 * this function updates the rest of our state to match that fact.
951 */
Craig Mautner525f3d92013-05-07 14:01:50 -0700952 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700953 next.idle = false;
954 next.results = null;
955 next.newIntents = null;
Craig Mautner07566322013-09-26 16:42:55 -0700956 if (next.nowVisible) {
957 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
958 mStackSupervisor.dismissKeyguard();
959 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700960
961 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -0700962 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700963
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700964 mStackSupervisor.reportResumedActivityLocked(next);
965
966 next.resumeKeyDispatchingLocked();
967 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700968
969 // Mark the point when the activity is resuming
970 // TODO: To be more accurate, the mark should be before the onCreate,
971 // not after the onResume. But for subsequent starts, onResume is fine.
972 if (next.app != null) {
Dianne Hackbornd2932242013-08-05 18:18:42 -0700973 synchronized (mService.mProcessCpuThread) {
974 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700975 }
976 } else {
977 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
978 }
979 }
980
Craig Mautner580ea812013-04-25 12:58:38 -0700981 /**
Craig Mautner39e1c5a2013-10-23 15:14:22 -0700982 * Determine if home should be visible below the passed record.
983 * @param record activity we are querying for.
984 * @return true if home is visible below the passed activity, false otherwise.
985 */
986 boolean isActivityOverHome(ActivityRecord record) {
987 // Start at record and go down, look for either home or a visible fullscreen activity.
988 final TaskRecord recordTask = record.task;
989 for (int taskNdx = mTaskHistory.indexOf(recordTask); taskNdx >= 0; --taskNdx) {
990 TaskRecord task = mTaskHistory.get(taskNdx);
991 final ArrayList<ActivityRecord> activities = task.mActivities;
992 final int startNdx =
993 task == recordTask ? activities.indexOf(record) : activities.size() - 1;
994 for (int activityNdx = startNdx; activityNdx >= 0; --activityNdx) {
995 final ActivityRecord r = activities.get(activityNdx);
996 if (r.isHomeActivity()) {
997 return true;
998 }
999 if (!r.finishing && r.visible && r.fullscreen) {
1000 // Passed activity is over a visible fullscreen activity.
1001 return false;
1002 }
1003 }
1004 if (task.mOnTopOfHome) {
1005 // Got to the bottom of a task on top of home without finding a visible fullscreen
1006 // activity. Home is visible.
1007 return true;
1008 }
1009 }
1010 // Got to the bottom of this stack and still don't know. If this is over the home stack
1011 // then record is over home. May not work if we ever get more than two layers.
1012 return mStackSupervisor.isFrontStack(this);
1013 }
1014
1015 /**
Craig Mautner580ea812013-04-25 12:58:38 -07001016 * Version of ensureActivitiesVisible that can easily be called anywhere.
1017 */
1018 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1019 return ensureActivitiesVisibleLocked(starting, configChanges, false);
1020 }
1021
1022 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1023 boolean forceHomeShown) {
1024 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -07001025 return r != null &&
1026 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -07001027 }
1028
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001029 /**
1030 * Make sure that all activities that need to be visible (that is, they
1031 * currently can be seen by the user) actually are.
1032 */
Craig Mautner580ea812013-04-25 12:58:38 -07001033 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
1034 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001035 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001036 TAG, "ensureActivitiesVisible behind " + top
1037 + " configChanges=0x" + Integer.toHexString(configChanges));
1038
Craig Mautner5eda9b32013-07-02 11:58:16 -07001039 if (mTranslucentActivityWaiting != top) {
1040 mUndrawnActivitiesBelowTopTranslucent.clear();
1041 if (mTranslucentActivityWaiting != null) {
1042 // Call the callback with a timeout indication.
1043 notifyActivityDrawnLocked(null);
1044 mTranslucentActivityWaiting = null;
1045 }
1046 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1047 }
1048
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001049 // If the top activity is not fullscreen, then we need to
1050 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001051 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -07001052 boolean showHomeBehindStack = false;
1053 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
1054 !(forceHomeShown && isHomeStack());
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001055 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001056 final TaskRecord task = mTaskHistory.get(taskNdx);
1057 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001058 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1059 final ActivityRecord r = activities.get(activityNdx);
1060 if (r.finishing) {
1061 continue;
1062 }
1063 if (aboveTop && r != top) {
1064 continue;
1065 }
1066 aboveTop = false;
1067 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001068 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001069 TAG, "Make visible? " + r + " finishing=" + r.finishing
1070 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001071
Craig Mautnerd44711d2013-02-23 11:24:36 -08001072 final boolean doThisProcess = onlyThisProcess == null
1073 || onlyThisProcess.equals(r.processName);
1074
1075 // First: if this is not the current activity being started, make
1076 // sure it matches the current configuration.
1077 if (r != starting && doThisProcess) {
1078 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001079 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001080
1081 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001082 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001083 // This activity needs to be visible, but isn't even
1084 // running... get it started, but don't resume it
1085 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001086 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001087 if (r != starting) {
1088 r.startFreezingScreenLocked(r.app, configChanges);
1089 }
1090 if (!r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001091 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001092 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001093 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001094 }
1095 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001096 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001097 }
1098 }
1099
1100 } else if (r.visible) {
1101 // If this activity is already visible, then there is nothing
1102 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001103 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001104 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001105
1106 } else if (onlyThisProcess == null) {
1107 // This activity is not currently visible, but is running.
1108 // Tell it to become visible.
1109 r.visible = true;
1110 if (r.state != ActivityState.RESUMED && r != starting) {
1111 // If this activity is paused, tell it
1112 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001113 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001114 TAG, "Making visible and scheduling visibility: " + r);
1115 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001116 if (mTranslucentActivityWaiting != null) {
1117 mUndrawnActivitiesBelowTopTranslucent.add(r);
1118 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001119 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001120 r.sleeping = false;
1121 r.app.pendingUiClean = true;
1122 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1123 r.stopFreezingScreenLocked(false);
1124 } catch (Exception e) {
1125 // Just skip on any failure; we'll make it
1126 // visible when it next restarts.
1127 Slog.w(TAG, "Exception thrown making visibile: "
1128 + r.intent.getComponent(), e);
1129 }
1130 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001131 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001132
Craig Mautnerd44711d2013-02-23 11:24:36 -08001133 // Aggregate current change flags.
1134 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001135
Craig Mautnerd44711d2013-02-23 11:24:36 -08001136 if (r.fullscreen) {
1137 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001138 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1139 behindFullscreen = true;
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001140 } else if (isActivityOverHome(r)) {
1141 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1142 showHomeBehindStack = true;
1143 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001144 }
1145 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001146 if (DEBUG_VISBILITY) Slog.v(
1147 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1148 + " state=" + r.state
1149 + " behindFullscreen=" + behindFullscreen);
1150 // Now for any activities that aren't visible to the user, make
1151 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001152 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001153 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001154 r.visible = false;
1155 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001156 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001157 switch (r.state) {
1158 case STOPPING:
1159 case STOPPED:
1160 if (r.app != null && r.app.thread != null) {
1161 if (DEBUG_VISBILITY) Slog.v(
1162 TAG, "Scheduling invisibility: " + r);
1163 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1164 }
1165 break;
1166
1167 case INITIALIZING:
1168 case RESUMED:
1169 case PAUSING:
1170 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001171 // This case created for transitioning activities from
1172 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautnere5273b42013-09-09 12:57:47 -07001173 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1174 mStackSupervisor.mStoppingActivities.add(r);
1175 }
1176 mStackSupervisor.scheduleIdleLocked();
Craig Mautner4addfc52013-06-25 08:05:45 -07001177 break;
1178
1179 default:
1180 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001181 }
1182 } catch (Exception e) {
1183 // Just skip on any failure; we'll make it
1184 // visible when it next restarts.
1185 Slog.w(TAG, "Exception thrown making hidden: "
1186 + r.intent.getComponent(), e);
1187 }
1188 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001189 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001190 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001191 }
1192 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001193 }
Craig Mautner580ea812013-04-25 12:58:38 -07001194 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001195 }
Craig Mautner58547802013-03-05 08:23:53 -08001196
Craig Mautner5eda9b32013-07-02 11:58:16 -07001197 void convertToTranslucent(ActivityRecord r) {
1198 mTranslucentActivityWaiting = r;
1199 mUndrawnActivitiesBelowTopTranslucent.clear();
1200 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1201 }
1202
1203 /**
1204 * Called as activities below the top translucent activity are redrawn. When the last one is
1205 * redrawn notify the top activity by calling
1206 * {@link Activity#onTranslucentConversionComplete}.
1207 *
1208 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1209 * occurred and the activity will be notified immediately.
1210 */
1211 void notifyActivityDrawnLocked(ActivityRecord r) {
1212 if ((r == null)
1213 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1214 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1215 // The last undrawn activity below the top has just been drawn. If there is an
1216 // opaque activity at the top, notify it that it can become translucent safely now.
1217 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1218 mTranslucentActivityWaiting = null;
1219 mUndrawnActivitiesBelowTopTranslucent.clear();
1220 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1221
1222 if (waitingActivity != null && waitingActivity.app != null &&
1223 waitingActivity.app.thread != null) {
1224 try {
1225 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1226 waitingActivity.appToken, r != null);
1227 } catch (RemoteException e) {
1228 }
1229 }
1230 }
1231 }
1232
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001233 /**
1234 * Ensure that the top activity in the stack is resumed.
1235 *
1236 * @param prev The previously resumed activity, for when in the process
1237 * of pausing; can be null to call from elsewhere.
1238 *
1239 * @return Returns true if something is being resumed, or false if
1240 * nothing happened.
1241 */
1242 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001243 return resumeTopActivityLocked(prev, null);
1244 }
1245
1246 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001247 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1248
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001249 // Find the first activity that is not finishing.
1250 ActivityRecord next = topRunningActivityLocked(null);
1251
1252 // Remember how we'll process this pause/resume situation, and ensure
1253 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001254 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1255 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001256
1257 if (next == null) {
1258 // There are no more activities! Let's just start up the
1259 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001260 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001261 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001262 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001263 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001264 }
1265
1266 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001267
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001268 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001269 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1270 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001271 // Make sure we have executed any pending transitions, since there
1272 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001273 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001274 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001275 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001276 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001277 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001278 return false;
1279 }
1280
Craig Mautner525f3d92013-05-07 14:01:50 -07001281 final TaskRecord nextTask = next.task;
1282 final TaskRecord prevTask = prev != null ? prev.task : null;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001283 if (prevTask != null && prevTask.mOnTopOfHome && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001284 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001285 if (prevTask == nextTask) {
1286 ArrayList<ActivityRecord> activities = prevTask.mActivities;
1287 final int numActivities = activities.size();
1288 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1289 final ActivityRecord r = activities.get(activityNdx);
1290 // r is usually the same as next, but what if two activities were launched
1291 // before prev finished?
1292 if (!r.finishing) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001293 r.frontOfTask = true;
1294 break;
1295 }
1296 }
1297 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001298 // This task is going away but it was supposed to return to the home task.
1299 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001300 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001301 mTaskHistory.get(taskNdx).mOnTopOfHome = true;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001302 } else {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001303 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Launching home next");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001304 return mStackSupervisor.resumeHomeActivity(prev);
1305 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001306 }
1307
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001308 // If we are sleeping, and there is no resumed activity, and the top
1309 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001310 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001311 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001312 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001313 // Make sure we have executed any pending transitions, since there
1314 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001315 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001316 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001317 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001318 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001319 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001320 return false;
1321 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001322
1323 // Make sure that the user who owns this activity is started. If not,
1324 // we will just leave it as is because someone should be bringing
1325 // another user's activities to the top of the stack.
1326 if (mService.mStartedUsers.get(next.userId) == null) {
1327 Slog.w(TAG, "Skipping resume of top activity " + next
1328 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001329 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001330 return false;
1331 }
1332
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001333 // The activity may be waiting for stop, but that is no longer
1334 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001335 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001336 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001337 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001338 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001339
Dianne Hackborn84375872012-06-01 19:03:50 -07001340 next.updateOptionsLocked(options);
1341
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001342 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1343
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001344 // If we are currently pausing an activity, then don't do anything
1345 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001346 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001347 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1348 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001349 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001350 return false;
1351 }
1352
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001353 // Okay we are now going to start a switch, to 'next'. We may first
1354 // have to pause the current activity, but this is an important point
1355 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001356 // XXX "App Redirected" dialog is getting too many false positives
1357 // at this point, so turn off for now.
1358 if (false) {
1359 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1360 long now = SystemClock.uptimeMillis();
1361 final boolean inTime = mLastStartedActivity.startTime != 0
1362 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1363 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1364 final int nextUid = next.info.applicationInfo.uid;
1365 if (inTime && lastUid != nextUid
1366 && lastUid != next.launchedFromUid
1367 && mService.checkPermission(
1368 android.Manifest.permission.STOP_APP_SWITCHES,
1369 -1, next.launchedFromUid)
1370 != PackageManager.PERMISSION_GRANTED) {
1371 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1372 } else {
1373 next.startTime = now;
1374 mLastStartedActivity = next;
1375 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001376 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001377 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001378 mLastStartedActivity = next;
1379 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001380 }
Craig Mautner58547802013-03-05 08:23:53 -08001381
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001382 // We need to start pausing the current activity so the top one
1383 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001384 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001385 if (mResumedActivity != null) {
1386 pausing = true;
1387 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001388 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001389 }
1390 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001391 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1392 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001393 // At this point we want to put the upcoming activity's process
1394 // at the top of the LRU list, since we know we will be needing it
1395 // very soon and it would be a waste to let it get killed if it
1396 // happens to be sitting towards the end.
1397 if (next.app != null && next.app.thread != null) {
1398 // No reason to do full oom adj update here; we'll let that
1399 // happen whenever it needs to later.
Dianne Hackborndb926082013-10-31 16:32:44 -07001400 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001401 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001402 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001403 return true;
1404 }
1405
Christopher Tated3f175c2012-06-14 14:16:54 -07001406 // If the most recent activity was noHistory but was only stopped rather
1407 // than stopped+finished because the device went to sleep, we need to make
1408 // sure to finish it as we're making a new activity topmost.
Craig Mautner0f922742013-08-06 08:44:42 -07001409 if (mService.mSleeping && mLastNoHistoryActivity != null &&
1410 !mLastNoHistoryActivity.finishing) {
1411 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1412 " on new resume");
1413 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1414 null, "no-history", false);
1415 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001416 }
1417
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001418 if (prev != null && prev != next) {
1419 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1420 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001421 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001422 if (DEBUG_SWITCH) Slog.v(
1423 TAG, "Resuming top, waiting visible to hide: " + prev);
1424 } else {
1425 // The next activity is already visible, so hide the previous
1426 // activity's windows right now so we can show the new one ASAP.
1427 // We only do this if the previous is finishing, which should mean
1428 // it is on top of the one being resumed so hiding it quickly
1429 // is good. Otherwise, we want to do the normal route of allowing
1430 // the resumed activity to be shown so we can decide if the
1431 // previous should actually be hidden depending on whether the
1432 // new one is found to be full-screen or not.
1433 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001434 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001435 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1436 + prev + ", waitingVisible="
1437 + (prev != null ? prev.waitingVisible : null)
1438 + ", nowVisible=" + next.nowVisible);
1439 } else {
1440 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1441 + prev + ", waitingVisible="
1442 + (prev != null ? prev.waitingVisible : null)
1443 + ", nowVisible=" + next.nowVisible);
1444 }
1445 }
1446 }
1447
Dianne Hackborne7f97212011-02-24 14:40:20 -08001448 // Launching this app's activity, make sure the app is no longer
1449 // considered stopped.
1450 try {
1451 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001452 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001453 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001454 } catch (IllegalArgumentException e) {
1455 Slog.w(TAG, "Failed trying to unstop package "
1456 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001457 }
1458
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001459 // We are starting up the next activity, so tell the window manager
1460 // that the previous one will be hidden soon. This way it can know
1461 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001462 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001463 if (prev != null) {
1464 if (prev.finishing) {
1465 if (DEBUG_TRANSITION) Slog.v(TAG,
1466 "Prepare close transition: prev=" + prev);
1467 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001468 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001469 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001470 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001471 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001472 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1473 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001474 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001475 mWindowManager.setAppWillBeHidden(prev.appToken);
1476 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001477 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001478 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001479 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001480 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001481 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001482 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001483 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001484 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1485 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001486 }
1487 }
1488 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001489 mWindowManager.setAppWillBeHidden(prev.appToken);
1490 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001491 }
Craig Mautner967212c2013-04-13 21:10:58 -07001492 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001493 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001494 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001495 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001496 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001497 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001498 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001499 }
1500 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001501 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001502 next.applyOptionsLocked();
1503 } else {
1504 next.clearOptionsLocked();
1505 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001506
Craig Mautnercf910b02013-04-23 11:23:27 -07001507 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001508 if (next.app != null && next.app.thread != null) {
1509 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1510
1511 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001512 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001513
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001514 // schedule launch ticks to collect information about slow apps.
1515 next.startLaunchTickingLocked();
1516
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001517 ActivityRecord lastResumedActivity =
1518 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001519 ActivityState lastState = next.state;
1520
1521 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001522
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001523 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001524 next.state = ActivityState.RESUMED;
1525 mResumedActivity = next;
1526 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001527 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001528 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001529 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001530 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001531
1532 // Have the window manager re-evaluate the orientation of
1533 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001534 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001535 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001536 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001537 mService.mConfiguration,
1538 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1539 if (config != null) {
1540 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001541 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001542 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001543 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001544
Craig Mautner525f3d92013-05-07 14:01:50 -07001545 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001546 // The configuration update wasn't able to keep the existing
1547 // instance of the activity, and instead started a new one.
1548 // We should be all done, but let's just make sure our activity
1549 // is still at the top and schedule another run if something
1550 // weird happened.
1551 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001552 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001553 "Activity config changed during resume: " + next
1554 + ", new next: " + nextNext);
1555 if (nextNext != next) {
1556 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001557 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001558 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001559 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001560 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001561 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001562 return true;
1563 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001564 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001565 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001566 }
Craig Mautner58547802013-03-05 08:23:53 -08001567
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001568 try {
1569 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001570 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001571 if (a != null) {
1572 final int N = a.size();
1573 if (!next.finishing && N > 0) {
1574 if (DEBUG_RESULTS) Slog.v(
1575 TAG, "Delivering results to " + next
1576 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001577 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001578 }
1579 }
1580
1581 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001582 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001583 }
1584
1585 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001586 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001587 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001588
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001589 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001590 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001591 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001592 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
1593 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001594 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001595
Craig Mautner0eea92c2013-05-16 13:35:39 -07001596 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001597
Craig Mautnerac6f8432013-07-17 13:24:59 -07001598 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001599 } catch (Exception e) {
1600 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001601 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1602 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001603 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001604 if (lastStack != null) {
1605 lastStack.mResumedActivity = lastResumedActivity;
1606 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001607 Slog.i(TAG, "Restarting because process died: " + next);
1608 if (!next.hasBeenLaunched) {
1609 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001610 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1611 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001612 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001613 next.appToken, next.packageName, next.theme,
1614 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001615 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1616 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001617 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001618 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001619 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001620 return true;
1621 }
1622
1623 // From this point on, if something goes wrong there is no way
1624 // to recover the activity.
1625 try {
1626 next.visible = true;
1627 completeResumeLocked(next);
1628 } catch (Exception e) {
1629 // If any exception gets thrown, toss away this
1630 // activity and try the next one.
1631 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001632 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001633 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001634 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001635 return true;
1636 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001637 next.stopped = false;
1638
1639 } else {
1640 // Whoops, need to restart this activity!
1641 if (!next.hasBeenLaunched) {
1642 next.hasBeenLaunched = true;
1643 } else {
1644 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001645 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001646 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001647 mService.compatibilityInfoForPackageLocked(
1648 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001649 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001650 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001651 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001652 }
1653 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1654 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001655 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
Craig Mautnere79d42682013-04-01 19:01:53 -07001656 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001657 }
1658
Craig Mautnercf910b02013-04-23 11:23:27 -07001659 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001660 return true;
1661 }
1662
Craig Mautnerac6f8432013-07-17 13:24:59 -07001663 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001664 // If this is being moved to the top by another activity or being launched from the home
1665 // activity, set mOnTopOfHome accordingly.
Craig Mautnerd99384d2013-10-14 07:09:18 -07001666 ActivityStack lastStack = mStackSupervisor.getLastStack();
1667 final boolean fromHome = lastStack == null ? true : lastStack.isHomeStack();
Craig Mautner9c85c202013-10-04 20:11:26 -07001668 if (!isHomeStack() && (fromHome || topTask() != task)) {
1669 task.mOnTopOfHome = fromHome;
1670 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001671
Craig Mautnerac6f8432013-07-17 13:24:59 -07001672 mTaskHistory.remove(task);
1673 // Now put task at top.
1674 int stackNdx = mTaskHistory.size();
1675 if (task.userId != mCurrentUser) {
1676 // Put non-current user tasks below current user tasks.
1677 while (--stackNdx >= 0) {
1678 if (mTaskHistory.get(stackNdx).userId != mCurrentUser) {
1679 break;
1680 }
1681 }
1682 ++stackNdx;
1683 }
1684 mTaskHistory.add(stackNdx, task);
1685 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001686
Craig Mautner8849a5e2013-04-02 16:41:03 -07001687 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001688 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001689 TaskRecord rTask = r.task;
1690 final int taskId = rTask.taskId;
1691 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001692 // Last activity in task had been removed or ActivityManagerService is reusing task.
1693 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001694 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001695 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001696 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001697 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001698 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001699 if (!newTask) {
1700 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001701 boolean startIt = true;
1702 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1703 task = mTaskHistory.get(taskNdx);
1704 if (task == r.task) {
1705 // Here it is! Now, if this is not yet visible to the
1706 // user, then just add it without starting; it will
1707 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001708 if (!startIt) {
1709 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1710 + task, new RuntimeException("here").fillInStackTrace());
1711 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001712 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001713 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1714 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001715 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
1716 r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001717 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001718 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001719 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001720 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001721 return;
1722 }
1723 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001724 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001725 startIt = false;
1726 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001727 }
1728 }
1729
1730 // Place a new activity at top of stack, so it is next to interact
1731 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001732
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001733 // If we are not placing the new activity frontmost, we do not want
1734 // to deliver the onUserLeaving callback to the actual frontmost
1735 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001736 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001737 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001738 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1739 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001740 }
Craig Mautner70a86932013-02-28 22:37:44 -08001741
1742 task = r.task;
1743
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001744 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001745 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001746 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001747 task.addActivityToTop(r);
1748
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001749 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001750 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001751 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001752 // We want to show the starting preview window if we are
1753 // switching to a new task, or the next activity's process is
1754 // not currently running.
1755 boolean showStartingIcon = newTask;
1756 ProcessRecord proc = r.app;
1757 if (proc == null) {
1758 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1759 }
1760 if (proc == null || proc.thread == null) {
1761 showStartingIcon = true;
1762 }
1763 if (DEBUG_TRANSITION) Slog.v(TAG,
1764 "Prepare open transition: starting " + r);
1765 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001766 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001767 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001768 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001769 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001770 ? AppTransition.TRANSIT_TASK_OPEN
1771 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001772 mNoAnimActivities.remove(r);
1773 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001774 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001775 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001776 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001777 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001778 boolean doShow = true;
1779 if (newTask) {
1780 // Even though this activity is starting fresh, we still need
1781 // to reset it to make sure we apply affinities to move any
1782 // existing activities from other tasks in to it.
1783 // If the caller has requested that the target task be
1784 // reset, then do so.
1785 if ((r.intent.getFlags()
1786 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1787 resetTaskIfNeededLocked(r, r);
1788 doShow = topRunningNonDelayedActivityLocked(null) == r;
1789 }
1790 }
1791 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1792 // Figure out if we are transitioning from another activity that is
1793 // "has the same starting icon" as the next one. This allows the
1794 // window manager to keep the previous window it had previously
1795 // created, if it still had one.
1796 ActivityRecord prev = mResumedActivity;
1797 if (prev != null) {
1798 // We don't want to reuse the previous starting preview if:
1799 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001800 if (prev.task != r.task) {
1801 prev = null;
1802 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001803 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001804 else if (prev.nowVisible) {
1805 prev = null;
1806 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001807 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001808 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001809 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001810 mService.compatibilityInfoForPackageLocked(
1811 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001812 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001813 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001814 }
1815 } else {
1816 // If this is the first activity, don't do any fancy animations,
1817 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001818 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001819 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001820 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001821 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001822 }
1823 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001824 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001825 }
1826
1827 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001828 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001829 }
1830 }
1831
Dianne Hackbornbe707852011-11-11 14:32:10 -08001832 final void validateAppTokensLocked() {
1833 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001834 mValidateAppTokens.ensureCapacity(numActivities());
1835 final int numTasks = mTaskHistory.size();
1836 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1837 TaskRecord task = mTaskHistory.get(taskNdx);
1838 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07001839 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08001840 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001841 }
Craig Mautner000f0022013-02-26 15:04:29 -08001842 TaskGroup group = new TaskGroup();
1843 group.taskId = task.taskId;
1844 mValidateAppTokens.add(group);
1845 final int numActivities = activities.size();
1846 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1847 final ActivityRecord r = activities.get(activityNdx);
1848 group.tokens.add(r.appToken);
1849 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001850 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001851 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001852 }
1853
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001854 /**
1855 * Perform a reset of the given task, if needed as part of launching it.
1856 * Returns the new HistoryRecord at the top of the task.
1857 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001858 /**
1859 * Helper method for #resetTaskIfNeededLocked.
1860 * We are inside of the task being reset... we'll either finish this activity, push it out
1861 * for another task, or leave it as-is.
1862 * @param task The task containing the Activity (taskTop) that might be reset.
1863 * @param forceReset
1864 * @return An ActivityOptions that needs to be processed.
1865 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001866 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001867 ActivityOptions topOptions = null;
1868
1869 int replyChainEnd = -1;
1870 boolean canMoveOptions = true;
1871
1872 // We only do this for activities that are not the root of the task (since if we finish
1873 // the root, we may no longer have the task!).
1874 final ArrayList<ActivityRecord> activities = task.mActivities;
1875 final int numActivities = activities.size();
1876 for (int i = numActivities - 1; i > 0; --i ) {
1877 ActivityRecord target = activities.get(i);
1878
1879 final int flags = target.info.flags;
1880 final boolean finishOnTaskLaunch =
1881 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1882 final boolean allowTaskReparenting =
1883 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1884 final boolean clearWhenTaskReset =
1885 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1886
1887 if (!finishOnTaskLaunch
1888 && !clearWhenTaskReset
1889 && target.resultTo != null) {
1890 // If this activity is sending a reply to a previous
1891 // activity, we can't do anything with it now until
1892 // we reach the start of the reply chain.
1893 // XXX note that we are assuming the result is always
1894 // to the previous activity, which is almost always
1895 // the case but we really shouldn't count on.
1896 if (replyChainEnd < 0) {
1897 replyChainEnd = i;
1898 }
1899 } else if (!finishOnTaskLaunch
1900 && !clearWhenTaskReset
1901 && allowTaskReparenting
1902 && target.taskAffinity != null
1903 && !target.taskAffinity.equals(task.affinity)) {
1904 // If this activity has an affinity for another
1905 // task, then we need to move it out of here. We will
1906 // move it as far out of the way as possible, to the
1907 // bottom of the activity stack. This also keeps it
1908 // correctly ordered with any activities we previously
1909 // moved.
Craig Mautnerdccb7702013-09-17 15:53:34 -07001910 final ActivityRecord bottom =
1911 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
1912 mTaskHistory.get(0).mActivities.get(0) : null;
1913 if (bottom != null && target.taskAffinity != null
1914 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001915 // If the activity currently at the bottom has the
1916 // same task affinity as the one we are moving,
1917 // then merge it into the same task.
Craig Mautnerdccb7702013-09-17 15:53:34 -07001918 target.setTask(bottom.task, bottom.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001919 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07001920 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001921 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001922 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001923 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001924 target.task.affinityIntent = target.intent;
1925 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1926 + " out to new task " + target.task);
1927 }
1928
1929 final TaskRecord targetTask = target.task;
1930 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001931 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001932
Craig Mautner525f3d92013-05-07 14:01:50 -07001933 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001934 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1935 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07001936 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001937 if (p.finishing) {
1938 continue;
1939 }
1940
Craig Mautner525f3d92013-05-07 14:01:50 -07001941 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001942 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001943 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001944 topOptions = p.takeOptionsLocked();
1945 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001946 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001947 }
1948 }
1949 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1950 + task + " adding to task=" + targetTask,
1951 new RuntimeException("here").fillInStackTrace());
1952 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1953 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001954 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001955 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001956
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001957 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001958 }
1959
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001960 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001961 if (VALIDATE_TOKENS) {
1962 validateAppTokensLocked();
1963 }
1964
1965 replyChainEnd = -1;
1966 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1967 // If the activity should just be removed -- either
1968 // because it asks for it, or the task should be
1969 // cleared -- then finish it and anything that is
1970 // part of its reply chain.
1971 int end;
1972 if (clearWhenTaskReset) {
1973 // In this case, we want to finish this activity
1974 // and everything above it, so be sneaky and pretend
1975 // like these are all in the reply chain.
1976 end = numActivities - 1;
1977 } else if (replyChainEnd < 0) {
1978 end = i;
1979 } else {
1980 end = replyChainEnd;
1981 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001982 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001983 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001984 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001985 if (p.finishing) {
1986 continue;
1987 }
1988 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001989 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001990 topOptions = p.takeOptionsLocked();
1991 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001992 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001993 }
1994 }
Craig Mautner58547802013-03-05 08:23:53 -08001995 if (DEBUG_TASKS) Slog.w(TAG,
1996 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001997 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001998 end--;
1999 srcPos--;
2000 }
2001 }
2002 replyChainEnd = -1;
2003 } else {
2004 // If we were in the middle of a chain, well the
2005 // activity that started it all doesn't want anything
2006 // special, so leave it all as-is.
2007 replyChainEnd = -1;
2008 }
2009 }
2010
2011 return topOptions;
2012 }
2013
2014 /**
2015 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2016 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2017 * @param affinityTask The task we are looking for an affinity to.
2018 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2019 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2020 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2021 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002022 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002023 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002024 int replyChainEnd = -1;
2025 final int taskId = task.taskId;
2026 final String taskAffinity = task.affinity;
2027
2028 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2029 final int numActivities = activities.size();
2030 // Do not operate on the root Activity.
2031 for (int i = numActivities - 1; i > 0; --i) {
2032 ActivityRecord target = activities.get(i);
2033
2034 final int flags = target.info.flags;
2035 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2036 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2037
2038 if (target.resultTo != null) {
2039 // If this activity is sending a reply to a previous
2040 // activity, we can't do anything with it now until
2041 // we reach the start of the reply chain.
2042 // XXX note that we are assuming the result is always
2043 // to the previous activity, which is almost always
2044 // the case but we really shouldn't count on.
2045 if (replyChainEnd < 0) {
2046 replyChainEnd = i;
2047 }
2048 } else if (topTaskIsHigher
2049 && allowTaskReparenting
2050 && taskAffinity != null
2051 && taskAffinity.equals(target.taskAffinity)) {
2052 // This activity has an affinity for our task. Either remove it if we are
2053 // clearing or move it over to our task. Note that
2054 // we currently punt on the case where we are resetting a
2055 // task that is not at the top but who has activities above
2056 // with an affinity to it... this is really not a normal
2057 // case, and we will need to later pull that task to the front
2058 // and usually at that point we will do the reset and pick
2059 // up those remaining activities. (This only happens if
2060 // someone starts an activity in a new task from an activity
2061 // in a task that is not currently on top.)
2062 if (forceReset || finishOnTaskLaunch) {
2063 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2064 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2065 for (int srcPos = start; srcPos >= i; --srcPos) {
2066 final ActivityRecord p = activities.get(srcPos);
2067 if (p.finishing) {
2068 continue;
2069 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002070 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002071 }
2072 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002073 if (taskInsertionPoint < 0) {
2074 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002075
Craig Mautner77878772013-03-04 19:46:24 -08002076 }
Craig Mautner77878772013-03-04 19:46:24 -08002077
2078 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2079 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2080 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2081 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002082 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002083 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002084 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002085
Craig Mautnere3a74d52013-02-22 14:14:58 -08002086 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2087 + " to stack at " + task,
2088 new RuntimeException("here").fillInStackTrace());
2089 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2090 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002091 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002092 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002093 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002094 if (VALIDATE_TOKENS) {
2095 validateAppTokensLocked();
2096 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002097
2098 // Now we've moved it in to place... but what if this is
2099 // a singleTop activity and we have put it on top of another
2100 // instance of the same activity? Then we drop the instance
2101 // below so it remains singleTop.
2102 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2103 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002104 int targetNdx = taskActivities.indexOf(target);
2105 if (targetNdx > 0) {
2106 ActivityRecord p = taskActivities.get(targetNdx - 1);
2107 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002108 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2109 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002110 }
2111 }
2112 }
2113 }
2114
2115 replyChainEnd = -1;
2116 }
2117 }
Craig Mautner77878772013-03-04 19:46:24 -08002118 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002119 }
2120
Craig Mautner8849a5e2013-04-02 16:41:03 -07002121 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002122 ActivityRecord newActivity) {
2123 boolean forceReset =
2124 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2125 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2126 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2127 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2128 forceReset = true;
2129 }
2130 }
2131
2132 final TaskRecord task = taskTop.task;
2133
2134 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2135 * for remaining tasks. Used for later tasks to reparent to task. */
2136 boolean taskFound = false;
2137
2138 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2139 ActivityOptions topOptions = null;
2140
Craig Mautner77878772013-03-04 19:46:24 -08002141 // Preserve the location for reparenting in the new task.
2142 int reparentInsertionPoint = -1;
2143
Craig Mautnere3a74d52013-02-22 14:14:58 -08002144 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2145 final TaskRecord targetTask = mTaskHistory.get(i);
2146
2147 if (targetTask == task) {
2148 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2149 taskFound = true;
2150 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002151 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2152 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002153 }
2154 }
2155
Craig Mautner70a86932013-02-28 22:37:44 -08002156 int taskNdx = mTaskHistory.indexOf(task);
2157 do {
2158 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2159 } while (taskTop == null && taskNdx >= 0);
2160
Craig Mautnere3a74d52013-02-22 14:14:58 -08002161 if (topOptions != null) {
2162 // If we got some ActivityOptions from an activity on top that
2163 // was removed from the task, propagate them to the new real top.
2164 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002165 taskTop.updateOptionsLocked(topOptions);
2166 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002167 topOptions.abort();
2168 }
2169 }
2170
2171 return taskTop;
2172 }
2173
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002174 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2175 String resultWho, int requestCode, int resultCode, Intent data) {
2176
2177 if (callingUid > 0) {
2178 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002179 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002180 }
2181
2182 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2183 + " : who=" + resultWho + " req=" + requestCode
2184 + " res=" + resultCode + " data=" + data);
2185 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2186 try {
2187 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2188 list.add(new ResultInfo(resultWho, requestCode,
2189 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002190 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002191 return;
2192 } catch (Exception e) {
2193 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2194 }
2195 }
2196
2197 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2198 }
2199
Craig Mautner04f0b702013-10-22 12:31:01 -07002200 private void adjustFocusedActivityLocked(ActivityRecord r) {
2201 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2202 ActivityRecord next = topRunningActivityLocked(null);
2203 if (next != r) {
2204 final TaskRecord task = r.task;
2205 if (r.frontOfTask && task == topTask() && task.mOnTopOfHome) {
2206 mStackSupervisor.moveHomeToTop();
2207 }
2208 }
2209 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
2210 }
2211 }
2212
Craig Mautnerf3333272013-04-22 10:55:53 -07002213 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002214 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2215 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2216 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2217 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002218 if (!mService.mSleeping) {
2219 if (DEBUG_STATES) {
2220 Slog.d(TAG, "no-history finish of " + r);
2221 }
2222 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002223 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002224 } else {
2225 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2226 + " on stop because we're just sleeping");
2227 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002228 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002229 }
2230
2231 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002232 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002233 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002234 try {
2235 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002236 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2237 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002238 r.state = ActivityState.STOPPING;
2239 if (DEBUG_VISBILITY) Slog.v(
2240 TAG, "Stopping visible=" + r.visible + " for " + r);
2241 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002242 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002243 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002244 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002245 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002246 r.setSleeping(true);
2247 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002248 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002249 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002250 } catch (Exception e) {
2251 // Maybe just ignore exceptions here... if the process
2252 // has crashed, our death notification will clean things
2253 // up.
2254 Slog.w(TAG, "Exception thrown during pause", e);
2255 // Just in case, assume it to be stopped.
2256 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002257 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002258 r.state = ActivityState.STOPPED;
2259 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002260 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002261 }
2262 }
2263 }
2264 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002265
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002266 /**
2267 * @return Returns true if the activity is being finished, false if for
2268 * some reason it is being left as-is.
2269 */
2270 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002271 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002272 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002273 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002274 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002275 + ", result=" + resultCode + ", data=" + resultData
2276 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002277 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002278 return false;
2279 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002280
Craig Mautnerd44711d2013-02-23 11:24:36 -08002281 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002282 return true;
2283 }
2284
Craig Mautnerd2328952013-03-05 12:46:26 -08002285 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002286 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2287 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2288 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2289 ActivityRecord r = activities.get(activityNdx);
2290 if (r.resultTo == self && r.requestCode == requestCode) {
2291 if ((r.resultWho == null && resultWho == null) ||
2292 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2293 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2294 false);
2295 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002296 }
2297 }
2298 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002299 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002300 }
2301
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002302 final void finishTopRunningActivityLocked(ProcessRecord app) {
2303 ActivityRecord r = topRunningActivityLocked(null);
2304 if (r != null && r.app == app) {
2305 // If the top running activity is from this crashing
2306 // process, then terminate it to avoid getting in a loop.
2307 Slog.w(TAG, " Force finishing activity "
2308 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002309 int taskNdx = mTaskHistory.indexOf(r.task);
2310 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002311 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002312 // Also terminate any activities below it that aren't yet
2313 // stopped, to avoid a situation where one will get
2314 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002315 --activityNdx;
2316 if (activityNdx < 0) {
2317 do {
2318 --taskNdx;
2319 if (taskNdx < 0) {
2320 break;
2321 }
2322 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2323 } while (activityNdx < 0);
2324 }
2325 if (activityNdx >= 0) {
2326 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002327 if (r.state == ActivityState.RESUMED
2328 || r.state == ActivityState.PAUSING
2329 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002330 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002331 Slog.w(TAG, " Force finishing activity "
2332 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002333 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002334 }
2335 }
2336 }
2337 }
2338 }
2339
Craig Mautnerd2328952013-03-05 12:46:26 -08002340 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002341 ArrayList<ActivityRecord> activities = r.task.mActivities;
2342 for (int index = activities.indexOf(r); index >= 0; --index) {
2343 ActivityRecord cur = activities.get(index);
2344 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002345 break;
2346 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002347 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002348 }
2349 return true;
2350 }
2351
Dianne Hackborn5c607432012-02-28 14:44:19 -08002352 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2353 // send the result
2354 ActivityRecord resultTo = r.resultTo;
2355 if (resultTo != null) {
2356 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2357 + " who=" + r.resultWho + " req=" + r.requestCode
2358 + " res=" + resultCode + " data=" + resultData);
2359 if (r.info.applicationInfo.uid > 0) {
2360 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2361 resultTo.packageName, resultData,
2362 resultTo.getUriPermissionsLocked());
2363 }
2364 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2365 resultData);
2366 r.resultTo = null;
2367 }
2368 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2369
2370 // Make sure this HistoryRecord is not holding on to other resources,
2371 // because clients have remote IPC references to this object so we
2372 // can't assume that will go away and want to avoid circular IPC refs.
2373 r.results = null;
2374 r.pendingResults = null;
2375 r.newIntents = null;
2376 r.icicle = null;
2377 }
2378
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002379 /**
2380 * @return Returns true if this activity has been removed from the history
2381 * list, or false if it is still in the list and will be removed later.
2382 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002383 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2384 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002385 if (r.finishing) {
2386 Slog.w(TAG, "Duplicate finish request for " + r);
2387 return false;
2388 }
2389
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002390 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002391 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002392 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002393 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002394 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2395 final int index = activities.indexOf(r);
2396 if (index < (activities.size() - 1)) {
2397 ActivityRecord next = activities.get(index+1);
2398 if (r.frontOfTask) {
2399 // The next activity is now the front of the task.
2400 next.frontOfTask = true;
2401 }
2402 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2403 // If the caller asked that this activity (and all above it)
2404 // be cleared when the task is reset, don't lose that information,
2405 // but propagate it up to the next activity.
2406 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002407 }
2408 }
2409
2410 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002411
2412 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002413
Dianne Hackborn5c607432012-02-28 14:44:19 -08002414 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002415
Craig Mautnerc8143c62013-09-03 12:15:57 -07002416 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002417 // There are clients waiting to receive thumbnails so, in case
2418 // this is an activity that someone is waiting for, add it
2419 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002420 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002421 }
2422
Craig Mautnerde4ef022013-04-07 19:01:33 -07002423 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002424 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002425 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002426 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002427 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002428 ? AppTransition.TRANSIT_TASK_CLOSE
2429 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002430
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002431 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002432 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002433
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002434 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002435 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2436 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2437 startPausingLocked(false, false);
2438 }
2439
2440 } else if (r.state != ActivityState.PAUSING) {
2441 // If the activity is PAUSING, we will complete the finish once
2442 // it is done pausing; else we can just directly finish it here.
2443 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002444 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002445 } else {
2446 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2447 }
2448
2449 return false;
2450 }
2451
Craig Mautnerf3333272013-04-22 10:55:53 -07002452 static final int FINISH_IMMEDIATELY = 0;
2453 static final int FINISH_AFTER_PAUSE = 1;
2454 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002455
Craig Mautnerf3333272013-04-22 10:55:53 -07002456 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002457 // First things first: if this activity is currently visible,
2458 // and the resumed activity is not yet visible, then hold off on
2459 // finishing until the resumed one becomes visible.
2460 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002461 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2462 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002463 if (mStackSupervisor.mStoppingActivities.size() > 3
2464 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002465 // If we already have a few activities waiting to stop,
2466 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002467 // them out. Or if r is the last of activity of the last task the stack
2468 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002469 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002470 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002471 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002472 }
2473 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002474 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2475 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002476 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002477 if (oomAdj) {
2478 mService.updateOomAdjLocked();
2479 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002480 return r;
2481 }
2482
2483 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002484 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002485 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002486 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002487 if (mResumedActivity == r) {
2488 mResumedActivity = null;
2489 }
2490 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002491 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002492 r.state = ActivityState.FINISHING;
2493
2494 if (mode == FINISH_IMMEDIATELY
2495 || prevState == ActivityState.STOPPED
2496 || prevState == ActivityState.INITIALIZING) {
2497 // If this activity is already stopped, we can just finish
2498 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002499 boolean activityRemoved = destroyActivityLocked(r, true,
2500 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002501 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002502 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002503 }
2504 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002505 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002506
2507 // Need to go through the full pause cycle to get this
2508 // activity into the stopped state and then finish it.
2509 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002510 mStackSupervisor.mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002511 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002512 return r;
2513 }
2514
Craig Mautnerd2328952013-03-05 12:46:26 -08002515 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002516 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002517 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002518 final TaskRecord task = srec.task;
2519 final ArrayList<ActivityRecord> activities = task.mActivities;
2520 final int start = activities.indexOf(srec);
2521 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002522 return false;
2523 }
2524 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002525 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002526 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002527 final ComponentName dest = destIntent.getComponent();
2528 if (start > 0 && dest != null) {
2529 for (int i = finishTo; i >= 0; i--) {
2530 ActivityRecord r = activities.get(i);
2531 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002532 r.info.name.equals(dest.getClassName())) {
2533 finishTo = i;
2534 parent = r;
2535 foundParentInTask = true;
2536 break;
2537 }
2538 }
2539 }
2540
2541 IActivityController controller = mService.mController;
2542 if (controller != null) {
2543 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2544 if (next != null) {
2545 // ask watcher if this is allowed
2546 boolean resumeOK = true;
2547 try {
2548 resumeOK = controller.activityResuming(next.packageName);
2549 } catch (RemoteException e) {
2550 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002551 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002552 }
2553
2554 if (!resumeOK) {
2555 return false;
2556 }
2557 }
2558 }
2559 final long origId = Binder.clearCallingIdentity();
2560 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002561 ActivityRecord r = activities.get(i);
2562 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002563 // Only return the supplied result for the first activity finished
2564 resultCode = Activity.RESULT_CANCELED;
2565 resultData = null;
2566 }
2567
2568 if (parent != null && foundParentInTask) {
2569 final int parentLaunchMode = parent.info.launchMode;
2570 final int destIntentFlags = destIntent.getFlags();
2571 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2572 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2573 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2574 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2575 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2576 } else {
2577 try {
2578 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2579 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002580 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002581 null, aInfo, parent.appToken, null,
2582 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2583 0, null, true, null);
2584 foundParentInTask = res == ActivityManager.START_SUCCESS;
2585 } catch (RemoteException e) {
2586 foundParentInTask = false;
2587 }
2588 requestFinishActivityLocked(parent.appToken, resultCode,
2589 resultData, "navigate-up", true);
2590 }
2591 }
2592 Binder.restoreCallingIdentity(origId);
2593 return foundParentInTask;
2594 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002595 /**
2596 * Perform the common clean-up of an activity record. This is called both
2597 * as part of destroyActivityLocked() (when destroying the client-side
2598 * representation) and cleaning things up as a result of its hosting
2599 * processing going away, in which case there is no remaining client-side
2600 * state to destroy so only the cleanup here is needed.
2601 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002602 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2603 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002604 if (mResumedActivity == r) {
2605 mResumedActivity = null;
2606 }
2607 if (mService.mFocusedActivity == r) {
2608 mService.mFocusedActivity = null;
2609 }
2610
2611 r.configDestroy = false;
2612 r.frozenBeforeDestroy = false;
2613
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002614 if (setState) {
2615 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2616 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002617 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002618 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002619 }
2620
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002621 // Make sure this record is no longer in the pending finishes list.
2622 // This could happen, for example, if we are trimming activities
2623 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002624 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002625 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002626
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002627 // Remove any pending results.
2628 if (r.finishing && r.pendingResults != null) {
2629 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2630 PendingIntentRecord rec = apr.get();
2631 if (rec != null) {
2632 mService.cancelIntentSenderLocked(rec, false);
2633 }
2634 }
2635 r.pendingResults = null;
2636 }
2637
2638 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002639 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002640 }
2641
Craig Mautnerc8143c62013-09-03 12:15:57 -07002642 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002643 // There are clients waiting to receive thumbnails so, in case
2644 // this is an activity that someone is waiting for, add it
2645 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002646 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002647 }
2648
2649 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002650 removeTimeoutsForActivityLocked(r);
2651 }
2652
2653 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002654 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002655 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002656 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002657 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002658 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002659 }
2660
Dianne Hackborn5c607432012-02-28 14:44:19 -08002661 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002662 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2663 r.makeFinishing();
2664 if (DEBUG_ADD_REMOVE) {
2665 RuntimeException here = new RuntimeException("here");
2666 here.fillInStackTrace();
2667 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002668 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002669 final TaskRecord task = r.task;
2670 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002671 if (DEBUG_STACK) Slog.i(TAG,
2672 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnere1db0dd2013-10-13 18:15:00 -07002673 if (mStackSupervisor.isFrontStack(this) && task == topTask() && task.mOnTopOfHome) {
Craig Mautner8e569572013-10-11 17:36:59 -07002674 mStackSupervisor.moveHomeToTop();
2675 }
Craig Mautner2219b752013-10-12 11:26:08 -07002676 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002677 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002678 r.takeFromHistory();
2679 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002680 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002681 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002682 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002683 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002684 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002685 if (VALIDATE_TOKENS) {
2686 validateAppTokensLocked();
2687 }
2688 cleanUpActivityServicesLocked(r);
2689 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002690 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002691
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002692 /**
2693 * Perform clean-up of service connections in an activity record.
2694 */
2695 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2696 // Throw away any services that have been bound by this activity.
2697 if (r.connections != null) {
2698 Iterator<ConnectionRecord> it = r.connections.iterator();
2699 while (it.hasNext()) {
2700 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002701 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002702 }
2703 r.connections = null;
2704 }
2705 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002706
2707 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2708 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2709 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2710 mHandler.sendMessage(msg);
2711 }
2712
Dianne Hackborn28695e02011-11-02 21:59:51 -07002713 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002714 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002715 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002716 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2717 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2718 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2719 final ActivityRecord r = activities.get(activityNdx);
2720 if (r.finishing) {
2721 continue;
2722 }
2723 if (r.fullscreen) {
2724 lastIsOpaque = true;
2725 }
2726 if (owner != null && r.app != owner) {
2727 continue;
2728 }
2729 if (!lastIsOpaque) {
2730 continue;
2731 }
2732 // We can destroy this one if we have its icicle saved and
2733 // it is not in the process of pausing/stopping/finishing.
2734 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2735 && r.haveState && !r.visible && r.stopped
2736 && r.state != ActivityState.DESTROYING
2737 && r.state != ActivityState.DESTROYED) {
2738 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2739 + " resumed=" + mResumedActivity
2740 + " pausing=" + mPausingActivity);
2741 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2742 activityRemoved = true;
2743 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002744 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002745 }
2746 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002747 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002748 mStackSupervisor.resumeTopActivitiesLocked();
2749
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002750 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002751 }
2752
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002753 /**
2754 * Destroy the current CLIENT SIDE instance of an activity. This may be
2755 * called both when actually finishing an activity, or when performing
2756 * a configuration switch where we destroy the current client-side object
2757 * but then create a new client-side object for this same HistoryRecord.
2758 */
2759 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002760 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002761 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002762 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002763 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2764 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002765 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002766 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002767
2768 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002769
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002770 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002771
2772 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002773
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002774 if (hadApp) {
2775 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002776 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002777 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2778 mService.mHeavyWeightProcess = null;
2779 mService.mHandler.sendEmptyMessage(
2780 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2781 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07002782 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07002783 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07002784 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002785 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002786 }
2787 }
2788
2789 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002790
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002791 try {
2792 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002793 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002794 r.configChangeFlags);
2795 } catch (Exception e) {
2796 // We can just ignore exceptions here... if the process
2797 // has crashed, our death notification will clean things
2798 // up.
2799 //Slog.w(TAG, "Exception thrown during finish", e);
2800 if (r.finishing) {
2801 removeActivityFromHistoryLocked(r);
2802 removedFromHistory = true;
2803 skipDestroy = true;
2804 }
2805 }
2806
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002807 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002808
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002809 // If the activity is finishing, we need to wait on removing it
2810 // from the list to give it a chance to do its cleanup. During
2811 // that time it may make calls back with its token so we need to
2812 // be able to find it on the list and so we don't want to remove
2813 // it from the list yet. Otherwise, we can just immediately put
2814 // it in the destroyed state since we are not removing it from the
2815 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002816 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002817 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2818 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002819 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002820 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002821 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2822 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002823 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002824 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002825 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002826 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002827 }
2828 } else {
2829 // remove this record from the history.
2830 if (r.finishing) {
2831 removeActivityFromHistoryLocked(r);
2832 removedFromHistory = true;
2833 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002834 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002835 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002836 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002837 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002838 }
2839 }
2840
2841 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002842
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002843 if (!mLRUActivities.remove(r) && hadApp) {
2844 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2845 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002846
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002847 return removedFromHistory;
2848 }
2849
Craig Mautnerd2328952013-03-05 12:46:26 -08002850 final void activityDestroyedLocked(IBinder token) {
2851 final long origId = Binder.clearCallingIdentity();
2852 try {
2853 ActivityRecord r = ActivityRecord.forToken(token);
2854 if (r != null) {
2855 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002856 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002857
2858 if (isInStackLocked(token) != null) {
2859 if (r.state == ActivityState.DESTROYING) {
2860 cleanUpActivityLocked(r, true, false);
2861 removeActivityFromHistoryLocked(r);
2862 }
2863 }
Craig Mautner05d29032013-05-03 13:40:13 -07002864 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002865 } finally {
2866 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002867 }
2868 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002869
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002870 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2871 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002872 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002873 if (DEBUG_CLEANUP) Slog.v(
2874 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002875 + " with " + i + " entries");
2876 while (i > 0) {
2877 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002878 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002879 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002880 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002881 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002882 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002883 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002884 }
2885 }
2886 }
2887
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002888 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2889 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002890 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2891 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002892 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2893 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002894 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002895 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002896 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2897 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002898
2899 boolean hasVisibleActivities = false;
2900
2901 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002902 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002903 if (DEBUG_CLEANUP) Slog.v(
2904 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002905 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2906 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2907 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2908 final ActivityRecord r = activities.get(activityNdx);
2909 --i;
2910 if (DEBUG_CLEANUP) Slog.v(
2911 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2912 if (r.app == app) {
2913 boolean remove;
2914 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2915 // Don't currently have state for the activity, or
2916 // it is finishing -- always remove it.
2917 remove = true;
2918 } else if (r.launchCount > 2 &&
2919 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2920 // We have launched this activity too many times since it was
2921 // able to run, so give up and remove it.
2922 remove = true;
2923 } else {
2924 // The process may be gone, but the activity lives on!
2925 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002926 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002927 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002928 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002929 RuntimeException here = new RuntimeException("here");
2930 here.fillInStackTrace();
2931 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2932 + ": haveState=" + r.haveState
2933 + " stateNotNeeded=" + r.stateNotNeeded
2934 + " finishing=" + r.finishing
2935 + " state=" + r.state, here);
2936 }
2937 if (!r.finishing) {
2938 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2939 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2940 r.userId, System.identityHashCode(r),
2941 r.task.taskId, r.shortComponentName,
2942 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07002943 if (r.state == ActivityState.RESUMED) {
2944 mService.updateUsageStats(r, false);
2945 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002946 }
2947 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002948
Craig Mautner0247fc82013-02-28 14:32:06 -08002949 } else {
2950 // We have the current state for this activity, so
2951 // it can be restarted later when needed.
2952 if (localLOGV) Slog.v(
2953 TAG, "Keeping entry, setting app to null");
2954 if (r.visible) {
2955 hasVisibleActivities = true;
2956 }
2957 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2958 + r);
2959 r.app = null;
2960 r.nowVisible = false;
2961 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002962 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08002963 "App died, clearing saved state of " + r);
2964 r.icicle = null;
2965 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002966 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002967
Craig Mautnerd2328952013-03-05 12:46:26 -08002968 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002969 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002970 }
2971 }
2972
2973 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002974 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002975
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002976 final void updateTransitLocked(int transit, Bundle options) {
2977 if (options != null) {
2978 ActivityRecord r = topRunningActivityLocked(null);
2979 if (r != null && r.state != ActivityState.RESUMED) {
2980 r.updateOptionsLocked(options);
2981 } else {
2982 ActivityOptions.abort(options);
2983 }
2984 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002985 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002986 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002987
Craig Mautnera82aa092013-09-13 15:34:08 -07002988 void moveHomeTaskToTop() {
2989 final int top = mTaskHistory.size() - 1;
2990 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
2991 final TaskRecord task = mTaskHistory.get(taskNdx);
2992 if (task.isHomeTask()) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002993 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG, "moveHomeTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07002994 mTaskHistory.remove(taskNdx);
2995 mTaskHistory.add(top, task);
2996 mWindowManager.moveTaskToTop(task.taskId);
2997 return;
2998 }
2999 }
3000 }
3001
Craig Mautnercae015f2013-02-08 14:31:27 -08003002 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003003 final TaskRecord task = taskForIdLocked(taskId);
3004 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003005 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003006 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003007 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003008 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
3009 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07003010 // we'll just indicate that this task returns to the home task.
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003011 task.mOnTopOfHome = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08003012 }
3013 moveTaskToFrontLocked(task, null, options);
3014 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003015 }
3016 return false;
3017 }
3018
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003019 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003020 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003021
Craig Mautner11bf9a52013-02-19 14:08:51 -08003022 final int numTasks = mTaskHistory.size();
3023 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003024 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003025 // nothing to do!
3026 if (reason != null &&
3027 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3028 ActivityOptions.abort(options);
3029 } else {
3030 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3031 }
3032 return;
3033 }
3034
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003035 mStackSupervisor.moveHomeStack(isHomeStack());
3036
Craig Mautner11bf9a52013-02-19 14:08:51 -08003037 // Shift all activities with this task up to the top
3038 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003039 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003040
3041 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003042 if (reason != null &&
3043 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003044 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003045 ActivityRecord r = topRunningActivityLocked(null);
3046 if (r != null) {
3047 mNoAnimActivities.add(r);
3048 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003049 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003050 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003051 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003052 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003053
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003054 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003055
Craig Mautner05d29032013-05-03 13:40:13 -07003056 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003057 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003058
3059 if (VALIDATE_TOKENS) {
3060 validateAppTokensLocked();
3061 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003062 }
3063
3064 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003065 * Worker method for rearranging history stack. Implements the function of moving all
3066 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003067 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003068 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003069 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3070 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003071 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003072 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003073 * @return Returns true if the move completed, false if not.
3074 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003075 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
3076 Slog.i(TAG, "moveTaskToBack: " + taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003077
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003078 // If we have a watcher, preflight the move before committing to it. First check
3079 // for *other* available tasks, but if none are available, then try again allowing the
3080 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003081 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003082 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003083 if (next == null) {
3084 next = topRunningActivityLocked(null, 0);
3085 }
3086 if (next != null) {
3087 // ask watcher if this is allowed
3088 boolean moveOK = true;
3089 try {
3090 moveOK = mService.mController.activityResuming(next.packageName);
3091 } catch (RemoteException e) {
3092 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003093 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003094 }
3095 if (!moveOK) {
3096 return false;
3097 }
3098 }
3099 }
3100
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003101 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003102 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003103
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003104 final TaskRecord tr = taskForIdLocked(taskId);
Craig Mautnerd2328952013-03-05 12:46:26 -08003105 if (tr == null) {
3106 return false;
3107 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003108
Craig Mautner11bf9a52013-02-19 14:08:51 -08003109 mTaskHistory.remove(tr);
3110 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003111
Craig Mautnerc8143c62013-09-03 12:15:57 -07003112 // There is an assumption that moving a task to the back moves it behind the home activity.
3113 // We make sure here that some activity in the stack will launch home.
3114 ActivityRecord lastActivity = null;
3115 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003116 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3117 final TaskRecord task = mTaskHistory.get(taskNdx);
3118 if (task.mOnTopOfHome) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003119 break;
3120 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003121 if (taskNdx == 1) {
3122 // Set the last task before tr to go to home.
3123 task.mOnTopOfHome = true;
3124 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003125 }
3126
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003127 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003128 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3129 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003130 ActivityRecord r = topRunningActivityLocked(null);
3131 if (r != null) {
3132 mNoAnimActivities.add(r);
3133 }
3134 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003135 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003136 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003137 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003138
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003139 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003140 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003141 }
3142
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003143 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
3144 if (task == tr && task.mOnTopOfHome || numTasks <= 1) {
3145 task.mOnTopOfHome = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003146 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003147 }
3148
Craig Mautner05d29032013-05-03 13:40:13 -07003149 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003150 return true;
3151 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003152
Craig Mautner8849a5e2013-04-02 16:41:03 -07003153 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003154 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003155 final Uri data = r.intent.getData();
3156 final String strData = data != null ? data.toSafeString() : null;
3157
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003158 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003159 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003160 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003161 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003162 }
3163
3164 /**
3165 * Make sure the given activity matches the current configuration. Returns
3166 * false if the activity had to be destroyed. Returns true if the
3167 * configuration is the same, or the activity will remain running as-is
3168 * for whatever reason. Ensures the HistoryRecord is updated with the
3169 * correct configuration and all other bookkeeping is handled.
3170 */
3171 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3172 int globalChanges) {
3173 if (mConfigWillChange) {
3174 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3175 "Skipping config check (will change): " + r);
3176 return true;
3177 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003178
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003179 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3180 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003181
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003182 // Short circuit: if the two configurations are the exact same
3183 // object (the common case), then there is nothing to do.
3184 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003185 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003186 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3187 "Configuration unchanged in " + r);
3188 return true;
3189 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003190
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003191 // We don't worry about activities that are finishing.
3192 if (r.finishing) {
3193 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3194 "Configuration doesn't matter in finishing " + r);
3195 r.stopFreezingScreenLocked(false);
3196 return true;
3197 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003198
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003199 // Okay we now are going to make this activity have the new config.
3200 // But then we need to figure out how it needs to deal with that.
3201 Configuration oldConfig = r.configuration;
3202 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003203
3204 // Determine what has changed. May be nothing, if this is a config
3205 // that has come back from the app after going idle. In that case
3206 // we just want to leave the official config object now in the
3207 // activity and do nothing else.
3208 final int changes = oldConfig.diff(newConfig);
3209 if (changes == 0 && !r.forceNewConfig) {
3210 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3211 "Configuration no differences in " + r);
3212 return true;
3213 }
3214
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003215 // If the activity isn't currently running, just leave the new
3216 // configuration and it will pick that up next time it starts.
3217 if (r.app == null || r.app.thread == null) {
3218 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3219 "Configuration doesn't matter not running " + r);
3220 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003221 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003222 return true;
3223 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003224
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003225 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003226 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3227 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3228 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003229 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003230 + ", newConfig=" + newConfig);
3231 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003232 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003233 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3234 r.configChangeFlags |= changes;
3235 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003236 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003237 if (r.app == null || r.app.thread == null) {
3238 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003239 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003240 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003241 } else if (r.state == ActivityState.PAUSING) {
3242 // A little annoying: we are waiting for this activity to
3243 // finish pausing. Let's not do anything now, but just
3244 // flag that it needs to be restarted when done pausing.
3245 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003246 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003247 r.configDestroy = true;
3248 return true;
3249 } else if (r.state == ActivityState.RESUMED) {
3250 // Try to optimize this case: the configuration is changing
3251 // and we need to restart the top, resumed activity.
3252 // Instead of doing the normal handshaking, just say
3253 // "restart!".
3254 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003255 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003256 relaunchActivityLocked(r, r.configChangeFlags, true);
3257 r.configChangeFlags = 0;
3258 } else {
3259 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003260 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003261 relaunchActivityLocked(r, r.configChangeFlags, false);
3262 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003263 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003264
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003265 // All done... tell the caller we weren't able to keep this
3266 // activity around.
3267 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003268 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003269
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003270 // Default case: the activity can handle this new configuration, so
3271 // hand it over. Note that we don't need to give it the new
3272 // configuration, since we always send configuration changes to all
3273 // process when they happen so it can just use whatever configuration
3274 // it last got.
3275 if (r.app != null && r.app.thread != null) {
3276 try {
3277 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003278 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003279 } catch (RemoteException e) {
3280 // If process died, whatever.
3281 }
3282 }
3283 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003284
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003285 return true;
3286 }
3287
Craig Mautnerc8143c62013-09-03 12:15:57 -07003288 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003289 int changes, boolean andResume) {
3290 List<ResultInfo> results = null;
3291 List<Intent> newIntents = null;
3292 if (andResume) {
3293 results = r.results;
3294 newIntents = r.newIntents;
3295 }
3296 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3297 + " with results=" + results + " newIntents=" + newIntents
3298 + " andResume=" + andResume);
3299 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003300 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003301 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003302
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003303 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003304
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003305 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003306 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3307 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3308 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003309 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003310 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003311 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003312 // Note: don't need to call pauseIfSleepingLocked() here, because
3313 // the caller will only pass in 'andResume' if this activity is
3314 // currently resumed, which implies we aren't sleeping.
3315 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003316 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003317 }
3318
3319 if (andResume) {
3320 r.results = null;
3321 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003322 r.state = ActivityState.RESUMED;
3323 } else {
3324 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3325 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003326 }
3327
3328 return true;
3329 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003330
3331 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003332 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3333 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3334 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3335 final ActivityRecord r = activities.get(activityNdx);
3336 if (r.appToken == token) {
3337 return true;
3338 }
3339 if (r.fullscreen && !r.finishing) {
3340 return false;
3341 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003342 }
3343 }
3344 return true;
3345 }
3346
3347 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003348 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3349 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3350 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3351 final ActivityRecord r = activities.get(activityNdx);
3352 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003353 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003354 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003355 }
3356 }
3357 }
3358
3359 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3360 boolean didSomething = false;
3361 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003362 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3363 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3364 int numActivities = activities.size();
3365 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3366 ActivityRecord r = activities.get(activityNdx);
3367 final boolean samePackage = r.packageName.equals(name)
3368 || (name == null && r.userId == userId);
3369 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3370 && (samePackage || r.task == lastTask)
3371 && (r.app == null || evenPersistent || !r.app.persistent)) {
3372 if (!doit) {
3373 if (r.finishing) {
3374 // If this activity is just finishing, then it is not
3375 // interesting as far as something to stop.
3376 continue;
3377 }
3378 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003379 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003380 didSomething = true;
3381 Slog.i(TAG, " Force finishing activity " + r);
3382 if (samePackage) {
3383 if (r.app != null) {
3384 r.app.removed = true;
3385 }
3386 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003387 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003388 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003389 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3390 true)) {
3391 // r has been deleted from mActivities, accommodate.
3392 --numActivities;
3393 --activityNdx;
3394 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003395 }
3396 }
3397 }
3398 return didSomething;
3399 }
3400
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003401 ActivityRecord getTasksLocked(IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003402 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003403 ActivityRecord topRecord = null;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003404 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003405 final TaskRecord task = mTaskHistory.get(taskNdx);
3406 ActivityRecord r = null;
3407 ActivityRecord top = null;
3408 int numActivities = 0;
3409 int numRunning = 0;
3410 final ArrayList<ActivityRecord> activities = task.mActivities;
3411 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3412 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003413
Craig Mautneraab647e2013-02-28 16:31:36 -08003414 // Initialize state for next task if needed.
3415 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3416 top = r;
3417 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003418 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003419
3420 // Add 'r' into the current task.
3421 numActivities++;
3422 if (r.app != null && r.app.thread != null) {
3423 numRunning++;
3424 }
3425
3426 if (localLOGV) Slog.v(
3427 TAG, r.intent.getComponent().flattenToShortString()
3428 + ": task=" + r.task);
3429 }
3430
3431 RunningTaskInfo ci = new RunningTaskInfo();
3432 ci.id = task.taskId;
3433 ci.baseActivity = r.intent.getComponent();
3434 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003435 ci.lastActiveTime = task.lastActiveTime;
3436
Craig Mautneraab647e2013-02-28 16:31:36 -08003437 if (top.thumbHolder != null) {
3438 ci.description = top.thumbHolder.lastDescription;
3439 }
3440 ci.numActivities = numActivities;
3441 ci.numRunning = numRunning;
3442 //System.out.println(
3443 // "#" + maxNum + ": " + " descr=" + ci.description);
3444 if (receiver != null) {
3445 if (localLOGV) Slog.v(
3446 TAG, "State=" + top.state + "Idle=" + top.idle
3447 + " app=" + top.app
3448 + " thr=" + (top.app != null ? top.app.thread : null));
3449 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3450 if (top.idle && top.app != null && top.app.thread != null) {
3451 topRecord = top;
3452 } else {
3453 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003454 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003455 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003456 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003457 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003458 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003459 }
3460 return topRecord;
3461 }
3462
3463 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003464 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003465 if (DEBUG_SWITCH) Slog.d(
3466 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003467 if (top >= 0) {
3468 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3469 int activityTop = activities.size() - 1;
3470 if (activityTop > 0) {
3471 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3472 "unhandled-back", true);
3473 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003474 }
3475 }
3476
Craig Mautner6b74cb52013-09-27 17:02:21 -07003477 /**
3478 * Reset local parameters because an app's activity died.
3479 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003480 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003481 */
3482 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003483 if (mPausingActivity != null && mPausingActivity.app == app) {
3484 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3485 "App died while pausing: " + mPausingActivity);
3486 mPausingActivity = null;
3487 }
3488 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3489 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003490 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003491 }
3492
Craig Mautner19091252013-10-05 00:03:53 -07003493 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003494 }
3495
Craig Mautnercae015f2013-02-08 14:31:27 -08003496 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003497 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3498 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3499 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3500 final ActivityRecord r = activities.get(activityNdx);
3501 if (r.app == app) {
3502 Slog.w(TAG, " Force finishing activity "
3503 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003504 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003505 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003506 }
3507 }
3508 }
3509
Dianne Hackborn390517b2013-05-30 15:03:32 -07003510 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003511 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003512 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003513 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3514 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003515 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3516 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003517 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003518 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003519 if (printed) {
3520 header = null;
3521 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003522 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003523 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003524 }
3525
3526 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3527 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3528
3529 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003530 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3531 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003532 }
3533 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003534 final int top = mTaskHistory.size() - 1;
3535 if (top >= 0) {
3536 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3537 int listTop = list.size() - 1;
3538 if (listTop >= 0) {
3539 activities.add(list.get(listTop));
3540 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003541 }
3542 } else {
3543 ItemMatcher matcher = new ItemMatcher();
3544 matcher.build(name);
3545
Craig Mautneraab647e2013-02-28 16:31:36 -08003546 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3547 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3548 if (matcher.match(r1, r1.intent.getComponent())) {
3549 activities.add(r1);
3550 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003551 }
3552 }
3553 }
3554
3555 return activities;
3556 }
3557
3558 ActivityRecord restartPackage(String packageName) {
3559 ActivityRecord starting = topRunningActivityLocked(null);
3560
3561 // All activities that came from the package must be
3562 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003563 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3564 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3565 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3566 final ActivityRecord a = activities.get(activityNdx);
3567 if (a.info.packageName.equals(packageName)) {
3568 a.forceNewConfig = true;
3569 if (starting != null && a == starting && a.visible) {
3570 a.startFreezingScreenLocked(starting.app,
3571 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3572 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003573 }
3574 }
3575 }
3576
3577 return starting;
3578 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003579
Craig Mautnerde4ef022013-04-07 19:01:33 -07003580 boolean removeTask(TaskRecord task) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003581 final int taskNdx = mTaskHistory.indexOf(task);
3582 final int topTaskNdx = mTaskHistory.size() - 1;
3583 if (task.mOnTopOfHome && taskNdx < topTaskNdx) {
3584 mTaskHistory.get(taskNdx + 1).mOnTopOfHome = true;
3585 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003586 mTaskHistory.remove(task);
Craig Mautnerc8143c62013-09-03 12:15:57 -07003587 return mTaskHistory.isEmpty();
Craig Mautner0247fc82013-02-28 14:32:06 -08003588 }
3589
Craig Mautnerde4ef022013-04-07 19:01:33 -07003590 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003591 TaskRecord task = new TaskRecord(taskId, info, intent);
3592 addTask(task, toTop);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003593 return task;
3594 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003595
3596 ArrayList<TaskRecord> getAllTasks() {
3597 return new ArrayList<TaskRecord>(mTaskHistory);
3598 }
3599
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003600 void addTask(final TaskRecord task, final boolean toTop) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003601 task.stack = this;
3602 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003603 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003604 } else {
3605 mTaskHistory.add(0, task);
3606 }
3607 }
3608
3609 public int getStackId() {
3610 return mStackId;
3611 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003612
3613 @Override
3614 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07003615 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
3616 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07003617 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003618}