blob: 40ce8493d9e1f78c0cc4e30674e20553e1ca945d [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;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070072import android.os.RemoteException;
73import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070074import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070077import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078
Craig Mautnercae015f2013-02-08 14:31:27 -080079import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080080import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070081import java.lang.ref.WeakReference;
82import java.util.ArrayList;
83import java.util.Iterator;
84import java.util.List;
85
86/**
87 * State and management of a single stack of activities.
88 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070089final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080090
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070091 // Ticks during which we check progress while waiting for an app to launch.
92 static final int LAUNCH_TICK = 500;
93
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070094 // How long we wait until giving up on the last activity to pause. This
95 // is short because it directly impacts the responsiveness of starting the
96 // next activity.
97 static final int PAUSE_TIMEOUT = 500;
98
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070099 // How long we wait for the activity to tell us it has stopped before
100 // giving up. This is a good amount of time because we really need this
101 // from the application in order to get its saved state.
102 static final int STOP_TIMEOUT = 10*1000;
103
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700104 // How long we wait until giving up on an activity telling us it has
105 // finished destroying itself.
106 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800107
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700108 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800109 // disabled.
110 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800111
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700112 // How long between activity launches that we consider safe to not warn
113 // the user about an unexpected activity being launched on top.
114 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800115
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700116 // Set to false to disable the preview that is shown while a new activity
117 // is being started.
118 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800119
Craig Mautner5eda9b32013-07-02 11:58:16 -0700120 // How long to wait for all background Activities to redraw following a call to
121 // convertToTranslucent().
122 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
123
John Reck172e87c2013-10-02 16:55:16 -0700124 static final boolean SCREENSHOT_FORCE_565 = ActivityManager
125 .isLowRamDeviceStatic() ? true : false;
126
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700127 enum ActivityState {
128 INITIALIZING,
129 RESUMED,
130 PAUSING,
131 PAUSED,
132 STOPPING,
133 STOPPED,
134 FINISHING,
135 DESTROYING,
136 DESTROYED
137 }
138
139 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700140 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800141
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700142 final Context mContext;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800143
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700144 /**
145 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800146 * running) activities. It contains #TaskRecord objects.
147 */
148 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
149
150 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800151 * Used for validating app tokens with window manager.
152 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800153 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800154
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700155 /**
156 * List of running activities, sorted by recent usage.
157 * The first entry in the list is the least recently used.
158 * It contains HistoryRecord objects.
159 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700160 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700161
162 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700163 * Animations that for the current transition have requested not to
164 * be considered for the transition animation.
165 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700166 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700167
168 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700169 * When we are in the process of pausing an activity, before starting the
170 * next one, this variable holds the activity that is currently being paused.
171 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800172 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700173
174 /**
175 * This is the last activity that we put into the paused state. This is
176 * used to determine if we need to do an activity transition while sleeping,
177 * when we normally hold the top activity paused.
178 */
179 ActivityRecord mLastPausedActivity = null;
180
181 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700182 * Activities that specify No History must be removed once the user navigates away from them.
183 * If the device goes to sleep with such an activity in the paused state then we save it here
184 * and finish it later if another activity replaces it on wakeup.
185 */
186 ActivityRecord mLastNoHistoryActivity = null;
187
188 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700189 * Current activity that is resumed, or null if there is none.
190 */
191 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800192
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700193 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700194 * This is the last activity that has been started. It is only used to
195 * identify when multiple activities are started at once so that the user
196 * can be warned they may not be in the activity they think they are.
197 */
198 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800199
Craig Mautner5eda9b32013-07-02 11:58:16 -0700200 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
201 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
202 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
203 // Activity in mTranslucentActivityWaiting is notified via
204 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
205 // background activity being drawn then the same call will be made with a true value.
206 ActivityRecord mTranslucentActivityWaiting = null;
207 ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
208 new ArrayList<ActivityRecord>();
209
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700210 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700211 * Set when we know we are going to be calling updateConfiguration()
212 * soon, so want to skip intermediate config checks.
213 */
214 boolean mConfigWillChange;
215
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700216 long mLaunchStartTime = 0;
217 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800218
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800219 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800220 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
221 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
222 */
223 private ActivityRecord mLastScreenshotActivity = null;
224 private Bitmap mLastScreenshotBitmap = null;
225
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800226 int mThumbnailWidth = -1;
227 int mThumbnailHeight = -1;
228
Craig Mautner858d8a62013-04-23 17:08:34 -0700229 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700230
Craig Mautnerc00204b2013-03-05 15:02:14 -0800231 final int mStackId;
232
Craig Mautner27084302013-03-25 08:05:25 -0700233 /** Run all ActivityStacks through this */
234 final ActivityStackSupervisor mStackSupervisor;
235
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700236 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700237 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
238 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
239 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
240 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700241 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700242
243 static class ScheduleDestroyArgs {
244 final ProcessRecord mOwner;
245 final boolean mOomAdj;
246 final String mReason;
247 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
248 mOwner = owner;
249 mOomAdj = oomAdj;
250 mReason = reason;
251 }
252 }
253
Zoran Marcetaf958b322012-08-09 20:27:12 +0900254 final Handler mHandler;
255
256 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700257 //public Handler() {
258 // if (localLOGV) Slog.v(TAG, "Handler started!");
259 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700260 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900261 super(looper);
262 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700263
Zoran Marcetaf958b322012-08-09 20:27:12 +0900264 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700265 public void handleMessage(Message msg) {
266 switch (msg.what) {
267 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800268 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700269 // We don't at this point know if the activity is fullscreen,
270 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800271 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700272 synchronized (mService) {
273 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700274 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700275 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700276 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800277 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700278 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700279 case LAUNCH_TICK_MSG: {
280 ActivityRecord r = (ActivityRecord)msg.obj;
281 synchronized (mService) {
282 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700283 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700284 }
285 }
286 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700287 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800288 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700289 // We don't at this point know if the activity is fullscreen,
290 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800291 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800292 synchronized (mService) {
293 activityDestroyedLocked(r != null ? r.appToken : null);
294 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700295 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700296 case STOP_TIMEOUT_MSG: {
297 ActivityRecord r = (ActivityRecord)msg.obj;
298 // We don't at this point know if the activity is fullscreen,
299 // so we need to be conservative and assume it isn't.
300 Slog.w(TAG, "Activity stop timeout for " + r);
301 synchronized (mService) {
302 if (r.isInHistory()) {
303 activityStoppedLocked(r, null, null, null);
304 }
305 }
306 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700307 case DESTROY_ACTIVITIES_MSG: {
308 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
309 synchronized (mService) {
310 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
311 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700312 } break;
313 case TRANSLUCENT_TIMEOUT_MSG: {
314 synchronized (mService) {
315 notifyActivityDrawnLocked(null);
316 }
317 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700318 }
319 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800320 }
321
Craig Mautner000f0022013-02-26 15:04:29 -0800322 private int numActivities() {
323 int count = 0;
324 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
325 count += mTaskHistory.get(taskNdx).mActivities.size();
326 }
327 return count;
328 }
329
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700330 ActivityStack(ActivityManagerService service, Context context, Looper looper, int stackId) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900331 mHandler = new ActivityStackHandler(looper);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700332 mService = service;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700333 mWindowManager = service.mWindowManager;
334 mStackSupervisor = service.mStackSupervisor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700335 mContext = context;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800336 mStackId = stackId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700337 mCurrentUser = service.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700338 }
Craig Mautner5962b122012-10-05 14:45:52 -0700339
Craig Mautner6b74cb52013-09-27 17:02:21 -0700340 boolean okToShow(ActivityRecord r) {
Craig Mautner5962b122012-10-05 14:45:52 -0700341 return r.userId == mCurrentUser
342 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
343 }
344
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700345 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800346 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700347 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
348 if (r != null) {
349 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800350 }
351 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700352 return null;
353 }
354
355 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800356 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
357 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800358 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800359 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
360 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800361 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
362 return r;
363 }
364 }
365 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700366 return null;
367 }
368
369 /**
370 * This is a simplified version of topRunningActivityLocked that provides a number of
371 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800372 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700373 * @param token If non-null, any history records matching this token will be skipped.
374 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800375 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700376 * @return Returns the HistoryRecord of the next activity on the stack.
377 */
378 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800379 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
380 TaskRecord task = mTaskHistory.get(taskNdx);
381 if (task.taskId == taskId) {
382 continue;
383 }
384 ArrayList<ActivityRecord> activities = task.mActivities;
385 for (int i = activities.size() - 1; i >= 0; --i) {
386 final ActivityRecord r = activities.get(i);
387 // Note: the taskId check depends on real taskId fields being non-zero
388 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
389 return r;
390 }
391 }
392 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700393 return null;
394 }
395
Craig Mautner8849a5e2013-04-02 16:41:03 -0700396 final ActivityRecord topActivity() {
397 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700398 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700399 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
400 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
401 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700402 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700403 }
404 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700405 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700406 }
407
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700408 final TaskRecord topTask() {
409 final int size = mTaskHistory.size();
410 if (size > 0) {
411 return mTaskHistory.get(size - 1);
412 }
413 return null;
414 }
415
Craig Mautnerd2328952013-03-05 12:46:26 -0800416 TaskRecord taskForIdLocked(int id) {
417 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
418 final TaskRecord task = mTaskHistory.get(taskNdx);
419 if (task.taskId == id) {
420 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800421 }
422 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700423 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700424 }
425
Craig Mautnerd2328952013-03-05 12:46:26 -0800426 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800427 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800428 if (r != null) {
429 final TaskRecord task = r.task;
430 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
431 if (task.stack != this) Slog.w(TAG,
432 "Illegal state! task does not point to stack it is in.");
433 return r;
434 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800435 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800436 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800437 }
438
Craig Mautnerf88c50f2013-04-18 19:25:12 -0700439 boolean containsApp(ProcessRecord app) {
440 if (app == null) {
441 return false;
442 }
443 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
444 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
445 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
446 final ActivityRecord r = activities.get(activityNdx);
447 if (r.finishing) {
448 continue;
449 }
450 if (r.app == app) {
451 return true;
452 }
453 }
454 }
455 return false;
456 }
457
Craig Mautner2420ead2013-04-01 17:13:20 -0700458 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700459 final boolean hadit = mLRUActivities.remove(r);
460 mLRUActivities.add(r);
461 return hadit;
462 }
463
Craig Mautnerde4ef022013-04-07 19:01:33 -0700464 final boolean isHomeStack() {
465 return mStackId == HOME_STACK_ID;
466 }
467
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700468 /**
469 * Returns the top activity in any existing task matching the given
470 * Intent. Returns null if no such task is found.
471 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700472 ActivityRecord findTaskLocked(ActivityRecord target) {
473 Intent intent = target.intent;
474 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700475 ComponentName cls = intent.getComponent();
476 if (info.targetActivity != null) {
477 cls = new ComponentName(info.packageName, info.targetActivity);
478 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700479 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800480
481 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
482 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700483 if (task.userId != userId) {
484 // Looking for a different task.
485 continue;
486 }
Craig Mautner000f0022013-02-26 15:04:29 -0800487 final ActivityRecord r = task.getTopActivity();
488 if (r == null || r.finishing || r.userId != userId ||
489 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
490 continue;
491 }
492
493 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
494 // + "/aff=" + r.task.affinity + " to new cls="
495 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
496 if (task.affinity != null) {
497 if (task.affinity.equals(info.taskAffinity)) {
498 //Slog.i(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700499 return r;
500 }
Craig Mautner000f0022013-02-26 15:04:29 -0800501 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
502 //Slog.i(TAG, "Found matching class!");
503 //dump();
504 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
505 return r;
506 } else if (task.affinityIntent != null
507 && task.affinityIntent.getComponent().equals(cls)) {
508 //Slog.i(TAG, "Found matching class!");
509 //dump();
510 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
511 return r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700512 }
513 }
514
515 return null;
516 }
517
518 /**
519 * Returns the first activity (starting from the top of the stack) that
520 * is the same as the given activity. Returns null if no such activity
521 * is found.
522 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700523 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700524 ComponentName cls = intent.getComponent();
525 if (info.targetActivity != null) {
526 cls = new ComponentName(info.packageName, info.targetActivity);
527 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700528 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700529
Craig Mautner000f0022013-02-26 15:04:29 -0800530 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700531 TaskRecord task = mTaskHistory.get(taskNdx);
532 if (task.userId != mCurrentUser) {
533 return null;
534 }
535 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800536 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
537 ActivityRecord r = activities.get(activityNdx);
538 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700539 //Slog.i(TAG, "Found matching class!");
540 //dump();
541 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
542 return r;
543 }
544 }
545 }
546
547 return null;
548 }
549
Amith Yamasani742a6712011-05-04 14:49:28 -0700550 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700551 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700552 */
Craig Mautner93529a42013-10-04 15:03:13 -0700553 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800554 if (VALIDATE_TOKENS) {
555 validateAppTokensLocked();
556 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800557 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700558 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800559 }
560 mCurrentUser = userId;
561
562 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800563 int index = mTaskHistory.size();
564 for (int i = 0; i < index; ++i) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700565 TaskRecord task = mTaskHistory.get(i);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800566 if (task.userId == userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800567 mTaskHistory.remove(i);
568 mTaskHistory.add(task);
569 --index;
570 }
571 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700572 }
573
Craig Mautner2420ead2013-04-01 17:13:20 -0700574 void minimalResumeActivityLocked(ActivityRecord r) {
575 r.state = ActivityState.RESUMED;
576 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
577 + " (starting new instance)");
578 r.stopped = false;
579 mResumedActivity = r;
580 r.task.touchActiveTime();
581 mService.addRecentTaskLocked(r.task);
582 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700583 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -0700584 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700585 }
586
Dianne Hackborncee04b52013-07-03 17:01:28 -0700587 private void startLaunchTraces() {
588 if (mFullyDrawnStartTime != 0) {
589 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
590 }
591 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
592 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
593 }
594
595 private void stopFullyDrawnTraceIfNeeded() {
596 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
597 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
598 mFullyDrawnStartTime = 0;
599 }
600 }
601
Craig Mautnere79d42682013-04-01 19:01:53 -0700602 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700603 if (r.displayStartTime == 0) {
604 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
605 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700606 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700607 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700608 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700609 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700610 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700611 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700612 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700613 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800614
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700615 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700616 // Make sure that there is no activity waiting for this to launch.
617 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
618 r.displayStartTime = r.fullyDrawnStartTime = 0;
619 } else {
620 mStackSupervisor.removeTimeoutsForActivityLocked(r);
621 mStackSupervisor.scheduleIdleTimeoutLocked(r);
622 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700623 }
624
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800625 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800626 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800627 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
628 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
629 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
630 activities.get(activityNdx).setSleeping(false);
631 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800632 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800633 }
634
Craig Mautner0eea92c2013-05-16 13:35:39 -0700635 /**
636 * @return true if something must be done before going to sleep.
637 */
638 boolean checkReadyForSleepLocked() {
639 if (mResumedActivity != null) {
640 // Still have something resumed; can't sleep until it is paused.
641 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
642 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
643 startPausingLocked(false, true);
644 return true;
645 }
646 if (mPausingActivity != null) {
647 // Still waiting for something to pause; can't sleep yet.
648 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
649 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800650 }
651
Craig Mautner0eea92c2013-05-16 13:35:39 -0700652 return false;
653 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800654
Craig Mautner0eea92c2013-05-16 13:35:39 -0700655 void goToSleep() {
656 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800657
Craig Mautner0eea92c2013-05-16 13:35:39 -0700658 // Make sure any stopped but visible activities are now sleeping.
659 // This ensures that the activity's onStop() is called.
660 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
661 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
662 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
663 final ActivityRecord r = activities.get(activityNdx);
664 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
665 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800666 }
667 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800668 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700669 }
Craig Mautner59c00972012-07-30 12:10:24 -0700670
Dianne Hackbornd2835932010-12-13 16:28:46 -0800671 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800672 if (who.noDisplay) {
673 return null;
674 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800675
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700676 TaskRecord tr = who.task;
Craig Mautner34740402013-10-11 11:05:35 -0700677 if (mService.getMostRecentTask() != tr && tr.intent != null &&
678 (tr.intent.getFlags() & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) != 0) {
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700679 // If this task is being excluded from recents, we don't want to take
680 // the expense of capturing a thumbnail, since we will never show it.
681 return null;
682 }
683
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800684 Resources res = mService.mContext.getResources();
685 int w = mThumbnailWidth;
686 int h = mThumbnailHeight;
687 if (w < 0) {
688 mThumbnailWidth = w =
689 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
690 mThumbnailHeight = h =
691 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
692 }
693
694 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800695 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700696 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800697 || mLastScreenshotBitmap.getWidth() != w
698 || mLastScreenshotBitmap.getHeight() != h) {
699 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700700 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700701 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800702 }
703 if (mLastScreenshotBitmap != null) {
John Reck172e87c2013-10-02 16:55:16 -0700704 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800705 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800706 }
707 return null;
708 }
709
Craig Mautnercf910b02013-04-23 11:23:27 -0700710 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800711 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800712 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700713 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800714 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700715 ActivityRecord prev = mResumedActivity;
716 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700717 Slog.e(TAG, "Trying to pause when nothing is resumed",
718 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700719 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700720 return;
721 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700722 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
723 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700724 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800725 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700726 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700727 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
728 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700729 prev.state = ActivityState.PAUSING;
730 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700731 clearLaunchTime(prev);
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700732 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700733 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700734
735 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800736
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700737 if (prev.app != null && prev.app.thread != null) {
738 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
739 try {
740 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700741 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700742 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700743 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800744 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
745 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700746 } catch (Exception e) {
747 // Ignore exception, if process died other code will cleanup.
748 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800749 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700750 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700751 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700752 }
753 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800754 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700755 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700756 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700757 }
758
759 // If we are not going to sleep, we want to ensure the device is
760 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700761 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700762 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700763 }
764
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800765 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700766 // Have the window manager pause its key dispatching until the new
767 // activity has started. If we're pausing the activity just because
768 // the screen is being turned off and the UI is sleeping, don't interrupt
769 // key dispatch; the same activity will pick it up again on wakeup.
770 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800771 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700772 } else {
773 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
774 }
775
776 // Schedule a pause timeout in case the app doesn't respond.
777 // We don't give it much time because this directly impacts the
778 // responsiveness seen by the user.
779 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
780 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700781 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700782 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
783 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
784 } else {
785 // This activity failed to schedule the
786 // pause, so just treat it as being paused now.
787 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700788 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700789 }
790 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700791
Craig Mautnerd2328952013-03-05 12:46:26 -0800792 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700793 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800794 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700795
Craig Mautnerd2328952013-03-05 12:46:26 -0800796 final ActivityRecord r = isInStackLocked(token);
797 if (r != null) {
798 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
799 if (mPausingActivity == r) {
800 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
801 + (timeout ? " (due to timeout)" : " (pause complete)"));
802 r.state = ActivityState.PAUSED;
803 completePauseLocked();
804 } else {
805 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
806 r.userId, System.identityHashCode(r), r.shortComponentName,
807 mPausingActivity != null
808 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700809 }
810 }
811 }
812
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700813 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
814 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700815 if (r.state != ActivityState.STOPPING) {
816 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
817 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
818 return;
819 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700820 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700821 if (icicle != null) {
822 // If icicle is null, this is happening due to a timeout, so we
823 // haven't really saved the state.
824 r.icicle = icicle;
825 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800826 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700827 r.updateThumbnail(thumbnail, description);
828 }
829 if (!r.stopped) {
830 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
831 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
832 r.stopped = true;
833 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700834 if (r.finishing) {
835 r.clearOptionsLocked();
836 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700837 if (r.configDestroy) {
838 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700839 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700840 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700841 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800842 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700843 }
844 }
845 }
846
Craig Mautnerc8143c62013-09-03 12:15:57 -0700847 private void completePauseLocked() {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800848 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700849 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800850
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800851 if (prev != null) {
852 if (prev.finishing) {
853 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700854 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800855 } else if (prev.app != null) {
856 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
857 if (prev.waitingVisible) {
858 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700859 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800860 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
861 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800862 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800863 if (prev.configDestroy) {
864 // The previous is being paused because the configuration
865 // is changing, which means it is actually stopping...
866 // To juggle the fact that we are also starting a new
867 // instance right now, we need to first completely stop
868 // the current instance before starting the new one.
869 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
870 destroyActivityLocked(prev, true, false, "pause-config");
871 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700872 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700873 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
874 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800875 // If we already have a few activities waiting to stop,
876 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700877 // them out. Or if r is the last of activity of the last task the stack
878 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800879 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700880 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800881 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700882 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800883 }
884 }
885 } else {
886 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
887 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800888 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800889 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700890 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800891
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700892 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700893 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700894 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800895 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700896 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700897 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700898 if (top == null || (prev != null && top != prev)) {
899 // If there are no more activities available to run,
900 // do resume anyway to start something. Also if the top
901 // activity on the stack is not the just paused activity,
902 // we need to go ahead and resume it to ensure we complete
903 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700904 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700905 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700906 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800907
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800908 if (prev != null) {
909 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700910
Craig Mautner525f3d92013-05-07 14:01:50 -0700911 if (prev.app != null && prev.cpuTimeAtResume > 0
912 && mService.mBatteryStatsService.isOnBattery()) {
913 long diff;
Dianne Hackbornd2932242013-08-05 18:18:42 -0700914 synchronized (mService.mProcessCpuThread) {
915 diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
Craig Mautner525f3d92013-05-07 14:01:50 -0700916 - prev.cpuTimeAtResume;
917 }
918 if (diff > 0) {
919 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
920 synchronized (bsi) {
921 BatteryStatsImpl.Uid.Proc ps =
922 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -0700923 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -0700924 if (ps != null) {
925 ps.addForegroundTimeLocked(diff);
926 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700927 }
928 }
929 }
Craig Mautner525f3d92013-05-07 14:01:50 -0700930 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700931 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700932 }
933
934 /**
935 * Once we know that we have asked an application to put an activity in
936 * the resumed state (either by launching it or explicitly telling it),
937 * this function updates the rest of our state to match that fact.
938 */
Craig Mautner525f3d92013-05-07 14:01:50 -0700939 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700940 next.idle = false;
941 next.results = null;
942 next.newIntents = null;
Craig Mautner07566322013-09-26 16:42:55 -0700943 if (next.nowVisible) {
944 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
945 mStackSupervisor.dismissKeyguard();
946 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700947
948 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -0700949 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700950
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700951 mStackSupervisor.reportResumedActivityLocked(next);
952
953 next.resumeKeyDispatchingLocked();
954 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700955
956 // Mark the point when the activity is resuming
957 // TODO: To be more accurate, the mark should be before the onCreate,
958 // not after the onResume. But for subsequent starts, onResume is fine.
959 if (next.app != null) {
Dianne Hackbornd2932242013-08-05 18:18:42 -0700960 synchronized (mService.mProcessCpuThread) {
961 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700962 }
963 } else {
964 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
965 }
966 }
967
Craig Mautner580ea812013-04-25 12:58:38 -0700968 /**
969 * Version of ensureActivitiesVisible that can easily be called anywhere.
970 */
971 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
972 return ensureActivitiesVisibleLocked(starting, configChanges, false);
973 }
974
975 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
976 boolean forceHomeShown) {
977 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -0700978 return r != null &&
979 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -0700980 }
981
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700982 /**
983 * Make sure that all activities that need to be visible (that is, they
984 * currently can be seen by the user) actually are.
985 */
Craig Mautner580ea812013-04-25 12:58:38 -0700986 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
987 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Craig Mautner323f7802013-10-01 21:16:22 -0700988 if (true || DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700989 TAG, "ensureActivitiesVisible behind " + top
990 + " configChanges=0x" + Integer.toHexString(configChanges));
991
Craig Mautner5eda9b32013-07-02 11:58:16 -0700992 if (mTranslucentActivityWaiting != top) {
993 mUndrawnActivitiesBelowTopTranslucent.clear();
994 if (mTranslucentActivityWaiting != null) {
995 // Call the callback with a timeout indication.
996 notifyActivityDrawnLocked(null);
997 mTranslucentActivityWaiting = null;
998 }
999 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1000 }
1001
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001002 // If the top activity is not fullscreen, then we need to
1003 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001004 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -07001005 boolean showHomeBehindStack = false;
1006 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
1007 !(forceHomeShown && isHomeStack());
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001008 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001009 final TaskRecord task = mTaskHistory.get(taskNdx);
1010 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001011 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1012 final ActivityRecord r = activities.get(activityNdx);
1013 if (r.finishing) {
1014 continue;
1015 }
1016 if (aboveTop && r != top) {
1017 continue;
1018 }
1019 aboveTop = false;
1020 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001021 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001022 TAG, "Make visible? " + r + " finishing=" + r.finishing
1023 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001024
Craig Mautnerd44711d2013-02-23 11:24:36 -08001025 final boolean doThisProcess = onlyThisProcess == null
1026 || onlyThisProcess.equals(r.processName);
1027
1028 // First: if this is not the current activity being started, make
1029 // sure it matches the current configuration.
1030 if (r != starting && doThisProcess) {
1031 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001032 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001033
1034 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001035 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001036 // This activity needs to be visible, but isn't even
1037 // running... get it started, but don't resume it
1038 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001039 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001040 if (r != starting) {
1041 r.startFreezingScreenLocked(r.app, configChanges);
1042 }
1043 if (!r.visible) {
Craig Mautner323f7802013-10-01 21:16:22 -07001044 if (true || DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001045 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001046 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001047 }
1048 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001049 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001050 }
1051 }
1052
1053 } else if (r.visible) {
1054 // If this activity is already visible, then there is nothing
1055 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001056 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001057 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001058
1059 } else if (onlyThisProcess == null) {
1060 // This activity is not currently visible, but is running.
1061 // Tell it to become visible.
1062 r.visible = true;
1063 if (r.state != ActivityState.RESUMED && r != starting) {
1064 // If this activity is paused, tell it
1065 // to now show its window.
Craig Mautner323f7802013-10-01 21:16:22 -07001066 if (true || DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001067 TAG, "Making visible and scheduling visibility: " + r);
1068 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001069 if (mTranslucentActivityWaiting != null) {
1070 mUndrawnActivitiesBelowTopTranslucent.add(r);
1071 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001072 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001073 r.sleeping = false;
1074 r.app.pendingUiClean = true;
1075 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1076 r.stopFreezingScreenLocked(false);
1077 } catch (Exception e) {
1078 // Just skip on any failure; we'll make it
1079 // visible when it next restarts.
1080 Slog.w(TAG, "Exception thrown making visibile: "
1081 + r.intent.getComponent(), e);
1082 }
1083 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001084 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001085
Craig Mautnerd44711d2013-02-23 11:24:36 -08001086 // Aggregate current change flags.
1087 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001088
Craig Mautnerd44711d2013-02-23 11:24:36 -08001089 if (r.fullscreen) {
1090 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001091 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1092 behindFullscreen = true;
Craig Mautner19d112d2013-09-30 10:34:55 -07001093 } else if (task.mOnTopOfHome) {
1094 // Work our way down from r to bottom of task and see if there are any
1095 // visible activities below r.
1096 int rIndex = task.mActivities.indexOf(r);
1097 for ( --rIndex; rIndex >= 0; --rIndex) {
1098 final ActivityRecord blocker = task.mActivities.get(rIndex);
Craig Mautnerff174d52013-10-06 12:24:56 -07001099 if (!blocker.finishing) {
Craig Mautner19d112d2013-09-30 10:34:55 -07001100 if (DEBUG_VISBILITY) Slog.v(TAG, "Home visibility for " +
1101 r + " blocked by " + blocker);
1102 break;
1103 }
1104 }
1105 if (rIndex < 0) {
1106 // Got to task bottom without finding a visible activity, show home.
1107 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1108 showHomeBehindStack = true;
1109 behindFullscreen = true;
1110 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001111 }
1112 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001113 if (DEBUG_VISBILITY) Slog.v(
1114 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1115 + " state=" + r.state
1116 + " behindFullscreen=" + behindFullscreen);
1117 // Now for any activities that aren't visible to the user, make
1118 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001119 if (r.visible) {
Craig Mautner323f7802013-10-01 21:16:22 -07001120 if (true || DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001121 r.visible = false;
1122 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001123 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001124 switch (r.state) {
1125 case STOPPING:
1126 case STOPPED:
1127 if (r.app != null && r.app.thread != null) {
1128 if (DEBUG_VISBILITY) Slog.v(
1129 TAG, "Scheduling invisibility: " + r);
1130 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1131 }
1132 break;
1133
1134 case INITIALIZING:
1135 case RESUMED:
1136 case PAUSING:
1137 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001138 // This case created for transitioning activities from
1139 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautnere5273b42013-09-09 12:57:47 -07001140 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1141 mStackSupervisor.mStoppingActivities.add(r);
1142 }
1143 mStackSupervisor.scheduleIdleLocked();
Craig Mautner4addfc52013-06-25 08:05:45 -07001144 break;
1145
1146 default:
1147 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001148 }
1149 } catch (Exception e) {
1150 // Just skip on any failure; we'll make it
1151 // visible when it next restarts.
1152 Slog.w(TAG, "Exception thrown making hidden: "
1153 + r.intent.getComponent(), e);
1154 }
1155 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001156 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001157 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001158 }
1159 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001160 }
Craig Mautner580ea812013-04-25 12:58:38 -07001161 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001162 }
Craig Mautner58547802013-03-05 08:23:53 -08001163
Craig Mautner5eda9b32013-07-02 11:58:16 -07001164 void convertToTranslucent(ActivityRecord r) {
1165 mTranslucentActivityWaiting = r;
1166 mUndrawnActivitiesBelowTopTranslucent.clear();
1167 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1168 }
1169
1170 /**
1171 * Called as activities below the top translucent activity are redrawn. When the last one is
1172 * redrawn notify the top activity by calling
1173 * {@link Activity#onTranslucentConversionComplete}.
1174 *
1175 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1176 * occurred and the activity will be notified immediately.
1177 */
1178 void notifyActivityDrawnLocked(ActivityRecord r) {
1179 if ((r == null)
1180 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1181 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1182 // The last undrawn activity below the top has just been drawn. If there is an
1183 // opaque activity at the top, notify it that it can become translucent safely now.
1184 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1185 mTranslucentActivityWaiting = null;
1186 mUndrawnActivitiesBelowTopTranslucent.clear();
1187 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1188
1189 if (waitingActivity != null && waitingActivity.app != null &&
1190 waitingActivity.app.thread != null) {
1191 try {
1192 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1193 waitingActivity.appToken, r != null);
1194 } catch (RemoteException e) {
1195 }
1196 }
1197 }
1198 }
1199
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001200 /**
1201 * Ensure that the top activity in the stack is resumed.
1202 *
1203 * @param prev The previously resumed activity, for when in the process
1204 * of pausing; can be null to call from elsewhere.
1205 *
1206 * @return Returns true if something is being resumed, or false if
1207 * nothing happened.
1208 */
1209 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001210 return resumeTopActivityLocked(prev, null);
1211 }
1212
1213 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001214 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1215
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001216 // Find the first activity that is not finishing.
1217 ActivityRecord next = topRunningActivityLocked(null);
1218
1219 // Remember how we'll process this pause/resume situation, and ensure
1220 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001221 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1222 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001223
1224 if (next == null) {
1225 // There are no more activities! Let's just start up the
1226 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001227 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001228 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001229 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001230 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001231 }
1232
1233 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001234
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001235 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001236 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1237 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001238 // Make sure we have executed any pending transitions, since there
1239 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001240 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001241 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001242 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001243 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001244 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001245 return false;
1246 }
1247
Craig Mautner525f3d92013-05-07 14:01:50 -07001248 final TaskRecord nextTask = next.task;
1249 final TaskRecord prevTask = prev != null ? prev.task : null;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001250 if (prevTask != null && prevTask.mOnTopOfHome && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001251 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001252 if (prevTask == nextTask) {
1253 ArrayList<ActivityRecord> activities = prevTask.mActivities;
1254 final int numActivities = activities.size();
1255 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1256 final ActivityRecord r = activities.get(activityNdx);
1257 // r is usually the same as next, but what if two activities were launched
1258 // before prev finished?
1259 if (!r.finishing) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001260 r.frontOfTask = true;
1261 break;
1262 }
1263 }
1264 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001265 // This task is going away but it was supposed to return to the home task.
1266 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001267 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001268 mTaskHistory.get(taskNdx).mOnTopOfHome = true;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001269 } else {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001270 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Launching home next");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001271 return mStackSupervisor.resumeHomeActivity(prev);
1272 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001273 }
1274
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001275 // If we are sleeping, and there is no resumed activity, and the top
1276 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001277 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001278 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001279 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001280 // Make sure we have executed any pending transitions, since there
1281 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001282 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001283 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001284 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001285 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001286 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001287 return false;
1288 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001289
1290 // Make sure that the user who owns this activity is started. If not,
1291 // we will just leave it as is because someone should be bringing
1292 // another user's activities to the top of the stack.
1293 if (mService.mStartedUsers.get(next.userId) == null) {
1294 Slog.w(TAG, "Skipping resume of top activity " + next
1295 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001296 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001297 return false;
1298 }
1299
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001300 // The activity may be waiting for stop, but that is no longer
1301 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001302 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001303 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001304 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001305 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001306
Dianne Hackborn84375872012-06-01 19:03:50 -07001307 next.updateOptionsLocked(options);
1308
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001309 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1310
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001311 // If we are currently pausing an activity, then don't do anything
1312 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001313 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001314 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1315 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001316 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001317 return false;
1318 }
1319
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001320 // Okay we are now going to start a switch, to 'next'. We may first
1321 // have to pause the current activity, but this is an important point
1322 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001323 // XXX "App Redirected" dialog is getting too many false positives
1324 // at this point, so turn off for now.
1325 if (false) {
1326 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1327 long now = SystemClock.uptimeMillis();
1328 final boolean inTime = mLastStartedActivity.startTime != 0
1329 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1330 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1331 final int nextUid = next.info.applicationInfo.uid;
1332 if (inTime && lastUid != nextUid
1333 && lastUid != next.launchedFromUid
1334 && mService.checkPermission(
1335 android.Manifest.permission.STOP_APP_SWITCHES,
1336 -1, next.launchedFromUid)
1337 != PackageManager.PERMISSION_GRANTED) {
1338 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1339 } else {
1340 next.startTime = now;
1341 mLastStartedActivity = next;
1342 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001343 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001344 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001345 mLastStartedActivity = next;
1346 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001347 }
Craig Mautner58547802013-03-05 08:23:53 -08001348
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001349 // We need to start pausing the current activity so the top one
1350 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001351 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001352 if (mResumedActivity != null) {
1353 pausing = true;
1354 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001355 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001356 }
1357 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001358 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1359 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001360 // At this point we want to put the upcoming activity's process
1361 // at the top of the LRU list, since we know we will be needing it
1362 // very soon and it would be a waste to let it get killed if it
1363 // happens to be sitting towards the end.
1364 if (next.app != null && next.app.thread != null) {
1365 // No reason to do full oom adj update here; we'll let that
1366 // happen whenever it needs to later.
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001367 mService.updateLruProcessLocked(next.app, false, true);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001368 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001369 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001370 return true;
1371 }
1372
Christopher Tated3f175c2012-06-14 14:16:54 -07001373 // If the most recent activity was noHistory but was only stopped rather
1374 // than stopped+finished because the device went to sleep, we need to make
1375 // sure to finish it as we're making a new activity topmost.
Craig Mautner0f922742013-08-06 08:44:42 -07001376 if (mService.mSleeping && mLastNoHistoryActivity != null &&
1377 !mLastNoHistoryActivity.finishing) {
1378 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1379 " on new resume");
1380 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1381 null, "no-history", false);
1382 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001383 }
1384
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001385 if (prev != null && prev != next) {
1386 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1387 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001388 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001389 if (DEBUG_SWITCH) Slog.v(
1390 TAG, "Resuming top, waiting visible to hide: " + prev);
1391 } else {
1392 // The next activity is already visible, so hide the previous
1393 // activity's windows right now so we can show the new one ASAP.
1394 // We only do this if the previous is finishing, which should mean
1395 // it is on top of the one being resumed so hiding it quickly
1396 // is good. Otherwise, we want to do the normal route of allowing
1397 // the resumed activity to be shown so we can decide if the
1398 // previous should actually be hidden depending on whether the
1399 // new one is found to be full-screen or not.
1400 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001401 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001402 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1403 + prev + ", waitingVisible="
1404 + (prev != null ? prev.waitingVisible : null)
1405 + ", nowVisible=" + next.nowVisible);
1406 } else {
1407 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1408 + prev + ", waitingVisible="
1409 + (prev != null ? prev.waitingVisible : null)
1410 + ", nowVisible=" + next.nowVisible);
1411 }
1412 }
1413 }
1414
Dianne Hackborne7f97212011-02-24 14:40:20 -08001415 // Launching this app's activity, make sure the app is no longer
1416 // considered stopped.
1417 try {
1418 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001419 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001420 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001421 } catch (IllegalArgumentException e) {
1422 Slog.w(TAG, "Failed trying to unstop package "
1423 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001424 }
1425
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001426 // We are starting up the next activity, so tell the window manager
1427 // that the previous one will be hidden soon. This way it can know
1428 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001429 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001430 if (prev != null) {
1431 if (prev.finishing) {
1432 if (DEBUG_TRANSITION) Slog.v(TAG,
1433 "Prepare close transition: prev=" + prev);
1434 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001435 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001436 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001437 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001438 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001439 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1440 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001441 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001442 mWindowManager.setAppWillBeHidden(prev.appToken);
1443 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001444 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001445 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001446 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001447 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001448 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001449 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001450 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001451 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1452 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001453 }
1454 }
1455 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001456 mWindowManager.setAppWillBeHidden(prev.appToken);
1457 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001458 }
Craig Mautner967212c2013-04-13 21:10:58 -07001459 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001460 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001461 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001462 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001463 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001464 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001465 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001466 }
1467 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001468 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001469 next.applyOptionsLocked();
1470 } else {
1471 next.clearOptionsLocked();
1472 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001473
Craig Mautnercf910b02013-04-23 11:23:27 -07001474 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001475 if (next.app != null && next.app.thread != null) {
1476 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1477
1478 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001479 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001480
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001481 // schedule launch ticks to collect information about slow apps.
1482 next.startLaunchTickingLocked();
1483
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001484 ActivityRecord lastResumedActivity =
1485 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001486 ActivityState lastState = next.state;
1487
1488 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001489
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001490 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001491 next.state = ActivityState.RESUMED;
1492 mResumedActivity = next;
1493 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001494 mService.addRecentTaskLocked(next.task);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001495 mService.updateLruProcessLocked(next.app, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001496 updateLRUListLocked(next);
1497
1498 // Have the window manager re-evaluate the orientation of
1499 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001500 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001501 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001502 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001503 mService.mConfiguration,
1504 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1505 if (config != null) {
1506 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001507 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001508 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001509 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001510
Craig Mautner525f3d92013-05-07 14:01:50 -07001511 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001512 // The configuration update wasn't able to keep the existing
1513 // instance of the activity, and instead started a new one.
1514 // We should be all done, but let's just make sure our activity
1515 // is still at the top and schedule another run if something
1516 // weird happened.
1517 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001518 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001519 "Activity config changed during resume: " + next
1520 + ", new next: " + nextNext);
1521 if (nextNext != next) {
1522 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001523 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001524 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001525 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001526 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001527 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001528 return true;
1529 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001530 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001531 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001532 }
Craig Mautner58547802013-03-05 08:23:53 -08001533
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001534 try {
1535 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001536 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001537 if (a != null) {
1538 final int N = a.size();
1539 if (!next.finishing && N > 0) {
1540 if (DEBUG_RESULTS) Slog.v(
1541 TAG, "Delivering results to " + next
1542 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001543 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001544 }
1545 }
1546
1547 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001548 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001549 }
1550
1551 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001552 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001553 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001554
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001555 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001556 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001557 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001558 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
1559 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001560 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001561
Craig Mautner0eea92c2013-05-16 13:35:39 -07001562 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001563
Craig Mautnerac6f8432013-07-17 13:24:59 -07001564 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001565 } catch (Exception e) {
1566 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001567 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1568 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001569 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001570 if (lastStack != null) {
1571 lastStack.mResumedActivity = lastResumedActivity;
1572 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001573 Slog.i(TAG, "Restarting because process died: " + next);
1574 if (!next.hasBeenLaunched) {
1575 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001576 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1577 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001578 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001579 next.appToken, next.packageName, next.theme,
1580 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001581 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1582 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001583 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001584 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001585 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001586 return true;
1587 }
1588
1589 // From this point on, if something goes wrong there is no way
1590 // to recover the activity.
1591 try {
1592 next.visible = true;
1593 completeResumeLocked(next);
1594 } catch (Exception e) {
1595 // If any exception gets thrown, toss away this
1596 // activity and try the next one.
1597 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001598 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001599 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001600 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001601 return true;
1602 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001603 next.stopped = false;
1604
1605 } else {
1606 // Whoops, need to restart this activity!
1607 if (!next.hasBeenLaunched) {
1608 next.hasBeenLaunched = true;
1609 } else {
1610 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001611 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001612 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001613 mService.compatibilityInfoForPackageLocked(
1614 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001615 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001616 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001617 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001618 }
1619 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1620 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001621 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
Craig Mautnere79d42682013-04-01 19:01:53 -07001622 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001623 }
1624
Craig Mautnercf910b02013-04-23 11:23:27 -07001625 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001626 return true;
1627 }
1628
Craig Mautnerac6f8432013-07-17 13:24:59 -07001629 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001630 // If this is being moved to the top by another activity or being launched from the home
1631 // activity, set mOnTopOfHome accordingly.
Craig Mautnerd99384d2013-10-14 07:09:18 -07001632 ActivityStack lastStack = mStackSupervisor.getLastStack();
1633 final boolean fromHome = lastStack == null ? true : lastStack.isHomeStack();
Craig Mautner9c85c202013-10-04 20:11:26 -07001634 if (!isHomeStack() && (fromHome || topTask() != task)) {
1635 task.mOnTopOfHome = fromHome;
1636 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001637
Craig Mautnerac6f8432013-07-17 13:24:59 -07001638 mTaskHistory.remove(task);
1639 // Now put task at top.
1640 int stackNdx = mTaskHistory.size();
1641 if (task.userId != mCurrentUser) {
1642 // Put non-current user tasks below current user tasks.
1643 while (--stackNdx >= 0) {
1644 if (mTaskHistory.get(stackNdx).userId != mCurrentUser) {
1645 break;
1646 }
1647 }
1648 ++stackNdx;
1649 }
1650 mTaskHistory.add(stackNdx, task);
1651 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001652
Craig Mautner8849a5e2013-04-02 16:41:03 -07001653 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001654 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001655 TaskRecord rTask = r.task;
1656 final int taskId = rTask.taskId;
1657 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001658 // Last activity in task had been removed or ActivityManagerService is reusing task.
1659 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001660 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001661 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001662 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001663 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001664 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001665 if (!newTask) {
1666 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001667 boolean startIt = true;
1668 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1669 task = mTaskHistory.get(taskNdx);
1670 if (task == r.task) {
1671 // Here it is! Now, if this is not yet visible to the
1672 // user, then just add it without starting; it will
1673 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001674 if (!startIt) {
1675 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1676 + task, new RuntimeException("here").fillInStackTrace());
1677 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001678 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001679 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1680 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001681 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
1682 r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001683 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001684 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001685 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001686 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001687 return;
1688 }
1689 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001690 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001691 startIt = false;
1692 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001693 }
1694 }
1695
1696 // Place a new activity at top of stack, so it is next to interact
1697 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001698
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001699 // If we are not placing the new activity frontmost, we do not want
1700 // to deliver the onUserLeaving callback to the actual frontmost
1701 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001702 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001703 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001704 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1705 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001706 }
Craig Mautner70a86932013-02-28 22:37:44 -08001707
1708 task = r.task;
1709
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001710 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001711 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001712 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001713 task.addActivityToTop(r);
1714
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001715 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001716 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001717 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001718 // We want to show the starting preview window if we are
1719 // switching to a new task, or the next activity's process is
1720 // not currently running.
1721 boolean showStartingIcon = newTask;
1722 ProcessRecord proc = r.app;
1723 if (proc == null) {
1724 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1725 }
1726 if (proc == null || proc.thread == null) {
1727 showStartingIcon = true;
1728 }
1729 if (DEBUG_TRANSITION) Slog.v(TAG,
1730 "Prepare open transition: starting " + r);
1731 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001732 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001733 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001734 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001735 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001736 ? AppTransition.TRANSIT_TASK_OPEN
1737 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001738 mNoAnimActivities.remove(r);
1739 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001740 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001741 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001742 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001743 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001744 boolean doShow = true;
1745 if (newTask) {
1746 // Even though this activity is starting fresh, we still need
1747 // to reset it to make sure we apply affinities to move any
1748 // existing activities from other tasks in to it.
1749 // If the caller has requested that the target task be
1750 // reset, then do so.
1751 if ((r.intent.getFlags()
1752 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1753 resetTaskIfNeededLocked(r, r);
1754 doShow = topRunningNonDelayedActivityLocked(null) == r;
1755 }
1756 }
1757 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1758 // Figure out if we are transitioning from another activity that is
1759 // "has the same starting icon" as the next one. This allows the
1760 // window manager to keep the previous window it had previously
1761 // created, if it still had one.
1762 ActivityRecord prev = mResumedActivity;
1763 if (prev != null) {
1764 // We don't want to reuse the previous starting preview if:
1765 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001766 if (prev.task != r.task) {
1767 prev = null;
1768 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001769 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001770 else if (prev.nowVisible) {
1771 prev = null;
1772 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001773 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001774 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001775 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001776 mService.compatibilityInfoForPackageLocked(
1777 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001778 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001779 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001780 }
1781 } else {
1782 // If this is the first activity, don't do any fancy animations,
1783 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001784 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001785 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001786 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001787 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001788 }
1789 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001790 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001791 }
1792
1793 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001794 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001795 }
1796 }
1797
Dianne Hackbornbe707852011-11-11 14:32:10 -08001798 final void validateAppTokensLocked() {
1799 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001800 mValidateAppTokens.ensureCapacity(numActivities());
1801 final int numTasks = mTaskHistory.size();
1802 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1803 TaskRecord task = mTaskHistory.get(taskNdx);
1804 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07001805 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08001806 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001807 }
Craig Mautner000f0022013-02-26 15:04:29 -08001808 TaskGroup group = new TaskGroup();
1809 group.taskId = task.taskId;
1810 mValidateAppTokens.add(group);
1811 final int numActivities = activities.size();
1812 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1813 final ActivityRecord r = activities.get(activityNdx);
1814 group.tokens.add(r.appToken);
1815 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001816 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001817 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001818 }
1819
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001820 /**
1821 * Perform a reset of the given task, if needed as part of launching it.
1822 * Returns the new HistoryRecord at the top of the task.
1823 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001824 /**
1825 * Helper method for #resetTaskIfNeededLocked.
1826 * We are inside of the task being reset... we'll either finish this activity, push it out
1827 * for another task, or leave it as-is.
1828 * @param task The task containing the Activity (taskTop) that might be reset.
1829 * @param forceReset
1830 * @return An ActivityOptions that needs to be processed.
1831 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001832 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001833 ActivityOptions topOptions = null;
1834
1835 int replyChainEnd = -1;
1836 boolean canMoveOptions = true;
1837
1838 // We only do this for activities that are not the root of the task (since if we finish
1839 // the root, we may no longer have the task!).
1840 final ArrayList<ActivityRecord> activities = task.mActivities;
1841 final int numActivities = activities.size();
1842 for (int i = numActivities - 1; i > 0; --i ) {
1843 ActivityRecord target = activities.get(i);
1844
1845 final int flags = target.info.flags;
1846 final boolean finishOnTaskLaunch =
1847 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1848 final boolean allowTaskReparenting =
1849 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1850 final boolean clearWhenTaskReset =
1851 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1852
1853 if (!finishOnTaskLaunch
1854 && !clearWhenTaskReset
1855 && target.resultTo != null) {
1856 // If this activity is sending a reply to a previous
1857 // activity, we can't do anything with it now until
1858 // we reach the start of the reply chain.
1859 // XXX note that we are assuming the result is always
1860 // to the previous activity, which is almost always
1861 // the case but we really shouldn't count on.
1862 if (replyChainEnd < 0) {
1863 replyChainEnd = i;
1864 }
1865 } else if (!finishOnTaskLaunch
1866 && !clearWhenTaskReset
1867 && allowTaskReparenting
1868 && target.taskAffinity != null
1869 && !target.taskAffinity.equals(task.affinity)) {
1870 // If this activity has an affinity for another
1871 // task, then we need to move it out of here. We will
1872 // move it as far out of the way as possible, to the
1873 // bottom of the activity stack. This also keeps it
1874 // correctly ordered with any activities we previously
1875 // moved.
Craig Mautnerdccb7702013-09-17 15:53:34 -07001876 final ActivityRecord bottom =
1877 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
1878 mTaskHistory.get(0).mActivities.get(0) : null;
1879 if (bottom != null && target.taskAffinity != null
1880 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001881 // If the activity currently at the bottom has the
1882 // same task affinity as the one we are moving,
1883 // then merge it into the same task.
Craig Mautnerdccb7702013-09-17 15:53:34 -07001884 target.setTask(bottom.task, bottom.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001885 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07001886 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001887 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001888 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001889 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001890 target.task.affinityIntent = target.intent;
1891 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1892 + " out to new task " + target.task);
1893 }
1894
1895 final TaskRecord targetTask = target.task;
1896 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001897 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001898
Craig Mautner525f3d92013-05-07 14:01:50 -07001899 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001900 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1901 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07001902 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001903 if (p.finishing) {
1904 continue;
1905 }
1906
Craig Mautner525f3d92013-05-07 14:01:50 -07001907 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001908 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001909 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001910 topOptions = p.takeOptionsLocked();
1911 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001912 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001913 }
1914 }
1915 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1916 + task + " adding to task=" + targetTask,
1917 new RuntimeException("here").fillInStackTrace());
1918 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1919 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001920 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001921 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001922
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001923 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001924 }
1925
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001926 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001927 if (VALIDATE_TOKENS) {
1928 validateAppTokensLocked();
1929 }
1930
1931 replyChainEnd = -1;
1932 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1933 // If the activity should just be removed -- either
1934 // because it asks for it, or the task should be
1935 // cleared -- then finish it and anything that is
1936 // part of its reply chain.
1937 int end;
1938 if (clearWhenTaskReset) {
1939 // In this case, we want to finish this activity
1940 // and everything above it, so be sneaky and pretend
1941 // like these are all in the reply chain.
1942 end = numActivities - 1;
1943 } else if (replyChainEnd < 0) {
1944 end = i;
1945 } else {
1946 end = replyChainEnd;
1947 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001948 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001949 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001950 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001951 if (p.finishing) {
1952 continue;
1953 }
1954 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001955 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001956 topOptions = p.takeOptionsLocked();
1957 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001958 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001959 }
1960 }
Craig Mautner58547802013-03-05 08:23:53 -08001961 if (DEBUG_TASKS) Slog.w(TAG,
1962 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001963 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001964 end--;
1965 srcPos--;
1966 }
1967 }
1968 replyChainEnd = -1;
1969 } else {
1970 // If we were in the middle of a chain, well the
1971 // activity that started it all doesn't want anything
1972 // special, so leave it all as-is.
1973 replyChainEnd = -1;
1974 }
1975 }
1976
1977 return topOptions;
1978 }
1979
1980 /**
1981 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1982 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1983 * @param affinityTask The task we are looking for an affinity to.
1984 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1985 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1986 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1987 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001988 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08001989 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001990 int replyChainEnd = -1;
1991 final int taskId = task.taskId;
1992 final String taskAffinity = task.affinity;
1993
1994 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1995 final int numActivities = activities.size();
1996 // Do not operate on the root Activity.
1997 for (int i = numActivities - 1; i > 0; --i) {
1998 ActivityRecord target = activities.get(i);
1999
2000 final int flags = target.info.flags;
2001 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2002 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2003
2004 if (target.resultTo != null) {
2005 // If this activity is sending a reply to a previous
2006 // activity, we can't do anything with it now until
2007 // we reach the start of the reply chain.
2008 // XXX note that we are assuming the result is always
2009 // to the previous activity, which is almost always
2010 // the case but we really shouldn't count on.
2011 if (replyChainEnd < 0) {
2012 replyChainEnd = i;
2013 }
2014 } else if (topTaskIsHigher
2015 && allowTaskReparenting
2016 && taskAffinity != null
2017 && taskAffinity.equals(target.taskAffinity)) {
2018 // This activity has an affinity for our task. Either remove it if we are
2019 // clearing or move it over to our task. Note that
2020 // we currently punt on the case where we are resetting a
2021 // task that is not at the top but who has activities above
2022 // with an affinity to it... this is really not a normal
2023 // case, and we will need to later pull that task to the front
2024 // and usually at that point we will do the reset and pick
2025 // up those remaining activities. (This only happens if
2026 // someone starts an activity in a new task from an activity
2027 // in a task that is not currently on top.)
2028 if (forceReset || finishOnTaskLaunch) {
2029 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2030 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2031 for (int srcPos = start; srcPos >= i; --srcPos) {
2032 final ActivityRecord p = activities.get(srcPos);
2033 if (p.finishing) {
2034 continue;
2035 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002036 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002037 }
2038 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002039 if (taskInsertionPoint < 0) {
2040 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002041
Craig Mautner77878772013-03-04 19:46:24 -08002042 }
Craig Mautner77878772013-03-04 19:46:24 -08002043
2044 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2045 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2046 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2047 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002048 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002049 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002050 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002051
Craig Mautnere3a74d52013-02-22 14:14:58 -08002052 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2053 + " to stack at " + task,
2054 new RuntimeException("here").fillInStackTrace());
2055 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2056 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002057 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002058 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002059 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002060 if (VALIDATE_TOKENS) {
2061 validateAppTokensLocked();
2062 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002063
2064 // Now we've moved it in to place... but what if this is
2065 // a singleTop activity and we have put it on top of another
2066 // instance of the same activity? Then we drop the instance
2067 // below so it remains singleTop.
2068 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2069 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002070 int targetNdx = taskActivities.indexOf(target);
2071 if (targetNdx > 0) {
2072 ActivityRecord p = taskActivities.get(targetNdx - 1);
2073 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002074 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2075 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002076 }
2077 }
2078 }
2079 }
2080
2081 replyChainEnd = -1;
2082 }
2083 }
Craig Mautner77878772013-03-04 19:46:24 -08002084 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002085 }
2086
Craig Mautner8849a5e2013-04-02 16:41:03 -07002087 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002088 ActivityRecord newActivity) {
2089 boolean forceReset =
2090 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2091 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2092 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2093 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2094 forceReset = true;
2095 }
2096 }
2097
2098 final TaskRecord task = taskTop.task;
2099
2100 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2101 * for remaining tasks. Used for later tasks to reparent to task. */
2102 boolean taskFound = false;
2103
2104 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2105 ActivityOptions topOptions = null;
2106
Craig Mautner77878772013-03-04 19:46:24 -08002107 // Preserve the location for reparenting in the new task.
2108 int reparentInsertionPoint = -1;
2109
Craig Mautnere3a74d52013-02-22 14:14:58 -08002110 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2111 final TaskRecord targetTask = mTaskHistory.get(i);
2112
2113 if (targetTask == task) {
2114 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2115 taskFound = true;
2116 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002117 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2118 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002119 }
2120 }
2121
Craig Mautner70a86932013-02-28 22:37:44 -08002122 int taskNdx = mTaskHistory.indexOf(task);
2123 do {
2124 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2125 } while (taskTop == null && taskNdx >= 0);
2126
Craig Mautnere3a74d52013-02-22 14:14:58 -08002127 if (topOptions != null) {
2128 // If we got some ActivityOptions from an activity on top that
2129 // was removed from the task, propagate them to the new real top.
2130 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002131 taskTop.updateOptionsLocked(topOptions);
2132 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002133 topOptions.abort();
2134 }
2135 }
2136
2137 return taskTop;
2138 }
2139
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002140 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2141 String resultWho, int requestCode, int resultCode, Intent data) {
2142
2143 if (callingUid > 0) {
2144 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002145 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002146 }
2147
2148 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2149 + " : who=" + resultWho + " req=" + requestCode
2150 + " res=" + resultCode + " data=" + data);
2151 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2152 try {
2153 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2154 list.add(new ResultInfo(resultWho, requestCode,
2155 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002156 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002157 return;
2158 } catch (Exception e) {
2159 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2160 }
2161 }
2162
2163 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2164 }
2165
Craig Mautnerf3333272013-04-22 10:55:53 -07002166 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002167 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2168 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2169 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2170 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002171 if (!mService.mSleeping) {
2172 if (DEBUG_STATES) {
2173 Slog.d(TAG, "no-history finish of " + r);
2174 }
2175 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002176 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002177 } else {
2178 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2179 + " on stop because we're just sleeping");
2180 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002181 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002182 }
2183
2184 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002185 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002186 if (mService.mFocusedActivity == r) {
2187 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2188 }
2189 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002190 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002191 try {
2192 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002193 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2194 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002195 r.state = ActivityState.STOPPING;
2196 if (DEBUG_VISBILITY) Slog.v(
2197 TAG, "Stopping visible=" + r.visible + " for " + r);
2198 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002199 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002200 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002201 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002202 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002203 r.setSleeping(true);
2204 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002205 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002206 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002207 } catch (Exception e) {
2208 // Maybe just ignore exceptions here... if the process
2209 // has crashed, our death notification will clean things
2210 // up.
2211 Slog.w(TAG, "Exception thrown during pause", e);
2212 // Just in case, assume it to be stopped.
2213 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002214 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002215 r.state = ActivityState.STOPPED;
2216 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002217 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002218 }
2219 }
2220 }
2221 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002222
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002223 /**
2224 * @return Returns true if the activity is being finished, false if for
2225 * some reason it is being left as-is.
2226 */
2227 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002228 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002229 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002230 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002231 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002232 + ", result=" + resultCode + ", data=" + resultData
2233 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002234 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002235 return false;
2236 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002237
Craig Mautnerd44711d2013-02-23 11:24:36 -08002238 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002239 return true;
2240 }
2241
Craig Mautnerd2328952013-03-05 12:46:26 -08002242 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002243 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2244 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2245 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2246 ActivityRecord r = activities.get(activityNdx);
2247 if (r.resultTo == self && r.requestCode == requestCode) {
2248 if ((r.resultWho == null && resultWho == null) ||
2249 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2250 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2251 false);
2252 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002253 }
2254 }
2255 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002256 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002257 }
2258
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002259 final void finishTopRunningActivityLocked(ProcessRecord app) {
2260 ActivityRecord r = topRunningActivityLocked(null);
2261 if (r != null && r.app == app) {
2262 // If the top running activity is from this crashing
2263 // process, then terminate it to avoid getting in a loop.
2264 Slog.w(TAG, " Force finishing activity "
2265 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002266 int taskNdx = mTaskHistory.indexOf(r.task);
2267 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002268 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002269 // Also terminate any activities below it that aren't yet
2270 // stopped, to avoid a situation where one will get
2271 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002272 --activityNdx;
2273 if (activityNdx < 0) {
2274 do {
2275 --taskNdx;
2276 if (taskNdx < 0) {
2277 break;
2278 }
2279 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2280 } while (activityNdx < 0);
2281 }
2282 if (activityNdx >= 0) {
2283 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002284 if (r.state == ActivityState.RESUMED
2285 || r.state == ActivityState.PAUSING
2286 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002287 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002288 Slog.w(TAG, " Force finishing activity "
2289 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002290 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002291 }
2292 }
2293 }
2294 }
2295 }
2296
Craig Mautnerd2328952013-03-05 12:46:26 -08002297 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002298 ArrayList<ActivityRecord> activities = r.task.mActivities;
2299 for (int index = activities.indexOf(r); index >= 0; --index) {
2300 ActivityRecord cur = activities.get(index);
2301 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002302 break;
2303 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002304 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002305 }
2306 return true;
2307 }
2308
Dianne Hackborn5c607432012-02-28 14:44:19 -08002309 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2310 // send the result
2311 ActivityRecord resultTo = r.resultTo;
2312 if (resultTo != null) {
2313 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2314 + " who=" + r.resultWho + " req=" + r.requestCode
2315 + " res=" + resultCode + " data=" + resultData);
2316 if (r.info.applicationInfo.uid > 0) {
2317 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2318 resultTo.packageName, resultData,
2319 resultTo.getUriPermissionsLocked());
2320 }
2321 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2322 resultData);
2323 r.resultTo = null;
2324 }
2325 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2326
2327 // Make sure this HistoryRecord is not holding on to other resources,
2328 // because clients have remote IPC references to this object so we
2329 // can't assume that will go away and want to avoid circular IPC refs.
2330 r.results = null;
2331 r.pendingResults = null;
2332 r.newIntents = null;
2333 r.icicle = null;
2334 }
2335
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002336 /**
2337 * @return Returns true if this activity has been removed from the history
2338 * list, or false if it is still in the list and will be removed later.
2339 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002340 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2341 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002342 if (r.finishing) {
2343 Slog.w(TAG, "Duplicate finish request for " + r);
2344 return false;
2345 }
2346
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002347 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002348 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002349 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002350 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002351 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2352 final int index = activities.indexOf(r);
2353 if (index < (activities.size() - 1)) {
2354 ActivityRecord next = activities.get(index+1);
2355 if (r.frontOfTask) {
2356 // The next activity is now the front of the task.
2357 next.frontOfTask = true;
2358 }
2359 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2360 // If the caller asked that this activity (and all above it)
2361 // be cleared when the task is reset, don't lose that information,
2362 // but propagate it up to the next activity.
2363 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002364 }
2365 }
2366
2367 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002368 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002369 if (mService.mFocusedActivity == r) {
Craig Mautner29219d92013-04-16 20:19:12 -07002370 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002371 }
2372 }
2373
Dianne Hackborn5c607432012-02-28 14:44:19 -08002374 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002375
Craig Mautnerc8143c62013-09-03 12:15:57 -07002376 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002377 // There are clients waiting to receive thumbnails so, in case
2378 // this is an activity that someone is waiting for, add it
2379 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002380 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002381 }
2382
Craig Mautnerde4ef022013-04-07 19:01:33 -07002383 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002384 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002385 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002386 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002387 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002388 ? AppTransition.TRANSIT_TASK_CLOSE
2389 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002390
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002391 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002392 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002393
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002394 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002395 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2396 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2397 startPausingLocked(false, false);
2398 }
2399
2400 } else if (r.state != ActivityState.PAUSING) {
2401 // If the activity is PAUSING, we will complete the finish once
2402 // it is done pausing; else we can just directly finish it here.
2403 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002404 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002405 } else {
2406 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2407 }
2408
2409 return false;
2410 }
2411
Craig Mautnerf3333272013-04-22 10:55:53 -07002412 static final int FINISH_IMMEDIATELY = 0;
2413 static final int FINISH_AFTER_PAUSE = 1;
2414 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002415
Craig Mautnerf3333272013-04-22 10:55:53 -07002416 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002417 // First things first: if this activity is currently visible,
2418 // and the resumed activity is not yet visible, then hold off on
2419 // finishing until the resumed one becomes visible.
2420 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002421 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2422 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002423 if (mStackSupervisor.mStoppingActivities.size() > 3
2424 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002425 // If we already have a few activities waiting to stop,
2426 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002427 // them out. Or if r is the last of activity of the last task the stack
2428 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002429 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002430 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002431 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002432 }
2433 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002434 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2435 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002436 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002437 if (oomAdj) {
2438 mService.updateOomAdjLocked();
2439 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002440 return r;
2441 }
2442
2443 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002444 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002445 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002446 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002447 if (mResumedActivity == r) {
2448 mResumedActivity = null;
2449 }
2450 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002451 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002452 r.state = ActivityState.FINISHING;
2453
2454 if (mode == FINISH_IMMEDIATELY
2455 || prevState == ActivityState.STOPPED
2456 || prevState == ActivityState.INITIALIZING) {
2457 // If this activity is already stopped, we can just finish
2458 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002459 boolean activityRemoved = destroyActivityLocked(r, true,
2460 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002461 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002462 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002463 }
2464 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002465 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002466
2467 // Need to go through the full pause cycle to get this
2468 // activity into the stopped state and then finish it.
2469 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002470 mStackSupervisor.mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002471 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002472 return r;
2473 }
2474
Craig Mautnerd2328952013-03-05 12:46:26 -08002475 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002476 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002477 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002478 final TaskRecord task = srec.task;
2479 final ArrayList<ActivityRecord> activities = task.mActivities;
2480 final int start = activities.indexOf(srec);
2481 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002482 return false;
2483 }
2484 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002485 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002486 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002487 final ComponentName dest = destIntent.getComponent();
2488 if (start > 0 && dest != null) {
2489 for (int i = finishTo; i >= 0; i--) {
2490 ActivityRecord r = activities.get(i);
2491 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002492 r.info.name.equals(dest.getClassName())) {
2493 finishTo = i;
2494 parent = r;
2495 foundParentInTask = true;
2496 break;
2497 }
2498 }
2499 }
2500
2501 IActivityController controller = mService.mController;
2502 if (controller != null) {
2503 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2504 if (next != null) {
2505 // ask watcher if this is allowed
2506 boolean resumeOK = true;
2507 try {
2508 resumeOK = controller.activityResuming(next.packageName);
2509 } catch (RemoteException e) {
2510 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002511 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002512 }
2513
2514 if (!resumeOK) {
2515 return false;
2516 }
2517 }
2518 }
2519 final long origId = Binder.clearCallingIdentity();
2520 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002521 ActivityRecord r = activities.get(i);
2522 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002523 // Only return the supplied result for the first activity finished
2524 resultCode = Activity.RESULT_CANCELED;
2525 resultData = null;
2526 }
2527
2528 if (parent != null && foundParentInTask) {
2529 final int parentLaunchMode = parent.info.launchMode;
2530 final int destIntentFlags = destIntent.getFlags();
2531 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2532 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2533 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2534 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2535 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2536 } else {
2537 try {
2538 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2539 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002540 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002541 null, aInfo, parent.appToken, null,
2542 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2543 0, null, true, null);
2544 foundParentInTask = res == ActivityManager.START_SUCCESS;
2545 } catch (RemoteException e) {
2546 foundParentInTask = false;
2547 }
2548 requestFinishActivityLocked(parent.appToken, resultCode,
2549 resultData, "navigate-up", true);
2550 }
2551 }
2552 Binder.restoreCallingIdentity(origId);
2553 return foundParentInTask;
2554 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002555 /**
2556 * Perform the common clean-up of an activity record. This is called both
2557 * as part of destroyActivityLocked() (when destroying the client-side
2558 * representation) and cleaning things up as a result of its hosting
2559 * processing going away, in which case there is no remaining client-side
2560 * state to destroy so only the cleanup here is needed.
2561 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002562 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2563 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002564 if (mResumedActivity == r) {
2565 mResumedActivity = null;
2566 }
2567 if (mService.mFocusedActivity == r) {
2568 mService.mFocusedActivity = null;
2569 }
2570
2571 r.configDestroy = false;
2572 r.frozenBeforeDestroy = false;
2573
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002574 if (setState) {
2575 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2576 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002577 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002578 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002579 }
2580
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002581 // Make sure this record is no longer in the pending finishes list.
2582 // This could happen, for example, if we are trimming activities
2583 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002584 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002585 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002586
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002587 // Remove any pending results.
2588 if (r.finishing && r.pendingResults != null) {
2589 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2590 PendingIntentRecord rec = apr.get();
2591 if (rec != null) {
2592 mService.cancelIntentSenderLocked(rec, false);
2593 }
2594 }
2595 r.pendingResults = null;
2596 }
2597
2598 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002599 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002600 }
2601
Craig Mautnerc8143c62013-09-03 12:15:57 -07002602 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002603 // There are clients waiting to receive thumbnails so, in case
2604 // this is an activity that someone is waiting for, add it
2605 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002606 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002607 }
2608
2609 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002610 removeTimeoutsForActivityLocked(r);
2611 }
2612
2613 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002614 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002615 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002616 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002617 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002618 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002619 }
2620
Dianne Hackborn5c607432012-02-28 14:44:19 -08002621 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002622 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2623 r.makeFinishing();
2624 if (DEBUG_ADD_REMOVE) {
2625 RuntimeException here = new RuntimeException("here");
2626 here.fillInStackTrace();
2627 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002628 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002629 final TaskRecord task = r.task;
2630 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002631 if (DEBUG_STACK) Slog.i(TAG,
2632 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner2219b752013-10-12 11:26:08 -07002633 if (task == topTask() && task.mOnTopOfHome) {
Craig Mautner8e569572013-10-11 17:36:59 -07002634 mStackSupervisor.moveHomeToTop();
2635 }
Craig Mautner2219b752013-10-12 11:26:08 -07002636 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002637 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002638 r.takeFromHistory();
2639 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002640 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002641 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002642 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002643 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002644 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002645 if (VALIDATE_TOKENS) {
2646 validateAppTokensLocked();
2647 }
2648 cleanUpActivityServicesLocked(r);
2649 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002650 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002651
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002652 /**
2653 * Perform clean-up of service connections in an activity record.
2654 */
2655 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2656 // Throw away any services that have been bound by this activity.
2657 if (r.connections != null) {
2658 Iterator<ConnectionRecord> it = r.connections.iterator();
2659 while (it.hasNext()) {
2660 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002661 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002662 }
2663 r.connections = null;
2664 }
2665 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002666
2667 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2668 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2669 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2670 mHandler.sendMessage(msg);
2671 }
2672
Dianne Hackborn28695e02011-11-02 21:59:51 -07002673 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002674 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002675 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002676 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2677 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2678 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2679 final ActivityRecord r = activities.get(activityNdx);
2680 if (r.finishing) {
2681 continue;
2682 }
2683 if (r.fullscreen) {
2684 lastIsOpaque = true;
2685 }
2686 if (owner != null && r.app != owner) {
2687 continue;
2688 }
2689 if (!lastIsOpaque) {
2690 continue;
2691 }
2692 // We can destroy this one if we have its icicle saved and
2693 // it is not in the process of pausing/stopping/finishing.
2694 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2695 && r.haveState && !r.visible && r.stopped
2696 && r.state != ActivityState.DESTROYING
2697 && r.state != ActivityState.DESTROYED) {
2698 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2699 + " resumed=" + mResumedActivity
2700 + " pausing=" + mPausingActivity);
2701 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2702 activityRemoved = true;
2703 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002704 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002705 }
2706 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002707 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002708 mStackSupervisor.resumeTopActivitiesLocked();
2709
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002710 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002711 }
2712
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002713 /**
2714 * Destroy the current CLIENT SIDE instance of an activity. This may be
2715 * called both when actually finishing an activity, or when performing
2716 * a configuration switch where we destroy the current client-side object
2717 * but then create a new client-side object for this same HistoryRecord.
2718 */
2719 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002720 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002721 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002722 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002723 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2724 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002725 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002726 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002727
2728 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002729
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002730 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002731
2732 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002733
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002734 if (hadApp) {
2735 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002736 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002737 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2738 mService.mHeavyWeightProcess = null;
2739 mService.mHandler.sendEmptyMessage(
2740 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2741 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07002742 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07002743 // No longer have activities, so update LRU list and oom adj.
2744 mService.updateLruProcessLocked(r.app, false, false);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002745 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002746 }
2747 }
2748
2749 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002750
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002751 try {
2752 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002753 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002754 r.configChangeFlags);
2755 } catch (Exception e) {
2756 // We can just ignore exceptions here... if the process
2757 // has crashed, our death notification will clean things
2758 // up.
2759 //Slog.w(TAG, "Exception thrown during finish", e);
2760 if (r.finishing) {
2761 removeActivityFromHistoryLocked(r);
2762 removedFromHistory = true;
2763 skipDestroy = true;
2764 }
2765 }
2766
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002767 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002768
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002769 // If the activity is finishing, we need to wait on removing it
2770 // from the list to give it a chance to do its cleanup. During
2771 // that time it may make calls back with its token so we need to
2772 // be able to find it on the list and so we don't want to remove
2773 // it from the list yet. Otherwise, we can just immediately put
2774 // it in the destroyed state since we are not removing it from the
2775 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002776 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002777 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2778 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002779 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002780 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002781 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2782 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002783 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002784 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002785 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002786 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002787 }
2788 } else {
2789 // remove this record from the history.
2790 if (r.finishing) {
2791 removeActivityFromHistoryLocked(r);
2792 removedFromHistory = true;
2793 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002794 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002795 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002796 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002797 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002798 }
2799 }
2800
2801 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002802
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002803 if (!mLRUActivities.remove(r) && hadApp) {
2804 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2805 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002806
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002807 return removedFromHistory;
2808 }
2809
Craig Mautnerd2328952013-03-05 12:46:26 -08002810 final void activityDestroyedLocked(IBinder token) {
2811 final long origId = Binder.clearCallingIdentity();
2812 try {
2813 ActivityRecord r = ActivityRecord.forToken(token);
2814 if (r != null) {
2815 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002816 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002817
2818 if (isInStackLocked(token) != null) {
2819 if (r.state == ActivityState.DESTROYING) {
2820 cleanUpActivityLocked(r, true, false);
2821 removeActivityFromHistoryLocked(r);
2822 }
2823 }
Craig Mautner05d29032013-05-03 13:40:13 -07002824 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002825 } finally {
2826 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002827 }
2828 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002829
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002830 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2831 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002832 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002833 if (DEBUG_CLEANUP) Slog.v(
2834 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002835 + " with " + i + " entries");
2836 while (i > 0) {
2837 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002838 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002839 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002840 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002841 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002842 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002843 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002844 }
2845 }
2846 }
2847
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002848 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2849 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002850 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2851 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002852 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2853 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002854 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002855 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002856 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2857 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002858
2859 boolean hasVisibleActivities = false;
2860
2861 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002862 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002863 if (DEBUG_CLEANUP) Slog.v(
2864 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002865 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2866 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2867 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2868 final ActivityRecord r = activities.get(activityNdx);
2869 --i;
2870 if (DEBUG_CLEANUP) Slog.v(
2871 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2872 if (r.app == app) {
2873 boolean remove;
2874 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2875 // Don't currently have state for the activity, or
2876 // it is finishing -- always remove it.
2877 remove = true;
2878 } else if (r.launchCount > 2 &&
2879 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2880 // We have launched this activity too many times since it was
2881 // able to run, so give up and remove it.
2882 remove = true;
2883 } else {
2884 // The process may be gone, but the activity lives on!
2885 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002886 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002887 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002888 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002889 RuntimeException here = new RuntimeException("here");
2890 here.fillInStackTrace();
2891 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2892 + ": haveState=" + r.haveState
2893 + " stateNotNeeded=" + r.stateNotNeeded
2894 + " finishing=" + r.finishing
2895 + " state=" + r.state, here);
2896 }
2897 if (!r.finishing) {
2898 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2899 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2900 r.userId, System.identityHashCode(r),
2901 r.task.taskId, r.shortComponentName,
2902 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07002903 if (r.state == ActivityState.RESUMED) {
2904 mService.updateUsageStats(r, false);
2905 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002906 }
2907 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002908
Craig Mautner0247fc82013-02-28 14:32:06 -08002909 } else {
2910 // We have the current state for this activity, so
2911 // it can be restarted later when needed.
2912 if (localLOGV) Slog.v(
2913 TAG, "Keeping entry, setting app to null");
2914 if (r.visible) {
2915 hasVisibleActivities = true;
2916 }
2917 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2918 + r);
2919 r.app = null;
2920 r.nowVisible = false;
2921 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002922 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08002923 "App died, clearing saved state of " + r);
2924 r.icicle = null;
2925 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002926 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002927
Craig Mautnerd2328952013-03-05 12:46:26 -08002928 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002929 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002930 }
2931 }
2932
2933 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002934 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002935
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002936 final void updateTransitLocked(int transit, Bundle options) {
2937 if (options != null) {
2938 ActivityRecord r = topRunningActivityLocked(null);
2939 if (r != null && r.state != ActivityState.RESUMED) {
2940 r.updateOptionsLocked(options);
2941 } else {
2942 ActivityOptions.abort(options);
2943 }
2944 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002945 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002946 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002947
Craig Mautnera82aa092013-09-13 15:34:08 -07002948 void moveHomeTaskToTop() {
2949 final int top = mTaskHistory.size() - 1;
2950 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
2951 final TaskRecord task = mTaskHistory.get(taskNdx);
2952 if (task.isHomeTask()) {
2953 mTaskHistory.remove(taskNdx);
2954 mTaskHistory.add(top, task);
2955 mWindowManager.moveTaskToTop(task.taskId);
2956 return;
2957 }
2958 }
2959 }
2960
Craig Mautnercae015f2013-02-08 14:31:27 -08002961 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002962 final TaskRecord task = taskForIdLocked(taskId);
2963 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08002964 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002965 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002966 }
Craig Mautneraab647e2013-02-28 16:31:36 -08002967 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2968 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07002969 // we'll just indicate that this task returns to the home task.
Craig Mautnerae7ecab2013-09-18 11:48:14 -07002970 task.mOnTopOfHome = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08002971 }
2972 moveTaskToFrontLocked(task, null, options);
2973 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002974 }
2975 return false;
2976 }
2977
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002978 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002979 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002980
Craig Mautner11bf9a52013-02-19 14:08:51 -08002981 final int numTasks = mTaskHistory.size();
2982 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07002983 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002984 // nothing to do!
2985 if (reason != null &&
2986 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2987 ActivityOptions.abort(options);
2988 } else {
2989 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
2990 }
2991 return;
2992 }
2993
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07002994 mStackSupervisor.moveHomeStack(isHomeStack());
2995
Craig Mautner11bf9a52013-02-19 14:08:51 -08002996 // Shift all activities with this task up to the top
2997 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07002998 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08002999
3000 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003001 if (reason != null &&
3002 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003003 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003004 ActivityRecord r = topRunningActivityLocked(null);
3005 if (r != null) {
3006 mNoAnimActivities.add(r);
3007 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003008 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003009 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003010 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003011 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003012
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003013 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003014
Craig Mautner05d29032013-05-03 13:40:13 -07003015 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003016 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003017
3018 if (VALIDATE_TOKENS) {
3019 validateAppTokensLocked();
3020 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003021 }
3022
3023 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003024 * Worker method for rearranging history stack. Implements the function of moving all
3025 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003026 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003027 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003028 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3029 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003030 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003031 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003032 * @return Returns true if the move completed, false if not.
3033 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003034 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
3035 Slog.i(TAG, "moveTaskToBack: " + taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003036
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003037 // If we have a watcher, preflight the move before committing to it. First check
3038 // for *other* available tasks, but if none are available, then try again allowing the
3039 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003040 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003041 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003042 if (next == null) {
3043 next = topRunningActivityLocked(null, 0);
3044 }
3045 if (next != null) {
3046 // ask watcher if this is allowed
3047 boolean moveOK = true;
3048 try {
3049 moveOK = mService.mController.activityResuming(next.packageName);
3050 } catch (RemoteException e) {
3051 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003052 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003053 }
3054 if (!moveOK) {
3055 return false;
3056 }
3057 }
3058 }
3059
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003060 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003061 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003062
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003063 final TaskRecord tr = taskForIdLocked(taskId);
Craig Mautnerd2328952013-03-05 12:46:26 -08003064 if (tr == null) {
3065 return false;
3066 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003067
Craig Mautner11bf9a52013-02-19 14:08:51 -08003068 mTaskHistory.remove(tr);
3069 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003070
Craig Mautnerc8143c62013-09-03 12:15:57 -07003071 // There is an assumption that moving a task to the back moves it behind the home activity.
3072 // We make sure here that some activity in the stack will launch home.
3073 ActivityRecord lastActivity = null;
3074 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003075 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3076 final TaskRecord task = mTaskHistory.get(taskNdx);
3077 if (task.mOnTopOfHome) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003078 break;
3079 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003080 if (taskNdx == 1) {
3081 // Set the last task before tr to go to home.
3082 task.mOnTopOfHome = true;
3083 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003084 }
3085
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003086 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003087 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3088 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003089 ActivityRecord r = topRunningActivityLocked(null);
3090 if (r != null) {
3091 mNoAnimActivities.add(r);
3092 }
3093 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003094 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003095 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003096 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003097
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003098 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003099 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003100 }
3101
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003102 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
3103 if (task == tr && task.mOnTopOfHome || numTasks <= 1) {
3104 task.mOnTopOfHome = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003105 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003106 }
3107
Craig Mautner05d29032013-05-03 13:40:13 -07003108 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003109 return true;
3110 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003111
Craig Mautner8849a5e2013-04-02 16:41:03 -07003112 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003113 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003114 final Uri data = r.intent.getData();
3115 final String strData = data != null ? data.toSafeString() : null;
3116
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003117 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003118 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003119 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003120 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003121 }
3122
3123 /**
3124 * Make sure the given activity matches the current configuration. Returns
3125 * false if the activity had to be destroyed. Returns true if the
3126 * configuration is the same, or the activity will remain running as-is
3127 * for whatever reason. Ensures the HistoryRecord is updated with the
3128 * correct configuration and all other bookkeeping is handled.
3129 */
3130 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3131 int globalChanges) {
3132 if (mConfigWillChange) {
3133 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3134 "Skipping config check (will change): " + r);
3135 return true;
3136 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003137
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003138 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3139 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003140
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003141 // Short circuit: if the two configurations are the exact same
3142 // object (the common case), then there is nothing to do.
3143 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003144 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003145 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3146 "Configuration unchanged in " + r);
3147 return true;
3148 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003149
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003150 // We don't worry about activities that are finishing.
3151 if (r.finishing) {
3152 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3153 "Configuration doesn't matter in finishing " + r);
3154 r.stopFreezingScreenLocked(false);
3155 return true;
3156 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003157
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003158 // Okay we now are going to make this activity have the new config.
3159 // But then we need to figure out how it needs to deal with that.
3160 Configuration oldConfig = r.configuration;
3161 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003162
3163 // Determine what has changed. May be nothing, if this is a config
3164 // that has come back from the app after going idle. In that case
3165 // we just want to leave the official config object now in the
3166 // activity and do nothing else.
3167 final int changes = oldConfig.diff(newConfig);
3168 if (changes == 0 && !r.forceNewConfig) {
3169 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3170 "Configuration no differences in " + r);
3171 return true;
3172 }
3173
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003174 // If the activity isn't currently running, just leave the new
3175 // configuration and it will pick that up next time it starts.
3176 if (r.app == null || r.app.thread == null) {
3177 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3178 "Configuration doesn't matter not running " + r);
3179 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003180 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003181 return true;
3182 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003183
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003184 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003185 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3186 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3187 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003188 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003189 + ", newConfig=" + newConfig);
3190 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003191 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003192 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3193 r.configChangeFlags |= changes;
3194 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003195 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003196 if (r.app == null || r.app.thread == null) {
3197 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003198 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003199 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003200 } else if (r.state == ActivityState.PAUSING) {
3201 // A little annoying: we are waiting for this activity to
3202 // finish pausing. Let's not do anything now, but just
3203 // flag that it needs to be restarted when done pausing.
3204 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003205 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003206 r.configDestroy = true;
3207 return true;
3208 } else if (r.state == ActivityState.RESUMED) {
3209 // Try to optimize this case: the configuration is changing
3210 // and we need to restart the top, resumed activity.
3211 // Instead of doing the normal handshaking, just say
3212 // "restart!".
3213 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003214 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003215 relaunchActivityLocked(r, r.configChangeFlags, true);
3216 r.configChangeFlags = 0;
3217 } else {
3218 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003219 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003220 relaunchActivityLocked(r, r.configChangeFlags, false);
3221 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003222 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003223
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003224 // All done... tell the caller we weren't able to keep this
3225 // activity around.
3226 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003227 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003228
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003229 // Default case: the activity can handle this new configuration, so
3230 // hand it over. Note that we don't need to give it the new
3231 // configuration, since we always send configuration changes to all
3232 // process when they happen so it can just use whatever configuration
3233 // it last got.
3234 if (r.app != null && r.app.thread != null) {
3235 try {
3236 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003237 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003238 } catch (RemoteException e) {
3239 // If process died, whatever.
3240 }
3241 }
3242 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003243
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003244 return true;
3245 }
3246
Craig Mautnerc8143c62013-09-03 12:15:57 -07003247 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003248 int changes, boolean andResume) {
3249 List<ResultInfo> results = null;
3250 List<Intent> newIntents = null;
3251 if (andResume) {
3252 results = r.results;
3253 newIntents = r.newIntents;
3254 }
3255 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3256 + " with results=" + results + " newIntents=" + newIntents
3257 + " andResume=" + andResume);
3258 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003259 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003260 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003261
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003262 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003263
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003264 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003265 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3266 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3267 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003268 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003269 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003270 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003271 // Note: don't need to call pauseIfSleepingLocked() here, because
3272 // the caller will only pass in 'andResume' if this activity is
3273 // currently resumed, which implies we aren't sleeping.
3274 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003275 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003276 }
3277
3278 if (andResume) {
3279 r.results = null;
3280 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003281 r.state = ActivityState.RESUMED;
3282 } else {
3283 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3284 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003285 }
3286
3287 return true;
3288 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003289
3290 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003291 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3292 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3293 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3294 final ActivityRecord r = activities.get(activityNdx);
3295 if (r.appToken == token) {
3296 return true;
3297 }
3298 if (r.fullscreen && !r.finishing) {
3299 return false;
3300 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003301 }
3302 }
3303 return true;
3304 }
3305
3306 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003307 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3308 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3309 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3310 final ActivityRecord r = activities.get(activityNdx);
3311 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003312 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003313 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003314 }
3315 }
3316 }
3317
3318 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3319 boolean didSomething = false;
3320 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003321 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3322 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3323 int numActivities = activities.size();
3324 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3325 ActivityRecord r = activities.get(activityNdx);
3326 final boolean samePackage = r.packageName.equals(name)
3327 || (name == null && r.userId == userId);
3328 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3329 && (samePackage || r.task == lastTask)
3330 && (r.app == null || evenPersistent || !r.app.persistent)) {
3331 if (!doit) {
3332 if (r.finishing) {
3333 // If this activity is just finishing, then it is not
3334 // interesting as far as something to stop.
3335 continue;
3336 }
3337 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003338 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003339 didSomething = true;
3340 Slog.i(TAG, " Force finishing activity " + r);
3341 if (samePackage) {
3342 if (r.app != null) {
3343 r.app.removed = true;
3344 }
3345 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003346 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003347 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003348 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3349 true)) {
3350 // r has been deleted from mActivities, accommodate.
3351 --numActivities;
3352 --activityNdx;
3353 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003354 }
3355 }
3356 }
3357 return didSomething;
3358 }
3359
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003360 ActivityRecord getTasksLocked(IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003361 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003362 ActivityRecord topRecord = null;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003363 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003364 final TaskRecord task = mTaskHistory.get(taskNdx);
3365 ActivityRecord r = null;
3366 ActivityRecord top = null;
3367 int numActivities = 0;
3368 int numRunning = 0;
3369 final ArrayList<ActivityRecord> activities = task.mActivities;
3370 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3371 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003372
Craig Mautneraab647e2013-02-28 16:31:36 -08003373 // Initialize state for next task if needed.
3374 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3375 top = r;
3376 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003377 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003378
3379 // Add 'r' into the current task.
3380 numActivities++;
3381 if (r.app != null && r.app.thread != null) {
3382 numRunning++;
3383 }
3384
3385 if (localLOGV) Slog.v(
3386 TAG, r.intent.getComponent().flattenToShortString()
3387 + ": task=" + r.task);
3388 }
3389
3390 RunningTaskInfo ci = new RunningTaskInfo();
3391 ci.id = task.taskId;
3392 ci.baseActivity = r.intent.getComponent();
3393 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003394 ci.lastActiveTime = task.lastActiveTime;
3395
Craig Mautneraab647e2013-02-28 16:31:36 -08003396 if (top.thumbHolder != null) {
3397 ci.description = top.thumbHolder.lastDescription;
3398 }
3399 ci.numActivities = numActivities;
3400 ci.numRunning = numRunning;
3401 //System.out.println(
3402 // "#" + maxNum + ": " + " descr=" + ci.description);
3403 if (receiver != null) {
3404 if (localLOGV) Slog.v(
3405 TAG, "State=" + top.state + "Idle=" + top.idle
3406 + " app=" + top.app
3407 + " thr=" + (top.app != null ? top.app.thread : null));
3408 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3409 if (top.idle && top.app != null && top.app.thread != null) {
3410 topRecord = top;
3411 } else {
3412 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003413 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003414 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003415 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003416 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003417 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003418 }
3419 return topRecord;
3420 }
3421
3422 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003423 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003424 if (DEBUG_SWITCH) Slog.d(
3425 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003426 if (top >= 0) {
3427 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3428 int activityTop = activities.size() - 1;
3429 if (activityTop > 0) {
3430 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3431 "unhandled-back", true);
3432 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003433 }
3434 }
3435
Craig Mautner6b74cb52013-09-27 17:02:21 -07003436 /**
3437 * Reset local parameters because an app's activity died.
3438 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003439 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003440 */
3441 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003442 if (mPausingActivity != null && mPausingActivity.app == app) {
3443 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3444 "App died while pausing: " + mPausingActivity);
3445 mPausingActivity = null;
3446 }
3447 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3448 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003449 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003450 }
3451
Craig Mautner19091252013-10-05 00:03:53 -07003452 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003453 }
3454
Craig Mautnercae015f2013-02-08 14:31:27 -08003455 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003456 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3457 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3458 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3459 final ActivityRecord r = activities.get(activityNdx);
3460 if (r.app == app) {
3461 Slog.w(TAG, " Force finishing activity "
3462 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003463 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003464 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003465 }
3466 }
3467 }
3468
Dianne Hackborn390517b2013-05-30 15:03:32 -07003469 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003470 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003471 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003472 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3473 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003474 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3475 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003476 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003477 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003478 if (printed) {
3479 header = null;
3480 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003481 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003482 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003483 }
3484
3485 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3486 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3487
3488 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003489 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3490 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003491 }
3492 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003493 final int top = mTaskHistory.size() - 1;
3494 if (top >= 0) {
3495 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3496 int listTop = list.size() - 1;
3497 if (listTop >= 0) {
3498 activities.add(list.get(listTop));
3499 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003500 }
3501 } else {
3502 ItemMatcher matcher = new ItemMatcher();
3503 matcher.build(name);
3504
Craig Mautneraab647e2013-02-28 16:31:36 -08003505 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3506 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3507 if (matcher.match(r1, r1.intent.getComponent())) {
3508 activities.add(r1);
3509 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003510 }
3511 }
3512 }
3513
3514 return activities;
3515 }
3516
3517 ActivityRecord restartPackage(String packageName) {
3518 ActivityRecord starting = topRunningActivityLocked(null);
3519
3520 // All activities that came from the package must be
3521 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003522 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3523 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3524 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3525 final ActivityRecord a = activities.get(activityNdx);
3526 if (a.info.packageName.equals(packageName)) {
3527 a.forceNewConfig = true;
3528 if (starting != null && a == starting && a.visible) {
3529 a.startFreezingScreenLocked(starting.app,
3530 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3531 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003532 }
3533 }
3534 }
3535
3536 return starting;
3537 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003538
Craig Mautnerde4ef022013-04-07 19:01:33 -07003539 boolean removeTask(TaskRecord task) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003540 final int taskNdx = mTaskHistory.indexOf(task);
3541 final int topTaskNdx = mTaskHistory.size() - 1;
3542 if (task.mOnTopOfHome && taskNdx < topTaskNdx) {
3543 mTaskHistory.get(taskNdx + 1).mOnTopOfHome = true;
3544 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003545 mTaskHistory.remove(task);
Craig Mautnerc8143c62013-09-03 12:15:57 -07003546 return mTaskHistory.isEmpty();
Craig Mautner0247fc82013-02-28 14:32:06 -08003547 }
3548
Craig Mautnerde4ef022013-04-07 19:01:33 -07003549 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003550 TaskRecord task = new TaskRecord(taskId, info, intent);
3551 addTask(task, toTop);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003552 return task;
3553 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003554
3555 ArrayList<TaskRecord> getAllTasks() {
3556 return new ArrayList<TaskRecord>(mTaskHistory);
3557 }
3558
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003559 void addTask(final TaskRecord task, final boolean toTop) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003560 task.stack = this;
3561 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003562 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003563 } else {
3564 mTaskHistory.add(0, task);
3565 }
3566 }
3567
3568 public int getStackId() {
3569 return mStackId;
3570 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003571
3572 @Override
3573 public String toString() {
3574 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3575 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003576}