blob: 11a058ef99af3ea5fcee75b539850331450757f5 [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 * @return whether there are any activities for the specified user.
553 */
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700554 final boolean switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800555 if (VALIDATE_TOKENS) {
556 validateAppTokensLocked();
557 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800558 if (mCurrentUser == userId) {
559 return true;
560 }
561 mCurrentUser = userId;
562
563 // Move userId's tasks to the top.
564 boolean haveActivities = false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800565 int index = mTaskHistory.size();
566 for (int i = 0; i < index; ++i) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700567 TaskRecord task = mTaskHistory.get(i);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800568 if (task.userId == userId) {
569 haveActivities = true;
570 mTaskHistory.remove(i);
571 mTaskHistory.add(task);
572 --index;
573 }
574 }
575
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700576 return haveActivities;
Amith Yamasani742a6712011-05-04 14:49:28 -0700577 }
578
Craig Mautner2420ead2013-04-01 17:13:20 -0700579 void minimalResumeActivityLocked(ActivityRecord r) {
580 r.state = ActivityState.RESUMED;
581 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
582 + " (starting new instance)");
583 r.stopped = false;
584 mResumedActivity = r;
585 r.task.touchActiveTime();
586 mService.addRecentTaskLocked(r.task);
587 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700588 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -0700589 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700590 }
591
Dianne Hackborncee04b52013-07-03 17:01:28 -0700592 private void startLaunchTraces() {
593 if (mFullyDrawnStartTime != 0) {
594 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
595 }
596 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
597 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
598 }
599
600 private void stopFullyDrawnTraceIfNeeded() {
601 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
602 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
603 mFullyDrawnStartTime = 0;
604 }
605 }
606
Craig Mautnere79d42682013-04-01 19:01:53 -0700607 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700608 if (r.displayStartTime == 0) {
609 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
610 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700611 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700612 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700613 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700614 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700615 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700616 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700617 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700618 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800619
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700620 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700621 // Make sure that there is no activity waiting for this to launch.
622 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
623 r.displayStartTime = r.fullyDrawnStartTime = 0;
624 } else {
625 mStackSupervisor.removeTimeoutsForActivityLocked(r);
626 mStackSupervisor.scheduleIdleTimeoutLocked(r);
627 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700628 }
629
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800630 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800631 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800632 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
633 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
634 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
635 activities.get(activityNdx).setSleeping(false);
636 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800637 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800638 }
639
Craig Mautner0eea92c2013-05-16 13:35:39 -0700640 /**
641 * @return true if something must be done before going to sleep.
642 */
643 boolean checkReadyForSleepLocked() {
644 if (mResumedActivity != null) {
645 // Still have something resumed; can't sleep until it is paused.
646 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
647 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
648 startPausingLocked(false, true);
649 return true;
650 }
651 if (mPausingActivity != null) {
652 // Still waiting for something to pause; can't sleep yet.
653 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
654 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800655 }
656
Craig Mautner0eea92c2013-05-16 13:35:39 -0700657 return false;
658 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800659
Craig Mautner0eea92c2013-05-16 13:35:39 -0700660 void goToSleep() {
661 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800662
Craig Mautner0eea92c2013-05-16 13:35:39 -0700663 // Make sure any stopped but visible activities are now sleeping.
664 // This ensures that the activity's onStop() is called.
665 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
666 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
667 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
668 final ActivityRecord r = activities.get(activityNdx);
669 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
670 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800671 }
672 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800673 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700674 }
Craig Mautner59c00972012-07-30 12:10:24 -0700675
Dianne Hackbornd2835932010-12-13 16:28:46 -0800676 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800677 if (who.noDisplay) {
678 return null;
679 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800680
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800681 Resources res = mService.mContext.getResources();
682 int w = mThumbnailWidth;
683 int h = mThumbnailHeight;
684 if (w < 0) {
685 mThumbnailWidth = w =
686 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
687 mThumbnailHeight = h =
688 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
689 }
690
691 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800692 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
693 || mLastScreenshotBitmap.getWidth() != w
694 || mLastScreenshotBitmap.getHeight() != h) {
695 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700696 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700697 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800698 }
699 if (mLastScreenshotBitmap != null) {
John Reck172e87c2013-10-02 16:55:16 -0700700 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800701 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800702 }
703 return null;
704 }
705
Craig Mautnercf910b02013-04-23 11:23:27 -0700706 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800707 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800708 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700709 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800710 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700711 ActivityRecord prev = mResumedActivity;
712 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700713 Slog.e(TAG, "Trying to pause when nothing is resumed",
714 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700715 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700716 return;
717 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700718 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
719 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700720 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800721 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700722 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700723 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
724 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700725 prev.state = ActivityState.PAUSING;
726 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700727 clearLaunchTime(prev);
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700728 prev.updateThumbnail(screenshotActivities(prev), null);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700729 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700730
731 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800732
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700733 if (prev.app != null && prev.app.thread != null) {
734 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
735 try {
736 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700737 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700738 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700739 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800740 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
741 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700742 } catch (Exception e) {
743 // Ignore exception, if process died other code will cleanup.
744 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800745 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700746 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700747 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700748 }
749 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800750 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700751 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700752 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700753 }
754
755 // If we are not going to sleep, we want to ensure the device is
756 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700757 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700758 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700759 }
760
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800761 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700762 // Have the window manager pause its key dispatching until the new
763 // activity has started. If we're pausing the activity just because
764 // the screen is being turned off and the UI is sleeping, don't interrupt
765 // key dispatch; the same activity will pick it up again on wakeup.
766 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800767 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700768 } else {
769 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
770 }
771
772 // Schedule a pause timeout in case the app doesn't respond.
773 // We don't give it much time because this directly impacts the
774 // responsiveness seen by the user.
775 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
776 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700777 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700778 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
779 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
780 } else {
781 // This activity failed to schedule the
782 // pause, so just treat it as being paused now.
783 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700784 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700785 }
786 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700787
Craig Mautnerd2328952013-03-05 12:46:26 -0800788 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700789 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800790 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700791
Craig Mautnerd2328952013-03-05 12:46:26 -0800792 final ActivityRecord r = isInStackLocked(token);
793 if (r != null) {
794 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
795 if (mPausingActivity == r) {
796 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
797 + (timeout ? " (due to timeout)" : " (pause complete)"));
798 r.state = ActivityState.PAUSED;
799 completePauseLocked();
800 } else {
801 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
802 r.userId, System.identityHashCode(r), r.shortComponentName,
803 mPausingActivity != null
804 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700805 }
806 }
807 }
808
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700809 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
810 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700811 if (r.state != ActivityState.STOPPING) {
812 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
813 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
814 return;
815 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700816 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700817 if (icicle != null) {
818 // If icicle is null, this is happening due to a timeout, so we
819 // haven't really saved the state.
820 r.icicle = icicle;
821 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800822 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700823 r.updateThumbnail(thumbnail, description);
824 }
825 if (!r.stopped) {
826 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
827 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
828 r.stopped = true;
829 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700830 if (r.finishing) {
831 r.clearOptionsLocked();
832 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700833 if (r.configDestroy) {
834 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700835 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700836 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700837 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800838 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700839 }
840 }
841 }
842
Craig Mautnerc8143c62013-09-03 12:15:57 -0700843 private void completePauseLocked() {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800844 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700845 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800846
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800847 if (prev != null) {
848 if (prev.finishing) {
849 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700850 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800851 } else if (prev.app != null) {
852 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
853 if (prev.waitingVisible) {
854 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700855 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800856 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
857 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800858 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800859 if (prev.configDestroy) {
860 // The previous is being paused because the configuration
861 // is changing, which means it is actually stopping...
862 // To juggle the fact that we are also starting a new
863 // instance right now, we need to first completely stop
864 // the current instance before starting the new one.
865 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
866 destroyActivityLocked(prev, true, false, "pause-config");
867 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700868 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700869 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
870 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800871 // If we already have a few activities waiting to stop,
872 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700873 // them out. Or if r is the last of activity of the last task the stack
874 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800875 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700876 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800877 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700878 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800879 }
880 }
881 } else {
882 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
883 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800884 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800885 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700886 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800887
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700888 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700889 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700890 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800891 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700892 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700893 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700894 if (top == null || (prev != null && top != prev)) {
895 // If there are no more activities available to run,
896 // do resume anyway to start something. Also if the top
897 // activity on the stack is not the just paused activity,
898 // we need to go ahead and resume it to ensure we complete
899 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700900 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700901 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700902 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800903
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800904 if (prev != null) {
905 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700906
Craig Mautner525f3d92013-05-07 14:01:50 -0700907 if (prev.app != null && prev.cpuTimeAtResume > 0
908 && mService.mBatteryStatsService.isOnBattery()) {
909 long diff;
Dianne Hackbornd2932242013-08-05 18:18:42 -0700910 synchronized (mService.mProcessCpuThread) {
911 diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
Craig Mautner525f3d92013-05-07 14:01:50 -0700912 - prev.cpuTimeAtResume;
913 }
914 if (diff > 0) {
915 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
916 synchronized (bsi) {
917 BatteryStatsImpl.Uid.Proc ps =
918 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -0700919 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -0700920 if (ps != null) {
921 ps.addForegroundTimeLocked(diff);
922 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700923 }
924 }
925 }
Craig Mautner525f3d92013-05-07 14:01:50 -0700926 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700927 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700928 }
929
930 /**
931 * Once we know that we have asked an application to put an activity in
932 * the resumed state (either by launching it or explicitly telling it),
933 * this function updates the rest of our state to match that fact.
934 */
Craig Mautner525f3d92013-05-07 14:01:50 -0700935 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700936 next.idle = false;
937 next.results = null;
938 next.newIntents = null;
Craig Mautner07566322013-09-26 16:42:55 -0700939 if (next.nowVisible) {
940 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
941 mStackSupervisor.dismissKeyguard();
942 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700943
944 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -0700945 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700946
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700947 mStackSupervisor.reportResumedActivityLocked(next);
948
949 next.resumeKeyDispatchingLocked();
950 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700951
952 // Mark the point when the activity is resuming
953 // TODO: To be more accurate, the mark should be before the onCreate,
954 // not after the onResume. But for subsequent starts, onResume is fine.
955 if (next.app != null) {
Dianne Hackbornd2932242013-08-05 18:18:42 -0700956 synchronized (mService.mProcessCpuThread) {
957 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700958 }
959 } else {
960 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
961 }
962 }
963
Craig Mautner580ea812013-04-25 12:58:38 -0700964 /**
965 * Version of ensureActivitiesVisible that can easily be called anywhere.
966 */
967 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
968 return ensureActivitiesVisibleLocked(starting, configChanges, false);
969 }
970
971 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
972 boolean forceHomeShown) {
973 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -0700974 return r != null &&
975 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -0700976 }
977
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700978 /**
979 * Make sure that all activities that need to be visible (that is, they
980 * currently can be seen by the user) actually are.
981 */
Craig Mautner580ea812013-04-25 12:58:38 -0700982 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
983 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Craig Mautner323f7802013-10-01 21:16:22 -0700984 if (true || DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700985 TAG, "ensureActivitiesVisible behind " + top
986 + " configChanges=0x" + Integer.toHexString(configChanges));
987
Craig Mautner5eda9b32013-07-02 11:58:16 -0700988 if (mTranslucentActivityWaiting != top) {
989 mUndrawnActivitiesBelowTopTranslucent.clear();
990 if (mTranslucentActivityWaiting != null) {
991 // Call the callback with a timeout indication.
992 notifyActivityDrawnLocked(null);
993 mTranslucentActivityWaiting = null;
994 }
995 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
996 }
997
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700998 // If the top activity is not fullscreen, then we need to
999 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001000 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -07001001 boolean showHomeBehindStack = false;
1002 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
1003 !(forceHomeShown && isHomeStack());
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001004 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001005 final TaskRecord task = mTaskHistory.get(taskNdx);
1006 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001007 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1008 final ActivityRecord r = activities.get(activityNdx);
1009 if (r.finishing) {
1010 continue;
1011 }
1012 if (aboveTop && r != top) {
1013 continue;
1014 }
1015 aboveTop = false;
1016 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001017 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001018 TAG, "Make visible? " + r + " finishing=" + r.finishing
1019 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001020
Craig Mautnerd44711d2013-02-23 11:24:36 -08001021 final boolean doThisProcess = onlyThisProcess == null
1022 || onlyThisProcess.equals(r.processName);
1023
1024 // First: if this is not the current activity being started, make
1025 // sure it matches the current configuration.
1026 if (r != starting && doThisProcess) {
1027 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001028 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001029
1030 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001031 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001032 // This activity needs to be visible, but isn't even
1033 // running... get it started, but don't resume it
1034 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001035 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001036 if (r != starting) {
1037 r.startFreezingScreenLocked(r.app, configChanges);
1038 }
1039 if (!r.visible) {
Craig Mautner323f7802013-10-01 21:16:22 -07001040 if (true || DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001041 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001042 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001043 }
1044 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001045 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001046 }
1047 }
1048
1049 } else if (r.visible) {
1050 // If this activity is already visible, then there is nothing
1051 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001052 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001053 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001054
1055 } else if (onlyThisProcess == null) {
1056 // This activity is not currently visible, but is running.
1057 // Tell it to become visible.
1058 r.visible = true;
1059 if (r.state != ActivityState.RESUMED && r != starting) {
1060 // If this activity is paused, tell it
1061 // to now show its window.
Craig Mautner323f7802013-10-01 21:16:22 -07001062 if (true || DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001063 TAG, "Making visible and scheduling visibility: " + r);
1064 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001065 if (mTranslucentActivityWaiting != null) {
1066 mUndrawnActivitiesBelowTopTranslucent.add(r);
1067 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001068 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001069 r.sleeping = false;
1070 r.app.pendingUiClean = true;
1071 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1072 r.stopFreezingScreenLocked(false);
1073 } catch (Exception e) {
1074 // Just skip on any failure; we'll make it
1075 // visible when it next restarts.
1076 Slog.w(TAG, "Exception thrown making visibile: "
1077 + r.intent.getComponent(), e);
1078 }
1079 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001080 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001081
Craig Mautnerd44711d2013-02-23 11:24:36 -08001082 // Aggregate current change flags.
1083 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001084
Craig Mautnerd44711d2013-02-23 11:24:36 -08001085 if (r.fullscreen) {
1086 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001087 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1088 behindFullscreen = true;
Craig Mautner19d112d2013-09-30 10:34:55 -07001089 } else if (task.mOnTopOfHome) {
1090 // Work our way down from r to bottom of task and see if there are any
1091 // visible activities below r.
1092 int rIndex = task.mActivities.indexOf(r);
1093 for ( --rIndex; rIndex >= 0; --rIndex) {
1094 final ActivityRecord blocker = task.mActivities.get(rIndex);
1095 if (!blocker.finishing && blocker.visible) {
1096 if (DEBUG_VISBILITY) Slog.v(TAG, "Home visibility for " +
1097 r + " blocked by " + blocker);
1098 break;
1099 }
1100 }
1101 if (rIndex < 0) {
1102 // Got to task bottom without finding a visible activity, show home.
1103 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1104 showHomeBehindStack = true;
1105 behindFullscreen = true;
1106 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001107 }
1108 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001109 if (DEBUG_VISBILITY) Slog.v(
1110 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1111 + " state=" + r.state
1112 + " behindFullscreen=" + behindFullscreen);
1113 // Now for any activities that aren't visible to the user, make
1114 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001115 if (r.visible) {
Craig Mautner323f7802013-10-01 21:16:22 -07001116 if (true || DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001117 r.visible = false;
1118 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001119 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001120 switch (r.state) {
1121 case STOPPING:
1122 case STOPPED:
1123 if (r.app != null && r.app.thread != null) {
1124 if (DEBUG_VISBILITY) Slog.v(
1125 TAG, "Scheduling invisibility: " + r);
1126 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1127 }
1128 break;
1129
1130 case INITIALIZING:
1131 case RESUMED:
1132 case PAUSING:
1133 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001134 // This case created for transitioning activities from
1135 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautnere5273b42013-09-09 12:57:47 -07001136 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1137 mStackSupervisor.mStoppingActivities.add(r);
1138 }
1139 mStackSupervisor.scheduleIdleLocked();
Craig Mautner4addfc52013-06-25 08:05:45 -07001140 break;
1141
1142 default:
1143 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001144 }
1145 } catch (Exception e) {
1146 // Just skip on any failure; we'll make it
1147 // visible when it next restarts.
1148 Slog.w(TAG, "Exception thrown making hidden: "
1149 + r.intent.getComponent(), e);
1150 }
1151 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001152 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001153 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001154 }
1155 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001156 }
Craig Mautner580ea812013-04-25 12:58:38 -07001157 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001158 }
Craig Mautner58547802013-03-05 08:23:53 -08001159
Craig Mautner5eda9b32013-07-02 11:58:16 -07001160 void convertToTranslucent(ActivityRecord r) {
1161 mTranslucentActivityWaiting = r;
1162 mUndrawnActivitiesBelowTopTranslucent.clear();
1163 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1164 }
1165
1166 /**
1167 * Called as activities below the top translucent activity are redrawn. When the last one is
1168 * redrawn notify the top activity by calling
1169 * {@link Activity#onTranslucentConversionComplete}.
1170 *
1171 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1172 * occurred and the activity will be notified immediately.
1173 */
1174 void notifyActivityDrawnLocked(ActivityRecord r) {
1175 if ((r == null)
1176 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1177 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1178 // The last undrawn activity below the top has just been drawn. If there is an
1179 // opaque activity at the top, notify it that it can become translucent safely now.
1180 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1181 mTranslucentActivityWaiting = null;
1182 mUndrawnActivitiesBelowTopTranslucent.clear();
1183 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1184
1185 if (waitingActivity != null && waitingActivity.app != null &&
1186 waitingActivity.app.thread != null) {
1187 try {
1188 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1189 waitingActivity.appToken, r != null);
1190 } catch (RemoteException e) {
1191 }
1192 }
1193 }
1194 }
1195
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001196 /**
1197 * Ensure that the top activity in the stack is resumed.
1198 *
1199 * @param prev The previously resumed activity, for when in the process
1200 * of pausing; can be null to call from elsewhere.
1201 *
1202 * @return Returns true if something is being resumed, or false if
1203 * nothing happened.
1204 */
1205 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001206 return resumeTopActivityLocked(prev, null);
1207 }
1208
1209 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001210 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1211
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001212 // Find the first activity that is not finishing.
1213 ActivityRecord next = topRunningActivityLocked(null);
1214
1215 // Remember how we'll process this pause/resume situation, and ensure
1216 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001217 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1218 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001219
1220 if (next == null) {
1221 // There are no more activities! Let's just start up the
1222 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001223 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001224 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001225 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001226 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001227 }
1228
1229 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001230
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001231 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001232 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1233 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001234 // Make sure we have executed any pending transitions, since there
1235 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001236 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001237 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001238 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001239 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001240 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001241 return false;
1242 }
1243
Craig Mautner525f3d92013-05-07 14:01:50 -07001244 final TaskRecord nextTask = next.task;
1245 final TaskRecord prevTask = prev != null ? prev.task : null;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001246 if (prevTask != null && prevTask.mOnTopOfHome && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001247 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001248 if (prevTask == nextTask) {
1249 ArrayList<ActivityRecord> activities = prevTask.mActivities;
1250 final int numActivities = activities.size();
1251 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1252 final ActivityRecord r = activities.get(activityNdx);
1253 // r is usually the same as next, but what if two activities were launched
1254 // before prev finished?
1255 if (!r.finishing) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001256 r.frontOfTask = true;
1257 break;
1258 }
1259 }
1260 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001261 // This task is going away but it was supposed to return to the home task.
1262 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001263 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001264 mTaskHistory.get(taskNdx).mOnTopOfHome = true;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001265 } else {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001266 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Launching home next");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001267 return mStackSupervisor.resumeHomeActivity(prev);
1268 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001269 }
1270
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001271 // If we are sleeping, and there is no resumed activity, and the top
1272 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001273 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001274 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001275 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001276 // Make sure we have executed any pending transitions, since there
1277 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001278 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001279 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001280 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001281 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001282 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001283 return false;
1284 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001285
1286 // Make sure that the user who owns this activity is started. If not,
1287 // we will just leave it as is because someone should be bringing
1288 // another user's activities to the top of the stack.
1289 if (mService.mStartedUsers.get(next.userId) == null) {
1290 Slog.w(TAG, "Skipping resume of top activity " + next
1291 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001292 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001293 return false;
1294 }
1295
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001296 // The activity may be waiting for stop, but that is no longer
1297 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001298 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001299 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001300 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001301 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001302
Dianne Hackborn84375872012-06-01 19:03:50 -07001303 next.updateOptionsLocked(options);
1304
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001305 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1306
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001307 // If we are currently pausing an activity, then don't do anything
1308 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001309 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001310 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1311 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001312 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001313 return false;
1314 }
1315
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001316 // Okay we are now going to start a switch, to 'next'. We may first
1317 // have to pause the current activity, but this is an important point
1318 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001319 // XXX "App Redirected" dialog is getting too many false positives
1320 // at this point, so turn off for now.
1321 if (false) {
1322 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1323 long now = SystemClock.uptimeMillis();
1324 final boolean inTime = mLastStartedActivity.startTime != 0
1325 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1326 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1327 final int nextUid = next.info.applicationInfo.uid;
1328 if (inTime && lastUid != nextUid
1329 && lastUid != next.launchedFromUid
1330 && mService.checkPermission(
1331 android.Manifest.permission.STOP_APP_SWITCHES,
1332 -1, next.launchedFromUid)
1333 != PackageManager.PERMISSION_GRANTED) {
1334 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1335 } else {
1336 next.startTime = now;
1337 mLastStartedActivity = next;
1338 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001339 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001340 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001341 mLastStartedActivity = next;
1342 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001343 }
Craig Mautner58547802013-03-05 08:23:53 -08001344
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001345 // We need to start pausing the current activity so the top one
1346 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001347 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001348 if (mResumedActivity != null) {
1349 pausing = true;
1350 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001351 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001352 }
1353 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001354 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1355 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001356 // At this point we want to put the upcoming activity's process
1357 // at the top of the LRU list, since we know we will be needing it
1358 // very soon and it would be a waste to let it get killed if it
1359 // happens to be sitting towards the end.
1360 if (next.app != null && next.app.thread != null) {
1361 // No reason to do full oom adj update here; we'll let that
1362 // happen whenever it needs to later.
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001363 mService.updateLruProcessLocked(next.app, false, true);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001364 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001365 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001366 return true;
1367 }
1368
Christopher Tated3f175c2012-06-14 14:16:54 -07001369 // If the most recent activity was noHistory but was only stopped rather
1370 // than stopped+finished because the device went to sleep, we need to make
1371 // sure to finish it as we're making a new activity topmost.
Craig Mautner0f922742013-08-06 08:44:42 -07001372 if (mService.mSleeping && mLastNoHistoryActivity != null &&
1373 !mLastNoHistoryActivity.finishing) {
1374 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1375 " on new resume");
1376 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1377 null, "no-history", false);
1378 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001379 }
1380
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001381 if (prev != null && prev != next) {
1382 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1383 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001384 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001385 if (DEBUG_SWITCH) Slog.v(
1386 TAG, "Resuming top, waiting visible to hide: " + prev);
1387 } else {
1388 // The next activity is already visible, so hide the previous
1389 // activity's windows right now so we can show the new one ASAP.
1390 // We only do this if the previous is finishing, which should mean
1391 // it is on top of the one being resumed so hiding it quickly
1392 // is good. Otherwise, we want to do the normal route of allowing
1393 // the resumed activity to be shown so we can decide if the
1394 // previous should actually be hidden depending on whether the
1395 // new one is found to be full-screen or not.
1396 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001397 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001398 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1399 + prev + ", waitingVisible="
1400 + (prev != null ? prev.waitingVisible : null)
1401 + ", nowVisible=" + next.nowVisible);
1402 } else {
1403 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1404 + prev + ", waitingVisible="
1405 + (prev != null ? prev.waitingVisible : null)
1406 + ", nowVisible=" + next.nowVisible);
1407 }
1408 }
1409 }
1410
Dianne Hackborne7f97212011-02-24 14:40:20 -08001411 // Launching this app's activity, make sure the app is no longer
1412 // considered stopped.
1413 try {
1414 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001415 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001416 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001417 } catch (IllegalArgumentException e) {
1418 Slog.w(TAG, "Failed trying to unstop package "
1419 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001420 }
1421
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001422 // We are starting up the next activity, so tell the window manager
1423 // that the previous one will be hidden soon. This way it can know
1424 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001425 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001426 if (prev != null) {
1427 if (prev.finishing) {
1428 if (DEBUG_TRANSITION) Slog.v(TAG,
1429 "Prepare close transition: prev=" + prev);
1430 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001431 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001432 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001433 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001434 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001435 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1436 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001437 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001438 mWindowManager.setAppWillBeHidden(prev.appToken);
1439 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001440 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001441 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001442 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001443 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001444 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001445 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001446 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001447 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1448 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001449 }
1450 }
1451 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001452 mWindowManager.setAppWillBeHidden(prev.appToken);
1453 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001454 }
Craig Mautner967212c2013-04-13 21:10:58 -07001455 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001456 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001457 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001458 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001459 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001460 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001461 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001462 }
1463 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001464 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001465 next.applyOptionsLocked();
1466 } else {
1467 next.clearOptionsLocked();
1468 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001469
Craig Mautnercf910b02013-04-23 11:23:27 -07001470 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001471 if (next.app != null && next.app.thread != null) {
1472 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1473
1474 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001475 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001476
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001477 // schedule launch ticks to collect information about slow apps.
1478 next.startLaunchTickingLocked();
1479
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001480 ActivityRecord lastResumedActivity =
1481 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001482 ActivityState lastState = next.state;
1483
1484 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001485
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001486 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001487 next.state = ActivityState.RESUMED;
1488 mResumedActivity = next;
1489 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001490 mService.addRecentTaskLocked(next.task);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001491 mService.updateLruProcessLocked(next.app, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001492 updateLRUListLocked(next);
1493
1494 // Have the window manager re-evaluate the orientation of
1495 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001496 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001497 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001498 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001499 mService.mConfiguration,
1500 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1501 if (config != null) {
1502 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001503 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001504 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001505 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001506
Craig Mautner525f3d92013-05-07 14:01:50 -07001507 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001508 // The configuration update wasn't able to keep the existing
1509 // instance of the activity, and instead started a new one.
1510 // We should be all done, but let's just make sure our activity
1511 // is still at the top and schedule another run if something
1512 // weird happened.
1513 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001514 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001515 "Activity config changed during resume: " + next
1516 + ", new next: " + nextNext);
1517 if (nextNext != next) {
1518 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001519 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001520 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001521 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001522 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001523 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001524 return true;
1525 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001526 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001527 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001528 }
Craig Mautner58547802013-03-05 08:23:53 -08001529
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001530 try {
1531 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001532 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001533 if (a != null) {
1534 final int N = a.size();
1535 if (!next.finishing && N > 0) {
1536 if (DEBUG_RESULTS) Slog.v(
1537 TAG, "Delivering results to " + next
1538 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001539 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001540 }
1541 }
1542
1543 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001544 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001545 }
1546
1547 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001548 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001549 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001550
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001551 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001552 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001553 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001554 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
1555 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001556 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001557
Craig Mautner0eea92c2013-05-16 13:35:39 -07001558 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001559
Craig Mautnerac6f8432013-07-17 13:24:59 -07001560 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001561 } catch (Exception e) {
1562 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001563 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1564 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001565 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001566 if (lastStack != null) {
1567 lastStack.mResumedActivity = lastResumedActivity;
1568 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001569 Slog.i(TAG, "Restarting because process died: " + next);
1570 if (!next.hasBeenLaunched) {
1571 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001572 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1573 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001574 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001575 next.appToken, next.packageName, next.theme,
1576 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001577 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1578 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001579 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001580 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001581 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001582 return true;
1583 }
1584
1585 // From this point on, if something goes wrong there is no way
1586 // to recover the activity.
1587 try {
1588 next.visible = true;
1589 completeResumeLocked(next);
1590 } catch (Exception e) {
1591 // If any exception gets thrown, toss away this
1592 // activity and try the next one.
1593 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001594 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001595 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001596 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001597 return true;
1598 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001599 next.stopped = false;
1600
1601 } else {
1602 // Whoops, need to restart this activity!
1603 if (!next.hasBeenLaunched) {
1604 next.hasBeenLaunched = true;
1605 } else {
1606 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001607 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001608 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001609 mService.compatibilityInfoForPackageLocked(
1610 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001611 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001612 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001613 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001614 }
1615 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1616 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001617 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
Craig Mautnere79d42682013-04-01 19:01:53 -07001618 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001619 }
1620
Craig Mautnercf910b02013-04-23 11:23:27 -07001621 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001622 return true;
1623 }
1624
Craig Mautnerac6f8432013-07-17 13:24:59 -07001625 private void insertTaskAtTop(TaskRecord task) {
1626 mTaskHistory.remove(task);
1627 // Now put task at top.
1628 int stackNdx = mTaskHistory.size();
1629 if (task.userId != mCurrentUser) {
1630 // Put non-current user tasks below current user tasks.
1631 while (--stackNdx >= 0) {
1632 if (mTaskHistory.get(stackNdx).userId != mCurrentUser) {
1633 break;
1634 }
1635 }
1636 ++stackNdx;
1637 }
1638 mTaskHistory.add(stackNdx, task);
1639 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001640
Craig Mautner8849a5e2013-04-02 16:41:03 -07001641 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001642 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001643 TaskRecord rTask = r.task;
1644 final int taskId = rTask.taskId;
1645 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001646 // Last activity in task had been removed or ActivityManagerService is reusing task.
1647 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001648 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001649 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001650 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001651 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001652 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001653 if (!newTask) {
1654 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001655 boolean startIt = true;
1656 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1657 task = mTaskHistory.get(taskNdx);
1658 if (task == r.task) {
1659 // Here it is! Now, if this is not yet visible to the
1660 // user, then just add it without starting; it will
1661 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001662 if (!startIt) {
1663 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1664 + task, new RuntimeException("here").fillInStackTrace());
1665 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001666 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001667 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1668 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001669 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
1670 r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001671 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001672 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001673 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001674 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001675 return;
1676 }
1677 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001678 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001679 startIt = false;
1680 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001681 }
1682 }
1683
1684 // Place a new activity at top of stack, so it is next to interact
1685 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001686
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001687 // If we are not placing the new activity frontmost, we do not want
1688 // to deliver the onUserLeaving callback to the actual frontmost
1689 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001690 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001691 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001692 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1693 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001694 }
Craig Mautner70a86932013-02-28 22:37:44 -08001695
1696 task = r.task;
1697
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001698 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001699 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001700 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001701 task.addActivityToTop(r);
1702
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001703 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001704 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001705 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001706 // We want to show the starting preview window if we are
1707 // switching to a new task, or the next activity's process is
1708 // not currently running.
1709 boolean showStartingIcon = newTask;
1710 ProcessRecord proc = r.app;
1711 if (proc == null) {
1712 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1713 }
1714 if (proc == null || proc.thread == null) {
1715 showStartingIcon = true;
1716 }
1717 if (DEBUG_TRANSITION) Slog.v(TAG,
1718 "Prepare open transition: starting " + r);
1719 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001720 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001721 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001722 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001723 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001724 ? AppTransition.TRANSIT_TASK_OPEN
1725 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001726 mNoAnimActivities.remove(r);
1727 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001728 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001729 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001730 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001731 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001732 boolean doShow = true;
1733 if (newTask) {
1734 // Even though this activity is starting fresh, we still need
1735 // to reset it to make sure we apply affinities to move any
1736 // existing activities from other tasks in to it.
1737 // If the caller has requested that the target task be
1738 // reset, then do so.
1739 if ((r.intent.getFlags()
1740 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1741 resetTaskIfNeededLocked(r, r);
1742 doShow = topRunningNonDelayedActivityLocked(null) == r;
1743 }
1744 }
1745 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1746 // Figure out if we are transitioning from another activity that is
1747 // "has the same starting icon" as the next one. This allows the
1748 // window manager to keep the previous window it had previously
1749 // created, if it still had one.
1750 ActivityRecord prev = mResumedActivity;
1751 if (prev != null) {
1752 // We don't want to reuse the previous starting preview if:
1753 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001754 if (prev.task != r.task) {
1755 prev = null;
1756 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001757 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001758 else if (prev.nowVisible) {
1759 prev = null;
1760 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001761 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001762 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001763 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001764 mService.compatibilityInfoForPackageLocked(
1765 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001766 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001767 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001768 }
1769 } else {
1770 // If this is the first activity, don't do any fancy animations,
1771 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001772 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001773 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001774 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001775 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001776 }
1777 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001778 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001779 }
1780
1781 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001782 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001783 }
1784 }
1785
Dianne Hackbornbe707852011-11-11 14:32:10 -08001786 final void validateAppTokensLocked() {
1787 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001788 mValidateAppTokens.ensureCapacity(numActivities());
1789 final int numTasks = mTaskHistory.size();
1790 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1791 TaskRecord task = mTaskHistory.get(taskNdx);
1792 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07001793 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08001794 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001795 }
Craig Mautner000f0022013-02-26 15:04:29 -08001796 TaskGroup group = new TaskGroup();
1797 group.taskId = task.taskId;
1798 mValidateAppTokens.add(group);
1799 final int numActivities = activities.size();
1800 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1801 final ActivityRecord r = activities.get(activityNdx);
1802 group.tokens.add(r.appToken);
1803 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001804 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001805 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001806 }
1807
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001808 /**
1809 * Perform a reset of the given task, if needed as part of launching it.
1810 * Returns the new HistoryRecord at the top of the task.
1811 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001812 /**
1813 * Helper method for #resetTaskIfNeededLocked.
1814 * We are inside of the task being reset... we'll either finish this activity, push it out
1815 * for another task, or leave it as-is.
1816 * @param task The task containing the Activity (taskTop) that might be reset.
1817 * @param forceReset
1818 * @return An ActivityOptions that needs to be processed.
1819 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001820 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001821 ActivityOptions topOptions = null;
1822
1823 int replyChainEnd = -1;
1824 boolean canMoveOptions = true;
1825
1826 // We only do this for activities that are not the root of the task (since if we finish
1827 // the root, we may no longer have the task!).
1828 final ArrayList<ActivityRecord> activities = task.mActivities;
1829 final int numActivities = activities.size();
1830 for (int i = numActivities - 1; i > 0; --i ) {
1831 ActivityRecord target = activities.get(i);
1832
1833 final int flags = target.info.flags;
1834 final boolean finishOnTaskLaunch =
1835 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1836 final boolean allowTaskReparenting =
1837 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1838 final boolean clearWhenTaskReset =
1839 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1840
1841 if (!finishOnTaskLaunch
1842 && !clearWhenTaskReset
1843 && target.resultTo != null) {
1844 // If this activity is sending a reply to a previous
1845 // activity, we can't do anything with it now until
1846 // we reach the start of the reply chain.
1847 // XXX note that we are assuming the result is always
1848 // to the previous activity, which is almost always
1849 // the case but we really shouldn't count on.
1850 if (replyChainEnd < 0) {
1851 replyChainEnd = i;
1852 }
1853 } else if (!finishOnTaskLaunch
1854 && !clearWhenTaskReset
1855 && allowTaskReparenting
1856 && target.taskAffinity != null
1857 && !target.taskAffinity.equals(task.affinity)) {
1858 // If this activity has an affinity for another
1859 // task, then we need to move it out of here. We will
1860 // move it as far out of the way as possible, to the
1861 // bottom of the activity stack. This also keeps it
1862 // correctly ordered with any activities we previously
1863 // moved.
Craig Mautnerdccb7702013-09-17 15:53:34 -07001864 final ActivityRecord bottom =
1865 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
1866 mTaskHistory.get(0).mActivities.get(0) : null;
1867 if (bottom != null && target.taskAffinity != null
1868 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001869 // If the activity currently at the bottom has the
1870 // same task affinity as the one we are moving,
1871 // then merge it into the same task.
Craig Mautnerdccb7702013-09-17 15:53:34 -07001872 target.setTask(bottom.task, bottom.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001873 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07001874 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001875 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001876 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001877 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001878 target.task.affinityIntent = target.intent;
1879 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1880 + " out to new task " + target.task);
1881 }
1882
1883 final TaskRecord targetTask = target.task;
1884 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001885 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001886
Craig Mautner525f3d92013-05-07 14:01:50 -07001887 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001888 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1889 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07001890 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001891 if (p.finishing) {
1892 continue;
1893 }
1894
Craig Mautner525f3d92013-05-07 14:01:50 -07001895 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001896 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001897 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001898 topOptions = p.takeOptionsLocked();
1899 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001900 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001901 }
1902 }
1903 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1904 + task + " adding to task=" + targetTask,
1905 new RuntimeException("here").fillInStackTrace());
1906 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1907 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001908 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001909 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001910
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001911 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001912 }
1913
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001914 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001915 if (VALIDATE_TOKENS) {
1916 validateAppTokensLocked();
1917 }
1918
1919 replyChainEnd = -1;
1920 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1921 // If the activity should just be removed -- either
1922 // because it asks for it, or the task should be
1923 // cleared -- then finish it and anything that is
1924 // part of its reply chain.
1925 int end;
1926 if (clearWhenTaskReset) {
1927 // In this case, we want to finish this activity
1928 // and everything above it, so be sneaky and pretend
1929 // like these are all in the reply chain.
1930 end = numActivities - 1;
1931 } else if (replyChainEnd < 0) {
1932 end = i;
1933 } else {
1934 end = replyChainEnd;
1935 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001936 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001937 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001938 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001939 if (p.finishing) {
1940 continue;
1941 }
1942 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001943 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001944 topOptions = p.takeOptionsLocked();
1945 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001946 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001947 }
1948 }
Craig Mautner58547802013-03-05 08:23:53 -08001949 if (DEBUG_TASKS) Slog.w(TAG,
1950 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001951 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001952 end--;
1953 srcPos--;
1954 }
1955 }
1956 replyChainEnd = -1;
1957 } else {
1958 // If we were in the middle of a chain, well the
1959 // activity that started it all doesn't want anything
1960 // special, so leave it all as-is.
1961 replyChainEnd = -1;
1962 }
1963 }
1964
1965 return topOptions;
1966 }
1967
1968 /**
1969 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1970 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1971 * @param affinityTask The task we are looking for an affinity to.
1972 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1973 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1974 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1975 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001976 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08001977 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001978 int replyChainEnd = -1;
1979 final int taskId = task.taskId;
1980 final String taskAffinity = task.affinity;
1981
1982 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1983 final int numActivities = activities.size();
1984 // Do not operate on the root Activity.
1985 for (int i = numActivities - 1; i > 0; --i) {
1986 ActivityRecord target = activities.get(i);
1987
1988 final int flags = target.info.flags;
1989 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1990 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1991
1992 if (target.resultTo != null) {
1993 // If this activity is sending a reply to a previous
1994 // activity, we can't do anything with it now until
1995 // we reach the start of the reply chain.
1996 // XXX note that we are assuming the result is always
1997 // to the previous activity, which is almost always
1998 // the case but we really shouldn't count on.
1999 if (replyChainEnd < 0) {
2000 replyChainEnd = i;
2001 }
2002 } else if (topTaskIsHigher
2003 && allowTaskReparenting
2004 && taskAffinity != null
2005 && taskAffinity.equals(target.taskAffinity)) {
2006 // This activity has an affinity for our task. Either remove it if we are
2007 // clearing or move it over to our task. Note that
2008 // we currently punt on the case where we are resetting a
2009 // task that is not at the top but who has activities above
2010 // with an affinity to it... this is really not a normal
2011 // case, and we will need to later pull that task to the front
2012 // and usually at that point we will do the reset and pick
2013 // up those remaining activities. (This only happens if
2014 // someone starts an activity in a new task from an activity
2015 // in a task that is not currently on top.)
2016 if (forceReset || finishOnTaskLaunch) {
2017 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2018 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2019 for (int srcPos = start; srcPos >= i; --srcPos) {
2020 final ActivityRecord p = activities.get(srcPos);
2021 if (p.finishing) {
2022 continue;
2023 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002024 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002025 }
2026 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002027 if (taskInsertionPoint < 0) {
2028 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002029
Craig Mautner77878772013-03-04 19:46:24 -08002030 }
Craig Mautner77878772013-03-04 19:46:24 -08002031
2032 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2033 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2034 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2035 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002036 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002037 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002038 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002039
Craig Mautnere3a74d52013-02-22 14:14:58 -08002040 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2041 + " to stack at " + task,
2042 new RuntimeException("here").fillInStackTrace());
2043 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2044 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002045 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002046 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002047 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002048 if (VALIDATE_TOKENS) {
2049 validateAppTokensLocked();
2050 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002051
2052 // Now we've moved it in to place... but what if this is
2053 // a singleTop activity and we have put it on top of another
2054 // instance of the same activity? Then we drop the instance
2055 // below so it remains singleTop.
2056 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2057 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002058 int targetNdx = taskActivities.indexOf(target);
2059 if (targetNdx > 0) {
2060 ActivityRecord p = taskActivities.get(targetNdx - 1);
2061 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002062 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2063 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002064 }
2065 }
2066 }
2067 }
2068
2069 replyChainEnd = -1;
2070 }
2071 }
Craig Mautner77878772013-03-04 19:46:24 -08002072 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002073 }
2074
Craig Mautner8849a5e2013-04-02 16:41:03 -07002075 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002076 ActivityRecord newActivity) {
2077 boolean forceReset =
2078 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2079 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2080 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2081 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2082 forceReset = true;
2083 }
2084 }
2085
2086 final TaskRecord task = taskTop.task;
2087
2088 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2089 * for remaining tasks. Used for later tasks to reparent to task. */
2090 boolean taskFound = false;
2091
2092 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2093 ActivityOptions topOptions = null;
2094
Craig Mautner77878772013-03-04 19:46:24 -08002095 // Preserve the location for reparenting in the new task.
2096 int reparentInsertionPoint = -1;
2097
Craig Mautnere3a74d52013-02-22 14:14:58 -08002098 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2099 final TaskRecord targetTask = mTaskHistory.get(i);
2100
2101 if (targetTask == task) {
2102 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2103 taskFound = true;
2104 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002105 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2106 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002107 }
2108 }
2109
Craig Mautner70a86932013-02-28 22:37:44 -08002110 int taskNdx = mTaskHistory.indexOf(task);
2111 do {
2112 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2113 } while (taskTop == null && taskNdx >= 0);
2114
Craig Mautnere3a74d52013-02-22 14:14:58 -08002115 if (topOptions != null) {
2116 // If we got some ActivityOptions from an activity on top that
2117 // was removed from the task, propagate them to the new real top.
2118 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002119 taskTop.updateOptionsLocked(topOptions);
2120 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002121 topOptions.abort();
2122 }
2123 }
2124
2125 return taskTop;
2126 }
2127
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002128 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2129 String resultWho, int requestCode, int resultCode, Intent data) {
2130
2131 if (callingUid > 0) {
2132 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002133 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002134 }
2135
2136 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2137 + " : who=" + resultWho + " req=" + requestCode
2138 + " res=" + resultCode + " data=" + data);
2139 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2140 try {
2141 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2142 list.add(new ResultInfo(resultWho, requestCode,
2143 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002144 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002145 return;
2146 } catch (Exception e) {
2147 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2148 }
2149 }
2150
2151 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2152 }
2153
Craig Mautnerf3333272013-04-22 10:55:53 -07002154 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002155 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2156 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2157 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2158 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002159 if (!mService.mSleeping) {
2160 if (DEBUG_STATES) {
2161 Slog.d(TAG, "no-history finish of " + r);
2162 }
2163 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002164 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002165 } else {
2166 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2167 + " on stop because we're just sleeping");
2168 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002169 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002170 }
2171
2172 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002173 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002174 if (mService.mFocusedActivity == r) {
2175 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2176 }
2177 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002178 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002179 try {
2180 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002181 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2182 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002183 r.state = ActivityState.STOPPING;
2184 if (DEBUG_VISBILITY) Slog.v(
2185 TAG, "Stopping visible=" + r.visible + " for " + r);
2186 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002187 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002188 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002189 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002190 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002191 r.setSleeping(true);
2192 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002193 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002194 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002195 } catch (Exception e) {
2196 // Maybe just ignore exceptions here... if the process
2197 // has crashed, our death notification will clean things
2198 // up.
2199 Slog.w(TAG, "Exception thrown during pause", e);
2200 // Just in case, assume it to be stopped.
2201 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002202 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002203 r.state = ActivityState.STOPPED;
2204 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002205 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002206 }
2207 }
2208 }
2209 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002210
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002211 /**
2212 * @return Returns true if the activity is being finished, false if for
2213 * some reason it is being left as-is.
2214 */
2215 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002216 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002217 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002218 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002219 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002220 + ", result=" + resultCode + ", data=" + resultData
2221 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002222 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002223 return false;
2224 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002225
Craig Mautnerd44711d2013-02-23 11:24:36 -08002226 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002227 return true;
2228 }
2229
Craig Mautnerd2328952013-03-05 12:46:26 -08002230 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002231 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2232 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2233 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2234 ActivityRecord r = activities.get(activityNdx);
2235 if (r.resultTo == self && r.requestCode == requestCode) {
2236 if ((r.resultWho == null && resultWho == null) ||
2237 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2238 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2239 false);
2240 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002241 }
2242 }
2243 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002244 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002245 }
2246
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002247 final void finishTopRunningActivityLocked(ProcessRecord app) {
2248 ActivityRecord r = topRunningActivityLocked(null);
2249 if (r != null && r.app == app) {
2250 // If the top running activity is from this crashing
2251 // process, then terminate it to avoid getting in a loop.
2252 Slog.w(TAG, " Force finishing activity "
2253 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002254 int taskNdx = mTaskHistory.indexOf(r.task);
2255 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002256 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002257 // Also terminate any activities below it that aren't yet
2258 // stopped, to avoid a situation where one will get
2259 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002260 --activityNdx;
2261 if (activityNdx < 0) {
2262 do {
2263 --taskNdx;
2264 if (taskNdx < 0) {
2265 break;
2266 }
2267 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2268 } while (activityNdx < 0);
2269 }
2270 if (activityNdx >= 0) {
2271 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002272 if (r.state == ActivityState.RESUMED
2273 || r.state == ActivityState.PAUSING
2274 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002275 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002276 Slog.w(TAG, " Force finishing activity "
2277 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002278 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002279 }
2280 }
2281 }
2282 }
2283 }
2284
Craig Mautnerd2328952013-03-05 12:46:26 -08002285 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002286 ArrayList<ActivityRecord> activities = r.task.mActivities;
2287 for (int index = activities.indexOf(r); index >= 0; --index) {
2288 ActivityRecord cur = activities.get(index);
2289 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002290 break;
2291 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002292 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002293 }
2294 return true;
2295 }
2296
Dianne Hackborn5c607432012-02-28 14:44:19 -08002297 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2298 // send the result
2299 ActivityRecord resultTo = r.resultTo;
2300 if (resultTo != null) {
2301 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2302 + " who=" + r.resultWho + " req=" + r.requestCode
2303 + " res=" + resultCode + " data=" + resultData);
2304 if (r.info.applicationInfo.uid > 0) {
2305 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2306 resultTo.packageName, resultData,
2307 resultTo.getUriPermissionsLocked());
2308 }
2309 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2310 resultData);
2311 r.resultTo = null;
2312 }
2313 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2314
2315 // Make sure this HistoryRecord is not holding on to other resources,
2316 // because clients have remote IPC references to this object so we
2317 // can't assume that will go away and want to avoid circular IPC refs.
2318 r.results = null;
2319 r.pendingResults = null;
2320 r.newIntents = null;
2321 r.icicle = null;
2322 }
2323
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002324 /**
2325 * @return Returns true if this activity has been removed from the history
2326 * list, or false if it is still in the list and will be removed later.
2327 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002328 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2329 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002330 if (r.finishing) {
2331 Slog.w(TAG, "Duplicate finish request for " + r);
2332 return false;
2333 }
2334
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002335 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002336 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002337 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002338 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002339 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2340 final int index = activities.indexOf(r);
2341 if (index < (activities.size() - 1)) {
2342 ActivityRecord next = activities.get(index+1);
2343 if (r.frontOfTask) {
2344 // The next activity is now the front of the task.
2345 next.frontOfTask = true;
2346 }
2347 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2348 // If the caller asked that this activity (and all above it)
2349 // be cleared when the task is reset, don't lose that information,
2350 // but propagate it up to the next activity.
2351 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002352 }
2353 }
2354
2355 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002356 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002357 if (mService.mFocusedActivity == r) {
Craig Mautner29219d92013-04-16 20:19:12 -07002358 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002359 }
2360 }
2361
Dianne Hackborn5c607432012-02-28 14:44:19 -08002362 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002363
Craig Mautnerc8143c62013-09-03 12:15:57 -07002364 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002365 // There are clients waiting to receive thumbnails so, in case
2366 // this is an activity that someone is waiting for, add it
2367 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002368 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002369 }
2370
Craig Mautnerde4ef022013-04-07 19:01:33 -07002371 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002372 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002373 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002374 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002375 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002376 ? AppTransition.TRANSIT_TASK_CLOSE
2377 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002378
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002379 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002380 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002381
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002382 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002383 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2384 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2385 startPausingLocked(false, false);
2386 }
2387
2388 } else if (r.state != ActivityState.PAUSING) {
2389 // If the activity is PAUSING, we will complete the finish once
2390 // it is done pausing; else we can just directly finish it here.
2391 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002392 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002393 } else {
2394 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2395 }
2396
2397 return false;
2398 }
2399
Craig Mautnerf3333272013-04-22 10:55:53 -07002400 static final int FINISH_IMMEDIATELY = 0;
2401 static final int FINISH_AFTER_PAUSE = 1;
2402 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002403
Craig Mautnerf3333272013-04-22 10:55:53 -07002404 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002405 // First things first: if this activity is currently visible,
2406 // and the resumed activity is not yet visible, then hold off on
2407 // finishing until the resumed one becomes visible.
2408 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002409 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2410 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002411 if (mStackSupervisor.mStoppingActivities.size() > 3
2412 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002413 // If we already have a few activities waiting to stop,
2414 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002415 // them out. Or if r is the last of activity of the last task the stack
2416 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002417 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002418 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002419 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002420 }
2421 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002422 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2423 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002424 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002425 if (oomAdj) {
2426 mService.updateOomAdjLocked();
2427 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002428 return r;
2429 }
2430
2431 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002432 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002433 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002434 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002435 if (mResumedActivity == r) {
2436 mResumedActivity = null;
2437 }
2438 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002439 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002440 r.state = ActivityState.FINISHING;
2441
2442 if (mode == FINISH_IMMEDIATELY
2443 || prevState == ActivityState.STOPPED
2444 || prevState == ActivityState.INITIALIZING) {
2445 // If this activity is already stopped, we can just finish
2446 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002447 boolean activityRemoved = destroyActivityLocked(r, true,
2448 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002449 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002450 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002451 }
2452 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002453 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002454
2455 // Need to go through the full pause cycle to get this
2456 // activity into the stopped state and then finish it.
2457 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002458 mStackSupervisor.mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002459 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002460 return r;
2461 }
2462
Craig Mautnerd2328952013-03-05 12:46:26 -08002463 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002464 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002465 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002466 final TaskRecord task = srec.task;
2467 final ArrayList<ActivityRecord> activities = task.mActivities;
2468 final int start = activities.indexOf(srec);
2469 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002470 return false;
2471 }
2472 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002473 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002474 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002475 final ComponentName dest = destIntent.getComponent();
2476 if (start > 0 && dest != null) {
2477 for (int i = finishTo; i >= 0; i--) {
2478 ActivityRecord r = activities.get(i);
2479 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002480 r.info.name.equals(dest.getClassName())) {
2481 finishTo = i;
2482 parent = r;
2483 foundParentInTask = true;
2484 break;
2485 }
2486 }
2487 }
2488
2489 IActivityController controller = mService.mController;
2490 if (controller != null) {
2491 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2492 if (next != null) {
2493 // ask watcher if this is allowed
2494 boolean resumeOK = true;
2495 try {
2496 resumeOK = controller.activityResuming(next.packageName);
2497 } catch (RemoteException e) {
2498 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002499 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002500 }
2501
2502 if (!resumeOK) {
2503 return false;
2504 }
2505 }
2506 }
2507 final long origId = Binder.clearCallingIdentity();
2508 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002509 ActivityRecord r = activities.get(i);
2510 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002511 // Only return the supplied result for the first activity finished
2512 resultCode = Activity.RESULT_CANCELED;
2513 resultData = null;
2514 }
2515
2516 if (parent != null && foundParentInTask) {
2517 final int parentLaunchMode = parent.info.launchMode;
2518 final int destIntentFlags = destIntent.getFlags();
2519 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2520 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2521 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2522 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2523 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2524 } else {
2525 try {
2526 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2527 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002528 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002529 null, aInfo, parent.appToken, null,
2530 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2531 0, null, true, null);
2532 foundParentInTask = res == ActivityManager.START_SUCCESS;
2533 } catch (RemoteException e) {
2534 foundParentInTask = false;
2535 }
2536 requestFinishActivityLocked(parent.appToken, resultCode,
2537 resultData, "navigate-up", true);
2538 }
2539 }
2540 Binder.restoreCallingIdentity(origId);
2541 return foundParentInTask;
2542 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002543 /**
2544 * Perform the common clean-up of an activity record. This is called both
2545 * as part of destroyActivityLocked() (when destroying the client-side
2546 * representation) and cleaning things up as a result of its hosting
2547 * processing going away, in which case there is no remaining client-side
2548 * state to destroy so only the cleanup here is needed.
2549 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002550 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2551 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002552 if (mResumedActivity == r) {
2553 mResumedActivity = null;
2554 }
2555 if (mService.mFocusedActivity == r) {
2556 mService.mFocusedActivity = null;
2557 }
2558
2559 r.configDestroy = false;
2560 r.frozenBeforeDestroy = false;
2561
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002562 if (setState) {
2563 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2564 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002565 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002566 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002567 }
2568
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002569 // Make sure this record is no longer in the pending finishes list.
2570 // This could happen, for example, if we are trimming activities
2571 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002572 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002573 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002574
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002575 // Remove any pending results.
2576 if (r.finishing && r.pendingResults != null) {
2577 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2578 PendingIntentRecord rec = apr.get();
2579 if (rec != null) {
2580 mService.cancelIntentSenderLocked(rec, false);
2581 }
2582 }
2583 r.pendingResults = null;
2584 }
2585
2586 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002587 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002588 }
2589
Craig Mautnerc8143c62013-09-03 12:15:57 -07002590 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002591 // There are clients waiting to receive thumbnails so, in case
2592 // this is an activity that someone is waiting for, add it
2593 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002594 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002595 }
2596
2597 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002598 removeTimeoutsForActivityLocked(r);
2599 }
2600
2601 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002602 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002603 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002604 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002605 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002606 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002607 }
2608
Dianne Hackborn5c607432012-02-28 14:44:19 -08002609 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002610 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2611 r.makeFinishing();
2612 if (DEBUG_ADD_REMOVE) {
2613 RuntimeException here = new RuntimeException("here");
2614 here.fillInStackTrace();
2615 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002616 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002617 final TaskRecord task = r.task;
2618 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002619 if (DEBUG_STACK) Slog.i(TAG,
2620 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002621 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002622 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002623 r.takeFromHistory();
2624 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002625 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002626 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002627 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002628 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002629 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002630 if (VALIDATE_TOKENS) {
2631 validateAppTokensLocked();
2632 }
2633 cleanUpActivityServicesLocked(r);
2634 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002635 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002636
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002637 /**
2638 * Perform clean-up of service connections in an activity record.
2639 */
2640 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2641 // Throw away any services that have been bound by this activity.
2642 if (r.connections != null) {
2643 Iterator<ConnectionRecord> it = r.connections.iterator();
2644 while (it.hasNext()) {
2645 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002646 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002647 }
2648 r.connections = null;
2649 }
2650 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002651
2652 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2653 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2654 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2655 mHandler.sendMessage(msg);
2656 }
2657
Dianne Hackborn28695e02011-11-02 21:59:51 -07002658 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002659 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002660 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002661 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2662 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2663 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2664 final ActivityRecord r = activities.get(activityNdx);
2665 if (r.finishing) {
2666 continue;
2667 }
2668 if (r.fullscreen) {
2669 lastIsOpaque = true;
2670 }
2671 if (owner != null && r.app != owner) {
2672 continue;
2673 }
2674 if (!lastIsOpaque) {
2675 continue;
2676 }
2677 // We can destroy this one if we have its icicle saved and
2678 // it is not in the process of pausing/stopping/finishing.
2679 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2680 && r.haveState && !r.visible && r.stopped
2681 && r.state != ActivityState.DESTROYING
2682 && r.state != ActivityState.DESTROYED) {
2683 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2684 + " resumed=" + mResumedActivity
2685 + " pausing=" + mPausingActivity);
2686 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2687 activityRemoved = true;
2688 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002689 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002690 }
2691 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002692 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002693 mStackSupervisor.resumeTopActivitiesLocked();
2694
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002695 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002696 }
2697
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002698 /**
2699 * Destroy the current CLIENT SIDE instance of an activity. This may be
2700 * called both when actually finishing an activity, or when performing
2701 * a configuration switch where we destroy the current client-side object
2702 * but then create a new client-side object for this same HistoryRecord.
2703 */
2704 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002705 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002706 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002707 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002708 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2709 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002710 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002711 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002712
2713 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002714
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002715 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002716
2717 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002718
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002719 if (hadApp) {
2720 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002721 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002722 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2723 mService.mHeavyWeightProcess = null;
2724 mService.mHandler.sendEmptyMessage(
2725 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2726 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07002727 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07002728 // No longer have activities, so update LRU list and oom adj.
2729 mService.updateLruProcessLocked(r.app, false, false);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002730 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002731 }
2732 }
2733
2734 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002735
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002736 try {
2737 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002738 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002739 r.configChangeFlags);
2740 } catch (Exception e) {
2741 // We can just ignore exceptions here... if the process
2742 // has crashed, our death notification will clean things
2743 // up.
2744 //Slog.w(TAG, "Exception thrown during finish", e);
2745 if (r.finishing) {
2746 removeActivityFromHistoryLocked(r);
2747 removedFromHistory = true;
2748 skipDestroy = true;
2749 }
2750 }
2751
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002752 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002753
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002754 // If the activity is finishing, we need to wait on removing it
2755 // from the list to give it a chance to do its cleanup. During
2756 // that time it may make calls back with its token so we need to
2757 // be able to find it on the list and so we don't want to remove
2758 // it from the list yet. Otherwise, we can just immediately put
2759 // it in the destroyed state since we are not removing it from the
2760 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002761 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002762 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2763 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002764 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002765 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002766 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2767 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002768 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002769 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002770 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002771 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002772 }
2773 } else {
2774 // remove this record from the history.
2775 if (r.finishing) {
2776 removeActivityFromHistoryLocked(r);
2777 removedFromHistory = true;
2778 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002779 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002780 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002781 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002782 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002783 }
2784 }
2785
2786 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002787
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002788 if (!mLRUActivities.remove(r) && hadApp) {
2789 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2790 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002791
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002792 return removedFromHistory;
2793 }
2794
Craig Mautnerd2328952013-03-05 12:46:26 -08002795 final void activityDestroyedLocked(IBinder token) {
2796 final long origId = Binder.clearCallingIdentity();
2797 try {
2798 ActivityRecord r = ActivityRecord.forToken(token);
2799 if (r != null) {
2800 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002801 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002802
2803 if (isInStackLocked(token) != null) {
2804 if (r.state == ActivityState.DESTROYING) {
2805 cleanUpActivityLocked(r, true, false);
2806 removeActivityFromHistoryLocked(r);
2807 }
2808 }
Craig Mautner05d29032013-05-03 13:40:13 -07002809 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002810 } finally {
2811 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002812 }
2813 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002814
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002815 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2816 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002817 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002818 if (DEBUG_CLEANUP) Slog.v(
2819 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002820 + " with " + i + " entries");
2821 while (i > 0) {
2822 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002823 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002824 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002825 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002826 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002827 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002828 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002829 }
2830 }
2831 }
2832
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002833 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2834 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002835 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2836 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002837 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2838 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002839 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002840 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002841 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2842 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002843
2844 boolean hasVisibleActivities = false;
2845
2846 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002847 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002848 if (DEBUG_CLEANUP) Slog.v(
2849 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002850 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2851 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2852 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2853 final ActivityRecord r = activities.get(activityNdx);
2854 --i;
2855 if (DEBUG_CLEANUP) Slog.v(
2856 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2857 if (r.app == app) {
2858 boolean remove;
2859 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2860 // Don't currently have state for the activity, or
2861 // it is finishing -- always remove it.
2862 remove = true;
2863 } else if (r.launchCount > 2 &&
2864 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2865 // We have launched this activity too many times since it was
2866 // able to run, so give up and remove it.
2867 remove = true;
2868 } else {
2869 // The process may be gone, but the activity lives on!
2870 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002871 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002872 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002873 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002874 RuntimeException here = new RuntimeException("here");
2875 here.fillInStackTrace();
2876 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2877 + ": haveState=" + r.haveState
2878 + " stateNotNeeded=" + r.stateNotNeeded
2879 + " finishing=" + r.finishing
2880 + " state=" + r.state, here);
2881 }
2882 if (!r.finishing) {
2883 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2884 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2885 r.userId, System.identityHashCode(r),
2886 r.task.taskId, r.shortComponentName,
2887 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07002888 if (r.state == ActivityState.RESUMED) {
2889 mService.updateUsageStats(r, false);
2890 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002891 }
2892 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002893
Craig Mautner0247fc82013-02-28 14:32:06 -08002894 } else {
2895 // We have the current state for this activity, so
2896 // it can be restarted later when needed.
2897 if (localLOGV) Slog.v(
2898 TAG, "Keeping entry, setting app to null");
2899 if (r.visible) {
2900 hasVisibleActivities = true;
2901 }
2902 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2903 + r);
2904 r.app = null;
2905 r.nowVisible = false;
2906 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002907 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08002908 "App died, clearing saved state of " + r);
2909 r.icicle = null;
2910 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002911 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002912
Craig Mautnerd2328952013-03-05 12:46:26 -08002913 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002914 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002915 }
2916 }
2917
2918 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002919 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002920
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002921 final void updateTransitLocked(int transit, Bundle options) {
2922 if (options != null) {
2923 ActivityRecord r = topRunningActivityLocked(null);
2924 if (r != null && r.state != ActivityState.RESUMED) {
2925 r.updateOptionsLocked(options);
2926 } else {
2927 ActivityOptions.abort(options);
2928 }
2929 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002930 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002931 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002932
Craig Mautnera82aa092013-09-13 15:34:08 -07002933 void moveHomeTaskToTop() {
2934 final int top = mTaskHistory.size() - 1;
2935 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
2936 final TaskRecord task = mTaskHistory.get(taskNdx);
2937 if (task.isHomeTask()) {
2938 mTaskHistory.remove(taskNdx);
2939 mTaskHistory.add(top, task);
2940 mWindowManager.moveTaskToTop(task.taskId);
2941 return;
2942 }
2943 }
2944 }
2945
Craig Mautnercae015f2013-02-08 14:31:27 -08002946 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002947 final TaskRecord task = taskForIdLocked(taskId);
2948 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08002949 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002950 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002951 }
Craig Mautneraab647e2013-02-28 16:31:36 -08002952 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2953 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07002954 // we'll just indicate that this task returns to the home task.
Craig Mautnerae7ecab2013-09-18 11:48:14 -07002955 task.mOnTopOfHome = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08002956 }
2957 moveTaskToFrontLocked(task, null, options);
2958 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002959 }
2960 return false;
2961 }
2962
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002963 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002964 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002965
Craig Mautner11bf9a52013-02-19 14:08:51 -08002966 final int numTasks = mTaskHistory.size();
2967 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07002968 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002969 // nothing to do!
2970 if (reason != null &&
2971 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2972 ActivityOptions.abort(options);
2973 } else {
2974 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
2975 }
2976 return;
2977 }
2978
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07002979 mStackSupervisor.moveHomeStack(isHomeStack());
2980
Craig Mautner11bf9a52013-02-19 14:08:51 -08002981 // Shift all activities with this task up to the top
2982 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07002983 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08002984
2985 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002986 if (reason != null &&
2987 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002988 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002989 ActivityRecord r = topRunningActivityLocked(null);
2990 if (r != null) {
2991 mNoAnimActivities.add(r);
2992 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002993 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002994 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08002995 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002996 }
Craig Mautner30e2d722013-02-12 11:30:16 -08002997
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002998 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002999
Craig Mautner05d29032013-05-03 13:40:13 -07003000 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003001 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003002
3003 if (VALIDATE_TOKENS) {
3004 validateAppTokensLocked();
3005 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003006 }
3007
3008 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003009 * Worker method for rearranging history stack. Implements the function of moving all
3010 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003011 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003012 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003013 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3014 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003015 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003016 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003017 * @return Returns true if the move completed, false if not.
3018 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003019 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
3020 Slog.i(TAG, "moveTaskToBack: " + taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003021
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003022 // If we have a watcher, preflight the move before committing to it. First check
3023 // for *other* available tasks, but if none are available, then try again allowing the
3024 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003025 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003026 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003027 if (next == null) {
3028 next = topRunningActivityLocked(null, 0);
3029 }
3030 if (next != null) {
3031 // ask watcher if this is allowed
3032 boolean moveOK = true;
3033 try {
3034 moveOK = mService.mController.activityResuming(next.packageName);
3035 } catch (RemoteException e) {
3036 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003037 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003038 }
3039 if (!moveOK) {
3040 return false;
3041 }
3042 }
3043 }
3044
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003045 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003046 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003047
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003048 final TaskRecord tr = taskForIdLocked(taskId);
Craig Mautnerd2328952013-03-05 12:46:26 -08003049 if (tr == null) {
3050 return false;
3051 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003052
Craig Mautner11bf9a52013-02-19 14:08:51 -08003053 mTaskHistory.remove(tr);
3054 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003055
Craig Mautnerc8143c62013-09-03 12:15:57 -07003056 // There is an assumption that moving a task to the back moves it behind the home activity.
3057 // We make sure here that some activity in the stack will launch home.
3058 ActivityRecord lastActivity = null;
3059 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003060 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3061 final TaskRecord task = mTaskHistory.get(taskNdx);
3062 if (task.mOnTopOfHome) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003063 break;
3064 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003065 if (taskNdx == 1) {
3066 // Set the last task before tr to go to home.
3067 task.mOnTopOfHome = true;
3068 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003069 }
3070
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003071 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003072 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3073 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003074 ActivityRecord r = topRunningActivityLocked(null);
3075 if (r != null) {
3076 mNoAnimActivities.add(r);
3077 }
3078 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003079 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003080 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003081 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003082
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003083 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003084 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003085 }
3086
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003087 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
3088 if (task == tr && task.mOnTopOfHome || numTasks <= 1) {
3089 task.mOnTopOfHome = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003090 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003091 }
3092
Craig Mautner05d29032013-05-03 13:40:13 -07003093 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003094 return true;
3095 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003096
Craig Mautner8849a5e2013-04-02 16:41:03 -07003097 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003098 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003099 final Uri data = r.intent.getData();
3100 final String strData = data != null ? data.toSafeString() : null;
3101
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003102 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003103 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003104 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003105 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003106 }
3107
3108 /**
3109 * Make sure the given activity matches the current configuration. Returns
3110 * false if the activity had to be destroyed. Returns true if the
3111 * configuration is the same, or the activity will remain running as-is
3112 * for whatever reason. Ensures the HistoryRecord is updated with the
3113 * correct configuration and all other bookkeeping is handled.
3114 */
3115 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3116 int globalChanges) {
3117 if (mConfigWillChange) {
3118 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3119 "Skipping config check (will change): " + r);
3120 return true;
3121 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003122
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003123 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3124 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003125
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003126 // Short circuit: if the two configurations are the exact same
3127 // object (the common case), then there is nothing to do.
3128 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003129 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003130 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3131 "Configuration unchanged in " + r);
3132 return true;
3133 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003134
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003135 // We don't worry about activities that are finishing.
3136 if (r.finishing) {
3137 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3138 "Configuration doesn't matter in finishing " + r);
3139 r.stopFreezingScreenLocked(false);
3140 return true;
3141 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003142
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003143 // Okay we now are going to make this activity have the new config.
3144 // But then we need to figure out how it needs to deal with that.
3145 Configuration oldConfig = r.configuration;
3146 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003147
3148 // Determine what has changed. May be nothing, if this is a config
3149 // that has come back from the app after going idle. In that case
3150 // we just want to leave the official config object now in the
3151 // activity and do nothing else.
3152 final int changes = oldConfig.diff(newConfig);
3153 if (changes == 0 && !r.forceNewConfig) {
3154 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3155 "Configuration no differences in " + r);
3156 return true;
3157 }
3158
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003159 // If the activity isn't currently running, just leave the new
3160 // configuration and it will pick that up next time it starts.
3161 if (r.app == null || r.app.thread == null) {
3162 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3163 "Configuration doesn't matter not running " + r);
3164 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003165 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003166 return true;
3167 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003168
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003169 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003170 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3171 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3172 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003173 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003174 + ", newConfig=" + newConfig);
3175 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003176 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003177 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3178 r.configChangeFlags |= changes;
3179 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003180 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003181 if (r.app == null || r.app.thread == null) {
3182 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003183 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003184 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003185 } else if (r.state == ActivityState.PAUSING) {
3186 // A little annoying: we are waiting for this activity to
3187 // finish pausing. Let's not do anything now, but just
3188 // flag that it needs to be restarted when done pausing.
3189 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003190 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003191 r.configDestroy = true;
3192 return true;
3193 } else if (r.state == ActivityState.RESUMED) {
3194 // Try to optimize this case: the configuration is changing
3195 // and we need to restart the top, resumed activity.
3196 // Instead of doing the normal handshaking, just say
3197 // "restart!".
3198 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003199 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003200 relaunchActivityLocked(r, r.configChangeFlags, true);
3201 r.configChangeFlags = 0;
3202 } else {
3203 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003204 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003205 relaunchActivityLocked(r, r.configChangeFlags, false);
3206 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003207 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003208
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003209 // All done... tell the caller we weren't able to keep this
3210 // activity around.
3211 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003212 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003213
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003214 // Default case: the activity can handle this new configuration, so
3215 // hand it over. Note that we don't need to give it the new
3216 // configuration, since we always send configuration changes to all
3217 // process when they happen so it can just use whatever configuration
3218 // it last got.
3219 if (r.app != null && r.app.thread != null) {
3220 try {
3221 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003222 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003223 } catch (RemoteException e) {
3224 // If process died, whatever.
3225 }
3226 }
3227 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003228
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003229 return true;
3230 }
3231
Craig Mautnerc8143c62013-09-03 12:15:57 -07003232 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003233 int changes, boolean andResume) {
3234 List<ResultInfo> results = null;
3235 List<Intent> newIntents = null;
3236 if (andResume) {
3237 results = r.results;
3238 newIntents = r.newIntents;
3239 }
3240 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3241 + " with results=" + results + " newIntents=" + newIntents
3242 + " andResume=" + andResume);
3243 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003244 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003245 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003246
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003247 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003248
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003249 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003250 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3251 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3252 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003253 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003254 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003255 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003256 // Note: don't need to call pauseIfSleepingLocked() here, because
3257 // the caller will only pass in 'andResume' if this activity is
3258 // currently resumed, which implies we aren't sleeping.
3259 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003260 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003261 }
3262
3263 if (andResume) {
3264 r.results = null;
3265 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003266 r.state = ActivityState.RESUMED;
3267 } else {
3268 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3269 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003270 }
3271
3272 return true;
3273 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003274
3275 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003276 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3277 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3278 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3279 final ActivityRecord r = activities.get(activityNdx);
3280 if (r.appToken == token) {
3281 return true;
3282 }
3283 if (r.fullscreen && !r.finishing) {
3284 return false;
3285 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003286 }
3287 }
3288 return true;
3289 }
3290
3291 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003292 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3293 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3294 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3295 final ActivityRecord r = activities.get(activityNdx);
3296 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003297 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003298 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003299 }
3300 }
3301 }
3302
3303 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3304 boolean didSomething = false;
3305 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003306 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3307 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3308 int numActivities = activities.size();
3309 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3310 ActivityRecord r = activities.get(activityNdx);
3311 final boolean samePackage = r.packageName.equals(name)
3312 || (name == null && r.userId == userId);
3313 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3314 && (samePackage || r.task == lastTask)
3315 && (r.app == null || evenPersistent || !r.app.persistent)) {
3316 if (!doit) {
3317 if (r.finishing) {
3318 // If this activity is just finishing, then it is not
3319 // interesting as far as something to stop.
3320 continue;
3321 }
3322 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003323 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003324 didSomething = true;
3325 Slog.i(TAG, " Force finishing activity " + r);
3326 if (samePackage) {
3327 if (r.app != null) {
3328 r.app.removed = true;
3329 }
3330 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003331 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003332 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003333 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3334 true)) {
3335 // r has been deleted from mActivities, accommodate.
3336 --numActivities;
3337 --activityNdx;
3338 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003339 }
3340 }
3341 }
3342 return didSomething;
3343 }
3344
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003345 ActivityRecord getTasksLocked(IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003346 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003347 ActivityRecord topRecord = null;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003348 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003349 final TaskRecord task = mTaskHistory.get(taskNdx);
3350 ActivityRecord r = null;
3351 ActivityRecord top = null;
3352 int numActivities = 0;
3353 int numRunning = 0;
3354 final ArrayList<ActivityRecord> activities = task.mActivities;
3355 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3356 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003357
Craig Mautneraab647e2013-02-28 16:31:36 -08003358 // Initialize state for next task if needed.
3359 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3360 top = r;
3361 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003362 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003363
3364 // Add 'r' into the current task.
3365 numActivities++;
3366 if (r.app != null && r.app.thread != null) {
3367 numRunning++;
3368 }
3369
3370 if (localLOGV) Slog.v(
3371 TAG, r.intent.getComponent().flattenToShortString()
3372 + ": task=" + r.task);
3373 }
3374
3375 RunningTaskInfo ci = new RunningTaskInfo();
3376 ci.id = task.taskId;
3377 ci.baseActivity = r.intent.getComponent();
3378 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003379 ci.lastActiveTime = task.lastActiveTime;
3380
Craig Mautneraab647e2013-02-28 16:31:36 -08003381 if (top.thumbHolder != null) {
3382 ci.description = top.thumbHolder.lastDescription;
3383 }
3384 ci.numActivities = numActivities;
3385 ci.numRunning = numRunning;
3386 //System.out.println(
3387 // "#" + maxNum + ": " + " descr=" + ci.description);
3388 if (receiver != null) {
3389 if (localLOGV) Slog.v(
3390 TAG, "State=" + top.state + "Idle=" + top.idle
3391 + " app=" + top.app
3392 + " thr=" + (top.app != null ? top.app.thread : null));
3393 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3394 if (top.idle && top.app != null && top.app.thread != null) {
3395 topRecord = top;
3396 } else {
3397 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003398 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003399 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003400 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003401 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003402 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003403 }
3404 return topRecord;
3405 }
3406
3407 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003408 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003409 if (DEBUG_SWITCH) Slog.d(
3410 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003411 if (top >= 0) {
3412 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3413 int activityTop = activities.size() - 1;
3414 if (activityTop > 0) {
3415 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3416 "unhandled-back", true);
3417 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003418 }
3419 }
3420
Craig Mautner6b74cb52013-09-27 17:02:21 -07003421 /**
3422 * Reset local parameters because an app's activity died.
3423 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003424 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003425 */
3426 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003427 if (mPausingActivity != null && mPausingActivity.app == app) {
3428 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3429 "App died while pausing: " + mPausingActivity);
3430 mPausingActivity = null;
3431 }
3432 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3433 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003434 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003435 }
3436
Craig Mautner19091252013-10-05 00:03:53 -07003437 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003438 }
3439
Craig Mautnercae015f2013-02-08 14:31:27 -08003440 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003441 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3442 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3443 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3444 final ActivityRecord r = activities.get(activityNdx);
3445 if (r.app == app) {
3446 Slog.w(TAG, " Force finishing activity "
3447 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003448 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003449 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003450 }
3451 }
3452 }
3453
Dianne Hackborn390517b2013-05-30 15:03:32 -07003454 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003455 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003456 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003457 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3458 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003459 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3460 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003461 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003462 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003463 if (printed) {
3464 header = null;
3465 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003466 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003467 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003468 }
3469
3470 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3471 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3472
3473 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003474 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3475 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003476 }
3477 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003478 final int top = mTaskHistory.size() - 1;
3479 if (top >= 0) {
3480 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3481 int listTop = list.size() - 1;
3482 if (listTop >= 0) {
3483 activities.add(list.get(listTop));
3484 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003485 }
3486 } else {
3487 ItemMatcher matcher = new ItemMatcher();
3488 matcher.build(name);
3489
Craig Mautneraab647e2013-02-28 16:31:36 -08003490 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3491 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3492 if (matcher.match(r1, r1.intent.getComponent())) {
3493 activities.add(r1);
3494 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003495 }
3496 }
3497 }
3498
3499 return activities;
3500 }
3501
3502 ActivityRecord restartPackage(String packageName) {
3503 ActivityRecord starting = topRunningActivityLocked(null);
3504
3505 // All activities that came from the package must be
3506 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003507 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3508 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3509 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3510 final ActivityRecord a = activities.get(activityNdx);
3511 if (a.info.packageName.equals(packageName)) {
3512 a.forceNewConfig = true;
3513 if (starting != null && a == starting && a.visible) {
3514 a.startFreezingScreenLocked(starting.app,
3515 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3516 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003517 }
3518 }
3519 }
3520
3521 return starting;
3522 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003523
Craig Mautnerde4ef022013-04-07 19:01:33 -07003524 boolean removeTask(TaskRecord task) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003525 final int taskNdx = mTaskHistory.indexOf(task);
3526 final int topTaskNdx = mTaskHistory.size() - 1;
3527 if (task.mOnTopOfHome && taskNdx < topTaskNdx) {
3528 mTaskHistory.get(taskNdx + 1).mOnTopOfHome = true;
3529 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003530 mTaskHistory.remove(task);
Craig Mautnerc8143c62013-09-03 12:15:57 -07003531 return mTaskHistory.isEmpty();
Craig Mautner0247fc82013-02-28 14:32:06 -08003532 }
3533
Craig Mautnerde4ef022013-04-07 19:01:33 -07003534 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003535 TaskRecord task = new TaskRecord(taskId, info, intent);
3536 addTask(task, toTop);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003537 return task;
3538 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003539
3540 ArrayList<TaskRecord> getAllTasks() {
3541 return new ArrayList<TaskRecord>(mTaskHistory);
3542 }
3543
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003544 void addTask(final TaskRecord task, final boolean toTop) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003545 task.stack = this;
3546 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003547 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003548 } else {
3549 mTaskHistory.add(0, task);
3550 }
3551 }
3552
3553 public int getStackId() {
3554 return mStackId;
3555 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003556
3557 @Override
3558 public String toString() {
3559 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3560 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003561}