blob: 4a07256cbe112e35456267cc22af7b21c8c62ae4 [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
Adam Powellcfbe9be2013-11-06 14:58:58 -080039import android.os.Trace;
40import android.util.Log;
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
Craig Mautnera61bc652013-10-28 15:43:18 -0700126 static final boolean SCREENSHOT_FORCE_565 = ActivityManager.isLowRamDeviceStatic();
John Reck172e87c2013-10-02 16:55:16 -0700127
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700128 enum ActivityState {
129 INITIALIZING,
130 RESUMED,
131 PAUSING,
132 PAUSED,
133 STOPPING,
134 STOPPED,
135 FINISHING,
136 DESTROYING,
137 DESTROYED
138 }
139
140 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700141 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800142
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700143 /**
144 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800145 * running) activities. It contains #TaskRecord objects.
146 */
147 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
148
149 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800150 * Used for validating app tokens with window manager.
151 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800152 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800153
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700154 /**
155 * List of running activities, sorted by recent usage.
156 * The first entry in the list is the least recently used.
157 * It contains HistoryRecord objects.
158 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700159 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700160
161 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700162 * Animations that for the current transition have requested not to
163 * be considered for the transition animation.
164 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700165 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700166
167 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700168 * When we are in the process of pausing an activity, before starting the
169 * next one, this variable holds the activity that is currently being paused.
170 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800171 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700172
173 /**
174 * This is the last activity that we put into the paused state. This is
175 * used to determine if we need to do an activity transition while sleeping,
176 * when we normally hold the top activity paused.
177 */
178 ActivityRecord mLastPausedActivity = null;
179
180 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700181 * Activities that specify No History must be removed once the user navigates away from them.
182 * If the device goes to sleep with such an activity in the paused state then we save it here
183 * and finish it later if another activity replaces it on wakeup.
184 */
185 ActivityRecord mLastNoHistoryActivity = null;
186
187 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700188 * Current activity that is resumed, or null if there is none.
189 */
190 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800191
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700192 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700193 * This is the last activity that has been started. It is only used to
194 * identify when multiple activities are started at once so that the user
195 * can be warned they may not be in the activity they think they are.
196 */
197 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800198
Craig Mautner5eda9b32013-07-02 11:58:16 -0700199 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
200 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
201 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
202 // Activity in mTranslucentActivityWaiting is notified via
203 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
204 // background activity being drawn then the same call will be made with a true value.
205 ActivityRecord mTranslucentActivityWaiting = null;
206 ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
207 new ArrayList<ActivityRecord>();
208
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700209 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700210 * Set when we know we are going to be calling updateConfiguration()
211 * soon, so want to skip intermediate config checks.
212 */
213 boolean mConfigWillChange;
214
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700215 long mLaunchStartTime = 0;
216 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800217
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800218 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800219 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
220 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
221 */
222 private ActivityRecord mLastScreenshotActivity = null;
223 private Bitmap mLastScreenshotBitmap = null;
224
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800225 int mThumbnailWidth = -1;
226 int mThumbnailHeight = -1;
227
Craig Mautner858d8a62013-04-23 17:08:34 -0700228 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700229
Craig Mautnerc00204b2013-03-05 15:02:14 -0800230 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800231 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800232 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
233 ArrayList<ActivityStack> mStacks;
234 /** The attached Display's unique identifier, or -1 if detached */
235 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800236
Craig Mautner27084302013-03-25 08:05:25 -0700237 /** Run all ActivityStacks through this */
238 final ActivityStackSupervisor mStackSupervisor;
239
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700240 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700241 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
242 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
243 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
244 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700245 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700246
247 static class ScheduleDestroyArgs {
248 final ProcessRecord mOwner;
249 final boolean mOomAdj;
250 final String mReason;
251 ScheduleDestroyArgs(ProcessRecord owner, boolean oomAdj, String reason) {
252 mOwner = owner;
253 mOomAdj = oomAdj;
254 mReason = reason;
255 }
256 }
257
Zoran Marcetaf958b322012-08-09 20:27:12 +0900258 final Handler mHandler;
259
260 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700261 //public Handler() {
262 // if (localLOGV) Slog.v(TAG, "Handler started!");
263 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700264 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900265 super(looper);
266 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700267
Zoran Marcetaf958b322012-08-09 20:27:12 +0900268 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700269 public void handleMessage(Message msg) {
270 switch (msg.what) {
271 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800272 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700273 // We don't at this point know if the activity is fullscreen,
274 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800275 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700276 synchronized (mService) {
277 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700278 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700279 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700280 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800281 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700282 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700283 case LAUNCH_TICK_MSG: {
284 ActivityRecord r = (ActivityRecord)msg.obj;
285 synchronized (mService) {
286 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700287 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700288 }
289 }
290 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700291 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800292 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700293 // We don't at this point know if the activity is fullscreen,
294 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800295 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800296 synchronized (mService) {
297 activityDestroyedLocked(r != null ? r.appToken : null);
298 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700299 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700300 case STOP_TIMEOUT_MSG: {
301 ActivityRecord r = (ActivityRecord)msg.obj;
302 // We don't at this point know if the activity is fullscreen,
303 // so we need to be conservative and assume it isn't.
304 Slog.w(TAG, "Activity stop timeout for " + r);
305 synchronized (mService) {
306 if (r.isInHistory()) {
307 activityStoppedLocked(r, null, null, null);
308 }
309 }
310 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700311 case DESTROY_ACTIVITIES_MSG: {
312 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
313 synchronized (mService) {
314 destroyActivitiesLocked(args.mOwner, args.mOomAdj, args.mReason);
315 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700316 } break;
317 case TRANSLUCENT_TIMEOUT_MSG: {
318 synchronized (mService) {
319 notifyActivityDrawnLocked(null);
320 }
321 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700322 }
323 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800324 }
325
Craig Mautner34b73df2014-01-12 21:11:08 -0800326 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800327 int count = 0;
328 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
329 count += mTaskHistory.get(taskNdx).mActivities.size();
330 }
331 return count;
332 }
333
Craig Mautner4a1cb222013-12-04 16:14:06 -0800334 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer) {
335 mActivityContainer = activityContainer;
336 mStackSupervisor = activityContainer.getOuter();
337 mService = mStackSupervisor.mService;
338 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
339 mWindowManager = mService.mWindowManager;
340 mStackId = activityContainer.mStackId;
341 mCurrentUser = mService.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700342 }
Craig Mautner5962b122012-10-05 14:45:52 -0700343
Craig Mautner6b74cb52013-09-27 17:02:21 -0700344 boolean okToShow(ActivityRecord r) {
Craig Mautner5962b122012-10-05 14:45:52 -0700345 return r.userId == mCurrentUser
346 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
347 }
348
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700349 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800350 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700351 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
352 if (r != null) {
353 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800354 }
355 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700356 return null;
357 }
358
359 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800360 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
361 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800362 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800363 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
364 ActivityRecord r = activities.get(activityNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800365 if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
366 return r;
367 }
368 }
369 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700370 return null;
371 }
372
373 /**
374 * This is a simplified version of topRunningActivityLocked that provides a number of
375 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800376 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700377 * @param token If non-null, any history records matching this token will be skipped.
378 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800379 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700380 * @return Returns the HistoryRecord of the next activity on the stack.
381 */
382 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800383 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
384 TaskRecord task = mTaskHistory.get(taskNdx);
385 if (task.taskId == taskId) {
386 continue;
387 }
388 ArrayList<ActivityRecord> activities = task.mActivities;
389 for (int i = activities.size() - 1; i >= 0; --i) {
390 final ActivityRecord r = activities.get(i);
391 // Note: the taskId check depends on real taskId fields being non-zero
392 if (!r.finishing && (token != r.appToken) && okToShow(r)) {
393 return r;
394 }
395 }
396 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700397 return null;
398 }
399
Craig Mautner8849a5e2013-04-02 16:41:03 -0700400 final ActivityRecord topActivity() {
401 // Iterate to find the first non-empty task stack. Note that this code can
Craig Mautnerde4ef022013-04-07 19:01:33 -0700402 // be simplified once we stop storing tasks with empty mActivities lists.
Craig Mautner8849a5e2013-04-02 16:41:03 -0700403 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
404 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautnera61bc652013-10-28 15:43:18 -0700405 final int topActivityNdx = activities.size() - 1;
406 if (topActivityNdx >= 0) {
407 return activities.get(topActivityNdx);
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
1030 /**
Craig Mautner580ea812013-04-25 12:58:38 -07001031 * Version of ensureActivitiesVisible that can easily be called anywhere.
1032 */
1033 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1034 return ensureActivitiesVisibleLocked(starting, configChanges, false);
1035 }
1036
1037 final boolean ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1038 boolean forceHomeShown) {
1039 ActivityRecord r = topRunningActivityLocked(null);
Craig Mautner525f3d92013-05-07 14:01:50 -07001040 return r != null &&
1041 ensureActivitiesVisibleLocked(r, starting, null, configChanges, forceHomeShown);
Craig Mautner580ea812013-04-25 12:58:38 -07001042 }
1043
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001044 /**
1045 * Make sure that all activities that need to be visible (that is, they
1046 * currently can be seen by the user) actually are.
1047 */
Craig Mautner580ea812013-04-25 12:58:38 -07001048 final boolean ensureActivitiesVisibleLocked(ActivityRecord top, ActivityRecord starting,
1049 String onlyThisProcess, int configChanges, boolean forceHomeShown) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001050 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001051 TAG, "ensureActivitiesVisible behind " + top
1052 + " configChanges=0x" + Integer.toHexString(configChanges));
1053
Craig Mautner5eda9b32013-07-02 11:58:16 -07001054 if (mTranslucentActivityWaiting != top) {
1055 mUndrawnActivitiesBelowTopTranslucent.clear();
1056 if (mTranslucentActivityWaiting != null) {
1057 // Call the callback with a timeout indication.
1058 notifyActivityDrawnLocked(null);
1059 mTranslucentActivityWaiting = null;
1060 }
1061 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1062 }
1063
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001064 // If the top activity is not fullscreen, then we need to
1065 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001066 boolean aboveTop = true;
Craig Mautner580ea812013-04-25 12:58:38 -07001067 boolean showHomeBehindStack = false;
1068 boolean behindFullscreen = !mStackSupervisor.isFrontStack(this) &&
1069 !(forceHomeShown && isHomeStack());
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001070 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001071 final TaskRecord task = mTaskHistory.get(taskNdx);
1072 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001073 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1074 final ActivityRecord r = activities.get(activityNdx);
1075 if (r.finishing) {
1076 continue;
1077 }
1078 if (aboveTop && r != top) {
1079 continue;
1080 }
1081 aboveTop = false;
1082 if (!behindFullscreen) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001083 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001084 TAG, "Make visible? " + r + " finishing=" + r.finishing
1085 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001086
Craig Mautnerd44711d2013-02-23 11:24:36 -08001087 final boolean doThisProcess = onlyThisProcess == null
1088 || onlyThisProcess.equals(r.processName);
1089
1090 // First: if this is not the current activity being started, make
1091 // sure it matches the current configuration.
1092 if (r != starting && doThisProcess) {
1093 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001094 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001095
1096 if (r.app == null || r.app.thread == null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001097 if (onlyThisProcess == null || onlyThisProcess.equals(r.processName)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001098 // This activity needs to be visible, but isn't even
1099 // running... get it started, but don't resume it
1100 // at this point.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001101 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001102 if (r != starting) {
1103 r.startFreezingScreenLocked(r.app, configChanges);
1104 }
1105 if (!r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001106 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001107 TAG, "Starting and making visible: " + r);
louis_changd5c91ec2014-01-28 18:38:06 +08001108 r.visible = true;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001109 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001110 }
1111 if (r != starting) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001112 mStackSupervisor.startSpecificActivityLocked(r, false, false, null);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001113 }
1114 }
1115
1116 } else if (r.visible) {
1117 // If this activity is already visible, then there is nothing
1118 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001119 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001120 r.stopFreezingScreenLocked(false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001121
1122 } else if (onlyThisProcess == null) {
1123 // This activity is not currently visible, but is running.
1124 // Tell it to become visible.
1125 r.visible = true;
1126 if (r.state != ActivityState.RESUMED && r != starting) {
1127 // If this activity is paused, tell it
1128 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001129 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001130 TAG, "Making visible and scheduling visibility: " + r);
1131 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001132 if (mTranslucentActivityWaiting != null) {
1133 mUndrawnActivitiesBelowTopTranslucent.add(r);
1134 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001135 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001136 r.sleeping = false;
1137 r.app.pendingUiClean = true;
1138 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1139 r.stopFreezingScreenLocked(false);
1140 } catch (Exception e) {
1141 // Just skip on any failure; we'll make it
1142 // visible when it next restarts.
1143 Slog.w(TAG, "Exception thrown making visibile: "
1144 + r.intent.getComponent(), e);
1145 }
1146 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001147 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001148
Craig Mautnerd44711d2013-02-23 11:24:36 -08001149 // Aggregate current change flags.
1150 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001151
Craig Mautnerd44711d2013-02-23 11:24:36 -08001152 if (r.fullscreen) {
1153 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001154 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1155 behindFullscreen = true;
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001156 } else if (isActivityOverHome(r)) {
1157 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
1158 showHomeBehindStack = true;
riddle_hsu446ef1d2014-01-09 20:24:34 +08001159 behindFullscreen = !isHomeStack() && r.frontOfTask && task.mOnTopOfHome;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001160 }
1161 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001162 if (DEBUG_VISBILITY) Slog.v(
1163 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1164 + " state=" + r.state
1165 + " behindFullscreen=" + behindFullscreen);
1166 // Now for any activities that aren't visible to the user, make
1167 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001168 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001169 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001170 r.visible = false;
1171 try {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001172 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001173 switch (r.state) {
1174 case STOPPING:
1175 case STOPPED:
1176 if (r.app != null && r.app.thread != null) {
1177 if (DEBUG_VISBILITY) Slog.v(
1178 TAG, "Scheduling invisibility: " + r);
1179 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1180 }
1181 break;
1182
1183 case INITIALIZING:
1184 case RESUMED:
1185 case PAUSING:
1186 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001187 // This case created for transitioning activities from
1188 // translucent to opaque {@link Activity#convertToOpaque}.
Craig Mautnere5273b42013-09-09 12:57:47 -07001189 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1190 mStackSupervisor.mStoppingActivities.add(r);
1191 }
1192 mStackSupervisor.scheduleIdleLocked();
Craig Mautner4addfc52013-06-25 08:05:45 -07001193 break;
1194
1195 default:
1196 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001197 }
1198 } catch (Exception e) {
1199 // Just skip on any failure; we'll make it
1200 // visible when it next restarts.
1201 Slog.w(TAG, "Exception thrown making hidden: "
1202 + r.intent.getComponent(), e);
1203 }
1204 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001205 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001206 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001207 }
1208 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001209 }
Craig Mautner580ea812013-04-25 12:58:38 -07001210 return showHomeBehindStack;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001211 }
Craig Mautner58547802013-03-05 08:23:53 -08001212
Craig Mautner5eda9b32013-07-02 11:58:16 -07001213 void convertToTranslucent(ActivityRecord r) {
1214 mTranslucentActivityWaiting = r;
1215 mUndrawnActivitiesBelowTopTranslucent.clear();
1216 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1217 }
1218
1219 /**
1220 * Called as activities below the top translucent activity are redrawn. When the last one is
1221 * redrawn notify the top activity by calling
1222 * {@link Activity#onTranslucentConversionComplete}.
1223 *
1224 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1225 * occurred and the activity will be notified immediately.
1226 */
1227 void notifyActivityDrawnLocked(ActivityRecord r) {
1228 if ((r == null)
1229 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1230 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1231 // The last undrawn activity below the top has just been drawn. If there is an
1232 // opaque activity at the top, notify it that it can become translucent safely now.
1233 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1234 mTranslucentActivityWaiting = null;
1235 mUndrawnActivitiesBelowTopTranslucent.clear();
1236 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1237
1238 if (waitingActivity != null && waitingActivity.app != null &&
1239 waitingActivity.app.thread != null) {
1240 try {
1241 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1242 waitingActivity.appToken, r != null);
1243 } catch (RemoteException e) {
1244 }
1245 }
1246 }
1247 }
1248
Craig Mautnera61bc652013-10-28 15:43:18 -07001249 /** If any activities below the top running one are in the INITIALIZING state and they have a
1250 * starting window displayed then remove that starting window. It is possible that the activity
1251 * in this state will never resumed in which case that starting window will be orphaned. */
1252 void cancelInitializingActivities() {
1253 final ActivityRecord topActivity = topRunningActivityLocked(null);
1254 boolean aboveTop = true;
1255 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1256 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1257 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1258 final ActivityRecord r = activities.get(activityNdx);
1259 if (aboveTop) {
1260 if (r == topActivity) {
1261 aboveTop = false;
1262 }
1263 continue;
1264 }
1265
1266 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1267 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1268 r.mStartingWindowShown = false;
1269 mWindowManager.removeAppStartingWindow(r.appToken);
1270 }
1271 }
1272 }
1273 }
1274
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001275 /**
1276 * Ensure that the top activity in the stack is resumed.
1277 *
1278 * @param prev The previously resumed activity, for when in the process
1279 * of pausing; can be null to call from elsewhere.
1280 *
1281 * @return Returns true if something is being resumed, or false if
1282 * nothing happened.
1283 */
1284 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001285 return resumeTopActivityLocked(prev, null);
1286 }
1287
1288 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001289 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1290
Craig Mautnerdf88d732014-01-27 09:21:32 -08001291 ActivityRecord parent = mActivityContainer.mParentActivity;
1292 if (parent != null && parent.state != ActivityState.RESUMED) {
1293 // Do not resume this stack if its parent is not resumed.
1294 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1295 return false;
1296 }
1297
Craig Mautnera61bc652013-10-28 15:43:18 -07001298 cancelInitializingActivities();
1299
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001300 // Find the first activity that is not finishing.
1301 ActivityRecord next = topRunningActivityLocked(null);
1302
1303 // Remember how we'll process this pause/resume situation, and ensure
1304 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001305 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1306 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001307
1308 if (next == null) {
1309 // There are no more activities! Let's just start up the
1310 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001311 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001312 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001313 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001314 // Only resume home if on home display
1315 return isOnHomeDisplay() && mStackSupervisor.resumeHomeActivity(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001316 }
1317
1318 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001319
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001320 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001321 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1322 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001323 // Make sure we have executed any pending transitions, since there
1324 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001325 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001326 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001327 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001328 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001329 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001330 return false;
1331 }
1332
Craig Mautner525f3d92013-05-07 14:01:50 -07001333 final TaskRecord nextTask = next.task;
1334 final TaskRecord prevTask = prev != null ? prev.task : null;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001335 if (prevTask != null && prevTask.mOnTopOfHome && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001336 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001337 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001338 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001339 } else if (prevTask != topTask()) {
Craig Mautnere418ecd2013-05-01 17:02:29 -07001340 // This task is going away but it was supposed to return to the home task.
1341 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001342 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001343 mTaskHistory.get(taskNdx).mOnTopOfHome = true;
Craig Mautnere418ecd2013-05-01 17:02:29 -07001344 } else {
Craig Mautnere0a38842013-12-16 16:14:02 -08001345 if (DEBUG_STATES && isOnHomeDisplay()) Slog.d(TAG,
1346 "resumeTopActivityLocked: Launching home next");
1347 // Only resume home if on home display
1348 return isOnHomeDisplay() && mStackSupervisor.resumeHomeActivity(prev);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001349 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001350 }
1351
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001352 // If we are sleeping, and there is no resumed activity, and the top
1353 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001354 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001355 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001356 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001357 // Make sure we have executed any pending transitions, since there
1358 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001359 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001360 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001361 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001362 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001363 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001364 return false;
1365 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001366
1367 // Make sure that the user who owns this activity is started. If not,
1368 // we will just leave it as is because someone should be bringing
1369 // another user's activities to the top of the stack.
1370 if (mService.mStartedUsers.get(next.userId) == null) {
1371 Slog.w(TAG, "Skipping resume of top activity " + next
1372 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001373 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001374 return false;
1375 }
1376
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001377 // The activity may be waiting for stop, but that is no longer
1378 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001379 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001380 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001381 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001382 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001383
Dianne Hackborn84375872012-06-01 19:03:50 -07001384 next.updateOptionsLocked(options);
1385
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001386 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1387
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001388 // If we are currently pausing an activity, then don't do anything
1389 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001390 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001391 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1392 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001393 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001394 return false;
1395 }
1396
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001397 // Okay we are now going to start a switch, to 'next'. We may first
1398 // have to pause the current activity, but this is an important point
1399 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001400 // XXX "App Redirected" dialog is getting too many false positives
1401 // at this point, so turn off for now.
1402 if (false) {
1403 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1404 long now = SystemClock.uptimeMillis();
1405 final boolean inTime = mLastStartedActivity.startTime != 0
1406 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1407 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1408 final int nextUid = next.info.applicationInfo.uid;
1409 if (inTime && lastUid != nextUid
1410 && lastUid != next.launchedFromUid
1411 && mService.checkPermission(
1412 android.Manifest.permission.STOP_APP_SWITCHES,
1413 -1, next.launchedFromUid)
1414 != PackageManager.PERMISSION_GRANTED) {
1415 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1416 } else {
1417 next.startTime = now;
1418 mLastStartedActivity = next;
1419 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001420 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001421 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001422 mLastStartedActivity = next;
1423 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001424 }
Craig Mautner58547802013-03-05 08:23:53 -08001425
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001426 // We need to start pausing the current activity so the top one
1427 // can be resumed...
Craig Mautner5314a402013-09-26 12:40:16 -07001428 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving);
Craig Mautnereb957862013-04-24 15:34:32 -07001429 if (mResumedActivity != null) {
1430 pausing = true;
1431 startPausingLocked(userLeaving, false);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001432 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001433 }
1434 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001435 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1436 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001437 // At this point we want to put the upcoming activity's process
1438 // at the top of the LRU list, since we know we will be needing it
1439 // very soon and it would be a waste to let it get killed if it
1440 // happens to be sitting towards the end.
1441 if (next.app != null && next.app.thread != null) {
1442 // No reason to do full oom adj update here; we'll let that
1443 // happen whenever it needs to later.
Dianne Hackborndb926082013-10-31 16:32:44 -07001444 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001445 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001446 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001447 return true;
1448 }
1449
Christopher Tated3f175c2012-06-14 14:16:54 -07001450 // If the most recent activity was noHistory but was only stopped rather
1451 // than stopped+finished because the device went to sleep, we need to make
1452 // sure to finish it as we're making a new activity topmost.
Craig Mautner0f922742013-08-06 08:44:42 -07001453 if (mService.mSleeping && mLastNoHistoryActivity != null &&
1454 !mLastNoHistoryActivity.finishing) {
1455 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1456 " on new resume");
1457 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1458 null, "no-history", false);
1459 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001460 }
1461
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001462 if (prev != null && prev != next) {
1463 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1464 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001465 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001466 if (DEBUG_SWITCH) Slog.v(
1467 TAG, "Resuming top, waiting visible to hide: " + prev);
1468 } else {
1469 // The next activity is already visible, so hide the previous
1470 // activity's windows right now so we can show the new one ASAP.
1471 // We only do this if the previous is finishing, which should mean
1472 // it is on top of the one being resumed so hiding it quickly
1473 // is good. Otherwise, we want to do the normal route of allowing
1474 // the resumed activity to be shown so we can decide if the
1475 // previous should actually be hidden depending on whether the
1476 // new one is found to be full-screen or not.
1477 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001478 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001479 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1480 + prev + ", waitingVisible="
1481 + (prev != null ? prev.waitingVisible : null)
1482 + ", nowVisible=" + next.nowVisible);
1483 } else {
1484 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1485 + prev + ", waitingVisible="
1486 + (prev != null ? prev.waitingVisible : null)
1487 + ", nowVisible=" + next.nowVisible);
1488 }
1489 }
1490 }
1491
Dianne Hackborne7f97212011-02-24 14:40:20 -08001492 // Launching this app's activity, make sure the app is no longer
1493 // considered stopped.
1494 try {
1495 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001496 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001497 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001498 } catch (IllegalArgumentException e) {
1499 Slog.w(TAG, "Failed trying to unstop package "
1500 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001501 }
1502
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001503 // We are starting up the next activity, so tell the window manager
1504 // that the previous one will be hidden soon. This way it can know
1505 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001506 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001507 if (prev != null) {
1508 if (prev.finishing) {
1509 if (DEBUG_TRANSITION) Slog.v(TAG,
1510 "Prepare close transition: prev=" + prev);
1511 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001512 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001513 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001514 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001515 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001516 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1517 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001518 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001519 mWindowManager.setAppWillBeHidden(prev.appToken);
1520 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001521 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001522 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001523 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001524 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001525 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001526 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001527 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001528 ? AppTransition.TRANSIT_ACTIVITY_OPEN
1529 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001530 }
1531 }
1532 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001533 mWindowManager.setAppWillBeHidden(prev.appToken);
1534 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001535 }
Craig Mautner967212c2013-04-13 21:10:58 -07001536 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001537 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001538 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001539 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001540 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001541 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001542 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001543 }
1544 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001545
1546 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001547 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001548 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1549 if (opts != null) {
1550 resumeAnimOptions = opts.toBundle();
1551 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001552 next.applyOptionsLocked();
1553 } else {
1554 next.clearOptionsLocked();
1555 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001556
Craig Mautnercf910b02013-04-23 11:23:27 -07001557 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001558 if (next.app != null && next.app.thread != null) {
1559 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1560
1561 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001562 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001563
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001564 // schedule launch ticks to collect information about slow apps.
1565 next.startLaunchTickingLocked();
1566
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001567 ActivityRecord lastResumedActivity =
1568 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001569 ActivityState lastState = next.state;
1570
1571 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001572
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001573 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001574 next.state = ActivityState.RESUMED;
1575 mResumedActivity = next;
1576 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001577 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001578 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001579 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001580 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001581
1582 // Have the window manager re-evaluate the orientation of
1583 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001584 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001585 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001586 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001587 mService.mConfiguration,
1588 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1589 if (config != null) {
1590 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001591 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001592 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001593 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001594
Craig Mautner525f3d92013-05-07 14:01:50 -07001595 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001596 // The configuration update wasn't able to keep the existing
1597 // instance of the activity, and instead started a new one.
1598 // We should be all done, but let's just make sure our activity
1599 // is still at the top and schedule another run if something
1600 // weird happened.
1601 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001602 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001603 "Activity config changed during resume: " + next
1604 + ", new next: " + nextNext);
1605 if (nextNext != next) {
1606 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001607 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001608 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001609 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001610 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001611 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001612 return true;
1613 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001614 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001615 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001616 }
Craig Mautner58547802013-03-05 08:23:53 -08001617
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001618 try {
1619 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001620 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001621 if (a != null) {
1622 final int N = a.size();
1623 if (!next.finishing && N > 0) {
1624 if (DEBUG_RESULTS) Slog.v(
1625 TAG, "Delivering results to " + next
1626 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001627 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001628 }
1629 }
1630
1631 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001632 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001633 }
1634
1635 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001636 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001637 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001638
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001639 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001640 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001641 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001642 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
1643 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001644 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001645
Craig Mautner0eea92c2013-05-16 13:35:39 -07001646 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001647
Craig Mautnerac6f8432013-07-17 13:24:59 -07001648 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001649 } catch (Exception e) {
1650 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001651 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1652 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001653 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001654 if (lastStack != null) {
1655 lastStack.mResumedActivity = lastResumedActivity;
1656 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001657 Slog.i(TAG, "Restarting because process died: " + next);
1658 if (!next.hasBeenLaunched) {
1659 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001660 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1661 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001662 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001663 next.appToken, next.packageName, next.theme,
1664 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001665 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1666 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001667 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001668 mStackSupervisor.startSpecificActivityLocked(next, true, false, resumeAnimOptions);
Craig Mautnercf910b02013-04-23 11:23:27 -07001669 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001670 return true;
1671 }
1672
1673 // From this point on, if something goes wrong there is no way
1674 // to recover the activity.
1675 try {
1676 next.visible = true;
1677 completeResumeLocked(next);
1678 } catch (Exception e) {
1679 // If any exception gets thrown, toss away this
1680 // activity and try the next one.
1681 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001682 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001683 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001684 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001685 return true;
1686 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001687 next.stopped = false;
1688
1689 } else {
1690 // Whoops, need to restart this activity!
1691 if (!next.hasBeenLaunched) {
1692 next.hasBeenLaunched = true;
1693 } else {
1694 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001695 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001696 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001697 mService.compatibilityInfoForPackageLocked(
1698 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001699 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001700 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001701 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001702 }
1703 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1704 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001705 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
Adam Powellcfbe9be2013-11-06 14:58:58 -08001706 mStackSupervisor.startSpecificActivityLocked(next, true, true, resumeAnimOptions);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001707 }
1708
Craig Mautnercf910b02013-04-23 11:23:27 -07001709 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001710 return true;
1711 }
1712
Craig Mautnerac6f8432013-07-17 13:24:59 -07001713 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001714 // If this is being moved to the top by another activity or being launched from the home
1715 // activity, set mOnTopOfHome accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001716 if (isOnHomeDisplay()) {
1717 ActivityStack lastStack = mStackSupervisor.getLastStack();
1718 final boolean fromHome = lastStack.isHomeStack();
1719 if (!isHomeStack() && (fromHome || topTask() != task)) {
1720 task.mOnTopOfHome = fromHome;
1721 }
1722 } else {
1723 task.mOnTopOfHome = false;
Craig Mautner9c85c202013-10-04 20:11:26 -07001724 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001725
Craig Mautnerac6f8432013-07-17 13:24:59 -07001726 mTaskHistory.remove(task);
1727 // Now put task at top.
1728 int stackNdx = mTaskHistory.size();
1729 if (task.userId != mCurrentUser) {
1730 // Put non-current user tasks below current user tasks.
1731 while (--stackNdx >= 0) {
1732 if (mTaskHistory.get(stackNdx).userId != mCurrentUser) {
1733 break;
1734 }
1735 }
1736 ++stackNdx;
1737 }
1738 mTaskHistory.add(stackNdx, task);
1739 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001740
Craig Mautner8849a5e2013-04-02 16:41:03 -07001741 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001742 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001743 TaskRecord rTask = r.task;
1744 final int taskId = rTask.taskId;
1745 if (taskForIdLocked(taskId) == null || newTask) {
Craig Mautner77878772013-03-04 19:46:24 -08001746 // Last activity in task had been removed or ActivityManagerService is reusing task.
1747 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001748 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001749 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001750 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001751 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001752 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001753 if (!newTask) {
1754 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001755 boolean startIt = true;
1756 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1757 task = mTaskHistory.get(taskNdx);
1758 if (task == r.task) {
1759 // Here it is! Now, if this is not yet visible to the
1760 // user, then just add it without starting; it will
1761 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001762 if (!startIt) {
1763 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1764 + task, new RuntimeException("here").fillInStackTrace());
1765 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001766 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001767 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1768 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001769 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001770 r.userId, r.info.configChanges);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001771 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001772 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001773 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001774 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001775 return;
1776 }
1777 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001778 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001779 startIt = false;
1780 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001781 }
1782 }
1783
1784 // Place a new activity at top of stack, so it is next to interact
1785 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001786
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001787 // If we are not placing the new activity frontmost, we do not want
1788 // to deliver the onUserLeaving callback to the actual frontmost
1789 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001790 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001791 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001792 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1793 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001794 }
Craig Mautner70a86932013-02-28 22:37:44 -08001795
1796 task = r.task;
1797
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001798 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08001799 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08001800 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08001801 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001802 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08001803
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001804 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001805 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001806 // We want to show the starting preview window if we are
1807 // switching to a new task, or the next activity's process is
1808 // not currently running.
1809 boolean showStartingIcon = newTask;
1810 ProcessRecord proc = r.app;
1811 if (proc == null) {
1812 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
1813 }
1814 if (proc == null || proc.thread == null) {
1815 showStartingIcon = true;
1816 }
1817 if (DEBUG_TRANSITION) Slog.v(TAG,
1818 "Prepare open transition: starting " + r);
1819 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001820 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001821 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001822 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001823 mWindowManager.prepareAppTransition(newTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08001824 ? AppTransition.TRANSIT_TASK_OPEN
1825 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001826 mNoAnimActivities.remove(r);
1827 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001828 r.updateOptionsLocked(options);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001829 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08001830 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001831 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
1832 r.info.configChanges);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001833 boolean doShow = true;
1834 if (newTask) {
1835 // Even though this activity is starting fresh, we still need
1836 // to reset it to make sure we apply affinities to move any
1837 // existing activities from other tasks in to it.
1838 // If the caller has requested that the target task be
1839 // reset, then do so.
1840 if ((r.intent.getFlags()
1841 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1842 resetTaskIfNeededLocked(r, r);
1843 doShow = topRunningNonDelayedActivityLocked(null) == r;
1844 }
1845 }
1846 if (SHOW_APP_STARTING_PREVIEW && doShow) {
1847 // Figure out if we are transitioning from another activity that is
1848 // "has the same starting icon" as the next one. This allows the
1849 // window manager to keep the previous window it had previously
1850 // created, if it still had one.
1851 ActivityRecord prev = mResumedActivity;
1852 if (prev != null) {
1853 // We don't want to reuse the previous starting preview if:
1854 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07001855 if (prev.task != r.task) {
1856 prev = null;
1857 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001858 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07001859 else if (prev.nowVisible) {
1860 prev = null;
1861 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001862 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001863 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001864 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001865 mService.compatibilityInfoForPackageLocked(
1866 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001867 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08001868 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07001869 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001870 }
1871 } else {
1872 // If this is the first activity, don't do any fancy animations,
1873 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001874 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08001875 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08001876 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
1877 r.info.configChanges);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001878 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001879 }
1880 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001881 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001882 }
1883
1884 if (doResume) {
Craig Mautner858d8a62013-04-23 17:08:34 -07001885 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001886 }
1887 }
1888
Dianne Hackbornbe707852011-11-11 14:32:10 -08001889 final void validateAppTokensLocked() {
1890 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08001891 mValidateAppTokens.ensureCapacity(numActivities());
1892 final int numTasks = mTaskHistory.size();
1893 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
1894 TaskRecord task = mTaskHistory.get(taskNdx);
1895 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07001896 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08001897 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001898 }
Craig Mautner000f0022013-02-26 15:04:29 -08001899 TaskGroup group = new TaskGroup();
1900 group.taskId = task.taskId;
1901 mValidateAppTokens.add(group);
1902 final int numActivities = activities.size();
1903 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1904 final ActivityRecord r = activities.get(activityNdx);
1905 group.tokens.add(r.appToken);
1906 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08001907 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001908 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001909 }
1910
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001911 /**
1912 * Perform a reset of the given task, if needed as part of launching it.
1913 * Returns the new HistoryRecord at the top of the task.
1914 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08001915 /**
1916 * Helper method for #resetTaskIfNeededLocked.
1917 * We are inside of the task being reset... we'll either finish this activity, push it out
1918 * for another task, or leave it as-is.
1919 * @param task The task containing the Activity (taskTop) that might be reset.
1920 * @param forceReset
1921 * @return An ActivityOptions that needs to be processed.
1922 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001923 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001924 ActivityOptions topOptions = null;
1925
1926 int replyChainEnd = -1;
1927 boolean canMoveOptions = true;
1928
1929 // We only do this for activities that are not the root of the task (since if we finish
1930 // the root, we may no longer have the task!).
1931 final ArrayList<ActivityRecord> activities = task.mActivities;
1932 final int numActivities = activities.size();
1933 for (int i = numActivities - 1; i > 0; --i ) {
1934 ActivityRecord target = activities.get(i);
1935
1936 final int flags = target.info.flags;
1937 final boolean finishOnTaskLaunch =
1938 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
1939 final boolean allowTaskReparenting =
1940 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
1941 final boolean clearWhenTaskReset =
1942 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
1943
1944 if (!finishOnTaskLaunch
1945 && !clearWhenTaskReset
1946 && target.resultTo != null) {
1947 // If this activity is sending a reply to a previous
1948 // activity, we can't do anything with it now until
1949 // we reach the start of the reply chain.
1950 // XXX note that we are assuming the result is always
1951 // to the previous activity, which is almost always
1952 // the case but we really shouldn't count on.
1953 if (replyChainEnd < 0) {
1954 replyChainEnd = i;
1955 }
1956 } else if (!finishOnTaskLaunch
1957 && !clearWhenTaskReset
1958 && allowTaskReparenting
1959 && target.taskAffinity != null
1960 && !target.taskAffinity.equals(task.affinity)) {
1961 // If this activity has an affinity for another
1962 // task, then we need to move it out of here. We will
1963 // move it as far out of the way as possible, to the
1964 // bottom of the activity stack. This also keeps it
1965 // correctly ordered with any activities we previously
1966 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08001967 final ThumbnailHolder newThumbHolder;
1968 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07001969 final ActivityRecord bottom =
1970 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08001971 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07001972 if (bottom != null && target.taskAffinity != null
1973 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08001974 // If the activity currently at the bottom has the
1975 // same task affinity as the one we are moving,
1976 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08001977 targetTask = bottom.task;
1978 newThumbHolder = bottom.thumbHolder == null ? targetTask : bottom.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001979 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07001980 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08001981 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08001982 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
1983 null, false);
1984 newThumbHolder = targetTask;
1985 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08001986 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
1987 + " out to new task " + target.task);
1988 }
1989
Craig Mautner329f4122013-11-07 09:10:42 -08001990 if (clearWhenTaskReset) {
1991 // This is the start of a new sub-task.
1992 if (target.thumbHolder == null) {
1993 target.thumbHolder = new ThumbnailHolder();
1994 }
1995 } else {
1996 target.thumbHolder = newThumbHolder;
1997 }
1998
Craig Mautnere3a74d52013-02-22 14:14:58 -08001999 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002000 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002001
Craig Mautner525f3d92013-05-07 14:01:50 -07002002 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002003 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2004 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002005 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002006 if (p.finishing) {
2007 continue;
2008 }
2009
Craig Mautner525f3d92013-05-07 14:01:50 -07002010 ThumbnailHolder curThumbHolder = p.thumbHolder;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002011 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002012 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002013 topOptions = p.takeOptionsLocked();
2014 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002015 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002016 }
2017 }
2018 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002019 + task + " adding to task=" + targetTask
2020 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002021 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2022 + " out to target's task " + target.task);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002023 p.setTask(targetTask, curThumbHolder, false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002024 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002025
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002026 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002027 }
2028
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002029 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002030 if (VALIDATE_TOKENS) {
2031 validateAppTokensLocked();
2032 }
2033
2034 replyChainEnd = -1;
2035 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2036 // If the activity should just be removed -- either
2037 // because it asks for it, or the task should be
2038 // cleared -- then finish it and anything that is
2039 // part of its reply chain.
2040 int end;
2041 if (clearWhenTaskReset) {
2042 // In this case, we want to finish this activity
2043 // and everything above it, so be sneaky and pretend
2044 // like these are all in the reply chain.
2045 end = numActivities - 1;
2046 } else if (replyChainEnd < 0) {
2047 end = i;
2048 } else {
2049 end = replyChainEnd;
2050 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002051 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002052 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002053 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002054 if (p.finishing) {
2055 continue;
2056 }
2057 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002058 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002059 topOptions = p.takeOptionsLocked();
2060 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002061 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002062 }
2063 }
Craig Mautner58547802013-03-05 08:23:53 -08002064 if (DEBUG_TASKS) Slog.w(TAG,
2065 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002066 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002067 end--;
2068 srcPos--;
2069 }
2070 }
2071 replyChainEnd = -1;
2072 } else {
2073 // If we were in the middle of a chain, well the
2074 // activity that started it all doesn't want anything
2075 // special, so leave it all as-is.
2076 replyChainEnd = -1;
2077 }
2078 }
2079
2080 return topOptions;
2081 }
2082
2083 /**
2084 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2085 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2086 * @param affinityTask The task we are looking for an affinity to.
2087 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2088 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2089 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2090 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002091 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002092 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002093 int replyChainEnd = -1;
2094 final int taskId = task.taskId;
2095 final String taskAffinity = task.affinity;
2096
2097 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2098 final int numActivities = activities.size();
2099 // Do not operate on the root Activity.
2100 for (int i = numActivities - 1; i > 0; --i) {
2101 ActivityRecord target = activities.get(i);
2102
2103 final int flags = target.info.flags;
2104 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2105 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2106
2107 if (target.resultTo != null) {
2108 // If this activity is sending a reply to a previous
2109 // activity, we can't do anything with it now until
2110 // we reach the start of the reply chain.
2111 // XXX note that we are assuming the result is always
2112 // to the previous activity, which is almost always
2113 // the case but we really shouldn't count on.
2114 if (replyChainEnd < 0) {
2115 replyChainEnd = i;
2116 }
2117 } else if (topTaskIsHigher
2118 && allowTaskReparenting
2119 && taskAffinity != null
2120 && taskAffinity.equals(target.taskAffinity)) {
2121 // This activity has an affinity for our task. Either remove it if we are
2122 // clearing or move it over to our task. Note that
2123 // we currently punt on the case where we are resetting a
2124 // task that is not at the top but who has activities above
2125 // with an affinity to it... this is really not a normal
2126 // case, and we will need to later pull that task to the front
2127 // and usually at that point we will do the reset and pick
2128 // up those remaining activities. (This only happens if
2129 // someone starts an activity in a new task from an activity
2130 // in a task that is not currently on top.)
2131 if (forceReset || finishOnTaskLaunch) {
2132 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2133 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2134 for (int srcPos = start; srcPos >= i; --srcPos) {
2135 final ActivityRecord p = activities.get(srcPos);
2136 if (p.finishing) {
2137 continue;
2138 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002139 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002140 }
2141 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002142 if (taskInsertionPoint < 0) {
2143 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002144
Craig Mautner77878772013-03-04 19:46:24 -08002145 }
Craig Mautner77878772013-03-04 19:46:24 -08002146
2147 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2148 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2149 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2150 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002151 final ActivityRecord p = activities.get(srcPos);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002152 p.setTask(task, null, false);
Craig Mautner77878772013-03-04 19:46:24 -08002153 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002154
Craig Mautnere3a74d52013-02-22 14:14:58 -08002155 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2156 + " to stack at " + task,
2157 new RuntimeException("here").fillInStackTrace());
2158 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2159 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002160 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002161 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002162 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002163 if (VALIDATE_TOKENS) {
2164 validateAppTokensLocked();
2165 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002166
2167 // Now we've moved it in to place... but what if this is
2168 // a singleTop activity and we have put it on top of another
2169 // instance of the same activity? Then we drop the instance
2170 // below so it remains singleTop.
2171 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2172 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002173 int targetNdx = taskActivities.indexOf(target);
2174 if (targetNdx > 0) {
2175 ActivityRecord p = taskActivities.get(targetNdx - 1);
2176 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002177 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2178 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002179 }
2180 }
2181 }
2182 }
2183
2184 replyChainEnd = -1;
2185 }
2186 }
Craig Mautner77878772013-03-04 19:46:24 -08002187 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002188 }
2189
Craig Mautner8849a5e2013-04-02 16:41:03 -07002190 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002191 ActivityRecord newActivity) {
2192 boolean forceReset =
2193 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2194 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2195 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2196 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2197 forceReset = true;
2198 }
2199 }
2200
2201 final TaskRecord task = taskTop.task;
2202
2203 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2204 * for remaining tasks. Used for later tasks to reparent to task. */
2205 boolean taskFound = false;
2206
2207 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2208 ActivityOptions topOptions = null;
2209
Craig Mautner77878772013-03-04 19:46:24 -08002210 // Preserve the location for reparenting in the new task.
2211 int reparentInsertionPoint = -1;
2212
Craig Mautnere3a74d52013-02-22 14:14:58 -08002213 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2214 final TaskRecord targetTask = mTaskHistory.get(i);
2215
2216 if (targetTask == task) {
2217 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2218 taskFound = true;
2219 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002220 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2221 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002222 }
2223 }
2224
Craig Mautner70a86932013-02-28 22:37:44 -08002225 int taskNdx = mTaskHistory.indexOf(task);
2226 do {
2227 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2228 } while (taskTop == null && taskNdx >= 0);
2229
Craig Mautnere3a74d52013-02-22 14:14:58 -08002230 if (topOptions != null) {
2231 // If we got some ActivityOptions from an activity on top that
2232 // was removed from the task, propagate them to the new real top.
2233 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002234 taskTop.updateOptionsLocked(topOptions);
2235 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002236 topOptions.abort();
2237 }
2238 }
2239
2240 return taskTop;
2241 }
2242
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002243 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2244 String resultWho, int requestCode, int resultCode, Intent data) {
2245
2246 if (callingUid > 0) {
2247 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Dianne Hackborn7e269642010-08-25 19:50:20 -07002248 data, r.getUriPermissionsLocked());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002249 }
2250
2251 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2252 + " : who=" + resultWho + " req=" + requestCode
2253 + " res=" + resultCode + " data=" + data);
2254 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2255 try {
2256 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2257 list.add(new ResultInfo(resultWho, requestCode,
2258 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002259 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002260 return;
2261 } catch (Exception e) {
2262 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2263 }
2264 }
2265
2266 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2267 }
2268
Craig Mautner04f0b702013-10-22 12:31:01 -07002269 private void adjustFocusedActivityLocked(ActivityRecord r) {
2270 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2271 ActivityRecord next = topRunningActivityLocked(null);
2272 if (next != r) {
2273 final TaskRecord task = r.task;
2274 if (r.frontOfTask && task == topTask() && task.mOnTopOfHome) {
2275 mStackSupervisor.moveHomeToTop();
2276 }
2277 }
2278 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked());
2279 }
2280 }
2281
Craig Mautnerf3333272013-04-22 10:55:53 -07002282 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002283 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2284 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2285 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2286 if (!r.finishing) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002287 if (!mService.mSleeping) {
2288 if (DEBUG_STATES) {
2289 Slog.d(TAG, "no-history finish of " + r);
2290 }
2291 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002292 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002293 } else {
2294 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2295 + " on stop because we're just sleeping");
2296 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002297 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002298 }
2299
2300 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002301 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002302 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002303 try {
2304 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002305 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2306 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002307 r.state = ActivityState.STOPPING;
2308 if (DEBUG_VISBILITY) Slog.v(
2309 TAG, "Stopping visible=" + r.visible + " for " + r);
2310 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002311 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002312 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002313 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002314 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002315 r.setSleeping(true);
2316 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002317 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002318 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002319 } catch (Exception e) {
2320 // Maybe just ignore exceptions here... if the process
2321 // has crashed, our death notification will clean things
2322 // up.
2323 Slog.w(TAG, "Exception thrown during pause", e);
2324 // Just in case, assume it to be stopped.
2325 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002326 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002327 r.state = ActivityState.STOPPED;
2328 if (r.configDestroy) {
Dianne Hackborn28695e02011-11-02 21:59:51 -07002329 destroyActivityLocked(r, true, false, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002330 }
2331 }
2332 }
2333 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002334
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002335 /**
2336 * @return Returns true if the activity is being finished, false if for
2337 * some reason it is being left as-is.
2338 */
2339 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002340 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002341 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002342 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002343 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002344 + ", result=" + resultCode + ", data=" + resultData
2345 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002346 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002347 return false;
2348 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002349
Craig Mautnerd44711d2013-02-23 11:24:36 -08002350 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002351 return true;
2352 }
2353
Craig Mautnerd2328952013-03-05 12:46:26 -08002354 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002355 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2356 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2357 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2358 ActivityRecord r = activities.get(activityNdx);
2359 if (r.resultTo == self && r.requestCode == requestCode) {
2360 if ((r.resultWho == null && resultWho == null) ||
2361 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2362 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2363 false);
2364 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002365 }
2366 }
2367 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002368 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002369 }
2370
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002371 final void finishTopRunningActivityLocked(ProcessRecord app) {
2372 ActivityRecord r = topRunningActivityLocked(null);
2373 if (r != null && r.app == app) {
2374 // If the top running activity is from this crashing
2375 // process, then terminate it to avoid getting in a loop.
2376 Slog.w(TAG, " Force finishing activity "
2377 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002378 int taskNdx = mTaskHistory.indexOf(r.task);
2379 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002380 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002381 // Also terminate any activities below it that aren't yet
2382 // stopped, to avoid a situation where one will get
2383 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002384 --activityNdx;
2385 if (activityNdx < 0) {
2386 do {
2387 --taskNdx;
2388 if (taskNdx < 0) {
2389 break;
2390 }
2391 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2392 } while (activityNdx < 0);
2393 }
2394 if (activityNdx >= 0) {
2395 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002396 if (r.state == ActivityState.RESUMED
2397 || r.state == ActivityState.PAUSING
2398 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002399 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002400 Slog.w(TAG, " Force finishing activity "
2401 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002402 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002403 }
2404 }
2405 }
2406 }
2407 }
2408
Craig Mautnerd2328952013-03-05 12:46:26 -08002409 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002410 ArrayList<ActivityRecord> activities = r.task.mActivities;
2411 for (int index = activities.indexOf(r); index >= 0; --index) {
2412 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002413 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002414 break;
2415 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002416 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002417 }
2418 return true;
2419 }
2420
Dianne Hackborn5c607432012-02-28 14:44:19 -08002421 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2422 // send the result
2423 ActivityRecord resultTo = r.resultTo;
2424 if (resultTo != null) {
2425 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2426 + " who=" + r.resultWho + " req=" + r.requestCode
2427 + " res=" + resultCode + " data=" + resultData);
2428 if (r.info.applicationInfo.uid > 0) {
2429 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2430 resultTo.packageName, resultData,
2431 resultTo.getUriPermissionsLocked());
2432 }
2433 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2434 resultData);
2435 r.resultTo = null;
2436 }
2437 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2438
2439 // Make sure this HistoryRecord is not holding on to other resources,
2440 // because clients have remote IPC references to this object so we
2441 // can't assume that will go away and want to avoid circular IPC refs.
2442 r.results = null;
2443 r.pendingResults = null;
2444 r.newIntents = null;
2445 r.icicle = null;
2446 }
2447
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002448 /**
2449 * @return Returns true if this activity has been removed from the history
2450 * list, or false if it is still in the list and will be removed later.
2451 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002452 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2453 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002454 if (r.finishing) {
2455 Slog.w(TAG, "Duplicate finish request for " + r);
2456 return false;
2457 }
2458
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002459 r.makeFinishing();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002460 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002461 r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002462 r.task.taskId, r.shortComponentName, reason);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002463 final ArrayList<ActivityRecord> activities = r.task.mActivities;
2464 final int index = activities.indexOf(r);
2465 if (index < (activities.size() - 1)) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002466 r.task.setFrontOfTask();
Craig Mautnerd44711d2013-02-23 11:24:36 -08002467 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2468 // If the caller asked that this activity (and all above it)
2469 // be cleared when the task is reset, don't lose that information,
2470 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002471 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002472 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002473 }
2474 }
2475
2476 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002477
2478 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002479
Dianne Hackborn5c607432012-02-28 14:44:19 -08002480 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002481
Craig Mautnerc8143c62013-09-03 12:15:57 -07002482 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002483 // There are clients waiting to receive thumbnails so, in case
2484 // this is an activity that someone is waiting for, add it
2485 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002486 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002487 }
2488
Craig Mautnerde4ef022013-04-07 19:01:33 -07002489 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002490 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002491 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002492 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002493 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002494 ? AppTransition.TRANSIT_TASK_CLOSE
2495 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002496
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002497 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002498 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002499
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002500 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002501 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2502 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
2503 startPausingLocked(false, false);
2504 }
2505
2506 } else if (r.state != ActivityState.PAUSING) {
2507 // If the activity is PAUSING, we will complete the finish once
2508 // it is done pausing; else we can just directly finish it here.
2509 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002510 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002511 } else {
2512 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2513 }
2514
2515 return false;
2516 }
2517
Craig Mautnerf3333272013-04-22 10:55:53 -07002518 static final int FINISH_IMMEDIATELY = 0;
2519 static final int FINISH_AFTER_PAUSE = 1;
2520 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002521
Craig Mautnerf3333272013-04-22 10:55:53 -07002522 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002523 // First things first: if this activity is currently visible,
2524 // and the resumed activity is not yet visible, then hold off on
2525 // finishing until the resumed one becomes visible.
2526 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002527 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2528 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002529 if (mStackSupervisor.mStoppingActivities.size() > 3
2530 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002531 // If we already have a few activities waiting to stop,
2532 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002533 // them out. Or if r is the last of activity of the last task the stack
2534 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002535 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002536 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002537 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002538 }
2539 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002540 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2541 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002542 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002543 if (oomAdj) {
2544 mService.updateOomAdjLocked();
2545 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002546 return r;
2547 }
2548
2549 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002550 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002551 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002552 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002553 if (mResumedActivity == r) {
2554 mResumedActivity = null;
2555 }
2556 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002557 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002558 r.state = ActivityState.FINISHING;
2559
2560 if (mode == FINISH_IMMEDIATELY
2561 || prevState == ActivityState.STOPPED
2562 || prevState == ActivityState.INITIALIZING) {
2563 // If this activity is already stopped, we can just finish
2564 // it right now.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002565 boolean activityRemoved = destroyActivityLocked(r, true,
2566 oomAdj, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002567 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002568 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002569 }
2570 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002571 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002572
2573 // Need to go through the full pause cycle to get this
2574 // activity into the stopped state and then finish it.
2575 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002576 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002577 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002578 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002579 return r;
2580 }
2581
Craig Mautnerd2328952013-03-05 12:46:26 -08002582 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002583 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002584 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002585 final TaskRecord task = srec.task;
2586 final ArrayList<ActivityRecord> activities = task.mActivities;
2587 final int start = activities.indexOf(srec);
2588 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002589 return false;
2590 }
2591 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002592 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002593 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002594 final ComponentName dest = destIntent.getComponent();
2595 if (start > 0 && dest != null) {
2596 for (int i = finishTo; i >= 0; i--) {
2597 ActivityRecord r = activities.get(i);
2598 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002599 r.info.name.equals(dest.getClassName())) {
2600 finishTo = i;
2601 parent = r;
2602 foundParentInTask = true;
2603 break;
2604 }
2605 }
2606 }
2607
2608 IActivityController controller = mService.mController;
2609 if (controller != null) {
2610 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2611 if (next != null) {
2612 // ask watcher if this is allowed
2613 boolean resumeOK = true;
2614 try {
2615 resumeOK = controller.activityResuming(next.packageName);
2616 } catch (RemoteException e) {
2617 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002618 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002619 }
2620
2621 if (!resumeOK) {
2622 return false;
2623 }
2624 }
2625 }
2626 final long origId = Binder.clearCallingIdentity();
2627 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002628 ActivityRecord r = activities.get(i);
2629 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002630 // Only return the supplied result for the first activity finished
2631 resultCode = Activity.RESULT_CANCELED;
2632 resultData = null;
2633 }
2634
2635 if (parent != null && foundParentInTask) {
2636 final int parentLaunchMode = parent.info.launchMode;
2637 final int destIntentFlags = destIntent.getFlags();
2638 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2639 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2640 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2641 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2642 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2643 } else {
2644 try {
2645 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2646 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002647 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002648 null, aInfo, parent.appToken, null,
2649 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08002650 0, null, true, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002651 foundParentInTask = res == ActivityManager.START_SUCCESS;
2652 } catch (RemoteException e) {
2653 foundParentInTask = false;
2654 }
2655 requestFinishActivityLocked(parent.appToken, resultCode,
2656 resultData, "navigate-up", true);
2657 }
2658 }
2659 Binder.restoreCallingIdentity(origId);
2660 return foundParentInTask;
2661 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002662 /**
2663 * Perform the common clean-up of an activity record. This is called both
2664 * as part of destroyActivityLocked() (when destroying the client-side
2665 * representation) and cleaning things up as a result of its hosting
2666 * processing going away, in which case there is no remaining client-side
2667 * state to destroy so only the cleanup here is needed.
2668 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002669 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2670 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002671 if (mResumedActivity == r) {
2672 mResumedActivity = null;
2673 }
2674 if (mService.mFocusedActivity == r) {
2675 mService.mFocusedActivity = null;
2676 }
2677
2678 r.configDestroy = false;
2679 r.frozenBeforeDestroy = false;
2680
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002681 if (setState) {
2682 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2683 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002684 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002685 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002686 }
2687
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002688 // Make sure this record is no longer in the pending finishes list.
2689 // This could happen, for example, if we are trimming activities
2690 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002691 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002692 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07002693
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002694 // Remove any pending results.
2695 if (r.finishing && r.pendingResults != null) {
2696 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2697 PendingIntentRecord rec = apr.get();
2698 if (rec != null) {
2699 mService.cancelIntentSenderLocked(rec, false);
2700 }
2701 }
2702 r.pendingResults = null;
2703 }
2704
2705 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07002706 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002707 }
2708
Craig Mautnerc8143c62013-09-03 12:15:57 -07002709 if (!mService.mPendingThumbnails.isEmpty()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002710 // There are clients waiting to receive thumbnails so, in case
2711 // this is an activity that someone is waiting for, add it
2712 // to the pending list so we can correctly update the clients.
Craig Mautnerf3333272013-04-22 10:55:53 -07002713 mStackSupervisor.mCancelledThumbnails.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002714 }
2715
2716 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002717 removeTimeoutsForActivityLocked(r);
2718 }
2719
2720 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07002721 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002722 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002723 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002724 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002725 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002726 }
2727
Craig Mautner04a0ea62014-01-13 12:51:26 -08002728 private void removeActivityFromHistoryLocked(ActivityRecord r) {
Craig Mautner34b73df2014-01-12 21:11:08 -08002729 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002730 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
2731 r.makeFinishing();
2732 if (DEBUG_ADD_REMOVE) {
2733 RuntimeException here = new RuntimeException("here");
2734 here.fillInStackTrace();
2735 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002736 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002737 final TaskRecord task = r.task;
2738 if (task != null && task.removeActivity(r)) {
Craig Mautnera9a3fb12013-04-18 10:01:00 -07002739 if (DEBUG_STACK) Slog.i(TAG,
2740 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautnere1db0dd2013-10-13 18:15:00 -07002741 if (mStackSupervisor.isFrontStack(this) && task == topTask() && task.mOnTopOfHome) {
Craig Mautner8e569572013-10-11 17:36:59 -07002742 mStackSupervisor.moveHomeToTop();
2743 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08002744 removeTask(task);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002745 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002746 r.takeFromHistory();
2747 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08002748 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002749 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002750 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002751 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002752 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002753 if (VALIDATE_TOKENS) {
2754 validateAppTokensLocked();
2755 }
2756 cleanUpActivityServicesLocked(r);
2757 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002758 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002759
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002760 /**
2761 * Perform clean-up of service connections in an activity record.
2762 */
2763 final void cleanUpActivityServicesLocked(ActivityRecord r) {
2764 // Throw away any services that have been bound by this activity.
2765 if (r.connections != null) {
2766 Iterator<ConnectionRecord> it = r.connections.iterator();
2767 while (it.hasNext()) {
2768 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07002769 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002770 }
2771 r.connections = null;
2772 }
2773 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002774
2775 final void scheduleDestroyActivities(ProcessRecord owner, boolean oomAdj, String reason) {
2776 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
2777 msg.obj = new ScheduleDestroyArgs(owner, oomAdj, reason);
2778 mHandler.sendMessage(msg);
2779 }
2780
Dianne Hackborn28695e02011-11-02 21:59:51 -07002781 final void destroyActivitiesLocked(ProcessRecord owner, boolean oomAdj, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002782 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002783 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002784 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2785 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2786 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2787 final ActivityRecord r = activities.get(activityNdx);
2788 if (r.finishing) {
2789 continue;
2790 }
2791 if (r.fullscreen) {
2792 lastIsOpaque = true;
2793 }
2794 if (owner != null && r.app != owner) {
2795 continue;
2796 }
2797 if (!lastIsOpaque) {
2798 continue;
2799 }
2800 // We can destroy this one if we have its icicle saved and
2801 // it is not in the process of pausing/stopping/finishing.
2802 if (r.app != null && r != mResumedActivity && r != mPausingActivity
2803 && r.haveState && !r.visible && r.stopped
2804 && r.state != ActivityState.DESTROYING
2805 && r.state != ActivityState.DESTROYED) {
2806 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
2807 + " resumed=" + mResumedActivity
2808 + " pausing=" + mPausingActivity);
2809 if (destroyActivityLocked(r, true, oomAdj, reason)) {
2810 activityRemoved = true;
2811 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002812 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002813 }
2814 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002815 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002816 mStackSupervisor.resumeTopActivitiesLocked();
2817
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002818 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002819 }
2820
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002821 /**
2822 * Destroy the current CLIENT SIDE instance of an activity. This may be
2823 * called both when actually finishing an activity, or when performing
2824 * a configuration switch where we destroy the current client-side object
2825 * but then create a new client-side object for this same HistoryRecord.
2826 */
2827 final boolean destroyActivityLocked(ActivityRecord r,
Dianne Hackborn28695e02011-11-02 21:59:51 -07002828 boolean removeFromApp, boolean oomAdj, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002829 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07002830 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002831 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
2832 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002833 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07002834 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002835
2836 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002837
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002838 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002839
2840 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002841
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002842 if (hadApp) {
2843 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002844 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002845 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
2846 mService.mHeavyWeightProcess = null;
2847 mService.mHandler.sendEmptyMessage(
2848 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
2849 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07002850 if (r.app.activities.isEmpty()) {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07002851 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07002852 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002853 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002854 }
2855 }
2856
2857 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002858
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002859 try {
2860 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002861 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002862 r.configChangeFlags);
2863 } catch (Exception e) {
2864 // We can just ignore exceptions here... if the process
2865 // has crashed, our death notification will clean things
2866 // up.
2867 //Slog.w(TAG, "Exception thrown during finish", e);
2868 if (r.finishing) {
2869 removeActivityFromHistoryLocked(r);
2870 removedFromHistory = true;
2871 skipDestroy = true;
2872 }
2873 }
2874
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002875 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002876
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002877 // If the activity is finishing, we need to wait on removing it
2878 // from the list to give it a chance to do its cleanup. During
2879 // that time it may make calls back with its token so we need to
2880 // be able to find it on the list and so we don't want to remove
2881 // it from the list yet. Otherwise, we can just immediately put
2882 // it in the destroyed state since we are not removing it from the
2883 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002884 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002885 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
2886 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002887 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002888 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002889 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
2890 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002891 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
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 } else {
2897 // remove this record from the history.
2898 if (r.finishing) {
2899 removeActivityFromHistoryLocked(r);
2900 removedFromHistory = true;
2901 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002902 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002903 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002904 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002905 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002906 }
2907 }
2908
2909 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002910
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002911 if (!mLRUActivities.remove(r) && hadApp) {
2912 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
2913 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002914
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002915 return removedFromHistory;
2916 }
2917
Craig Mautnerd2328952013-03-05 12:46:26 -08002918 final void activityDestroyedLocked(IBinder token) {
2919 final long origId = Binder.clearCallingIdentity();
2920 try {
2921 ActivityRecord r = ActivityRecord.forToken(token);
2922 if (r != null) {
2923 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002924 }
Craig Mautnerd2328952013-03-05 12:46:26 -08002925
2926 if (isInStackLocked(token) != null) {
2927 if (r.state == ActivityState.DESTROYING) {
2928 cleanUpActivityLocked(r, true, false);
2929 removeActivityFromHistoryLocked(r);
2930 }
2931 }
Craig Mautner05d29032013-05-03 13:40:13 -07002932 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08002933 } finally {
2934 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002935 }
2936 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002937
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002938 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
2939 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002940 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002941 if (DEBUG_CLEANUP) Slog.v(
2942 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002943 + " with " + i + " entries");
2944 while (i > 0) {
2945 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002946 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002947 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002948 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002949 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002950 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002951 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002952 }
2953 }
2954 }
2955
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002956 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
2957 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002958 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
2959 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002960 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
2961 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07002962 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002963 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07002964 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
2965 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002966
2967 boolean hasVisibleActivities = false;
2968
2969 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08002970 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002971 if (DEBUG_CLEANUP) Slog.v(
2972 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08002973 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2974 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2975 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2976 final ActivityRecord r = activities.get(activityNdx);
2977 --i;
2978 if (DEBUG_CLEANUP) Slog.v(
2979 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2980 if (r.app == app) {
2981 boolean remove;
2982 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
2983 // Don't currently have state for the activity, or
2984 // it is finishing -- always remove it.
2985 remove = true;
2986 } else if (r.launchCount > 2 &&
2987 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
2988 // We have launched this activity too many times since it was
2989 // able to run, so give up and remove it.
2990 remove = true;
2991 } else {
2992 // The process may be gone, but the activity lives on!
2993 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002994 }
Craig Mautner0247fc82013-02-28 14:32:06 -08002995 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002996 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002997 RuntimeException here = new RuntimeException("here");
2998 here.fillInStackTrace();
2999 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3000 + ": haveState=" + r.haveState
3001 + " stateNotNeeded=" + r.stateNotNeeded
3002 + " finishing=" + r.finishing
3003 + " state=" + r.state, here);
3004 }
3005 if (!r.finishing) {
3006 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3007 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3008 r.userId, System.identityHashCode(r),
3009 r.task.taskId, r.shortComponentName,
3010 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003011 if (r.state == ActivityState.RESUMED) {
3012 mService.updateUsageStats(r, false);
3013 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003014 }
3015 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003016
Craig Mautner0247fc82013-02-28 14:32:06 -08003017 } else {
3018 // We have the current state for this activity, so
3019 // it can be restarted later when needed.
3020 if (localLOGV) Slog.v(
3021 TAG, "Keeping entry, setting app to null");
3022 if (r.visible) {
3023 hasVisibleActivities = true;
3024 }
3025 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3026 + r);
3027 r.app = null;
3028 r.nowVisible = false;
3029 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003030 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003031 "App died, clearing saved state of " + r);
3032 r.icicle = null;
3033 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003034 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003035
Craig Mautnerd2328952013-03-05 12:46:26 -08003036 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003037 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003038 }
3039 }
3040
3041 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003042 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003043
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003044 final void updateTransitLocked(int transit, Bundle options) {
3045 if (options != null) {
3046 ActivityRecord r = topRunningActivityLocked(null);
3047 if (r != null && r.state != ActivityState.RESUMED) {
3048 r.updateOptionsLocked(options);
3049 } else {
3050 ActivityOptions.abort(options);
3051 }
3052 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003053 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003054 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003055
Craig Mautnera82aa092013-09-13 15:34:08 -07003056 void moveHomeTaskToTop() {
3057 final int top = mTaskHistory.size() - 1;
3058 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3059 final TaskRecord task = mTaskHistory.get(taskNdx);
3060 if (task.isHomeTask()) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003061 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG, "moveHomeTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003062 mTaskHistory.remove(taskNdx);
3063 mTaskHistory.add(top, task);
3064 mWindowManager.moveTaskToTop(task.taskId);
3065 return;
3066 }
3067 }
3068 }
3069
Craig Mautnercae015f2013-02-08 14:31:27 -08003070 final boolean findTaskToMoveToFrontLocked(int taskId, int flags, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003071 final TaskRecord task = taskForIdLocked(taskId);
3072 if (task != null) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003073 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003074 mStackSupervisor.mUserLeaving = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003075 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003076 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
3077 // Caller wants the home activity moved with it. To accomplish this,
Craig Mautnere418ecd2013-05-01 17:02:29 -07003078 // we'll just indicate that this task returns to the home task.
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003079 task.mOnTopOfHome = true;
Craig Mautneraab647e2013-02-28 16:31:36 -08003080 }
3081 moveTaskToFrontLocked(task, null, options);
3082 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003083 }
3084 return false;
3085 }
3086
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003087 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003088 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003089
Craig Mautner11bf9a52013-02-19 14:08:51 -08003090 final int numTasks = mTaskHistory.size();
3091 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003092 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003093 // nothing to do!
3094 if (reason != null &&
3095 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3096 ActivityOptions.abort(options);
3097 } else {
3098 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3099 }
3100 return;
3101 }
3102
Craig Mautnere0a38842013-12-16 16:14:02 -08003103 moveToFront();
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003104
Craig Mautner11bf9a52013-02-19 14:08:51 -08003105 // Shift all activities with this task up to the top
3106 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003107 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003108
3109 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003110 if (reason != null &&
3111 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003112 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003113 ActivityRecord r = topRunningActivityLocked(null);
3114 if (r != null) {
3115 mNoAnimActivities.add(r);
3116 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003117 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003118 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003119 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003120 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003121
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003122 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003123
Craig Mautner05d29032013-05-03 13:40:13 -07003124 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003125 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003126
3127 if (VALIDATE_TOKENS) {
3128 validateAppTokensLocked();
3129 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003130 }
3131
3132 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003133 * Worker method for rearranging history stack. Implements the function of moving all
3134 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003135 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003136 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003137 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3138 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003139 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003140 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003141 * @return Returns true if the move completed, false if not.
3142 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003143 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
3144 Slog.i(TAG, "moveTaskToBack: " + taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003145
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003146 // If we have a watcher, preflight the move before committing to it. First check
3147 // for *other* available tasks, but if none are available, then try again allowing the
3148 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003149 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003150 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003151 if (next == null) {
3152 next = topRunningActivityLocked(null, 0);
3153 }
3154 if (next != null) {
3155 // ask watcher if this is allowed
3156 boolean moveOK = true;
3157 try {
3158 moveOK = mService.mController.activityResuming(next.packageName);
3159 } catch (RemoteException e) {
3160 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003161 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003162 }
3163 if (!moveOK) {
3164 return false;
3165 }
3166 }
3167 }
3168
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003169 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003170 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003171
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003172 final TaskRecord tr = taskForIdLocked(taskId);
Craig Mautnerd2328952013-03-05 12:46:26 -08003173 if (tr == null) {
3174 return false;
3175 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003176
Craig Mautner11bf9a52013-02-19 14:08:51 -08003177 mTaskHistory.remove(tr);
3178 mTaskHistory.add(0, tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003179
Craig Mautnerc8143c62013-09-03 12:15:57 -07003180 // There is an assumption that moving a task to the back moves it behind the home activity.
3181 // We make sure here that some activity in the stack will launch home.
3182 ActivityRecord lastActivity = null;
3183 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003184 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3185 final TaskRecord task = mTaskHistory.get(taskNdx);
3186 if (task.mOnTopOfHome) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003187 break;
3188 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003189 if (taskNdx == 1) {
3190 // Set the last task before tr to go to home.
3191 task.mOnTopOfHome = true;
3192 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003193 }
3194
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003195 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003196 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3197 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003198 ActivityRecord r = topRunningActivityLocked(null);
3199 if (r != null) {
3200 mNoAnimActivities.add(r);
3201 }
3202 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003203 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003204 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003205 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003206
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003207 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003208 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003209 }
3210
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003211 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Craig Mautnere0a38842013-12-16 16:14:02 -08003212 if (task == tr && tr.mOnTopOfHome || numTasks <= 1 && isOnHomeDisplay()) {
Daniel 2 Olofsson9cdf9e52013-12-16 13:24:25 +01003213 tr.mOnTopOfHome = false;
Craig Mautner69ada552013-04-18 13:51:51 -07003214 return mStackSupervisor.resumeHomeActivity(null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003215 }
3216
Craig Mautner05d29032013-05-03 13:40:13 -07003217 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003218 return true;
3219 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003220
Craig Mautner8849a5e2013-04-02 16:41:03 -07003221 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003222 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003223 final Uri data = r.intent.getData();
3224 final String strData = data != null ? data.toSafeString() : null;
3225
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003226 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003227 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003228 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003229 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003230 }
3231
3232 /**
3233 * Make sure the given activity matches the current configuration. Returns
3234 * false if the activity had to be destroyed. Returns true if the
3235 * configuration is the same, or the activity will remain running as-is
3236 * for whatever reason. Ensures the HistoryRecord is updated with the
3237 * correct configuration and all other bookkeeping is handled.
3238 */
3239 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3240 int globalChanges) {
3241 if (mConfigWillChange) {
3242 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3243 "Skipping config check (will change): " + r);
3244 return true;
3245 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003246
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003247 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3248 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003249
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003250 // Short circuit: if the two configurations are the exact same
3251 // object (the common case), then there is nothing to do.
3252 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003253 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003254 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3255 "Configuration unchanged in " + r);
3256 return true;
3257 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003258
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003259 // We don't worry about activities that are finishing.
3260 if (r.finishing) {
3261 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3262 "Configuration doesn't matter in finishing " + r);
3263 r.stopFreezingScreenLocked(false);
3264 return true;
3265 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003266
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003267 // Okay we now are going to make this activity have the new config.
3268 // But then we need to figure out how it needs to deal with that.
3269 Configuration oldConfig = r.configuration;
3270 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003271
3272 // Determine what has changed. May be nothing, if this is a config
3273 // that has come back from the app after going idle. In that case
3274 // we just want to leave the official config object now in the
3275 // activity and do nothing else.
3276 final int changes = oldConfig.diff(newConfig);
3277 if (changes == 0 && !r.forceNewConfig) {
3278 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3279 "Configuration no differences in " + r);
3280 return true;
3281 }
3282
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003283 // If the activity isn't currently running, just leave the new
3284 // configuration and it will pick that up next time it starts.
3285 if (r.app == null || r.app.thread == null) {
3286 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3287 "Configuration doesn't matter not running " + r);
3288 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003289 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003290 return true;
3291 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003292
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003293 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003294 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3295 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3296 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003297 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003298 + ", newConfig=" + newConfig);
3299 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003300 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003301 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3302 r.configChangeFlags |= changes;
3303 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003304 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003305 if (r.app == null || r.app.thread == null) {
3306 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003307 "Config is destroying non-running " + r);
Dianne Hackborn28695e02011-11-02 21:59:51 -07003308 destroyActivityLocked(r, true, false, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003309 } else if (r.state == ActivityState.PAUSING) {
3310 // A little annoying: we are waiting for this activity to
3311 // finish pausing. Let's not do anything now, but just
3312 // flag that it needs to be restarted when done pausing.
3313 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003314 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003315 r.configDestroy = true;
3316 return true;
3317 } else if (r.state == ActivityState.RESUMED) {
3318 // Try to optimize this case: the configuration is changing
3319 // and we need to restart the top, resumed activity.
3320 // Instead of doing the normal handshaking, just say
3321 // "restart!".
3322 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003323 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003324 relaunchActivityLocked(r, r.configChangeFlags, true);
3325 r.configChangeFlags = 0;
3326 } else {
3327 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003328 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003329 relaunchActivityLocked(r, r.configChangeFlags, false);
3330 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003331 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003332
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003333 // All done... tell the caller we weren't able to keep this
3334 // activity around.
3335 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003336 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003337
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003338 // Default case: the activity can handle this new configuration, so
3339 // hand it over. Note that we don't need to give it the new
3340 // configuration, since we always send configuration changes to all
3341 // process when they happen so it can just use whatever configuration
3342 // it last got.
3343 if (r.app != null && r.app.thread != null) {
3344 try {
3345 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003346 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003347 } catch (RemoteException e) {
3348 // If process died, whatever.
3349 }
3350 }
3351 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003352
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003353 return true;
3354 }
3355
Craig Mautnerc8143c62013-09-03 12:15:57 -07003356 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003357 int changes, boolean andResume) {
3358 List<ResultInfo> results = null;
3359 List<Intent> newIntents = null;
3360 if (andResume) {
3361 results = r.results;
3362 newIntents = r.newIntents;
3363 }
3364 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3365 + " with results=" + results + " newIntents=" + newIntents
3366 + " andResume=" + andResume);
3367 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003368 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003369 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003370
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003371 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003372
Craig Mautner34b73df2014-01-12 21:11:08 -08003373 mStackSupervisor.removeChildActivityContainers(r);
3374
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003375 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003376 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3377 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3378 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003379 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003380 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003381 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003382 // Note: don't need to call pauseIfSleepingLocked() here, because
3383 // the caller will only pass in 'andResume' if this activity is
3384 // currently resumed, which implies we aren't sleeping.
3385 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003386 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003387 }
3388
3389 if (andResume) {
3390 r.results = null;
3391 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003392 r.state = ActivityState.RESUMED;
3393 } else {
3394 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3395 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003396 }
3397
3398 return true;
3399 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003400
3401 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003402 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3403 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3404 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3405 final ActivityRecord r = activities.get(activityNdx);
3406 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003407 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003408 }
3409 if (r.fullscreen && !r.finishing) {
3410 return false;
3411 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003412 }
3413 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003414 final ActivityRecord r = ActivityRecord.forToken(token);
3415 if (r == null) {
3416 return false;
3417 }
3418 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3419 + " would have returned true for r=" + r);
3420 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003421 }
3422
3423 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003424 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3425 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3426 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3427 final ActivityRecord r = activities.get(activityNdx);
3428 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003429 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003430 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003431 }
3432 }
3433 }
3434
3435 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3436 boolean didSomething = false;
3437 TaskRecord lastTask = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003438 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3439 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3440 int numActivities = activities.size();
3441 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3442 ActivityRecord r = activities.get(activityNdx);
3443 final boolean samePackage = r.packageName.equals(name)
3444 || (name == null && r.userId == userId);
3445 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3446 && (samePackage || r.task == lastTask)
3447 && (r.app == null || evenPersistent || !r.app.persistent)) {
3448 if (!doit) {
3449 if (r.finishing) {
3450 // If this activity is just finishing, then it is not
3451 // interesting as far as something to stop.
3452 continue;
3453 }
3454 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003455 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003456 didSomething = true;
3457 Slog.i(TAG, " Force finishing activity " + r);
3458 if (samePackage) {
3459 if (r.app != null) {
3460 r.app.removed = true;
3461 }
3462 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003463 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003464 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003465 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3466 true)) {
3467 // r has been deleted from mActivities, accommodate.
3468 --numActivities;
3469 --activityNdx;
3470 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003471 }
3472 }
3473 }
3474 return didSomething;
3475 }
3476
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003477 ActivityRecord getTasksLocked(IThumbnailReceiver receiver,
Craig Mautneraab647e2013-02-28 16:31:36 -08003478 PendingThumbnailsRecord pending, List<RunningTaskInfo> list) {
Craig Mautnercae015f2013-02-08 14:31:27 -08003479 ActivityRecord topRecord = null;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003480 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003481 final TaskRecord task = mTaskHistory.get(taskNdx);
3482 ActivityRecord r = null;
3483 ActivityRecord top = null;
3484 int numActivities = 0;
3485 int numRunning = 0;
3486 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003487 if (activities.isEmpty()) {
3488 continue;
3489 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003490 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3491 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003492
Craig Mautneraab647e2013-02-28 16:31:36 -08003493 // Initialize state for next task if needed.
3494 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3495 top = r;
3496 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003497 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003498
3499 // Add 'r' into the current task.
3500 numActivities++;
3501 if (r.app != null && r.app.thread != null) {
3502 numRunning++;
3503 }
3504
3505 if (localLOGV) Slog.v(
3506 TAG, r.intent.getComponent().flattenToShortString()
3507 + ": task=" + r.task);
3508 }
3509
3510 RunningTaskInfo ci = new RunningTaskInfo();
3511 ci.id = task.taskId;
3512 ci.baseActivity = r.intent.getComponent();
3513 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003514 ci.lastActiveTime = task.lastActiveTime;
3515
Craig Mautneraab647e2013-02-28 16:31:36 -08003516 if (top.thumbHolder != null) {
3517 ci.description = top.thumbHolder.lastDescription;
3518 }
3519 ci.numActivities = numActivities;
3520 ci.numRunning = numRunning;
3521 //System.out.println(
3522 // "#" + maxNum + ": " + " descr=" + ci.description);
3523 if (receiver != null) {
3524 if (localLOGV) Slog.v(
3525 TAG, "State=" + top.state + "Idle=" + top.idle
3526 + " app=" + top.app
3527 + " thr=" + (top.app != null ? top.app.thread : null));
3528 if (top.state == ActivityState.RESUMED || top.state == ActivityState.PAUSING) {
3529 if (top.idle && top.app != null && top.app.thread != null) {
3530 topRecord = top;
3531 } else {
3532 top.thumbnailNeeded = true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003533 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003534 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003535 pending.pendingRecords.add(top);
Craig Mautnercae015f2013-02-08 14:31:27 -08003536 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003537 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003538 }
3539 return topRecord;
3540 }
3541
3542 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003543 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003544 if (DEBUG_SWITCH) Slog.d(
3545 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003546 if (top >= 0) {
3547 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3548 int activityTop = activities.size() - 1;
3549 if (activityTop > 0) {
3550 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3551 "unhandled-back", true);
3552 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003553 }
3554 }
3555
Craig Mautner6b74cb52013-09-27 17:02:21 -07003556 /**
3557 * Reset local parameters because an app's activity died.
3558 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003559 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003560 */
3561 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003562 if (mPausingActivity != null && mPausingActivity.app == app) {
3563 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3564 "App died while pausing: " + mPausingActivity);
3565 mPausingActivity = null;
3566 }
3567 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3568 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003569 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003570 }
3571
Craig Mautner19091252013-10-05 00:03:53 -07003572 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003573 }
3574
Craig Mautnercae015f2013-02-08 14:31:27 -08003575 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003576 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3577 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3578 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3579 final ActivityRecord r = activities.get(activityNdx);
3580 if (r.app == app) {
3581 Slog.w(TAG, " Force finishing activity "
3582 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08003583 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003584 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003585 }
3586 }
3587 }
3588
Dianne Hackborn390517b2013-05-30 15:03:32 -07003589 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003590 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003591 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003592 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3593 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003594 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3595 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003596 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003597 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003598 if (printed) {
3599 header = null;
3600 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003601 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003602 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003603 }
3604
3605 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3606 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3607
3608 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003609 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3610 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08003611 }
3612 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003613 final int top = mTaskHistory.size() - 1;
3614 if (top >= 0) {
3615 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
3616 int listTop = list.size() - 1;
3617 if (listTop >= 0) {
3618 activities.add(list.get(listTop));
3619 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003620 }
3621 } else {
3622 ItemMatcher matcher = new ItemMatcher();
3623 matcher.build(name);
3624
Craig Mautneraab647e2013-02-28 16:31:36 -08003625 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3626 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
3627 if (matcher.match(r1, r1.intent.getComponent())) {
3628 activities.add(r1);
3629 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003630 }
3631 }
3632 }
3633
3634 return activities;
3635 }
3636
3637 ActivityRecord restartPackage(String packageName) {
3638 ActivityRecord starting = topRunningActivityLocked(null);
3639
3640 // All activities that came from the package must be
3641 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08003642 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3643 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3644 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3645 final ActivityRecord a = activities.get(activityNdx);
3646 if (a.info.packageName.equals(packageName)) {
3647 a.forceNewConfig = true;
3648 if (starting != null && a == starting && a.visible) {
3649 a.startFreezingScreenLocked(starting.app,
3650 ActivityInfo.CONFIG_SCREEN_LAYOUT);
3651 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003652 }
3653 }
3654 }
3655
3656 return starting;
3657 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003658
Craig Mautner04a0ea62014-01-13 12:51:26 -08003659 void removeTask(TaskRecord task) {
3660 mWindowManager.removeTask(task.taskId);
3661 final ActivityRecord r = mResumedActivity;
3662 if (r != null && r.task == task) {
3663 mResumedActivity = null;
3664 }
3665
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003666 final int taskNdx = mTaskHistory.indexOf(task);
3667 final int topTaskNdx = mTaskHistory.size() - 1;
3668 if (task.mOnTopOfHome && taskNdx < topTaskNdx) {
3669 mTaskHistory.get(taskNdx + 1).mOnTopOfHome = true;
3670 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003671 mTaskHistory.remove(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08003672
3673 if (mTaskHistory.isEmpty()) {
3674 if (DEBUG_STACK) Slog.i(TAG, "removeTask: moving to back stack=" + this);
3675 if (isOnHomeDisplay()) {
3676 mStackSupervisor.moveHomeStack(!isHomeStack());
3677 }
Craig Mautner593a4e62014-01-15 17:55:51 -08003678 if (mStacks != null) {
3679 mStacks.remove(this);
3680 mStacks.add(0, this);
3681 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08003682 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003683 }
3684
Craig Mautnerde4ef022013-04-07 19:01:33 -07003685 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent, boolean toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003686 TaskRecord task = new TaskRecord(taskId, info, intent);
3687 addTask(task, toTop);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08003688 return task;
3689 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08003690
3691 ArrayList<TaskRecord> getAllTasks() {
3692 return new ArrayList<TaskRecord>(mTaskHistory);
3693 }
3694
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003695 void addTask(final TaskRecord task, final boolean toTop) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08003696 task.stack = this;
3697 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07003698 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08003699 } else {
3700 mTaskHistory.add(0, task);
3701 }
3702 }
3703
3704 public int getStackId() {
3705 return mStackId;
3706 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003707
3708 @Override
3709 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07003710 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
3711 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07003712 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003713}