blob: 197aac51dd40935f2db09345cebd5b7f368b47b4 [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) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001626 // If this is being moved to the top by another activity or being launched from the home
1627 // activity, set mOnTopOfHome accordingly.
1628 final boolean fromHome = mStackSupervisor.getLastStack().isHomeStack();
1629 if (!isHomeStack() && (fromHome || topTask() != task)) {
1630 task.mOnTopOfHome = fromHome;
1631 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001632 mTaskHistory.remove(task);
1633 // Now put task at top.
1634 int stackNdx = mTaskHistory.size();
1635 if (task.userId != mCurrentUser) {
1636 // Put non-current user tasks below current user tasks.
1637 while (--stackNdx >= 0) {
1638 if (mTaskHistory.get(stackNdx).userId != mCurrentUser) {
1639 break;
1640 }
1641 }
1642 ++stackNdx;
1643 }
1644 mTaskHistory.add(stackNdx, task);
1645 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001646
Craig Mautner8849a5e2013-04-02 16:41:03 -07001647 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001648 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001649 TaskRecord rTask = r.task;
1650 final int taskId = rTask.taskId;
1651 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001652 // Last activity in task had been removed or ActivityManagerService is reusing task.
1653 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001654 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001655 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001656 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001657 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001658 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001659 if (!newTask) {
1660 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001661 boolean startIt = true;
1662 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1663 task = mTaskHistory.get(taskNdx);
1664 if (task == r.task) {
1665 // Here it is! Now, if this is not yet visible to the
1666 // user, then just add it without starting; it will
1667 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001668 if (!startIt) {
1669 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1670 + task, new RuntimeException("here").fillInStackTrace());
1671 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001672 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001673 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1674 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001675 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
1676 r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001677 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001678 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001679 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001680 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001681 return;
1682 }
1683 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001684 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001685 startIt = false;
1686 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001687 }
1688 }
1689
1690 // Place a new activity at top of stack, so it is next to interact
1691 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001692
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001693 // If we are not placing the new activity frontmost, we do not want
1694 // to deliver the onUserLeaving callback to the actual frontmost
1695 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001696 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001697 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001698 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1699 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001700 }
Craig Mautner70a86932013-02-28 22:37:44 -08001701
1702 task = r.task;
1703
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001704 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001705 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001706 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001707 task.addActivityToTop(r);
1708
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001709 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001710 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001711 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001712 // We want to show the starting preview window if we are
1713 // switching to a new task, or the next activity's process is
1714 // not currently running.
1715 boolean showStartingIcon = newTask;
1716 ProcessRecord proc = r.app;
1717 if (proc == null) {
1718 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1719 }
1720 if (proc == null || proc.thread == null) {
1721 showStartingIcon = true;
1722 }
1723 if (DEBUG_TRANSITION) Slog.v(TAG,
1724 "Prepare open transition: starting " + r);
1725 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001726 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001727 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001728 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001729 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001730 ? AppTransition.TRANSIT_TASK_OPEN
1731 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001732 mNoAnimActivities.remove(r);
1733 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001734 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001735 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001736 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001737 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001738 boolean doShow = true;
1739 if (newTask) {
1740 // Even though this activity is starting fresh, we still need
1741 // to reset it to make sure we apply affinities to move any
1742 // existing activities from other tasks in to it.
1743 // If the caller has requested that the target task be
1744 // reset, then do so.
1745 if ((r.intent.getFlags()
1746 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1747 resetTaskIfNeededLocked(r, r);
1748 doShow = topRunningNonDelayedActivityLocked(null) == r;
1749 }
1750 }
1751 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1752 // Figure out if we are transitioning from another activity that is
1753 // "has the same starting icon" as the next one. This allows the
1754 // window manager to keep the previous window it had previously
1755 // created, if it still had one.
1756 ActivityRecord prev = mResumedActivity;
1757 if (prev != null) {
1758 // We don't want to reuse the previous starting preview if:
1759 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001760 if (prev.task != r.task) {
1761 prev = null;
1762 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001763 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001764 else if (prev.nowVisible) {
1765 prev = null;
1766 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001767 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001768 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001769 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001770 mService.compatibilityInfoForPackageLocked(
1771 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001772 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001773 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001774 }
1775 } else {
1776 // If this is the first activity, don't do any fancy animations,
1777 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001778 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001779 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001780 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001781 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001782 }
1783 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001784 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001785 }
1786
1787 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001788 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001789 }
1790 }
1791
Dianne Hackbornbe707852011-11-11 14:32:10 -08001792 final void validateAppTokensLocked() {
1793 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001794 mValidateAppTokens.ensureCapacity(numActivities());
1795 final int numTasks = mTaskHistory.size();
1796 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1797 TaskRecord task = mTaskHistory.get(taskNdx);
1798 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07001799 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08001800 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001801 }
Craig Mautner000f0022013-02-26 15:04:29 -08001802 TaskGroup group = new TaskGroup();
1803 group.taskId = task.taskId;
1804 mValidateAppTokens.add(group);
1805 final int numActivities = activities.size();
1806 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1807 final ActivityRecord r = activities.get(activityNdx);
1808 group.tokens.add(r.appToken);
1809 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001810 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001811 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001812 }
1813
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001814 /**
1815 * Perform a reset of the given task, if needed as part of launching it.
1816 * Returns the new HistoryRecord at the top of the task.
1817 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001818 /**
1819 * Helper method for #resetTaskIfNeededLocked.
1820 * We are inside of the task being reset... we'll either finish this activity, push it out
1821 * for another task, or leave it as-is.
1822 * @param task The task containing the Activity (taskTop) that might be reset.
1823 * @param forceReset
1824 * @return An ActivityOptions that needs to be processed.
1825 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001826 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001827 ActivityOptions topOptions = null;
1828
1829 int replyChainEnd = -1;
1830 boolean canMoveOptions = true;
1831
1832 // We only do this for activities that are not the root of the task (since if we finish
1833 // the root, we may no longer have the task!).
1834 final ArrayList<ActivityRecord> activities = task.mActivities;
1835 final int numActivities = activities.size();
1836 for (int i = numActivities - 1; i > 0; --i ) {
1837 ActivityRecord target = activities.get(i);
1838
1839 final int flags = target.info.flags;
1840 final boolean finishOnTaskLaunch =
1841 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1842 final boolean allowTaskReparenting =
1843 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1844 final boolean clearWhenTaskReset =
1845 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1846
1847 if (!finishOnTaskLaunch
1848 && !clearWhenTaskReset
1849 && target.resultTo != null) {
1850 // If this activity is sending a reply to a previous
1851 // activity, we can't do anything with it now until
1852 // we reach the start of the reply chain.
1853 // XXX note that we are assuming the result is always
1854 // to the previous activity, which is almost always
1855 // the case but we really shouldn't count on.
1856 if (replyChainEnd < 0) {
1857 replyChainEnd = i;
1858 }
1859 } else if (!finishOnTaskLaunch
1860 && !clearWhenTaskReset
1861 && allowTaskReparenting
1862 && target.taskAffinity != null
1863 && !target.taskAffinity.equals(task.affinity)) {
1864 // If this activity has an affinity for another
1865 // task, then we need to move it out of here. We will
1866 // move it as far out of the way as possible, to the
1867 // bottom of the activity stack. This also keeps it
1868 // correctly ordered with any activities we previously
1869 // moved.
Craig Mautnerdccb7702013-09-17 15:53:34 -07001870 final ActivityRecord bottom =
1871 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
1872 mTaskHistory.get(0).mActivities.get(0) : null;
1873 if (bottom != null && target.taskAffinity != null
1874 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001875 // If the activity currently at the bottom has the
1876 // same task affinity as the one we are moving,
1877 // then merge it into the same task.
Craig Mautnerdccb7702013-09-17 15:53:34 -07001878 target.setTask(bottom.task, bottom.thumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001879 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07001880 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001881 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001882 target.setTask(createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Craig Mautner8d341ef2013-03-26 09:03:27 -07001883 null, false), null, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001884 target.task.affinityIntent = target.intent;
1885 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1886 + " out to new task " + target.task);
1887 }
1888
1889 final TaskRecord targetTask = target.task;
1890 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001891 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001892
Craig Mautner525f3d92013-05-07 14:01:50 -07001893 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001894 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1895 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07001896 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001897 if (p.finishing) {
1898 continue;
1899 }
1900
Craig Mautner525f3d92013-05-07 14:01:50 -07001901 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001902 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001903 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001904 topOptions = p.takeOptionsLocked();
1905 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001906 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001907 }
1908 }
1909 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
1910 + task + " adding to task=" + targetTask,
1911 new RuntimeException("here").fillInStackTrace());
1912 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1913 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001914 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001915 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001916
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001917 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001918 }
1919
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001920 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001921 if (VALIDATE_TOKENS) {
1922 validateAppTokensLocked();
1923 }
1924
1925 replyChainEnd = -1;
1926 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1927 // If the activity should just be removed -- either
1928 // because it asks for it, or the task should be
1929 // cleared -- then finish it and anything that is
1930 // part of its reply chain.
1931 int end;
1932 if (clearWhenTaskReset) {
1933 // In this case, we want to finish this activity
1934 // and everything above it, so be sneaky and pretend
1935 // like these are all in the reply chain.
1936 end = numActivities - 1;
1937 } else if (replyChainEnd < 0) {
1938 end = i;
1939 } else {
1940 end = replyChainEnd;
1941 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001942 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001943 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001944 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001945 if (p.finishing) {
1946 continue;
1947 }
1948 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001949 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001950 topOptions = p.takeOptionsLocked();
1951 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001952 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001953 }
1954 }
Craig Mautner58547802013-03-05 08:23:53 -08001955 if (DEBUG_TASKS) Slog.w(TAG,
1956 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001957 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001958 end--;
1959 srcPos--;
1960 }
1961 }
1962 replyChainEnd = -1;
1963 } else {
1964 // If we were in the middle of a chain, well the
1965 // activity that started it all doesn't want anything
1966 // special, so leave it all as-is.
1967 replyChainEnd = -1;
1968 }
1969 }
1970
1971 return topOptions;
1972 }
1973
1974 /**
1975 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
1976 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
1977 * @param affinityTask The task we are looking for an affinity to.
1978 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
1979 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
1980 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
1981 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001982 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08001983 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001984 int replyChainEnd = -1;
1985 final int taskId = task.taskId;
1986 final String taskAffinity = task.affinity;
1987
1988 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
1989 final int numActivities = activities.size();
1990 // Do not operate on the root Activity.
1991 for (int i = numActivities - 1; i > 0; --i) {
1992 ActivityRecord target = activities.get(i);
1993
1994 final int flags = target.info.flags;
1995 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1996 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1997
1998 if (target.resultTo != null) {
1999 // If this activity is sending a reply to a previous
2000 // activity, we can't do anything with it now until
2001 // we reach the start of the reply chain.
2002 // XXX note that we are assuming the result is always
2003 // to the previous activity, which is almost always
2004 // the case but we really shouldn't count on.
2005 if (replyChainEnd < 0) {
2006 replyChainEnd = i;
2007 }
2008 } else if (topTaskIsHigher
2009 && allowTaskReparenting
2010 && taskAffinity != null
2011 && taskAffinity.equals(target.taskAffinity)) {
2012 // This activity has an affinity for our task. Either remove it if we are
2013 // clearing or move it over to our task. Note that
2014 // we currently punt on the case where we are resetting a
2015 // task that is not at the top but who has activities above
2016 // with an affinity to it... this is really not a normal
2017 // case, and we will need to later pull that task to the front
2018 // and usually at that point we will do the reset and pick
2019 // up those remaining activities. (This only happens if
2020 // someone starts an activity in a new task from an activity
2021 // in a task that is not currently on top.)
2022 if (forceReset || finishOnTaskLaunch) {
2023 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2024 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2025 for (int srcPos = start; srcPos >= i; --srcPos) {
2026 final ActivityRecord p = activities.get(srcPos);
2027 if (p.finishing) {
2028 continue;
2029 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002030 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002031 }
2032 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002033 if (taskInsertionPoint < 0) {
2034 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002035
Craig Mautner77878772013-03-04 19:46:24 -08002036 }
Craig Mautner77878772013-03-04 19:46:24 -08002037
2038 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2039 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2040 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2041 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002042 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002043 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002044 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002045
Craig Mautnere3a74d52013-02-22 14:14:58 -08002046 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2047 + " to stack at " + task,
2048 new RuntimeException("here").fillInStackTrace());
2049 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2050 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002051 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002052 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002053 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002054 if (VALIDATE_TOKENS) {
2055 validateAppTokensLocked();
2056 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002057
2058 // Now we've moved it in to place... but what if this is
2059 // a singleTop activity and we have put it on top of another
2060 // instance of the same activity? Then we drop the instance
2061 // below so it remains singleTop.
2062 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2063 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002064 int targetNdx = taskActivities.indexOf(target);
2065 if (targetNdx > 0) {
2066 ActivityRecord p = taskActivities.get(targetNdx - 1);
2067 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002068 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2069 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002070 }
2071 }
2072 }
2073 }
2074
2075 replyChainEnd = -1;
2076 }
2077 }
Craig Mautner77878772013-03-04 19:46:24 -08002078 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002079 }
2080
Craig Mautner8849a5e2013-04-02 16:41:03 -07002081 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002082 ActivityRecord newActivity) {
2083 boolean forceReset =
2084 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2085 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2086 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2087 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2088 forceReset = true;
2089 }
2090 }
2091
2092 final TaskRecord task = taskTop.task;
2093
2094 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2095 * for remaining tasks. Used for later tasks to reparent to task. */
2096 boolean taskFound = false;
2097
2098 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2099 ActivityOptions topOptions = null;
2100
Craig Mautner77878772013-03-04 19:46:24 -08002101 // Preserve the location for reparenting in the new task.
2102 int reparentInsertionPoint = -1;
2103
Craig Mautnere3a74d52013-02-22 14:14:58 -08002104 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2105 final TaskRecord targetTask = mTaskHistory.get(i);
2106
2107 if (targetTask == task) {
2108 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2109 taskFound = true;
2110 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002111 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2112 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002113 }
2114 }
2115
Craig Mautner70a86932013-02-28 22:37:44 -08002116 int taskNdx = mTaskHistory.indexOf(task);
2117 do {
2118 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2119 } while (taskTop == null && taskNdx >= 0);
2120
Craig Mautnere3a74d52013-02-22 14:14:58 -08002121 if (topOptions != null) {
2122 // If we got some ActivityOptions from an activity on top that
2123 // was removed from the task, propagate them to the new real top.
2124 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002125 taskTop.updateOptionsLocked(topOptions);
2126 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002127 topOptions.abort();
2128 }
2129 }
2130
2131 return taskTop;
2132 }
2133
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002134 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2135 String resultWho, int requestCode, int resultCode, Intent data) {
2136
2137 if (callingUid > 0) {
2138 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002139 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002140 }
2141
2142 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2143 + " : who=" + resultWho + " req=" + requestCode
2144 + " res=" + resultCode + " data=" + data);
2145 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2146 try {
2147 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2148 list.add(new ResultInfo(resultWho, requestCode,
2149 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002150 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002151 return;
2152 } catch (Exception e) {
2153 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2154 }
2155 }
2156
2157 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2158 }
2159
Craig Mautnerf3333272013-04-22 10:55:53 -07002160 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002161 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2162 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2163 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2164 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002165 if (!mService.mSleeping) {
2166 if (DEBUG_STATES) {
2167 Slog.d(TAG, "no-history finish of " + r);
2168 }
2169 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002170 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002171 } else {
2172 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2173 + " on stop because we're just sleeping");
2174 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002175 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002176 }
2177
2178 if (r.app != null && r.app.thread != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002179 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002180 if (mService.mFocusedActivity == r) {
2181 mService.setFocusedActivityLocked(topRunningActivityLocked(null));
2182 }
2183 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002184 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002185 try {
2186 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002187 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2188 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002189 r.state = ActivityState.STOPPING;
2190 if (DEBUG_VISBILITY) Slog.v(
2191 TAG, "Stopping visible=" + r.visible + " for " + r);
2192 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002193 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002194 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002195 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002196 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002197 r.setSleeping(true);
2198 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002199 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002200 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002201 } catch (Exception e) {
2202 // Maybe just ignore exceptions here... if the process
2203 // has crashed, our death notification will clean things
2204 // up.
2205 Slog.w(TAG, "Exception thrown during pause", e);
2206 // Just in case, assume it to be stopped.
2207 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002208 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002209 r.state = ActivityState.STOPPED;
2210 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002211 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002212 }
2213 }
2214 }
2215 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002216
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002217 /**
2218 * @return Returns true if the activity is being finished, false if for
2219 * some reason it is being left as-is.
2220 */
2221 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002222 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002223 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002224 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002225 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002226 + ", result=" + resultCode + ", data=" + resultData
2227 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002228 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002229 return false;
2230 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002231
Craig Mautnerd44711d2013-02-23 11:24:36 -08002232 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002233 return true;
2234 }
2235
Craig Mautnerd2328952013-03-05 12:46:26 -08002236 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002237 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2238 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2239 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2240 ActivityRecord r = activities.get(activityNdx);
2241 if (r.resultTo == self && r.requestCode == requestCode) {
2242 if ((r.resultWho == null && resultWho == null) ||
2243 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2244 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2245 false);
2246 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002247 }
2248 }
2249 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002250 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002251 }
2252
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002253 final void finishTopRunningActivityLocked(ProcessRecord app) {
2254 ActivityRecord r = topRunningActivityLocked(null);
2255 if (r != null && r.app == app) {
2256 // If the top running activity is from this crashing
2257 // process, then terminate it to avoid getting in a loop.
2258 Slog.w(TAG, " Force finishing activity "
2259 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002260 int taskNdx = mTaskHistory.indexOf(r.task);
2261 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002262 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002263 // Also terminate any activities below it that aren't yet
2264 // stopped, to avoid a situation where one will get
2265 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002266 --activityNdx;
2267 if (activityNdx < 0) {
2268 do {
2269 --taskNdx;
2270 if (taskNdx < 0) {
2271 break;
2272 }
2273 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2274 } while (activityNdx < 0);
2275 }
2276 if (activityNdx >= 0) {
2277 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002278 if (r.state == ActivityState.RESUMED
2279 || r.state == ActivityState.PAUSING
2280 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002281 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002282 Slog.w(TAG, " Force finishing activity "
2283 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002284 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002285 }
2286 }
2287 }
2288 }
2289 }
2290
Craig Mautnerd2328952013-03-05 12:46:26 -08002291 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002292 ArrayList<ActivityRecord> activities = r.task.mActivities;
2293 for (int index = activities.indexOf(r); index >= 0; --index) {
2294 ActivityRecord cur = activities.get(index);
2295 if (!Objects.equal(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002296 break;
2297 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002298 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002299 }
2300 return true;
2301 }
2302
Dianne Hackborn5c607432012-02-28 14:44:19 -08002303 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2304 // send the result
2305 ActivityRecord resultTo = r.resultTo;
2306 if (resultTo != null) {
2307 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2308 + " who=" + r.resultWho + " req=" + r.requestCode
2309 + " res=" + resultCode + " data=" + resultData);
2310 if (r.info.applicationInfo.uid > 0) {
2311 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2312 resultTo.packageName, resultData,
2313 resultTo.getUriPermissionsLocked());
2314 }
2315 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2316 resultData);
2317 r.resultTo = null;
2318 }
2319 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2320
2321 // Make sure this HistoryRecord is not holding on to other resources,
2322 // because clients have remote IPC references to this object so we
2323 // can't assume that will go away and want to avoid circular IPC refs.
2324 r.results = null;
2325 r.pendingResults = null;
2326 r.newIntents = null;
2327 r.icicle = null;
2328 }
2329
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002330 /**
2331 * @return Returns true if this activity has been removed from the history
2332 * list, or false if it is still in the list and will be removed later.
2333 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002334 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2335 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002336 if (r.finishing) {
2337 Slog.w(TAG, "Duplicate finish request for " + r);
2338 return false;
2339 }
2340
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002341 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002342 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002343 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002344 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002345 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2346 final int index = activities.indexOf(r);
2347 if (index < (activities.size() - 1)) {
2348 ActivityRecord next = activities.get(index+1);
2349 if (r.frontOfTask) {
2350 // The next activity is now the front of the task.
2351 next.frontOfTask = true;
2352 }
2353 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2354 // If the caller asked that this activity (and all above it)
2355 // be cleared when the task is reset, don't lose that information,
2356 // but propagate it up to the next activity.
2357 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002358 }
2359 }
2360
2361 r.pauseKeyDispatchingLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002362 if (mStackSupervisor.isFrontStack(this)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002363 if (mService.mFocusedActivity == r) {
Craig Mautner29219d92013-04-16 20:19:12 -07002364 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002365 }
2366 }
2367
Dianne Hackborn5c607432012-02-28 14:44:19 -08002368 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002369
Craig Mautnerc8143c62013-09-03 12:15:57 -07002370 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002371 // There are clients waiting to receive thumbnails so, in case
2372 // this is an activity that someone is waiting for, add it
2373 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002374 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002375 }
2376
Craig Mautnerde4ef022013-04-07 19:01:33 -07002377 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002378 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002379 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002380 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002381 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002382 ? AppTransition.TRANSIT_TASK_CLOSE
2383 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002384
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002385 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002386 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002387
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002388 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002389 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2390 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2391 startPausingLocked(false, false);
2392 }
2393
2394 } else if (r.state != ActivityState.PAUSING) {
2395 // If the activity is PAUSING, we will complete the finish once
2396 // it is done pausing; else we can just directly finish it here.
2397 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002398 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002399 } else {
2400 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2401 }
2402
2403 return false;
2404 }
2405
Craig Mautnerf3333272013-04-22 10:55:53 -07002406 static final int FINISH_IMMEDIATELY = 0;
2407 static final int FINISH_AFTER_PAUSE = 1;
2408 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002409
Craig Mautnerf3333272013-04-22 10:55:53 -07002410 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002411 // First things first: if this activity is currently visible,
2412 // and the resumed activity is not yet visible, then hold off on
2413 // finishing until the resumed one becomes visible.
2414 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002415 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2416 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002417 if (mStackSupervisor.mStoppingActivities.size() > 3
2418 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002419 // If we already have a few activities waiting to stop,
2420 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002421 // them out. Or if r is the last of activity of the last task the stack
2422 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002423 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002424 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002425 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002426 }
2427 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002428 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2429 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002430 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002431 if (oomAdj) {
2432 mService.updateOomAdjLocked();
2433 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002434 return r;
2435 }
2436
2437 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002438 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002439 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002440 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002441 if (mResumedActivity == r) {
2442 mResumedActivity = null;
2443 }
2444 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002445 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002446 r.state = ActivityState.FINISHING;
2447
2448 if (mode == FINISH_IMMEDIATELY
2449 || prevState == ActivityState.STOPPED
2450 || prevState == ActivityState.INITIALIZING) {
2451 // If this activity is already stopped, we can just finish
2452 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002453 boolean activityRemoved = destroyActivityLocked(r, true,
2454 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002455 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002456 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002457 }
2458 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002459 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002460
2461 // Need to go through the full pause cycle to get this
2462 // activity into the stopped state and then finish it.
2463 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002464 mStackSupervisor.mFinishingActivities.add(r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002465 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002466 return r;
2467 }
2468
Craig Mautnerd2328952013-03-05 12:46:26 -08002469 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002470 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002471 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002472 final TaskRecord task = srec.task;
2473 final ArrayList<ActivityRecord> activities = task.mActivities;
2474 final int start = activities.indexOf(srec);
2475 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002476 return false;
2477 }
2478 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002479 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002480 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002481 final ComponentName dest = destIntent.getComponent();
2482 if (start > 0 && dest != null) {
2483 for (int i = finishTo; i >= 0; i--) {
2484 ActivityRecord r = activities.get(i);
2485 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002486 r.info.name.equals(dest.getClassName())) {
2487 finishTo = i;
2488 parent = r;
2489 foundParentInTask = true;
2490 break;
2491 }
2492 }
2493 }
2494
2495 IActivityController controller = mService.mController;
2496 if (controller != null) {
2497 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2498 if (next != null) {
2499 // ask watcher if this is allowed
2500 boolean resumeOK = true;
2501 try {
2502 resumeOK = controller.activityResuming(next.packageName);
2503 } catch (RemoteException e) {
2504 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002505 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002506 }
2507
2508 if (!resumeOK) {
2509 return false;
2510 }
2511 }
2512 }
2513 final long origId = Binder.clearCallingIdentity();
2514 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002515 ActivityRecord r = activities.get(i);
2516 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002517 // Only return the supplied result for the first activity finished
2518 resultCode = Activity.RESULT_CANCELED;
2519 resultData = null;
2520 }
2521
2522 if (parent != null && foundParentInTask) {
2523 final int parentLaunchMode = parent.info.launchMode;
2524 final int destIntentFlags = destIntent.getFlags();
2525 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2526 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2527 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2528 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2529 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2530 } else {
2531 try {
2532 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2533 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002534 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002535 null, aInfo, parent.appToken, null,
2536 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2537 0, null, true, null);
2538 foundParentInTask = res == ActivityManager.START_SUCCESS;
2539 } catch (RemoteException e) {
2540 foundParentInTask = false;
2541 }
2542 requestFinishActivityLocked(parent.appToken, resultCode,
2543 resultData, "navigate-up", true);
2544 }
2545 }
2546 Binder.restoreCallingIdentity(origId);
2547 return foundParentInTask;
2548 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002549 /**
2550 * Perform the common clean-up of an activity record. This is called both
2551 * as part of destroyActivityLocked() (when destroying the client-side
2552 * representation) and cleaning things up as a result of its hosting
2553 * processing going away, in which case there is no remaining client-side
2554 * state to destroy so only the cleanup here is needed.
2555 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002556 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2557 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002558 if (mResumedActivity == r) {
2559 mResumedActivity = null;
2560 }
2561 if (mService.mFocusedActivity == r) {
2562 mService.mFocusedActivity = null;
2563 }
2564
2565 r.configDestroy = false;
2566 r.frozenBeforeDestroy = false;
2567
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002568 if (setState) {
2569 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2570 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002571 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002572 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002573 }
2574
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002575 // Make sure this record is no longer in the pending finishes list.
2576 // This could happen, for example, if we are trimming activities
2577 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002578 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002579 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002580
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002581 // Remove any pending results.
2582 if (r.finishing && r.pendingResults != null) {
2583 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2584 PendingIntentRecord rec = apr.get();
2585 if (rec != null) {
2586 mService.cancelIntentSenderLocked(rec, false);
2587 }
2588 }
2589 r.pendingResults = null;
2590 }
2591
2592 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002593 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002594 }
2595
Craig Mautnerc8143c62013-09-03 12:15:57 -07002596 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002597 // There are clients waiting to receive thumbnails so, in case
2598 // this is an activity that someone is waiting for, add it
2599 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002600 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002601 }
2602
2603 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002604 removeTimeoutsForActivityLocked(r);
2605 }
2606
2607 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002608 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002609 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002610 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002611 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002612 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002613 }
2614
Dianne Hackborn5c607432012-02-28 14:44:19 -08002615 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002616 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2617 r.makeFinishing();
2618 if (DEBUG_ADD_REMOVE) {
2619 RuntimeException here = new RuntimeException("here");
2620 here.fillInStackTrace();
2621 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002622 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002623 final TaskRecord task = r.task;
2624 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002625 if (DEBUG_STACK) Slog.i(TAG,
2626 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002627 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002628 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002629 r.takeFromHistory();
2630 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002631 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002632 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002633 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002634 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002635 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002636 if (VALIDATE_TOKENS) {
2637 validateAppTokensLocked();
2638 }
2639 cleanUpActivityServicesLocked(r);
2640 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002641 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002642
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002643 /**
2644 * Perform clean-up of service connections in an activity record.
2645 */
2646 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2647 // Throw away any services that have been bound by this activity.
2648 if (r.connections != null) {
2649 Iterator<ConnectionRecord> it = r.connections.iterator();
2650 while (it.hasNext()) {
2651 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002652 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002653 }
2654 r.connections = null;
2655 }
2656 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002657
2658 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2659 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2660 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2661 mHandler.sendMessage(msg);
2662 }
2663
Dianne Hackborn28695e02011-11-02 21:59:51 -07002664 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002665 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002666 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002667 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2668 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2669 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2670 final ActivityRecord r = activities.get(activityNdx);
2671 if (r.finishing) {
2672 continue;
2673 }
2674 if (r.fullscreen) {
2675 lastIsOpaque = true;
2676 }
2677 if (owner != null && r.app != owner) {
2678 continue;
2679 }
2680 if (!lastIsOpaque) {
2681 continue;
2682 }
2683 // We can destroy this one if we have its icicle saved and
2684 // it is not in the process of pausing/stopping/finishing.
2685 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2686 && r.haveState && !r.visible && r.stopped
2687 && r.state != ActivityState.DESTROYING
2688 && r.state != ActivityState.DESTROYED) {
2689 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2690 + " resumed=" + mResumedActivity
2691 + " pausing=" + mPausingActivity);
2692 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2693 activityRemoved = true;
2694 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002695 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002696 }
2697 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002698 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002699 mStackSupervisor.resumeTopActivitiesLocked();
2700
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002701 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002702 }
2703
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002704 /**
2705 * Destroy the current CLIENT SIDE instance of an activity. This may be
2706 * called both when actually finishing an activity, or when performing
2707 * a configuration switch where we destroy the current client-side object
2708 * but then create a new client-side object for this same HistoryRecord.
2709 */
2710 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002711 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002712 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002713 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002714 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2715 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002716 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002717 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002718
2719 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002720
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002721 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002722
2723 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002724
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002725 if (hadApp) {
2726 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002727 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002728 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2729 mService.mHeavyWeightProcess = null;
2730 mService.mHandler.sendEmptyMessage(
2731 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2732 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07002733 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07002734 // No longer have activities, so update LRU list and oom adj.
2735 mService.updateLruProcessLocked(r.app, false, false);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002736 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002737 }
2738 }
2739
2740 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002741
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002742 try {
2743 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002744 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002745 r.configChangeFlags);
2746 } catch (Exception e) {
2747 // We can just ignore exceptions here... if the process
2748 // has crashed, our death notification will clean things
2749 // up.
2750 //Slog.w(TAG, "Exception thrown during finish", e);
2751 if (r.finishing) {
2752 removeActivityFromHistoryLocked(r);
2753 removedFromHistory = true;
2754 skipDestroy = true;
2755 }
2756 }
2757
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002758 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002759
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002760 // If the activity is finishing, we need to wait on removing it
2761 // from the list to give it a chance to do its cleanup. During
2762 // that time it may make calls back with its token so we need to
2763 // be able to find it on the list and so we don't want to remove
2764 // it from the list yet. Otherwise, we can just immediately put
2765 // it in the destroyed state since we are not removing it from the
2766 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002767 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002768 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2769 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002770 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002771 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002772 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2773 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002774 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002775 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002776 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002777 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002778 }
2779 } else {
2780 // remove this record from the history.
2781 if (r.finishing) {
2782 removeActivityFromHistoryLocked(r);
2783 removedFromHistory = true;
2784 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002785 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002786 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002787 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002788 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002789 }
2790 }
2791
2792 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002793
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002794 if (!mLRUActivities.remove(r) && hadApp) {
2795 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2796 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002797
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002798 return removedFromHistory;
2799 }
2800
Craig Mautnerd2328952013-03-05 12:46:26 -08002801 final void activityDestroyedLocked(IBinder token) {
2802 final long origId = Binder.clearCallingIdentity();
2803 try {
2804 ActivityRecord r = ActivityRecord.forToken(token);
2805 if (r != null) {
2806 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002807 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002808
2809 if (isInStackLocked(token) != null) {
2810 if (r.state == ActivityState.DESTROYING) {
2811 cleanUpActivityLocked(r, true, false);
2812 removeActivityFromHistoryLocked(r);
2813 }
2814 }
Craig Mautner05d29032013-05-03 13:40:13 -07002815 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002816 } finally {
2817 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002818 }
2819 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002820
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002821 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2822 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002823 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002824 if (DEBUG_CLEANUP) Slog.v(
2825 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002826 + " with " + i + " entries");
2827 while (i > 0) {
2828 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002829 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002830 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002831 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002832 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002833 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002834 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002835 }
2836 }
2837 }
2838
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002839 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2840 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002841 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2842 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002843 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2844 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002845 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002846 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002847 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2848 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002849
2850 boolean hasVisibleActivities = false;
2851
2852 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002853 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002854 if (DEBUG_CLEANUP) Slog.v(
2855 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002856 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2857 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2858 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2859 final ActivityRecord r = activities.get(activityNdx);
2860 --i;
2861 if (DEBUG_CLEANUP) Slog.v(
2862 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2863 if (r.app == app) {
2864 boolean remove;
2865 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2866 // Don't currently have state for the activity, or
2867 // it is finishing -- always remove it.
2868 remove = true;
2869 } else if (r.launchCount > 2 &&
2870 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2871 // We have launched this activity too many times since it was
2872 // able to run, so give up and remove it.
2873 remove = true;
2874 } else {
2875 // The process may be gone, but the activity lives on!
2876 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002877 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002878 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002879 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002880 RuntimeException here = new RuntimeException("here");
2881 here.fillInStackTrace();
2882 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2883 + ": haveState=" + r.haveState
2884 + " stateNotNeeded=" + r.stateNotNeeded
2885 + " finishing=" + r.finishing
2886 + " state=" + r.state, here);
2887 }
2888 if (!r.finishing) {
2889 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2890 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2891 r.userId, System.identityHashCode(r),
2892 r.task.taskId, r.shortComponentName,
2893 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07002894 if (r.state == ActivityState.RESUMED) {
2895 mService.updateUsageStats(r, false);
2896 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002897 }
2898 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002899
Craig Mautner0247fc82013-02-28 14:32:06 -08002900 } else {
2901 // We have the current state for this activity, so
2902 // it can be restarted later when needed.
2903 if (localLOGV) Slog.v(
2904 TAG, "Keeping entry, setting app to null");
2905 if (r.visible) {
2906 hasVisibleActivities = true;
2907 }
2908 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2909 + r);
2910 r.app = null;
2911 r.nowVisible = false;
2912 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002913 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08002914 "App died, clearing saved state of " + r);
2915 r.icicle = null;
2916 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002917 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002918
Craig Mautnerd2328952013-03-05 12:46:26 -08002919 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002920 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002921 }
2922 }
2923
2924 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002925 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002926
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002927 final void updateTransitLocked(int transit, Bundle options) {
2928 if (options != null) {
2929 ActivityRecord r = topRunningActivityLocked(null);
2930 if (r != null && r.state != ActivityState.RESUMED) {
2931 r.updateOptionsLocked(options);
2932 } else {
2933 ActivityOptions.abort(options);
2934 }
2935 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002936 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002937 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002938
Craig Mautnera82aa092013-09-13 15:34:08 -07002939 void moveHomeTaskToTop() {
2940 final int top = mTaskHistory.size() - 1;
2941 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
2942 final TaskRecord task = mTaskHistory.get(taskNdx);
2943 if (task.isHomeTask()) {
2944 mTaskHistory.remove(taskNdx);
2945 mTaskHistory.add(top, task);
2946 mWindowManager.moveTaskToTop(task.taskId);
2947 return;
2948 }
2949 }
2950 }
2951
Craig Mautnercae015f2013-02-08 14:31:27 -08002952 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002953 final TaskRecord task = taskForIdLocked(taskId);
2954 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08002955 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002956 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002957 }
Craig Mautneraab647e2013-02-28 16:31:36 -08002958 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2959 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07002960 // we'll just indicate that this task returns to the home task.
Craig Mautnerae7ecab2013-09-18 11:48:14 -07002961 task.mOnTopOfHome = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08002962 }
2963 moveTaskToFrontLocked(task, null, options);
2964 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08002965 }
2966 return false;
2967 }
2968
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002969 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002970 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002971
Craig Mautner11bf9a52013-02-19 14:08:51 -08002972 final int numTasks = mTaskHistory.size();
2973 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07002974 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08002975 // nothing to do!
2976 if (reason != null &&
2977 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2978 ActivityOptions.abort(options);
2979 } else {
2980 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
2981 }
2982 return;
2983 }
2984
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07002985 mStackSupervisor.moveHomeStack(isHomeStack());
2986
Craig Mautner11bf9a52013-02-19 14:08:51 -08002987 // Shift all activities with this task up to the top
2988 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07002989 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08002990
2991 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002992 if (reason != null &&
2993 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002994 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002995 ActivityRecord r = topRunningActivityLocked(null);
2996 if (r != null) {
2997 mNoAnimActivities.add(r);
2998 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002999 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003000 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003001 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003002 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003003
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003004 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003005
Craig Mautner05d29032013-05-03 13:40:13 -07003006 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003007 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003008
3009 if (VALIDATE_TOKENS) {
3010 validateAppTokensLocked();
3011 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003012 }
3013
3014 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003015 * Worker method for rearranging history stack. Implements the function of moving all
3016 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003017 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003018 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003019 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3020 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003021 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003022 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003023 * @return Returns true if the move completed, false if not.
3024 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003025 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
3026 Slog.i(TAG, "moveTaskToBack: " + taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003027
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003028 // If we have a watcher, preflight the move before committing to it. First check
3029 // for *other* available tasks, but if none are available, then try again allowing the
3030 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003031 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003032 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003033 if (next == null) {
3034 next = topRunningActivityLocked(null, 0);
3035 }
3036 if (next != null) {
3037 // ask watcher if this is allowed
3038 boolean moveOK = true;
3039 try {
3040 moveOK = mService.mController.activityResuming(next.packageName);
3041 } catch (RemoteException e) {
3042 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003043 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003044 }
3045 if (!moveOK) {
3046 return false;
3047 }
3048 }
3049 }
3050
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003051 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003052 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003053
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003054 final TaskRecord tr = taskForIdLocked(taskId);
Craig Mautnerd2328952013-03-05 12:46:26 -08003055 if (tr == null) {
3056 return false;
3057 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003058
Craig Mautner11bf9a52013-02-19 14:08:51 -08003059 mTaskHistory.remove(tr);
3060 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003061
Craig Mautnerc8143c62013-09-03 12:15:57 -07003062 // There is an assumption that moving a task to the back moves it behind the home activity.
3063 // We make sure here that some activity in the stack will launch home.
3064 ActivityRecord lastActivity = null;
3065 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003066 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3067 final TaskRecord task = mTaskHistory.get(taskNdx);
3068 if (task.mOnTopOfHome) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003069 break;
3070 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003071 if (taskNdx == 1) {
3072 // Set the last task before tr to go to home.
3073 task.mOnTopOfHome = true;
3074 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003075 }
3076
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003077 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003078 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3079 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003080 ActivityRecord r = topRunningActivityLocked(null);
3081 if (r != null) {
3082 mNoAnimActivities.add(r);
3083 }
3084 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003085 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003086 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003087 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003088
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003089 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003090 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003091 }
3092
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003093 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
3094 if (task == tr && task.mOnTopOfHome || numTasks <= 1) {
3095 task.mOnTopOfHome = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003096 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003097 }
3098
Craig Mautner05d29032013-05-03 13:40:13 -07003099 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003100 return true;
3101 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003102
Craig Mautner8849a5e2013-04-02 16:41:03 -07003103 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003104 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003105 final Uri data = r.intent.getData();
3106 final String strData = data != null ? data.toSafeString() : null;
3107
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003108 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003109 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003110 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003111 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003112 }
3113
3114 /**
3115 * Make sure the given activity matches the current configuration. Returns
3116 * false if the activity had to be destroyed. Returns true if the
3117 * configuration is the same, or the activity will remain running as-is
3118 * for whatever reason. Ensures the HistoryRecord is updated with the
3119 * correct configuration and all other bookkeeping is handled.
3120 */
3121 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3122 int globalChanges) {
3123 if (mConfigWillChange) {
3124 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3125 "Skipping config check (will change): " + r);
3126 return true;
3127 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003128
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003129 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3130 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003131
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003132 // Short circuit: if the two configurations are the exact same
3133 // object (the common case), then there is nothing to do.
3134 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003135 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003136 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3137 "Configuration unchanged in " + r);
3138 return true;
3139 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003140
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003141 // We don't worry about activities that are finishing.
3142 if (r.finishing) {
3143 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3144 "Configuration doesn't matter in finishing " + r);
3145 r.stopFreezingScreenLocked(false);
3146 return true;
3147 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003148
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003149 // Okay we now are going to make this activity have the new config.
3150 // But then we need to figure out how it needs to deal with that.
3151 Configuration oldConfig = r.configuration;
3152 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003153
3154 // Determine what has changed. May be nothing, if this is a config
3155 // that has come back from the app after going idle. In that case
3156 // we just want to leave the official config object now in the
3157 // activity and do nothing else.
3158 final int changes = oldConfig.diff(newConfig);
3159 if (changes == 0 && !r.forceNewConfig) {
3160 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3161 "Configuration no differences in " + r);
3162 return true;
3163 }
3164
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003165 // If the activity isn't currently running, just leave the new
3166 // configuration and it will pick that up next time it starts.
3167 if (r.app == null || r.app.thread == null) {
3168 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3169 "Configuration doesn't matter not running " + r);
3170 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003171 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003172 return true;
3173 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003174
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003175 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003176 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3177 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3178 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003179 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003180 + ", newConfig=" + newConfig);
3181 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003182 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003183 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3184 r.configChangeFlags |= changes;
3185 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003186 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003187 if (r.app == null || r.app.thread == null) {
3188 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003189 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003190 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003191 } else if (r.state == ActivityState.PAUSING) {
3192 // A little annoying: we are waiting for this activity to
3193 // finish pausing. Let's not do anything now, but just
3194 // flag that it needs to be restarted when done pausing.
3195 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003196 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003197 r.configDestroy = true;
3198 return true;
3199 } else if (r.state == ActivityState.RESUMED) {
3200 // Try to optimize this case: the configuration is changing
3201 // and we need to restart the top, resumed activity.
3202 // Instead of doing the normal handshaking, just say
3203 // "restart!".
3204 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003205 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003206 relaunchActivityLocked(r, r.configChangeFlags, true);
3207 r.configChangeFlags = 0;
3208 } else {
3209 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003210 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003211 relaunchActivityLocked(r, r.configChangeFlags, false);
3212 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003213 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003214
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003215 // All done... tell the caller we weren't able to keep this
3216 // activity around.
3217 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003218 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003219
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003220 // Default case: the activity can handle this new configuration, so
3221 // hand it over. Note that we don't need to give it the new
3222 // configuration, since we always send configuration changes to all
3223 // process when they happen so it can just use whatever configuration
3224 // it last got.
3225 if (r.app != null && r.app.thread != null) {
3226 try {
3227 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003228 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003229 } catch (RemoteException e) {
3230 // If process died, whatever.
3231 }
3232 }
3233 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003234
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003235 return true;
3236 }
3237
Craig Mautnerc8143c62013-09-03 12:15:57 -07003238 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003239 int changes, boolean andResume) {
3240 List<ResultInfo> results = null;
3241 List<Intent> newIntents = null;
3242 if (andResume) {
3243 results = r.results;
3244 newIntents = r.newIntents;
3245 }
3246 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3247 + " with results=" + results + " newIntents=" + newIntents
3248 + " andResume=" + andResume);
3249 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003250 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003251 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003252
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003253 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003254
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003255 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003256 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3257 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3258 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003259 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003260 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003261 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003262 // Note: don't need to call pauseIfSleepingLocked() here, because
3263 // the caller will only pass in 'andResume' if this activity is
3264 // currently resumed, which implies we aren't sleeping.
3265 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003266 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003267 }
3268
3269 if (andResume) {
3270 r.results = null;
3271 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003272 r.state = ActivityState.RESUMED;
3273 } else {
3274 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3275 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003276 }
3277
3278 return true;
3279 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003280
3281 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003282 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3283 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3284 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3285 final ActivityRecord r = activities.get(activityNdx);
3286 if (r.appToken == token) {
3287 return true;
3288 }
3289 if (r.fullscreen && !r.finishing) {
3290 return false;
3291 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003292 }
3293 }
3294 return true;
3295 }
3296
3297 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003298 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3299 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3300 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3301 final ActivityRecord r = activities.get(activityNdx);
3302 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003303 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003304 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003305 }
3306 }
3307 }
3308
3309 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3310 boolean didSomething = false;
3311 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003312 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3313 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3314 int numActivities = activities.size();
3315 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3316 ActivityRecord r = activities.get(activityNdx);
3317 final boolean samePackage = r.packageName.equals(name)
3318 || (name == null && r.userId == userId);
3319 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3320 && (samePackage || r.task == lastTask)
3321 && (r.app == null || evenPersistent || !r.app.persistent)) {
3322 if (!doit) {
3323 if (r.finishing) {
3324 // If this activity is just finishing, then it is not
3325 // interesting as far as something to stop.
3326 continue;
3327 }
3328 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003329 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003330 didSomething = true;
3331 Slog.i(TAG, " Force finishing activity " + r);
3332 if (samePackage) {
3333 if (r.app != null) {
3334 r.app.removed = true;
3335 }
3336 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003337 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003338 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003339 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3340 true)) {
3341 // r has been deleted from mActivities, accommodate.
3342 --numActivities;
3343 --activityNdx;
3344 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003345 }
3346 }
3347 }
3348 return didSomething;
3349 }
3350
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003351 ActivityRecord getTasksLocked(IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003352 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003353 ActivityRecord topRecord = null;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003354 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003355 final TaskRecord task = mTaskHistory.get(taskNdx);
3356 ActivityRecord r = null;
3357 ActivityRecord top = null;
3358 int numActivities = 0;
3359 int numRunning = 0;
3360 final ArrayList<ActivityRecord> activities = task.mActivities;
3361 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3362 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003363
Craig Mautneraab647e2013-02-28 16:31:36 -08003364 // Initialize state for next task if needed.
3365 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3366 top = r;
3367 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003368 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003369
3370 // Add 'r' into the current task.
3371 numActivities++;
3372 if (r.app != null && r.app.thread != null) {
3373 numRunning++;
3374 }
3375
3376 if (localLOGV) Slog.v(
3377 TAG, r.intent.getComponent().flattenToShortString()
3378 + ": task=" + r.task);
3379 }
3380
3381 RunningTaskInfo ci = new RunningTaskInfo();
3382 ci.id = task.taskId;
3383 ci.baseActivity = r.intent.getComponent();
3384 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003385 ci.lastActiveTime = task.lastActiveTime;
3386
Craig Mautneraab647e2013-02-28 16:31:36 -08003387 if (top.thumbHolder != null) {
3388 ci.description = top.thumbHolder.lastDescription;
3389 }
3390 ci.numActivities = numActivities;
3391 ci.numRunning = numRunning;
3392 //System.out.println(
3393 // "#" + maxNum + ": " + " descr=" + ci.description);
3394 if (receiver != null) {
3395 if (localLOGV) Slog.v(
3396 TAG, "State=" + top.state + "Idle=" + top.idle
3397 + " app=" + top.app
3398 + " thr=" + (top.app != null ? top.app.thread : null));
3399 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3400 if (top.idle && top.app != null && top.app.thread != null) {
3401 topRecord = top;
3402 } else {
3403 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003404 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003405 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003406 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003407 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003408 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003409 }
3410 return topRecord;
3411 }
3412
3413 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003414 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003415 if (DEBUG_SWITCH) Slog.d(
3416 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003417 if (top >= 0) {
3418 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3419 int activityTop = activities.size() - 1;
3420 if (activityTop > 0) {
3421 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3422 "unhandled-back", true);
3423 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003424 }
3425 }
3426
Craig Mautner6b74cb52013-09-27 17:02:21 -07003427 /**
3428 * Reset local parameters because an app's activity died.
3429 * @param app The app of the activity that died.
3430 * @return true if home should be launched next.
3431 */
3432 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003433 if (!containsApp(app)) {
Craig Mautner6b74cb52013-09-27 17:02:21 -07003434 return false;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07003435 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07003436
Craig Mautnere79d42682013-04-01 19:01:53 -07003437 if (mPausingActivity != null && mPausingActivity.app == app) {
3438 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3439 "App died while pausing: " + mPausingActivity);
3440 mPausingActivity = null;
3441 }
3442 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3443 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003444 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003445 }
3446
Craig Mautner6b74cb52013-09-27 17:02:21 -07003447 // Determine if the top task is exiting and should return to home. Do this before it gets
3448 // removed in removeHistoryRecordsForAppsLocked.
3449 boolean launchHomeNext = false;
Craig Mautner51277a82013-10-01 14:28:23 -07003450 TaskRecord topTask = mTaskHistory.get(mTaskHistory.size() - 1);
3451 ArrayList<ActivityRecord> activities = topTask.mActivities;
3452 int activityNdx;
3453 for (activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3454 ActivityRecord r = activities.get(activityNdx);
3455 if (r.finishing) {
Craig Mautner6b74cb52013-09-27 17:02:21 -07003456 continue;
Craig Mautnere79d42682013-04-01 19:01:53 -07003457 }
Craig Mautner51277a82013-10-01 14:28:23 -07003458 if (r.app != app) {
3459 // This is the dying activity.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003460 break;
3461 }
Craig Mautner51277a82013-10-01 14:28:23 -07003462 }
3463 if (activityNdx < 0) {
3464 // All activities in task belong to app. Set launchHomeNext to task's value.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003465 launchHomeNext = topTask.mOnTopOfHome;
Craig Mautnere79d42682013-04-01 19:01:53 -07003466 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07003467
3468 removeHistoryRecordsForAppLocked(app);
3469
3470 return launchHomeNext;
Craig Mautnere79d42682013-04-01 19:01:53 -07003471 }
3472
Craig Mautnercae015f2013-02-08 14:31:27 -08003473 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003474 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3475 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3476 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3477 final ActivityRecord r = activities.get(activityNdx);
3478 if (r.app == app) {
3479 Slog.w(TAG, " Force finishing activity "
3480 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003481 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003482 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003483 }
3484 }
3485 }
3486
Dianne Hackborn390517b2013-05-30 15:03:32 -07003487 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003488 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003489 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003490 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3491 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003492 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3493 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003494 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003495 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003496 if (printed) {
3497 header = null;
3498 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003499 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003500 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003501 }
3502
3503 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3504 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3505
3506 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003507 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3508 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003509 }
3510 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003511 final int top = mTaskHistory.size() - 1;
3512 if (top >= 0) {
3513 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3514 int listTop = list.size() - 1;
3515 if (listTop >= 0) {
3516 activities.add(list.get(listTop));
3517 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003518 }
3519 } else {
3520 ItemMatcher matcher = new ItemMatcher();
3521 matcher.build(name);
3522
Craig Mautneraab647e2013-02-28 16:31:36 -08003523 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3524 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3525 if (matcher.match(r1, r1.intent.getComponent())) {
3526 activities.add(r1);
3527 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003528 }
3529 }
3530 }
3531
3532 return activities;
3533 }
3534
3535 ActivityRecord restartPackage(String packageName) {
3536 ActivityRecord starting = topRunningActivityLocked(null);
3537
3538 // All activities that came from the package must be
3539 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003540 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3541 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3542 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3543 final ActivityRecord a = activities.get(activityNdx);
3544 if (a.info.packageName.equals(packageName)) {
3545 a.forceNewConfig = true;
3546 if (starting != null && a == starting && a.visible) {
3547 a.startFreezingScreenLocked(starting.app,
3548 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3549 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003550 }
3551 }
3552 }
3553
3554 return starting;
3555 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003556
Craig Mautnerde4ef022013-04-07 19:01:33 -07003557 boolean removeTask(TaskRecord task) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003558 final int taskNdx = mTaskHistory.indexOf(task);
3559 final int topTaskNdx = mTaskHistory.size() - 1;
3560 if (task.mOnTopOfHome && taskNdx < topTaskNdx) {
3561 mTaskHistory.get(taskNdx + 1).mOnTopOfHome = true;
3562 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003563 mTaskHistory.remove(task);
Craig Mautnerc8143c62013-09-03 12:15:57 -07003564 return mTaskHistory.isEmpty();
Craig Mautner0247fc82013-02-28 14:32:06 -08003565 }
3566
Craig Mautnerde4ef022013-04-07 19:01:33 -07003567 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003568 TaskRecord task = new TaskRecord(taskId, info, intent);
3569 addTask(task, toTop);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003570 return task;
3571 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003572
3573 ArrayList<TaskRecord> getAllTasks() {
3574 return new ArrayList<TaskRecord>(mTaskHistory);
3575 }
3576
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003577 void addTask(final TaskRecord task, final boolean toTop) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003578 task.stack = this;
3579 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003580 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003581 } else {
3582 mTaskHistory.add(0, task);
3583 }
3584 }
3585
3586 public int getStackId() {
3587 return mStackId;
3588 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003589
3590 @Override
3591 public String toString() {
3592 return "stackId=" + mStackId + " tasks=" + mTaskHistory;
3593 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003594}