blob: 37f4cd7a32af3785e6ae6aaa641a6919b1a928d4 [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 Hackborn50dc3bc2010-06-25 10:05:59 -070039import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070040import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080041import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4b71aa12012-12-27 17:20:01 -080042import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080043import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070044import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070045
46import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070047import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070048import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070049import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080050import android.app.IActivityController;
Craig Mautnercae015f2013-02-08 14:31:27 -080051import android.app.IThumbnailReceiver;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070052import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080053import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054import android.content.ComponentName;
55import android.content.Context;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080060import android.content.res.Resources;
61import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080062import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070064import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080065import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.os.Handler;
67import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090068import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070069import android.os.Message;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070070import android.os.RemoteException;
71import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080072import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070073import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070074import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070076import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077
Craig Mautnercae015f2013-02-08 14:31:27 -080078import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080079import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070080import java.lang.ref.WeakReference;
81import java.util.ArrayList;
82import java.util.Iterator;
83import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080084import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070085
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
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700481 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800482 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
483 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700484 if (task.userId != userId) {
485 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700486 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700487 continue;
488 }
Craig Mautner000f0022013-02-26 15:04:29 -0800489 final ActivityRecord r = task.getTopActivity();
490 if (r == null || r.finishing || r.userId != userId ||
491 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700492 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800493 continue;
494 }
495
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700496 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
497 + r.task.intent.getComponent().flattenToShortString()
498 + "/aff=" + r.task.affinity + " to new cls="
499 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Craig Mautner000f0022013-02-26 15:04:29 -0800500 if (task.affinity != null) {
501 if (task.affinity.equals(info.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700502 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700503 return r;
504 }
Craig Mautner000f0022013-02-26 15:04:29 -0800505 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700506 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800507 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700508 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
509 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800510 return r;
511 } else if (task.affinityIntent != null
512 && task.affinityIntent.getComponent().equals(cls)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700513 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800514 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700515 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
516 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800517 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700518 } else if (DEBUG_TASKS) {
519 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700520 }
521 }
522
523 return null;
524 }
525
526 /**
527 * Returns the first activity (starting from the top of the stack) that
528 * is the same as the given activity. Returns null if no such activity
529 * is found.
530 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700531 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700532 ComponentName cls = intent.getComponent();
533 if (info.targetActivity != null) {
534 cls = new ComponentName(info.packageName, info.targetActivity);
535 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700536 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700537
Craig Mautner000f0022013-02-26 15:04:29 -0800538 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700539 TaskRecord task = mTaskHistory.get(taskNdx);
540 if (task.userId != mCurrentUser) {
541 return null;
542 }
543 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800544 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
545 ActivityRecord r = activities.get(activityNdx);
546 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700547 //Slog.i(TAG, "Found matching class!");
548 //dump();
549 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
550 return r;
551 }
552 }
553 }
554
555 return null;
556 }
557
Amith Yamasani742a6712011-05-04 14:49:28 -0700558 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700559 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700560 */
Craig Mautner93529a42013-10-04 15:03:13 -0700561 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800562 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700563 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800564 }
565 mCurrentUser = userId;
566
567 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800568 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800569 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700570 TaskRecord task = mTaskHistory.get(i);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800571 if (task.userId == userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700572 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
573 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800574 mTaskHistory.remove(i);
575 mTaskHistory.add(task);
576 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800577 // Use same value for i.
578 } else {
579 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800580 }
581 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700582 if (VALIDATE_TOKENS) {
583 validateAppTokensLocked();
584 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700585 }
586
Craig Mautner2420ead2013-04-01 17:13:20 -0700587 void minimalResumeActivityLocked(ActivityRecord r) {
588 r.state = ActivityState.RESUMED;
589 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
590 + " (starting new instance)");
591 r.stopped = false;
592 mResumedActivity = r;
593 r.task.touchActiveTime();
594 mService.addRecentTaskLocked(r.task);
595 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700596 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700597 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700598 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700599 }
600
Dianne Hackborncee04b52013-07-03 17:01:28 -0700601 private void startLaunchTraces() {
602 if (mFullyDrawnStartTime != 0) {
603 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
604 }
605 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
606 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
607 }
608
609 private void stopFullyDrawnTraceIfNeeded() {
610 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
611 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
612 mFullyDrawnStartTime = 0;
613 }
614 }
615
Craig Mautnere79d42682013-04-01 19:01:53 -0700616 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700617 if (r.displayStartTime == 0) {
618 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
619 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700620 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700621 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700622 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700623 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700624 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700625 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700626 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700627 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800628
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700629 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700630 // Make sure that there is no activity waiting for this to launch.
631 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
632 r.displayStartTime = r.fullyDrawnStartTime = 0;
633 } else {
634 mStackSupervisor.removeTimeoutsForActivityLocked(r);
635 mStackSupervisor.scheduleIdleTimeoutLocked(r);
636 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700637 }
638
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800639 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800640 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800641 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
642 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
643 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
644 activities.get(activityNdx).setSleeping(false);
645 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800646 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800647 }
648
Craig Mautner0eea92c2013-05-16 13:35:39 -0700649 /**
650 * @return true if something must be done before going to sleep.
651 */
652 boolean checkReadyForSleepLocked() {
653 if (mResumedActivity != null) {
654 // Still have something resumed; can't sleep until it is paused.
655 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
656 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
657 startPausingLocked(false, true);
658 return true;
659 }
660 if (mPausingActivity != null) {
661 // Still waiting for something to pause; can't sleep yet.
662 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
663 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800664 }
665
Craig Mautner0eea92c2013-05-16 13:35:39 -0700666 return false;
667 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800668
Craig Mautner0eea92c2013-05-16 13:35:39 -0700669 void goToSleep() {
670 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800671
Craig Mautner0eea92c2013-05-16 13:35:39 -0700672 // Make sure any stopped but visible activities are now sleeping.
673 // This ensures that the activity's onStop() is called.
674 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
675 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
676 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
677 final ActivityRecord r = activities.get(activityNdx);
678 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
679 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800680 }
681 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800682 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700683 }
Craig Mautner59c00972012-07-30 12:10:24 -0700684
Dianne Hackbornd2835932010-12-13 16:28:46 -0800685 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800686 if (who.noDisplay) {
687 return null;
688 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800689
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700690 TaskRecord tr = who.task;
Craig Mautner34740402013-10-11 11:05:35 -0700691 if (mService.getMostRecentTask() != tr && tr.intent != null &&
692 (tr.intent.getFlags() & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) != 0) {
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700693 // If this task is being excluded from recents, we don't want to take
694 // the expense of capturing a thumbnail, since we will never show it.
695 return null;
696 }
697
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800698 Resources res = mService.mContext.getResources();
699 int w = mThumbnailWidth;
700 int h = mThumbnailHeight;
701 if (w < 0) {
702 mThumbnailWidth = w =
703 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
704 mThumbnailHeight = h =
705 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
706 }
707
708 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800709 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700710 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800711 || mLastScreenshotBitmap.getWidth() != w
712 || mLastScreenshotBitmap.getHeight() != h) {
713 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700714 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700715 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800716 }
717 if (mLastScreenshotBitmap != null) {
John Reck172e87c2013-10-02 16:55:16 -0700718 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800719 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800720 }
721 return null;
722 }
723
Craig Mautnercf910b02013-04-23 11:23:27 -0700724 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800725 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800726 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700727 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800728 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700729 ActivityRecord prev = mResumedActivity;
730 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700731 Slog.e(TAG, "Trying to pause when nothing is resumed",
732 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700733 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700734 return;
735 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700736 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
737 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700738 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800739 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700740 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700741 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
742 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700743 prev.state = ActivityState.PAUSING;
744 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700745 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700746 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
747 if (next == null || next.task != prev.task) {
748 prev.updateThumbnail(screenshotActivities(prev), null);
749 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700750 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700751
752 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800753
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700754 if (prev.app != null && prev.app.thread != null) {
755 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
756 try {
757 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700758 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700759 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700760 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800761 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
762 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700763 } catch (Exception e) {
764 // Ignore exception, if process died other code will cleanup.
765 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800766 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700767 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700768 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700769 }
770 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800771 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700772 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700773 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700774 }
775
776 // If we are not going to sleep, we want to ensure the device is
777 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700778 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700779 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700780 }
781
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800782 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700783 // Have the window manager pause its key dispatching until the new
784 // activity has started. If we're pausing the activity just because
785 // the screen is being turned off and the UI is sleeping, don't interrupt
786 // key dispatch; the same activity will pick it up again on wakeup.
787 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800788 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700789 } else {
790 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
791 }
792
793 // Schedule a pause timeout in case the app doesn't respond.
794 // We don't give it much time because this directly impacts the
795 // responsiveness seen by the user.
796 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
797 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700798 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700799 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
800 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
801 } else {
802 // This activity failed to schedule the
803 // pause, so just treat it as being paused now.
804 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700805 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700806 }
807 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700808
Craig Mautnerd2328952013-03-05 12:46:26 -0800809 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700810 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800811 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700812
Craig Mautnerd2328952013-03-05 12:46:26 -0800813 final ActivityRecord r = isInStackLocked(token);
814 if (r != null) {
815 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
816 if (mPausingActivity == r) {
817 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
818 + (timeout ? " (due to timeout)" : " (pause complete)"));
819 r.state = ActivityState.PAUSED;
820 completePauseLocked();
821 } else {
822 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
823 r.userId, System.identityHashCode(r), r.shortComponentName,
824 mPausingActivity != null
825 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700826 }
827 }
828 }
829
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700830 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
831 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700832 if (r.state != ActivityState.STOPPING) {
833 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
834 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
835 return;
836 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700837 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700838 if (icicle != null) {
839 // If icicle is null, this is happening due to a timeout, so we
840 // haven't really saved the state.
841 r.icicle = icicle;
842 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800843 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700844 r.updateThumbnail(thumbnail, description);
845 }
846 if (!r.stopped) {
847 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
848 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
849 r.stopped = true;
850 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700851 if (r.finishing) {
852 r.clearOptionsLocked();
853 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700854 if (r.configDestroy) {
855 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700856 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700857 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700858 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800859 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700860 }
861 }
862 }
863
Craig Mautnerc8143c62013-09-03 12:15:57 -0700864 private void completePauseLocked() {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800865 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700866 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800867
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800868 if (prev != null) {
869 if (prev.finishing) {
870 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700871 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800872 } else if (prev.app != null) {
873 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
874 if (prev.waitingVisible) {
875 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700876 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800877 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
878 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800879 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800880 if (prev.configDestroy) {
881 // The previous is being paused because the configuration
882 // is changing, which means it is actually stopping...
883 // To juggle the fact that we are also starting a new
884 // instance right now, we need to first completely stop
885 // the current instance before starting the new one.
886 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
887 destroyActivityLocked(prev, true, false, "pause-config");
888 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700889 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700890 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
891 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800892 // If we already have a few activities waiting to stop,
893 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700894 // them out. Or if r is the last of activity of the last task the stack
895 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800896 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700897 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800898 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700899 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800900 }
901 }
902 } else {
903 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
904 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800905 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800906 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700907 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800908
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700909 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700910 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700911 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800912 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700913 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700914 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700915 if (top == null || (prev != null && top != prev)) {
916 // If there are no more activities available to run,
917 // do resume anyway to start something. Also if the top
918 // activity on the stack is not the just paused activity,
919 // we need to go ahead and resume it to ensure we complete
920 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700921 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700922 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700923 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800924
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800925 if (prev != null) {
926 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700927
Craig Mautner525f3d92013-05-07 14:01:50 -0700928 if (prev.app != null && prev.cpuTimeAtResume > 0
929 && mService.mBatteryStatsService.isOnBattery()) {
930 long diff;
Dianne Hackbornd2932242013-08-05 18:18:42 -0700931 synchronized (mService.mProcessCpuThread) {
932 diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
Craig Mautner525f3d92013-05-07 14:01:50 -0700933 - prev.cpuTimeAtResume;
934 }
935 if (diff > 0) {
936 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
937 synchronized (bsi) {
938 BatteryStatsImpl.Uid.Proc ps =
939 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -0700940 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -0700941 if (ps != null) {
942 ps.addForegroundTimeLocked(diff);
943 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700944 }
945 }
946 }
Craig Mautner525f3d92013-05-07 14:01:50 -0700947 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700948 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700949 }
950
951 /**
952 * Once we know that we have asked an application to put an activity in
953 * the resumed state (either by launching it or explicitly telling it),
954 * this function updates the rest of our state to match that fact.
955 */
Craig Mautner525f3d92013-05-07 14:01:50 -0700956 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700957 next.idle = false;
958 next.results = null;
959 next.newIntents = null;
Craig Mautner07566322013-09-26 16:42:55 -0700960 if (next.nowVisible) {
961 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
962 mStackSupervisor.dismissKeyguard();
963 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700964
965 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -0700966 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700967
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700968 mStackSupervisor.reportResumedActivityLocked(next);
969
970 next.resumeKeyDispatchingLocked();
971 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700972
973 // Mark the point when the activity is resuming
974 // TODO: To be more accurate, the mark should be before the onCreate,
975 // not after the onResume. But for subsequent starts, onResume is fine.
976 if (next.app != null) {
Dianne Hackbornd2932242013-08-05 18:18:42 -0700977 synchronized (mService.mProcessCpuThread) {
978 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700979 }
980 } else {
981 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
982 }
983 }
984
Craig Mautner580ea812013-04-25 12:58:38 -0700985 /**
Craig Mautner39e1c5a2013-10-23 15:14:22 -0700986 * Determine if home should be visible below the passed record.
987 * @param record activity we are querying for.
988 * @return true if home is visible below the passed activity, false otherwise.
989 */
990 boolean isActivityOverHome(ActivityRecord record) {
991 // Start at record and go down, look for either home or a visible fullscreen activity.
992 final TaskRecord recordTask = record.task;
993 for (int taskNdx = mTaskHistory.indexOf(recordTask); taskNdx >= 0; --taskNdx) {
994 TaskRecord task = mTaskHistory.get(taskNdx);
995 final ArrayList<ActivityRecord> activities = task.mActivities;
996 final int startNdx =
997 task == recordTask ? activities.indexOf(record) : activities.size() - 1;
998 for (int activityNdx = startNdx; activityNdx >= 0; --activityNdx) {
999 final ActivityRecord r = activities.get(activityNdx);
1000 if (r.isHomeActivity()) {
1001 return true;
1002 }
Craig Mautner76be9d22013-11-04 16:01:22 -08001003 if (!r.finishing && r.fullscreen) {
1004 // Passed activity is over a fullscreen activity.
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001005 return false;
1006 }
1007 }
1008 if (task.mOnTopOfHome) {
1009 // Got to the bottom of a task on top of home without finding a visible fullscreen
1010 // activity. Home is visible.
1011 return true;
1012 }
1013 }
1014 // Got to the bottom of this stack and still don't know. If this is over the home stack
1015 // then record is over home. May not work if we ever get more than two layers.
1016 return mStackSupervisor.isFrontStack(this);
1017 }
1018
1019 /**
Craig Mautner580ea812013-04-25 12:58:38 -07001020 * Version of ensureActivitiesVisible that can easily be called anywhere.
1021 */
1022 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1023 return ensureActivitiesVisibleLocked(starting, configChanges, false);
1024 }
1025
1026 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1027 boolean forceHomeShown) {
1028 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -07001029 return r != null &&
1030 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -07001031 }
1032
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001033 /**
1034 * Make sure that all activities that need to be visible (that is, they
1035 * currently can be seen by the user) actually are.
1036 */
Craig Mautner580ea812013-04-25 12:58:38 -07001037 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
1038 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001039 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001040 TAG, "ensureActivitiesVisible behind " + top
1041 + " configChanges=0x" + Integer.toHexString(configChanges));
1042
Craig Mautner5eda9b32013-07-02 11:58:16 -07001043 if (mTranslucentActivityWaiting != top) {
1044 mUndrawnActivitiesBelowTopTranslucent.clear();
1045 if (mTranslucentActivityWaiting != null) {
1046 // Call the callback with a timeout indication.
1047 notifyActivityDrawnLocked(null);
1048 mTranslucentActivityWaiting = null;
1049 }
1050 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1051 }
1052
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001053 // If the top activity is not fullscreen, then we need to
1054 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001055 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -07001056 boolean showHomeBehindStack = false;
1057 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
1058 !(forceHomeShown && isHomeStack());
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001059 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001060 final TaskRecord task = mTaskHistory.get(taskNdx);
1061 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001062 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1063 final ActivityRecord r = activities.get(activityNdx);
1064 if (r.finishing) {
1065 continue;
1066 }
1067 if (aboveTop && r != top) {
1068 continue;
1069 }
1070 aboveTop = false;
1071 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001072 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001073 TAG, "Make visible? " + r + " finishing=" + r.finishing
1074 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001075
Craig Mautnerd44711d2013-02-23 11:24:36 -08001076 final boolean doThisProcess = onlyThisProcess == null
1077 || onlyThisProcess.equals(r.processName);
1078
1079 // First: if this is not the current activity being started, make
1080 // sure it matches the current configuration.
1081 if (r != starting && doThisProcess) {
1082 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001083 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001084
1085 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001086 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001087 // This activity needs to be visible, but isn't even
1088 // running... get it started, but don't resume it
1089 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001090 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001091 if (r != starting) {
1092 r.startFreezingScreenLocked(r.app, configChanges);
1093 }
1094 if (!r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001095 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001096 TAG, "Starting and making visible: " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001097 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001098 }
1099 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001100 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001101 }
1102 }
1103
1104 } else if (r.visible) {
1105 // If this activity is already visible, then there is nothing
1106 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001107 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001108 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001109
1110 } else if (onlyThisProcess == null) {
1111 // This activity is not currently visible, but is running.
1112 // Tell it to become visible.
1113 r.visible = true;
1114 if (r.state != ActivityState.RESUMED && r != starting) {
1115 // If this activity is paused, tell it
1116 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001117 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001118 TAG, "Making visible and scheduling visibility: " + r);
1119 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001120 if (mTranslucentActivityWaiting != null) {
1121 mUndrawnActivitiesBelowTopTranslucent.add(r);
1122 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001123 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001124 r.sleeping = false;
1125 r.app.pendingUiClean = true;
1126 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1127 r.stopFreezingScreenLocked(false);
1128 } catch (Exception e) {
1129 // Just skip on any failure; we'll make it
1130 // visible when it next restarts.
1131 Slog.w(TAG, "Exception thrown making visibile: "
1132 + r.intent.getComponent(), e);
1133 }
1134 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001135 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001136
Craig Mautnerd44711d2013-02-23 11:24:36 -08001137 // Aggregate current change flags.
1138 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001139
Craig Mautnerd44711d2013-02-23 11:24:36 -08001140 if (r.fullscreen) {
1141 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001142 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1143 behindFullscreen = true;
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001144 } else if (isActivityOverHome(r)) {
1145 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1146 showHomeBehindStack = true;
Craig Mautnerf41bcd42013-11-07 11:51:29 -08001147 behindFullscreen = !isHomeStack();
Craig Mautnerd44711d2013-02-23 11:24:36 -08001148 }
1149 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001150 if (DEBUG_VISBILITY) Slog.v(
1151 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1152 + " state=" + r.state
1153 + " behindFullscreen=" + behindFullscreen);
1154 // Now for any activities that aren't visible to the user, make
1155 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001156 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001157 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001158 r.visible = false;
1159 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001160 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001161 switch (r.state) {
1162 case STOPPING:
1163 case STOPPED:
1164 if (r.app != null && r.app.thread != null) {
1165 if (DEBUG_VISBILITY) Slog.v(
1166 TAG, "Scheduling invisibility: " + r);
1167 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1168 }
1169 break;
1170
1171 case INITIALIZING:
1172 case RESUMED:
1173 case PAUSING:
1174 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001175 // This case created for transitioning activities from
1176 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautnere5273b42013-09-09 12:57:47 -07001177 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1178 mStackSupervisor.mStoppingActivities.add(r);
1179 }
1180 mStackSupervisor.scheduleIdleLocked();
Craig Mautner4addfc52013-06-25 08:05:45 -07001181 break;
1182
1183 default:
1184 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001185 }
1186 } catch (Exception e) {
1187 // Just skip on any failure; we'll make it
1188 // visible when it next restarts.
1189 Slog.w(TAG, "Exception thrown making hidden: "
1190 + r.intent.getComponent(), e);
1191 }
1192 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001193 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001194 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001195 }
1196 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001197 }
Craig Mautner580ea812013-04-25 12:58:38 -07001198 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001199 }
Craig Mautner58547802013-03-05 08:23:53 -08001200
Craig Mautner5eda9b32013-07-02 11:58:16 -07001201 void convertToTranslucent(ActivityRecord r) {
1202 mTranslucentActivityWaiting = r;
1203 mUndrawnActivitiesBelowTopTranslucent.clear();
1204 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1205 }
1206
1207 /**
1208 * Called as activities below the top translucent activity are redrawn. When the last one is
1209 * redrawn notify the top activity by calling
1210 * {@link Activity#onTranslucentConversionComplete}.
1211 *
1212 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1213 * occurred and the activity will be notified immediately.
1214 */
1215 void notifyActivityDrawnLocked(ActivityRecord r) {
1216 if ((r == null)
1217 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1218 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1219 // The last undrawn activity below the top has just been drawn. If there is an
1220 // opaque activity at the top, notify it that it can become translucent safely now.
1221 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1222 mTranslucentActivityWaiting = null;
1223 mUndrawnActivitiesBelowTopTranslucent.clear();
1224 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1225
1226 if (waitingActivity != null && waitingActivity.app != null &&
1227 waitingActivity.app.thread != null) {
1228 try {
1229 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1230 waitingActivity.appToken, r != null);
1231 } catch (RemoteException e) {
1232 }
1233 }
1234 }
1235 }
1236
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001237 /**
1238 * Ensure that the top activity in the stack is resumed.
1239 *
1240 * @param prev The previously resumed activity, for when in the process
1241 * of pausing; can be null to call from elsewhere.
1242 *
1243 * @return Returns true if something is being resumed, or false if
1244 * nothing happened.
1245 */
1246 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001247 return resumeTopActivityLocked(prev, null);
1248 }
1249
1250 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001251 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1252
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001253 // Find the first activity that is not finishing.
1254 ActivityRecord next = topRunningActivityLocked(null);
1255
1256 // Remember how we'll process this pause/resume situation, and ensure
1257 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001258 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1259 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001260
1261 if (next == null) {
1262 // There are no more activities! Let's just start up the
1263 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001264 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001265 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001266 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner69ada552013-04-18 13:51:51 -07001267 return mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001268 }
1269
1270 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001271
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001272 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001273 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1274 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001275 // Make sure we have executed any pending transitions, since there
1276 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001277 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001278 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001279 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001280 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001281 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001282 return false;
1283 }
1284
Craig Mautner525f3d92013-05-07 14:01:50 -07001285 final TaskRecord nextTask = next.task;
1286 final TaskRecord prevTask = prev != null ? prev.task : null;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001287 if (prevTask != null && prevTask.mOnTopOfHome && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001288 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001289 if (prevTask == nextTask) {
1290 ArrayList<ActivityRecord> activities = prevTask.mActivities;
1291 final int numActivities = activities.size();
1292 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1293 final ActivityRecord r = activities.get(activityNdx);
1294 // r is usually the same as next, but what if two activities were launched
1295 // before prev finished?
1296 if (!r.finishing) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001297 r.frontOfTask = true;
1298 break;
1299 }
1300 }
1301 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001302 // This task is going away but it was supposed to return to the home task.
1303 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001304 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001305 mTaskHistory.get(taskNdx).mOnTopOfHome = true;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001306 } else {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001307 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Launching home next");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001308 return mStackSupervisor.resumeHomeActivity(prev);
1309 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001310 }
1311
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001312 // If we are sleeping, and there is no resumed activity, and the top
1313 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001314 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001315 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001316 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001317 // Make sure we have executed any pending transitions, since there
1318 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001319 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001320 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001321 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001322 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001323 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001324 return false;
1325 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001326
1327 // Make sure that the user who owns this activity is started. If not,
1328 // we will just leave it as is because someone should be bringing
1329 // another user's activities to the top of the stack.
1330 if (mService.mStartedUsers.get(next.userId) == null) {
1331 Slog.w(TAG, "Skipping resume of top activity " + next
1332 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001333 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001334 return false;
1335 }
1336
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001337 // The activity may be waiting for stop, but that is no longer
1338 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001339 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001340 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001341 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001342 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001343
Dianne Hackborn84375872012-06-01 19:03:50 -07001344 next.updateOptionsLocked(options);
1345
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001346 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1347
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001348 // If we are currently pausing an activity, then don't do anything
1349 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001350 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001351 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1352 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001353 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001354 return false;
1355 }
1356
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001357 // Okay we are now going to start a switch, to 'next'. We may first
1358 // have to pause the current activity, but this is an important point
1359 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001360 // XXX "App Redirected" dialog is getting too many false positives
1361 // at this point, so turn off for now.
1362 if (false) {
1363 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1364 long now = SystemClock.uptimeMillis();
1365 final boolean inTime = mLastStartedActivity.startTime != 0
1366 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1367 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1368 final int nextUid = next.info.applicationInfo.uid;
1369 if (inTime && lastUid != nextUid
1370 && lastUid != next.launchedFromUid
1371 && mService.checkPermission(
1372 android.Manifest.permission.STOP_APP_SWITCHES,
1373 -1, next.launchedFromUid)
1374 != PackageManager.PERMISSION_GRANTED) {
1375 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1376 } else {
1377 next.startTime = now;
1378 mLastStartedActivity = next;
1379 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001380 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001381 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001382 mLastStartedActivity = next;
1383 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001384 }
Craig Mautner58547802013-03-05 08:23:53 -08001385
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001386 // We need to start pausing the current activity so the top one
1387 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001388 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001389 if (mResumedActivity != null) {
1390 pausing = true;
1391 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001392 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001393 }
1394 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001395 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1396 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001397 // At this point we want to put the upcoming activity's process
1398 // at the top of the LRU list, since we know we will be needing it
1399 // very soon and it would be a waste to let it get killed if it
1400 // happens to be sitting towards the end.
1401 if (next.app != null && next.app.thread != null) {
1402 // No reason to do full oom adj update here; we'll let that
1403 // happen whenever it needs to later.
Dianne Hackborndb926082013-10-31 16:32:44 -07001404 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001405 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001406 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001407 return true;
1408 }
1409
Christopher Tated3f175c2012-06-14 14:16:54 -07001410 // If the most recent activity was noHistory but was only stopped rather
1411 // than stopped+finished because the device went to sleep, we need to make
1412 // sure to finish it as we're making a new activity topmost.
Craig Mautner0f922742013-08-06 08:44:42 -07001413 if (mService.mSleeping && mLastNoHistoryActivity != null &&
1414 !mLastNoHistoryActivity.finishing) {
1415 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1416 " on new resume");
1417 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1418 null, "no-history", false);
1419 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001420 }
1421
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001422 if (prev != null && prev != next) {
1423 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1424 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001425 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001426 if (DEBUG_SWITCH) Slog.v(
1427 TAG, "Resuming top, waiting visible to hide: " + prev);
1428 } else {
1429 // The next activity is already visible, so hide the previous
1430 // activity's windows right now so we can show the new one ASAP.
1431 // We only do this if the previous is finishing, which should mean
1432 // it is on top of the one being resumed so hiding it quickly
1433 // is good. Otherwise, we want to do the normal route of allowing
1434 // the resumed activity to be shown so we can decide if the
1435 // previous should actually be hidden depending on whether the
1436 // new one is found to be full-screen or not.
1437 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001438 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001439 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1440 + prev + ", waitingVisible="
1441 + (prev != null ? prev.waitingVisible : null)
1442 + ", nowVisible=" + next.nowVisible);
1443 } else {
1444 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1445 + prev + ", waitingVisible="
1446 + (prev != null ? prev.waitingVisible : null)
1447 + ", nowVisible=" + next.nowVisible);
1448 }
1449 }
1450 }
1451
Dianne Hackborne7f97212011-02-24 14:40:20 -08001452 // Launching this app's activity, make sure the app is no longer
1453 // considered stopped.
1454 try {
1455 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001456 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001457 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001458 } catch (IllegalArgumentException e) {
1459 Slog.w(TAG, "Failed trying to unstop package "
1460 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001461 }
1462
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001463 // We are starting up the next activity, so tell the window manager
1464 // that the previous one will be hidden soon. This way it can know
1465 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001466 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001467 if (prev != null) {
1468 if (prev.finishing) {
1469 if (DEBUG_TRANSITION) Slog.v(TAG,
1470 "Prepare close transition: prev=" + prev);
1471 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001472 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001473 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001474 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001475 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001476 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1477 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001478 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001479 mWindowManager.setAppWillBeHidden(prev.appToken);
1480 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001481 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001482 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001483 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001484 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001485 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001486 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001487 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001488 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1489 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001490 }
1491 }
1492 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001493 mWindowManager.setAppWillBeHidden(prev.appToken);
1494 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001495 }
Craig Mautner967212c2013-04-13 21:10:58 -07001496 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001497 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001498 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001499 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001500 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001501 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001502 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001503 }
1504 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001505 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001506 next.applyOptionsLocked();
1507 } else {
1508 next.clearOptionsLocked();
1509 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001510
Craig Mautnercf910b02013-04-23 11:23:27 -07001511 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001512 if (next.app != null && next.app.thread != null) {
1513 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1514
1515 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001516 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001517
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001518 // schedule launch ticks to collect information about slow apps.
1519 next.startLaunchTickingLocked();
1520
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001521 ActivityRecord lastResumedActivity =
1522 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001523 ActivityState lastState = next.state;
1524
1525 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001526
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001527 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001528 next.state = ActivityState.RESUMED;
1529 mResumedActivity = next;
1530 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001531 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001532 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001533 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001534 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001535
1536 // Have the window manager re-evaluate the orientation of
1537 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001538 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001539 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001540 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001541 mService.mConfiguration,
1542 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1543 if (config != null) {
1544 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001545 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001546 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001547 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001548
Craig Mautner525f3d92013-05-07 14:01:50 -07001549 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001550 // The configuration update wasn't able to keep the existing
1551 // instance of the activity, and instead started a new one.
1552 // We should be all done, but let's just make sure our activity
1553 // is still at the top and schedule another run if something
1554 // weird happened.
1555 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001556 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001557 "Activity config changed during resume: " + next
1558 + ", new next: " + nextNext);
1559 if (nextNext != next) {
1560 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001561 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001562 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001563 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001564 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001565 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001566 return true;
1567 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001568 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001569 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001570 }
Craig Mautner58547802013-03-05 08:23:53 -08001571
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001572 try {
1573 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001574 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001575 if (a != null) {
1576 final int N = a.size();
1577 if (!next.finishing && N > 0) {
1578 if (DEBUG_RESULTS) Slog.v(
1579 TAG, "Delivering results to " + next
1580 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001581 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001582 }
1583 }
1584
1585 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001586 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001587 }
1588
1589 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001590 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001591 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001592
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001593 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001594 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001595 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001596 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
1597 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001598 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001599
Craig Mautner0eea92c2013-05-16 13:35:39 -07001600 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001601
Craig Mautnerac6f8432013-07-17 13:24:59 -07001602 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001603 } catch (Exception e) {
1604 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001605 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1606 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001607 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001608 if (lastStack != null) {
1609 lastStack.mResumedActivity = lastResumedActivity;
1610 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001611 Slog.i(TAG, "Restarting because process died: " + next);
1612 if (!next.hasBeenLaunched) {
1613 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001614 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1615 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001616 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001617 next.appToken, next.packageName, next.theme,
1618 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001619 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1620 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001621 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001622 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001623 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001624 return true;
1625 }
1626
1627 // From this point on, if something goes wrong there is no way
1628 // to recover the activity.
1629 try {
1630 next.visible = true;
1631 completeResumeLocked(next);
1632 } catch (Exception e) {
1633 // If any exception gets thrown, toss away this
1634 // activity and try the next one.
1635 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001636 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001637 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001638 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001639 return true;
1640 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001641 next.stopped = false;
1642
1643 } else {
1644 // Whoops, need to restart this activity!
1645 if (!next.hasBeenLaunched) {
1646 next.hasBeenLaunched = true;
1647 } else {
1648 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001649 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001650 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001651 mService.compatibilityInfoForPackageLocked(
1652 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001653 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001654 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001655 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001656 }
1657 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1658 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001659 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
Craig Mautnere79d42682013-04-01 19:01:53 -07001660 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001661 }
1662
Craig Mautnercf910b02013-04-23 11:23:27 -07001663 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001664 return true;
1665 }
1666
Craig Mautnerac6f8432013-07-17 13:24:59 -07001667 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001668 // If this is being moved to the top by another activity or being launched from the home
1669 // activity, set mOnTopOfHome accordingly.
Craig Mautnerd99384d2013-10-14 07:09:18 -07001670 ActivityStack lastStack = mStackSupervisor.getLastStack();
1671 final boolean fromHome = lastStack == null ? true : lastStack.isHomeStack();
Craig Mautner9c85c202013-10-04 20:11:26 -07001672 if (!isHomeStack() && (fromHome || topTask() != task)) {
1673 task.mOnTopOfHome = fromHome;
1674 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001675
Craig Mautnerac6f8432013-07-17 13:24:59 -07001676 mTaskHistory.remove(task);
1677 // Now put task at top.
1678 int stackNdx = mTaskHistory.size();
1679 if (task.userId != mCurrentUser) {
1680 // Put non-current user tasks below current user tasks.
1681 while (--stackNdx >= 0) {
1682 if (mTaskHistory.get(stackNdx).userId != mCurrentUser) {
1683 break;
1684 }
1685 }
1686 ++stackNdx;
1687 }
1688 mTaskHistory.add(stackNdx, task);
1689 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001690
Craig Mautner8849a5e2013-04-02 16:41:03 -07001691 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001692 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001693 TaskRecord rTask = r.task;
1694 final int taskId = rTask.taskId;
1695 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001696 // Last activity in task had been removed or ActivityManagerService is reusing task.
1697 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001698 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001699 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001700 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001701 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001702 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001703 if (!newTask) {
1704 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001705 boolean startIt = true;
1706 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1707 task = mTaskHistory.get(taskNdx);
1708 if (task == r.task) {
1709 // Here it is! Now, if this is not yet visible to the
1710 // user, then just add it without starting; it will
1711 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001712 if (!startIt) {
1713 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1714 + task, new RuntimeException("here").fillInStackTrace());
1715 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001716 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001717 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1718 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001719 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001720 r.userId, r.info.configChanges);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001721 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001722 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001723 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001724 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001725 return;
1726 }
1727 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001728 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001729 startIt = false;
1730 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001731 }
1732 }
1733
1734 // Place a new activity at top of stack, so it is next to interact
1735 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001736
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001737 // If we are not placing the new activity frontmost, we do not want
1738 // to deliver the onUserLeaving callback to the actual frontmost
1739 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001740 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001741 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001742 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1743 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001744 }
Craig Mautner70a86932013-02-28 22:37:44 -08001745
1746 task = r.task;
1747
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001748 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001749 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001750 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001751 task.addActivityToTop(r);
1752
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001753 r.putInHistory();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001754 r.frontOfTask = newTask;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001755 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001756 // We want to show the starting preview window if we are
1757 // switching to a new task, or the next activity's process is
1758 // not currently running.
1759 boolean showStartingIcon = newTask;
1760 ProcessRecord proc = r.app;
1761 if (proc == null) {
1762 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1763 }
1764 if (proc == null || proc.thread == null) {
1765 showStartingIcon = true;
1766 }
1767 if (DEBUG_TRANSITION) Slog.v(TAG,
1768 "Prepare open transition: starting " + r);
1769 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001770 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001771 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001772 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001773 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001774 ? AppTransition.TRANSIT_TASK_OPEN
1775 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001776 mNoAnimActivities.remove(r);
1777 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001778 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001779 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001780 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001781 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
1782 r.info.configChanges);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001783 boolean doShow = true;
1784 if (newTask) {
1785 // Even though this activity is starting fresh, we still need
1786 // to reset it to make sure we apply affinities to move any
1787 // existing activities from other tasks in to it.
1788 // If the caller has requested that the target task be
1789 // reset, then do so.
1790 if ((r.intent.getFlags()
1791 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1792 resetTaskIfNeededLocked(r, r);
1793 doShow = topRunningNonDelayedActivityLocked(null) == r;
1794 }
1795 }
1796 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1797 // Figure out if we are transitioning from another activity that is
1798 // "has the same starting icon" as the next one. This allows the
1799 // window manager to keep the previous window it had previously
1800 // created, if it still had one.
1801 ActivityRecord prev = mResumedActivity;
1802 if (prev != null) {
1803 // We don't want to reuse the previous starting preview if:
1804 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001805 if (prev.task != r.task) {
1806 prev = null;
1807 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001808 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001809 else if (prev.nowVisible) {
1810 prev = null;
1811 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001812 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001813 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001814 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001815 mService.compatibilityInfoForPackageLocked(
1816 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001817 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001818 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001819 }
1820 } else {
1821 // If this is the first activity, don't do any fancy animations,
1822 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001823 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001824 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001825 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
1826 r.info.configChanges);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001827 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001828 }
1829 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001830 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001831 }
1832
1833 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001834 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001835 }
1836 }
1837
Dianne Hackbornbe707852011-11-11 14:32:10 -08001838 final void validateAppTokensLocked() {
1839 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001840 mValidateAppTokens.ensureCapacity(numActivities());
1841 final int numTasks = mTaskHistory.size();
1842 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1843 TaskRecord task = mTaskHistory.get(taskNdx);
1844 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07001845 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08001846 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001847 }
Craig Mautner000f0022013-02-26 15:04:29 -08001848 TaskGroup group = new TaskGroup();
1849 group.taskId = task.taskId;
1850 mValidateAppTokens.add(group);
1851 final int numActivities = activities.size();
1852 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1853 final ActivityRecord r = activities.get(activityNdx);
1854 group.tokens.add(r.appToken);
1855 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001856 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001857 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001858 }
1859
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001860 /**
1861 * Perform a reset of the given task, if needed as part of launching it.
1862 * Returns the new HistoryRecord at the top of the task.
1863 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001864 /**
1865 * Helper method for #resetTaskIfNeededLocked.
1866 * We are inside of the task being reset... we'll either finish this activity, push it out
1867 * for another task, or leave it as-is.
1868 * @param task The task containing the Activity (taskTop) that might be reset.
1869 * @param forceReset
1870 * @return An ActivityOptions that needs to be processed.
1871 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001872 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001873 ActivityOptions topOptions = null;
1874
1875 int replyChainEnd = -1;
1876 boolean canMoveOptions = true;
1877
1878 // We only do this for activities that are not the root of the task (since if we finish
1879 // the root, we may no longer have the task!).
1880 final ArrayList<ActivityRecord> activities = task.mActivities;
1881 final int numActivities = activities.size();
1882 for (int i = numActivities - 1; i > 0; --i ) {
1883 ActivityRecord target = activities.get(i);
1884
1885 final int flags = target.info.flags;
1886 final boolean finishOnTaskLaunch =
1887 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1888 final boolean allowTaskReparenting =
1889 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1890 final boolean clearWhenTaskReset =
1891 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1892
1893 if (!finishOnTaskLaunch
1894 && !clearWhenTaskReset
1895 && target.resultTo != null) {
1896 // If this activity is sending a reply to a previous
1897 // activity, we can't do anything with it now until
1898 // we reach the start of the reply chain.
1899 // XXX note that we are assuming the result is always
1900 // to the previous activity, which is almost always
1901 // the case but we really shouldn't count on.
1902 if (replyChainEnd < 0) {
1903 replyChainEnd = i;
1904 }
1905 } else if (!finishOnTaskLaunch
1906 && !clearWhenTaskReset
1907 && allowTaskReparenting
1908 && target.taskAffinity != null
1909 && !target.taskAffinity.equals(task.affinity)) {
1910 // If this activity has an affinity for another
1911 // task, then we need to move it out of here. We will
1912 // move it as far out of the way as possible, to the
1913 // bottom of the activity stack. This also keeps it
1914 // correctly ordered with any activities we previously
1915 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08001916 final ThumbnailHolder newThumbHolder;
1917 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07001918 final ActivityRecord bottom =
1919 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08001920 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07001921 if (bottom != null && target.taskAffinity != null
1922 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001923 // If the activity currently at the bottom has the
1924 // same task affinity as the one we are moving,
1925 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08001926 targetTask = bottom.task;
1927 newThumbHolder = bottom.thumbHolder == null ? targetTask : bottom.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001928 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07001929 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001930 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08001931 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
1932 null, false);
1933 newThumbHolder = targetTask;
1934 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001935 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1936 + " out to new task " + target.task);
1937 }
1938
Craig Mautner329f4122013-11-07 09:10:42 -08001939 if (clearWhenTaskReset) {
1940 // This is the start of a new sub-task.
1941 if (target.thumbHolder == null) {
1942 target.thumbHolder = new ThumbnailHolder();
1943 }
1944 } else {
1945 target.thumbHolder = newThumbHolder;
1946 }
1947
Craig Mautnere3a74d52013-02-22 14:14:58 -08001948 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001949 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001950
Craig Mautner525f3d92013-05-07 14:01:50 -07001951 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001952 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1953 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07001954 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001955 if (p.finishing) {
1956 continue;
1957 }
1958
Craig Mautner525f3d92013-05-07 14:01:50 -07001959 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001960 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001961 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001962 topOptions = p.takeOptionsLocked();
1963 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001964 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001965 }
1966 }
1967 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08001968 + task + " adding to task=" + targetTask
1969 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08001970 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1971 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001972 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001973 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08001974
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001975 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001976 }
1977
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001978 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001979 if (VALIDATE_TOKENS) {
1980 validateAppTokensLocked();
1981 }
1982
1983 replyChainEnd = -1;
1984 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
1985 // If the activity should just be removed -- either
1986 // because it asks for it, or the task should be
1987 // cleared -- then finish it and anything that is
1988 // part of its reply chain.
1989 int end;
1990 if (clearWhenTaskReset) {
1991 // In this case, we want to finish this activity
1992 // and everything above it, so be sneaky and pretend
1993 // like these are all in the reply chain.
1994 end = numActivities - 1;
1995 } else if (replyChainEnd < 0) {
1996 end = i;
1997 } else {
1998 end = replyChainEnd;
1999 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002000 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002001 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002002 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002003 if (p.finishing) {
2004 continue;
2005 }
2006 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002007 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002008 topOptions = p.takeOptionsLocked();
2009 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002010 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002011 }
2012 }
Craig Mautner58547802013-03-05 08:23:53 -08002013 if (DEBUG_TASKS) Slog.w(TAG,
2014 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002015 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002016 end--;
2017 srcPos--;
2018 }
2019 }
2020 replyChainEnd = -1;
2021 } else {
2022 // If we were in the middle of a chain, well the
2023 // activity that started it all doesn't want anything
2024 // special, so leave it all as-is.
2025 replyChainEnd = -1;
2026 }
2027 }
2028
2029 return topOptions;
2030 }
2031
2032 /**
2033 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2034 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2035 * @param affinityTask The task we are looking for an affinity to.
2036 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2037 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2038 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2039 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002040 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002041 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002042 int replyChainEnd = -1;
2043 final int taskId = task.taskId;
2044 final String taskAffinity = task.affinity;
2045
2046 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2047 final int numActivities = activities.size();
2048 // Do not operate on the root Activity.
2049 for (int i = numActivities - 1; i > 0; --i) {
2050 ActivityRecord target = activities.get(i);
2051
2052 final int flags = target.info.flags;
2053 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2054 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2055
2056 if (target.resultTo != null) {
2057 // If this activity is sending a reply to a previous
2058 // activity, we can't do anything with it now until
2059 // we reach the start of the reply chain.
2060 // XXX note that we are assuming the result is always
2061 // to the previous activity, which is almost always
2062 // the case but we really shouldn't count on.
2063 if (replyChainEnd < 0) {
2064 replyChainEnd = i;
2065 }
2066 } else if (topTaskIsHigher
2067 && allowTaskReparenting
2068 && taskAffinity != null
2069 && taskAffinity.equals(target.taskAffinity)) {
2070 // This activity has an affinity for our task. Either remove it if we are
2071 // clearing or move it over to our task. Note that
2072 // we currently punt on the case where we are resetting a
2073 // task that is not at the top but who has activities above
2074 // with an affinity to it... this is really not a normal
2075 // case, and we will need to later pull that task to the front
2076 // and usually at that point we will do the reset and pick
2077 // up those remaining activities. (This only happens if
2078 // someone starts an activity in a new task from an activity
2079 // in a task that is not currently on top.)
2080 if (forceReset || finishOnTaskLaunch) {
2081 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2082 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2083 for (int srcPos = start; srcPos >= i; --srcPos) {
2084 final ActivityRecord p = activities.get(srcPos);
2085 if (p.finishing) {
2086 continue;
2087 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002088 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002089 }
2090 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002091 if (taskInsertionPoint < 0) {
2092 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002093
Craig Mautner77878772013-03-04 19:46:24 -08002094 }
Craig Mautner77878772013-03-04 19:46:24 -08002095
2096 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2097 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2098 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2099 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002100 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002101 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002102 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002103
Craig Mautnere3a74d52013-02-22 14:14:58 -08002104 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2105 + " to stack at " + task,
2106 new RuntimeException("here").fillInStackTrace());
2107 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2108 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002109 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002110 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002111 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002112 if (VALIDATE_TOKENS) {
2113 validateAppTokensLocked();
2114 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002115
2116 // Now we've moved it in to place... but what if this is
2117 // a singleTop activity and we have put it on top of another
2118 // instance of the same activity? Then we drop the instance
2119 // below so it remains singleTop.
2120 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2121 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002122 int targetNdx = taskActivities.indexOf(target);
2123 if (targetNdx > 0) {
2124 ActivityRecord p = taskActivities.get(targetNdx - 1);
2125 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002126 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2127 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002128 }
2129 }
2130 }
2131 }
2132
2133 replyChainEnd = -1;
2134 }
2135 }
Craig Mautner77878772013-03-04 19:46:24 -08002136 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002137 }
2138
Craig Mautner8849a5e2013-04-02 16:41:03 -07002139 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002140 ActivityRecord newActivity) {
2141 boolean forceReset =
2142 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2143 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2144 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2145 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2146 forceReset = true;
2147 }
2148 }
2149
2150 final TaskRecord task = taskTop.task;
2151
2152 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2153 * for remaining tasks. Used for later tasks to reparent to task. */
2154 boolean taskFound = false;
2155
2156 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2157 ActivityOptions topOptions = null;
2158
Craig Mautner77878772013-03-04 19:46:24 -08002159 // Preserve the location for reparenting in the new task.
2160 int reparentInsertionPoint = -1;
2161
Craig Mautnere3a74d52013-02-22 14:14:58 -08002162 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2163 final TaskRecord targetTask = mTaskHistory.get(i);
2164
2165 if (targetTask == task) {
2166 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2167 taskFound = true;
2168 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002169 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2170 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002171 }
2172 }
2173
Craig Mautner70a86932013-02-28 22:37:44 -08002174 int taskNdx = mTaskHistory.indexOf(task);
2175 do {
2176 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2177 } while (taskTop == null && taskNdx >= 0);
2178
Craig Mautnere3a74d52013-02-22 14:14:58 -08002179 if (topOptions != null) {
2180 // If we got some ActivityOptions from an activity on top that
2181 // was removed from the task, propagate them to the new real top.
2182 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002183 taskTop.updateOptionsLocked(topOptions);
2184 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002185 topOptions.abort();
2186 }
2187 }
2188
2189 return taskTop;
2190 }
2191
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002192 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2193 String resultWho, int requestCode, int resultCode, Intent data) {
2194
2195 if (callingUid > 0) {
2196 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002197 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002198 }
2199
2200 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2201 + " : who=" + resultWho + " req=" + requestCode
2202 + " res=" + resultCode + " data=" + data);
2203 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2204 try {
2205 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2206 list.add(new ResultInfo(resultWho, requestCode,
2207 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002208 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002209 return;
2210 } catch (Exception e) {
2211 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2212 }
2213 }
2214
2215 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2216 }
2217
Craig Mautner04f0b702013-10-22 12:31:01 -07002218 private void adjustFocusedActivityLocked(ActivityRecord r) {
2219 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2220 ActivityRecord next = topRunningActivityLocked(null);
2221 if (next != r) {
2222 final TaskRecord task = r.task;
2223 if (r.frontOfTask && task == topTask() && task.mOnTopOfHome) {
2224 mStackSupervisor.moveHomeToTop();
2225 }
2226 }
2227 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
2228 }
2229 }
2230
Craig Mautnerf3333272013-04-22 10:55:53 -07002231 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002232 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2233 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2234 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2235 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002236 if (!mService.mSleeping) {
2237 if (DEBUG_STATES) {
2238 Slog.d(TAG, "no-history finish of " + r);
2239 }
2240 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002241 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002242 } else {
2243 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2244 + " on stop because we're just sleeping");
2245 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002246 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002247 }
2248
2249 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002250 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002251 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002252 try {
2253 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002254 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2255 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002256 r.state = ActivityState.STOPPING;
2257 if (DEBUG_VISBILITY) Slog.v(
2258 TAG, "Stopping visible=" + r.visible + " for " + r);
2259 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002260 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002261 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002262 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002263 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002264 r.setSleeping(true);
2265 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002266 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002267 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002268 } catch (Exception e) {
2269 // Maybe just ignore exceptions here... if the process
2270 // has crashed, our death notification will clean things
2271 // up.
2272 Slog.w(TAG, "Exception thrown during pause", e);
2273 // Just in case, assume it to be stopped.
2274 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002275 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002276 r.state = ActivityState.STOPPED;
2277 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002278 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002279 }
2280 }
2281 }
2282 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002283
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002284 /**
2285 * @return Returns true if the activity is being finished, false if for
2286 * some reason it is being left as-is.
2287 */
2288 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002289 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002290 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002291 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002292 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002293 + ", result=" + resultCode + ", data=" + resultData
2294 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002295 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002296 return false;
2297 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002298
Craig Mautnerd44711d2013-02-23 11:24:36 -08002299 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002300 return true;
2301 }
2302
Craig Mautnerd2328952013-03-05 12:46:26 -08002303 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002304 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2305 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2306 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2307 ActivityRecord r = activities.get(activityNdx);
2308 if (r.resultTo == self && r.requestCode == requestCode) {
2309 if ((r.resultWho == null && resultWho == null) ||
2310 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2311 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2312 false);
2313 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002314 }
2315 }
2316 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002317 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002318 }
2319
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002320 final void finishTopRunningActivityLocked(ProcessRecord app) {
2321 ActivityRecord r = topRunningActivityLocked(null);
2322 if (r != null && r.app == app) {
2323 // If the top running activity is from this crashing
2324 // process, then terminate it to avoid getting in a loop.
2325 Slog.w(TAG, " Force finishing activity "
2326 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002327 int taskNdx = mTaskHistory.indexOf(r.task);
2328 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002329 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002330 // Also terminate any activities below it that aren't yet
2331 // stopped, to avoid a situation where one will get
2332 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002333 --activityNdx;
2334 if (activityNdx < 0) {
2335 do {
2336 --taskNdx;
2337 if (taskNdx < 0) {
2338 break;
2339 }
2340 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2341 } while (activityNdx < 0);
2342 }
2343 if (activityNdx >= 0) {
2344 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002345 if (r.state == ActivityState.RESUMED
2346 || r.state == ActivityState.PAUSING
2347 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002348 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002349 Slog.w(TAG, " Force finishing activity "
2350 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002351 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002352 }
2353 }
2354 }
2355 }
2356 }
2357
Craig Mautnerd2328952013-03-05 12:46:26 -08002358 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002359 ArrayList<ActivityRecord> activities = r.task.mActivities;
2360 for (int index = activities.indexOf(r); index >= 0; --index) {
2361 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002362 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002363 break;
2364 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002365 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002366 }
2367 return true;
2368 }
2369
Dianne Hackborn5c607432012-02-28 14:44:19 -08002370 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2371 // send the result
2372 ActivityRecord resultTo = r.resultTo;
2373 if (resultTo != null) {
2374 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2375 + " who=" + r.resultWho + " req=" + r.requestCode
2376 + " res=" + resultCode + " data=" + resultData);
2377 if (r.info.applicationInfo.uid > 0) {
2378 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2379 resultTo.packageName, resultData,
2380 resultTo.getUriPermissionsLocked());
2381 }
2382 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2383 resultData);
2384 r.resultTo = null;
2385 }
2386 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2387
2388 // Make sure this HistoryRecord is not holding on to other resources,
2389 // because clients have remote IPC references to this object so we
2390 // can't assume that will go away and want to avoid circular IPC refs.
2391 r.results = null;
2392 r.pendingResults = null;
2393 r.newIntents = null;
2394 r.icicle = null;
2395 }
2396
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002397 /**
2398 * @return Returns true if this activity has been removed from the history
2399 * list, or false if it is still in the list and will be removed later.
2400 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002401 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2402 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002403 if (r.finishing) {
2404 Slog.w(TAG, "Duplicate finish request for " + r);
2405 return false;
2406 }
2407
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002408 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002409 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002410 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002411 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002412 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2413 final int index = activities.indexOf(r);
2414 if (index < (activities.size() - 1)) {
2415 ActivityRecord next = activities.get(index+1);
2416 if (r.frontOfTask) {
2417 // The next activity is now the front of the task.
2418 next.frontOfTask = true;
2419 }
2420 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2421 // If the caller asked that this activity (and all above it)
2422 // be cleared when the task is reset, don't lose that information,
2423 // but propagate it up to the next activity.
2424 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002425 }
2426 }
2427
2428 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002429
2430 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002431
Dianne Hackborn5c607432012-02-28 14:44:19 -08002432 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002433
Craig Mautnerc8143c62013-09-03 12:15:57 -07002434 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002435 // There are clients waiting to receive thumbnails so, in case
2436 // this is an activity that someone is waiting for, add it
2437 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002438 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002439 }
2440
Craig Mautnerde4ef022013-04-07 19:01:33 -07002441 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002442 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002443 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002444 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002445 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002446 ? AppTransition.TRANSIT_TASK_CLOSE
2447 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002448
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002449 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002450 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002451
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002452 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002453 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2454 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2455 startPausingLocked(false, false);
2456 }
2457
2458 } else if (r.state != ActivityState.PAUSING) {
2459 // If the activity is PAUSING, we will complete the finish once
2460 // it is done pausing; else we can just directly finish it here.
2461 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002462 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002463 } else {
2464 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2465 }
2466
2467 return false;
2468 }
2469
Craig Mautnerf3333272013-04-22 10:55:53 -07002470 static final int FINISH_IMMEDIATELY = 0;
2471 static final int FINISH_AFTER_PAUSE = 1;
2472 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002473
Craig Mautnerf3333272013-04-22 10:55:53 -07002474 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002475 // First things first: if this activity is currently visible,
2476 // and the resumed activity is not yet visible, then hold off on
2477 // finishing until the resumed one becomes visible.
2478 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002479 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2480 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002481 if (mStackSupervisor.mStoppingActivities.size() > 3
2482 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002483 // If we already have a few activities waiting to stop,
2484 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002485 // them out. Or if r is the last of activity of the last task the stack
2486 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002487 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002488 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002489 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002490 }
2491 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002492 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2493 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002494 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002495 if (oomAdj) {
2496 mService.updateOomAdjLocked();
2497 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002498 return r;
2499 }
2500
2501 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002502 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002503 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002504 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002505 if (mResumedActivity == r) {
2506 mResumedActivity = null;
2507 }
2508 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002509 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002510 r.state = ActivityState.FINISHING;
2511
2512 if (mode == FINISH_IMMEDIATELY
2513 || prevState == ActivityState.STOPPED
2514 || prevState == ActivityState.INITIALIZING) {
2515 // If this activity is already stopped, we can just finish
2516 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002517 boolean activityRemoved = destroyActivityLocked(r, true,
2518 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002519 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002520 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002521 }
2522 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002523 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002524
2525 // Need to go through the full pause cycle to get this
2526 // activity into the stopped state and then finish it.
2527 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002528 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002529 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002530 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002531 return r;
2532 }
2533
Craig Mautnerd2328952013-03-05 12:46:26 -08002534 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002535 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002536 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002537 final TaskRecord task = srec.task;
2538 final ArrayList<ActivityRecord> activities = task.mActivities;
2539 final int start = activities.indexOf(srec);
2540 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002541 return false;
2542 }
2543 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002544 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002545 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002546 final ComponentName dest = destIntent.getComponent();
2547 if (start > 0 && dest != null) {
2548 for (int i = finishTo; i >= 0; i--) {
2549 ActivityRecord r = activities.get(i);
2550 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002551 r.info.name.equals(dest.getClassName())) {
2552 finishTo = i;
2553 parent = r;
2554 foundParentInTask = true;
2555 break;
2556 }
2557 }
2558 }
2559
2560 IActivityController controller = mService.mController;
2561 if (controller != null) {
2562 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2563 if (next != null) {
2564 // ask watcher if this is allowed
2565 boolean resumeOK = true;
2566 try {
2567 resumeOK = controller.activityResuming(next.packageName);
2568 } catch (RemoteException e) {
2569 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002570 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002571 }
2572
2573 if (!resumeOK) {
2574 return false;
2575 }
2576 }
2577 }
2578 final long origId = Binder.clearCallingIdentity();
2579 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002580 ActivityRecord r = activities.get(i);
2581 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002582 // Only return the supplied result for the first activity finished
2583 resultCode = Activity.RESULT_CANCELED;
2584 resultData = null;
2585 }
2586
2587 if (parent != null && foundParentInTask) {
2588 final int parentLaunchMode = parent.info.launchMode;
2589 final int destIntentFlags = destIntent.getFlags();
2590 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2591 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2592 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2593 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2594 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2595 } else {
2596 try {
2597 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2598 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002599 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002600 null, aInfo, parent.appToken, null,
2601 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
2602 0, null, true, null);
2603 foundParentInTask = res == ActivityManager.START_SUCCESS;
2604 } catch (RemoteException e) {
2605 foundParentInTask = false;
2606 }
2607 requestFinishActivityLocked(parent.appToken, resultCode,
2608 resultData, "navigate-up", true);
2609 }
2610 }
2611 Binder.restoreCallingIdentity(origId);
2612 return foundParentInTask;
2613 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002614 /**
2615 * Perform the common clean-up of an activity record. This is called both
2616 * as part of destroyActivityLocked() (when destroying the client-side
2617 * representation) and cleaning things up as a result of its hosting
2618 * processing going away, in which case there is no remaining client-side
2619 * state to destroy so only the cleanup here is needed.
2620 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002621 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2622 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002623 if (mResumedActivity == r) {
2624 mResumedActivity = null;
2625 }
2626 if (mService.mFocusedActivity == r) {
2627 mService.mFocusedActivity = null;
2628 }
2629
2630 r.configDestroy = false;
2631 r.frozenBeforeDestroy = false;
2632
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002633 if (setState) {
2634 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2635 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002636 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002637 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002638 }
2639
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002640 // Make sure this record is no longer in the pending finishes list.
2641 // This could happen, for example, if we are trimming activities
2642 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002643 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002644 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002645
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002646 // Remove any pending results.
2647 if (r.finishing && r.pendingResults != null) {
2648 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2649 PendingIntentRecord rec = apr.get();
2650 if (rec != null) {
2651 mService.cancelIntentSenderLocked(rec, false);
2652 }
2653 }
2654 r.pendingResults = null;
2655 }
2656
2657 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002658 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002659 }
2660
Craig Mautnerc8143c62013-09-03 12:15:57 -07002661 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002662 // There are clients waiting to receive thumbnails so, in case
2663 // this is an activity that someone is waiting for, add it
2664 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002665 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002666 }
2667
2668 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002669 removeTimeoutsForActivityLocked(r);
2670 }
2671
2672 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002673 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002674 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002675 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002676 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002677 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002678 }
2679
Dianne Hackborn5c607432012-02-28 14:44:19 -08002680 final void removeActivityFromHistoryLocked(ActivityRecord r) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002681 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2682 r.makeFinishing();
2683 if (DEBUG_ADD_REMOVE) {
2684 RuntimeException here = new RuntimeException("here");
2685 here.fillInStackTrace();
2686 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002687 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002688 final TaskRecord task = r.task;
2689 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002690 if (DEBUG_STACK) Slog.i(TAG,
2691 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnere1db0dd2013-10-13 18:15:00 -07002692 if (mStackSupervisor.isFrontStack(this) && task == topTask() && task.mOnTopOfHome) {
Craig Mautner8e569572013-10-11 17:36:59 -07002693 mStackSupervisor.moveHomeToTop();
2694 }
Craig Mautner2219b752013-10-12 11:26:08 -07002695 mStackSupervisor.removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002696 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002697 r.takeFromHistory();
2698 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002699 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002700 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002701 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002702 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002703 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002704 if (VALIDATE_TOKENS) {
2705 validateAppTokensLocked();
2706 }
2707 cleanUpActivityServicesLocked(r);
2708 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002709 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002710
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002711 /**
2712 * Perform clean-up of service connections in an activity record.
2713 */
2714 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2715 // Throw away any services that have been bound by this activity.
2716 if (r.connections != null) {
2717 Iterator<ConnectionRecord> it = r.connections.iterator();
2718 while (it.hasNext()) {
2719 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002720 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002721 }
2722 r.connections = null;
2723 }
2724 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002725
2726 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2727 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2728 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2729 mHandler.sendMessage(msg);
2730 }
2731
Dianne Hackborn28695e02011-11-02 21:59:51 -07002732 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002733 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002734 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002735 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2736 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2737 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2738 final ActivityRecord r = activities.get(activityNdx);
2739 if (r.finishing) {
2740 continue;
2741 }
2742 if (r.fullscreen) {
2743 lastIsOpaque = true;
2744 }
2745 if (owner != null && r.app != owner) {
2746 continue;
2747 }
2748 if (!lastIsOpaque) {
2749 continue;
2750 }
2751 // We can destroy this one if we have its icicle saved and
2752 // it is not in the process of pausing/stopping/finishing.
2753 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2754 && r.haveState && !r.visible && r.stopped
2755 && r.state != ActivityState.DESTROYING
2756 && r.state != ActivityState.DESTROYED) {
2757 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2758 + " resumed=" + mResumedActivity
2759 + " pausing=" + mPausingActivity);
2760 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2761 activityRemoved = true;
2762 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002763 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002764 }
2765 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002766 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002767 mStackSupervisor.resumeTopActivitiesLocked();
2768
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002769 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002770 }
2771
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002772 /**
2773 * Destroy the current CLIENT SIDE instance of an activity. This may be
2774 * called both when actually finishing an activity, or when performing
2775 * a configuration switch where we destroy the current client-side object
2776 * but then create a new client-side object for this same HistoryRecord.
2777 */
2778 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002779 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002780 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002781 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002782 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2783 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002784 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002785 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002786
2787 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002788
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002789 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002790
2791 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002792
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002793 if (hadApp) {
2794 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002795 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002796 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2797 mService.mHeavyWeightProcess = null;
2798 mService.mHandler.sendEmptyMessage(
2799 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2800 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07002801 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07002802 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07002803 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002804 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002805 }
2806 }
2807
2808 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002809
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002810 try {
2811 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002812 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002813 r.configChangeFlags);
2814 } catch (Exception e) {
2815 // We can just ignore exceptions here... if the process
2816 // has crashed, our death notification will clean things
2817 // up.
2818 //Slog.w(TAG, "Exception thrown during finish", e);
2819 if (r.finishing) {
2820 removeActivityFromHistoryLocked(r);
2821 removedFromHistory = true;
2822 skipDestroy = true;
2823 }
2824 }
2825
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002826 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002827
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002828 // If the activity is finishing, we need to wait on removing it
2829 // from the list to give it a chance to do its cleanup. During
2830 // that time it may make calls back with its token so we need to
2831 // be able to find it on the list and so we don't want to remove
2832 // it from the list yet. Otherwise, we can just immediately put
2833 // it in the destroyed state since we are not removing it from the
2834 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002835 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002836 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2837 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002838 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002839 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002840 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2841 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002842 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002843 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002844 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002845 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002846 }
2847 } else {
2848 // remove this record from the history.
2849 if (r.finishing) {
2850 removeActivityFromHistoryLocked(r);
2851 removedFromHistory = true;
2852 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002853 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002854 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002855 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002856 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002857 }
2858 }
2859
2860 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002861
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002862 if (!mLRUActivities.remove(r) && hadApp) {
2863 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2864 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002865
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002866 return removedFromHistory;
2867 }
2868
Craig Mautnerd2328952013-03-05 12:46:26 -08002869 final void activityDestroyedLocked(IBinder token) {
2870 final long origId = Binder.clearCallingIdentity();
2871 try {
2872 ActivityRecord r = ActivityRecord.forToken(token);
2873 if (r != null) {
2874 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002875 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002876
2877 if (isInStackLocked(token) != null) {
2878 if (r.state == ActivityState.DESTROYING) {
2879 cleanUpActivityLocked(r, true, false);
2880 removeActivityFromHistoryLocked(r);
2881 }
2882 }
Craig Mautner05d29032013-05-03 13:40:13 -07002883 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002884 } finally {
2885 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002886 }
2887 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002888
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002889 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2890 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002891 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002892 if (DEBUG_CLEANUP) Slog.v(
2893 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002894 + " with " + i + " entries");
2895 while (i > 0) {
2896 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002897 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002898 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002899 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002900 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002901 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002902 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002903 }
2904 }
2905 }
2906
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002907 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2908 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002909 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2910 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002911 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2912 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002913 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002914 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002915 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2916 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002917
2918 boolean hasVisibleActivities = false;
2919
2920 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002921 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002922 if (DEBUG_CLEANUP) Slog.v(
2923 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002924 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2925 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2926 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2927 final ActivityRecord r = activities.get(activityNdx);
2928 --i;
2929 if (DEBUG_CLEANUP) Slog.v(
2930 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2931 if (r.app == app) {
2932 boolean remove;
2933 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2934 // Don't currently have state for the activity, or
2935 // it is finishing -- always remove it.
2936 remove = true;
2937 } else if (r.launchCount > 2 &&
2938 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2939 // We have launched this activity too many times since it was
2940 // able to run, so give up and remove it.
2941 remove = true;
2942 } else {
2943 // The process may be gone, but the activity lives on!
2944 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002945 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002946 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002947 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002948 RuntimeException here = new RuntimeException("here");
2949 here.fillInStackTrace();
2950 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2951 + ": haveState=" + r.haveState
2952 + " stateNotNeeded=" + r.stateNotNeeded
2953 + " finishing=" + r.finishing
2954 + " state=" + r.state, here);
2955 }
2956 if (!r.finishing) {
2957 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2958 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2959 r.userId, System.identityHashCode(r),
2960 r.task.taskId, r.shortComponentName,
2961 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07002962 if (r.state == ActivityState.RESUMED) {
2963 mService.updateUsageStats(r, false);
2964 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002965 }
2966 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002967
Craig Mautner0247fc82013-02-28 14:32:06 -08002968 } else {
2969 // We have the current state for this activity, so
2970 // it can be restarted later when needed.
2971 if (localLOGV) Slog.v(
2972 TAG, "Keeping entry, setting app to null");
2973 if (r.visible) {
2974 hasVisibleActivities = true;
2975 }
2976 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
2977 + r);
2978 r.app = null;
2979 r.nowVisible = false;
2980 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002981 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08002982 "App died, clearing saved state of " + r);
2983 r.icicle = null;
2984 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002985 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002986
Craig Mautnerd2328952013-03-05 12:46:26 -08002987 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08002988 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002989 }
2990 }
2991
2992 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002993 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002994
Dianne Hackborn7f58b952012-04-18 12:59:29 -07002995 final void updateTransitLocked(int transit, Bundle options) {
2996 if (options != null) {
2997 ActivityRecord r = topRunningActivityLocked(null);
2998 if (r != null && r.state != ActivityState.RESUMED) {
2999 r.updateOptionsLocked(options);
3000 } else {
3001 ActivityOptions.abort(options);
3002 }
3003 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003004 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003005 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003006
Craig Mautnera82aa092013-09-13 15:34:08 -07003007 void moveHomeTaskToTop() {
3008 final int top = mTaskHistory.size() - 1;
3009 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3010 final TaskRecord task = mTaskHistory.get(taskNdx);
3011 if (task.isHomeTask()) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003012 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG, "moveHomeTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003013 mTaskHistory.remove(taskNdx);
3014 mTaskHistory.add(top, task);
3015 mWindowManager.moveTaskToTop(task.taskId);
3016 return;
3017 }
3018 }
3019 }
3020
Craig Mautnercae015f2013-02-08 14:31:27 -08003021 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003022 final TaskRecord task = taskForIdLocked(taskId);
3023 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003024 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003025 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003026 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003027 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
3028 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07003029 // we'll just indicate that this task returns to the home task.
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003030 task.mOnTopOfHome = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08003031 }
3032 moveTaskToFrontLocked(task, null, options);
3033 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003034 }
3035 return false;
3036 }
3037
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003038 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003039 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003040
Craig Mautner11bf9a52013-02-19 14:08:51 -08003041 final int numTasks = mTaskHistory.size();
3042 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003043 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003044 // nothing to do!
3045 if (reason != null &&
3046 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3047 ActivityOptions.abort(options);
3048 } else {
3049 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3050 }
3051 return;
3052 }
3053
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003054 mStackSupervisor.moveHomeStack(isHomeStack());
3055
Craig Mautner11bf9a52013-02-19 14:08:51 -08003056 // Shift all activities with this task up to the top
3057 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003058 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003059
3060 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003061 if (reason != null &&
3062 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003063 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003064 ActivityRecord r = topRunningActivityLocked(null);
3065 if (r != null) {
3066 mNoAnimActivities.add(r);
3067 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003068 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003069 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003070 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003071 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003072
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003073 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003074
Craig Mautner05d29032013-05-03 13:40:13 -07003075 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003076 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003077
3078 if (VALIDATE_TOKENS) {
3079 validateAppTokensLocked();
3080 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003081 }
3082
3083 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003084 * Worker method for rearranging history stack. Implements the function of moving all
3085 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003086 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003087 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003088 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3089 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003090 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003091 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003092 * @return Returns true if the move completed, false if not.
3093 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003094 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
3095 Slog.i(TAG, "moveTaskToBack: " + taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003096
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003097 // If we have a watcher, preflight the move before committing to it. First check
3098 // for *other* available tasks, but if none are available, then try again allowing the
3099 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003100 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003101 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003102 if (next == null) {
3103 next = topRunningActivityLocked(null, 0);
3104 }
3105 if (next != null) {
3106 // ask watcher if this is allowed
3107 boolean moveOK = true;
3108 try {
3109 moveOK = mService.mController.activityResuming(next.packageName);
3110 } catch (RemoteException e) {
3111 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003112 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003113 }
3114 if (!moveOK) {
3115 return false;
3116 }
3117 }
3118 }
3119
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003120 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003121 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003122
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003123 final TaskRecord tr = taskForIdLocked(taskId);
Craig Mautnerd2328952013-03-05 12:46:26 -08003124 if (tr == null) {
3125 return false;
3126 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003127
Craig Mautner11bf9a52013-02-19 14:08:51 -08003128 mTaskHistory.remove(tr);
3129 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003130
Craig Mautnerc8143c62013-09-03 12:15:57 -07003131 // There is an assumption that moving a task to the back moves it behind the home activity.
3132 // We make sure here that some activity in the stack will launch home.
3133 ActivityRecord lastActivity = null;
3134 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003135 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3136 final TaskRecord task = mTaskHistory.get(taskNdx);
3137 if (task.mOnTopOfHome) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003138 break;
3139 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003140 if (taskNdx == 1) {
3141 // Set the last task before tr to go to home.
3142 task.mOnTopOfHome = true;
3143 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003144 }
3145
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003146 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003147 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3148 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003149 ActivityRecord r = topRunningActivityLocked(null);
3150 if (r != null) {
3151 mNoAnimActivities.add(r);
3152 }
3153 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003154 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003155 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003156 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003157
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003158 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003159 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003160 }
3161
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003162 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
3163 if (task == tr && task.mOnTopOfHome || numTasks <= 1) {
Daniel 2 Olofsson9cdf9e52013-12-16 13:24:25 +01003164 tr.mOnTopOfHome = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003165 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003166 }
3167
Craig Mautner05d29032013-05-03 13:40:13 -07003168 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003169 return true;
3170 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003171
Craig Mautner8849a5e2013-04-02 16:41:03 -07003172 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003173 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003174 final Uri data = r.intent.getData();
3175 final String strData = data != null ? data.toSafeString() : null;
3176
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003177 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003178 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003179 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003180 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003181 }
3182
3183 /**
3184 * Make sure the given activity matches the current configuration. Returns
3185 * false if the activity had to be destroyed. Returns true if the
3186 * configuration is the same, or the activity will remain running as-is
3187 * for whatever reason. Ensures the HistoryRecord is updated with the
3188 * correct configuration and all other bookkeeping is handled.
3189 */
3190 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3191 int globalChanges) {
3192 if (mConfigWillChange) {
3193 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3194 "Skipping config check (will change): " + r);
3195 return true;
3196 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003197
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003198 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3199 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003200
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003201 // Short circuit: if the two configurations are the exact same
3202 // object (the common case), then there is nothing to do.
3203 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003204 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003205 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3206 "Configuration unchanged in " + r);
3207 return true;
3208 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003209
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003210 // We don't worry about activities that are finishing.
3211 if (r.finishing) {
3212 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3213 "Configuration doesn't matter in finishing " + r);
3214 r.stopFreezingScreenLocked(false);
3215 return true;
3216 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003217
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003218 // Okay we now are going to make this activity have the new config.
3219 // But then we need to figure out how it needs to deal with that.
3220 Configuration oldConfig = r.configuration;
3221 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003222
3223 // Determine what has changed. May be nothing, if this is a config
3224 // that has come back from the app after going idle. In that case
3225 // we just want to leave the official config object now in the
3226 // activity and do nothing else.
3227 final int changes = oldConfig.diff(newConfig);
3228 if (changes == 0 && !r.forceNewConfig) {
3229 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3230 "Configuration no differences in " + r);
3231 return true;
3232 }
3233
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003234 // If the activity isn't currently running, just leave the new
3235 // configuration and it will pick that up next time it starts.
3236 if (r.app == null || r.app.thread == null) {
3237 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3238 "Configuration doesn't matter not running " + r);
3239 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003240 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003241 return true;
3242 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003243
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003244 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003245 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3246 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3247 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003248 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003249 + ", newConfig=" + newConfig);
3250 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003251 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003252 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3253 r.configChangeFlags |= changes;
3254 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003255 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003256 if (r.app == null || r.app.thread == null) {
3257 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003258 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003259 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003260 } else if (r.state == ActivityState.PAUSING) {
3261 // A little annoying: we are waiting for this activity to
3262 // finish pausing. Let's not do anything now, but just
3263 // flag that it needs to be restarted when done pausing.
3264 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003265 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003266 r.configDestroy = true;
3267 return true;
3268 } else if (r.state == ActivityState.RESUMED) {
3269 // Try to optimize this case: the configuration is changing
3270 // and we need to restart the top, resumed activity.
3271 // Instead of doing the normal handshaking, just say
3272 // "restart!".
3273 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003274 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003275 relaunchActivityLocked(r, r.configChangeFlags, true);
3276 r.configChangeFlags = 0;
3277 } else {
3278 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003279 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003280 relaunchActivityLocked(r, r.configChangeFlags, false);
3281 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003282 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003283
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003284 // All done... tell the caller we weren't able to keep this
3285 // activity around.
3286 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003287 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003288
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003289 // Default case: the activity can handle this new configuration, so
3290 // hand it over. Note that we don't need to give it the new
3291 // configuration, since we always send configuration changes to all
3292 // process when they happen so it can just use whatever configuration
3293 // it last got.
3294 if (r.app != null && r.app.thread != null) {
3295 try {
3296 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003297 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003298 } catch (RemoteException e) {
3299 // If process died, whatever.
3300 }
3301 }
3302 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003303
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003304 return true;
3305 }
3306
Craig Mautnerc8143c62013-09-03 12:15:57 -07003307 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003308 int changes, boolean andResume) {
3309 List<ResultInfo> results = null;
3310 List<Intent> newIntents = null;
3311 if (andResume) {
3312 results = r.results;
3313 newIntents = r.newIntents;
3314 }
3315 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3316 + " with results=" + results + " newIntents=" + newIntents
3317 + " andResume=" + andResume);
3318 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003319 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003320 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003321
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003322 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003323
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003324 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003325 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3326 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3327 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003328 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003329 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003330 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003331 // Note: don't need to call pauseIfSleepingLocked() here, because
3332 // the caller will only pass in 'andResume' if this activity is
3333 // currently resumed, which implies we aren't sleeping.
3334 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003335 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003336 }
3337
3338 if (andResume) {
3339 r.results = null;
3340 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003341 r.state = ActivityState.RESUMED;
3342 } else {
3343 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3344 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003345 }
3346
3347 return true;
3348 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003349
3350 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003351 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3352 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3353 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3354 final ActivityRecord r = activities.get(activityNdx);
3355 if (r.appToken == token) {
3356 return true;
3357 }
3358 if (r.fullscreen && !r.finishing) {
3359 return false;
3360 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003361 }
3362 }
3363 return true;
3364 }
3365
3366 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003367 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3368 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3369 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3370 final ActivityRecord r = activities.get(activityNdx);
3371 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003372 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003373 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003374 }
3375 }
3376 }
3377
3378 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3379 boolean didSomething = false;
3380 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003381 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3382 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3383 int numActivities = activities.size();
3384 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3385 ActivityRecord r = activities.get(activityNdx);
3386 final boolean samePackage = r.packageName.equals(name)
3387 || (name == null && r.userId == userId);
3388 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3389 && (samePackage || r.task == lastTask)
3390 && (r.app == null || evenPersistent || !r.app.persistent)) {
3391 if (!doit) {
3392 if (r.finishing) {
3393 // If this activity is just finishing, then it is not
3394 // interesting as far as something to stop.
3395 continue;
3396 }
3397 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003398 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003399 didSomething = true;
3400 Slog.i(TAG, " Force finishing activity " + r);
3401 if (samePackage) {
3402 if (r.app != null) {
3403 r.app.removed = true;
3404 }
3405 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003406 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003407 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003408 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3409 true)) {
3410 // r has been deleted from mActivities, accommodate.
3411 --numActivities;
3412 --activityNdx;
3413 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003414 }
3415 }
3416 }
3417 return didSomething;
3418 }
3419
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003420 ActivityRecord getTasksLocked(IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003421 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003422 ActivityRecord topRecord = null;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003423 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003424 final TaskRecord task = mTaskHistory.get(taskNdx);
3425 ActivityRecord r = null;
3426 ActivityRecord top = null;
3427 int numActivities = 0;
3428 int numRunning = 0;
3429 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003430 if (activities.isEmpty()) {
3431 continue;
3432 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003433 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3434 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003435
Craig Mautneraab647e2013-02-28 16:31:36 -08003436 // Initialize state for next task if needed.
3437 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3438 top = r;
3439 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003440 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003441
3442 // Add 'r' into the current task.
3443 numActivities++;
3444 if (r.app != null && r.app.thread != null) {
3445 numRunning++;
3446 }
3447
3448 if (localLOGV) Slog.v(
3449 TAG, r.intent.getComponent().flattenToShortString()
3450 + ": task=" + r.task);
3451 }
3452
3453 RunningTaskInfo ci = new RunningTaskInfo();
3454 ci.id = task.taskId;
3455 ci.baseActivity = r.intent.getComponent();
3456 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003457 ci.lastActiveTime = task.lastActiveTime;
3458
Craig Mautneraab647e2013-02-28 16:31:36 -08003459 if (top.thumbHolder != null) {
3460 ci.description = top.thumbHolder.lastDescription;
3461 }
3462 ci.numActivities = numActivities;
3463 ci.numRunning = numRunning;
3464 //System.out.println(
3465 // "#" + maxNum + ": " + " descr=" + ci.description);
3466 if (receiver != null) {
3467 if (localLOGV) Slog.v(
3468 TAG, "State=" + top.state + "Idle=" + top.idle
3469 + " app=" + top.app
3470 + " thr=" + (top.app != null ? top.app.thread : null));
3471 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3472 if (top.idle && top.app != null && top.app.thread != null) {
3473 topRecord = top;
3474 } else {
3475 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003476 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003477 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003478 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003479 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003480 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003481 }
3482 return topRecord;
3483 }
3484
3485 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003486 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003487 if (DEBUG_SWITCH) Slog.d(
3488 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003489 if (top >= 0) {
3490 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3491 int activityTop = activities.size() - 1;
3492 if (activityTop > 0) {
3493 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3494 "unhandled-back", true);
3495 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003496 }
3497 }
3498
Craig Mautner6b74cb52013-09-27 17:02:21 -07003499 /**
3500 * Reset local parameters because an app's activity died.
3501 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003502 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003503 */
3504 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003505 if (mPausingActivity != null && mPausingActivity.app == app) {
3506 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3507 "App died while pausing: " + mPausingActivity);
3508 mPausingActivity = null;
3509 }
3510 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3511 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003512 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003513 }
3514
Craig Mautner19091252013-10-05 00:03:53 -07003515 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003516 }
3517
Craig Mautnercae015f2013-02-08 14:31:27 -08003518 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003519 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3520 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3521 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3522 final ActivityRecord r = activities.get(activityNdx);
3523 if (r.app == app) {
3524 Slog.w(TAG, " Force finishing activity "
3525 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003526 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003527 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003528 }
3529 }
3530 }
3531
Dianne Hackborn390517b2013-05-30 15:03:32 -07003532 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003533 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003534 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003535 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3536 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003537 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3538 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003539 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003540 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003541 if (printed) {
3542 header = null;
3543 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003544 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003545 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003546 }
3547
3548 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3549 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3550
3551 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003552 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3553 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003554 }
3555 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003556 final int top = mTaskHistory.size() - 1;
3557 if (top >= 0) {
3558 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3559 int listTop = list.size() - 1;
3560 if (listTop >= 0) {
3561 activities.add(list.get(listTop));
3562 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003563 }
3564 } else {
3565 ItemMatcher matcher = new ItemMatcher();
3566 matcher.build(name);
3567
Craig Mautneraab647e2013-02-28 16:31:36 -08003568 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3569 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3570 if (matcher.match(r1, r1.intent.getComponent())) {
3571 activities.add(r1);
3572 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003573 }
3574 }
3575 }
3576
3577 return activities;
3578 }
3579
3580 ActivityRecord restartPackage(String packageName) {
3581 ActivityRecord starting = topRunningActivityLocked(null);
3582
3583 // All activities that came from the package must be
3584 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003585 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3586 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3587 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3588 final ActivityRecord a = activities.get(activityNdx);
3589 if (a.info.packageName.equals(packageName)) {
3590 a.forceNewConfig = true;
3591 if (starting != null && a == starting && a.visible) {
3592 a.startFreezingScreenLocked(starting.app,
3593 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3594 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003595 }
3596 }
3597 }
3598
3599 return starting;
3600 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003601
Craig Mautnerde4ef022013-04-07 19:01:33 -07003602 boolean removeTask(TaskRecord task) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003603 final int taskNdx = mTaskHistory.indexOf(task);
3604 final int topTaskNdx = mTaskHistory.size() - 1;
3605 if (task.mOnTopOfHome && taskNdx < topTaskNdx) {
3606 mTaskHistory.get(taskNdx + 1).mOnTopOfHome = true;
3607 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003608 mTaskHistory.remove(task);
Craig Mautnerc8143c62013-09-03 12:15:57 -07003609 return mTaskHistory.isEmpty();
Craig Mautner0247fc82013-02-28 14:32:06 -08003610 }
3611
Craig Mautnerde4ef022013-04-07 19:01:33 -07003612 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003613 TaskRecord task = new TaskRecord(taskId, info, intent);
3614 addTask(task, toTop);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003615 return task;
3616 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003617
3618 ArrayList<TaskRecord> getAllTasks() {
3619 return new ArrayList<TaskRecord>(mTaskHistory);
3620 }
3621
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003622 void addTask(final TaskRecord task, final boolean toTop) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003623 task.stack = this;
3624 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003625 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003626 } else {
3627 mTaskHistory.add(0, task);
3628 }
3629 }
3630
3631 public int getStackId() {
3632 return mStackId;
3633 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003634
3635 @Override
3636 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07003637 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
3638 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07003639 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003640}