blob: 064b3a8cdedf7374acd71ccb02b0d4cfe2f5ea9a [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 Mautnerd44711d2013-02-23 11:24:36 -08001003 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1004 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1005 final ActivityRecord r = activities.get(activityNdx);
1006 if (r.finishing) {
1007 continue;
1008 }
1009 if (aboveTop && r != top) {
1010 continue;
1011 }
1012 aboveTop = false;
1013 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001014 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001015 TAG, "Make visible? " + r + " finishing=" + r.finishing
1016 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001017
Craig Mautnerd44711d2013-02-23 11:24:36 -08001018 final boolean doThisProcess = onlyThisProcess == null
1019 || onlyThisProcess.equals(r.processName);
1020
1021 // First: if this is not the current activity being started, make
1022 // sure it matches the current configuration.
1023 if (r != starting && doThisProcess) {
1024 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001025 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001026
1027 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001028 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001029 // This activity needs to be visible, but isn't even
1030 // running... get it started, but don't resume it
1031 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001032 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001033 if (r != starting) {
1034 r.startFreezingScreenLocked(r.app, configChanges);
1035 }
1036 if (!r.visible) {
1037 if (DEBUG_VISBILITY) Slog.v(
1038 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001039 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001040 }
1041 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001042 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001043 }
1044 }
1045
1046 } else if (r.visible) {
1047 // If this activity is already visible, then there is nothing
1048 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001049 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001050 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001051
1052 } else if (onlyThisProcess == null) {
1053 // This activity is not currently visible, but is running.
1054 // Tell it to become visible.
1055 r.visible = true;
1056 if (r.state != ActivityState.RESUMED && r != starting) {
1057 // If this activity is paused, tell it
1058 // to now show its window.
1059 if (DEBUG_VISBILITY) Slog.v(
1060 TAG, "Making visible and scheduling visibility: " + r);
1061 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001062 if (mTranslucentActivityWaiting != null) {
1063 mUndrawnActivitiesBelowTopTranslucent.add(r);
1064 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001065 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001066 r.sleeping = false;
1067 r.app.pendingUiClean = true;
1068 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1069 r.stopFreezingScreenLocked(false);
1070 } catch (Exception e) {
1071 // Just skip on any failure; we'll make it
1072 // visible when it next restarts.
1073 Slog.w(TAG, "Exception thrown making visibile: "
1074 + r.intent.getComponent(), e);
1075 }
1076 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001077 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001078
Craig Mautnerd44711d2013-02-23 11:24:36 -08001079 // Aggregate current change flags.
1080 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001081
Craig Mautnerd44711d2013-02-23 11:24:36 -08001082 if (r.fullscreen) {
1083 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001084 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1085 behindFullscreen = true;
1086 } else if (r.mLaunchHomeTaskNext) {
1087 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1088 showHomeBehindStack = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001089 behindFullscreen = true;
1090 }
1091 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001092 if (DEBUG_VISBILITY) Slog.v(
1093 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1094 + " state=" + r.state
1095 + " behindFullscreen=" + behindFullscreen);
1096 // Now for any activities that aren't visible to the user, make
1097 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001098 if (r.visible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001099 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001100 r.visible = false;
1101 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001102 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001103 switch (r.state) {
1104 case STOPPING:
1105 case STOPPED:
1106 if (r.app != null && r.app.thread != null) {
1107 if (DEBUG_VISBILITY) Slog.v(
1108 TAG, "Scheduling invisibility: " + r);
1109 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1110 }
1111 break;
1112
1113 case INITIALIZING:
1114 case RESUMED:
1115 case PAUSING:
1116 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001117 // This case created for transitioning activities from
1118 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautnere5273b42013-09-09 12:57:47 -07001119 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1120 mStackSupervisor.mStoppingActivities.add(r);
1121 }
1122 mStackSupervisor.scheduleIdleLocked();
Craig Mautner4addfc52013-06-25 08:05:45 -07001123 break;
1124
1125 default:
1126 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001127 }
1128 } catch (Exception e) {
1129 // Just skip on any failure; we'll make it
1130 // visible when it next restarts.
1131 Slog.w(TAG, "Exception thrown making hidden: "
1132 + r.intent.getComponent(), e);
1133 }
1134 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001135 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001136 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001137 }
1138 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001139 }
Craig Mautner580ea812013-04-25 12:58:38 -07001140 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001141 }
Craig Mautner58547802013-03-05 08:23:53 -08001142
Craig Mautner5eda9b32013-07-02 11:58:16 -07001143 void convertToTranslucent(ActivityRecord r) {
1144 mTranslucentActivityWaiting = r;
1145 mUndrawnActivitiesBelowTopTranslucent.clear();
1146 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1147 }
1148
1149 /**
1150 * Called as activities below the top translucent activity are redrawn. When the last one is
1151 * redrawn notify the top activity by calling
1152 * {@link Activity#onTranslucentConversionComplete}.
1153 *
1154 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1155 * occurred and the activity will be notified immediately.
1156 */
1157 void notifyActivityDrawnLocked(ActivityRecord r) {
1158 if ((r == null)
1159 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1160 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1161 // The last undrawn activity below the top has just been drawn. If there is an
1162 // opaque activity at the top, notify it that it can become translucent safely now.
1163 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1164 mTranslucentActivityWaiting = null;
1165 mUndrawnActivitiesBelowTopTranslucent.clear();
1166 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1167
1168 if (waitingActivity != null && waitingActivity.app != null &&
1169 waitingActivity.app.thread != null) {
1170 try {
1171 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1172 waitingActivity.appToken, r != null);
1173 } catch (RemoteException e) {
1174 }
1175 }
1176 }
1177 }
1178
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001179 /**
1180 * Ensure that the top activity in the stack is resumed.
1181 *
1182 * @param prev The previously resumed activity, for when in the process
1183 * of pausing; can be null to call from elsewhere.
1184 *
1185 * @return Returns true if something is being resumed, or false if
1186 * nothing happened.
1187 */
1188 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001189 return resumeTopActivityLocked(prev, null);
1190 }
1191
1192 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001193 // Find the first activity that is not finishing.
1194 ActivityRecord next = topRunningActivityLocked(null);
1195
1196 // Remember how we'll process this pause/resume situation, and ensure
1197 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001198 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1199 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001200
1201 if (next == null) {
1202 // There are no more activities! Let's just start up the
1203 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001204 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001205 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001206 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001207 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001208 }
1209
1210 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001211
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001212 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001213 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1214 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001215 // Make sure we have executed any pending transitions, since there
1216 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001217 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001218 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001219 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001220 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001221 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001222 return false;
1223 }
1224
Craig Mautner525f3d92013-05-07 14:01:50 -07001225 final TaskRecord nextTask = next.task;
1226 final TaskRecord prevTask = prev != null ? prev.task : null;
1227 if (prevTask != null && prev.mLaunchHomeTaskNext && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001228 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001229 if (prevTask == nextTask) {
1230 ArrayList<ActivityRecord> activities = prevTask.mActivities;
1231 final int numActivities = activities.size();
1232 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1233 final ActivityRecord r = activities.get(activityNdx);
1234 // r is usually the same as next, but what if two activities were launched
1235 // before prev finished?
1236 if (!r.finishing) {
1237 r.mLaunchHomeTaskNext = true;
1238 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 Mautnere418ecd2013-05-01 17:02:29 -07001246 mTaskHistory.get(taskNdx).mActivities.get(0).mLaunchHomeTaskNext = true;
1247 } 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.
1846 TaskRecord bottomTask = mTaskHistory.get(0);
1847 ActivityRecord p = bottomTask.mActivities.get(0);
1848 if (target.taskAffinity != null
1849 && target.taskAffinity.equals(p.task.affinity)) {
1850 // If the activity currently at the bottom has the
1851 // same task affinity as the one we are moving,
1852 // then merge it into the same task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001853 target.setTask(p.task, p.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001854 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1855 + " out to bottom task " + p.task);
1856 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001857 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001858 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001859 target.task.affinityIntent = target.intent;
1860 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1861 + " out to new task " + target.task);
1862 }
1863
1864 final TaskRecord targetTask = target.task;
1865 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001866 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001867
Craig Mautner525f3d92013-05-07 14:01:50 -07001868 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001869 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1870 for (int srcPos = start; srcPos >= i; --srcPos) {
1871 p = activities.get(srcPos);
1872 if (p.finishing) {
1873 continue;
1874 }
1875
Craig Mautner525f3d92013-05-07 14:01:50 -07001876 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001877 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001878 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001879 topOptions = p.takeOptionsLocked();
1880 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001881 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001882 }
1883 }
1884 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1885 + task + " adding to task=" + targetTask,
1886 new RuntimeException("here").fillInStackTrace());
1887 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1888 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001889 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001890 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001891
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001892 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001893 }
1894
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001895 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001896 if (VALIDATE_TOKENS) {
1897 validateAppTokensLocked();
1898 }
1899
1900 replyChainEnd = -1;
1901 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1902 // If the activity should just be removed -- either
1903 // because it asks for it, or the task should be
1904 // cleared -- then finish it and anything that is
1905 // part of its reply chain.
1906 int end;
1907 if (clearWhenTaskReset) {
1908 // In this case, we want to finish this activity
1909 // and everything above it, so be sneaky and pretend
1910 // like these are all in the reply chain.
1911 end = numActivities - 1;
1912 } else if (replyChainEnd < 0) {
1913 end = i;
1914 } else {
1915 end = replyChainEnd;
1916 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001917 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001918 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001919 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001920 if (p.finishing) {
1921 continue;
1922 }
1923 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001924 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001925 topOptions = p.takeOptionsLocked();
1926 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001927 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001928 }
1929 }
Craig Mautner58547802013-03-05 08:23:53 -08001930 if (DEBUG_TASKS) Slog.w(TAG,
1931 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001932 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001933 end--;
1934 srcPos--;
1935 }
1936 }
1937 replyChainEnd = -1;
1938 } else {
1939 // If we were in the middle of a chain, well the
1940 // activity that started it all doesn't want anything
1941 // special, so leave it all as-is.
1942 replyChainEnd = -1;
1943 }
1944 }
1945
1946 return topOptions;
1947 }
1948
1949 /**
1950 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1951 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1952 * @param affinityTask The task we are looking for an affinity to.
1953 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1954 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1955 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1956 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001957 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08001958 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001959 int replyChainEnd = -1;
1960 final int taskId = task.taskId;
1961 final String taskAffinity = task.affinity;
1962
1963 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1964 final int numActivities = activities.size();
1965 // Do not operate on the root Activity.
1966 for (int i = numActivities - 1; i > 0; --i) {
1967 ActivityRecord target = activities.get(i);
1968
1969 final int flags = target.info.flags;
1970 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1971 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1972
1973 if (target.resultTo != null) {
1974 // If this activity is sending a reply to a previous
1975 // activity, we can't do anything with it now until
1976 // we reach the start of the reply chain.
1977 // XXX note that we are assuming the result is always
1978 // to the previous activity, which is almost always
1979 // the case but we really shouldn't count on.
1980 if (replyChainEnd < 0) {
1981 replyChainEnd = i;
1982 }
1983 } else if (topTaskIsHigher
1984 && allowTaskReparenting
1985 && taskAffinity != null
1986 && taskAffinity.equals(target.taskAffinity)) {
1987 // This activity has an affinity for our task. Either remove it if we are
1988 // clearing or move it over to our task. Note that
1989 // we currently punt on the case where we are resetting a
1990 // task that is not at the top but who has activities above
1991 // with an affinity to it... this is really not a normal
1992 // case, and we will need to later pull that task to the front
1993 // and usually at that point we will do the reset and pick
1994 // up those remaining activities. (This only happens if
1995 // someone starts an activity in a new task from an activity
1996 // in a task that is not currently on top.)
1997 if (forceReset || finishOnTaskLaunch) {
1998 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
1999 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2000 for (int srcPos = start; srcPos >= i; --srcPos) {
2001 final ActivityRecord p = activities.get(srcPos);
2002 if (p.finishing) {
2003 continue;
2004 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002005 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002006 }
2007 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002008 if (taskInsertionPoint < 0) {
2009 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002010
Craig Mautner77878772013-03-04 19:46:24 -08002011 }
Craig Mautner77878772013-03-04 19:46:24 -08002012
2013 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2014 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2015 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2016 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002017 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002018 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002019 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002020
Craig Mautnere3a74d52013-02-22 14:14:58 -08002021 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2022 + " to stack at " + task,
2023 new RuntimeException("here").fillInStackTrace());
2024 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2025 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002026 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002027 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002028 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002029 if (VALIDATE_TOKENS) {
2030 validateAppTokensLocked();
2031 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002032
2033 // Now we've moved it in to place... but what if this is
2034 // a singleTop activity and we have put it on top of another
2035 // instance of the same activity? Then we drop the instance
2036 // below so it remains singleTop.
2037 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2038 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002039 int targetNdx = taskActivities.indexOf(target);
2040 if (targetNdx > 0) {
2041 ActivityRecord p = taskActivities.get(targetNdx - 1);
2042 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002043 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2044 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002045 }
2046 }
2047 }
2048 }
2049
2050 replyChainEnd = -1;
2051 }
2052 }
Craig Mautner77878772013-03-04 19:46:24 -08002053 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002054 }
2055
Craig Mautner8849a5e2013-04-02 16:41:03 -07002056 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002057 ActivityRecord newActivity) {
2058 boolean forceReset =
2059 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2060 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2061 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2062 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2063 forceReset = true;
2064 }
2065 }
2066
2067 final TaskRecord task = taskTop.task;
2068
2069 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2070 * for remaining tasks. Used for later tasks to reparent to task. */
2071 boolean taskFound = false;
2072
2073 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2074 ActivityOptions topOptions = null;
2075
Craig Mautner77878772013-03-04 19:46:24 -08002076 // Preserve the location for reparenting in the new task.
2077 int reparentInsertionPoint = -1;
2078
Craig Mautnere3a74d52013-02-22 14:14:58 -08002079 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2080 final TaskRecord targetTask = mTaskHistory.get(i);
2081
2082 if (targetTask == task) {
2083 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2084 taskFound = true;
2085 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002086 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2087 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002088 }
2089 }
2090
Craig Mautner70a86932013-02-28 22:37:44 -08002091 int taskNdx = mTaskHistory.indexOf(task);
2092 do {
2093 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2094 } while (taskTop == null && taskNdx >= 0);
2095
Craig Mautnere3a74d52013-02-22 14:14:58 -08002096 if (topOptions != null) {
2097 // If we got some ActivityOptions from an activity on top that
2098 // was removed from the task, propagate them to the new real top.
2099 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002100 taskTop.updateOptionsLocked(topOptions);
2101 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002102 topOptions.abort();
2103 }
2104 }
2105
2106 return taskTop;
2107 }
2108
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002109 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2110 String resultWho, int requestCode, int resultCode, Intent data) {
2111
2112 if (callingUid > 0) {
2113 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002114 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002115 }
2116
2117 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2118 + " : who=" + resultWho + " req=" + requestCode
2119 + " res=" + resultCode + " data=" + data);
2120 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2121 try {
2122 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2123 list.add(new ResultInfo(resultWho, requestCode,
2124 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002125 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002126 return;
2127 } catch (Exception e) {
2128 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2129 }
2130 }
2131
2132 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2133 }
2134
Craig Mautnerf3333272013-04-22 10:55:53 -07002135 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002136 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2137 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2138 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2139 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002140 if (!mService.mSleeping) {
2141 if (DEBUG_STATES) {
2142 Slog.d(TAG, "no-history finish of " + r);
2143 }
2144 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002145 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002146 } else {
2147 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2148 + " on stop because we're just sleeping");
2149 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002150 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002151 }
2152
2153 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002154 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002155 if (mService.mFocusedActivity == r) {
2156 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2157 }
2158 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002159 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002160 try {
2161 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002162 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2163 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002164 r.state = ActivityState.STOPPING;
2165 if (DEBUG_VISBILITY) Slog.v(
2166 TAG, "Stopping visible=" + r.visible + " for " + r);
2167 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002168 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002169 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002170 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002171 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002172 r.setSleeping(true);
2173 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002174 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002175 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002176 } catch (Exception e) {
2177 // Maybe just ignore exceptions here... if the process
2178 // has crashed, our death notification will clean things
2179 // up.
2180 Slog.w(TAG, "Exception thrown during pause", e);
2181 // Just in case, assume it to be stopped.
2182 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002183 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002184 r.state = ActivityState.STOPPED;
2185 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002186 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002187 }
2188 }
2189 }
2190 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002191
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002192 /**
2193 * @return Returns true if the activity is being finished, false if for
2194 * some reason it is being left as-is.
2195 */
2196 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002197 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002198 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002199 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002200 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002201 + ", result=" + resultCode + ", data=" + resultData
2202 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002203 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002204 return false;
2205 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002206
Craig Mautnerd44711d2013-02-23 11:24:36 -08002207 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002208 return true;
2209 }
2210
Craig Mautnerd2328952013-03-05 12:46:26 -08002211 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002212 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2213 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2214 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2215 ActivityRecord r = activities.get(activityNdx);
2216 if (r.resultTo == self && r.requestCode == requestCode) {
2217 if ((r.resultWho == null && resultWho == null) ||
2218 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2219 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2220 false);
2221 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002222 }
2223 }
2224 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002225 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002226 }
2227
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002228 final void finishTopRunningActivityLocked(ProcessRecord app) {
2229 ActivityRecord r = topRunningActivityLocked(null);
2230 if (r != null && r.app == app) {
2231 // If the top running activity is from this crashing
2232 // process, then terminate it to avoid getting in a loop.
2233 Slog.w(TAG, " Force finishing activity "
2234 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002235 int taskNdx = mTaskHistory.indexOf(r.task);
2236 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002237 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002238 // Also terminate any activities below it that aren't yet
2239 // stopped, to avoid a situation where one will get
2240 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002241 --activityNdx;
2242 if (activityNdx < 0) {
2243 do {
2244 --taskNdx;
2245 if (taskNdx < 0) {
2246 break;
2247 }
2248 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2249 } while (activityNdx < 0);
2250 }
2251 if (activityNdx >= 0) {
2252 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002253 if (r.state == ActivityState.RESUMED
2254 || r.state == ActivityState.PAUSING
2255 || r.state == ActivityState.PAUSED) {
Craig Mautnere428a7f2013-08-17 17:04:56 -07002256 if (!r.isHomeActivity() || !mService.mHomeProcess.contains(r.app)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002257 Slog.w(TAG, " Force finishing activity "
2258 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002259 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002260 }
2261 }
2262 }
2263 }
2264 }
2265
Craig Mautnerd2328952013-03-05 12:46:26 -08002266 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002267 ArrayList<ActivityRecord> activities = r.task.mActivities;
2268 for (int index = activities.indexOf(r); index >= 0; --index) {
2269 ActivityRecord cur = activities.get(index);
2270 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002271 break;
2272 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002273 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002274 }
2275 return true;
2276 }
2277
Dianne Hackborn5c607432012-02-28 14:44:19 -08002278 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2279 // send the result
2280 ActivityRecord resultTo = r.resultTo;
2281 if (resultTo != null) {
2282 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2283 + " who=" + r.resultWho + " req=" + r.requestCode
2284 + " res=" + resultCode + " data=" + resultData);
2285 if (r.info.applicationInfo.uid > 0) {
2286 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2287 resultTo.packageName, resultData,
2288 resultTo.getUriPermissionsLocked());
2289 }
2290 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2291 resultData);
2292 r.resultTo = null;
2293 }
2294 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2295
2296 // Make sure this HistoryRecord is not holding on to other resources,
2297 // because clients have remote IPC references to this object so we
2298 // can't assume that will go away and want to avoid circular IPC refs.
2299 r.results = null;
2300 r.pendingResults = null;
2301 r.newIntents = null;
2302 r.icicle = null;
2303 }
2304
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002305 /**
2306 * @return Returns true if this activity has been removed from the history
2307 * list, or false if it is still in the list and will be removed later.
2308 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002309 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2310 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002311 if (r.finishing) {
2312 Slog.w(TAG, "Duplicate finish request for " + r);
2313 return false;
2314 }
2315
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002316 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002317 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002318 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002319 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002320 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2321 final int index = activities.indexOf(r);
2322 if (index < (activities.size() - 1)) {
2323 ActivityRecord next = activities.get(index+1);
2324 if (r.frontOfTask) {
2325 // The next activity is now the front of the task.
2326 next.frontOfTask = true;
2327 }
2328 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2329 // If the caller asked that this activity (and all above it)
2330 // be cleared when the task is reset, don't lose that information,
2331 // but propagate it up to the next activity.
2332 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002333 }
2334 }
2335
2336 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002337 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002338 if (mService.mFocusedActivity == r) {
Craig Mautner29219d92013-04-16 20:19:12 -07002339 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002340 }
2341 }
2342
Dianne Hackborn5c607432012-02-28 14:44:19 -08002343 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002344
Craig Mautnerc8143c62013-09-03 12:15:57 -07002345 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002346 // There are clients waiting to receive thumbnails so, in case
2347 // this is an activity that someone is waiting for, add it
2348 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002349 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002350 }
2351
Craig Mautnerde4ef022013-04-07 19:01:33 -07002352 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002353 boolean endTask = index <= 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002354 if (DEBUG_TRANSITION) Slog.v(TAG,
2355 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002356 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002357 ? AppTransition.TRANSIT_TASK_CLOSE
2358 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002359
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002360 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002361 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002362
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002363 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002364 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2365 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2366 startPausingLocked(false, false);
2367 }
2368
2369 } else if (r.state != ActivityState.PAUSING) {
2370 // If the activity is PAUSING, we will complete the finish once
2371 // it is done pausing; else we can just directly finish it here.
2372 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002373 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002374 } else {
2375 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2376 }
2377
2378 return false;
2379 }
2380
Craig Mautnerf3333272013-04-22 10:55:53 -07002381 static final int FINISH_IMMEDIATELY = 0;
2382 static final int FINISH_AFTER_PAUSE = 1;
2383 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002384
Craig Mautnerf3333272013-04-22 10:55:53 -07002385 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002386 // First things first: if this activity is currently visible,
2387 // and the resumed activity is not yet visible, then hold off on
2388 // finishing until the resumed one becomes visible.
2389 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002390 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2391 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002392 if (mStackSupervisor.mStoppingActivities.size() > 3
2393 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002394 // If we already have a few activities waiting to stop,
2395 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002396 // them out. Or if r is the last of activity of the last task the stack
2397 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002398 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002399 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002400 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002401 }
2402 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002403 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2404 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002405 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002406 if (oomAdj) {
2407 mService.updateOomAdjLocked();
2408 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002409 return r;
2410 }
2411
2412 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002413 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002414 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002415 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002416 if (mResumedActivity == r) {
2417 mResumedActivity = null;
2418 }
2419 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002420 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002421 r.state = ActivityState.FINISHING;
2422
2423 if (mode == FINISH_IMMEDIATELY
2424 || prevState == ActivityState.STOPPED
2425 || prevState == ActivityState.INITIALIZING) {
2426 // If this activity is already stopped, we can just finish
2427 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002428 boolean activityRemoved = destroyActivityLocked(r, true,
2429 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002430 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002431 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002432 }
2433 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002434 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002435
2436 // Need to go through the full pause cycle to get this
2437 // activity into the stopped state and then finish it.
2438 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002439 mStackSupervisor.mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002440 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002441 return r;
2442 }
2443
Craig Mautnerd2328952013-03-05 12:46:26 -08002444 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002445 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002446 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002447 final TaskRecord task = srec.task;
2448 final ArrayList<ActivityRecord> activities = task.mActivities;
2449 final int start = activities.indexOf(srec);
2450 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002451 return false;
2452 }
2453 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002454 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002455 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002456 final ComponentName dest = destIntent.getComponent();
2457 if (start > 0 && dest != null) {
2458 for (int i = finishTo; i >= 0; i--) {
2459 ActivityRecord r = activities.get(i);
2460 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002461 r.info.name.equals(dest.getClassName())) {
2462 finishTo = i;
2463 parent = r;
2464 foundParentInTask = true;
2465 break;
2466 }
2467 }
2468 }
2469
2470 IActivityController controller = mService.mController;
2471 if (controller != null) {
2472 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2473 if (next != null) {
2474 // ask watcher if this is allowed
2475 boolean resumeOK = true;
2476 try {
2477 resumeOK = controller.activityResuming(next.packageName);
2478 } catch (RemoteException e) {
2479 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002480 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002481 }
2482
2483 if (!resumeOK) {
2484 return false;
2485 }
2486 }
2487 }
2488 final long origId = Binder.clearCallingIdentity();
2489 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002490 ActivityRecord r = activities.get(i);
2491 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002492 // Only return the supplied result for the first activity finished
2493 resultCode = Activity.RESULT_CANCELED;
2494 resultData = null;
2495 }
2496
2497 if (parent != null && foundParentInTask) {
2498 final int parentLaunchMode = parent.info.launchMode;
2499 final int destIntentFlags = destIntent.getFlags();
2500 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2501 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2502 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2503 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2504 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2505 } else {
2506 try {
2507 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2508 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002509 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002510 null, aInfo, parent.appToken, null,
2511 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2512 0, null, true, null);
2513 foundParentInTask = res == ActivityManager.START_SUCCESS;
2514 } catch (RemoteException e) {
2515 foundParentInTask = false;
2516 }
2517 requestFinishActivityLocked(parent.appToken, resultCode,
2518 resultData, "navigate-up", true);
2519 }
2520 }
2521 Binder.restoreCallingIdentity(origId);
2522 return foundParentInTask;
2523 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002524 /**
2525 * Perform the common clean-up of an activity record. This is called both
2526 * as part of destroyActivityLocked() (when destroying the client-side
2527 * representation) and cleaning things up as a result of its hosting
2528 * processing going away, in which case there is no remaining client-side
2529 * state to destroy so only the cleanup here is needed.
2530 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002531 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2532 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002533 if (mResumedActivity == r) {
2534 mResumedActivity = null;
2535 }
2536 if (mService.mFocusedActivity == r) {
2537 mService.mFocusedActivity = null;
2538 }
2539
2540 r.configDestroy = false;
2541 r.frozenBeforeDestroy = false;
2542
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002543 if (setState) {
2544 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2545 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002546 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002547 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002548 }
2549
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002550 // Make sure this record is no longer in the pending finishes list.
2551 // This could happen, for example, if we are trimming activities
2552 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002553 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002554 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002555
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002556 // Remove any pending results.
2557 if (r.finishing && r.pendingResults != null) {
2558 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2559 PendingIntentRecord rec = apr.get();
2560 if (rec != null) {
2561 mService.cancelIntentSenderLocked(rec, false);
2562 }
2563 }
2564 r.pendingResults = null;
2565 }
2566
2567 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002568 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002569 }
2570
Craig Mautnerc8143c62013-09-03 12:15:57 -07002571 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002572 // There are clients waiting to receive thumbnails so, in case
2573 // this is an activity that someone is waiting for, add it
2574 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002575 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002576 }
2577
2578 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002579 removeTimeoutsForActivityLocked(r);
2580 }
2581
2582 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002583 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002584 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002585 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002586 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002587 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002588 }
2589
Dianne Hackborn5c607432012-02-28 14:44:19 -08002590 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002591 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2592 r.makeFinishing();
2593 if (DEBUG_ADD_REMOVE) {
2594 RuntimeException here = new RuntimeException("here");
2595 here.fillInStackTrace();
2596 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002597 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002598 final TaskRecord task = r.task;
2599 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002600 if (DEBUG_STACK) Slog.i(TAG,
2601 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002602 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002603 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002604 r.takeFromHistory();
2605 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002606 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002607 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002608 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002609 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002610 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002611 if (VALIDATE_TOKENS) {
2612 validateAppTokensLocked();
2613 }
2614 cleanUpActivityServicesLocked(r);
2615 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002616 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002617
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002618 /**
2619 * Perform clean-up of service connections in an activity record.
2620 */
2621 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2622 // Throw away any services that have been bound by this activity.
2623 if (r.connections != null) {
2624 Iterator<ConnectionRecord> it = r.connections.iterator();
2625 while (it.hasNext()) {
2626 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002627 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002628 }
2629 r.connections = null;
2630 }
2631 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002632
2633 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2634 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2635 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2636 mHandler.sendMessage(msg);
2637 }
2638
Dianne Hackborn28695e02011-11-02 21:59:51 -07002639 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002640 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002641 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002642 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2643 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2644 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2645 final ActivityRecord r = activities.get(activityNdx);
2646 if (r.finishing) {
2647 continue;
2648 }
2649 if (r.fullscreen) {
2650 lastIsOpaque = true;
2651 }
2652 if (owner != null && r.app != owner) {
2653 continue;
2654 }
2655 if (!lastIsOpaque) {
2656 continue;
2657 }
2658 // We can destroy this one if we have its icicle saved and
2659 // it is not in the process of pausing/stopping/finishing.
2660 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2661 && r.haveState && !r.visible && r.stopped
2662 && r.state != ActivityState.DESTROYING
2663 && r.state != ActivityState.DESTROYED) {
2664 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2665 + " resumed=" + mResumedActivity
2666 + " pausing=" + mPausingActivity);
2667 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2668 activityRemoved = true;
2669 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002670 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002671 }
2672 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002673 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002674 mStackSupervisor.resumeTopActivitiesLocked();
2675
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002676 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002677 }
2678
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002679 /**
2680 * Destroy the current CLIENT SIDE instance of an activity. This may be
2681 * called both when actually finishing an activity, or when performing
2682 * a configuration switch where we destroy the current client-side object
2683 * but then create a new client-side object for this same HistoryRecord.
2684 */
2685 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002686 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002687 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002688 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002689 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2690 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002691 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002692 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002693
2694 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002695
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002696 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002697
2698 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002699
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002700 if (hadApp) {
2701 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002702 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002703 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2704 mService.mHeavyWeightProcess = null;
2705 mService.mHandler.sendEmptyMessage(
2706 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2707 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07002708 if (r.app.activities.isEmpty()) {
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002709 // No longer have activities, so update oom adj.
2710 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002711 }
2712 }
2713
2714 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002715
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002716 try {
2717 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002718 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002719 r.configChangeFlags);
2720 } catch (Exception e) {
2721 // We can just ignore exceptions here... if the process
2722 // has crashed, our death notification will clean things
2723 // up.
2724 //Slog.w(TAG, "Exception thrown during finish", e);
2725 if (r.finishing) {
2726 removeActivityFromHistoryLocked(r);
2727 removedFromHistory = true;
2728 skipDestroy = true;
2729 }
2730 }
2731
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002732 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002733
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002734 // If the activity is finishing, we need to wait on removing it
2735 // from the list to give it a chance to do its cleanup. During
2736 // that time it may make calls back with its token so we need to
2737 // be able to find it on the list and so we don't want to remove
2738 // it from the list yet. Otherwise, we can just immediately put
2739 // it in the destroyed state since we are not removing it from the
2740 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002741 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002742 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2743 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002744 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002745 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002746 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2747 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002748 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002749 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002750 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002751 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002752 }
2753 } else {
2754 // remove this record from the history.
2755 if (r.finishing) {
2756 removeActivityFromHistoryLocked(r);
2757 removedFromHistory = true;
2758 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002759 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002760 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002761 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002762 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002763 }
2764 }
2765
2766 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002767
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002768 if (!mLRUActivities.remove(r) && hadApp) {
2769 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2770 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002771
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002772 return removedFromHistory;
2773 }
2774
Craig Mautnerd2328952013-03-05 12:46:26 -08002775 final void activityDestroyedLocked(IBinder token) {
2776 final long origId = Binder.clearCallingIdentity();
2777 try {
2778 ActivityRecord r = ActivityRecord.forToken(token);
2779 if (r != null) {
2780 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002781 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002782
2783 if (isInStackLocked(token) != null) {
2784 if (r.state == ActivityState.DESTROYING) {
2785 cleanUpActivityLocked(r, true, false);
2786 removeActivityFromHistoryLocked(r);
2787 }
2788 }
Craig Mautner05d29032013-05-03 13:40:13 -07002789 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002790 } finally {
2791 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002792 }
2793 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002794
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002795 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2796 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002797 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002798 if (DEBUG_CLEANUP) Slog.v(
2799 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002800 + " with " + i + " entries");
2801 while (i > 0) {
2802 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002803 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002804 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002805 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002806 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002807 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002808 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002809 }
2810 }
2811 }
2812
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002813 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2814 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002815 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2816 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002817 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2818 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002819 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002820 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002821 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2822 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002823
2824 boolean hasVisibleActivities = false;
2825
2826 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002827 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002828 if (DEBUG_CLEANUP) Slog.v(
2829 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002830 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2831 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2832 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2833 final ActivityRecord r = activities.get(activityNdx);
2834 --i;
2835 if (DEBUG_CLEANUP) Slog.v(
2836 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2837 if (r.app == app) {
2838 boolean remove;
2839 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2840 // Don't currently have state for the activity, or
2841 // it is finishing -- always remove it.
2842 remove = true;
2843 } else if (r.launchCount > 2 &&
2844 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2845 // We have launched this activity too many times since it was
2846 // able to run, so give up and remove it.
2847 remove = true;
2848 } else {
2849 // The process may be gone, but the activity lives on!
2850 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002851 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002852 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002853 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002854 RuntimeException here = new RuntimeException("here");
2855 here.fillInStackTrace();
2856 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2857 + ": haveState=" + r.haveState
2858 + " stateNotNeeded=" + r.stateNotNeeded
2859 + " finishing=" + r.finishing
2860 + " state=" + r.state, here);
2861 }
2862 if (!r.finishing) {
2863 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2864 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2865 r.userId, System.identityHashCode(r),
2866 r.task.taskId, r.shortComponentName,
2867 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07002868 if (r.state == ActivityState.RESUMED) {
2869 mService.updateUsageStats(r, false);
2870 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002871 }
2872 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002873
Craig Mautner0247fc82013-02-28 14:32:06 -08002874 } else {
2875 // We have the current state for this activity, so
2876 // it can be restarted later when needed.
2877 if (localLOGV) Slog.v(
2878 TAG, "Keeping entry, setting app to null");
2879 if (r.visible) {
2880 hasVisibleActivities = true;
2881 }
2882 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2883 + r);
2884 r.app = null;
2885 r.nowVisible = false;
2886 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002887 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08002888 "App died, clearing saved state of " + r);
2889 r.icicle = null;
2890 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002891 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002892
Craig Mautnerd2328952013-03-05 12:46:26 -08002893 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002894 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002895 }
2896 }
2897
2898 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002899 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002900
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002901 final void updateTransitLocked(int transit, Bundle options) {
2902 if (options != null) {
2903 ActivityRecord r = topRunningActivityLocked(null);
2904 if (r != null && r.state != ActivityState.RESUMED) {
2905 r.updateOptionsLocked(options);
2906 } else {
2907 ActivityOptions.abort(options);
2908 }
2909 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002910 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002911 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002912
Craig Mautnera82aa092013-09-13 15:34:08 -07002913 void moveHomeTaskToTop() {
2914 final int top = mTaskHistory.size() - 1;
2915 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
2916 final TaskRecord task = mTaskHistory.get(taskNdx);
2917 if (task.isHomeTask()) {
2918 mTaskHistory.remove(taskNdx);
2919 mTaskHistory.add(top, task);
2920 mWindowManager.moveTaskToTop(task.taskId);
2921 return;
2922 }
2923 }
2924 }
2925
Craig Mautnercae015f2013-02-08 14:31:27 -08002926 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002927 final TaskRecord task = taskForIdLocked(taskId);
2928 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08002929 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002930 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002931 }
Craig Mautneraab647e2013-02-28 16:31:36 -08002932 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2933 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07002934 // we'll just indicate that this task returns to the home task.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002935 task.mActivities.get(0).mLaunchHomeTaskNext = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08002936 }
2937 moveTaskToFrontLocked(task, null, options);
2938 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002939 }
2940 return false;
2941 }
2942
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002943 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002944 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002945
Craig Mautner11bf9a52013-02-19 14:08:51 -08002946 final int numTasks = mTaskHistory.size();
2947 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07002948 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002949 // nothing to do!
2950 if (reason != null &&
2951 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2952 ActivityOptions.abort(options);
2953 } else {
2954 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
2955 }
2956 return;
2957 }
2958
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07002959 mStackSupervisor.moveHomeStack(isHomeStack());
2960
Craig Mautner11bf9a52013-02-19 14:08:51 -08002961 // Shift all activities with this task up to the top
2962 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07002963 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08002964
2965 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002966 if (reason != null &&
2967 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002968 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002969 ActivityRecord r = topRunningActivityLocked(null);
2970 if (r != null) {
2971 mNoAnimActivities.add(r);
2972 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002973 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002974 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08002975 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002976 }
Craig Mautner30e2d722013-02-12 11:30:16 -08002977
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002978 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002979
Craig Mautner05d29032013-05-03 13:40:13 -07002980 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08002981 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08002982
2983 if (VALIDATE_TOKENS) {
2984 validateAppTokensLocked();
2985 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002986 }
2987
2988 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002989 * Worker method for rearranging history stack. Implements the function of moving all
2990 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002991 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002992 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002993 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
2994 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002995 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002996 * @param task The taskId to collect and move to the bottom.
2997 * @return Returns true if the move completed, false if not.
2998 */
2999 final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
3000 Slog.i(TAG, "moveTaskToBack: " + task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003001
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003002 // If we have a watcher, preflight the move before committing to it. First check
3003 // for *other* available tasks, but if none are available, then try again allowing the
3004 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003005 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003006 ActivityRecord next = topRunningActivityLocked(null, task);
3007 if (next == null) {
3008 next = topRunningActivityLocked(null, 0);
3009 }
3010 if (next != null) {
3011 // ask watcher if this is allowed
3012 boolean moveOK = true;
3013 try {
3014 moveOK = mService.mController.activityResuming(next.packageName);
3015 } catch (RemoteException e) {
3016 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003017 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003018 }
3019 if (!moveOK) {
3020 return false;
3021 }
3022 }
3023 }
3024
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003025 if (DEBUG_TRANSITION) Slog.v(TAG,
3026 "Prepare to back transition: task=" + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003027
Craig Mautnerd2328952013-03-05 12:46:26 -08003028 final TaskRecord tr = taskForIdLocked(task);
3029 if (tr == null) {
3030 return false;
3031 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003032
Craig Mautner11bf9a52013-02-19 14:08:51 -08003033 mTaskHistory.remove(tr);
3034 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003035
Craig Mautnerc8143c62013-09-03 12:15:57 -07003036 // There is an assumption that moving a task to the back moves it behind the home activity.
3037 // We make sure here that some activity in the stack will launch home.
3038 ActivityRecord lastActivity = null;
3039 int numTasks = mTaskHistory.size();
3040 int taskNdx;
3041 for (taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3042 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3043 int activityNdx;
3044 for (activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3045 final ActivityRecord r = activities.get(activityNdx);
3046 if (r.mLaunchHomeTaskNext) {
3047 break;
3048 }
3049 if (taskNdx == 1 && activityNdx == 0) {
3050 // Final activity before tr task.
3051 lastActivity = r;
3052 }
3053 }
3054 if (activityNdx >= 0) {
3055 // Early exit, we found an activity that will launchHomeTaskNext.
3056 break;
3057 }
3058 }
3059 if (lastActivity != null) {
3060 // No early exit, we did not find an activity that will launchHomeTaskNext, set one.
3061 lastActivity.mLaunchHomeTaskNext = true;
3062 }
3063
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003064 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003065 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3066 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003067 ActivityRecord r = topRunningActivityLocked(null);
3068 if (r != null) {
3069 mNoAnimActivities.add(r);
3070 }
3071 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003072 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003073 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003074 mWindowManager.moveTaskToBottom(task);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003075
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003076 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003077 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003078 }
3079
Craig Mautnerc8143c62013-09-03 12:15:57 -07003080 if (numTasks <= 1 || (mResumedActivity != null && mResumedActivity.task == tr &&
3081 mResumedActivity.mLaunchHomeTaskNext)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003082 mResumedActivity.mLaunchHomeTaskNext = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003083 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003084 }
3085
Craig Mautner05d29032013-05-03 13:40:13 -07003086 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003087 return true;
3088 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003089
Craig Mautner8849a5e2013-04-02 16:41:03 -07003090 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003091 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003092 final Uri data = r.intent.getData();
3093 final String strData = data != null ? data.toSafeString() : null;
3094
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003095 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003096 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003097 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003098 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003099 }
3100
3101 /**
3102 * Make sure the given activity matches the current configuration. Returns
3103 * false if the activity had to be destroyed. Returns true if the
3104 * configuration is the same, or the activity will remain running as-is
3105 * for whatever reason. Ensures the HistoryRecord is updated with the
3106 * correct configuration and all other bookkeeping is handled.
3107 */
3108 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3109 int globalChanges) {
3110 if (mConfigWillChange) {
3111 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3112 "Skipping config check (will change): " + r);
3113 return true;
3114 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003115
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003116 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3117 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003118
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003119 // Short circuit: if the two configurations are the exact same
3120 // object (the common case), then there is nothing to do.
3121 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003122 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003123 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3124 "Configuration unchanged in " + r);
3125 return true;
3126 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003127
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003128 // We don't worry about activities that are finishing.
3129 if (r.finishing) {
3130 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3131 "Configuration doesn't matter in finishing " + r);
3132 r.stopFreezingScreenLocked(false);
3133 return true;
3134 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003135
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003136 // Okay we now are going to make this activity have the new config.
3137 // But then we need to figure out how it needs to deal with that.
3138 Configuration oldConfig = r.configuration;
3139 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003140
3141 // Determine what has changed. May be nothing, if this is a config
3142 // that has come back from the app after going idle. In that case
3143 // we just want to leave the official config object now in the
3144 // activity and do nothing else.
3145 final int changes = oldConfig.diff(newConfig);
3146 if (changes == 0 && !r.forceNewConfig) {
3147 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3148 "Configuration no differences in " + r);
3149 return true;
3150 }
3151
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003152 // If the activity isn't currently running, just leave the new
3153 // configuration and it will pick that up next time it starts.
3154 if (r.app == null || r.app.thread == null) {
3155 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3156 "Configuration doesn't matter not running " + r);
3157 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003158 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003159 return true;
3160 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003161
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003162 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003163 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3164 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3165 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003166 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003167 + ", newConfig=" + newConfig);
3168 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003169 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003170 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3171 r.configChangeFlags |= changes;
3172 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003173 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003174 if (r.app == null || r.app.thread == null) {
3175 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003176 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003177 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003178 } else if (r.state == ActivityState.PAUSING) {
3179 // A little annoying: we are waiting for this activity to
3180 // finish pausing. Let's not do anything now, but just
3181 // flag that it needs to be restarted when done pausing.
3182 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003183 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003184 r.configDestroy = true;
3185 return true;
3186 } else if (r.state == ActivityState.RESUMED) {
3187 // Try to optimize this case: the configuration is changing
3188 // and we need to restart the top, resumed activity.
3189 // Instead of doing the normal handshaking, just say
3190 // "restart!".
3191 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003192 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003193 relaunchActivityLocked(r, r.configChangeFlags, true);
3194 r.configChangeFlags = 0;
3195 } else {
3196 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003197 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003198 relaunchActivityLocked(r, r.configChangeFlags, false);
3199 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003200 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003201
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003202 // All done... tell the caller we weren't able to keep this
3203 // activity around.
3204 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003205 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003206
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003207 // Default case: the activity can handle this new configuration, so
3208 // hand it over. Note that we don't need to give it the new
3209 // configuration, since we always send configuration changes to all
3210 // process when they happen so it can just use whatever configuration
3211 // it last got.
3212 if (r.app != null && r.app.thread != null) {
3213 try {
3214 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003215 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003216 } catch (RemoteException e) {
3217 // If process died, whatever.
3218 }
3219 }
3220 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003221
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003222 return true;
3223 }
3224
Craig Mautnerc8143c62013-09-03 12:15:57 -07003225 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003226 int changes, boolean andResume) {
3227 List<ResultInfo> results = null;
3228 List<Intent> newIntents = null;
3229 if (andResume) {
3230 results = r.results;
3231 newIntents = r.newIntents;
3232 }
3233 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3234 + " with results=" + results + " newIntents=" + newIntents
3235 + " andResume=" + andResume);
3236 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003237 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003238 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003239
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003240 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003241
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003242 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003243 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3244 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3245 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003246 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003247 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003248 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003249 // Note: don't need to call pauseIfSleepingLocked() here, because
3250 // the caller will only pass in 'andResume' if this activity is
3251 // currently resumed, which implies we aren't sleeping.
3252 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003253 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003254 }
3255
3256 if (andResume) {
3257 r.results = null;
3258 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003259 r.state = ActivityState.RESUMED;
3260 } else {
3261 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3262 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003263 }
3264
3265 return true;
3266 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003267
3268 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003269 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3270 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3271 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3272 final ActivityRecord r = activities.get(activityNdx);
3273 if (r.appToken == token) {
3274 return true;
3275 }
3276 if (r.fullscreen && !r.finishing) {
3277 return false;
3278 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003279 }
3280 }
3281 return true;
3282 }
3283
3284 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003285 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3286 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3287 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3288 final ActivityRecord r = activities.get(activityNdx);
3289 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003290 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003291 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003292 }
3293 }
3294 }
3295
3296 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3297 boolean didSomething = false;
3298 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003299 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3300 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3301 int numActivities = activities.size();
3302 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3303 ActivityRecord r = activities.get(activityNdx);
3304 final boolean samePackage = r.packageName.equals(name)
3305 || (name == null && r.userId == userId);
3306 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3307 && (samePackage || r.task == lastTask)
3308 && (r.app == null || evenPersistent || !r.app.persistent)) {
3309 if (!doit) {
3310 if (r.finishing) {
3311 // If this activity is just finishing, then it is not
3312 // interesting as far as something to stop.
3313 continue;
3314 }
3315 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003316 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003317 didSomething = true;
3318 Slog.i(TAG, " Force finishing activity " + r);
3319 if (samePackage) {
3320 if (r.app != null) {
3321 r.app.removed = true;
3322 }
3323 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003324 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003325 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003326 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3327 true)) {
3328 // r has been deleted from mActivities, accommodate.
3329 --numActivities;
3330 --activityNdx;
3331 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003332 }
3333 }
3334 }
3335 return didSomething;
3336 }
3337
3338 ActivityRecord getTasksLocked(int maxNum, IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003339 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003340 ActivityRecord topRecord = null;
Craig Mautneraab647e2013-02-28 16:31:36 -08003341 for (int taskNdx = mTaskHistory.size() - 1; maxNum > 0 && taskNdx >= 0;
3342 --maxNum, --taskNdx) {
3343 final TaskRecord task = mTaskHistory.get(taskNdx);
3344 ActivityRecord r = null;
3345 ActivityRecord top = null;
3346 int numActivities = 0;
3347 int numRunning = 0;
3348 final ArrayList<ActivityRecord> activities = task.mActivities;
3349 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3350 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003351
Craig Mautneraab647e2013-02-28 16:31:36 -08003352 // Initialize state for next task if needed.
3353 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3354 top = r;
3355 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003356 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003357
3358 // Add 'r' into the current task.
3359 numActivities++;
3360 if (r.app != null && r.app.thread != null) {
3361 numRunning++;
3362 }
3363
3364 if (localLOGV) Slog.v(
3365 TAG, r.intent.getComponent().flattenToShortString()
3366 + ": task=" + r.task);
3367 }
3368
3369 RunningTaskInfo ci = new RunningTaskInfo();
3370 ci.id = task.taskId;
3371 ci.baseActivity = r.intent.getComponent();
3372 ci.topActivity = top.intent.getComponent();
3373 if (top.thumbHolder != null) {
3374 ci.description = top.thumbHolder.lastDescription;
3375 }
3376 ci.numActivities = numActivities;
3377 ci.numRunning = numRunning;
3378 //System.out.println(
3379 // "#" + maxNum + ": " + " descr=" + ci.description);
3380 if (receiver != null) {
3381 if (localLOGV) Slog.v(
3382 TAG, "State=" + top.state + "Idle=" + top.idle
3383 + " app=" + top.app
3384 + " thr=" + (top.app != null ? top.app.thread : null));
3385 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3386 if (top.idle && top.app != null && top.app.thread != null) {
3387 topRecord = top;
3388 } else {
3389 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003390 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003391 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003392 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003393 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003394 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003395 }
3396 return topRecord;
3397 }
3398
3399 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003400 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003401 if (DEBUG_SWITCH) Slog.d(
3402 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003403 if (top >= 0) {
3404 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3405 int activityTop = activities.size() - 1;
3406 if (activityTop > 0) {
3407 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3408 "unhandled-back", true);
3409 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003410 }
3411 }
3412
Craig Mautnere79d42682013-04-01 19:01:53 -07003413 void handleAppDiedLocked(ProcessRecord app, boolean restarting) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003414 if (!containsApp(app)) {
3415 return;
3416 }
3417 // TODO: handle the case where an app spans multiple stacks.
Craig Mautnere79d42682013-04-01 19:01:53 -07003418 if (mPausingActivity != null && mPausingActivity.app == app) {
3419 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3420 "App died while pausing: " + mPausingActivity);
3421 mPausingActivity = null;
3422 }
3423 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3424 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003425 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003426 }
Craig Mautner37f748d2013-06-24 16:19:55 -07003427 final ActivityRecord top = topRunningActivityLocked(null);
3428 final boolean launchHomeTaskNext =
3429 top != null && top.app == app && top.mLaunchHomeTaskNext;
Craig Mautnere79d42682013-04-01 19:01:53 -07003430
3431 // Remove this application's activities from active lists.
3432 boolean hasVisibleActivities = removeHistoryRecordsForAppLocked(app);
3433
3434 if (!restarting) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003435 ActivityStack stack = mStackSupervisor.getFocusedStack();
Craig Mautner37f748d2013-06-24 16:19:55 -07003436 if (stack == null || launchHomeTaskNext) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003437 mStackSupervisor.resumeHomeActivity(null);
Craig Mautner05d29032013-05-03 13:40:13 -07003438 } else if (!mStackSupervisor.resumeTopActivitiesLocked(stack, null, null)) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003439 // If there was nothing to resume, and we are not already
3440 // restarting this process, but there is a visible activity that
3441 // is hosted by the process... then make sure all visible
3442 // activities are running, taking care of restarting this
3443 // process.
3444 if (hasVisibleActivities) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003445 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
Craig Mautnere79d42682013-04-01 19:01:53 -07003446 }
3447 }
3448 }
3449 }
3450
Craig Mautnercae015f2013-02-08 14:31:27 -08003451 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003452 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3453 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3454 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3455 final ActivityRecord r = activities.get(activityNdx);
3456 if (r.app == app) {
3457 Slog.w(TAG, " Force finishing activity "
3458 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003459 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003460 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003461 }
3462 }
3463 }
3464
Dianne Hackborn390517b2013-05-30 15:03:32 -07003465 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003466 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003467 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003468 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3469 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003470 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3471 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003472 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003473 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003474 if (printed) {
3475 header = null;
3476 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003477 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003478 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003479 }
3480
3481 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3482 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3483
3484 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003485 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3486 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003487 }
3488 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003489 final int top = mTaskHistory.size() - 1;
3490 if (top >= 0) {
3491 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3492 int listTop = list.size() - 1;
3493 if (listTop >= 0) {
3494 activities.add(list.get(listTop));
3495 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003496 }
3497 } else {
3498 ItemMatcher matcher = new ItemMatcher();
3499 matcher.build(name);
3500
Craig Mautneraab647e2013-02-28 16:31:36 -08003501 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3502 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3503 if (matcher.match(r1, r1.intent.getComponent())) {
3504 activities.add(r1);
3505 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003506 }
3507 }
3508 }
3509
3510 return activities;
3511 }
3512
3513 ActivityRecord restartPackage(String packageName) {
3514 ActivityRecord starting = topRunningActivityLocked(null);
3515
3516 // All activities that came from the package must be
3517 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003518 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3519 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3520 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3521 final ActivityRecord a = activities.get(activityNdx);
3522 if (a.info.packageName.equals(packageName)) {
3523 a.forceNewConfig = true;
3524 if (starting != null && a == starting && a.visible) {
3525 a.startFreezingScreenLocked(starting.app,
3526 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3527 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003528 }
3529 }
3530 }
3531
3532 return starting;
3533 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003534
Craig Mautnerde4ef022013-04-07 19:01:33 -07003535 boolean removeTask(TaskRecord task) {
3536 mTaskHistory.remove(task);
Craig Mautnerc8143c62013-09-03 12:15:57 -07003537 return mTaskHistory.isEmpty();
Craig Mautner0247fc82013-02-28 14:32:06 -08003538 }
3539
Craig Mautnerde4ef022013-04-07 19:01:33 -07003540 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003541 TaskRecord task = new TaskRecord(taskId, info, intent);
3542 addTask(task, toTop);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003543 return task;
3544 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003545
3546 ArrayList<TaskRecord> getAllTasks() {
3547 return new ArrayList<TaskRecord>(mTaskHistory);
3548 }
3549
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003550 void addTask(final TaskRecord task, final boolean toTop) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003551 task.stack = this;
3552 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003553 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003554 } else {
3555 mTaskHistory.add(0, task);
3556 }
3557 }
3558
3559 public int getStackId() {
3560 return mStackId;
3561 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003562
3563 @Override
3564 public String toString() {
3565 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3566 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003567}