blob: f4f39e27244b30896115baccfe24853a3fb936f9 [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
Craig Mautnerf4c909b2014-04-17 18:39:38 -070039import static com.android.server.am.ActivityStackSupervisor.ActivityContainer.CONTAINER_STATE_HAS_SURFACE;
40
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070041import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070042import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080043import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080044import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080045import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080046import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070047import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070048
49import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070050import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070051import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070052import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080053import android.app.IActivityController;
Craig Mautnercae015f2013-02-08 14:31:27 -080054import android.app.IThumbnailReceiver;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080056import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080062import android.content.res.Resources;
63import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080064import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070066import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080067import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070068import android.os.Handler;
69import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090070import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070071import android.os.Message;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070072import android.os.RemoteException;
73import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080074import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070075import android.os.UserHandle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070078import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079
Craig Mautnercae015f2013-02-08 14:31:27 -080080import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080081import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070082import java.lang.ref.WeakReference;
83import java.util.ArrayList;
84import java.util.Iterator;
85import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080086import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070087
88/**
89 * State and management of a single stack of activities.
90 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070091final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080092
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -070093 // Ticks during which we check progress while waiting for an app to launch.
94 static final int LAUNCH_TICK = 500;
95
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070096 // How long we wait until giving up on the last activity to pause. This
97 // is short because it directly impacts the responsiveness of starting the
98 // next activity.
99 static final int PAUSE_TIMEOUT = 500;
100
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700101 // How long we wait for the activity to tell us it has stopped before
102 // giving up. This is a good amount of time because we really need this
103 // from the application in order to get its saved state.
104 static final int STOP_TIMEOUT = 10*1000;
105
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700106 // How long we wait until giving up on an activity telling us it has
107 // finished destroying itself.
108 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800109
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700110 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800111 // disabled.
112 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800113
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700114 // How long between activity launches that we consider safe to not warn
115 // the user about an unexpected activity being launched on top.
116 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800117
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700118 // Set to false to disable the preview that is shown while a new activity
119 // is being started.
120 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800121
Craig Mautner5eda9b32013-07-02 11:58:16 -0700122 // How long to wait for all background Activities to redraw following a call to
123 // convertToTranslucent().
124 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
125
John Reck172e87c2013-10-02 16:55:16 -0700126 static final boolean SCREENSHOT_FORCE_565 = ActivityManager
127 .isLowRamDeviceStatic() ? true : false;
128
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700129 enum ActivityState {
130 INITIALIZING,
131 RESUMED,
132 PAUSING,
133 PAUSED,
134 STOPPING,
135 STOPPED,
136 FINISHING,
137 DESTROYING,
138 DESTROYED
139 }
140
141 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700142 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -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;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800232 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800233 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
234 ArrayList<ActivityStack> mStacks;
235 /** The attached Display's unique identifier, or -1 if detached */
236 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800237
Craig Mautner27084302013-03-25 08:05:25 -0700238 /** Run all ActivityStacks through this */
239 final ActivityStackSupervisor mStackSupervisor;
240
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700241 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700242 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
243 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
244 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
245 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700246 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700247
248 static class ScheduleDestroyArgs {
249 final ProcessRecord mOwner;
250 final boolean mOomAdj;
251 final String mReason;
252 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
253 mOwner = owner;
254 mOomAdj = oomAdj;
255 mReason = reason;
256 }
257 }
258
Zoran Marcetaf958b322012-08-09 20:27:12 +0900259 final Handler mHandler;
260
261 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700262 //public Handler() {
263 // if (localLOGV) Slog.v(TAG, "Handler started!");
264 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700265 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900266 super(looper);
267 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700268
Zoran Marcetaf958b322012-08-09 20:27:12 +0900269 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700270 public void handleMessage(Message msg) {
271 switch (msg.what) {
272 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800273 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700274 // We don't at this point know if the activity is fullscreen,
275 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800276 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700277 synchronized (mService) {
278 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700279 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700280 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700281 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800282 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700283 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700284 case LAUNCH_TICK_MSG: {
285 ActivityRecord r = (ActivityRecord)msg.obj;
286 synchronized (mService) {
287 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700288 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700289 }
290 }
291 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700292 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800293 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700294 // We don't at this point know if the activity is fullscreen,
295 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800296 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800297 synchronized (mService) {
298 activityDestroyedLocked(r != null ? r.appToken : null);
299 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700300 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700301 case STOP_TIMEOUT_MSG: {
302 ActivityRecord r = (ActivityRecord)msg.obj;
303 // We don't at this point know if the activity is fullscreen,
304 // so we need to be conservative and assume it isn't.
305 Slog.w(TAG, "Activity stop timeout for " + r);
306 synchronized (mService) {
307 if (r.isInHistory()) {
308 activityStoppedLocked(r, null, null, null);
309 }
310 }
311 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700312 case DESTROY_ACTIVITIES_MSG: {
313 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
314 synchronized (mService) {
315 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
316 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700317 } break;
318 case TRANSLUCENT_TIMEOUT_MSG: {
319 synchronized (mService) {
320 notifyActivityDrawnLocked(null);
321 }
322 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700323 }
324 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800325 }
326
Craig Mautner34b73df2014-01-12 21:11:08 -0800327 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800328 int count = 0;
329 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
330 count += mTaskHistory.get(taskNdx).mActivities.size();
331 }
332 return count;
333 }
334
Craig Mautner4a1cb222013-12-04 16:14:06 -0800335 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer) {
336 mActivityContainer = activityContainer;
337 mStackSupervisor = activityContainer.getOuter();
338 mService = mStackSupervisor.mService;
339 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
340 mWindowManager = mService.mWindowManager;
341 mStackId = activityContainer.mStackId;
342 mCurrentUser = mService.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700343 }
Craig Mautner5962b122012-10-05 14:45:52 -0700344
Craig Mautner6b74cb52013-09-27 17:02:21 -0700345 boolean okToShow(ActivityRecord r) {
Craig Mautner5962b122012-10-05 14:45:52 -0700346 return r.userId == mCurrentUser
347 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
348 }
349
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700350 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800351 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700352 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
353 if (r != null) {
354 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800355 }
356 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700357 return null;
358 }
359
360 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800361 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
362 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800363 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800364 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
365 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800366 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
367 return r;
368 }
369 }
370 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700371 return null;
372 }
373
374 /**
375 * This is a simplified version of topRunningActivityLocked that provides a number of
376 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800377 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700378 * @param token If non-null, any history records matching this token will be skipped.
379 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800380 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700381 * @return Returns the HistoryRecord of the next activity on the stack.
382 */
383 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800384 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
385 TaskRecord task = mTaskHistory.get(taskNdx);
386 if (task.taskId == taskId) {
387 continue;
388 }
389 ArrayList<ActivityRecord> activities = task.mActivities;
390 for (int i = activities.size() - 1; i >= 0; --i) {
391 final ActivityRecord r = activities.get(i);
392 // Note: the taskId check depends on real taskId fields being non-zero
393 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
394 return r;
395 }
396 }
397 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700398 return null;
399 }
400
Craig Mautner8849a5e2013-04-02 16:41:03 -0700401 final ActivityRecord topActivity() {
402 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700403 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700404 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
405 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
406 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700407 return activities.get(activityNdx);
Craig Mautner8849a5e2013-04-02 16:41:03 -0700408 }
409 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700410 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700411 }
412
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700413 final TaskRecord topTask() {
414 final int size = mTaskHistory.size();
415 if (size > 0) {
416 return mTaskHistory.get(size - 1);
417 }
418 return null;
419 }
420
Craig Mautnerd2328952013-03-05 12:46:26 -0800421 TaskRecord taskForIdLocked(int id) {
422 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
423 final TaskRecord task = mTaskHistory.get(taskNdx);
424 if (task.taskId == id) {
425 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800426 }
427 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700428 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700429 }
430
Craig Mautnerd2328952013-03-05 12:46:26 -0800431 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800432 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800433 if (r != null) {
434 final TaskRecord task = r.task;
435 if (task.mActivities.contains(r) && mTaskHistory.contains(task)) {
436 if (task.stack != this) Slog.w(TAG,
437 "Illegal state! task does not point to stack it is in.");
438 return r;
439 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800440 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800441 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800442 }
443
Craig Mautner2420ead2013-04-01 17:13:20 -0700444 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700445 final boolean hadit = mLRUActivities.remove(r);
446 mLRUActivities.add(r);
447 return hadit;
448 }
449
Craig Mautnerde4ef022013-04-07 19:01:33 -0700450 final boolean isHomeStack() {
451 return mStackId == HOME_STACK_ID;
452 }
453
Craig Mautnere0a38842013-12-16 16:14:02 -0800454 final boolean isOnHomeDisplay() {
455 return isAttached() &&
456 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
457 }
458
459 final void moveToFront() {
460 if (isAttached()) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800461 if (isOnHomeDisplay()) {
462 mStackSupervisor.moveHomeStack(isHomeStack());
463 }
Craig Mautnerfefeef82014-01-10 08:40:23 -0800464 mStacks.remove(this);
465 mStacks.add(this);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800466 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800467 }
468
469 final boolean isAttached() {
470 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800471 }
472
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700473 /**
474 * Returns the top activity in any existing task matching the given
475 * Intent. Returns null if no such task is found.
476 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700477 ActivityRecord findTaskLocked(ActivityRecord target) {
478 Intent intent = target.intent;
479 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700480 ComponentName cls = intent.getComponent();
481 if (info.targetActivity != null) {
482 cls = new ComponentName(info.packageName, info.targetActivity);
483 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700484 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner000f0022013-02-26 15:04:29 -0800485
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700486 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800487 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
488 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700489 if (task.userId != userId) {
490 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700491 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700492 continue;
493 }
Craig Mautner000f0022013-02-26 15:04:29 -0800494 final ActivityRecord r = task.getTopActivity();
495 if (r == null || r.finishing || r.userId != userId ||
496 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700497 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800498 continue;
499 }
500
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700501 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
502 + r.task.intent.getComponent().flattenToShortString()
503 + "/aff=" + r.task.affinity + " to new cls="
504 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Craig Mautner000f0022013-02-26 15:04:29 -0800505 if (task.affinity != null) {
506 if (task.affinity.equals(info.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700507 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700508 return r;
509 }
Craig Mautner000f0022013-02-26 15:04:29 -0800510 } else if (task.intent != null && task.intent.getComponent().equals(cls)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700511 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800512 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700513 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
514 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800515 return r;
516 } else if (task.affinityIntent != null
517 && task.affinityIntent.getComponent().equals(cls)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700518 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800519 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700520 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
521 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800522 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700523 } else if (DEBUG_TASKS) {
524 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700525 }
526 }
527
528 return null;
529 }
530
531 /**
532 * Returns the first activity (starting from the top of the stack) that
533 * is the same as the given activity. Returns null if no such activity
534 * is found.
535 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700536 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700537 ComponentName cls = intent.getComponent();
538 if (info.targetActivity != null) {
539 cls = new ComponentName(info.packageName, info.targetActivity);
540 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700541 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700542
Craig Mautner000f0022013-02-26 15:04:29 -0800543 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700544 TaskRecord task = mTaskHistory.get(taskNdx);
545 if (task.userId != mCurrentUser) {
546 return null;
547 }
548 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800549 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
550 ActivityRecord r = activities.get(activityNdx);
551 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700552 //Slog.i(TAG, "Found matching class!");
553 //dump();
554 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
555 return r;
556 }
557 }
558 }
559
560 return null;
561 }
562
Amith Yamasani742a6712011-05-04 14:49:28 -0700563 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700564 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700565 */
Craig Mautner93529a42013-10-04 15:03:13 -0700566 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800567 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700568 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800569 }
570 mCurrentUser = userId;
571
572 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800573 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800574 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700575 TaskRecord task = mTaskHistory.get(i);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800576 if (task.userId == userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700577 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
578 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800579 mTaskHistory.remove(i);
580 mTaskHistory.add(task);
581 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800582 // Use same value for i.
583 } else {
584 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800585 }
586 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700587 if (VALIDATE_TOKENS) {
588 validateAppTokensLocked();
589 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700590 }
591
Craig Mautner2420ead2013-04-01 17:13:20 -0700592 void minimalResumeActivityLocked(ActivityRecord r) {
593 r.state = ActivityState.RESUMED;
594 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
595 + " (starting new instance)");
596 r.stopped = false;
597 mResumedActivity = r;
598 r.task.touchActiveTime();
599 mService.addRecentTaskLocked(r.task);
600 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700601 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700602 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700603 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700604 }
605
Dianne Hackborncee04b52013-07-03 17:01:28 -0700606 private void startLaunchTraces() {
607 if (mFullyDrawnStartTime != 0) {
608 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
609 }
610 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
611 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
612 }
613
614 private void stopFullyDrawnTraceIfNeeded() {
615 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
616 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
617 mFullyDrawnStartTime = 0;
618 }
619 }
620
Craig Mautnere79d42682013-04-01 19:01:53 -0700621 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700622 if (r.displayStartTime == 0) {
623 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
624 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700625 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700626 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700627 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700628 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700629 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700630 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700631 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700632 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800633
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700634 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700635 // Make sure that there is no activity waiting for this to launch.
636 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
637 r.displayStartTime = r.fullyDrawnStartTime = 0;
638 } else {
639 mStackSupervisor.removeTimeoutsForActivityLocked(r);
640 mStackSupervisor.scheduleIdleTimeoutLocked(r);
641 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700642 }
643
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800644 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800645 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800646 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
647 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
648 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
649 activities.get(activityNdx).setSleeping(false);
650 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800651 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800652 }
653
Craig Mautner0eea92c2013-05-16 13:35:39 -0700654 /**
655 * @return true if something must be done before going to sleep.
656 */
657 boolean checkReadyForSleepLocked() {
658 if (mResumedActivity != null) {
659 // Still have something resumed; can't sleep until it is paused.
660 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
661 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
662 startPausingLocked(false, true);
663 return true;
664 }
665 if (mPausingActivity != null) {
666 // Still waiting for something to pause; can't sleep yet.
667 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
668 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800669 }
670
Craig Mautner0eea92c2013-05-16 13:35:39 -0700671 return false;
672 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800673
Craig Mautner0eea92c2013-05-16 13:35:39 -0700674 void goToSleep() {
675 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800676
Craig Mautner0eea92c2013-05-16 13:35:39 -0700677 // Make sure any stopped but visible activities are now sleeping.
678 // This ensures that the activity's onStop() is called.
679 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
680 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
681 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
682 final ActivityRecord r = activities.get(activityNdx);
683 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
684 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800685 }
686 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800687 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700688 }
Craig Mautner59c00972012-07-30 12:10:24 -0700689
Dianne Hackbornd2835932010-12-13 16:28:46 -0800690 public final Bitmap screenshotActivities(ActivityRecord who) {
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800691 if (who.noDisplay) {
692 return null;
693 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800694
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700695 TaskRecord tr = who.task;
Craig Mautner34740402013-10-11 11:05:35 -0700696 if (mService.getMostRecentTask() != tr && tr.intent != null &&
697 (tr.intent.getFlags() & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) != 0) {
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700698 // If this task is being excluded from recents, we don't want to take
699 // the expense of capturing a thumbnail, since we will never show it.
700 return null;
701 }
702
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800703 Resources res = mService.mContext.getResources();
704 int w = mThumbnailWidth;
705 int h = mThumbnailHeight;
706 if (w < 0) {
707 mThumbnailWidth = w =
708 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
709 mThumbnailHeight = h =
710 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
711 }
712
713 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800714 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700715 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800716 || mLastScreenshotBitmap.getWidth() != w
717 || mLastScreenshotBitmap.getHeight() != h) {
718 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700719 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700720 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800721 }
722 if (mLastScreenshotBitmap != null) {
John Reck172e87c2013-10-02 16:55:16 -0700723 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800724 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800725 }
726 return null;
727 }
728
Craig Mautnercf910b02013-04-23 11:23:27 -0700729 final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800730 if (mPausingActivity != null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800731 Slog.e(TAG, "Trying to pause when pause is already pending for "
Craig Mautnere11f2b72013-04-01 12:37:17 -0700732 + mPausingActivity, new RuntimeException("here").fillInStackTrace());
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800733 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700734 ActivityRecord prev = mResumedActivity;
735 if (prev == null) {
Craig Mautnere11f2b72013-04-01 12:37:17 -0700736 Slog.e(TAG, "Trying to pause when nothing is resumed",
737 new RuntimeException("here").fillInStackTrace());
Craig Mautner05d29032013-05-03 13:40:13 -0700738 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700739 return;
740 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800741
742 if (mActivityContainer.mParentActivity == null) {
743 // Top level stack, not a child. Look for child stacks.
744 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping);
745 }
746
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700747 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
748 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700749 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800750 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700751 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700752 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
753 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700754 prev.state = ActivityState.PAUSING;
755 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700756 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700757 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
758 if (next == null || next.task != prev.task) {
759 prev.updateThumbnail(screenshotActivities(prev), null);
760 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700761 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700762
763 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800764
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700765 if (prev.app != null && prev.app.thread != null) {
766 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
767 try {
768 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700769 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700770 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700771 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800772 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
773 userLeaving, prev.configChangeFlags);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700774 } catch (Exception e) {
775 // Ignore exception, if process died other code will cleanup.
776 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800777 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700778 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700779 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700780 }
781 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800782 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700783 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700784 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700785 }
786
787 // If we are not going to sleep, we want to ensure the device is
788 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700789 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700790 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700791 }
792
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800793 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700794 // Have the window manager pause its key dispatching until the new
795 // activity has started. If we're pausing the activity just because
796 // the screen is being turned off and the UI is sleeping, don't interrupt
797 // key dispatch; the same activity will pick it up again on wakeup.
798 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800799 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700800 } else {
801 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
802 }
803
804 // Schedule a pause timeout in case the app doesn't respond.
805 // We don't give it much time because this directly impacts the
806 // responsiveness seen by the user.
807 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
808 msg.obj = prev;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700809 prev.pauseTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700810 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
811 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
812 } else {
813 // This activity failed to schedule the
814 // pause, so just treat it as being paused now.
815 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700816 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700817 }
818 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700819
Craig Mautnerd2328952013-03-05 12:46:26 -0800820 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700821 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800822 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700823
Craig Mautnerd2328952013-03-05 12:46:26 -0800824 final ActivityRecord r = isInStackLocked(token);
825 if (r != null) {
826 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
827 if (mPausingActivity == r) {
828 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
829 + (timeout ? " (due to timeout)" : " (pause complete)"));
830 r.state = ActivityState.PAUSED;
831 completePauseLocked();
832 } else {
833 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
834 r.userId, System.identityHashCode(r), r.shortComponentName,
835 mPausingActivity != null
836 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700837 }
838 }
839 }
840
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700841 final void activityStoppedLocked(ActivityRecord r, Bundle icicle, Bitmap thumbnail,
842 CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700843 if (r.state != ActivityState.STOPPING) {
844 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
845 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
846 return;
847 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700848 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700849 if (icicle != null) {
850 // If icicle is null, this is happening due to a timeout, so we
851 // haven't really saved the state.
852 r.icicle = icicle;
853 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800854 r.launchCount = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700855 r.updateThumbnail(thumbnail, description);
856 }
857 if (!r.stopped) {
858 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
859 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
860 r.stopped = true;
861 r.state = ActivityState.STOPPED;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700862 if (r.finishing) {
863 r.clearOptionsLocked();
864 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700865 if (r.configDestroy) {
866 destroyActivityLocked(r, true, false, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700867 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700868 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700869 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800870 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700871 }
872 }
873 }
874
Craig Mautnerc8143c62013-09-03 12:15:57 -0700875 private void completePauseLocked() {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800876 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700877 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800878
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800879 if (prev != null) {
880 if (prev.finishing) {
881 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700882 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800883 } else if (prev.app != null) {
884 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
885 if (prev.waitingVisible) {
886 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700887 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800888 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
889 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800890 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800891 if (prev.configDestroy) {
892 // The previous is being paused because the configuration
893 // is changing, which means it is actually stopping...
894 // To juggle the fact that we are also starting a new
895 // instance right now, we need to first completely stop
896 // the current instance before starting the new one.
897 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
898 destroyActivityLocked(prev, true, false, "pause-config");
899 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700900 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700901 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
902 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800903 // If we already have a few activities waiting to stop,
904 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700905 // them out. Or if r is the last of activity of the last task the stack
906 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800907 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700908 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800909 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700910 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800911 }
912 }
913 } else {
914 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
915 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800916 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800917 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700918 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800919
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700920 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Craig Mautnere11f2b72013-04-01 12:37:17 -0700921 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner05d29032013-05-03 13:40:13 -0700922 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800923 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700924 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700925 ActivityRecord top = topStack.topRunningActivityLocked(null);
Dianne Hackborncc5a0552012-10-01 16:32:39 -0700926 if (top == null || (prev != null && top != prev)) {
927 // If there are no more activities available to run,
928 // do resume anyway to start something. Also if the top
929 // activity on the stack is not the just paused activity,
930 // we need to go ahead and resume it to ensure we complete
931 // an in-flight app switch.
Craig Mautner05d29032013-05-03 13:40:13 -0700932 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
Dianne Hackborn42e620c2012-06-24 13:20:51 -0700933 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700934 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800935
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800936 if (prev != null) {
937 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700938
Craig Mautner525f3d92013-05-07 14:01:50 -0700939 if (prev.app != null && prev.cpuTimeAtResume > 0
940 && mService.mBatteryStatsService.isOnBattery()) {
941 long diff;
Dianne Hackbornd2932242013-08-05 18:18:42 -0700942 synchronized (mService.mProcessCpuThread) {
943 diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
Craig Mautner525f3d92013-05-07 14:01:50 -0700944 - prev.cpuTimeAtResume;
945 }
946 if (diff > 0) {
947 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
948 synchronized (bsi) {
949 BatteryStatsImpl.Uid.Proc ps =
950 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -0700951 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -0700952 if (ps != null) {
953 ps.addForegroundTimeLocked(diff);
954 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700955 }
956 }
957 }
Craig Mautner525f3d92013-05-07 14:01:50 -0700958 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700959 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700960 }
961
962 /**
963 * Once we know that we have asked an application to put an activity in
964 * the resumed state (either by launching it or explicitly telling it),
965 * this function updates the rest of our state to match that fact.
966 */
Craig Mautner525f3d92013-05-07 14:01:50 -0700967 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700968 next.idle = false;
969 next.results = null;
970 next.newIntents = null;
Craig Mautner07566322013-09-26 16:42:55 -0700971 if (next.nowVisible) {
972 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
973 mStackSupervisor.dismissKeyguard();
974 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700975
976 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -0700977 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700978
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700979 mStackSupervisor.reportResumedActivityLocked(next);
980
981 next.resumeKeyDispatchingLocked();
982 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700983
984 // Mark the point when the activity is resuming
985 // TODO: To be more accurate, the mark should be before the onCreate,
986 // not after the onResume. But for subsequent starts, onResume is fine.
987 if (next.app != null) {
Dianne Hackbornd2932242013-08-05 18:18:42 -0700988 synchronized (mService.mProcessCpuThread) {
989 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700990 }
991 } else {
992 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
993 }
994 }
995
Craig Mautner580ea812013-04-25 12:58:38 -0700996 /**
Craig Mautner39e1c5a2013-10-23 15:14:22 -0700997 * Determine if home should be visible below the passed record.
998 * @param record activity we are querying for.
999 * @return true if home is visible below the passed activity, false otherwise.
1000 */
1001 boolean isActivityOverHome(ActivityRecord record) {
1002 // Start at record and go down, look for either home or a visible fullscreen activity.
1003 final TaskRecord recordTask = record.task;
1004 for (int taskNdx = mTaskHistory.indexOf(recordTask); taskNdx >= 0; --taskNdx) {
1005 TaskRecord task = mTaskHistory.get(taskNdx);
1006 final ArrayList<ActivityRecord> activities = task.mActivities;
1007 final int startNdx =
1008 task == recordTask ? activities.indexOf(record) : activities.size() - 1;
1009 for (int activityNdx = startNdx; activityNdx >= 0; --activityNdx) {
1010 final ActivityRecord r = activities.get(activityNdx);
1011 if (r.isHomeActivity()) {
1012 return true;
1013 }
Craig Mautner76be9d22013-11-04 16:01:22 -08001014 if (!r.finishing && r.fullscreen) {
1015 // Passed activity is over a fullscreen activity.
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001016 return false;
1017 }
1018 }
1019 if (task.mOnTopOfHome) {
1020 // Got to the bottom of a task on top of home without finding a visible fullscreen
1021 // activity. Home is visible.
1022 return true;
1023 }
1024 }
1025 // Got to the bottom of this stack and still don't know. If this is over the home stack
1026 // then record is over home. May not work if we ever get more than two layers.
1027 return mStackSupervisor.isFrontStack(this);
1028 }
1029
Craig Mautnere3a00d72014-04-16 08:31:19 -07001030 private void setVisibile(ActivityRecord r, boolean visible) {
1031 r.visible = visible;
1032 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001033 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001034 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001035 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001036 container.setVisible(visible);
1037 }
1038 }
1039
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001040 /**
Craig Mautner580ea812013-04-25 12:58:38 -07001041 * Version of ensureActivitiesVisible that can easily be called anywhere.
1042 */
1043 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1044 return ensureActivitiesVisibleLocked(starting, configChanges, false);
1045 }
1046
1047 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1048 boolean forceHomeShown) {
1049 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -07001050 return r != null &&
1051 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -07001052 }
1053
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001054 /**
1055 * Make sure that all activities that need to be visible (that is, they
1056 * currently can be seen by the user) actually are.
1057 */
Craig Mautner580ea812013-04-25 12:58:38 -07001058 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
1059 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001060 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001061 TAG, "ensureActivitiesVisible behind " + top
1062 + " configChanges=0x" + Integer.toHexString(configChanges));
1063
Craig Mautner5eda9b32013-07-02 11:58:16 -07001064 if (mTranslucentActivityWaiting != top) {
1065 mUndrawnActivitiesBelowTopTranslucent.clear();
1066 if (mTranslucentActivityWaiting != null) {
1067 // Call the callback with a timeout indication.
1068 notifyActivityDrawnLocked(null);
1069 mTranslucentActivityWaiting = null;
1070 }
1071 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1072 }
1073
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001074 // If the top activity is not fullscreen, then we need to
1075 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001076 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -07001077 boolean showHomeBehindStack = false;
1078 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
1079 !(forceHomeShown && isHomeStack());
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001080 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001081 final TaskRecord task = mTaskHistory.get(taskNdx);
1082 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001083 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1084 final ActivityRecord r = activities.get(activityNdx);
1085 if (r.finishing) {
1086 continue;
1087 }
1088 if (aboveTop && r != top) {
1089 continue;
1090 }
1091 aboveTop = false;
1092 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001093 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001094 TAG, "Make visible? " + r + " finishing=" + r.finishing
1095 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001096
Craig Mautnerd44711d2013-02-23 11:24:36 -08001097 final boolean doThisProcess = onlyThisProcess == null
1098 || onlyThisProcess.equals(r.processName);
1099
1100 // First: if this is not the current activity being started, make
1101 // sure it matches the current configuration.
1102 if (r != starting && doThisProcess) {
1103 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001104 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001105
1106 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001107 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001108 // This activity needs to be visible, but isn't even
1109 // running... get it started, but don't resume it
1110 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001111 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001112 if (r != starting) {
1113 r.startFreezingScreenLocked(r.app, configChanges);
1114 }
1115 if (!r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001116 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001117 TAG, "Starting and making visible: " + r);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001118 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001119 }
1120 if (r != starting) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001121 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001122 }
1123 }
1124
1125 } else if (r.visible) {
1126 // If this activity is already visible, then there is nothing
1127 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001128 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001129 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001130
1131 } else if (onlyThisProcess == null) {
1132 // This activity is not currently visible, but is running.
1133 // Tell it to become visible.
1134 r.visible = true;
1135 if (r.state != ActivityState.RESUMED && r != starting) {
1136 // If this activity is paused, tell it
1137 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001138 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001139 TAG, "Making visible and scheduling visibility: " + r);
1140 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001141 if (mTranslucentActivityWaiting != null) {
1142 mUndrawnActivitiesBelowTopTranslucent.add(r);
1143 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001144 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001145 r.sleeping = false;
1146 r.app.pendingUiClean = true;
1147 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1148 r.stopFreezingScreenLocked(false);
1149 } catch (Exception e) {
1150 // Just skip on any failure; we'll make it
1151 // visible when it next restarts.
1152 Slog.w(TAG, "Exception thrown making visibile: "
1153 + r.intent.getComponent(), e);
1154 }
1155 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001156 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001157
Craig Mautnerd44711d2013-02-23 11:24:36 -08001158 // Aggregate current change flags.
1159 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001160
Craig Mautnerd44711d2013-02-23 11:24:36 -08001161 if (r.fullscreen) {
1162 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001163 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1164 behindFullscreen = true;
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001165 } else if (isActivityOverHome(r)) {
1166 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1167 showHomeBehindStack = true;
riddle_hsu446ef1d2014-01-09 20:24:34 +08001168 behindFullscreen = !isHomeStack() && r.frontOfTask && task.mOnTopOfHome;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001169 }
1170 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001171 if (DEBUG_VISBILITY) Slog.v(
1172 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1173 + " state=" + r.state
1174 + " behindFullscreen=" + behindFullscreen);
1175 // Now for any activities that aren't visible to the user, make
1176 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001177 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001178 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001179 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001180 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001181 switch (r.state) {
1182 case STOPPING:
1183 case STOPPED:
1184 if (r.app != null && r.app.thread != null) {
1185 if (DEBUG_VISBILITY) Slog.v(
1186 TAG, "Scheduling invisibility: " + r);
1187 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1188 }
1189 break;
1190
1191 case INITIALIZING:
1192 case RESUMED:
1193 case PAUSING:
1194 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001195 // This case created for transitioning activities from
1196 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautnere5273b42013-09-09 12:57:47 -07001197 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1198 mStackSupervisor.mStoppingActivities.add(r);
1199 }
1200 mStackSupervisor.scheduleIdleLocked();
Craig Mautner4addfc52013-06-25 08:05:45 -07001201 break;
1202
1203 default:
1204 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001205 }
1206 } catch (Exception e) {
1207 // Just skip on any failure; we'll make it
1208 // visible when it next restarts.
1209 Slog.w(TAG, "Exception thrown making hidden: "
1210 + r.intent.getComponent(), e);
1211 }
1212 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001213 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001214 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001215 }
1216 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001217 }
Craig Mautner580ea812013-04-25 12:58:38 -07001218 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001219 }
Craig Mautner58547802013-03-05 08:23:53 -08001220
Craig Mautner5eda9b32013-07-02 11:58:16 -07001221 void convertToTranslucent(ActivityRecord r) {
1222 mTranslucentActivityWaiting = r;
1223 mUndrawnActivitiesBelowTopTranslucent.clear();
1224 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1225 }
1226
1227 /**
1228 * Called as activities below the top translucent activity are redrawn. When the last one is
1229 * redrawn notify the top activity by calling
1230 * {@link Activity#onTranslucentConversionComplete}.
1231 *
1232 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1233 * occurred and the activity will be notified immediately.
1234 */
1235 void notifyActivityDrawnLocked(ActivityRecord r) {
1236 if ((r == null)
1237 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1238 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1239 // The last undrawn activity below the top has just been drawn. If there is an
1240 // opaque activity at the top, notify it that it can become translucent safely now.
1241 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1242 mTranslucentActivityWaiting = null;
1243 mUndrawnActivitiesBelowTopTranslucent.clear();
1244 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1245
Craig Mautner71dd1b62014-02-18 15:48:52 -08001246 if (waitingActivity != null) {
1247 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1248 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1249 try {
1250 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1251 waitingActivity.appToken, r != null);
1252 } catch (RemoteException e) {
1253 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001254 }
1255 }
1256 }
1257 }
1258
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001259 /**
1260 * Ensure that the top activity in the stack is resumed.
1261 *
1262 * @param prev The previously resumed activity, for when in the process
1263 * of pausing; can be null to call from elsewhere.
1264 *
1265 * @return Returns true if something is being resumed, or false if
1266 * nothing happened.
1267 */
1268 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001269 return resumeTopActivityLocked(prev, null);
1270 }
1271
1272 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001273 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1274
Craig Mautnerdf88d732014-01-27 09:21:32 -08001275 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001276 if ((parent != null && parent.state != ActivityState.RESUMED) ||
1277 mActivityContainer.mContainerState != CONTAINER_STATE_HAS_SURFACE) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001278 // Do not resume this stack if its parent is not resumed.
1279 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1280 return false;
1281 }
1282
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001283 // Find the first activity that is not finishing.
1284 ActivityRecord next = topRunningActivityLocked(null);
1285
1286 // Remember how we'll process this pause/resume situation, and ensure
1287 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001288 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1289 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001290
1291 if (next == null) {
1292 // There are no more activities! Let's just start up the
1293 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001294 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001295 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001296 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001297 // Only resume home if on home display
1298 return isOnHomeDisplay() && mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001299 }
1300
1301 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001302
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001303 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001304 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1305 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001306 // Make sure we have executed any pending transitions, since there
1307 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001308 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001309 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001310 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001311 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001312 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001313 return false;
1314 }
1315
Craig Mautner525f3d92013-05-07 14:01:50 -07001316 final TaskRecord nextTask = next.task;
1317 final TaskRecord prevTask = prev != null ? prev.task : null;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001318 if (prevTask != null && prevTask.mOnTopOfHome && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001319 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001320 if (prevTask == nextTask) {
Craig Mautner3b475fe2013-12-16 15:58:31 -08001321 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001322 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001323 // This task is going away but it was supposed to return to the home task.
1324 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001325 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001326 mTaskHistory.get(taskNdx).mOnTopOfHome = true;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001327 } else {
Craig Mautnere0a38842013-12-16 16:14:02 -08001328 if (DEBUG_STATES && isOnHomeDisplay()) Slog.d(TAG,
1329 "resumeTopActivityLocked: Launching home next");
1330 // Only resume home if on home display
1331 return isOnHomeDisplay() && mStackSupervisor.resumeHomeActivity(prev);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001332 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001333 }
1334
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001335 // If we are sleeping, and there is no resumed activity, and the top
1336 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001337 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001338 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001339 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001340 // Make sure we have executed any pending transitions, since there
1341 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001342 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001343 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001344 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001345 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001346 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001347 return false;
1348 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001349
1350 // Make sure that the user who owns this activity is started. If not,
1351 // we will just leave it as is because someone should be bringing
1352 // another user's activities to the top of the stack.
1353 if (mService.mStartedUsers.get(next.userId) == null) {
1354 Slog.w(TAG, "Skipping resume of top activity " + next
1355 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001356 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001357 return false;
1358 }
1359
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001360 // The activity may be waiting for stop, but that is no longer
1361 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001362 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001363 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001364 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001365 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001366
Dianne Hackborn84375872012-06-01 19:03:50 -07001367 next.updateOptionsLocked(options);
1368
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001369 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1370
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001371 // If we are currently pausing an activity, then don't do anything
1372 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001373 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001374 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1375 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001376 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001377 return false;
1378 }
1379
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001380 // Okay we are now going to start a switch, to 'next'. We may first
1381 // have to pause the current activity, but this is an important point
1382 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001383 // XXX "App Redirected" dialog is getting too many false positives
1384 // at this point, so turn off for now.
1385 if (false) {
1386 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1387 long now = SystemClock.uptimeMillis();
1388 final boolean inTime = mLastStartedActivity.startTime != 0
1389 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1390 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1391 final int nextUid = next.info.applicationInfo.uid;
1392 if (inTime && lastUid != nextUid
1393 && lastUid != next.launchedFromUid
1394 && mService.checkPermission(
1395 android.Manifest.permission.STOP_APP_SWITCHES,
1396 -1, next.launchedFromUid)
1397 != PackageManager.PERMISSION_GRANTED) {
1398 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1399 } else {
1400 next.startTime = now;
1401 mLastStartedActivity = next;
1402 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001403 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001404 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001405 mLastStartedActivity = next;
1406 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001407 }
Craig Mautner58547802013-03-05 08:23:53 -08001408
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001409 // We need to start pausing the current activity so the top one
1410 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001411 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001412 if (mResumedActivity != null) {
1413 pausing = true;
1414 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001415 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001416 }
1417 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001418 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1419 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001420 // At this point we want to put the upcoming activity's process
1421 // at the top of the LRU list, since we know we will be needing it
1422 // very soon and it would be a waste to let it get killed if it
1423 // happens to be sitting towards the end.
1424 if (next.app != null && next.app.thread != null) {
1425 // No reason to do full oom adj update here; we'll let that
1426 // happen whenever it needs to later.
Dianne Hackborndb926082013-10-31 16:32:44 -07001427 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001428 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001429 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001430 return true;
1431 }
1432
Christopher Tated3f175c2012-06-14 14:16:54 -07001433 // If the most recent activity was noHistory but was only stopped rather
1434 // than stopped+finished because the device went to sleep, we need to make
1435 // sure to finish it as we're making a new activity topmost.
Craig Mautner0f922742013-08-06 08:44:42 -07001436 if (mService.mSleeping && mLastNoHistoryActivity != null &&
1437 !mLastNoHistoryActivity.finishing) {
1438 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1439 " on new resume");
1440 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1441 null, "no-history", false);
1442 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001443 }
1444
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001445 if (prev != null && prev != next) {
1446 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1447 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001448 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001449 if (DEBUG_SWITCH) Slog.v(
1450 TAG, "Resuming top, waiting visible to hide: " + prev);
1451 } else {
1452 // The next activity is already visible, so hide the previous
1453 // activity's windows right now so we can show the new one ASAP.
1454 // We only do this if the previous is finishing, which should mean
1455 // it is on top of the one being resumed so hiding it quickly
1456 // is good. Otherwise, we want to do the normal route of allowing
1457 // the resumed activity to be shown so we can decide if the
1458 // previous should actually be hidden depending on whether the
1459 // new one is found to be full-screen or not.
1460 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001461 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001462 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1463 + prev + ", waitingVisible="
1464 + (prev != null ? prev.waitingVisible : null)
1465 + ", nowVisible=" + next.nowVisible);
1466 } else {
1467 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1468 + prev + ", waitingVisible="
1469 + (prev != null ? prev.waitingVisible : null)
1470 + ", nowVisible=" + next.nowVisible);
1471 }
1472 }
1473 }
1474
Dianne Hackborne7f97212011-02-24 14:40:20 -08001475 // Launching this app's activity, make sure the app is no longer
1476 // considered stopped.
1477 try {
1478 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001479 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001480 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001481 } catch (IllegalArgumentException e) {
1482 Slog.w(TAG, "Failed trying to unstop package "
1483 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001484 }
1485
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001486 // We are starting up the next activity, so tell the window manager
1487 // that the previous one will be hidden soon. This way it can know
1488 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001489 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001490 if (prev != null) {
1491 if (prev.finishing) {
1492 if (DEBUG_TRANSITION) Slog.v(TAG,
1493 "Prepare close transition: prev=" + prev);
1494 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001495 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001496 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001497 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001498 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001499 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1500 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001501 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001502 mWindowManager.setAppWillBeHidden(prev.appToken);
1503 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001504 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001505 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001506 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001507 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001508 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001509 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001510 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001511 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1512 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001513 }
1514 }
1515 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001516 mWindowManager.setAppWillBeHidden(prev.appToken);
1517 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001518 }
Craig Mautner967212c2013-04-13 21:10:58 -07001519 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001520 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001521 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001522 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001523 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001524 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001525 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001526 }
1527 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001528 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001529 next.applyOptionsLocked();
1530 } else {
1531 next.clearOptionsLocked();
1532 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001533
Craig Mautnercf910b02013-04-23 11:23:27 -07001534 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001535 if (next.app != null && next.app.thread != null) {
1536 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1537
1538 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001539 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001540
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001541 // schedule launch ticks to collect information about slow apps.
1542 next.startLaunchTickingLocked();
1543
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001544 ActivityRecord lastResumedActivity =
1545 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001546 ActivityState lastState = next.state;
1547
1548 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001549
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001550 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001551 next.state = ActivityState.RESUMED;
1552 mResumedActivity = next;
1553 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001554 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001555 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001556 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001557 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001558
1559 // Have the window manager re-evaluate the orientation of
1560 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001561 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001562 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001563 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001564 mService.mConfiguration,
1565 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1566 if (config != null) {
1567 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001568 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001569 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001570 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001571
Craig Mautner525f3d92013-05-07 14:01:50 -07001572 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001573 // The configuration update wasn't able to keep the existing
1574 // instance of the activity, and instead started a new one.
1575 // We should be all done, but let's just make sure our activity
1576 // is still at the top and schedule another run if something
1577 // weird happened.
1578 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001579 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001580 "Activity config changed during resume: " + next
1581 + ", new next: " + nextNext);
1582 if (nextNext != next) {
1583 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001584 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001585 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001586 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001587 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001588 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001589 return true;
1590 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001591 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001592 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001593 }
Craig Mautner58547802013-03-05 08:23:53 -08001594
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001595 try {
1596 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001597 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001598 if (a != null) {
1599 final int N = a.size();
1600 if (!next.finishing && N > 0) {
1601 if (DEBUG_RESULTS) Slog.v(
1602 TAG, "Delivering results to " + next
1603 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001604 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001605 }
1606 }
1607
1608 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001609 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001610 }
1611
1612 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001613 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001614 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001615
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001616 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001617 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001618 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001619 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
1620 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001621 mService.isNextTransitionForward());
Craig Mautner58547802013-03-05 08:23:53 -08001622
Craig Mautner0eea92c2013-05-16 13:35:39 -07001623 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001624
Craig Mautnerac6f8432013-07-17 13:24:59 -07001625 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001626 } catch (Exception e) {
1627 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001628 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1629 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001630 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001631 if (lastStack != null) {
1632 lastStack.mResumedActivity = lastResumedActivity;
1633 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001634 Slog.i(TAG, "Restarting because process died: " + next);
1635 if (!next.hasBeenLaunched) {
1636 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001637 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1638 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001639 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001640 next.appToken, next.packageName, next.theme,
1641 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001642 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1643 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001644 }
Craig Mautnere79d42682013-04-01 19:01:53 -07001645 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001646 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001647 return true;
1648 }
1649
1650 // From this point on, if something goes wrong there is no way
1651 // to recover the activity.
1652 try {
1653 next.visible = true;
1654 completeResumeLocked(next);
1655 } catch (Exception e) {
1656 // If any exception gets thrown, toss away this
1657 // activity and try the next one.
1658 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001659 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001660 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001661 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001662 return true;
1663 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001664 next.stopped = false;
1665
1666 } else {
1667 // Whoops, need to restart this activity!
1668 if (!next.hasBeenLaunched) {
1669 next.hasBeenLaunched = true;
1670 } else {
1671 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001672 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001673 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001674 mService.compatibilityInfoForPackageLocked(
1675 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001676 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001677 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001678 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001679 }
1680 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1681 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001682 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
Craig Mautnere79d42682013-04-01 19:01:53 -07001683 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001684 }
1685
Craig Mautnercf910b02013-04-23 11:23:27 -07001686 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001687 return true;
1688 }
1689
Craig Mautnerac6f8432013-07-17 13:24:59 -07001690 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001691 // If this is being moved to the top by another activity or being launched from the home
1692 // activity, set mOnTopOfHome accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001693 if (isOnHomeDisplay()) {
1694 ActivityStack lastStack = mStackSupervisor.getLastStack();
1695 final boolean fromHome = lastStack.isHomeStack();
1696 if (!isHomeStack() && (fromHome || topTask() != task)) {
1697 task.mOnTopOfHome = fromHome;
1698 }
1699 } else {
1700 task.mOnTopOfHome = false;
Craig Mautner9c85c202013-10-04 20:11:26 -07001701 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001702
Craig Mautnerac6f8432013-07-17 13:24:59 -07001703 mTaskHistory.remove(task);
1704 // Now put task at top.
1705 int stackNdx = mTaskHistory.size();
1706 if (task.userId != mCurrentUser) {
1707 // Put non-current user tasks below current user tasks.
1708 while (--stackNdx >= 0) {
1709 if (mTaskHistory.get(stackNdx).userId != mCurrentUser) {
1710 break;
1711 }
1712 }
1713 ++stackNdx;
1714 }
1715 mTaskHistory.add(stackNdx, task);
1716 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001717
Craig Mautner8849a5e2013-04-02 16:41:03 -07001718 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001719 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001720 TaskRecord rTask = r.task;
1721 final int taskId = rTask.taskId;
1722 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001723 // Last activity in task had been removed or ActivityManagerService is reusing task.
1724 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001725 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001726 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001727 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001728 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001729 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001730 if (!newTask) {
1731 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001732 boolean startIt = true;
1733 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1734 task = mTaskHistory.get(taskNdx);
1735 if (task == r.task) {
1736 // Here it is! Now, if this is not yet visible to the
1737 // user, then just add it without starting; it will
1738 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001739 if (!startIt) {
1740 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1741 + task, new RuntimeException("here").fillInStackTrace());
1742 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001743 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001744 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1745 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001746 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001747 r.userId, r.info.configChanges);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001748 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001749 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001750 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001751 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001752 return;
1753 }
1754 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001755 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001756 startIt = false;
1757 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001758 }
1759 }
1760
1761 // Place a new activity at top of stack, so it is next to interact
1762 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001763
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001764 // If we are not placing the new activity frontmost, we do not want
1765 // to deliver the onUserLeaving callback to the actual frontmost
1766 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001767 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001768 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001769 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1770 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001771 }
Craig Mautner70a86932013-02-28 22:37:44 -08001772
1773 task = r.task;
1774
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001775 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001776 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001777 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001778 task.addActivityToTop(r);
Craig Mautner3b475fe2013-12-16 15:58:31 -08001779 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08001780
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001781 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001782 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001783 // We want to show the starting preview window if we are
1784 // switching to a new task, or the next activity's process is
1785 // not currently running.
1786 boolean showStartingIcon = newTask;
1787 ProcessRecord proc = r.app;
1788 if (proc == null) {
1789 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1790 }
1791 if (proc == null || proc.thread == null) {
1792 showStartingIcon = true;
1793 }
1794 if (DEBUG_TRANSITION) Slog.v(TAG,
1795 "Prepare open transition: starting " + r);
1796 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001797 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001798 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001799 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001800 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001801 ? AppTransition.TRANSIT_TASK_OPEN
1802 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001803 mNoAnimActivities.remove(r);
1804 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001805 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001806 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001807 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001808 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
1809 r.info.configChanges);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001810 boolean doShow = true;
1811 if (newTask) {
1812 // Even though this activity is starting fresh, we still need
1813 // to reset it to make sure we apply affinities to move any
1814 // existing activities from other tasks in to it.
1815 // If the caller has requested that the target task be
1816 // reset, then do so.
1817 if ((r.intent.getFlags()
1818 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1819 resetTaskIfNeededLocked(r, r);
1820 doShow = topRunningNonDelayedActivityLocked(null) == r;
1821 }
1822 }
1823 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1824 // Figure out if we are transitioning from another activity that is
1825 // "has the same starting icon" as the next one. This allows the
1826 // window manager to keep the previous window it had previously
1827 // created, if it still had one.
1828 ActivityRecord prev = mResumedActivity;
1829 if (prev != null) {
1830 // We don't want to reuse the previous starting preview if:
1831 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001832 if (prev.task != r.task) {
1833 prev = null;
1834 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001835 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001836 else if (prev.nowVisible) {
1837 prev = null;
1838 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001839 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001840 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001841 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001842 mService.compatibilityInfoForPackageLocked(
1843 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001844 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001845 prev != null ? prev.appToken : null, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001846 }
1847 } else {
1848 // If this is the first activity, don't do any fancy animations,
1849 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001850 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001851 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001852 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
1853 r.info.configChanges);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001854 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001855 }
1856 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001857 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001858 }
1859
1860 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001861 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001862 }
1863 }
1864
Dianne Hackbornbe707852011-11-11 14:32:10 -08001865 final void validateAppTokensLocked() {
1866 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001867 mValidateAppTokens.ensureCapacity(numActivities());
1868 final int numTasks = mTaskHistory.size();
1869 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1870 TaskRecord task = mTaskHistory.get(taskNdx);
1871 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07001872 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08001873 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001874 }
Craig Mautner000f0022013-02-26 15:04:29 -08001875 TaskGroup group = new TaskGroup();
1876 group.taskId = task.taskId;
1877 mValidateAppTokens.add(group);
1878 final int numActivities = activities.size();
1879 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1880 final ActivityRecord r = activities.get(activityNdx);
1881 group.tokens.add(r.appToken);
1882 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001883 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001884 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001885 }
1886
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001887 /**
1888 * Perform a reset of the given task, if needed as part of launching it.
1889 * Returns the new HistoryRecord at the top of the task.
1890 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001891 /**
1892 * Helper method for #resetTaskIfNeededLocked.
1893 * We are inside of the task being reset... we'll either finish this activity, push it out
1894 * for another task, or leave it as-is.
1895 * @param task The task containing the Activity (taskTop) that might be reset.
1896 * @param forceReset
1897 * @return An ActivityOptions that needs to be processed.
1898 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001899 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001900 ActivityOptions topOptions = null;
1901
1902 int replyChainEnd = -1;
1903 boolean canMoveOptions = true;
1904
1905 // We only do this for activities that are not the root of the task (since if we finish
1906 // the root, we may no longer have the task!).
1907 final ArrayList<ActivityRecord> activities = task.mActivities;
1908 final int numActivities = activities.size();
1909 for (int i = numActivities - 1; i > 0; --i ) {
1910 ActivityRecord target = activities.get(i);
1911
1912 final int flags = target.info.flags;
1913 final boolean finishOnTaskLaunch =
1914 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1915 final boolean allowTaskReparenting =
1916 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1917 final boolean clearWhenTaskReset =
1918 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1919
1920 if (!finishOnTaskLaunch
1921 && !clearWhenTaskReset
1922 && target.resultTo != null) {
1923 // If this activity is sending a reply to a previous
1924 // activity, we can't do anything with it now until
1925 // we reach the start of the reply chain.
1926 // XXX note that we are assuming the result is always
1927 // to the previous activity, which is almost always
1928 // the case but we really shouldn't count on.
1929 if (replyChainEnd < 0) {
1930 replyChainEnd = i;
1931 }
1932 } else if (!finishOnTaskLaunch
1933 && !clearWhenTaskReset
1934 && allowTaskReparenting
1935 && target.taskAffinity != null
1936 && !target.taskAffinity.equals(task.affinity)) {
1937 // If this activity has an affinity for another
1938 // task, then we need to move it out of here. We will
1939 // move it as far out of the way as possible, to the
1940 // bottom of the activity stack. This also keeps it
1941 // correctly ordered with any activities we previously
1942 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08001943 final ThumbnailHolder newThumbHolder;
1944 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07001945 final ActivityRecord bottom =
1946 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08001947 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07001948 if (bottom != null && target.taskAffinity != null
1949 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001950 // If the activity currently at the bottom has the
1951 // same task affinity as the one we are moving,
1952 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08001953 targetTask = bottom.task;
1954 newThumbHolder = bottom.thumbHolder == null ? targetTask : bottom.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001955 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07001956 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001957 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08001958 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
1959 null, false);
1960 newThumbHolder = targetTask;
1961 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001962 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1963 + " out to new task " + target.task);
1964 }
1965
Craig Mautner329f4122013-11-07 09:10:42 -08001966 if (clearWhenTaskReset) {
1967 // This is the start of a new sub-task.
1968 if (target.thumbHolder == null) {
1969 target.thumbHolder = new ThumbnailHolder();
1970 }
1971 } else {
1972 target.thumbHolder = newThumbHolder;
1973 }
1974
Craig Mautnere3a74d52013-02-22 14:14:58 -08001975 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001976 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001977
Craig Mautner525f3d92013-05-07 14:01:50 -07001978 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001979 final int start = replyChainEnd < 0 ? i : replyChainEnd;
1980 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07001981 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001982 if (p.finishing) {
1983 continue;
1984 }
1985
Craig Mautner525f3d92013-05-07 14:01:50 -07001986 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001987 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07001988 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001989 topOptions = p.takeOptionsLocked();
1990 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001991 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001992 }
1993 }
1994 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08001995 + task + " adding to task=" + targetTask
1996 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08001997 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
1998 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001999 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002000 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002001
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002002 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002003 }
2004
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002005 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002006 if (VALIDATE_TOKENS) {
2007 validateAppTokensLocked();
2008 }
2009
2010 replyChainEnd = -1;
2011 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2012 // If the activity should just be removed -- either
2013 // because it asks for it, or the task should be
2014 // cleared -- then finish it and anything that is
2015 // part of its reply chain.
2016 int end;
2017 if (clearWhenTaskReset) {
2018 // In this case, we want to finish this activity
2019 // and everything above it, so be sneaky and pretend
2020 // like these are all in the reply chain.
2021 end = numActivities - 1;
2022 } else if (replyChainEnd < 0) {
2023 end = i;
2024 } else {
2025 end = replyChainEnd;
2026 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002027 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002028 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002029 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002030 if (p.finishing) {
2031 continue;
2032 }
2033 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002034 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002035 topOptions = p.takeOptionsLocked();
2036 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002037 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002038 }
2039 }
Craig Mautner58547802013-03-05 08:23:53 -08002040 if (DEBUG_TASKS) Slog.w(TAG,
2041 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002042 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002043 end--;
2044 srcPos--;
2045 }
2046 }
2047 replyChainEnd = -1;
2048 } else {
2049 // If we were in the middle of a chain, well the
2050 // activity that started it all doesn't want anything
2051 // special, so leave it all as-is.
2052 replyChainEnd = -1;
2053 }
2054 }
2055
2056 return topOptions;
2057 }
2058
2059 /**
2060 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2061 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2062 * @param affinityTask The task we are looking for an affinity to.
2063 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2064 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2065 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2066 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002067 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002068 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002069 int replyChainEnd = -1;
2070 final int taskId = task.taskId;
2071 final String taskAffinity = task.affinity;
2072
2073 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2074 final int numActivities = activities.size();
2075 // Do not operate on the root Activity.
2076 for (int i = numActivities - 1; i > 0; --i) {
2077 ActivityRecord target = activities.get(i);
2078
2079 final int flags = target.info.flags;
2080 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2081 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2082
2083 if (target.resultTo != null) {
2084 // If this activity is sending a reply to a previous
2085 // activity, we can't do anything with it now until
2086 // we reach the start of the reply chain.
2087 // XXX note that we are assuming the result is always
2088 // to the previous activity, which is almost always
2089 // the case but we really shouldn't count on.
2090 if (replyChainEnd < 0) {
2091 replyChainEnd = i;
2092 }
2093 } else if (topTaskIsHigher
2094 && allowTaskReparenting
2095 && taskAffinity != null
2096 && taskAffinity.equals(target.taskAffinity)) {
2097 // This activity has an affinity for our task. Either remove it if we are
2098 // clearing or move it over to our task. Note that
2099 // we currently punt on the case where we are resetting a
2100 // task that is not at the top but who has activities above
2101 // with an affinity to it... this is really not a normal
2102 // case, and we will need to later pull that task to the front
2103 // and usually at that point we will do the reset and pick
2104 // up those remaining activities. (This only happens if
2105 // someone starts an activity in a new task from an activity
2106 // in a task that is not currently on top.)
2107 if (forceReset || finishOnTaskLaunch) {
2108 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2109 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2110 for (int srcPos = start; srcPos >= i; --srcPos) {
2111 final ActivityRecord p = activities.get(srcPos);
2112 if (p.finishing) {
2113 continue;
2114 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002115 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002116 }
2117 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002118 if (taskInsertionPoint < 0) {
2119 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002120
Craig Mautner77878772013-03-04 19:46:24 -08002121 }
Craig Mautner77878772013-03-04 19:46:24 -08002122
2123 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2124 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2125 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2126 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002127 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002128 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002129 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002130
Craig Mautnere3a74d52013-02-22 14:14:58 -08002131 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2132 + " to stack at " + task,
2133 new RuntimeException("here").fillInStackTrace());
2134 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2135 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002136 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002137 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002138 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002139 if (VALIDATE_TOKENS) {
2140 validateAppTokensLocked();
2141 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002142
2143 // Now we've moved it in to place... but what if this is
2144 // a singleTop activity and we have put it on top of another
2145 // instance of the same activity? Then we drop the instance
2146 // below so it remains singleTop.
2147 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2148 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002149 int targetNdx = taskActivities.indexOf(target);
2150 if (targetNdx > 0) {
2151 ActivityRecord p = taskActivities.get(targetNdx - 1);
2152 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002153 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2154 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002155 }
2156 }
2157 }
2158 }
2159
2160 replyChainEnd = -1;
2161 }
2162 }
Craig Mautner77878772013-03-04 19:46:24 -08002163 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002164 }
2165
Craig Mautner8849a5e2013-04-02 16:41:03 -07002166 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002167 ActivityRecord newActivity) {
2168 boolean forceReset =
2169 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2170 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2171 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2172 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2173 forceReset = true;
2174 }
2175 }
2176
2177 final TaskRecord task = taskTop.task;
2178
2179 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2180 * for remaining tasks. Used for later tasks to reparent to task. */
2181 boolean taskFound = false;
2182
2183 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2184 ActivityOptions topOptions = null;
2185
Craig Mautner77878772013-03-04 19:46:24 -08002186 // Preserve the location for reparenting in the new task.
2187 int reparentInsertionPoint = -1;
2188
Craig Mautnere3a74d52013-02-22 14:14:58 -08002189 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2190 final TaskRecord targetTask = mTaskHistory.get(i);
2191
2192 if (targetTask == task) {
2193 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2194 taskFound = true;
2195 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002196 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2197 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002198 }
2199 }
2200
Craig Mautner70a86932013-02-28 22:37:44 -08002201 int taskNdx = mTaskHistory.indexOf(task);
2202 do {
2203 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2204 } while (taskTop == null && taskNdx >= 0);
2205
Craig Mautnere3a74d52013-02-22 14:14:58 -08002206 if (topOptions != null) {
2207 // If we got some ActivityOptions from an activity on top that
2208 // was removed from the task, propagate them to the new real top.
2209 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002210 taskTop.updateOptionsLocked(topOptions);
2211 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002212 topOptions.abort();
2213 }
2214 }
2215
2216 return taskTop;
2217 }
2218
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002219 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2220 String resultWho, int requestCode, int resultCode, Intent data) {
2221
2222 if (callingUid > 0) {
2223 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002224 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002225 }
2226
2227 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2228 + " : who=" + resultWho + " req=" + requestCode
2229 + " res=" + resultCode + " data=" + data);
2230 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2231 try {
2232 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2233 list.add(new ResultInfo(resultWho, requestCode,
2234 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002235 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002236 return;
2237 } catch (Exception e) {
2238 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2239 }
2240 }
2241
2242 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2243 }
2244
Craig Mautner04f0b702013-10-22 12:31:01 -07002245 private void adjustFocusedActivityLocked(ActivityRecord r) {
2246 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2247 ActivityRecord next = topRunningActivityLocked(null);
2248 if (next != r) {
2249 final TaskRecord task = r.task;
2250 if (r.frontOfTask && task == topTask() && task.mOnTopOfHome) {
2251 mStackSupervisor.moveHomeToTop();
2252 }
2253 }
2254 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
2255 }
2256 }
2257
Craig Mautnerf3333272013-04-22 10:55:53 -07002258 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002259 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2260 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2261 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2262 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002263 if (!mService.mSleeping) {
2264 if (DEBUG_STATES) {
2265 Slog.d(TAG, "no-history finish of " + r);
2266 }
2267 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002268 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002269 } else {
2270 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2271 + " on stop because we're just sleeping");
2272 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002273 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002274 }
2275
2276 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002277 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002278 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002279 try {
2280 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002281 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2282 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002283 r.state = ActivityState.STOPPING;
2284 if (DEBUG_VISBILITY) Slog.v(
2285 TAG, "Stopping visible=" + r.visible + " for " + r);
2286 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002287 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002288 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002289 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002290 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002291 r.setSleeping(true);
2292 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002293 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002294 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002295 } catch (Exception e) {
2296 // Maybe just ignore exceptions here... if the process
2297 // has crashed, our death notification will clean things
2298 // up.
2299 Slog.w(TAG, "Exception thrown during pause", e);
2300 // Just in case, assume it to be stopped.
2301 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002302 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002303 r.state = ActivityState.STOPPED;
2304 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002305 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002306 }
2307 }
2308 }
2309 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002310
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002311 /**
2312 * @return Returns true if the activity is being finished, false if for
2313 * some reason it is being left as-is.
2314 */
2315 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002316 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002317 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002318 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002319 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002320 + ", result=" + resultCode + ", data=" + resultData
2321 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002322 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002323 return false;
2324 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002325
Craig Mautnerd44711d2013-02-23 11:24:36 -08002326 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002327 return true;
2328 }
2329
Craig Mautnerd2328952013-03-05 12:46:26 -08002330 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002331 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2332 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2333 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2334 ActivityRecord r = activities.get(activityNdx);
2335 if (r.resultTo == self && r.requestCode == requestCode) {
2336 if ((r.resultWho == null && resultWho == null) ||
2337 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2338 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2339 false);
2340 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002341 }
2342 }
2343 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002344 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002345 }
2346
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002347 final void finishTopRunningActivityLocked(ProcessRecord app) {
2348 ActivityRecord r = topRunningActivityLocked(null);
2349 if (r != null && r.app == app) {
2350 // If the top running activity is from this crashing
2351 // process, then terminate it to avoid getting in a loop.
2352 Slog.w(TAG, " Force finishing activity "
2353 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002354 int taskNdx = mTaskHistory.indexOf(r.task);
2355 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002356 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002357 // Also terminate any activities below it that aren't yet
2358 // stopped, to avoid a situation where one will get
2359 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002360 --activityNdx;
2361 if (activityNdx < 0) {
2362 do {
2363 --taskNdx;
2364 if (taskNdx < 0) {
2365 break;
2366 }
2367 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2368 } while (activityNdx < 0);
2369 }
2370 if (activityNdx >= 0) {
2371 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002372 if (r.state == ActivityState.RESUMED
2373 || r.state == ActivityState.PAUSING
2374 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002375 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002376 Slog.w(TAG, " Force finishing activity "
2377 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002378 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002379 }
2380 }
2381 }
2382 }
2383 }
2384
Craig Mautnerd2328952013-03-05 12:46:26 -08002385 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002386 ArrayList<ActivityRecord> activities = r.task.mActivities;
2387 for (int index = activities.indexOf(r); index >= 0; --index) {
2388 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002389 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002390 break;
2391 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002392 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002393 }
2394 return true;
2395 }
2396
Dianne Hackborn5c607432012-02-28 14:44:19 -08002397 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2398 // send the result
2399 ActivityRecord resultTo = r.resultTo;
2400 if (resultTo != null) {
2401 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2402 + " who=" + r.resultWho + " req=" + r.requestCode
2403 + " res=" + resultCode + " data=" + resultData);
2404 if (r.info.applicationInfo.uid > 0) {
2405 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2406 resultTo.packageName, resultData,
2407 resultTo.getUriPermissionsLocked());
2408 }
2409 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2410 resultData);
2411 r.resultTo = null;
2412 }
2413 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2414
2415 // Make sure this HistoryRecord is not holding on to other resources,
2416 // because clients have remote IPC references to this object so we
2417 // can't assume that will go away and want to avoid circular IPC refs.
2418 r.results = null;
2419 r.pendingResults = null;
2420 r.newIntents = null;
2421 r.icicle = null;
2422 }
2423
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002424 /**
2425 * @return Returns true if this activity has been removed from the history
2426 * list, or false if it is still in the list and will be removed later.
2427 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002428 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2429 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002430 if (r.finishing) {
2431 Slog.w(TAG, "Duplicate finish request for " + r);
2432 return false;
2433 }
2434
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002435 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002436 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002437 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002438 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002439 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2440 final int index = activities.indexOf(r);
2441 if (index < (activities.size() - 1)) {
Craig Mautner3b475fe2013-12-16 15:58:31 -08002442 r.task.setFrontOfTask();
Craig Mautnerd44711d2013-02-23 11:24:36 -08002443 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2444 // If the caller asked that this activity (and all above it)
2445 // be cleared when the task is reset, don't lose that information,
2446 // but propagate it up to the next activity.
Craig Mautner3b475fe2013-12-16 15:58:31 -08002447 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002448 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002449 }
2450 }
2451
2452 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002453
2454 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002455
Dianne Hackborn5c607432012-02-28 14:44:19 -08002456 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002457
Craig Mautnerc8143c62013-09-03 12:15:57 -07002458 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002459 // There are clients waiting to receive thumbnails so, in case
2460 // this is an activity that someone is waiting for, add it
2461 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002462 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002463 }
2464
Craig Mautnerde4ef022013-04-07 19:01:33 -07002465 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002466 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002467 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002468 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002469 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002470 ? AppTransition.TRANSIT_TASK_CLOSE
2471 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002472
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002473 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002474 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002475
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002476 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002477 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2478 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2479 startPausingLocked(false, false);
2480 }
2481
2482 } else if (r.state != ActivityState.PAUSING) {
2483 // If the activity is PAUSING, we will complete the finish once
2484 // it is done pausing; else we can just directly finish it here.
2485 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002486 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002487 } else {
2488 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2489 }
2490
2491 return false;
2492 }
2493
Craig Mautnerf3333272013-04-22 10:55:53 -07002494 static final int FINISH_IMMEDIATELY = 0;
2495 static final int FINISH_AFTER_PAUSE = 1;
2496 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002497
Craig Mautnerf3333272013-04-22 10:55:53 -07002498 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002499 // First things first: if this activity is currently visible,
2500 // and the resumed activity is not yet visible, then hold off on
2501 // finishing until the resumed one becomes visible.
2502 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002503 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2504 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002505 if (mStackSupervisor.mStoppingActivities.size() > 3
2506 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002507 // If we already have a few activities waiting to stop,
2508 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002509 // them out. Or if r is the last of activity of the last task the stack
2510 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002511 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002512 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002513 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002514 }
2515 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002516 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2517 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002518 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002519 if (oomAdj) {
2520 mService.updateOomAdjLocked();
2521 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002522 return r;
2523 }
2524
2525 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002526 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002527 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002528 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002529 if (mResumedActivity == r) {
2530 mResumedActivity = null;
2531 }
2532 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002533 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002534 r.state = ActivityState.FINISHING;
2535
2536 if (mode == FINISH_IMMEDIATELY
2537 || prevState == ActivityState.STOPPED
2538 || prevState == ActivityState.INITIALIZING) {
2539 // If this activity is already stopped, we can just finish
2540 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002541 boolean activityRemoved = destroyActivityLocked(r, true,
2542 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002543 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002544 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002545 }
2546 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002547 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002548
2549 // Need to go through the full pause cycle to get this
2550 // activity into the stopped state and then finish it.
2551 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002552 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002553 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002554 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002555 return r;
2556 }
2557
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002558 void finishAllActivitiesLocked() {
2559 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2560 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2561 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2562 final ActivityRecord r = activities.get(activityNdx);
2563 if (r.finishing) {
2564 continue;
2565 }
2566 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r);
2567 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2568 }
2569 }
2570 }
2571
Craig Mautnerd2328952013-03-05 12:46:26 -08002572 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002573 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002574 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002575 final TaskRecord task = srec.task;
2576 final ArrayList<ActivityRecord> activities = task.mActivities;
2577 final int start = activities.indexOf(srec);
2578 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002579 return false;
2580 }
2581 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002582 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002583 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002584 final ComponentName dest = destIntent.getComponent();
2585 if (start > 0 && dest != null) {
2586 for (int i = finishTo; i >= 0; i--) {
2587 ActivityRecord r = activities.get(i);
2588 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002589 r.info.name.equals(dest.getClassName())) {
2590 finishTo = i;
2591 parent = r;
2592 foundParentInTask = true;
2593 break;
2594 }
2595 }
2596 }
2597
2598 IActivityController controller = mService.mController;
2599 if (controller != null) {
2600 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2601 if (next != null) {
2602 // ask watcher if this is allowed
2603 boolean resumeOK = true;
2604 try {
2605 resumeOK = controller.activityResuming(next.packageName);
2606 } catch (RemoteException e) {
2607 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002608 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002609 }
2610
2611 if (!resumeOK) {
2612 return false;
2613 }
2614 }
2615 }
2616 final long origId = Binder.clearCallingIdentity();
2617 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002618 ActivityRecord r = activities.get(i);
2619 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002620 // Only return the supplied result for the first activity finished
2621 resultCode = Activity.RESULT_CANCELED;
2622 resultData = null;
2623 }
2624
2625 if (parent != null && foundParentInTask) {
2626 final int parentLaunchMode = parent.info.launchMode;
2627 final int destIntentFlags = destIntent.getFlags();
2628 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2629 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2630 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2631 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2632 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2633 } else {
2634 try {
2635 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2636 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002637 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002638 null, aInfo, parent.appToken, null,
2639 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08002640 0, null, true, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002641 foundParentInTask = res == ActivityManager.START_SUCCESS;
2642 } catch (RemoteException e) {
2643 foundParentInTask = false;
2644 }
2645 requestFinishActivityLocked(parent.appToken, resultCode,
2646 resultData, "navigate-up", true);
2647 }
2648 }
2649 Binder.restoreCallingIdentity(origId);
2650 return foundParentInTask;
2651 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002652 /**
2653 * Perform the common clean-up of an activity record. This is called both
2654 * as part of destroyActivityLocked() (when destroying the client-side
2655 * representation) and cleaning things up as a result of its hosting
2656 * processing going away, in which case there is no remaining client-side
2657 * state to destroy so only the cleanup here is needed.
2658 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002659 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2660 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002661 if (mResumedActivity == r) {
2662 mResumedActivity = null;
2663 }
2664 if (mService.mFocusedActivity == r) {
2665 mService.mFocusedActivity = null;
2666 }
2667
2668 r.configDestroy = false;
2669 r.frozenBeforeDestroy = false;
2670
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002671 if (setState) {
2672 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2673 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002674 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002675 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002676 }
2677
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002678 // Make sure this record is no longer in the pending finishes list.
2679 // This could happen, for example, if we are trimming activities
2680 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002681 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002682 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002683
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002684 // Remove any pending results.
2685 if (r.finishing && r.pendingResults != null) {
2686 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2687 PendingIntentRecord rec = apr.get();
2688 if (rec != null) {
2689 mService.cancelIntentSenderLocked(rec, false);
2690 }
2691 }
2692 r.pendingResults = null;
2693 }
2694
2695 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002696 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002697 }
2698
Craig Mautnerc8143c62013-09-03 12:15:57 -07002699 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002700 // There are clients waiting to receive thumbnails so, in case
2701 // this is an activity that someone is waiting for, add it
2702 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002703 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002704 }
2705
2706 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002707 removeTimeoutsForActivityLocked(r);
2708 }
2709
2710 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002711 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002712 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002713 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002714 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002715 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002716 }
2717
Craig Mautner04a0ea62014-01-13 12:51:26 -08002718 private void removeActivityFromHistoryLocked(ActivityRecord r) {
Craig Mautner34b73df2014-01-12 21:11:08 -08002719 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002720 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2721 r.makeFinishing();
2722 if (DEBUG_ADD_REMOVE) {
2723 RuntimeException here = new RuntimeException("here");
2724 here.fillInStackTrace();
2725 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002726 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002727 r.takeFromHistory();
2728 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002729 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002730 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002731 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002732 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002733 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002734 if (VALIDATE_TOKENS) {
2735 validateAppTokensLocked();
2736 }
Craig Mautner312ba862014-02-10 17:55:01 -08002737 final TaskRecord task = r.task;
2738 if (task != null && task.removeActivity(r)) {
2739 if (DEBUG_STACK) Slog.i(TAG,
2740 "removeActivityFromHistoryLocked: last activity removed from " + this);
2741 if (mStackSupervisor.isFrontStack(this) && task == topTask() && task.mOnTopOfHome) {
2742 mStackSupervisor.moveHomeToTop();
2743 }
2744 removeTask(task);
2745 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002746 cleanUpActivityServicesLocked(r);
2747 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002748 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002749
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002750 /**
2751 * Perform clean-up of service connections in an activity record.
2752 */
2753 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2754 // Throw away any services that have been bound by this activity.
2755 if (r.connections != null) {
2756 Iterator<ConnectionRecord> it = r.connections.iterator();
2757 while (it.hasNext()) {
2758 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002759 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002760 }
2761 r.connections = null;
2762 }
2763 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002764
2765 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2766 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2767 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2768 mHandler.sendMessage(msg);
2769 }
2770
Dianne Hackborn28695e02011-11-02 21:59:51 -07002771 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002772 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002773 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002774 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2775 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2776 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2777 final ActivityRecord r = activities.get(activityNdx);
2778 if (r.finishing) {
2779 continue;
2780 }
2781 if (r.fullscreen) {
2782 lastIsOpaque = true;
2783 }
2784 if (owner != null && r.app != owner) {
2785 continue;
2786 }
2787 if (!lastIsOpaque) {
2788 continue;
2789 }
2790 // We can destroy this one if we have its icicle saved and
2791 // it is not in the process of pausing/stopping/finishing.
2792 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2793 && r.haveState && !r.visible && r.stopped
2794 && r.state != ActivityState.DESTROYING
2795 && r.state != ActivityState.DESTROYED) {
2796 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2797 + " resumed=" + mResumedActivity
2798 + " pausing=" + mPausingActivity);
2799 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2800 activityRemoved = true;
2801 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002802 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002803 }
2804 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002805 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002806 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002807 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002808 }
2809
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002810 /**
2811 * Destroy the current CLIENT SIDE instance of an activity. This may be
2812 * called both when actually finishing an activity, or when performing
2813 * a configuration switch where we destroy the current client-side object
2814 * but then create a new client-side object for this same HistoryRecord.
2815 */
2816 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002817 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002818 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002819 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002820 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2821 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002822 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002823 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002824
2825 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002826
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002827 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002828
2829 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002830
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002831 if (hadApp) {
2832 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002833 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002834 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2835 mService.mHeavyWeightProcess = null;
2836 mService.mHandler.sendEmptyMessage(
2837 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2838 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07002839 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07002840 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07002841 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002842 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002843 }
2844 }
2845
2846 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002847
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002848 try {
2849 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002850 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002851 r.configChangeFlags);
2852 } catch (Exception e) {
2853 // We can just ignore exceptions here... if the process
2854 // has crashed, our death notification will clean things
2855 // up.
2856 //Slog.w(TAG, "Exception thrown during finish", e);
2857 if (r.finishing) {
2858 removeActivityFromHistoryLocked(r);
2859 removedFromHistory = true;
2860 skipDestroy = true;
2861 }
2862 }
2863
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002864 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002865
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002866 // If the activity is finishing, we need to wait on removing it
2867 // from the list to give it a chance to do its cleanup. During
2868 // that time it may make calls back with its token so we need to
2869 // be able to find it on the list and so we don't want to remove
2870 // it from the list yet. Otherwise, we can just immediately put
2871 // it in the destroyed state since we are not removing it from the
2872 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002873 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002874 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2875 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002876 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002877 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002878 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2879 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002880 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002881 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002882 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002883 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002884 }
2885 } else {
2886 // remove this record from the history.
2887 if (r.finishing) {
2888 removeActivityFromHistoryLocked(r);
2889 removedFromHistory = true;
2890 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002891 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002892 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002893 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002894 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002895 }
2896 }
2897
2898 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002899
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002900 if (!mLRUActivities.remove(r) && hadApp) {
2901 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2902 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002903
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002904 return removedFromHistory;
2905 }
2906
Craig Mautnerd2328952013-03-05 12:46:26 -08002907 final void activityDestroyedLocked(IBinder token) {
2908 final long origId = Binder.clearCallingIdentity();
2909 try {
2910 ActivityRecord r = ActivityRecord.forToken(token);
2911 if (r != null) {
2912 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002913 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002914
2915 if (isInStackLocked(token) != null) {
2916 if (r.state == ActivityState.DESTROYING) {
2917 cleanUpActivityLocked(r, true, false);
2918 removeActivityFromHistoryLocked(r);
2919 }
2920 }
Craig Mautner05d29032013-05-03 13:40:13 -07002921 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002922 } finally {
2923 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002924 }
2925 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002926
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002927 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2928 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002929 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002930 if (DEBUG_CLEANUP) Slog.v(
2931 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002932 + " with " + i + " entries");
2933 while (i > 0) {
2934 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002935 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002936 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002937 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002938 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002939 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002940 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002941 }
2942 }
2943 }
2944
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002945 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2946 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002947 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2948 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002949 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2950 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002951 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002952 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002953 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2954 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002955
2956 boolean hasVisibleActivities = false;
2957
2958 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002959 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002960 if (DEBUG_CLEANUP) Slog.v(
2961 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002962 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2963 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2964 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2965 final ActivityRecord r = activities.get(activityNdx);
2966 --i;
2967 if (DEBUG_CLEANUP) Slog.v(
2968 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2969 if (r.app == app) {
2970 boolean remove;
2971 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2972 // Don't currently have state for the activity, or
2973 // it is finishing -- always remove it.
2974 remove = true;
2975 } else if (r.launchCount > 2 &&
2976 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2977 // We have launched this activity too many times since it was
2978 // able to run, so give up and remove it.
2979 remove = true;
2980 } else {
2981 // The process may be gone, but the activity lives on!
2982 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002983 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002984 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002985 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002986 RuntimeException here = new RuntimeException("here");
2987 here.fillInStackTrace();
2988 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2989 + ": haveState=" + r.haveState
2990 + " stateNotNeeded=" + r.stateNotNeeded
2991 + " finishing=" + r.finishing
2992 + " state=" + r.state, here);
2993 }
2994 if (!r.finishing) {
2995 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
2996 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2997 r.userId, System.identityHashCode(r),
2998 r.task.taskId, r.shortComponentName,
2999 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003000 if (r.state == ActivityState.RESUMED) {
3001 mService.updateUsageStats(r, false);
3002 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003003 }
3004 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003005
Craig Mautner0247fc82013-02-28 14:32:06 -08003006 } else {
3007 // We have the current state for this activity, so
3008 // it can be restarted later when needed.
3009 if (localLOGV) Slog.v(
3010 TAG, "Keeping entry, setting app to null");
3011 if (r.visible) {
3012 hasVisibleActivities = true;
3013 }
3014 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3015 + r);
3016 r.app = null;
3017 r.nowVisible = false;
3018 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003019 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003020 "App died, clearing saved state of " + r);
3021 r.icicle = null;
3022 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003023 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003024
Craig Mautnerd2328952013-03-05 12:46:26 -08003025 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003026 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003027 }
3028 }
3029
3030 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003031 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003032
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003033 final void updateTransitLocked(int transit, Bundle options) {
3034 if (options != null) {
3035 ActivityRecord r = topRunningActivityLocked(null);
3036 if (r != null && r.state != ActivityState.RESUMED) {
3037 r.updateOptionsLocked(options);
3038 } else {
3039 ActivityOptions.abort(options);
3040 }
3041 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003042 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003043 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003044
Craig Mautnera82aa092013-09-13 15:34:08 -07003045 void moveHomeTaskToTop() {
3046 final int top = mTaskHistory.size() - 1;
3047 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3048 final TaskRecord task = mTaskHistory.get(taskNdx);
3049 if (task.isHomeTask()) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003050 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG, "moveHomeTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003051 mTaskHistory.remove(taskNdx);
3052 mTaskHistory.add(top, task);
3053 mWindowManager.moveTaskToTop(task.taskId);
3054 return;
3055 }
3056 }
3057 }
3058
Craig Mautnercae015f2013-02-08 14:31:27 -08003059 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003060 final TaskRecord task = taskForIdLocked(taskId);
3061 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003062 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003063 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003064 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003065 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
3066 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07003067 // we'll just indicate that this task returns to the home task.
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003068 task.mOnTopOfHome = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08003069 }
3070 moveTaskToFrontLocked(task, null, options);
3071 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003072 }
3073 return false;
3074 }
3075
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003076 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003077 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003078
Craig Mautner11bf9a52013-02-19 14:08:51 -08003079 final int numTasks = mTaskHistory.size();
3080 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003081 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003082 // nothing to do!
3083 if (reason != null &&
3084 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3085 ActivityOptions.abort(options);
3086 } else {
3087 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3088 }
3089 return;
3090 }
3091
Craig Mautnere0a38842013-12-16 16:14:02 -08003092 moveToFront();
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003093
Craig Mautner11bf9a52013-02-19 14:08:51 -08003094 // Shift all activities with this task up to the top
3095 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003096 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003097
3098 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003099 if (reason != null &&
3100 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003101 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003102 ActivityRecord r = topRunningActivityLocked(null);
3103 if (r != null) {
3104 mNoAnimActivities.add(r);
3105 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003106 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003107 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003108 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003109 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003110
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003111 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003112
Craig Mautner05d29032013-05-03 13:40:13 -07003113 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003114 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003115
3116 if (VALIDATE_TOKENS) {
3117 validateAppTokensLocked();
3118 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003119 }
3120
3121 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003122 * Worker method for rearranging history stack. Implements the function of moving all
3123 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003124 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003125 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003126 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3127 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003128 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003129 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003130 * @return Returns true if the move completed, false if not.
3131 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003132 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
3133 Slog.i(TAG, "moveTaskToBack: " + taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003134
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003135 // If we have a watcher, preflight the move before committing to it. First check
3136 // for *other* available tasks, but if none are available, then try again allowing the
3137 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003138 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003139 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003140 if (next == null) {
3141 next = topRunningActivityLocked(null, 0);
3142 }
3143 if (next != null) {
3144 // ask watcher if this is allowed
3145 boolean moveOK = true;
3146 try {
3147 moveOK = mService.mController.activityResuming(next.packageName);
3148 } catch (RemoteException e) {
3149 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003150 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003151 }
3152 if (!moveOK) {
3153 return false;
3154 }
3155 }
3156 }
3157
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003158 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003159 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003160
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003161 final TaskRecord tr = taskForIdLocked(taskId);
Craig Mautnerd2328952013-03-05 12:46:26 -08003162 if (tr == null) {
3163 return false;
3164 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003165
Craig Mautner11bf9a52013-02-19 14:08:51 -08003166 mTaskHistory.remove(tr);
3167 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003168
Craig Mautnerc8143c62013-09-03 12:15:57 -07003169 // There is an assumption that moving a task to the back moves it behind the home activity.
3170 // We make sure here that some activity in the stack will launch home.
3171 ActivityRecord lastActivity = null;
3172 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003173 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3174 final TaskRecord task = mTaskHistory.get(taskNdx);
3175 if (task.mOnTopOfHome) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003176 break;
3177 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003178 if (taskNdx == 1) {
3179 // Set the last task before tr to go to home.
3180 task.mOnTopOfHome = true;
3181 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003182 }
3183
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003184 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003185 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3186 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003187 ActivityRecord r = topRunningActivityLocked(null);
3188 if (r != null) {
3189 mNoAnimActivities.add(r);
3190 }
3191 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003192 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003193 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003194 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003195
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003196 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003197 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003198 }
3199
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003200 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Craig Mautnere0a38842013-12-16 16:14:02 -08003201 if (task == tr && tr.mOnTopOfHome || numTasks <= 1 && isOnHomeDisplay()) {
Daniel 2 Olofsson9cdf9e52013-12-16 13:24:25 +01003202 tr.mOnTopOfHome = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003203 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003204 }
3205
Craig Mautner05d29032013-05-03 13:40:13 -07003206 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003207 return true;
3208 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003209
Craig Mautner8849a5e2013-04-02 16:41:03 -07003210 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003211 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003212 final Uri data = r.intent.getData();
3213 final String strData = data != null ? data.toSafeString() : null;
3214
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003215 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003216 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003217 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003218 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003219 }
3220
3221 /**
3222 * Make sure the given activity matches the current configuration. Returns
3223 * false if the activity had to be destroyed. Returns true if the
3224 * configuration is the same, or the activity will remain running as-is
3225 * for whatever reason. Ensures the HistoryRecord is updated with the
3226 * correct configuration and all other bookkeeping is handled.
3227 */
3228 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3229 int globalChanges) {
3230 if (mConfigWillChange) {
3231 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3232 "Skipping config check (will change): " + r);
3233 return true;
3234 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003235
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003236 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3237 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003238
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003239 // Short circuit: if the two configurations are the exact same
3240 // object (the common case), then there is nothing to do.
3241 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003242 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003243 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3244 "Configuration unchanged in " + r);
3245 return true;
3246 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003247
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003248 // We don't worry about activities that are finishing.
3249 if (r.finishing) {
3250 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3251 "Configuration doesn't matter in finishing " + r);
3252 r.stopFreezingScreenLocked(false);
3253 return true;
3254 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003255
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003256 // Okay we now are going to make this activity have the new config.
3257 // But then we need to figure out how it needs to deal with that.
3258 Configuration oldConfig = r.configuration;
3259 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003260
3261 // Determine what has changed. May be nothing, if this is a config
3262 // that has come back from the app after going idle. In that case
3263 // we just want to leave the official config object now in the
3264 // activity and do nothing else.
3265 final int changes = oldConfig.diff(newConfig);
3266 if (changes == 0 && !r.forceNewConfig) {
3267 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3268 "Configuration no differences in " + r);
3269 return true;
3270 }
3271
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003272 // If the activity isn't currently running, just leave the new
3273 // configuration and it will pick that up next time it starts.
3274 if (r.app == null || r.app.thread == null) {
3275 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3276 "Configuration doesn't matter not running " + r);
3277 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003278 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003279 return true;
3280 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003281
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003282 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003283 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3284 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3285 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003286 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003287 + ", newConfig=" + newConfig);
3288 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003289 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003290 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3291 r.configChangeFlags |= changes;
3292 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003293 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003294 if (r.app == null || r.app.thread == null) {
3295 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003296 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003297 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003298 } else if (r.state == ActivityState.PAUSING) {
3299 // A little annoying: we are waiting for this activity to
3300 // finish pausing. Let's not do anything now, but just
3301 // flag that it needs to be restarted when done pausing.
3302 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003303 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003304 r.configDestroy = true;
3305 return true;
3306 } else if (r.state == ActivityState.RESUMED) {
3307 // Try to optimize this case: the configuration is changing
3308 // and we need to restart the top, resumed activity.
3309 // Instead of doing the normal handshaking, just say
3310 // "restart!".
3311 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003312 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003313 relaunchActivityLocked(r, r.configChangeFlags, true);
3314 r.configChangeFlags = 0;
3315 } else {
3316 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003317 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003318 relaunchActivityLocked(r, r.configChangeFlags, false);
3319 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003320 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003321
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003322 // All done... tell the caller we weren't able to keep this
3323 // activity around.
3324 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003325 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003326
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003327 // Default case: the activity can handle this new configuration, so
3328 // hand it over. Note that we don't need to give it the new
3329 // configuration, since we always send configuration changes to all
3330 // process when they happen so it can just use whatever configuration
3331 // it last got.
3332 if (r.app != null && r.app.thread != null) {
3333 try {
3334 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003335 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003336 } catch (RemoteException e) {
3337 // If process died, whatever.
3338 }
3339 }
3340 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003341
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003342 return true;
3343 }
3344
Craig Mautnerc8143c62013-09-03 12:15:57 -07003345 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003346 int changes, boolean andResume) {
3347 List<ResultInfo> results = null;
3348 List<Intent> newIntents = null;
3349 if (andResume) {
3350 results = r.results;
3351 newIntents = r.newIntents;
3352 }
3353 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3354 + " with results=" + results + " newIntents=" + newIntents
3355 + " andResume=" + andResume);
3356 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003357 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003358 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003359
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003360 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003361
Craig Mautner34b73df2014-01-12 21:11:08 -08003362 mStackSupervisor.removeChildActivityContainers(r);
3363
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003364 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003365 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3366 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3367 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003368 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003369 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003370 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003371 // Note: don't need to call pauseIfSleepingLocked() here, because
3372 // the caller will only pass in 'andResume' if this activity is
3373 // currently resumed, which implies we aren't sleeping.
3374 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003375 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003376 }
3377
3378 if (andResume) {
3379 r.results = null;
3380 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003381 r.state = ActivityState.RESUMED;
3382 } else {
3383 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3384 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003385 }
3386
3387 return true;
3388 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003389
3390 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003391 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3392 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3393 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3394 final ActivityRecord r = activities.get(activityNdx);
3395 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003396 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003397 }
3398 if (r.fullscreen && !r.finishing) {
3399 return false;
3400 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003401 }
3402 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003403 final ActivityRecord r = ActivityRecord.forToken(token);
3404 if (r == null) {
3405 return false;
3406 }
3407 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3408 + " would have returned true for r=" + r);
3409 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003410 }
3411
3412 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003413 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3414 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3415 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3416 final ActivityRecord r = activities.get(activityNdx);
3417 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003418 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003419 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003420 }
3421 }
3422 }
3423
3424 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3425 boolean didSomething = false;
3426 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003427 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3428 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3429 int numActivities = activities.size();
3430 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3431 ActivityRecord r = activities.get(activityNdx);
3432 final boolean samePackage = r.packageName.equals(name)
3433 || (name == null && r.userId == userId);
3434 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3435 && (samePackage || r.task == lastTask)
3436 && (r.app == null || evenPersistent || !r.app.persistent)) {
3437 if (!doit) {
3438 if (r.finishing) {
3439 // If this activity is just finishing, then it is not
3440 // interesting as far as something to stop.
3441 continue;
3442 }
3443 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003444 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003445 didSomething = true;
3446 Slog.i(TAG, " Force finishing activity " + r);
3447 if (samePackage) {
3448 if (r.app != null) {
3449 r.app.removed = true;
3450 }
3451 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003452 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003453 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003454 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3455 true)) {
3456 // r has been deleted from mActivities, accommodate.
3457 --numActivities;
3458 --activityNdx;
3459 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003460 }
3461 }
3462 }
3463 return didSomething;
3464 }
3465
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003466 ActivityRecord getTasksLocked(IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003467 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003468 ActivityRecord topRecord = null;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003469 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003470 final TaskRecord task = mTaskHistory.get(taskNdx);
3471 ActivityRecord r = null;
3472 ActivityRecord top = null;
3473 int numActivities = 0;
3474 int numRunning = 0;
3475 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003476 if (activities.isEmpty()) {
3477 continue;
3478 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003479 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3480 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003481
Craig Mautneraab647e2013-02-28 16:31:36 -08003482 // Initialize state for next task if needed.
3483 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3484 top = r;
3485 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003486 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003487
3488 // Add 'r' into the current task.
3489 numActivities++;
3490 if (r.app != null && r.app.thread != null) {
3491 numRunning++;
3492 }
3493
3494 if (localLOGV) Slog.v(
3495 TAG, r.intent.getComponent().flattenToShortString()
3496 + ": task=" + r.task);
3497 }
3498
3499 RunningTaskInfo ci = new RunningTaskInfo();
3500 ci.id = task.taskId;
3501 ci.baseActivity = r.intent.getComponent();
3502 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003503 ci.lastActiveTime = task.lastActiveTime;
3504
Craig Mautneraab647e2013-02-28 16:31:36 -08003505 if (top.thumbHolder != null) {
3506 ci.description = top.thumbHolder.lastDescription;
3507 }
3508 ci.numActivities = numActivities;
3509 ci.numRunning = numRunning;
3510 //System.out.println(
3511 // "#" + maxNum + ": " + " descr=" + ci.description);
3512 if (receiver != null) {
3513 if (localLOGV) Slog.v(
3514 TAG, "State=" + top.state + "Idle=" + top.idle
3515 + " app=" + top.app
3516 + " thr=" + (top.app != null ? top.app.thread : null));
3517 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3518 if (top.idle && top.app != null && top.app.thread != null) {
3519 topRecord = top;
3520 } else {
3521 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003522 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003523 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003524 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003525 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003526 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003527 }
3528 return topRecord;
3529 }
3530
3531 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003532 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003533 if (DEBUG_SWITCH) Slog.d(
3534 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003535 if (top >= 0) {
3536 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3537 int activityTop = activities.size() - 1;
3538 if (activityTop > 0) {
3539 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3540 "unhandled-back", true);
3541 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003542 }
3543 }
3544
Craig Mautner6b74cb52013-09-27 17:02:21 -07003545 /**
3546 * Reset local parameters because an app's activity died.
3547 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003548 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003549 */
3550 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003551 if (mPausingActivity != null && mPausingActivity.app == app) {
3552 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3553 "App died while pausing: " + mPausingActivity);
3554 mPausingActivity = null;
3555 }
3556 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3557 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003558 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003559 }
3560
Craig Mautner19091252013-10-05 00:03:53 -07003561 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003562 }
3563
Craig Mautnercae015f2013-02-08 14:31:27 -08003564 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003565 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3566 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3567 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3568 final ActivityRecord r = activities.get(activityNdx);
3569 if (r.app == app) {
3570 Slog.w(TAG, " Force finishing activity "
3571 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003572 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003573 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003574 }
3575 }
3576 }
3577
Dianne Hackborn390517b2013-05-30 15:03:32 -07003578 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003579 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003580 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003581 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3582 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003583 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3584 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003585 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003586 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003587 if (printed) {
3588 header = null;
3589 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003590 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003591 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003592 }
3593
3594 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3595 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3596
3597 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003598 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3599 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003600 }
3601 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003602 final int top = mTaskHistory.size() - 1;
3603 if (top >= 0) {
3604 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3605 int listTop = list.size() - 1;
3606 if (listTop >= 0) {
3607 activities.add(list.get(listTop));
3608 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003609 }
3610 } else {
3611 ItemMatcher matcher = new ItemMatcher();
3612 matcher.build(name);
3613
Craig Mautneraab647e2013-02-28 16:31:36 -08003614 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3615 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3616 if (matcher.match(r1, r1.intent.getComponent())) {
3617 activities.add(r1);
3618 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003619 }
3620 }
3621 }
3622
3623 return activities;
3624 }
3625
3626 ActivityRecord restartPackage(String packageName) {
3627 ActivityRecord starting = topRunningActivityLocked(null);
3628
3629 // All activities that came from the package must be
3630 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003631 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3632 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3633 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3634 final ActivityRecord a = activities.get(activityNdx);
3635 if (a.info.packageName.equals(packageName)) {
3636 a.forceNewConfig = true;
3637 if (starting != null && a == starting && a.visible) {
3638 a.startFreezingScreenLocked(starting.app,
3639 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3640 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003641 }
3642 }
3643 }
3644
3645 return starting;
3646 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003647
Craig Mautner04a0ea62014-01-13 12:51:26 -08003648 void removeTask(TaskRecord task) {
3649 mWindowManager.removeTask(task.taskId);
3650 final ActivityRecord r = mResumedActivity;
3651 if (r != null && r.task == task) {
3652 mResumedActivity = null;
3653 }
3654
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003655 final int taskNdx = mTaskHistory.indexOf(task);
3656 final int topTaskNdx = mTaskHistory.size() - 1;
3657 if (task.mOnTopOfHome && taskNdx < topTaskNdx) {
3658 mTaskHistory.get(taskNdx + 1).mOnTopOfHome = true;
3659 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003660 mTaskHistory.remove(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08003661
3662 if (mTaskHistory.isEmpty()) {
3663 if (DEBUG_STACK) Slog.i(TAG, "removeTask: moving to back stack=" + this);
3664 if (isOnHomeDisplay()) {
3665 mStackSupervisor.moveHomeStack(!isHomeStack());
3666 }
Craig Mautner593a4e62014-01-15 17:55:51 -08003667 if (mStacks != null) {
3668 mStacks.remove(this);
3669 mStacks.add(0, this);
3670 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08003671 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003672 }
3673
Craig Mautnerde4ef022013-04-07 19:01:33 -07003674 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003675 TaskRecord task = new TaskRecord(taskId, info, intent);
3676 addTask(task, toTop);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003677 return task;
3678 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003679
3680 ArrayList<TaskRecord> getAllTasks() {
3681 return new ArrayList<TaskRecord>(mTaskHistory);
3682 }
3683
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003684 void addTask(final TaskRecord task, final boolean toTop) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003685 task.stack = this;
3686 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003687 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003688 } else {
3689 mTaskHistory.add(0, task);
3690 }
3691 }
3692
3693 public int getStackId() {
3694 return mStackId;
3695 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003696
3697 @Override
3698 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07003699 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
3700 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07003701 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003702}