blob: dc86f052d360fbd0ff6f5df96adb5e853e2f4165 [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;
Craig Mautnerb12428a2012-12-20 16:07:06 -080064import android.graphics.Bitmap.Config;
Santos Cordon73ff7d82013-03-06 17:24:11 -080065import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070067import android.os.Bundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070068import android.os.Handler;
69import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090070import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070071import android.os.Message;
72import android.os.PowerManager;
73import android.os.RemoteException;
74import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070075import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070078import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079
Craig Mautnercae015f2013-02-08 14:31:27 -080080import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080081import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070082import java.lang.ref.WeakReference;
83import java.util.ArrayList;
84import java.util.Iterator;
85import java.util.List;
86
87/**
88 * State and management of a single stack of activities.
89 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070090final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080091
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070092 // Ticks during which we check progress while waiting for an app to launch.
93 static final int LAUNCH_TICK = 500;
94
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070095 // How long we wait until giving up on the last activity to pause. This
96 // is short because it directly impacts the responsiveness of starting the
97 // next activity.
98 static final int PAUSE_TIMEOUT = 500;
99
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700100 // How long we wait for the activity to tell us it has stopped before
101 // giving up. This is a good amount of time because we really need this
102 // from the application in order to get its saved state.
103 static final int STOP_TIMEOUT = 10*1000;
104
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700105 // How long we wait until giving up on an activity telling us it has
106 // finished destroying itself.
107 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800108
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700109 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800110 // disabled.
111 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800112
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700113 // How long between activity launches that we consider safe to not warn
114 // the user about an unexpected activity being launched on top.
115 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800116
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700117 // Set to false to disable the preview that is shown while a new activity
118 // is being started.
119 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800120
Craig Mautner5eda9b32013-07-02 11:58:16 -0700121 // How long to wait for all background Activities to redraw following a call to
122 // convertToTranslucent().
123 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
124
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700125 enum ActivityState {
126 INITIALIZING,
127 RESUMED,
128 PAUSING,
129 PAUSED,
130 STOPPING,
131 STOPPED,
132 FINISHING,
133 DESTROYING,
134 DESTROYED
135 }
136
137 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700138 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800139
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700140 final Context mContext;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800141
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700142 /**
143 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800144 * running) activities. It contains #TaskRecord objects.
145 */
146 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
147
148 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800149 * Used for validating app tokens with window manager.
150 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800151 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800152
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700153 /**
154 * List of running activities, sorted by recent usage.
155 * The first entry in the list is the least recently used.
156 * It contains HistoryRecord objects.
157 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700158 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700159
160 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700161 * Animations that for the current transition have requested not to
162 * be considered for the transition animation.
163 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700164 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700165
166 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700167 * When we are in the process of pausing an activity, before starting the
168 * next one, this variable holds the activity that is currently being paused.
169 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800170 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700171
172 /**
173 * This is the last activity that we put into the paused state. This is
174 * used to determine if we need to do an activity transition while sleeping,
175 * when we normally hold the top activity paused.
176 */
177 ActivityRecord mLastPausedActivity = null;
178
179 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700180 * Activities that specify No History must be removed once the user navigates away from them.
181 * If the device goes to sleep with such an activity in the paused state then we save it here
182 * and finish it later if another activity replaces it on wakeup.
183 */
184 ActivityRecord mLastNoHistoryActivity = null;
185
186 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700187 * Current activity that is resumed, or null if there is none.
188 */
189 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800190
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700191 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700192 * This is the last activity that has been started. It is only used to
193 * identify when multiple activities are started at once so that the user
194 * can be warned they may not be in the activity they think they are.
195 */
196 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800197
Craig Mautner5eda9b32013-07-02 11:58:16 -0700198 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
199 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
200 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
201 // Activity in mTranslucentActivityWaiting is notified via
202 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
203 // background activity being drawn then the same call will be made with a true value.
204 ActivityRecord mTranslucentActivityWaiting = null;
205 ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
206 new ArrayList<ActivityRecord>();
207
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700208 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700209 * Set when we know we are going to be calling updateConfiguration()
210 * soon, so want to skip intermediate config checks.
211 */
212 boolean mConfigWillChange;
213
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700214 long mLaunchStartTime = 0;
215 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800216
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800217 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800218 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
219 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
220 */
221 private ActivityRecord mLastScreenshotActivity = null;
222 private Bitmap mLastScreenshotBitmap = null;
223
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800224 int mThumbnailWidth = -1;
225 int mThumbnailHeight = -1;
226
Craig Mautner858d8a62013-04-23 17:08:34 -0700227 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700228
Craig Mautnerc00204b2013-03-05 15:02:14 -0800229 final int mStackId;
230
Craig Mautner27084302013-03-25 08:05:25 -0700231 /** Run all ActivityStacks through this */
232 final ActivityStackSupervisor mStackSupervisor;
233
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700234 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700235 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
236 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
237 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
238 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700239 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700240
241 static class ScheduleDestroyArgs {
242 final ProcessRecord mOwner;
243 final boolean mOomAdj;
244 final String mReason;
245 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
246 mOwner = owner;
247 mOomAdj = oomAdj;
248 mReason = reason;
249 }
250 }
251
Zoran Marcetaf958b322012-08-09 20:27:12 +0900252 final Handler mHandler;
253
254 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700255 //public Handler() {
256 // if (localLOGV) Slog.v(TAG, "Handler started!");
257 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700258 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900259 super(looper);
260 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700261
Zoran Marcetaf958b322012-08-09 20:27:12 +0900262 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700263 public void handleMessage(Message msg) {
264 switch (msg.what) {
265 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800266 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700267 // We don't at this point know if the activity is fullscreen,
268 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800269 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700270 synchronized (mService) {
271 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700272 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700273 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700274 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800275 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700276 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700277 case LAUNCH_TICK_MSG: {
278 ActivityRecord r = (ActivityRecord)msg.obj;
279 synchronized (mService) {
280 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700281 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700282 }
283 }
284 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700285 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800286 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700287 // We don't at this point know if the activity is fullscreen,
288 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800289 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800290 synchronized (mService) {
291 activityDestroyedLocked(r != null ? r.appToken : null);
292 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700293 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700294 case STOP_TIMEOUT_MSG: {
295 ActivityRecord r = (ActivityRecord)msg.obj;
296 // We don't at this point know if the activity is fullscreen,
297 // so we need to be conservative and assume it isn't.
298 Slog.w(TAG, "Activity stop timeout for " + r);
299 synchronized (mService) {
300 if (r.isInHistory()) {
301 activityStoppedLocked(r, null, null, null);
302 }
303 }
304 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700305 case DESTROY_ACTIVITIES_MSG: {
306 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
307 synchronized (mService) {
308 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
309 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700310 } break;
311 case TRANSLUCENT_TIMEOUT_MSG: {
312 synchronized (mService) {
313 notifyActivityDrawnLocked(null);
314 }
315 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700316 }
317 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800318 }
319
Craig Mautner000f0022013-02-26 15:04:29 -0800320 private int numActivities() {
321 int count = 0;
322 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
323 count += mTaskHistory.get(taskNdx).mActivities.size();
324 }
325 return count;
326 }
327
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700328 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900329 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700330 mService = service;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700331 mWindowManager = service.mWindowManager;
332 mStackSupervisor = service.mStackSupervisor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700333 mContext = context;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800334 mStackId = stackId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700335 mCurrentUser = service.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700336 }
Craig Mautner5962b122012-10-05 14:45:52 -0700337
338 private boolean okToShow(ActivityRecord r) {
339 return r.userId == mCurrentUser
340 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
341 }
342
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700343 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800344 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
345 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800346 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800347 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
348 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800349 if (!r.finishing && r != notTop && okToShow(r)) {
350 return r;
351 }
352 }
353 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700354 return null;
355 }
356
357 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800358 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
359 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800360 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800361 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
362 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800363 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
364 return r;
365 }
366 }
367 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700368 return null;
369 }
370
371 /**
372 * This is a simplified version of topRunningActivityLocked that provides a number of
373 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800374 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700375 * @param token If non-null, any history records matching this token will be skipped.
376 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800377 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700378 * @return Returns the HistoryRecord of the next activity on the stack.
379 */
380 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800381 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
382 TaskRecord task = mTaskHistory.get(taskNdx);
383 if (task.taskId == taskId) {
384 continue;
385 }
386 ArrayList<ActivityRecord> activities = task.mActivities;
387 for (int i = activities.size() - 1; i >= 0; --i) {
388 final ActivityRecord r = activities.get(i);
389 // Note: the taskId check depends on real taskId fields being non-zero
390 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
391 return r;
392 }
393 }
394 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700395 return null;
396 }
397
Craig Mautner8849a5e2013-04-02 16:41:03 -0700398 final ActivityRecord topActivity() {
399 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700400 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700401 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
402 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
403 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700404 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700405 }
406 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700407 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700408 }
409
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700410 final TaskRecord topTask() {
411 final int size = mTaskHistory.size();
412 if (size > 0) {
413 return mTaskHistory.get(size - 1);
414 }
415 return null;
416 }
417
Craig Mautnerd2328952013-03-05 12:46:26 -0800418 TaskRecord taskForIdLocked(int id) {
419 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
420 final TaskRecord task = mTaskHistory.get(taskNdx);
421 if (task.taskId == id) {
422 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800423 }
424 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700425 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700426 }
427
Craig Mautnerd2328952013-03-05 12:46:26 -0800428 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800429 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800430 if (r != null) {
431 final TaskRecord task = r.task;
432 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
433 if (task.stack != this) Slog.w(TAG,
434 "Illegal state! task does not point to stack it is in.");
435 return r;
436 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800437 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800438 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800439 }
440
Craig Mautnerf88c50f2013-04-18 19:25:12 -0700441 boolean containsApp(ProcessRecord app) {
442 if (app == null) {
443 return false;
444 }
445 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
446 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
447 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
448 final ActivityRecord r = activities.get(activityNdx);
449 if (r.finishing) {
450 continue;
451 }
452 if (r.app == app) {
453 return true;
454 }
455 }
456 }
457 return false;
458 }
459
Craig Mautner2420ead2013-04-01 17:13:20 -0700460 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700461 final boolean hadit = mLRUActivities.remove(r);
462 mLRUActivities.add(r);
463 return hadit;
464 }
465
Craig Mautnerde4ef022013-04-07 19:01:33 -0700466 final boolean isHomeStack() {
467 return mStackId == HOME_STACK_ID;
468 }
469
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700470 /**
471 * Returns the top activity in any existing task matching the given
472 * Intent. Returns null if no such task is found.
473 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700474 ActivityRecord findTaskLocked(ActivityRecord target) {
475 Intent intent = target.intent;
476 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700477 ComponentName cls = intent.getComponent();
478 if (info.targetActivity != null) {
479 cls = new ComponentName(info.packageName, info.targetActivity);
480 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700481 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800482
483 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
484 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700485 if (task.userId != userId) {
486 // Looking for a different task.
487 continue;
488 }
Craig Mautner000f0022013-02-26 15:04:29 -0800489 final ActivityRecord r = task.getTopActivity();
490 if (r == null || r.finishing || r.userId != userId ||
491 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
492 continue;
493 }
494
495 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
496 // + "/aff=" + r.task.affinity + " to new cls="
497 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
498 if (task.affinity != null) {
499 if (task.affinity.equals(info.taskAffinity)) {
500 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700501 return r;
502 }
Craig Mautner000f0022013-02-26 15:04:29 -0800503 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
504 //Slog.i(TAG, "Found matching class!");
505 //dump();
506 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
507 return r;
508 } else if (task.affinityIntent != null
509 && task.affinityIntent.getComponent().equals(cls)) {
510 //Slog.i(TAG, "Found matching class!");
511 //dump();
512 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
513 return r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700514 }
515 }
516
517 return null;
518 }
519
520 /**
521 * Returns the first activity (starting from the top of the stack) that
522 * is the same as the given activity. Returns null if no such activity
523 * is found.
524 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700525 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700526 ComponentName cls = intent.getComponent();
527 if (info.targetActivity != null) {
528 cls = new ComponentName(info.packageName, info.targetActivity);
529 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700530 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700531
Craig Mautner000f0022013-02-26 15:04:29 -0800532 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700533 TaskRecord task = mTaskHistory.get(taskNdx);
534 if (task.userId != mCurrentUser) {
535 return null;
536 }
537 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800538 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
539 ActivityRecord r = activities.get(activityNdx);
540 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700541 //Slog.i(TAG, "Found matching class!");
542 //dump();
543 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
544 return r;
545 }
546 }
547 }
548
549 return null;
550 }
551
Amith Yamasani742a6712011-05-04 14:49:28 -0700552 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700553 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700554 * @return whether there are any activities for the specified user.
555 */
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700556 final boolean switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800557 if (VALIDATE_TOKENS) {
558 validateAppTokensLocked();
559 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800560 if (mCurrentUser == userId) {
561 return true;
562 }
563 mCurrentUser = userId;
564
565 // Move userId's tasks to the top.
566 boolean haveActivities = false;
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) {
571 haveActivities = true;
572 mTaskHistory.remove(i);
573 mTaskHistory.add(task);
574 --index;
575 }
576 }
577
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700578 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700579 }
580
Craig Mautner2420ead2013-04-01 17:13:20 -0700581 void minimalResumeActivityLocked(ActivityRecord r) {
582 r.state = ActivityState.RESUMED;
583 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
584 + " (starting new instance)");
585 r.stopped = false;
586 mResumedActivity = r;
587 r.task.touchActiveTime();
588 mService.addRecentTaskLocked(r.task);
589 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700590 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -0700591 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700592 }
593
Dianne Hackborncee04b52013-07-03 17:01:28 -0700594 private void startLaunchTraces() {
595 if (mFullyDrawnStartTime != 0) {
596 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
597 }
598 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
599 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
600 }
601
602 private void stopFullyDrawnTraceIfNeeded() {
603 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
604 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
605 mFullyDrawnStartTime = 0;
606 }
607 }
608
Craig Mautnere79d42682013-04-01 19:01:53 -0700609 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700610 if (r.displayStartTime == 0) {
611 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
612 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700613 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700614 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700615 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700616 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700617 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700618 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700619 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700620 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800621
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700622 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700623 // Make sure that there is no activity waiting for this to launch.
624 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
625 r.displayStartTime = r.fullyDrawnStartTime = 0;
626 } else {
627 mStackSupervisor.removeTimeoutsForActivityLocked(r);
628 mStackSupervisor.scheduleIdleTimeoutLocked(r);
629 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700630 }
631
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800632 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800633 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800634 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
635 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
636 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
637 activities.get(activityNdx).setSleeping(false);
638 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800639 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800640 }
641
Craig Mautner0eea92c2013-05-16 13:35:39 -0700642 /**
643 * @return true if something must be done before going to sleep.
644 */
645 boolean checkReadyForSleepLocked() {
646 if (mResumedActivity != null) {
647 // Still have something resumed; can't sleep until it is paused.
648 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
649 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
650 startPausingLocked(false, true);
651 return true;
652 }
653 if (mPausingActivity != null) {
654 // Still waiting for something to pause; can't sleep yet.
655 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
656 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800657 }
658
Craig Mautner0eea92c2013-05-16 13:35:39 -0700659 return false;
660 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800661
Craig Mautner0eea92c2013-05-16 13:35:39 -0700662 void goToSleep() {
663 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800664
Craig Mautner0eea92c2013-05-16 13:35:39 -0700665 // Make sure any stopped but visible activities are now sleeping.
666 // This ensures that the activity's onStop() is called.
667 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
668 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
669 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
670 final ActivityRecord r = activities.get(activityNdx);
671 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
672 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800673 }
674 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800675 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700676 }
Craig Mautner59c00972012-07-30 12:10:24 -0700677
Dianne Hackbornd2835932010-12-13 16:28:46 -0800678 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800679 if (who.noDisplay) {
680 return null;
681 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800682
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800683 Resources res = mService.mContext.getResources();
684 int w = mThumbnailWidth;
685 int h = mThumbnailHeight;
686 if (w < 0) {
687 mThumbnailWidth = w =
688 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
689 mThumbnailHeight = h =
690 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
691 }
692
693 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800694 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
695 || mLastScreenshotBitmap.getWidth() != w
696 || mLastScreenshotBitmap.getHeight() != h) {
697 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700698 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
Craig Mautnerb12428a2012-12-20 16:07:06 -0800699 who.appToken, Display.DEFAULT_DISPLAY, w, h);
700 }
701 if (mLastScreenshotBitmap != null) {
702 return mLastScreenshotBitmap.copy(Config.ARGB_8888, true);
703 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800704 }
705 return null;
706 }
707
Craig Mautnercf910b02013-04-23 11:23:27 -0700708 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800709 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800710 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700711 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800712 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700713 ActivityRecord prev = mResumedActivity;
714 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700715 Slog.e(TAG, "Trying to pause when nothing is resumed",
716 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700717 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700718 return;
719 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700720 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
721 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700722 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800723 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700724 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700725 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
726 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700727 prev.state = ActivityState.PAUSING;
728 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700729 clearLaunchTime(prev);
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700730 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700731 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700732
733 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800734
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700735 if (prev.app != null && prev.app.thread != null) {
736 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
737 try {
738 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700739 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700740 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700741 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800742 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
743 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700744 } catch (Exception e) {
745 // Ignore exception, if process died other code will cleanup.
746 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800747 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700748 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700749 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700750 }
751 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800752 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700753 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700754 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700755 }
756
757 // If we are not going to sleep, we want to ensure the device is
758 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700759 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700760 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700761 }
762
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800763 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700764 // Have the window manager pause its key dispatching until the new
765 // activity has started. If we're pausing the activity just because
766 // the screen is being turned off and the UI is sleeping, don't interrupt
767 // key dispatch; the same activity will pick it up again on wakeup.
768 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800769 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700770 } else {
771 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
772 }
773
774 // Schedule a pause timeout in case the app doesn't respond.
775 // We don't give it much time because this directly impacts the
776 // responsiveness seen by the user.
777 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
778 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700779 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700780 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
781 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
782 } else {
783 // This activity failed to schedule the
784 // pause, so just treat it as being paused now.
785 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700786 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700787 }
788 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700789
Craig Mautnerd2328952013-03-05 12:46:26 -0800790 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700791 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800792 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700793
Craig Mautnerd2328952013-03-05 12:46:26 -0800794 final ActivityRecord r = isInStackLocked(token);
795 if (r != null) {
796 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
797 if (mPausingActivity == r) {
798 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
799 + (timeout ? " (due to timeout)" : " (pause complete)"));
800 r.state = ActivityState.PAUSED;
801 completePauseLocked();
802 } else {
803 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
804 r.userId, System.identityHashCode(r), r.shortComponentName,
805 mPausingActivity != null
806 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700807 }
808 }
809 }
810
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700811 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
812 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700813 if (r.state != ActivityState.STOPPING) {
814 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
815 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
816 return;
817 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700818 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700819 if (icicle != null) {
820 // If icicle is null, this is happening due to a timeout, so we
821 // haven't really saved the state.
822 r.icicle = icicle;
823 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800824 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700825 r.updateThumbnail(thumbnail, description);
826 }
827 if (!r.stopped) {
828 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
829 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
830 r.stopped = true;
831 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700832 if (r.finishing) {
833 r.clearOptionsLocked();
834 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700835 if (r.configDestroy) {
836 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700837 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700838 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700839 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800840 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700841 }
842 }
843 }
844
Craig Mautnerc8143c62013-09-03 12:15:57 -0700845 private void completePauseLocked() {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800846 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700847 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800848
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800849 if (prev != null) {
850 if (prev.finishing) {
851 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700852 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800853 } else if (prev.app != null) {
854 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
855 if (prev.waitingVisible) {
856 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700857 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800858 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
859 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800860 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800861 if (prev.configDestroy) {
862 // The previous is being paused because the configuration
863 // is changing, which means it is actually stopping...
864 // To juggle the fact that we are also starting a new
865 // instance right now, we need to first completely stop
866 // the current instance before starting the new one.
867 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
868 destroyActivityLocked(prev, true, false, "pause-config");
869 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700870 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700871 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
872 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800873 // If we already have a few activities waiting to stop,
874 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700875 // them out. Or if r is the last of activity of the last task the stack
876 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800877 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700878 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800879 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700880 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800881 }
882 }
883 } else {
884 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
885 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800886 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800887 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700888 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800889
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700890 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700891 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700892 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800893 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700894 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700895 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700896 if (top == null || (prev != null && top != prev)) {
897 // If there are no more activities available to run,
898 // do resume anyway to start something. Also if the top
899 // activity on the stack is not the just paused activity,
900 // we need to go ahead and resume it to ensure we complete
901 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700902 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700903 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700904 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800905
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800906 if (prev != null) {
907 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700908
Craig Mautner525f3d92013-05-07 14:01:50 -0700909 if (prev.app != null && prev.cpuTimeAtResume > 0
910 && mService.mBatteryStatsService.isOnBattery()) {
911 long diff;
Dianne Hackbornd2932242013-08-05 18:18:42 -0700912 synchronized (mService.mProcessCpuThread) {
913 diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
Craig Mautner525f3d92013-05-07 14:01:50 -0700914 - prev.cpuTimeAtResume;
915 }
916 if (diff > 0) {
917 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
918 synchronized (bsi) {
919 BatteryStatsImpl.Uid.Proc ps =
920 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -0700921 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -0700922 if (ps != null) {
923 ps.addForegroundTimeLocked(diff);
924 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700925 }
926 }
927 }
Craig Mautner525f3d92013-05-07 14:01:50 -0700928 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700929 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700930 }
931
932 /**
933 * Once we know that we have asked an application to put an activity in
934 * the resumed state (either by launching it or explicitly telling it),
935 * this function updates the rest of our state to match that fact.
936 */
Craig Mautner525f3d92013-05-07 14:01:50 -0700937 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700938 next.idle = false;
939 next.results = null;
940 next.newIntents = null;
941
942 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -0700943 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700944
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700945 mStackSupervisor.reportResumedActivityLocked(next);
946
947 next.resumeKeyDispatchingLocked();
948 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700949
950 // Mark the point when the activity is resuming
951 // TODO: To be more accurate, the mark should be before the onCreate,
952 // not after the onResume. But for subsequent starts, onResume is fine.
953 if (next.app != null) {
Dianne Hackbornd2932242013-08-05 18:18:42 -0700954 synchronized (mService.mProcessCpuThread) {
955 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700956 }
957 } else {
958 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
959 }
960 }
961
Craig Mautner580ea812013-04-25 12:58:38 -0700962 /**
963 * Version of ensureActivitiesVisible that can easily be called anywhere.
964 */
965 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
966 return ensureActivitiesVisibleLocked(starting, configChanges, false);
967 }
968
969 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
970 boolean forceHomeShown) {
971 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -0700972 return r != null &&
973 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -0700974 }
975
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700976 /**
977 * Make sure that all activities that need to be visible (that is, they
978 * currently can be seen by the user) actually are.
979 */
Craig Mautner580ea812013-04-25 12:58:38 -0700980 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
981 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700982 if (DEBUG_VISBILITY) Slog.v(
983 TAG, "ensureActivitiesVisible behind " + top
984 + " configChanges=0x" + Integer.toHexString(configChanges));
985
Craig Mautner5eda9b32013-07-02 11:58:16 -0700986 if (mTranslucentActivityWaiting != top) {
987 mUndrawnActivitiesBelowTopTranslucent.clear();
988 if (mTranslucentActivityWaiting != null) {
989 // Call the callback with a timeout indication.
990 notifyActivityDrawnLocked(null);
991 mTranslucentActivityWaiting = null;
992 }
993 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
994 }
995
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700996 // If the top activity is not fullscreen, then we need to
997 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800998 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -0700999 boolean showHomeBehindStack = false;
1000 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
1001 !(forceHomeShown && isHomeStack());
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001002 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001003 final TaskRecord task = mTaskHistory.get(taskNdx);
1004 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001005 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1006 final ActivityRecord r = activities.get(activityNdx);
1007 if (r.finishing) {
1008 continue;
1009 }
1010 if (aboveTop && r != top) {
1011 continue;
1012 }
1013 aboveTop = false;
1014 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001015 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001016 TAG, "Make visible? " + r + " finishing=" + r.finishing
1017 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001018
Craig Mautnerd44711d2013-02-23 11:24:36 -08001019 final boolean doThisProcess = onlyThisProcess == null
1020 || onlyThisProcess.equals(r.processName);
1021
1022 // First: if this is not the current activity being started, make
1023 // sure it matches the current configuration.
1024 if (r != starting && doThisProcess) {
1025 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001026 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001027
1028 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001029 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001030 // This activity needs to be visible, but isn't even
1031 // running... get it started, but don't resume it
1032 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001033 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001034 if (r != starting) {
1035 r.startFreezingScreenLocked(r.app, configChanges);
1036 }
1037 if (!r.visible) {
1038 if (DEBUG_VISBILITY) Slog.v(
1039 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001040 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001041 }
1042 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001043 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001044 }
1045 }
1046
1047 } else if (r.visible) {
1048 // If this activity is already visible, then there is nothing
1049 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001050 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001051 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001052
1053 } else if (onlyThisProcess == null) {
1054 // This activity is not currently visible, but is running.
1055 // Tell it to become visible.
1056 r.visible = true;
1057 if (r.state != ActivityState.RESUMED && r != starting) {
1058 // If this activity is paused, tell it
1059 // to now show its window.
1060 if (DEBUG_VISBILITY) Slog.v(
1061 TAG, "Making visible and scheduling visibility: " + r);
1062 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001063 if (mTranslucentActivityWaiting != null) {
1064 mUndrawnActivitiesBelowTopTranslucent.add(r);
1065 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001066 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001067 r.sleeping = false;
1068 r.app.pendingUiClean = true;
1069 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1070 r.stopFreezingScreenLocked(false);
1071 } catch (Exception e) {
1072 // Just skip on any failure; we'll make it
1073 // visible when it next restarts.
1074 Slog.w(TAG, "Exception thrown making visibile: "
1075 + r.intent.getComponent(), e);
1076 }
1077 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001078 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001079
Craig Mautnerd44711d2013-02-23 11:24:36 -08001080 // Aggregate current change flags.
1081 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001082
Craig Mautnerd44711d2013-02-23 11:24:36 -08001083 if (r.fullscreen) {
1084 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001085 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1086 behindFullscreen = true;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001087 } else if (task.mOnTopOfHome) {
Craig Mautner580ea812013-04-25 12:58:38 -07001088 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1089 showHomeBehindStack = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001090 behindFullscreen = true;
1091 }
1092 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001093 if (DEBUG_VISBILITY) Slog.v(
1094 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1095 + " state=" + r.state
1096 + " behindFullscreen=" + behindFullscreen);
1097 // Now for any activities that aren't visible to the user, make
1098 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001099 if (r.visible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001100 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001101 r.visible = false;
1102 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001103 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001104 switch (r.state) {
1105 case STOPPING:
1106 case STOPPED:
1107 if (r.app != null && r.app.thread != null) {
1108 if (DEBUG_VISBILITY) Slog.v(
1109 TAG, "Scheduling invisibility: " + r);
1110 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1111 }
1112 break;
1113
1114 case INITIALIZING:
1115 case RESUMED:
1116 case PAUSING:
1117 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001118 // This case created for transitioning activities from
1119 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautnere5273b42013-09-09 12:57:47 -07001120 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1121 mStackSupervisor.mStoppingActivities.add(r);
1122 }
1123 mStackSupervisor.scheduleIdleLocked();
Craig Mautner4addfc52013-06-25 08:05:45 -07001124 break;
1125
1126 default:
1127 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001128 }
1129 } catch (Exception e) {
1130 // Just skip on any failure; we'll make it
1131 // visible when it next restarts.
1132 Slog.w(TAG, "Exception thrown making hidden: "
1133 + r.intent.getComponent(), e);
1134 }
1135 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001136 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001137 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001138 }
1139 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001140 }
Craig Mautner580ea812013-04-25 12:58:38 -07001141 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001142 }
Craig Mautner58547802013-03-05 08:23:53 -08001143
Craig Mautner5eda9b32013-07-02 11:58:16 -07001144 void convertToTranslucent(ActivityRecord r) {
1145 mTranslucentActivityWaiting = r;
1146 mUndrawnActivitiesBelowTopTranslucent.clear();
1147 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1148 }
1149
1150 /**
1151 * Called as activities below the top translucent activity are redrawn. When the last one is
1152 * redrawn notify the top activity by calling
1153 * {@link Activity#onTranslucentConversionComplete}.
1154 *
1155 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1156 * occurred and the activity will be notified immediately.
1157 */
1158 void notifyActivityDrawnLocked(ActivityRecord r) {
1159 if ((r == null)
1160 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1161 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1162 // The last undrawn activity below the top has just been drawn. If there is an
1163 // opaque activity at the top, notify it that it can become translucent safely now.
1164 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1165 mTranslucentActivityWaiting = null;
1166 mUndrawnActivitiesBelowTopTranslucent.clear();
1167 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1168
1169 if (waitingActivity != null && waitingActivity.app != null &&
1170 waitingActivity.app.thread != null) {
1171 try {
1172 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1173 waitingActivity.appToken, r != null);
1174 } catch (RemoteException e) {
1175 }
1176 }
1177 }
1178 }
1179
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001180 /**
1181 * Ensure that the top activity in the stack is resumed.
1182 *
1183 * @param prev The previously resumed activity, for when in the process
1184 * of pausing; can be null to call from elsewhere.
1185 *
1186 * @return Returns true if something is being resumed, or false if
1187 * nothing happened.
1188 */
1189 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001190 return resumeTopActivityLocked(prev, null);
1191 }
1192
1193 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001194 // Find the first activity that is not finishing.
1195 ActivityRecord next = topRunningActivityLocked(null);
1196
1197 // Remember how we'll process this pause/resume situation, and ensure
1198 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001199 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1200 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001201
1202 if (next == null) {
1203 // There are no more activities! Let's just start up the
1204 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001205 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001206 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001207 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001208 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001209 }
1210
1211 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001212
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001213 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001214 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1215 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001216 // Make sure we have executed any pending transitions, since there
1217 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001218 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001219 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001220 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001221 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001222 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001223 return false;
1224 }
1225
Craig Mautner525f3d92013-05-07 14:01:50 -07001226 final TaskRecord nextTask = next.task;
1227 final TaskRecord prevTask = prev != null ? prev.task : null;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001228 if (prevTask != null && prevTask.mOnTopOfHome && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001229 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001230 if (prevTask == nextTask) {
1231 ArrayList<ActivityRecord> activities = prevTask.mActivities;
1232 final int numActivities = activities.size();
1233 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1234 final ActivityRecord r = activities.get(activityNdx);
1235 // r is usually the same as next, but what if two activities were launched
1236 // before prev finished?
1237 if (!r.finishing) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001238 r.frontOfTask = true;
1239 break;
1240 }
1241 }
1242 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001243 // This task is going away but it was supposed to return to the home task.
1244 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001245 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001246 mTaskHistory.get(taskNdx).mOnTopOfHome = true;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001247 } else {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001248 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Launching home next");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001249 return mStackSupervisor.resumeHomeActivity(prev);
1250 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001251 }
1252
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001253 // If we are sleeping, and there is no resumed activity, and the top
1254 // activity is paused, well that is the state we want.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001255 if ((mService.isSleepingOrShuttingDown())
p13451dbad2872012-04-18 11:39:23 +09001256 && mLastPausedActivity == next
Craig Mautnerde4ef022013-04-07 19:01:33 -07001257 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001258 // Make sure we have executed any pending transitions, since there
1259 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001260 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001261 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001262 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001263 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001264 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001265 return false;
1266 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001267
1268 // Make sure that the user who owns this activity is started. If not,
1269 // we will just leave it as is because someone should be bringing
1270 // another user's activities to the top of the stack.
1271 if (mService.mStartedUsers.get(next.userId) == null) {
1272 Slog.w(TAG, "Skipping resume of top activity " + next
1273 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001274 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001275 return false;
1276 }
1277
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001278 // The activity may be waiting for stop, but that is no longer
1279 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001280 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001281 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001282 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001283 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001284
Dianne Hackborn84375872012-06-01 19:03:50 -07001285 next.updateOptionsLocked(options);
1286
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001287 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1288
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001289 // If we are currently pausing an activity, then don't do anything
1290 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001291 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001292 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1293 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001294 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001295 return false;
1296 }
1297
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001298 // Okay we are now going to start a switch, to 'next'. We may first
1299 // have to pause the current activity, but this is an important point
1300 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001301 // XXX "App Redirected" dialog is getting too many false positives
1302 // at this point, so turn off for now.
1303 if (false) {
1304 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1305 long now = SystemClock.uptimeMillis();
1306 final boolean inTime = mLastStartedActivity.startTime != 0
1307 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1308 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1309 final int nextUid = next.info.applicationInfo.uid;
1310 if (inTime && lastUid != nextUid
1311 && lastUid != next.launchedFromUid
1312 && mService.checkPermission(
1313 android.Manifest.permission.STOP_APP_SWITCHES,
1314 -1, next.launchedFromUid)
1315 != PackageManager.PERMISSION_GRANTED) {
1316 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1317 } else {
1318 next.startTime = now;
1319 mLastStartedActivity = next;
1320 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001321 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001322 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001323 mLastStartedActivity = next;
1324 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001325 }
Craig Mautner58547802013-03-05 08:23:53 -08001326
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001327 // We need to start pausing the current activity so the top one
1328 // can be resumed...
Craig Mautnereb957862013-04-24 15:34:32 -07001329 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
1330 if (mResumedActivity != null) {
1331 pausing = true;
1332 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001333 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001334 }
1335 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001336 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1337 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001338 // At this point we want to put the upcoming activity's process
1339 // at the top of the LRU list, since we know we will be needing it
1340 // very soon and it would be a waste to let it get killed if it
1341 // happens to be sitting towards the end.
1342 if (next.app != null && next.app.thread != null) {
1343 // No reason to do full oom adj update here; we'll let that
1344 // happen whenever it needs to later.
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001345 mService.updateLruProcessLocked(next.app, false);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001346 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001347 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001348 return true;
1349 }
1350
Christopher Tated3f175c2012-06-14 14:16:54 -07001351 // If the most recent activity was noHistory but was only stopped rather
1352 // than stopped+finished because the device went to sleep, we need to make
1353 // sure to finish it as we're making a new activity topmost.
Craig Mautner0f922742013-08-06 08:44:42 -07001354 if (mService.mSleeping && mLastNoHistoryActivity != null &&
1355 !mLastNoHistoryActivity.finishing) {
1356 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1357 " on new resume");
1358 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1359 null, "no-history", false);
1360 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001361 }
1362
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001363 if (prev != null && prev != next) {
1364 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1365 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001366 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001367 if (DEBUG_SWITCH) Slog.v(
1368 TAG, "Resuming top, waiting visible to hide: " + prev);
1369 } else {
1370 // The next activity is already visible, so hide the previous
1371 // activity's windows right now so we can show the new one ASAP.
1372 // We only do this if the previous is finishing, which should mean
1373 // it is on top of the one being resumed so hiding it quickly
1374 // is good. Otherwise, we want to do the normal route of allowing
1375 // the resumed activity to be shown so we can decide if the
1376 // previous should actually be hidden depending on whether the
1377 // new one is found to be full-screen or not.
1378 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001379 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001380 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1381 + prev + ", waitingVisible="
1382 + (prev != null ? prev.waitingVisible : null)
1383 + ", nowVisible=" + next.nowVisible);
1384 } else {
1385 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1386 + prev + ", waitingVisible="
1387 + (prev != null ? prev.waitingVisible : null)
1388 + ", nowVisible=" + next.nowVisible);
1389 }
1390 }
1391 }
1392
Dianne Hackborne7f97212011-02-24 14:40:20 -08001393 // Launching this app's activity, make sure the app is no longer
1394 // considered stopped.
1395 try {
1396 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001397 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001398 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001399 } catch (IllegalArgumentException e) {
1400 Slog.w(TAG, "Failed trying to unstop package "
1401 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001402 }
1403
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001404 // We are starting up the next activity, so tell the window manager
1405 // that the previous one will be hidden soon. This way it can know
1406 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001407 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001408 if (prev != null) {
1409 if (prev.finishing) {
1410 if (DEBUG_TRANSITION) Slog.v(TAG,
1411 "Prepare close transition: prev=" + prev);
1412 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001413 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001414 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001415 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001416 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001417 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1418 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001419 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001420 mWindowManager.setAppWillBeHidden(prev.appToken);
1421 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001422 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001423 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001424 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001425 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001426 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001427 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001428 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001429 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1430 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001431 }
1432 }
1433 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001434 mWindowManager.setAppWillBeHidden(prev.appToken);
1435 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001436 }
Craig Mautner967212c2013-04-13 21:10:58 -07001437 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001438 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001439 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001440 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001441 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001442 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001443 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001444 }
1445 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001446 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001447 next.applyOptionsLocked();
1448 } else {
1449 next.clearOptionsLocked();
1450 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001451
Craig Mautnercf910b02013-04-23 11:23:27 -07001452 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001453 if (next.app != null && next.app.thread != null) {
1454 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1455
1456 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001457 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001458
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001459 // schedule launch ticks to collect information about slow apps.
1460 next.startLaunchTickingLocked();
1461
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001462 ActivityRecord lastResumedActivity =
1463 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001464 ActivityState lastState = next.state;
1465
1466 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001467
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001468 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001469 next.state = ActivityState.RESUMED;
1470 mResumedActivity = next;
1471 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001472 mService.addRecentTaskLocked(next.task);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001473 mService.updateLruProcessLocked(next.app, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001474 updateLRUListLocked(next);
1475
1476 // Have the window manager re-evaluate the orientation of
1477 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001478 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001479 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001480 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001481 mService.mConfiguration,
1482 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1483 if (config != null) {
1484 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001485 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001486 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001487 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001488
Craig Mautner525f3d92013-05-07 14:01:50 -07001489 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001490 // The configuration update wasn't able to keep the existing
1491 // instance of the activity, and instead started a new one.
1492 // We should be all done, but let's just make sure our activity
1493 // is still at the top and schedule another run if something
1494 // weird happened.
1495 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001496 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001497 "Activity config changed during resume: " + next
1498 + ", new next: " + nextNext);
1499 if (nextNext != next) {
1500 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001501 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001502 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001503 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001504 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001505 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001506 return true;
1507 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001508 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001509 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001510 }
Craig Mautner58547802013-03-05 08:23:53 -08001511
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001512 try {
1513 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001514 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001515 if (a != null) {
1516 final int N = a.size();
1517 if (!next.finishing && N > 0) {
1518 if (DEBUG_RESULTS) Slog.v(
1519 TAG, "Delivering results to " + next
1520 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001521 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001522 }
1523 }
1524
1525 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001526 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001527 }
1528
1529 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001530 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001531 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001532
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001533 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001534 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001535 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001536 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
1537 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001538 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001539
Craig Mautner0eea92c2013-05-16 13:35:39 -07001540 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001541
Craig Mautnerac6f8432013-07-17 13:24:59 -07001542 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001543 } catch (Exception e) {
1544 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001545 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1546 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001547 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001548 if (lastStack != null) {
1549 lastStack.mResumedActivity = lastResumedActivity;
1550 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001551 Slog.i(TAG, "Restarting because process died: " + next);
1552 if (!next.hasBeenLaunched) {
1553 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001554 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1555 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001556 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001557 next.appToken, next.packageName, next.theme,
1558 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001559 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1560 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001561 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001562 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001563 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001564 return true;
1565 }
1566
1567 // From this point on, if something goes wrong there is no way
1568 // to recover the activity.
1569 try {
1570 next.visible = true;
1571 completeResumeLocked(next);
1572 } catch (Exception e) {
1573 // If any exception gets thrown, toss away this
1574 // activity and try the next one.
1575 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001576 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001577 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001578 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001579 return true;
1580 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001581 next.stopped = false;
1582
1583 } else {
1584 // Whoops, need to restart this activity!
1585 if (!next.hasBeenLaunched) {
1586 next.hasBeenLaunched = true;
1587 } else {
1588 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001589 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001590 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001591 mService.compatibilityInfoForPackageLocked(
1592 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001593 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001594 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001595 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001596 }
1597 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1598 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001599 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
Craig Mautnere79d42682013-04-01 19:01:53 -07001600 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001601 }
1602
Craig Mautnercf910b02013-04-23 11:23:27 -07001603 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001604 return true;
1605 }
1606
Craig Mautnerac6f8432013-07-17 13:24:59 -07001607 private void insertTaskAtTop(TaskRecord task) {
1608 mTaskHistory.remove(task);
1609 // Now put task at top.
1610 int stackNdx = mTaskHistory.size();
1611 if (task.userId != mCurrentUser) {
1612 // Put non-current user tasks below current user tasks.
1613 while (--stackNdx >= 0) {
1614 if (mTaskHistory.get(stackNdx).userId != mCurrentUser) {
1615 break;
1616 }
1617 }
1618 ++stackNdx;
1619 }
1620 mTaskHistory.add(stackNdx, task);
1621 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001622
Craig Mautner8849a5e2013-04-02 16:41:03 -07001623 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001624 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001625 TaskRecord rTask = r.task;
1626 final int taskId = rTask.taskId;
1627 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001628 // Last activity in task had been removed or ActivityManagerService is reusing task.
1629 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001630 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001631 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001632 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001633 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001634 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001635 if (!newTask) {
1636 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001637 boolean startIt = true;
1638 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1639 task = mTaskHistory.get(taskNdx);
1640 if (task == r.task) {
1641 // Here it is! Now, if this is not yet visible to the
1642 // user, then just add it without starting; it will
1643 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001644 if (!startIt) {
1645 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1646 + task, new RuntimeException("here").fillInStackTrace());
1647 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001648 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001649 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1650 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001651 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
1652 r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001653 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001654 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001655 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001656 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001657 return;
1658 }
1659 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001660 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001661 startIt = false;
1662 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001663 }
1664 }
1665
1666 // Place a new activity at top of stack, so it is next to interact
1667 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001668
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001669 // If we are not placing the new activity frontmost, we do not want
1670 // to deliver the onUserLeaving callback to the actual frontmost
1671 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001672 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001673 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001674 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1675 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001676 }
Craig Mautner70a86932013-02-28 22:37:44 -08001677
1678 task = r.task;
1679
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001680 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001681 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001682 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001683 task.addActivityToTop(r);
1684
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001685 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001686 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001687 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001688 // We want to show the starting preview window if we are
1689 // switching to a new task, or the next activity's process is
1690 // not currently running.
1691 boolean showStartingIcon = newTask;
1692 ProcessRecord proc = r.app;
1693 if (proc == null) {
1694 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1695 }
1696 if (proc == null || proc.thread == null) {
1697 showStartingIcon = true;
1698 }
1699 if (DEBUG_TRANSITION) Slog.v(TAG,
1700 "Prepare open transition: starting " + r);
1701 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001702 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001703 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001704 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001705 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001706 ? AppTransition.TRANSIT_TASK_OPEN
1707 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001708 mNoAnimActivities.remove(r);
1709 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001710 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001711 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001712 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001713 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001714 boolean doShow = true;
1715 if (newTask) {
1716 // Even though this activity is starting fresh, we still need
1717 // to reset it to make sure we apply affinities to move any
1718 // existing activities from other tasks in to it.
1719 // If the caller has requested that the target task be
1720 // reset, then do so.
1721 if ((r.intent.getFlags()
1722 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1723 resetTaskIfNeededLocked(r, r);
1724 doShow = topRunningNonDelayedActivityLocked(null) == r;
1725 }
1726 }
1727 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1728 // Figure out if we are transitioning from another activity that is
1729 // "has the same starting icon" as the next one. This allows the
1730 // window manager to keep the previous window it had previously
1731 // created, if it still had one.
1732 ActivityRecord prev = mResumedActivity;
1733 if (prev != null) {
1734 // We don't want to reuse the previous starting preview if:
1735 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001736 if (prev.task != r.task) {
1737 prev = null;
1738 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001739 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001740 else if (prev.nowVisible) {
1741 prev = null;
1742 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001743 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001744 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001745 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001746 mService.compatibilityInfoForPackageLocked(
1747 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001748 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001749 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001750 }
1751 } else {
1752 // If this is the first activity, don't do any fancy animations,
1753 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001754 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001755 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001756 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001757 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001758 }
1759 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001760 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001761 }
1762
1763 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001764 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001765 }
1766 }
1767
Dianne Hackbornbe707852011-11-11 14:32:10 -08001768 final void validateAppTokensLocked() {
1769 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001770 mValidateAppTokens.ensureCapacity(numActivities());
1771 final int numTasks = mTaskHistory.size();
1772 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1773 TaskRecord task = mTaskHistory.get(taskNdx);
1774 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07001775 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08001776 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001777 }
Craig Mautner000f0022013-02-26 15:04:29 -08001778 TaskGroup group = new TaskGroup();
1779 group.taskId = task.taskId;
1780 mValidateAppTokens.add(group);
1781 final int numActivities = activities.size();
1782 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1783 final ActivityRecord r = activities.get(activityNdx);
1784 group.tokens.add(r.appToken);
1785 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001786 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001787 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001788 }
1789
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001790 /**
1791 * Perform a reset of the given task, if needed as part of launching it.
1792 * Returns the new HistoryRecord at the top of the task.
1793 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001794 /**
1795 * Helper method for #resetTaskIfNeededLocked.
1796 * We are inside of the task being reset... we'll either finish this activity, push it out
1797 * for another task, or leave it as-is.
1798 * @param task The task containing the Activity (taskTop) that might be reset.
1799 * @param forceReset
1800 * @return An ActivityOptions that needs to be processed.
1801 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001802 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001803 ActivityOptions topOptions = null;
1804
1805 int replyChainEnd = -1;
1806 boolean canMoveOptions = true;
1807
1808 // We only do this for activities that are not the root of the task (since if we finish
1809 // the root, we may no longer have the task!).
1810 final ArrayList<ActivityRecord> activities = task.mActivities;
1811 final int numActivities = activities.size();
1812 for (int i = numActivities - 1; i > 0; --i ) {
1813 ActivityRecord target = activities.get(i);
1814
1815 final int flags = target.info.flags;
1816 final boolean finishOnTaskLaunch =
1817 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1818 final boolean allowTaskReparenting =
1819 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1820 final boolean clearWhenTaskReset =
1821 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1822
1823 if (!finishOnTaskLaunch
1824 && !clearWhenTaskReset
1825 && target.resultTo != null) {
1826 // If this activity is sending a reply to a previous
1827 // activity, we can't do anything with it now until
1828 // we reach the start of the reply chain.
1829 // XXX note that we are assuming the result is always
1830 // to the previous activity, which is almost always
1831 // the case but we really shouldn't count on.
1832 if (replyChainEnd < 0) {
1833 replyChainEnd = i;
1834 }
1835 } else if (!finishOnTaskLaunch
1836 && !clearWhenTaskReset
1837 && allowTaskReparenting
1838 && target.taskAffinity != null
1839 && !target.taskAffinity.equals(task.affinity)) {
1840 // If this activity has an affinity for another
1841 // task, then we need to move it out of here. We will
1842 // move it as far out of the way as possible, to the
1843 // bottom of the activity stack. This also keeps it
1844 // correctly ordered with any activities we previously
1845 // moved.
Craig Mautnerdccb7702013-09-17 15:53:34 -07001846 final ActivityRecord bottom =
1847 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
1848 mTaskHistory.get(0).mActivities.get(0) : null;
1849 if (bottom != null && target.taskAffinity != null
1850 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001851 // If the activity currently at the bottom has the
1852 // same task affinity as the one we are moving,
1853 // then merge it into the same task.
Craig Mautnerdccb7702013-09-17 15:53:34 -07001854 target.setTask(bottom.task, bottom.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001855 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07001856 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001857 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001858 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001859 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001860 target.task.affinityIntent = target.intent;
1861 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1862 + " out to new task " + target.task);
1863 }
1864
1865 final TaskRecord targetTask = target.task;
1866 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001867 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001868
Craig Mautner525f3d92013-05-07 14:01:50 -07001869 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001870 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1871 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07001872 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001873 if (p.finishing) {
1874 continue;
1875 }
1876
Craig Mautner525f3d92013-05-07 14:01:50 -07001877 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001878 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001879 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001880 topOptions = p.takeOptionsLocked();
1881 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001882 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001883 }
1884 }
1885 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1886 + task + " adding to task=" + targetTask,
1887 new RuntimeException("here").fillInStackTrace());
1888 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1889 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001890 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001891 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001892
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001893 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001894 }
1895
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001896 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001897 if (VALIDATE_TOKENS) {
1898 validateAppTokensLocked();
1899 }
1900
1901 replyChainEnd = -1;
1902 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1903 // If the activity should just be removed -- either
1904 // because it asks for it, or the task should be
1905 // cleared -- then finish it and anything that is
1906 // part of its reply chain.
1907 int end;
1908 if (clearWhenTaskReset) {
1909 // In this case, we want to finish this activity
1910 // and everything above it, so be sneaky and pretend
1911 // like these are all in the reply chain.
1912 end = numActivities - 1;
1913 } else if (replyChainEnd < 0) {
1914 end = i;
1915 } else {
1916 end = replyChainEnd;
1917 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001918 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001919 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001920 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001921 if (p.finishing) {
1922 continue;
1923 }
1924 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001925 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001926 topOptions = p.takeOptionsLocked();
1927 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001928 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001929 }
1930 }
Craig Mautner58547802013-03-05 08:23:53 -08001931 if (DEBUG_TASKS) Slog.w(TAG,
1932 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001933 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001934 end--;
1935 srcPos--;
1936 }
1937 }
1938 replyChainEnd = -1;
1939 } else {
1940 // If we were in the middle of a chain, well the
1941 // activity that started it all doesn't want anything
1942 // special, so leave it all as-is.
1943 replyChainEnd = -1;
1944 }
1945 }
1946
1947 return topOptions;
1948 }
1949
1950 /**
1951 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1952 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1953 * @param affinityTask The task we are looking for an affinity to.
1954 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1955 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1956 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1957 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001958 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08001959 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001960 int replyChainEnd = -1;
1961 final int taskId = task.taskId;
1962 final String taskAffinity = task.affinity;
1963
1964 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1965 final int numActivities = activities.size();
1966 // Do not operate on the root Activity.
1967 for (int i = numActivities - 1; i > 0; --i) {
1968 ActivityRecord target = activities.get(i);
1969
1970 final int flags = target.info.flags;
1971 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1972 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1973
1974 if (target.resultTo != null) {
1975 // If this activity is sending a reply to a previous
1976 // activity, we can't do anything with it now until
1977 // we reach the start of the reply chain.
1978 // XXX note that we are assuming the result is always
1979 // to the previous activity, which is almost always
1980 // the case but we really shouldn't count on.
1981 if (replyChainEnd < 0) {
1982 replyChainEnd = i;
1983 }
1984 } else if (topTaskIsHigher
1985 && allowTaskReparenting
1986 && taskAffinity != null
1987 && taskAffinity.equals(target.taskAffinity)) {
1988 // This activity has an affinity for our task. Either remove it if we are
1989 // clearing or move it over to our task. Note that
1990 // we currently punt on the case where we are resetting a
1991 // task that is not at the top but who has activities above
1992 // with an affinity to it... this is really not a normal
1993 // case, and we will need to later pull that task to the front
1994 // and usually at that point we will do the reset and pick
1995 // up those remaining activities. (This only happens if
1996 // someone starts an activity in a new task from an activity
1997 // in a task that is not currently on top.)
1998 if (forceReset || finishOnTaskLaunch) {
1999 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2000 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2001 for (int srcPos = start; srcPos >= i; --srcPos) {
2002 final ActivityRecord p = activities.get(srcPos);
2003 if (p.finishing) {
2004 continue;
2005 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002006 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002007 }
2008 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002009 if (taskInsertionPoint < 0) {
2010 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002011
Craig Mautner77878772013-03-04 19:46:24 -08002012 }
Craig Mautner77878772013-03-04 19:46:24 -08002013
2014 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2015 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2016 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2017 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002018 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002019 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002020 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002021
Craig Mautnere3a74d52013-02-22 14:14:58 -08002022 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2023 + " to stack at " + task,
2024 new RuntimeException("here").fillInStackTrace());
2025 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2026 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002027 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002028 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002029 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002030 if (VALIDATE_TOKENS) {
2031 validateAppTokensLocked();
2032 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002033
2034 // Now we've moved it in to place... but what if this is
2035 // a singleTop activity and we have put it on top of another
2036 // instance of the same activity? Then we drop the instance
2037 // below so it remains singleTop.
2038 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2039 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002040 int targetNdx = taskActivities.indexOf(target);
2041 if (targetNdx > 0) {
2042 ActivityRecord p = taskActivities.get(targetNdx - 1);
2043 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002044 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2045 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002046 }
2047 }
2048 }
2049 }
2050
2051 replyChainEnd = -1;
2052 }
2053 }
Craig Mautner77878772013-03-04 19:46:24 -08002054 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002055 }
2056
Craig Mautner8849a5e2013-04-02 16:41:03 -07002057 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002058 ActivityRecord newActivity) {
2059 boolean forceReset =
2060 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2061 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2062 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2063 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2064 forceReset = true;
2065 }
2066 }
2067
2068 final TaskRecord task = taskTop.task;
2069
2070 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2071 * for remaining tasks. Used for later tasks to reparent to task. */
2072 boolean taskFound = false;
2073
2074 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2075 ActivityOptions topOptions = null;
2076
Craig Mautner77878772013-03-04 19:46:24 -08002077 // Preserve the location for reparenting in the new task.
2078 int reparentInsertionPoint = -1;
2079
Craig Mautnere3a74d52013-02-22 14:14:58 -08002080 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2081 final TaskRecord targetTask = mTaskHistory.get(i);
2082
2083 if (targetTask == task) {
2084 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2085 taskFound = true;
2086 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002087 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2088 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002089 }
2090 }
2091
Craig Mautner70a86932013-02-28 22:37:44 -08002092 int taskNdx = mTaskHistory.indexOf(task);
2093 do {
2094 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2095 } while (taskTop == null && taskNdx >= 0);
2096
Craig Mautnere3a74d52013-02-22 14:14:58 -08002097 if (topOptions != null) {
2098 // If we got some ActivityOptions from an activity on top that
2099 // was removed from the task, propagate them to the new real top.
2100 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002101 taskTop.updateOptionsLocked(topOptions);
2102 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002103 topOptions.abort();
2104 }
2105 }
2106
2107 return taskTop;
2108 }
2109
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002110 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2111 String resultWho, int requestCode, int resultCode, Intent data) {
2112
2113 if (callingUid > 0) {
2114 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002115 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002116 }
2117
2118 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2119 + " : who=" + resultWho + " req=" + requestCode
2120 + " res=" + resultCode + " data=" + data);
2121 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2122 try {
2123 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2124 list.add(new ResultInfo(resultWho, requestCode,
2125 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002126 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002127 return;
2128 } catch (Exception e) {
2129 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2130 }
2131 }
2132
2133 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2134 }
2135
Craig Mautnerf3333272013-04-22 10:55:53 -07002136 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002137 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2138 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2139 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2140 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002141 if (!mService.mSleeping) {
2142 if (DEBUG_STATES) {
2143 Slog.d(TAG, "no-history finish of " + r);
2144 }
2145 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002146 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002147 } else {
2148 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2149 + " on stop because we're just sleeping");
2150 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002151 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002152 }
2153
2154 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002155 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002156 if (mService.mFocusedActivity == r) {
2157 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2158 }
2159 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002160 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002161 try {
2162 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002163 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2164 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002165 r.state = ActivityState.STOPPING;
2166 if (DEBUG_VISBILITY) Slog.v(
2167 TAG, "Stopping visible=" + r.visible + " for " + r);
2168 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002169 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002170 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002171 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002172 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002173 r.setSleeping(true);
2174 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002175 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002176 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002177 } catch (Exception e) {
2178 // Maybe just ignore exceptions here... if the process
2179 // has crashed, our death notification will clean things
2180 // up.
2181 Slog.w(TAG, "Exception thrown during pause", e);
2182 // Just in case, assume it to be stopped.
2183 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002184 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002185 r.state = ActivityState.STOPPED;
2186 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002187 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002188 }
2189 }
2190 }
2191 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002192
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002193 /**
2194 * @return Returns true if the activity is being finished, false if for
2195 * some reason it is being left as-is.
2196 */
2197 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002198 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002199 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002200 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002201 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002202 + ", result=" + resultCode + ", data=" + resultData
2203 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002204 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002205 return false;
2206 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002207
Craig Mautnerd44711d2013-02-23 11:24:36 -08002208 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002209 return true;
2210 }
2211
Craig Mautnerd2328952013-03-05 12:46:26 -08002212 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002213 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2214 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2215 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2216 ActivityRecord r = activities.get(activityNdx);
2217 if (r.resultTo == self && r.requestCode == requestCode) {
2218 if ((r.resultWho == null && resultWho == null) ||
2219 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2220 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2221 false);
2222 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002223 }
2224 }
2225 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002226 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002227 }
2228
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002229 final void finishTopRunningActivityLocked(ProcessRecord app) {
2230 ActivityRecord r = topRunningActivityLocked(null);
2231 if (r != null && r.app == app) {
2232 // If the top running activity is from this crashing
2233 // process, then terminate it to avoid getting in a loop.
2234 Slog.w(TAG, " Force finishing activity "
2235 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002236 int taskNdx = mTaskHistory.indexOf(r.task);
2237 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002238 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002239 // Also terminate any activities below it that aren't yet
2240 // stopped, to avoid a situation where one will get
2241 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002242 --activityNdx;
2243 if (activityNdx < 0) {
2244 do {
2245 --taskNdx;
2246 if (taskNdx < 0) {
2247 break;
2248 }
2249 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2250 } while (activityNdx < 0);
2251 }
2252 if (activityNdx >= 0) {
2253 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002254 if (r.state == ActivityState.RESUMED
2255 || r.state == ActivityState.PAUSING
2256 || r.state == ActivityState.PAUSED) {
Craig Mautnere428a7f2013-08-17 17:04:56 -07002257 if (!r.isHomeActivity() || !mService.mHomeProcess.contains(r.app)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002258 Slog.w(TAG, " Force finishing activity "
2259 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002260 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002261 }
2262 }
2263 }
2264 }
2265 }
2266
Craig Mautnerd2328952013-03-05 12:46:26 -08002267 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002268 ArrayList<ActivityRecord> activities = r.task.mActivities;
2269 for (int index = activities.indexOf(r); index >= 0; --index) {
2270 ActivityRecord cur = activities.get(index);
2271 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002272 break;
2273 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002274 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002275 }
2276 return true;
2277 }
2278
Dianne Hackborn5c607432012-02-28 14:44:19 -08002279 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2280 // send the result
2281 ActivityRecord resultTo = r.resultTo;
2282 if (resultTo != null) {
2283 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2284 + " who=" + r.resultWho + " req=" + r.requestCode
2285 + " res=" + resultCode + " data=" + resultData);
2286 if (r.info.applicationInfo.uid > 0) {
2287 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2288 resultTo.packageName, resultData,
2289 resultTo.getUriPermissionsLocked());
2290 }
2291 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2292 resultData);
2293 r.resultTo = null;
2294 }
2295 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2296
2297 // Make sure this HistoryRecord is not holding on to other resources,
2298 // because clients have remote IPC references to this object so we
2299 // can't assume that will go away and want to avoid circular IPC refs.
2300 r.results = null;
2301 r.pendingResults = null;
2302 r.newIntents = null;
2303 r.icicle = null;
2304 }
2305
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002306 /**
2307 * @return Returns true if this activity has been removed from the history
2308 * list, or false if it is still in the list and will be removed later.
2309 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002310 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2311 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002312 if (r.finishing) {
2313 Slog.w(TAG, "Duplicate finish request for " + r);
2314 return false;
2315 }
2316
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002317 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002318 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002319 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002320 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002321 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2322 final int index = activities.indexOf(r);
2323 if (index < (activities.size() - 1)) {
2324 ActivityRecord next = activities.get(index+1);
2325 if (r.frontOfTask) {
2326 // The next activity is now the front of the task.
2327 next.frontOfTask = true;
2328 }
2329 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2330 // If the caller asked that this activity (and all above it)
2331 // be cleared when the task is reset, don't lose that information,
2332 // but propagate it up to the next activity.
2333 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002334 }
2335 }
2336
2337 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002338 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002339 if (mService.mFocusedActivity == r) {
Craig Mautner29219d92013-04-16 20:19:12 -07002340 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002341 }
2342 }
2343
Dianne Hackborn5c607432012-02-28 14:44:19 -08002344 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002345
Craig Mautnerc8143c62013-09-03 12:15:57 -07002346 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002347 // There are clients waiting to receive thumbnails so, in case
2348 // this is an activity that someone is waiting for, add it
2349 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002350 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002351 }
2352
Craig Mautnerde4ef022013-04-07 19:01:33 -07002353 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002354 boolean endTask = index <= 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002355 if (DEBUG_TRANSITION) Slog.v(TAG,
2356 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002357 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002358 ? AppTransition.TRANSIT_TASK_CLOSE
2359 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002360
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002361 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002362 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002363
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002364 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002365 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2366 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2367 startPausingLocked(false, false);
2368 }
2369
2370 } else if (r.state != ActivityState.PAUSING) {
2371 // If the activity is PAUSING, we will complete the finish once
2372 // it is done pausing; else we can just directly finish it here.
2373 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002374 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002375 } else {
2376 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2377 }
2378
2379 return false;
2380 }
2381
Craig Mautnerf3333272013-04-22 10:55:53 -07002382 static final int FINISH_IMMEDIATELY = 0;
2383 static final int FINISH_AFTER_PAUSE = 1;
2384 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002385
Craig Mautnerf3333272013-04-22 10:55:53 -07002386 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002387 // First things first: if this activity is currently visible,
2388 // and the resumed activity is not yet visible, then hold off on
2389 // finishing until the resumed one becomes visible.
2390 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002391 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2392 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002393 if (mStackSupervisor.mStoppingActivities.size() > 3
2394 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002395 // If we already have a few activities waiting to stop,
2396 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002397 // them out. Or if r is the last of activity of the last task the stack
2398 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002399 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002400 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002401 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002402 }
2403 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002404 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2405 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002406 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002407 if (oomAdj) {
2408 mService.updateOomAdjLocked();
2409 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002410 return r;
2411 }
2412
2413 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002414 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002415 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002416 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002417 if (mResumedActivity == r) {
2418 mResumedActivity = null;
2419 }
2420 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002421 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002422 r.state = ActivityState.FINISHING;
2423
2424 if (mode == FINISH_IMMEDIATELY
2425 || prevState == ActivityState.STOPPED
2426 || prevState == ActivityState.INITIALIZING) {
2427 // If this activity is already stopped, we can just finish
2428 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002429 boolean activityRemoved = destroyActivityLocked(r, true,
2430 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002431 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002432 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002433 }
2434 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002435 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002436
2437 // Need to go through the full pause cycle to get this
2438 // activity into the stopped state and then finish it.
2439 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002440 mStackSupervisor.mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002441 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002442 return r;
2443 }
2444
Craig Mautnerd2328952013-03-05 12:46:26 -08002445 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002446 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002447 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002448 final TaskRecord task = srec.task;
2449 final ArrayList<ActivityRecord> activities = task.mActivities;
2450 final int start = activities.indexOf(srec);
2451 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002452 return false;
2453 }
2454 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002455 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002456 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002457 final ComponentName dest = destIntent.getComponent();
2458 if (start > 0 && dest != null) {
2459 for (int i = finishTo; i >= 0; i--) {
2460 ActivityRecord r = activities.get(i);
2461 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002462 r.info.name.equals(dest.getClassName())) {
2463 finishTo = i;
2464 parent = r;
2465 foundParentInTask = true;
2466 break;
2467 }
2468 }
2469 }
2470
2471 IActivityController controller = mService.mController;
2472 if (controller != null) {
2473 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2474 if (next != null) {
2475 // ask watcher if this is allowed
2476 boolean resumeOK = true;
2477 try {
2478 resumeOK = controller.activityResuming(next.packageName);
2479 } catch (RemoteException e) {
2480 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002481 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002482 }
2483
2484 if (!resumeOK) {
2485 return false;
2486 }
2487 }
2488 }
2489 final long origId = Binder.clearCallingIdentity();
2490 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002491 ActivityRecord r = activities.get(i);
2492 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002493 // Only return the supplied result for the first activity finished
2494 resultCode = Activity.RESULT_CANCELED;
2495 resultData = null;
2496 }
2497
2498 if (parent != null && foundParentInTask) {
2499 final int parentLaunchMode = parent.info.launchMode;
2500 final int destIntentFlags = destIntent.getFlags();
2501 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2502 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2503 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2504 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2505 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2506 } else {
2507 try {
2508 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2509 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002510 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002511 null, aInfo, parent.appToken, null,
2512 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2513 0, null, true, null);
2514 foundParentInTask = res == ActivityManager.START_SUCCESS;
2515 } catch (RemoteException e) {
2516 foundParentInTask = false;
2517 }
2518 requestFinishActivityLocked(parent.appToken, resultCode,
2519 resultData, "navigate-up", true);
2520 }
2521 }
2522 Binder.restoreCallingIdentity(origId);
2523 return foundParentInTask;
2524 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002525 /**
2526 * Perform the common clean-up of an activity record. This is called both
2527 * as part of destroyActivityLocked() (when destroying the client-side
2528 * representation) and cleaning things up as a result of its hosting
2529 * processing going away, in which case there is no remaining client-side
2530 * state to destroy so only the cleanup here is needed.
2531 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002532 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2533 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002534 if (mResumedActivity == r) {
2535 mResumedActivity = null;
2536 }
2537 if (mService.mFocusedActivity == r) {
2538 mService.mFocusedActivity = null;
2539 }
2540
2541 r.configDestroy = false;
2542 r.frozenBeforeDestroy = false;
2543
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002544 if (setState) {
2545 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2546 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002547 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002548 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002549 }
2550
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002551 // Make sure this record is no longer in the pending finishes list.
2552 // This could happen, for example, if we are trimming activities
2553 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002554 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002555 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002556
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002557 // Remove any pending results.
2558 if (r.finishing && r.pendingResults != null) {
2559 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2560 PendingIntentRecord rec = apr.get();
2561 if (rec != null) {
2562 mService.cancelIntentSenderLocked(rec, false);
2563 }
2564 }
2565 r.pendingResults = null;
2566 }
2567
2568 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002569 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002570 }
2571
Craig Mautnerc8143c62013-09-03 12:15:57 -07002572 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002573 // There are clients waiting to receive thumbnails so, in case
2574 // this is an activity that someone is waiting for, add it
2575 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002576 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002577 }
2578
2579 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002580 removeTimeoutsForActivityLocked(r);
2581 }
2582
2583 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002584 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002585 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002586 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002587 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002588 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002589 }
2590
Dianne Hackborn5c607432012-02-28 14:44:19 -08002591 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002592 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2593 r.makeFinishing();
2594 if (DEBUG_ADD_REMOVE) {
2595 RuntimeException here = new RuntimeException("here");
2596 here.fillInStackTrace();
2597 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002598 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002599 final TaskRecord task = r.task;
2600 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002601 if (DEBUG_STACK) Slog.i(TAG,
2602 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002603 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002604 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002605 r.takeFromHistory();
2606 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002607 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002608 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002609 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002610 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002611 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002612 if (VALIDATE_TOKENS) {
2613 validateAppTokensLocked();
2614 }
2615 cleanUpActivityServicesLocked(r);
2616 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002617 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002618
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002619 /**
2620 * Perform clean-up of service connections in an activity record.
2621 */
2622 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2623 // Throw away any services that have been bound by this activity.
2624 if (r.connections != null) {
2625 Iterator<ConnectionRecord> it = r.connections.iterator();
2626 while (it.hasNext()) {
2627 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002628 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002629 }
2630 r.connections = null;
2631 }
2632 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002633
2634 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2635 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2636 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2637 mHandler.sendMessage(msg);
2638 }
2639
Dianne Hackborn28695e02011-11-02 21:59:51 -07002640 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002641 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002642 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002643 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2644 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2645 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2646 final ActivityRecord r = activities.get(activityNdx);
2647 if (r.finishing) {
2648 continue;
2649 }
2650 if (r.fullscreen) {
2651 lastIsOpaque = true;
2652 }
2653 if (owner != null && r.app != owner) {
2654 continue;
2655 }
2656 if (!lastIsOpaque) {
2657 continue;
2658 }
2659 // We can destroy this one if we have its icicle saved and
2660 // it is not in the process of pausing/stopping/finishing.
2661 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2662 && r.haveState && !r.visible && r.stopped
2663 && r.state != ActivityState.DESTROYING
2664 && r.state != ActivityState.DESTROYED) {
2665 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2666 + " resumed=" + mResumedActivity
2667 + " pausing=" + mPausingActivity);
2668 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2669 activityRemoved = true;
2670 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002671 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002672 }
2673 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002674 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002675 mStackSupervisor.resumeTopActivitiesLocked();
2676
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002677 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002678 }
2679
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002680 /**
2681 * Destroy the current CLIENT SIDE instance of an activity. This may be
2682 * called both when actually finishing an activity, or when performing
2683 * a configuration switch where we destroy the current client-side object
2684 * but then create a new client-side object for this same HistoryRecord.
2685 */
2686 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002687 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002688 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002689 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002690 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2691 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002692 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002693 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002694
2695 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002696
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002697 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002698
2699 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002700
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002701 if (hadApp) {
2702 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002703 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002704 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2705 mService.mHeavyWeightProcess = null;
2706 mService.mHandler.sendEmptyMessage(
2707 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2708 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07002709 if (r.app.activities.isEmpty()) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002710 // No longer have activities, so update oom adj.
2711 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002712 }
2713 }
2714
2715 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002716
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002717 try {
2718 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002719 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002720 r.configChangeFlags);
2721 } catch (Exception e) {
2722 // We can just ignore exceptions here... if the process
2723 // has crashed, our death notification will clean things
2724 // up.
2725 //Slog.w(TAG, "Exception thrown during finish", e);
2726 if (r.finishing) {
2727 removeActivityFromHistoryLocked(r);
2728 removedFromHistory = true;
2729 skipDestroy = true;
2730 }
2731 }
2732
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002733 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002734
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002735 // If the activity is finishing, we need to wait on removing it
2736 // from the list to give it a chance to do its cleanup. During
2737 // that time it may make calls back with its token so we need to
2738 // be able to find it on the list and so we don't want to remove
2739 // it from the list yet. Otherwise, we can just immediately put
2740 // it in the destroyed state since we are not removing it from the
2741 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002742 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002743 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2744 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002745 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002746 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002747 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2748 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002749 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002750 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002751 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002752 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002753 }
2754 } else {
2755 // remove this record from the history.
2756 if (r.finishing) {
2757 removeActivityFromHistoryLocked(r);
2758 removedFromHistory = true;
2759 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002760 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002761 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002762 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002763 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002764 }
2765 }
2766
2767 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002768
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002769 if (!mLRUActivities.remove(r) && hadApp) {
2770 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2771 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002772
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002773 return removedFromHistory;
2774 }
2775
Craig Mautnerd2328952013-03-05 12:46:26 -08002776 final void activityDestroyedLocked(IBinder token) {
2777 final long origId = Binder.clearCallingIdentity();
2778 try {
2779 ActivityRecord r = ActivityRecord.forToken(token);
2780 if (r != null) {
2781 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002782 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002783
2784 if (isInStackLocked(token) != null) {
2785 if (r.state == ActivityState.DESTROYING) {
2786 cleanUpActivityLocked(r, true, false);
2787 removeActivityFromHistoryLocked(r);
2788 }
2789 }
Craig Mautner05d29032013-05-03 13:40:13 -07002790 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002791 } finally {
2792 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002793 }
2794 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002795
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002796 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2797 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002798 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002799 if (DEBUG_CLEANUP) Slog.v(
2800 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002801 + " with " + i + " entries");
2802 while (i > 0) {
2803 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002804 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002805 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002806 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002807 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002808 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002809 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002810 }
2811 }
2812 }
2813
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002814 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2815 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002816 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2817 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002818 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2819 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002820 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002821 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002822 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2823 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002824
2825 boolean hasVisibleActivities = false;
2826
2827 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002828 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002829 if (DEBUG_CLEANUP) Slog.v(
2830 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002831 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2832 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2833 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2834 final ActivityRecord r = activities.get(activityNdx);
2835 --i;
2836 if (DEBUG_CLEANUP) Slog.v(
2837 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2838 if (r.app == app) {
2839 boolean remove;
2840 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2841 // Don't currently have state for the activity, or
2842 // it is finishing -- always remove it.
2843 remove = true;
2844 } else if (r.launchCount > 2 &&
2845 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2846 // We have launched this activity too many times since it was
2847 // able to run, so give up and remove it.
2848 remove = true;
2849 } else {
2850 // The process may be gone, but the activity lives on!
2851 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002852 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002853 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002854 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002855 RuntimeException here = new RuntimeException("here");
2856 here.fillInStackTrace();
2857 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2858 + ": haveState=" + r.haveState
2859 + " stateNotNeeded=" + r.stateNotNeeded
2860 + " finishing=" + r.finishing
2861 + " state=" + r.state, here);
2862 }
2863 if (!r.finishing) {
2864 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2865 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2866 r.userId, System.identityHashCode(r),
2867 r.task.taskId, r.shortComponentName,
2868 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07002869 if (r.state == ActivityState.RESUMED) {
2870 mService.updateUsageStats(r, false);
2871 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002872 }
2873 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002874
Craig Mautner0247fc82013-02-28 14:32:06 -08002875 } else {
2876 // We have the current state for this activity, so
2877 // it can be restarted later when needed.
2878 if (localLOGV) Slog.v(
2879 TAG, "Keeping entry, setting app to null");
2880 if (r.visible) {
2881 hasVisibleActivities = true;
2882 }
2883 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2884 + r);
2885 r.app = null;
2886 r.nowVisible = false;
2887 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002888 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08002889 "App died, clearing saved state of " + r);
2890 r.icicle = null;
2891 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002892 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002893
Craig Mautnerd2328952013-03-05 12:46:26 -08002894 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002895 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002896 }
2897 }
2898
2899 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002900 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002901
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002902 final void updateTransitLocked(int transit, Bundle options) {
2903 if (options != null) {
2904 ActivityRecord r = topRunningActivityLocked(null);
2905 if (r != null && r.state != ActivityState.RESUMED) {
2906 r.updateOptionsLocked(options);
2907 } else {
2908 ActivityOptions.abort(options);
2909 }
2910 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002911 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002912 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002913
Craig Mautnera82aa092013-09-13 15:34:08 -07002914 void moveHomeTaskToTop() {
2915 final int top = mTaskHistory.size() - 1;
2916 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
2917 final TaskRecord task = mTaskHistory.get(taskNdx);
2918 if (task.isHomeTask()) {
2919 mTaskHistory.remove(taskNdx);
2920 mTaskHistory.add(top, task);
2921 mWindowManager.moveTaskToTop(task.taskId);
2922 return;
2923 }
2924 }
2925 }
2926
Craig Mautnercae015f2013-02-08 14:31:27 -08002927 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002928 final TaskRecord task = taskForIdLocked(taskId);
2929 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08002930 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002931 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002932 }
Craig Mautneraab647e2013-02-28 16:31:36 -08002933 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2934 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07002935 // we'll just indicate that this task returns to the home task.
Craig Mautnerae7ecab2013-09-18 11:48:14 -07002936 task.mOnTopOfHome = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08002937 }
2938 moveTaskToFrontLocked(task, null, options);
2939 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002940 }
2941 return false;
2942 }
2943
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002944 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002945 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002946
Craig Mautner11bf9a52013-02-19 14:08:51 -08002947 final int numTasks = mTaskHistory.size();
2948 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07002949 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002950 // nothing to do!
2951 if (reason != null &&
2952 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2953 ActivityOptions.abort(options);
2954 } else {
2955 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
2956 }
2957 return;
2958 }
2959
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07002960 mStackSupervisor.moveHomeStack(isHomeStack());
2961
Craig Mautner11bf9a52013-02-19 14:08:51 -08002962 // Shift all activities with this task up to the top
2963 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07002964 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08002965
2966 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002967 if (reason != null &&
2968 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002969 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002970 ActivityRecord r = topRunningActivityLocked(null);
2971 if (r != null) {
2972 mNoAnimActivities.add(r);
2973 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002974 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002975 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08002976 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002977 }
Craig Mautner30e2d722013-02-12 11:30:16 -08002978
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002979 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002980
Craig Mautner05d29032013-05-03 13:40:13 -07002981 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08002982 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08002983
2984 if (VALIDATE_TOKENS) {
2985 validateAppTokensLocked();
2986 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002987 }
2988
2989 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002990 * Worker method for rearranging history stack. Implements the function of moving all
2991 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002992 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002993 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002994 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
2995 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002996 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07002997 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002998 * @return Returns true if the move completed, false if not.
2999 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003000 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
3001 Slog.i(TAG, "moveTaskToBack: " + taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003002
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003003 // If we have a watcher, preflight the move before committing to it. First check
3004 // for *other* available tasks, but if none are available, then try again allowing the
3005 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003006 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003007 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003008 if (next == null) {
3009 next = topRunningActivityLocked(null, 0);
3010 }
3011 if (next != null) {
3012 // ask watcher if this is allowed
3013 boolean moveOK = true;
3014 try {
3015 moveOK = mService.mController.activityResuming(next.packageName);
3016 } catch (RemoteException e) {
3017 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003018 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003019 }
3020 if (!moveOK) {
3021 return false;
3022 }
3023 }
3024 }
3025
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003026 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003027 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003028
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003029 final TaskRecord tr = taskForIdLocked(taskId);
Craig Mautnerd2328952013-03-05 12:46:26 -08003030 if (tr == null) {
3031 return false;
3032 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003033
Craig Mautner11bf9a52013-02-19 14:08:51 -08003034 mTaskHistory.remove(tr);
3035 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003036
Craig Mautnerc8143c62013-09-03 12:15:57 -07003037 // There is an assumption that moving a task to the back moves it behind the home activity.
3038 // We make sure here that some activity in the stack will launch home.
3039 ActivityRecord lastActivity = null;
3040 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003041 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3042 final TaskRecord task = mTaskHistory.get(taskNdx);
3043 if (task.mOnTopOfHome) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003044 break;
3045 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003046 if (taskNdx == 1) {
3047 // Set the last task before tr to go to home.
3048 task.mOnTopOfHome = true;
3049 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003050 }
3051
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003052 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003053 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3054 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003055 ActivityRecord r = topRunningActivityLocked(null);
3056 if (r != null) {
3057 mNoAnimActivities.add(r);
3058 }
3059 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003060 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003061 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003062 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003063
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003064 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003065 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003066 }
3067
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003068 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
3069 if (task == tr && task.mOnTopOfHome || numTasks <= 1) {
3070 task.mOnTopOfHome = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003071 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003072 }
3073
Craig Mautner05d29032013-05-03 13:40:13 -07003074 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003075 return true;
3076 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003077
Craig Mautner8849a5e2013-04-02 16:41:03 -07003078 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003079 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003080 final Uri data = r.intent.getData();
3081 final String strData = data != null ? data.toSafeString() : null;
3082
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003083 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003084 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003085 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003086 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003087 }
3088
3089 /**
3090 * Make sure the given activity matches the current configuration. Returns
3091 * false if the activity had to be destroyed. Returns true if the
3092 * configuration is the same, or the activity will remain running as-is
3093 * for whatever reason. Ensures the HistoryRecord is updated with the
3094 * correct configuration and all other bookkeeping is handled.
3095 */
3096 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3097 int globalChanges) {
3098 if (mConfigWillChange) {
3099 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3100 "Skipping config check (will change): " + r);
3101 return true;
3102 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003103
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003104 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3105 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003106
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003107 // Short circuit: if the two configurations are the exact same
3108 // object (the common case), then there is nothing to do.
3109 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003110 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003111 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3112 "Configuration unchanged in " + r);
3113 return true;
3114 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003115
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003116 // We don't worry about activities that are finishing.
3117 if (r.finishing) {
3118 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3119 "Configuration doesn't matter in finishing " + r);
3120 r.stopFreezingScreenLocked(false);
3121 return true;
3122 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003123
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003124 // Okay we now are going to make this activity have the new config.
3125 // But then we need to figure out how it needs to deal with that.
3126 Configuration oldConfig = r.configuration;
3127 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003128
3129 // Determine what has changed. May be nothing, if this is a config
3130 // that has come back from the app after going idle. In that case
3131 // we just want to leave the official config object now in the
3132 // activity and do nothing else.
3133 final int changes = oldConfig.diff(newConfig);
3134 if (changes == 0 && !r.forceNewConfig) {
3135 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3136 "Configuration no differences in " + r);
3137 return true;
3138 }
3139
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003140 // If the activity isn't currently running, just leave the new
3141 // configuration and it will pick that up next time it starts.
3142 if (r.app == null || r.app.thread == null) {
3143 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3144 "Configuration doesn't matter not running " + r);
3145 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003146 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003147 return true;
3148 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003149
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003150 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003151 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3152 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3153 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003154 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003155 + ", newConfig=" + newConfig);
3156 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003157 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003158 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3159 r.configChangeFlags |= changes;
3160 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003161 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003162 if (r.app == null || r.app.thread == null) {
3163 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003164 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003165 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003166 } else if (r.state == ActivityState.PAUSING) {
3167 // A little annoying: we are waiting for this activity to
3168 // finish pausing. Let's not do anything now, but just
3169 // flag that it needs to be restarted when done pausing.
3170 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003171 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003172 r.configDestroy = true;
3173 return true;
3174 } else if (r.state == ActivityState.RESUMED) {
3175 // Try to optimize this case: the configuration is changing
3176 // and we need to restart the top, resumed activity.
3177 // Instead of doing the normal handshaking, just say
3178 // "restart!".
3179 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003180 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003181 relaunchActivityLocked(r, r.configChangeFlags, true);
3182 r.configChangeFlags = 0;
3183 } else {
3184 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003185 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003186 relaunchActivityLocked(r, r.configChangeFlags, false);
3187 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003188 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003189
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003190 // All done... tell the caller we weren't able to keep this
3191 // activity around.
3192 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003193 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003194
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003195 // Default case: the activity can handle this new configuration, so
3196 // hand it over. Note that we don't need to give it the new
3197 // configuration, since we always send configuration changes to all
3198 // process when they happen so it can just use whatever configuration
3199 // it last got.
3200 if (r.app != null && r.app.thread != null) {
3201 try {
3202 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003203 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003204 } catch (RemoteException e) {
3205 // If process died, whatever.
3206 }
3207 }
3208 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003209
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003210 return true;
3211 }
3212
Craig Mautnerc8143c62013-09-03 12:15:57 -07003213 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003214 int changes, boolean andResume) {
3215 List<ResultInfo> results = null;
3216 List<Intent> newIntents = null;
3217 if (andResume) {
3218 results = r.results;
3219 newIntents = r.newIntents;
3220 }
3221 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3222 + " with results=" + results + " newIntents=" + newIntents
3223 + " andResume=" + andResume);
3224 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003225 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003226 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003227
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003228 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003229
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003230 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003231 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3232 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3233 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003234 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003235 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003236 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003237 // Note: don't need to call pauseIfSleepingLocked() here, because
3238 // the caller will only pass in 'andResume' if this activity is
3239 // currently resumed, which implies we aren't sleeping.
3240 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003241 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003242 }
3243
3244 if (andResume) {
3245 r.results = null;
3246 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003247 r.state = ActivityState.RESUMED;
3248 } else {
3249 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3250 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003251 }
3252
3253 return true;
3254 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003255
3256 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003257 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3258 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3259 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3260 final ActivityRecord r = activities.get(activityNdx);
3261 if (r.appToken == token) {
3262 return true;
3263 }
3264 if (r.fullscreen && !r.finishing) {
3265 return false;
3266 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003267 }
3268 }
3269 return true;
3270 }
3271
3272 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003273 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3274 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3275 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3276 final ActivityRecord r = activities.get(activityNdx);
3277 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003278 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003279 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003280 }
3281 }
3282 }
3283
3284 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3285 boolean didSomething = false;
3286 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003287 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3288 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3289 int numActivities = activities.size();
3290 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3291 ActivityRecord r = activities.get(activityNdx);
3292 final boolean samePackage = r.packageName.equals(name)
3293 || (name == null && r.userId == userId);
3294 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3295 && (samePackage || r.task == lastTask)
3296 && (r.app == null || evenPersistent || !r.app.persistent)) {
3297 if (!doit) {
3298 if (r.finishing) {
3299 // If this activity is just finishing, then it is not
3300 // interesting as far as something to stop.
3301 continue;
3302 }
3303 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003304 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003305 didSomething = true;
3306 Slog.i(TAG, " Force finishing activity " + r);
3307 if (samePackage) {
3308 if (r.app != null) {
3309 r.app.removed = true;
3310 }
3311 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003312 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003313 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003314 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3315 true)) {
3316 // r has been deleted from mActivities, accommodate.
3317 --numActivities;
3318 --activityNdx;
3319 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003320 }
3321 }
3322 }
3323 return didSomething;
3324 }
3325
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003326 ActivityRecord getTasksLocked(IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003327 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003328 ActivityRecord topRecord = null;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003329 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003330 final TaskRecord task = mTaskHistory.get(taskNdx);
3331 ActivityRecord r = null;
3332 ActivityRecord top = null;
3333 int numActivities = 0;
3334 int numRunning = 0;
3335 final ArrayList<ActivityRecord> activities = task.mActivities;
3336 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3337 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003338
Craig Mautneraab647e2013-02-28 16:31:36 -08003339 // Initialize state for next task if needed.
3340 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3341 top = r;
3342 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003343 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003344
3345 // Add 'r' into the current task.
3346 numActivities++;
3347 if (r.app != null && r.app.thread != null) {
3348 numRunning++;
3349 }
3350
3351 if (localLOGV) Slog.v(
3352 TAG, r.intent.getComponent().flattenToShortString()
3353 + ": task=" + r.task);
3354 }
3355
3356 RunningTaskInfo ci = new RunningTaskInfo();
3357 ci.id = task.taskId;
3358 ci.baseActivity = r.intent.getComponent();
3359 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003360 ci.lastActiveTime = task.lastActiveTime;
3361
Craig Mautneraab647e2013-02-28 16:31:36 -08003362 if (top.thumbHolder != null) {
3363 ci.description = top.thumbHolder.lastDescription;
3364 }
3365 ci.numActivities = numActivities;
3366 ci.numRunning = numRunning;
3367 //System.out.println(
3368 // "#" + maxNum + ": " + " descr=" + ci.description);
3369 if (receiver != null) {
3370 if (localLOGV) Slog.v(
3371 TAG, "State=" + top.state + "Idle=" + top.idle
3372 + " app=" + top.app
3373 + " thr=" + (top.app != null ? top.app.thread : null));
3374 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3375 if (top.idle && top.app != null && top.app.thread != null) {
3376 topRecord = top;
3377 } else {
3378 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003379 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003380 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003381 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003382 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003383 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003384 }
3385 return topRecord;
3386 }
3387
3388 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003389 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003390 if (DEBUG_SWITCH) Slog.d(
3391 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003392 if (top >= 0) {
3393 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3394 int activityTop = activities.size() - 1;
3395 if (activityTop > 0) {
3396 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3397 "unhandled-back", true);
3398 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003399 }
3400 }
3401
Craig Mautnere79d42682013-04-01 19:01:53 -07003402 void handleAppDiedLocked(ProcessRecord app, boolean restarting) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003403 if (!containsApp(app)) {
3404 return;
3405 }
3406 // TODO: handle the case where an app spans multiple stacks.
Craig Mautnere79d42682013-04-01 19:01:53 -07003407 if (mPausingActivity != null && mPausingActivity.app == app) {
3408 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3409 "App died while pausing: " + mPausingActivity);
3410 mPausingActivity = null;
3411 }
3412 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3413 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003414 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003415 }
Craig Mautner37f748d2013-06-24 16:19:55 -07003416 final ActivityRecord top = topRunningActivityLocked(null);
3417 final boolean launchHomeTaskNext =
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003418 top != null && top.app == app && top.task.mOnTopOfHome;
Craig Mautnere79d42682013-04-01 19:01:53 -07003419
3420 // Remove this application's activities from active lists.
3421 boolean hasVisibleActivities = removeHistoryRecordsForAppLocked(app);
3422
3423 if (!restarting) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003424 ActivityStack stack = mStackSupervisor.getFocusedStack();
Craig Mautner37f748d2013-06-24 16:19:55 -07003425 if (stack == null || launchHomeTaskNext) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003426 mStackSupervisor.resumeHomeActivity(null);
Craig Mautner05d29032013-05-03 13:40:13 -07003427 } else if (!mStackSupervisor.resumeTopActivitiesLocked(stack, null, null)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003428 // If there was nothing to resume, and we are not already
3429 // restarting this process, but there is a visible activity that
3430 // is hosted by the process... then make sure all visible
3431 // activities are running, taking care of restarting this
3432 // process.
3433 if (hasVisibleActivities) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003434 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
Craig Mautnere79d42682013-04-01 19:01:53 -07003435 }
3436 }
3437 }
3438 }
3439
Craig Mautnercae015f2013-02-08 14:31:27 -08003440 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003441 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3442 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3443 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3444 final ActivityRecord r = activities.get(activityNdx);
3445 if (r.app == app) {
3446 Slog.w(TAG, " Force finishing activity "
3447 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003448 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003449 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003450 }
3451 }
3452 }
3453
Dianne Hackborn390517b2013-05-30 15:03:32 -07003454 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003455 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003456 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003457 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3458 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003459 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3460 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003461 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003462 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003463 if (printed) {
3464 header = null;
3465 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003466 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003467 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003468 }
3469
3470 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3471 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3472
3473 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003474 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3475 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003476 }
3477 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003478 final int top = mTaskHistory.size() - 1;
3479 if (top >= 0) {
3480 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3481 int listTop = list.size() - 1;
3482 if (listTop >= 0) {
3483 activities.add(list.get(listTop));
3484 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003485 }
3486 } else {
3487 ItemMatcher matcher = new ItemMatcher();
3488 matcher.build(name);
3489
Craig Mautneraab647e2013-02-28 16:31:36 -08003490 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3491 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3492 if (matcher.match(r1, r1.intent.getComponent())) {
3493 activities.add(r1);
3494 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003495 }
3496 }
3497 }
3498
3499 return activities;
3500 }
3501
3502 ActivityRecord restartPackage(String packageName) {
3503 ActivityRecord starting = topRunningActivityLocked(null);
3504
3505 // All activities that came from the package must be
3506 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003507 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3508 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3509 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3510 final ActivityRecord a = activities.get(activityNdx);
3511 if (a.info.packageName.equals(packageName)) {
3512 a.forceNewConfig = true;
3513 if (starting != null && a == starting && a.visible) {
3514 a.startFreezingScreenLocked(starting.app,
3515 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3516 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003517 }
3518 }
3519 }
3520
3521 return starting;
3522 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003523
Craig Mautnerde4ef022013-04-07 19:01:33 -07003524 boolean removeTask(TaskRecord task) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003525 final int taskNdx = mTaskHistory.indexOf(task);
3526 final int topTaskNdx = mTaskHistory.size() - 1;
3527 if (task.mOnTopOfHome && taskNdx < topTaskNdx) {
3528 mTaskHistory.get(taskNdx + 1).mOnTopOfHome = true;
3529 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003530 mTaskHistory.remove(task);
Craig Mautnerc8143c62013-09-03 12:15:57 -07003531 return mTaskHistory.isEmpty();
Craig Mautner0247fc82013-02-28 14:32:06 -08003532 }
3533
Craig Mautnerde4ef022013-04-07 19:01:33 -07003534 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003535 TaskRecord task = new TaskRecord(taskId, info, intent);
3536 addTask(task, toTop);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003537 return task;
3538 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003539
3540 ArrayList<TaskRecord> getAllTasks() {
3541 return new ArrayList<TaskRecord>(mTaskHistory);
3542 }
3543
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003544 void addTask(final TaskRecord task, final boolean toTop) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003545 task.stack = this;
3546 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003547 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003548 } else {
3549 mTaskHistory.add(0, task);
3550 }
3551 }
3552
3553 public int getStackId() {
3554 return mStackId;
3555 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003556
3557 @Override
3558 public String toString() {
3559 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3560 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003561}