blob: b955011bc3b2bc4eb66632ad26d9a3ad2d12aaa7 [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
Craig Mautner84984fa2014-06-19 11:19:20 -070033import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
34import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070035
Craig Mautner0eea92c2013-05-16 13:35:39 -070036import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE;
37import static com.android.server.am.ActivityStackSupervisor.DEBUG_APP;
Craig Mautnerd163e752014-06-13 17:18:47 -070038import static com.android.server.am.ActivityStackSupervisor.DEBUG_CONTAINERS;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070039import static com.android.server.am.ActivityStackSupervisor.DEBUG_RELEASE;
Craig Mautner0eea92c2013-05-16 13:35:39 -070040import static com.android.server.am.ActivityStackSupervisor.DEBUG_SAVED_STATE;
Winson Chung376543b2014-05-21 17:43:28 -070041import static com.android.server.am.ActivityStackSupervisor.DEBUG_SCREENSHOTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070042import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
Craig Mautnerde4ef022013-04-07 19:01:33 -070043import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
44
Dianne Hackborn89ad4562014-08-24 16:45:38 -070045import android.util.ArraySet;
Dianne Hackborn91097de2014-04-04 18:02:06 -070046import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070047import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070048import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080049import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080050import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080051import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080052import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070053import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054
55import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070056import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070057import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080059import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080061import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070062import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080067import android.content.res.Resources;
68import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080069import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070070import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070071import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080072import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070073import android.os.Handler;
74import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090075import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070077import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078import android.os.RemoteException;
79import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080080import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070081import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070082import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070083import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070084import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070085import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070086
Craig Mautnercae015f2013-02-08 14:31:27 -080087import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080088import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070089import java.lang.ref.WeakReference;
90import java.util.ArrayList;
91import java.util.Iterator;
92import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080093import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070094
95/**
96 * State and management of a single stack of activities.
97 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070098final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080099
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700100 // Ticks during which we check progress while waiting for an app to launch.
101 static final int LAUNCH_TICK = 500;
102
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700103 // How long we wait until giving up on the last activity to pause. This
104 // is short because it directly impacts the responsiveness of starting the
105 // next activity.
106 static final int PAUSE_TIMEOUT = 500;
107
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700108 // How long we wait for the activity to tell us it has stopped before
109 // giving up. This is a good amount of time because we really need this
110 // from the application in order to get its saved state.
111 static final int STOP_TIMEOUT = 10*1000;
112
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700113 // How long we wait until giving up on an activity telling us it has
114 // finished destroying itself.
115 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800116
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700117 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800118 // disabled.
119 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800120
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700121 // How long between activity launches that we consider safe to not warn
122 // the user about an unexpected activity being launched on top.
123 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800124
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700125 // Set to false to disable the preview that is shown while a new activity
126 // is being started.
127 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800128
Craig Mautner5eda9b32013-07-02 11:58:16 -0700129 // How long to wait for all background Activities to redraw following a call to
130 // convertToTranslucent().
131 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
132
Craig Mautnera61bc652013-10-28 15:43:18 -0700133 static final boolean SCREENSHOT_FORCE_565 = ActivityManager.isLowRamDeviceStatic();
John Reck172e87c2013-10-02 16:55:16 -0700134
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700135 enum ActivityState {
136 INITIALIZING,
137 RESUMED,
138 PAUSING,
139 PAUSED,
140 STOPPING,
141 STOPPED,
142 FINISHING,
143 DESTROYING,
144 DESTROYED
145 }
146
147 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700148 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800149
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700150 /**
151 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800152 * running) activities. It contains #TaskRecord objects.
153 */
154 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
155
156 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800157 * Used for validating app tokens with window manager.
158 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800159 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800160
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700161 /**
162 * List of running activities, sorted by recent usage.
163 * The first entry in the list is the least recently used.
164 * It contains HistoryRecord objects.
165 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700166 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700167
168 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700169 * Animations that for the current transition have requested not to
170 * be considered for the transition animation.
171 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700172 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700173
174 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700175 * When we are in the process of pausing an activity, before starting the
176 * next one, this variable holds the activity that is currently being paused.
177 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800178 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700179
180 /**
181 * This is the last activity that we put into the paused state. This is
182 * used to determine if we need to do an activity transition while sleeping,
183 * when we normally hold the top activity paused.
184 */
185 ActivityRecord mLastPausedActivity = null;
186
187 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700188 * Activities that specify No History must be removed once the user navigates away from them.
189 * If the device goes to sleep with such an activity in the paused state then we save it here
190 * and finish it later if another activity replaces it on wakeup.
191 */
192 ActivityRecord mLastNoHistoryActivity = null;
193
194 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700195 * Current activity that is resumed, or null if there is none.
196 */
197 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800198
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700199 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700200 * This is the last activity that has been started. It is only used to
201 * identify when multiple activities are started at once so that the user
202 * can be warned they may not be in the activity they think they are.
203 */
204 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800205
Craig Mautner5eda9b32013-07-02 11:58:16 -0700206 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
207 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
208 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
209 // Activity in mTranslucentActivityWaiting is notified via
210 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
211 // background activity being drawn then the same call will be made with a true value.
212 ActivityRecord mTranslucentActivityWaiting = null;
Craig Mautnereb8abf72014-07-02 15:04:09 -0700213 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
Craig Mautner5eda9b32013-07-02 11:58:16 -0700214 new ArrayList<ActivityRecord>();
215
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700216 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700217 * Set when we know we are going to be calling updateConfiguration()
218 * soon, so want to skip intermediate config checks.
219 */
220 boolean mConfigWillChange;
221
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700222 long mLaunchStartTime = 0;
223 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800224
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800225 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800226 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
227 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
228 */
229 private ActivityRecord mLastScreenshotActivity = null;
230 private Bitmap mLastScreenshotBitmap = null;
231
Craig Mautner858d8a62013-04-23 17:08:34 -0700232 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700233
Craig Mautnerc00204b2013-03-05 15:02:14 -0800234 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800235 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800236 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
237 ArrayList<ActivityStack> mStacks;
238 /** The attached Display's unique identifier, or -1 if detached */
239 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800240
Craig Mautner27084302013-03-25 08:05:25 -0700241 /** Run all ActivityStacks through this */
242 final ActivityStackSupervisor mStackSupervisor;
243
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700244 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700245 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
246 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
247 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
248 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700249 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700250 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700251 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700252
253 static class ScheduleDestroyArgs {
254 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700255 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700256 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700257 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700258 mReason = reason;
259 }
260 }
261
Zoran Marcetaf958b322012-08-09 20:27:12 +0900262 final Handler mHandler;
263
264 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700265 //public Handler() {
266 // if (localLOGV) Slog.v(TAG, "Handler started!");
267 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700268 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900269 super(looper);
270 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700271
Zoran Marcetaf958b322012-08-09 20:27:12 +0900272 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700273 public void handleMessage(Message msg) {
274 switch (msg.what) {
275 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800276 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700277 // We don't at this point know if the activity is fullscreen,
278 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800279 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700280 synchronized (mService) {
281 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700282 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700283 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700284 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800285 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700286 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700287 case LAUNCH_TICK_MSG: {
288 ActivityRecord r = (ActivityRecord)msg.obj;
289 synchronized (mService) {
290 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700291 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700292 }
293 }
294 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700295 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800296 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700297 // We don't at this point know if the activity is fullscreen,
298 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800299 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800300 synchronized (mService) {
301 activityDestroyedLocked(r != null ? r.appToken : null);
302 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700303 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700304 case STOP_TIMEOUT_MSG: {
305 ActivityRecord r = (ActivityRecord)msg.obj;
306 // We don't at this point know if the activity is fullscreen,
307 // so we need to be conservative and assume it isn't.
308 Slog.w(TAG, "Activity stop timeout for " + r);
309 synchronized (mService) {
310 if (r.isInHistory()) {
311 activityStoppedLocked(r, null, null, null);
312 }
313 }
314 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700315 case DESTROY_ACTIVITIES_MSG: {
316 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
317 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700318 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700319 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700320 } break;
321 case TRANSLUCENT_TIMEOUT_MSG: {
322 synchronized (mService) {
323 notifyActivityDrawnLocked(null);
324 }
325 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700326 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700327 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700328 final ActivityRecord r = getVisibleBehindActivity();
329 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700330 if (r != null) {
331 mService.killAppAtUsersRequest(r.app, null);
332 }
333 }
334 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700335 }
336 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800337 }
338
Craig Mautner34b73df2014-01-12 21:11:08 -0800339 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800340 int count = 0;
341 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
342 count += mTaskHistory.get(taskNdx).mActivities.size();
343 }
344 return count;
345 }
346
Craig Mautner4a1cb222013-12-04 16:14:06 -0800347 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer) {
348 mActivityContainer = activityContainer;
349 mStackSupervisor = activityContainer.getOuter();
350 mService = mStackSupervisor.mService;
351 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
352 mWindowManager = mService.mWindowManager;
353 mStackId = activityContainer.mStackId;
354 mCurrentUser = mService.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700355 }
Craig Mautner5962b122012-10-05 14:45:52 -0700356
Amith Yamasani734983f2014-03-04 16:48:05 -0800357 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100358 * Checks whether the userid is a profile of the current user.
Amith Yamasani734983f2014-03-04 16:48:05 -0800359 */
Kenny Guy2a764942014-04-02 13:29:20 +0100360 private boolean isCurrentProfileLocked(int userId) {
Kenny Guyf4824a02014-04-02 19:17:41 +0100361 if (userId == mCurrentUser) return true;
Kenny Guy2a764942014-04-02 13:29:20 +0100362 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
363 if (mService.mCurrentProfileIds[i] == userId) return true;
Amith Yamasani734983f2014-03-04 16:48:05 -0800364 }
365 return false;
366 }
367
368 boolean okToShowLocked(ActivityRecord r) {
Kenny Guy2a764942014-04-02 13:29:20 +0100369 return isCurrentProfileLocked(r.userId)
Craig Mautner5962b122012-10-05 14:45:52 -0700370 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
371 }
372
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700373 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800374 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700375 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
376 if (r != null) {
377 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800378 }
379 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700380 return null;
381 }
382
383 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800384 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
385 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800386 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800387 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
388 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800389 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800390 return r;
391 }
392 }
393 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700394 return null;
395 }
396
397 /**
398 * This is a simplified version of topRunningActivityLocked that provides a number of
399 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800400 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700401 * @param token If non-null, any history records matching this token will be skipped.
402 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800403 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700404 * @return Returns the HistoryRecord of the next activity on the stack.
405 */
406 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800407 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
408 TaskRecord task = mTaskHistory.get(taskNdx);
409 if (task.taskId == taskId) {
410 continue;
411 }
412 ArrayList<ActivityRecord> activities = task.mActivities;
413 for (int i = activities.size() - 1; i >= 0; --i) {
414 final ActivityRecord r = activities.get(i);
415 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800416 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800417 return r;
418 }
419 }
420 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700421 return null;
422 }
423
Craig Mautner8849a5e2013-04-02 16:41:03 -0700424 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700425 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
426 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700427 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
428 final ActivityRecord r = activities.get(activityNdx);
429 if (!r.finishing) {
430 return r;
431 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700432 }
433 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700434 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700435 }
436
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700437 final TaskRecord topTask() {
438 final int size = mTaskHistory.size();
439 if (size > 0) {
440 return mTaskHistory.get(size - 1);
441 }
442 return null;
443 }
444
Craig Mautnerd2328952013-03-05 12:46:26 -0800445 TaskRecord taskForIdLocked(int id) {
446 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
447 final TaskRecord task = mTaskHistory.get(taskNdx);
448 if (task.taskId == id) {
449 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800450 }
451 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700452 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700453 }
454
Craig Mautnerd2328952013-03-05 12:46:26 -0800455 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800456 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800457 if (r != null) {
458 final TaskRecord task = r.task;
Dianne Hackborn925e2b32014-09-03 16:40:49 -0700459 if (task != null && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Craig Mautnerd2328952013-03-05 12:46:26 -0800460 if (task.stack != this) Slog.w(TAG,
461 "Illegal state! task does not point to stack it is in.");
462 return r;
463 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800464 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800465 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800466 }
467
Craig Mautner2420ead2013-04-01 17:13:20 -0700468 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700469 final boolean hadit = mLRUActivities.remove(r);
470 mLRUActivities.add(r);
471 return hadit;
472 }
473
Craig Mautnerde4ef022013-04-07 19:01:33 -0700474 final boolean isHomeStack() {
475 return mStackId == HOME_STACK_ID;
476 }
477
Craig Mautnere0a38842013-12-16 16:14:02 -0800478 final boolean isOnHomeDisplay() {
479 return isAttached() &&
480 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
481 }
482
483 final void moveToFront() {
484 if (isAttached()) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800485 if (isOnHomeDisplay()) {
486 mStackSupervisor.moveHomeStack(isHomeStack());
487 }
Craig Mautnerfefeef82014-01-10 08:40:23 -0800488 mStacks.remove(this);
489 mStacks.add(this);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800490 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800491 }
492
493 final boolean isAttached() {
494 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800495 }
496
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700497 /**
498 * Returns the top activity in any existing task matching the given
499 * Intent. Returns null if no such task is found.
500 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700501 ActivityRecord findTaskLocked(ActivityRecord target) {
502 Intent intent = target.intent;
503 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700504 ComponentName cls = intent.getComponent();
505 if (info.targetActivity != null) {
506 cls = new ComponentName(info.packageName, info.targetActivity);
507 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700508 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700509 boolean isDocument = intent != null & intent.isDocument();
510 // If documentData is non-null then it must match the existing task data.
511 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800512
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700513 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800514 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
515 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700516 if (task.voiceSession != null) {
517 // We never match voice sessions; those always run independently.
518 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": voice session");
519 continue;
520 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700521 if (task.userId != userId) {
522 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700523 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700524 continue;
525 }
Craig Mautner000f0022013-02-26 15:04:29 -0800526 final ActivityRecord r = task.getTopActivity();
527 if (r == null || r.finishing || r.userId != userId ||
528 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700529 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800530 continue;
531 }
532
Craig Mautnerd00f4742014-03-12 14:17:26 -0700533 final Intent taskIntent = task.intent;
534 final Intent affinityIntent = task.affinityIntent;
535 final boolean taskIsDocument;
536 final Uri taskDocumentData;
537 if (taskIntent != null && taskIntent.isDocument()) {
538 taskIsDocument = true;
539 taskDocumentData = taskIntent.getData();
540 } else if (affinityIntent != null && affinityIntent.isDocument()) {
541 taskIsDocument = true;
542 taskDocumentData = affinityIntent.getData();
543 } else {
544 taskIsDocument = false;
545 taskDocumentData = null;
546 }
547
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700548 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700549 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700550 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700551 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700552 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
553 if (task.rootAffinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700554 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700555 return r;
556 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700557 } else if (taskIntent != null && taskIntent.getComponent() != null &&
558 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700559 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700560 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800561 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700562 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
563 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800564 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700565 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
566 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700567 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700568 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800569 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700570 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
571 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800572 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700573 } else if (DEBUG_TASKS) {
574 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700575 }
576 }
577
578 return null;
579 }
580
581 /**
582 * Returns the first activity (starting from the top of the stack) that
583 * is the same as the given activity. Returns null if no such activity
584 * is found.
585 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700586 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700587 ComponentName cls = intent.getComponent();
588 if (info.targetActivity != null) {
589 cls = new ComponentName(info.packageName, info.targetActivity);
590 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700591 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700592
Craig Mautner000f0022013-02-26 15:04:29 -0800593 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700594 TaskRecord task = mTaskHistory.get(taskNdx);
Kenny Guy2a764942014-04-02 13:29:20 +0100595 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700596 return null;
597 }
598 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800599 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
600 ActivityRecord r = activities.get(activityNdx);
601 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700602 //Slog.i(TAG, "Found matching class!");
603 //dump();
604 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
605 return r;
606 }
607 }
608 }
609
610 return null;
611 }
612
Amith Yamasani742a6712011-05-04 14:49:28 -0700613 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700614 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700615 */
Craig Mautner93529a42013-10-04 15:03:13 -0700616 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800617 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700618 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800619 }
620 mCurrentUser = userId;
621
622 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800623 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800624 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700625 TaskRecord task = mTaskHistory.get(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100626 if (isCurrentProfileLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700627 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
628 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800629 mTaskHistory.remove(i);
630 mTaskHistory.add(task);
631 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800632 // Use same value for i.
633 } else {
634 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800635 }
636 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700637 if (VALIDATE_TOKENS) {
638 validateAppTokensLocked();
639 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700640 }
641
Craig Mautner2420ead2013-04-01 17:13:20 -0700642 void minimalResumeActivityLocked(ActivityRecord r) {
643 r.state = ActivityState.RESUMED;
644 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
645 + " (starting new instance)");
646 r.stopped = false;
647 mResumedActivity = r;
648 r.task.touchActiveTime();
649 mService.addRecentTaskLocked(r.task);
650 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700651 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700652 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700653 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700654 }
655
Dianne Hackborncee04b52013-07-03 17:01:28 -0700656 private void startLaunchTraces() {
657 if (mFullyDrawnStartTime != 0) {
658 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
659 }
660 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
661 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
662 }
663
664 private void stopFullyDrawnTraceIfNeeded() {
665 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
666 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
667 mFullyDrawnStartTime = 0;
668 }
669 }
670
Craig Mautnere79d42682013-04-01 19:01:53 -0700671 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700672 if (r.displayStartTime == 0) {
673 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
674 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700675 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700676 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700677 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700678 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700679 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700680 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700681 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700682 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800683
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700684 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700685 // Make sure that there is no activity waiting for this to launch.
686 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
687 r.displayStartTime = r.fullyDrawnStartTime = 0;
688 } else {
689 mStackSupervisor.removeTimeoutsForActivityLocked(r);
690 mStackSupervisor.scheduleIdleTimeoutLocked(r);
691 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700692 }
693
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800694 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800695 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800696 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
697 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
698 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
699 activities.get(activityNdx).setSleeping(false);
700 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800701 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800702 }
703
Craig Mautner0eea92c2013-05-16 13:35:39 -0700704 /**
705 * @return true if something must be done before going to sleep.
706 */
707 boolean checkReadyForSleepLocked() {
708 if (mResumedActivity != null) {
709 // Still have something resumed; can't sleep until it is paused.
710 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
711 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700712 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700713 return true;
714 }
715 if (mPausingActivity != null) {
716 // Still waiting for something to pause; can't sleep yet.
717 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
718 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800719 }
720
Craig Mautner0eea92c2013-05-16 13:35:39 -0700721 return false;
722 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800723
Craig Mautner0eea92c2013-05-16 13:35:39 -0700724 void goToSleep() {
725 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800726
Craig Mautner0eea92c2013-05-16 13:35:39 -0700727 // Make sure any stopped but visible activities are now sleeping.
728 // This ensures that the activity's onStop() is called.
729 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
730 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
731 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
732 final ActivityRecord r = activities.get(activityNdx);
733 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
734 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800735 }
736 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800737 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700738 }
Craig Mautner59c00972012-07-30 12:10:24 -0700739
Winson Chung376543b2014-05-21 17:43:28 -0700740 /**
741 * This resets the saved state from the last screenshot, forcing a new screenshot to be taken
742 * again when requested.
743 */
744 private void invalidateLastScreenshot() {
745 mLastScreenshotActivity = null;
746 if (mLastScreenshotBitmap != null) {
747 mLastScreenshotBitmap.recycle();
748 }
749 mLastScreenshotBitmap = null;
750 }
751
Dianne Hackbornd2835932010-12-13 16:28:46 -0800752 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700753 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800754 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700755 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800756 return null;
757 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800758
Winson Chung083baf92014-07-11 10:32:42 -0700759 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700760 // This is an optimization -- since we never show Home or Recents within Recents itself,
761 // we can just go ahead and skip taking the screenshot if this is the home stack. In
762 // the case where the most recent task is not the task that was supplied, then the stack
763 // has changed, so invalidate the last screenshot().
764 invalidateLastScreenshot();
765 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tIs Home stack? " + isHomeStack());
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700766 return null;
767 }
768
Winson Chung48a10a52014-08-27 14:36:51 -0700769 int w = mService.mThumbnailWidth;
770 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800771 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800772 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700773 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800774 || mLastScreenshotBitmap.getWidth() != w
775 || mLastScreenshotBitmap.getHeight() != h) {
Winson Chung376543b2014-05-21 17:43:28 -0700776 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tUpdating screenshot");
Craig Mautnerb12428a2012-12-20 16:07:06 -0800777 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700778 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700779 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800780 }
781 if (mLastScreenshotBitmap != null) {
Winson Chung376543b2014-05-21 17:43:28 -0700782 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tReusing last screenshot");
John Reck172e87c2013-10-02 16:55:16 -0700783 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800784 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800785 }
Winson Chung376543b2014-05-21 17:43:28 -0700786 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800787 return null;
788 }
789
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700790 /**
791 * Start pausing the currently resumed activity. It is an error to call this if there
792 * is already an activity being paused or there is no resumed activity.
793 *
794 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
795 * @param uiSleeping True if this is happening with the user interface going to sleep (the
796 * screen turning off).
797 * @param resuming True if this is being called as part of resuming the top activity, so
798 * we shouldn't try to instigate a resume here.
799 * @param dontWait True if the caller does not want to wait for the pause to complete. If
800 * set to true, we will immediately complete the pause here before returning.
801 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
802 * it to tell us when it is done.
803 */
804 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
805 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800806 if (mPausingActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700807 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity);
808 completePauseLocked(false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800809 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700810 ActivityRecord prev = mResumedActivity;
811 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700812 if (!resuming) {
813 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
814 mStackSupervisor.resumeTopActivitiesLocked();
815 }
816 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700817 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800818
819 if (mActivityContainer.mParentActivity == null) {
820 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700821 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800822 }
823
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700824 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
825 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700826 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800827 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700828 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700829 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
830 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700831 prev.state = ActivityState.PAUSING;
832 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700833 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700834 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwaled3e2a082014-11-04 16:09:19 -0800835 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700836 prev.updateThumbnail(screenshotActivities(prev), null);
837 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700838 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700839
840 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800841
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700842 if (prev.app != null && prev.app.thread != null) {
843 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
844 try {
845 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700846 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700847 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700848 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800849 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700850 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700851 } catch (Exception e) {
852 // Ignore exception, if process died other code will cleanup.
853 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800854 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700855 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700856 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700857 }
858 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800859 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700860 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700861 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700862 }
863
864 // If we are not going to sleep, we want to ensure the device is
865 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700866 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700867 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700868 }
869
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800870 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700871 // Have the window manager pause its key dispatching until the new
872 // activity has started. If we're pausing the activity just because
873 // the screen is being turned off and the UI is sleeping, don't interrupt
874 // key dispatch; the same activity will pick it up again on wakeup.
875 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800876 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700877 } else {
878 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
879 }
880
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700881 if (dontWait) {
882 // If the caller said they don't want to wait for the pause, then complete
883 // the pause now.
884 completePauseLocked(false);
885 return false;
886
887 } else {
888 // Schedule a pause timeout in case the app doesn't respond.
889 // We don't give it much time because this directly impacts the
890 // responsiveness seen by the user.
891 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
892 msg.obj = prev;
893 prev.pauseTime = SystemClock.uptimeMillis();
894 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
895 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
896 return true;
897 }
898
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700899 } else {
900 // This activity failed to schedule the
901 // pause, so just treat it as being paused now.
902 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700903 if (!resuming) {
904 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
905 }
906 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700907 }
908 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700909
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700910 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700911 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800912 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700913
Craig Mautnerd2328952013-03-05 12:46:26 -0800914 final ActivityRecord r = isInStackLocked(token);
915 if (r != null) {
916 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
917 if (mPausingActivity == r) {
918 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
919 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700920 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800921 } else {
922 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
923 r.userId, System.identityHashCode(r), r.shortComponentName,
924 mPausingActivity != null
925 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700926 }
927 }
928 }
929
Craig Mautnera0026042014-04-23 11:45:37 -0700930 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
931 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700932 if (r.state != ActivityState.STOPPING) {
933 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
934 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
935 return;
936 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700937 if (persistentState != null) {
938 r.persistentState = persistentState;
939 mService.notifyTaskPersisterLocked(r.task, false);
940 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700941 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700942 if (icicle != null) {
943 // If icicle is null, this is happening due to a timeout, so we
944 // haven't really saved the state.
945 r.icicle = icicle;
946 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800947 r.launchCount = 0;
Craig Mautnera0026042014-04-23 11:45:37 -0700948 r.updateThumbnail(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700949 }
950 if (!r.stopped) {
951 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
952 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
953 r.stopped = true;
954 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -0700955 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
956 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700957 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700958 if (r.finishing) {
959 r.clearOptionsLocked();
960 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700961 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700962 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700963 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700964 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700965 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800966 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700967 }
968 }
969 }
970
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700971 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800972 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700973 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800974
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800975 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700976 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800977 if (prev.finishing) {
978 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700979 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800980 } else if (prev.app != null) {
981 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
982 if (prev.waitingVisible) {
983 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700984 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800985 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
986 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800987 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800988 if (prev.configDestroy) {
989 // The previous is being paused because the configuration
990 // is changing, which means it is actually stopping...
991 // To juggle the fact that we are also starting a new
992 // instance right now, we need to first completely stop
993 // the current instance before starting the new one.
994 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700995 destroyActivityLocked(prev, true, "pause-config");
Jose Lima4b6c6692014-08-12 17:41:12 -0700996 } else if (!hasVisibleBehindActivity()) {
997 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -0700998 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700999 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001000 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1001 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001002 // If we already have a few activities waiting to stop,
1003 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001004 // them out. Or if r is the last of activity of the last task the stack
1005 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001006 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001007 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001008 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001009 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001010 }
1011 }
1012 } else {
1013 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
1014 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001015 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001016 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001017 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001018
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001019 if (resumeNext) {
1020 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1021 if (!mService.isSleepingOrShuttingDown()) {
1022 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1023 } else {
1024 mStackSupervisor.checkReadyForSleepLocked();
1025 ActivityRecord top = topStack.topRunningActivityLocked(null);
1026 if (top == null || (prev != null && top != prev)) {
1027 // If there are no more activities available to run,
1028 // do resume anyway to start something. Also if the top
1029 // activity on the stack is not the just paused activity,
1030 // we need to go ahead and resume it to ensure we complete
1031 // an in-flight app switch.
1032 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1033 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001034 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001035 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001036
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001037 if (prev != null) {
1038 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001039
Craig Mautner525f3d92013-05-07 14:01:50 -07001040 if (prev.app != null && prev.cpuTimeAtResume > 0
1041 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001042 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1043 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001044 if (diff > 0) {
1045 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1046 synchronized (bsi) {
1047 BatteryStatsImpl.Uid.Proc ps =
1048 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001049 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001050 if (ps != null) {
1051 ps.addForegroundTimeLocked(diff);
1052 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001053 }
1054 }
1055 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001056 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001057 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001058 }
1059
1060 /**
1061 * Once we know that we have asked an application to put an activity in
1062 * the resumed state (either by launching it or explicitly telling it),
1063 * this function updates the rest of our state to match that fact.
1064 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001065 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001066 next.idle = false;
1067 next.results = null;
1068 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001069
1070 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1071 ProcessRecord app = next.task.mActivities.get(0).app;
1072 if (app != null && app != mService.mHomeProcess) {
1073 mService.mHomeProcess = app;
1074 }
1075 }
1076
Craig Mautner07566322013-09-26 16:42:55 -07001077 if (next.nowVisible) {
1078 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001079 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001080 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001081
1082 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001083 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001084
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001085 mStackSupervisor.reportResumedActivityLocked(next);
1086
1087 next.resumeKeyDispatchingLocked();
1088 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001089
1090 // Mark the point when the activity is resuming
1091 // TODO: To be more accurate, the mark should be before the onCreate,
1092 // not after the onResume. But for subsequent starts, onResume is fine.
1093 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001094 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001095 } else {
1096 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1097 }
Winson Chung376543b2014-05-21 17:43:28 -07001098
1099 // If we are resuming the activity that we had last screenshotted, then we know it will be
1100 // updated, so invalidate the last screenshot to ensure we take a fresh one when requested
1101 if (next == mLastScreenshotActivity) {
1102 invalidateLastScreenshot();
1103 }
George Mount6ba042b2014-07-28 11:12:28 -07001104 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001105
1106 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1107 // When resuming an activity, require it to call requestVisibleBehind() again.
1108 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1109 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001110 }
1111
Craig Mautnere3a00d72014-04-16 08:31:19 -07001112 private void setVisibile(ActivityRecord r, boolean visible) {
1113 r.visible = visible;
1114 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001115 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001116 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001117 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001118 container.setVisible(visible);
1119 }
1120 }
1121
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001122 // Find the first visible activity above the passed activity and if it is translucent return it
1123 // otherwise return null;
1124 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1125 TaskRecord task = r.task;
1126 if (task == null) {
1127 return null;
1128 }
1129
1130 ActivityStack stack = task.stack;
1131 if (stack == null) {
1132 return null;
1133 }
1134
1135 int stackNdx = mStacks.indexOf(stack);
1136
1137 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1138 int taskNdx = tasks.indexOf(task);
1139
1140 ArrayList<ActivityRecord> activities = task.mActivities;
1141 int activityNdx = activities.indexOf(r) + 1;
1142
1143 final int numStacks = mStacks.size();
1144 while (stackNdx < numStacks) {
1145 tasks = mStacks.get(stackNdx).mTaskHistory;
1146 final int numTasks = tasks.size();
1147 while (taskNdx < numTasks) {
1148 activities = tasks.get(taskNdx).mActivities;
1149 final int numActivities = activities.size();
1150 while (activityNdx < numActivities) {
1151 final ActivityRecord activity = activities.get(activityNdx);
1152 if (!activity.finishing) {
1153 return activity.fullscreen ? null : activity;
1154 }
1155 ++activityNdx;
1156 }
1157 activityNdx = 0;
1158 ++taskNdx;
1159 }
1160 taskNdx = 0;
1161 ++stackNdx;
1162 }
1163
1164 return null;
1165 }
1166
Jose Lima7ba71252014-04-30 12:59:27 -07001167 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1168 // If so, this stack is hidden, otherwise it is visible.
1169 private boolean isStackVisible() {
1170 if (!isAttached()) {
1171 return false;
1172 }
1173
1174 if (mStackSupervisor.isFrontStack(this)) {
1175 return true;
1176 }
1177
Jose Lima729cb232014-05-05 15:59:40 -07001178 /**
1179 * Start at the task above this one and go up, looking for a visible
1180 * fullscreen activity, or a translucent activity that requested the
1181 * wallpaper to be shown behind it.
1182 */
Jose Lima7ba71252014-04-30 12:59:27 -07001183 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
1184 final ArrayList<TaskRecord> tasks = mStacks.get(i).getAllTasks();
1185 for (int taskNdx = 0; taskNdx < tasks.size(); taskNdx++) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001186 final TaskRecord task = tasks.get(taskNdx);
1187 final ArrayList<ActivityRecord> activities = task.mActivities;
Jose Lima7ba71252014-04-30 12:59:27 -07001188 for (int activityNdx = 0; activityNdx < activities.size(); activityNdx++) {
1189 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001190
1191 // Conditions for an activity to obscure the stack we're
1192 // examining:
1193 // 1. Not Finishing AND Visible AND:
1194 // 2. Either:
1195 // - Full Screen Activity OR
1196 // - On top of Home and our stack is NOT home
1197 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001198 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001199 return false;
1200 }
1201 }
1202 }
1203 }
1204
1205 return true;
1206 }
1207
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001208 /**
1209 * Make sure that all activities that need to be visible (that is, they
1210 * currently can be seen by the user) actually are.
1211 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001212 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1213 ActivityRecord top = topRunningActivityLocked(null);
1214 if (top == null) {
1215 return;
1216 }
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001217 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001218 TAG, "ensureActivitiesVisible behind " + top
1219 + " configChanges=0x" + Integer.toHexString(configChanges));
1220
Craig Mautner5eda9b32013-07-02 11:58:16 -07001221 if (mTranslucentActivityWaiting != top) {
1222 mUndrawnActivitiesBelowTopTranslucent.clear();
1223 if (mTranslucentActivityWaiting != null) {
1224 // Call the callback with a timeout indication.
1225 notifyActivityDrawnLocked(null);
1226 mTranslucentActivityWaiting = null;
1227 }
1228 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1229 }
1230
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001231 // If the top activity is not fullscreen, then we need to
1232 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001233 boolean aboveTop = true;
Jose Lima7ba71252014-04-30 12:59:27 -07001234 boolean behindFullscreen = !isStackVisible();
1235
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001236 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001237 final TaskRecord task = mTaskHistory.get(taskNdx);
1238 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001239 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1240 final ActivityRecord r = activities.get(activityNdx);
1241 if (r.finishing) {
1242 continue;
1243 }
1244 if (aboveTop && r != top) {
1245 continue;
1246 }
1247 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001248 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1249 // but must be drawn initially for the animation as though they were visible.
1250 if (!behindFullscreen || r.mLaunchTaskBehind) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001251 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001252 TAG, "Make visible? " + r + " finishing=" + r.finishing
1253 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001254
Craig Mautnerd44711d2013-02-23 11:24:36 -08001255 // First: if this is not the current activity being started, make
1256 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001257 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001258 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001259 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001260
1261 if (r.app == null || r.app.thread == null) {
Craig Mautnerbb742462014-07-07 15:28:55 -07001262 // This activity needs to be visible, but isn't even
1263 // running... get it started, but don't resume it
1264 // at this point.
1265 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
1266 if (r != starting) {
1267 r.startFreezingScreenLocked(r.app, configChanges);
1268 }
1269 if (!r.visible || r.mLaunchTaskBehind) {
1270 if (DEBUG_VISBILITY) Slog.v(
1271 TAG, "Starting and making visible: " + r);
1272 setVisibile(r, true);
1273 }
1274 if (r != starting) {
1275 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001276 }
1277
1278 } else if (r.visible) {
1279 // If this activity is already visible, then there is nothing
1280 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001281 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001282 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001283 try {
George Mount6ba042b2014-07-28 11:12:28 -07001284 if (r.returningOptions != null) {
1285 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1286 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001287 }
1288 } catch(RemoteException e) {
1289 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001290 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001291 // This activity is not currently visible, but is running.
1292 // Tell it to become visible.
1293 r.visible = true;
1294 if (r.state != ActivityState.RESUMED && r != starting) {
1295 // If this activity is paused, tell it
1296 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001297 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001298 TAG, "Making visible and scheduling visibility: " + r);
1299 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001300 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001301 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001302 mUndrawnActivitiesBelowTopTranslucent.add(r);
1303 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001304 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001305 r.sleeping = false;
1306 r.app.pendingUiClean = true;
1307 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1308 r.stopFreezingScreenLocked(false);
1309 } catch (Exception e) {
1310 // Just skip on any failure; we'll make it
1311 // visible when it next restarts.
1312 Slog.w(TAG, "Exception thrown making visibile: "
1313 + r.intent.getComponent(), e);
1314 }
1315 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001316 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001317
Craig Mautnerd44711d2013-02-23 11:24:36 -08001318 // Aggregate current change flags.
1319 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001320
Craig Mautnerd44711d2013-02-23 11:24:36 -08001321 if (r.fullscreen) {
1322 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001323 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1324 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001325 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001326 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001327 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001328 }
1329 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001330 if (DEBUG_VISBILITY) Slog.v(
1331 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1332 + " state=" + r.state
1333 + " behindFullscreen=" + behindFullscreen);
1334 // Now for any activities that aren't visible to the user, make
1335 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001336 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001337 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001338 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001339 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001340 switch (r.state) {
1341 case STOPPING:
1342 case STOPPED:
1343 if (r.app != null && r.app.thread != null) {
1344 if (DEBUG_VISBILITY) Slog.v(
1345 TAG, "Scheduling invisibility: " + r);
1346 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1347 }
1348 break;
1349
1350 case INITIALIZING:
1351 case RESUMED:
1352 case PAUSING:
1353 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001354 // This case created for transitioning activities from
1355 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001356 if (getVisibleBehindActivity() == r) {
1357 releaseBackgroundResources();
Craig Mautneree2e45a2014-06-27 12:10:03 -07001358 } else {
1359 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1360 mStackSupervisor.mStoppingActivities.add(r);
1361 }
1362 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001363 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001364 break;
1365
1366 default:
1367 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001368 }
1369 } catch (Exception e) {
1370 // Just skip on any failure; we'll make it
1371 // visible when it next restarts.
1372 Slog.w(TAG, "Exception thrown making hidden: "
1373 + r.intent.getComponent(), e);
1374 }
1375 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001376 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001377 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001378 }
1379 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001380 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001381
1382 if (mTranslucentActivityWaiting != null &&
1383 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1384 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1385 notifyActivityDrawnLocked(null);
1386 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001387 }
Craig Mautner58547802013-03-05 08:23:53 -08001388
George Mount6ba042b2014-07-28 11:12:28 -07001389 void convertToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001390 mTranslucentActivityWaiting = r;
1391 mUndrawnActivitiesBelowTopTranslucent.clear();
1392 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1393 }
1394
1395 /**
1396 * Called as activities below the top translucent activity are redrawn. When the last one is
1397 * redrawn notify the top activity by calling
1398 * {@link Activity#onTranslucentConversionComplete}.
1399 *
1400 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1401 * occurred and the activity will be notified immediately.
1402 */
1403 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001404 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001405 if ((r == null)
1406 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1407 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1408 // The last undrawn activity below the top has just been drawn. If there is an
1409 // opaque activity at the top, notify it that it can become translucent safely now.
1410 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1411 mTranslucentActivityWaiting = null;
1412 mUndrawnActivitiesBelowTopTranslucent.clear();
1413 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1414
Craig Mautner71dd1b62014-02-18 15:48:52 -08001415 if (waitingActivity != null) {
1416 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1417 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1418 try {
1419 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1420 waitingActivity.appToken, r != null);
1421 } catch (RemoteException e) {
1422 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001423 }
1424 }
1425 }
1426 }
1427
Craig Mautnera61bc652013-10-28 15:43:18 -07001428 /** If any activities below the top running one are in the INITIALIZING state and they have a
1429 * starting window displayed then remove that starting window. It is possible that the activity
1430 * in this state will never resumed in which case that starting window will be orphaned. */
1431 void cancelInitializingActivities() {
1432 final ActivityRecord topActivity = topRunningActivityLocked(null);
1433 boolean aboveTop = true;
1434 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1435 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1436 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1437 final ActivityRecord r = activities.get(activityNdx);
1438 if (aboveTop) {
1439 if (r == topActivity) {
1440 aboveTop = false;
1441 }
1442 continue;
1443 }
1444
1445 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1446 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1447 r.mStartingWindowShown = false;
1448 mWindowManager.removeAppStartingWindow(r.appToken);
1449 }
1450 }
1451 }
1452 }
1453
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001454 /**
1455 * Ensure that the top activity in the stack is resumed.
1456 *
1457 * @param prev The previously resumed activity, for when in the process
1458 * of pausing; can be null to call from elsewhere.
1459 *
1460 * @return Returns true if something is being resumed, or false if
1461 * nothing happened.
1462 */
1463 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001464 return resumeTopActivityLocked(prev, null);
1465 }
1466
1467 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001468 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001469 // Don't even start recursing.
1470 return false;
1471 }
1472
1473 boolean result = false;
1474 try {
1475 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001476 mStackSupervisor.inResumeTopActivity = true;
1477 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1478 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
1479 mService.comeOutOfSleepIfNeededLocked();
1480 }
Craig Mautner544efa72014-09-04 16:41:20 -07001481 result = resumeTopActivityInnerLocked(prev, options);
1482 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001483 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001484 }
1485 return result;
1486 }
1487
1488 final boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001489 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1490
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001491 if (!mService.mBooting && !mService.mBooted) {
1492 // Not ready yet!
1493 return false;
1494 }
1495
Craig Mautnerdf88d732014-01-27 09:21:32 -08001496 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001497 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001498 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001499 // Do not resume this stack if its parent is not resumed.
1500 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1501 return false;
1502 }
1503
Craig Mautnera61bc652013-10-28 15:43:18 -07001504 cancelInitializingActivities();
1505
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001506 // Find the first activity that is not finishing.
1507 ActivityRecord next = topRunningActivityLocked(null);
1508
1509 // Remember how we'll process this pause/resume situation, and ensure
1510 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001511 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1512 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001513
Craig Mautner84984fa2014-06-19 11:19:20 -07001514 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001515 if (next == null) {
1516 // There are no more activities! Let's just start up the
1517 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001518 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001519 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001520 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001521 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001522 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1523 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1524 return isOnHomeDisplay() &&
1525 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001526 }
1527
1528 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001529
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001530 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001531 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1532 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001533 // Make sure we have executed any pending transitions, since there
1534 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001535 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001536 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001537 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001538 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001539 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Jorim Jaggi4c499be2014-08-27 15:31:31 +02001540
1541 // Make sure to notify Keyguard as well if it is waiting for an activity to be drawn.
1542 mStackSupervisor.notifyActivityDrawnForKeyguard();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001543 return false;
1544 }
1545
Craig Mautner525f3d92013-05-07 14:01:50 -07001546 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001547 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001548 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001549 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001550 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001551 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001552 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001553 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001554 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001555 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001556 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001557 } else {
Craig Mautnere0a38842013-12-16 16:14:02 -08001558 if (DEBUG_STATES && isOnHomeDisplay()) Slog.d(TAG,
1559 "resumeTopActivityLocked: Launching home next");
1560 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001561 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1562 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1563 return isOnHomeDisplay() &&
1564 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001565 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001566 }
1567
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001568 // If we are sleeping, and there is no resumed activity, and the top
1569 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001570 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001571 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001572 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001573 // Make sure we have executed any pending transitions, since there
1574 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001575 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001576 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001577 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001578 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001579 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001580 return false;
1581 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001582
1583 // Make sure that the user who owns this activity is started. If not,
1584 // we will just leave it as is because someone should be bringing
1585 // another user's activities to the top of the stack.
1586 if (mService.mStartedUsers.get(next.userId) == null) {
1587 Slog.w(TAG, "Skipping resume of top activity " + next
1588 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001589 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001590 return false;
1591 }
1592
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001593 // The activity may be waiting for stop, but that is no longer
1594 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001595 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001596 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001597 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001598 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001599
1600 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1601
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001602 // If we are currently pausing an activity, then don't do anything
1603 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001604 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001605 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1606 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001607 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001608 return false;
1609 }
1610
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001611 // Okay we are now going to start a switch, to 'next'. We may first
1612 // have to pause the current activity, but this is an important point
1613 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001614 // XXX "App Redirected" dialog is getting too many false positives
1615 // at this point, so turn off for now.
1616 if (false) {
1617 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1618 long now = SystemClock.uptimeMillis();
1619 final boolean inTime = mLastStartedActivity.startTime != 0
1620 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1621 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1622 final int nextUid = next.info.applicationInfo.uid;
1623 if (inTime && lastUid != nextUid
1624 && lastUid != next.launchedFromUid
1625 && mService.checkPermission(
1626 android.Manifest.permission.STOP_APP_SWITCHES,
1627 -1, next.launchedFromUid)
1628 != PackageManager.PERMISSION_GRANTED) {
1629 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1630 } else {
1631 next.startTime = now;
1632 mLastStartedActivity = next;
1633 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001634 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001635 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001636 mLastStartedActivity = next;
1637 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001638 }
Craig Mautner58547802013-03-05 08:23:53 -08001639
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001640 // We need to start pausing the current activity so the top one
1641 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001642 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1643 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001644 if (mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001645 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001646 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnereb957862013-04-24 15:34:32 -07001647 }
1648 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001649 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1650 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001651 // At this point we want to put the upcoming activity's process
1652 // at the top of the LRU list, since we know we will be needing it
1653 // very soon and it would be a waste to let it get killed if it
1654 // happens to be sitting towards the end.
1655 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001656 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001657 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001658 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001659 return true;
1660 }
1661
Christopher Tated3f175c2012-06-14 14:16:54 -07001662 // If the most recent activity was noHistory but was only stopped rather
1663 // than stopped+finished because the device went to sleep, we need to make
1664 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001665 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001666 !mLastNoHistoryActivity.finishing) {
1667 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1668 " on new resume");
1669 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1670 null, "no-history", false);
1671 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001672 }
1673
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001674 if (prev != null && prev != next) {
1675 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1676 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001677 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001678 if (DEBUG_SWITCH) Slog.v(
1679 TAG, "Resuming top, waiting visible to hide: " + prev);
1680 } else {
1681 // The next activity is already visible, so hide the previous
1682 // activity's windows right now so we can show the new one ASAP.
1683 // We only do this if the previous is finishing, which should mean
1684 // it is on top of the one being resumed so hiding it quickly
1685 // is good. Otherwise, we want to do the normal route of allowing
1686 // the resumed activity to be shown so we can decide if the
1687 // previous should actually be hidden depending on whether the
1688 // new one is found to be full-screen or not.
1689 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001690 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001691 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1692 + prev + ", waitingVisible="
1693 + (prev != null ? prev.waitingVisible : null)
1694 + ", nowVisible=" + next.nowVisible);
1695 } else {
1696 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1697 + prev + ", waitingVisible="
1698 + (prev != null ? prev.waitingVisible : null)
1699 + ", nowVisible=" + next.nowVisible);
1700 }
1701 }
1702 }
1703
Dianne Hackborne7f97212011-02-24 14:40:20 -08001704 // Launching this app's activity, make sure the app is no longer
1705 // considered stopped.
1706 try {
1707 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001708 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001709 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001710 } catch (IllegalArgumentException e) {
1711 Slog.w(TAG, "Failed trying to unstop package "
1712 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001713 }
1714
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001715 // We are starting up the next activity, so tell the window manager
1716 // that the previous one will be hidden soon. This way it can know
1717 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001718 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001719 if (prev != null) {
1720 if (prev.finishing) {
1721 if (DEBUG_TRANSITION) Slog.v(TAG,
1722 "Prepare close transition: prev=" + prev);
1723 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001724 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001725 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001726 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001727 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001728 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1729 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001730 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001731 mWindowManager.setAppWillBeHidden(prev.appToken);
1732 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001733 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001734 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001735 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001736 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001737 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001738 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001739 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001740 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001741 : next.mLaunchTaskBehind
1742 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1743 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001744 }
1745 }
1746 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001747 mWindowManager.setAppWillBeHidden(prev.appToken);
1748 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001749 }
Craig Mautner967212c2013-04-13 21:10:58 -07001750 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001751 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001752 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001753 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001754 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001755 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001756 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001757 }
1758 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001759
1760 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001761 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001762 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1763 if (opts != null) {
1764 resumeAnimOptions = opts.toBundle();
1765 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001766 next.applyOptionsLocked();
1767 } else {
1768 next.clearOptionsLocked();
1769 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001770
Craig Mautnercf910b02013-04-23 11:23:27 -07001771 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001772 if (next.app != null && next.app.thread != null) {
1773 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1774
1775 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001776 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001777
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001778 // schedule launch ticks to collect information about slow apps.
1779 next.startLaunchTickingLocked();
1780
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001781 ActivityRecord lastResumedActivity =
1782 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001783 ActivityState lastState = next.state;
1784
1785 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001786
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001787 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001788 next.state = ActivityState.RESUMED;
1789 mResumedActivity = next;
1790 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001791 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001792 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001793 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001794 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001795
1796 // Have the window manager re-evaluate the orientation of
1797 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001798 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001799 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001800 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001801 mService.mConfiguration,
1802 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1803 if (config != null) {
1804 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001805 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001806 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001807 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001808
Craig Mautner525f3d92013-05-07 14:01:50 -07001809 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001810 // The configuration update wasn't able to keep the existing
1811 // instance of the activity, and instead started a new one.
1812 // We should be all done, but let's just make sure our activity
1813 // is still at the top and schedule another run if something
1814 // weird happened.
1815 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001816 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001817 "Activity config changed during resume: " + next
1818 + ", new next: " + nextNext);
1819 if (nextNext != next) {
1820 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001821 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001822 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001823 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001824 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001825 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001826 return true;
1827 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001828 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001829 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001830 }
Craig Mautner58547802013-03-05 08:23:53 -08001831
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001832 try {
1833 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001834 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001835 if (a != null) {
1836 final int N = a.size();
1837 if (!next.finishing && N > 0) {
1838 if (DEBUG_RESULTS) Slog.v(
1839 TAG, "Delivering results to " + next
1840 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001841 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001842 }
1843 }
1844
1845 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001846 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001847 }
1848
1849 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001850 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001851 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001852
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001853 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001854 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001855 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001856 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001857 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001858 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001859 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001860
Craig Mautner0eea92c2013-05-16 13:35:39 -07001861 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001862
Craig Mautnerac6f8432013-07-17 13:24:59 -07001863 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001864 } catch (Exception e) {
1865 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001866 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1867 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001868 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001869 if (lastStack != null) {
1870 lastStack.mResumedActivity = lastResumedActivity;
1871 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001872 Slog.i(TAG, "Restarting because process died: " + next);
1873 if (!next.hasBeenLaunched) {
1874 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001875 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1876 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001877 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001878 next.appToken, next.packageName, next.theme,
1879 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001880 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1881 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001882 }
George Mount2c92c972014-03-20 09:38:23 -07001883 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001884 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001885 return true;
1886 }
1887
1888 // From this point on, if something goes wrong there is no way
1889 // to recover the activity.
1890 try {
1891 next.visible = true;
1892 completeResumeLocked(next);
1893 } catch (Exception e) {
1894 // If any exception gets thrown, toss away this
1895 // activity and try the next one.
1896 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001897 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001898 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001899 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001900 return true;
1901 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001902 next.stopped = false;
1903
1904 } else {
1905 // Whoops, need to restart this activity!
1906 if (!next.hasBeenLaunched) {
1907 next.hasBeenLaunched = true;
1908 } else {
1909 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001910 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001911 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001912 mService.compatibilityInfoForPackageLocked(
1913 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001914 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001915 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001916 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001917 }
1918 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1919 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001920 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001921 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001922 }
1923
Craig Mautnercf910b02013-04-23 11:23:27 -07001924 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001925 return true;
1926 }
1927
Craig Mautnerac6f8432013-07-17 13:24:59 -07001928 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001929 // If this is being moved to the top by another activity or being launched from the home
1930 // activity, set mOnTopOfHome accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001931 if (isOnHomeDisplay()) {
1932 ActivityStack lastStack = mStackSupervisor.getLastStack();
1933 final boolean fromHome = lastStack.isHomeStack();
1934 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07001935 task.setTaskToReturnTo(fromHome
1936 ? lastStack.topTask() == null
1937 ? HOME_ACTIVITY_TYPE
1938 : lastStack.topTask().taskType
1939 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08001940 }
1941 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07001942 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07001943 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001944
Craig Mautnerac6f8432013-07-17 13:24:59 -07001945 mTaskHistory.remove(task);
1946 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07001947 int taskNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01001948 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001949 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07001950 while (--taskNdx >= 0) {
1951 if (!isCurrentProfileLocked(mTaskHistory.get(taskNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001952 break;
1953 }
1954 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001955 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001956 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001957 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001958 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001959 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001960
Craig Mautner8849a5e2013-04-02 16:41:03 -07001961 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001962 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001963 TaskRecord rTask = r.task;
1964 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07001965 // mLaunchTaskBehind tasks get placed at the back of the task stack.
1966 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08001967 // Last activity in task had been removed or ActivityManagerService is reusing task.
1968 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001969 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001970 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001971 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001972 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001973 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001974 if (!newTask) {
1975 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001976 boolean startIt = true;
1977 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1978 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08001979 if (task.getTopActivity() == null) {
1980 // All activities in task are finishing.
1981 continue;
1982 }
Craig Mautner70a86932013-02-28 22:37:44 -08001983 if (task == r.task) {
1984 // Here it is! Now, if this is not yet visible to the
1985 // user, then just add it without starting; it will
1986 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001987 if (!startIt) {
1988 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1989 + task, new RuntimeException("here").fillInStackTrace());
1990 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001991 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001992 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1993 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001994 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07001995 r.userId, r.info.configChanges, task.voiceSession != null,
1996 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001997 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001998 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001999 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002000 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002001 return;
2002 }
2003 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002004 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002005 startIt = false;
2006 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002007 }
2008 }
2009
2010 // Place a new activity at top of stack, so it is next to interact
2011 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002012
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002013 // If we are not placing the new activity frontmost, we do not want
2014 // to deliver the onUserLeaving callback to the actual frontmost
2015 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002016 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002017 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08002018 if (DEBUG_USER_LEAVING) Slog.v(TAG,
2019 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002020 }
Craig Mautner70a86932013-02-28 22:37:44 -08002021
2022 task = r.task;
2023
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002024 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002025 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002026 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002027 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002028 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002029
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002030 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002031 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002032 // We want to show the starting preview window if we are
2033 // switching to a new task, or the next activity's process is
2034 // not currently running.
2035 boolean showStartingIcon = newTask;
2036 ProcessRecord proc = r.app;
2037 if (proc == null) {
2038 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2039 }
2040 if (proc == null || proc.thread == null) {
2041 showStartingIcon = true;
2042 }
2043 if (DEBUG_TRANSITION) Slog.v(TAG,
2044 "Prepare open transition: starting " + r);
2045 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002046 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002047 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002048 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002049 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002050 ? r.mLaunchTaskBehind
2051 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2052 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002053 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002054 mNoAnimActivities.remove(r);
2055 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002056 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002057 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002058 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002059 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002060 boolean doShow = true;
2061 if (newTask) {
2062 // Even though this activity is starting fresh, we still need
2063 // to reset it to make sure we apply affinities to move any
2064 // existing activities from other tasks in to it.
2065 // If the caller has requested that the target task be
2066 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002067 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002068 resetTaskIfNeededLocked(r, r);
2069 doShow = topRunningNonDelayedActivityLocked(null) == r;
2070 }
George Mount70778d72014-07-01 16:33:45 -07002071 } else if (options != null && new ActivityOptions(options).getAnimationType()
2072 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2073 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002074 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002075 if (r.mLaunchTaskBehind) {
2076 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2077 // tell WindowManager that r is visible even though it is at the back of the stack.
2078 mWindowManager.setAppVisibility(r.appToken, true);
2079 ensureActivitiesVisibleLocked(null, 0);
2080 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002081 // Figure out if we are transitioning from another activity that is
2082 // "has the same starting icon" as the next one. This allows the
2083 // window manager to keep the previous window it had previously
2084 // created, if it still had one.
2085 ActivityRecord prev = mResumedActivity;
2086 if (prev != null) {
2087 // We don't want to reuse the previous starting preview if:
2088 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002089 if (prev.task != r.task) {
2090 prev = null;
2091 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002092 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002093 else if (prev.nowVisible) {
2094 prev = null;
2095 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002096 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002097 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002098 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002099 mService.compatibilityInfoForPackageLocked(
2100 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002101 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002102 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002103 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002104 }
2105 } else {
2106 // If this is the first activity, don't do any fancy animations,
2107 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002108 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002109 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002110 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002111 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002112 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002113 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002114 }
2115 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002116 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002117 }
2118
2119 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002120 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002121 }
2122 }
2123
Dianne Hackbornbe707852011-11-11 14:32:10 -08002124 final void validateAppTokensLocked() {
2125 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002126 mValidateAppTokens.ensureCapacity(numActivities());
2127 final int numTasks = mTaskHistory.size();
2128 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2129 TaskRecord task = mTaskHistory.get(taskNdx);
2130 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002131 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002132 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002133 }
Craig Mautner000f0022013-02-26 15:04:29 -08002134 TaskGroup group = new TaskGroup();
2135 group.taskId = task.taskId;
2136 mValidateAppTokens.add(group);
2137 final int numActivities = activities.size();
2138 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2139 final ActivityRecord r = activities.get(activityNdx);
2140 group.tokens.add(r.appToken);
2141 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002142 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002143 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002144 }
2145
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002146 /**
2147 * Perform a reset of the given task, if needed as part of launching it.
2148 * Returns the new HistoryRecord at the top of the task.
2149 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002150 /**
2151 * Helper method for #resetTaskIfNeededLocked.
2152 * We are inside of the task being reset... we'll either finish this activity, push it out
2153 * for another task, or leave it as-is.
2154 * @param task The task containing the Activity (taskTop) that might be reset.
2155 * @param forceReset
2156 * @return An ActivityOptions that needs to be processed.
2157 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002158 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002159 ActivityOptions topOptions = null;
2160
2161 int replyChainEnd = -1;
2162 boolean canMoveOptions = true;
2163
2164 // We only do this for activities that are not the root of the task (since if we finish
2165 // the root, we may no longer have the task!).
2166 final ArrayList<ActivityRecord> activities = task.mActivities;
2167 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002168 final int rootActivityNdx = task.findEffectiveRootIndex();
2169 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002170 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002171 if (target.frontOfTask)
2172 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002173
2174 final int flags = target.info.flags;
2175 final boolean finishOnTaskLaunch =
2176 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2177 final boolean allowTaskReparenting =
2178 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2179 final boolean clearWhenTaskReset =
2180 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2181
2182 if (!finishOnTaskLaunch
2183 && !clearWhenTaskReset
2184 && target.resultTo != null) {
2185 // If this activity is sending a reply to a previous
2186 // activity, we can't do anything with it now until
2187 // we reach the start of the reply chain.
2188 // XXX note that we are assuming the result is always
2189 // to the previous activity, which is almost always
2190 // the case but we really shouldn't count on.
2191 if (replyChainEnd < 0) {
2192 replyChainEnd = i;
2193 }
2194 } else if (!finishOnTaskLaunch
2195 && !clearWhenTaskReset
2196 && allowTaskReparenting
2197 && target.taskAffinity != null
2198 && !target.taskAffinity.equals(task.affinity)) {
2199 // If this activity has an affinity for another
2200 // task, then we need to move it out of here. We will
2201 // move it as far out of the way as possible, to the
2202 // bottom of the activity stack. This also keeps it
2203 // correctly ordered with any activities we previously
2204 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002205 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002206 final ActivityRecord bottom =
2207 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002208 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002209 if (bottom != null && target.taskAffinity != null
2210 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002211 // If the activity currently at the bottom has the
2212 // same task affinity as the one we are moving,
2213 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002214 targetTask = bottom.task;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002215 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002216 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002217 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002218 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002219 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002220 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002221 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2222 + " out to new task " + target.task);
2223 }
2224
Craig Mautnere3a74d52013-02-22 14:14:58 -08002225 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002226 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002227
Craig Mautner525f3d92013-05-07 14:01:50 -07002228 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002229 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2230 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002231 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002232 if (p.finishing) {
2233 continue;
2234 }
2235
Craig Mautnere3a74d52013-02-22 14:14:58 -08002236 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002237 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002238 topOptions = p.takeOptionsLocked();
2239 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002240 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002241 }
2242 }
2243 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002244 + task + " adding to task=" + targetTask
2245 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002246 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2247 + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002248 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002249 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002250
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002251 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002252 }
2253
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002254 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002255 if (VALIDATE_TOKENS) {
2256 validateAppTokensLocked();
2257 }
2258
2259 replyChainEnd = -1;
2260 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2261 // If the activity should just be removed -- either
2262 // because it asks for it, or the task should be
2263 // cleared -- then finish it and anything that is
2264 // part of its reply chain.
2265 int end;
2266 if (clearWhenTaskReset) {
2267 // In this case, we want to finish this activity
2268 // and everything above it, so be sneaky and pretend
2269 // like these are all in the reply chain.
2270 end = numActivities - 1;
2271 } else if (replyChainEnd < 0) {
2272 end = i;
2273 } else {
2274 end = replyChainEnd;
2275 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002276 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002277 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002278 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002279 if (p.finishing) {
2280 continue;
2281 }
2282 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002283 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002284 topOptions = p.takeOptionsLocked();
2285 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002286 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002287 }
2288 }
Craig Mautner58547802013-03-05 08:23:53 -08002289 if (DEBUG_TASKS) Slog.w(TAG,
2290 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002291 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002292 end--;
2293 srcPos--;
2294 }
2295 }
2296 replyChainEnd = -1;
2297 } else {
2298 // If we were in the middle of a chain, well the
2299 // activity that started it all doesn't want anything
2300 // special, so leave it all as-is.
2301 replyChainEnd = -1;
2302 }
2303 }
2304
2305 return topOptions;
2306 }
2307
2308 /**
2309 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2310 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2311 * @param affinityTask The task we are looking for an affinity to.
2312 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2313 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2314 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2315 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002316 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002317 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002318 int replyChainEnd = -1;
2319 final int taskId = task.taskId;
2320 final String taskAffinity = task.affinity;
2321
2322 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2323 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002324 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2325
2326 // Do not operate on or below the effective root Activity.
2327 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002328 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002329 if (target.frontOfTask)
2330 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002331
2332 final int flags = target.info.flags;
2333 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2334 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2335
2336 if (target.resultTo != null) {
2337 // If this activity is sending a reply to a previous
2338 // activity, we can't do anything with it now until
2339 // we reach the start of the reply chain.
2340 // XXX note that we are assuming the result is always
2341 // to the previous activity, which is almost always
2342 // the case but we really shouldn't count on.
2343 if (replyChainEnd < 0) {
2344 replyChainEnd = i;
2345 }
2346 } else if (topTaskIsHigher
2347 && allowTaskReparenting
2348 && taskAffinity != null
2349 && taskAffinity.equals(target.taskAffinity)) {
2350 // This activity has an affinity for our task. Either remove it if we are
2351 // clearing or move it over to our task. Note that
2352 // we currently punt on the case where we are resetting a
2353 // task that is not at the top but who has activities above
2354 // with an affinity to it... this is really not a normal
2355 // case, and we will need to later pull that task to the front
2356 // and usually at that point we will do the reset and pick
2357 // up those remaining activities. (This only happens if
2358 // someone starts an activity in a new task from an activity
2359 // in a task that is not currently on top.)
2360 if (forceReset || finishOnTaskLaunch) {
2361 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2362 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2363 for (int srcPos = start; srcPos >= i; --srcPos) {
2364 final ActivityRecord p = activities.get(srcPos);
2365 if (p.finishing) {
2366 continue;
2367 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002368 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002369 }
2370 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002371 if (taskInsertionPoint < 0) {
2372 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002373
Craig Mautner77878772013-03-04 19:46:24 -08002374 }
Craig Mautner77878772013-03-04 19:46:24 -08002375
2376 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2377 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2378 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2379 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002380 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002381 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002382 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002383
Craig Mautnere3a74d52013-02-22 14:14:58 -08002384 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2385 + " to stack at " + task,
2386 new RuntimeException("here").fillInStackTrace());
2387 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2388 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002389 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002390 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002391 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002392 if (VALIDATE_TOKENS) {
2393 validateAppTokensLocked();
2394 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002395
2396 // Now we've moved it in to place... but what if this is
2397 // a singleTop activity and we have put it on top of another
2398 // instance of the same activity? Then we drop the instance
2399 // below so it remains singleTop.
2400 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2401 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002402 int targetNdx = taskActivities.indexOf(target);
2403 if (targetNdx > 0) {
2404 ActivityRecord p = taskActivities.get(targetNdx - 1);
2405 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002406 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2407 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002408 }
2409 }
2410 }
2411 }
2412
2413 replyChainEnd = -1;
2414 }
2415 }
Craig Mautner77878772013-03-04 19:46:24 -08002416 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002417 }
2418
Craig Mautner8849a5e2013-04-02 16:41:03 -07002419 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002420 ActivityRecord newActivity) {
2421 boolean forceReset =
2422 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2423 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2424 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2425 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2426 forceReset = true;
2427 }
2428 }
2429
2430 final TaskRecord task = taskTop.task;
2431
2432 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2433 * for remaining tasks. Used for later tasks to reparent to task. */
2434 boolean taskFound = false;
2435
2436 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2437 ActivityOptions topOptions = null;
2438
Craig Mautner77878772013-03-04 19:46:24 -08002439 // Preserve the location for reparenting in the new task.
2440 int reparentInsertionPoint = -1;
2441
Craig Mautnere3a74d52013-02-22 14:14:58 -08002442 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2443 final TaskRecord targetTask = mTaskHistory.get(i);
2444
2445 if (targetTask == task) {
2446 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2447 taskFound = true;
2448 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002449 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2450 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002451 }
2452 }
2453
Craig Mautner70a86932013-02-28 22:37:44 -08002454 int taskNdx = mTaskHistory.indexOf(task);
2455 do {
2456 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2457 } while (taskTop == null && taskNdx >= 0);
2458
Craig Mautnere3a74d52013-02-22 14:14:58 -08002459 if (topOptions != null) {
2460 // If we got some ActivityOptions from an activity on top that
2461 // was removed from the task, propagate them to the new real top.
2462 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002463 taskTop.updateOptionsLocked(topOptions);
2464 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002465 topOptions.abort();
2466 }
2467 }
2468
2469 return taskTop;
2470 }
2471
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002472 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2473 String resultWho, int requestCode, int resultCode, Intent data) {
2474
2475 if (callingUid > 0) {
2476 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002477 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002478 }
2479
2480 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2481 + " : who=" + resultWho + " req=" + requestCode
2482 + " res=" + resultCode + " data=" + data);
2483 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2484 try {
2485 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2486 list.add(new ResultInfo(resultWho, requestCode,
2487 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002488 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002489 return;
2490 } catch (Exception e) {
2491 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2492 }
2493 }
2494
2495 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2496 }
2497
Craig Mautner04f0b702013-10-22 12:31:01 -07002498 private void adjustFocusedActivityLocked(ActivityRecord r) {
2499 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2500 ActivityRecord next = topRunningActivityLocked(null);
2501 if (next != r) {
2502 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002503 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2504 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner04f0b702013-10-22 12:31:01 -07002505 }
2506 }
Winson Chung648c83b2014-04-28 15:11:56 -07002507 ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2508 if (top != null) {
2509 mService.setFocusedActivityLocked(top);
2510 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002511 }
2512 }
2513
Craig Mautnerf3333272013-04-22 10:55:53 -07002514 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002515 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2516 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2517 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2518 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002519 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002520 if (DEBUG_STATES) {
2521 Slog.d(TAG, "no-history finish of " + r);
2522 }
2523 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002524 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002525 } else {
2526 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2527 + " on stop because we're just sleeping");
2528 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002529 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002530 }
2531
2532 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002533 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002534 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002535 try {
2536 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002537 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2538 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002539 r.state = ActivityState.STOPPING;
2540 if (DEBUG_VISBILITY) Slog.v(
2541 TAG, "Stopping visible=" + r.visible + " for " + r);
2542 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002543 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002544 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002545 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002546 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002547 r.setSleeping(true);
2548 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002549 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002550 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002551 } catch (Exception e) {
2552 // Maybe just ignore exceptions here... if the process
2553 // has crashed, our death notification will clean things
2554 // up.
2555 Slog.w(TAG, "Exception thrown during pause", e);
2556 // Just in case, assume it to be stopped.
2557 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002558 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002559 r.state = ActivityState.STOPPED;
2560 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002561 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002562 }
2563 }
2564 }
2565 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002566
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002567 /**
2568 * @return Returns true if the activity is being finished, false if for
2569 * some reason it is being left as-is.
2570 */
2571 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002572 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002573 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002574 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002575 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002576 + ", result=" + resultCode + ", data=" + resultData
2577 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002578 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002579 return false;
2580 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002581
Craig Mautnerd44711d2013-02-23 11:24:36 -08002582 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002583 return true;
2584 }
2585
Craig Mautnerd2328952013-03-05 12:46:26 -08002586 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002587 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2588 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2589 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2590 ActivityRecord r = activities.get(activityNdx);
2591 if (r.resultTo == self && r.requestCode == requestCode) {
2592 if ((r.resultWho == null && resultWho == null) ||
2593 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2594 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2595 false);
2596 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002597 }
2598 }
2599 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002600 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002601 }
2602
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002603 final void finishTopRunningActivityLocked(ProcessRecord app) {
2604 ActivityRecord r = topRunningActivityLocked(null);
2605 if (r != null && r.app == app) {
2606 // If the top running activity is from this crashing
2607 // process, then terminate it to avoid getting in a loop.
2608 Slog.w(TAG, " Force finishing activity "
2609 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002610 int taskNdx = mTaskHistory.indexOf(r.task);
2611 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002612 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002613 // Also terminate any activities below it that aren't yet
2614 // stopped, to avoid a situation where one will get
2615 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002616 --activityNdx;
2617 if (activityNdx < 0) {
2618 do {
2619 --taskNdx;
2620 if (taskNdx < 0) {
2621 break;
2622 }
2623 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2624 } while (activityNdx < 0);
2625 }
2626 if (activityNdx >= 0) {
2627 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002628 if (r.state == ActivityState.RESUMED
2629 || r.state == ActivityState.PAUSING
2630 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002631 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002632 Slog.w(TAG, " Force finishing activity "
2633 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002634 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002635 }
2636 }
2637 }
2638 }
2639 }
2640
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002641 final void finishVoiceTask(IVoiceInteractionSession session) {
2642 IBinder sessionBinder = session.asBinder();
2643 boolean didOne = false;
2644 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2645 TaskRecord tr = mTaskHistory.get(taskNdx);
2646 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2647 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2648 ActivityRecord r = tr.mActivities.get(activityNdx);
2649 if (!r.finishing) {
2650 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2651 false);
2652 didOne = true;
2653 }
2654 }
2655 }
2656 }
2657 if (didOne) {
2658 mService.updateOomAdjLocked();
2659 }
2660 }
2661
Craig Mautnerd2328952013-03-05 12:46:26 -08002662 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002663 ArrayList<ActivityRecord> activities = r.task.mActivities;
2664 for (int index = activities.indexOf(r); index >= 0; --index) {
2665 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002666 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002667 break;
2668 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002669 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002670 }
2671 return true;
2672 }
2673
Dianne Hackborn5c607432012-02-28 14:44:19 -08002674 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2675 // send the result
2676 ActivityRecord resultTo = r.resultTo;
2677 if (resultTo != null) {
2678 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2679 + " who=" + r.resultWho + " req=" + r.requestCode
2680 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002681 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002682 if (resultData != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002683 resultData.setContentUserHint(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002684 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002685 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002686 if (r.info.applicationInfo.uid > 0) {
2687 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2688 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002689 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002690 }
2691 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2692 resultData);
2693 r.resultTo = null;
2694 }
2695 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2696
2697 // Make sure this HistoryRecord is not holding on to other resources,
2698 // because clients have remote IPC references to this object so we
2699 // can't assume that will go away and want to avoid circular IPC refs.
2700 r.results = null;
2701 r.pendingResults = null;
2702 r.newIntents = null;
2703 r.icicle = null;
2704 }
2705
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002706 /**
2707 * @return Returns true if this activity has been removed from the history
2708 * list, or false if it is still in the list and will be removed later.
2709 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002710 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2711 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002712 if (r.finishing) {
2713 Slog.w(TAG, "Duplicate finish request for " + r);
2714 return false;
2715 }
2716
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002717 r.makeFinishing();
Craig Mautneraea74a52014-03-08 14:23:10 -08002718 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002719 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002720 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002721 task.taskId, r.shortComponentName, reason);
2722 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002723 final int index = activities.indexOf(r);
2724 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002725 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002726 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002727 // If the caller asked that this activity (and all above it)
2728 // be cleared when the task is reset, don't lose that information,
2729 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002730 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002731 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002732 }
2733 }
2734
2735 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002736
2737 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002738
Dianne Hackborn5c607432012-02-28 14:44:19 -08002739 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002740
Craig Mautnerde4ef022013-04-07 19:01:33 -07002741 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002742 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002743 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002744 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002745 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002746 ? AppTransition.TRANSIT_TASK_CLOSE
2747 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002748
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002749 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002750 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002751
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002752 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002753 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2754 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002755 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002756 }
2757
Craig Mautneraea74a52014-03-08 14:23:10 -08002758 if (endTask) {
2759 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2760 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002761 } else if (r.state != ActivityState.PAUSING) {
2762 // If the activity is PAUSING, we will complete the finish once
2763 // it is done pausing; else we can just directly finish it here.
2764 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002765 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002766 } else {
2767 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2768 }
2769
2770 return false;
2771 }
2772
Craig Mautnerf3333272013-04-22 10:55:53 -07002773 static final int FINISH_IMMEDIATELY = 0;
2774 static final int FINISH_AFTER_PAUSE = 1;
2775 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002776
Craig Mautnerf3333272013-04-22 10:55:53 -07002777 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002778 // First things first: if this activity is currently visible,
2779 // and the resumed activity is not yet visible, then hold off on
2780 // finishing until the resumed one becomes visible.
2781 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002782 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2783 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002784 if (mStackSupervisor.mStoppingActivities.size() > 3
2785 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002786 // If we already have a few activities waiting to stop,
2787 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002788 // them out. Or if r is the last of activity of the last task the stack
2789 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002790 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002791 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002792 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002793 }
2794 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002795 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2796 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002797 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002798 if (oomAdj) {
2799 mService.updateOomAdjLocked();
2800 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002801 return r;
2802 }
2803
2804 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002805 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002806 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002807 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002808 if (mResumedActivity == r) {
2809 mResumedActivity = null;
2810 }
2811 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002812 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002813 r.state = ActivityState.FINISHING;
2814
2815 if (mode == FINISH_IMMEDIATELY
2816 || prevState == ActivityState.STOPPED
2817 || prevState == ActivityState.INITIALIZING) {
2818 // If this activity is already stopped, we can just finish
2819 // it right now.
Craig Mautnerd163e752014-06-13 17:18:47 -07002820 r.makeFinishing();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002821 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002822 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002823 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002824 }
Craig Mautnerd163e752014-06-13 17:18:47 -07002825 if (DEBUG_CONTAINERS) Slog.d(TAG,
2826 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2827 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002828 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002829 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002830
2831 // Need to go through the full pause cycle to get this
2832 // activity into the stopped state and then finish it.
2833 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002834 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002835 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002836 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002837 return r;
2838 }
2839
Craig Mautneree36c772014-07-16 14:56:05 -07002840 void finishAllActivitiesLocked(boolean immediately) {
2841 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002842 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2843 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2844 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2845 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07002846 noActivitiesInStack = false;
2847 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002848 continue;
2849 }
Craig Mautneree36c772014-07-16 14:56:05 -07002850 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002851 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2852 }
2853 }
Craig Mautneree36c772014-07-16 14:56:05 -07002854 if (noActivitiesInStack) {
2855 mActivityContainer.onTaskListEmptyLocked();
2856 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002857 }
2858
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002859 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
2860 // Basic case: for simple app-centric recents, we need to recreate
2861 // the task if the affinity has changed.
2862 if (srec == null || srec.task.affinity == null ||
2863 !srec.task.affinity.equals(destAffinity)) {
2864 return true;
2865 }
2866 // Document-centric case: an app may be split in to multiple documents;
2867 // they need to re-create their task if this current activity is the root
2868 // of a document, unless simply finishing it will return them to the the
2869 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07002870 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
2871 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002872 // Okay, this activity is at the root of its task. What to do, what to do...
2873 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
2874 // Finishing won't return to an application, so we need to recreate.
2875 return true;
2876 }
2877 // We now need to get the task below it to determine what to do.
2878 int taskIdx = mTaskHistory.indexOf(srec.task);
2879 if (taskIdx <= 0) {
2880 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
2881 return false;
2882 }
2883 if (taskIdx == 0) {
2884 // At the bottom of the stack, nothing to go back to.
2885 return true;
2886 }
2887 TaskRecord prevTask = mTaskHistory.get(taskIdx);
2888 if (!srec.task.affinity.equals(prevTask.affinity)) {
2889 // These are different apps, so need to recreate.
2890 return true;
2891 }
2892 }
2893 return false;
2894 }
2895
Craig Mautnerd2328952013-03-05 12:46:26 -08002896 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002897 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002898 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002899 final TaskRecord task = srec.task;
2900 final ArrayList<ActivityRecord> activities = task.mActivities;
2901 final int start = activities.indexOf(srec);
2902 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002903 return false;
2904 }
2905 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002906 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002907 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002908 final ComponentName dest = destIntent.getComponent();
2909 if (start > 0 && dest != null) {
2910 for (int i = finishTo; i >= 0; i--) {
2911 ActivityRecord r = activities.get(i);
2912 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002913 r.info.name.equals(dest.getClassName())) {
2914 finishTo = i;
2915 parent = r;
2916 foundParentInTask = true;
2917 break;
2918 }
2919 }
2920 }
2921
2922 IActivityController controller = mService.mController;
2923 if (controller != null) {
2924 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2925 if (next != null) {
2926 // ask watcher if this is allowed
2927 boolean resumeOK = true;
2928 try {
2929 resumeOK = controller.activityResuming(next.packageName);
2930 } catch (RemoteException e) {
2931 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002932 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002933 }
2934
2935 if (!resumeOK) {
2936 return false;
2937 }
2938 }
2939 }
2940 final long origId = Binder.clearCallingIdentity();
2941 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002942 ActivityRecord r = activities.get(i);
2943 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002944 // Only return the supplied result for the first activity finished
2945 resultCode = Activity.RESULT_CANCELED;
2946 resultData = null;
2947 }
2948
2949 if (parent != null && foundParentInTask) {
2950 final int parentLaunchMode = parent.info.launchMode;
2951 final int destIntentFlags = destIntent.getFlags();
2952 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2953 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2954 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2955 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
2956 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
2957 } else {
2958 try {
2959 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2960 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002961 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002962 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002963 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborn95465202014-09-15 16:21:55 -07002964 -1, parent.launchedFromUid, 0, null, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002965 foundParentInTask = res == ActivityManager.START_SUCCESS;
2966 } catch (RemoteException e) {
2967 foundParentInTask = false;
2968 }
2969 requestFinishActivityLocked(parent.appToken, resultCode,
2970 resultData, "navigate-up", true);
2971 }
2972 }
2973 Binder.restoreCallingIdentity(origId);
2974 return foundParentInTask;
2975 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002976 /**
2977 * Perform the common clean-up of an activity record. This is called both
2978 * as part of destroyActivityLocked() (when destroying the client-side
2979 * representation) and cleaning things up as a result of its hosting
2980 * processing going away, in which case there is no remaining client-side
2981 * state to destroy so only the cleanup here is needed.
2982 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002983 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2984 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002985 if (mResumedActivity == r) {
2986 mResumedActivity = null;
2987 }
Craig Mautner1872ce32014-03-28 23:05:42 +00002988 if (mPausingActivity == r) {
2989 mPausingActivity = null;
2990 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002991 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002992
2993 r.configDestroy = false;
2994 r.frozenBeforeDestroy = false;
2995
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002996 if (setState) {
2997 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2998 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002999 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003000 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003001 }
3002
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003003 // Make sure this record is no longer in the pending finishes list.
3004 // This could happen, for example, if we are trimming activities
3005 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003006 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003007 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003008
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003009 // Remove any pending results.
3010 if (r.finishing && r.pendingResults != null) {
3011 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3012 PendingIntentRecord rec = apr.get();
3013 if (rec != null) {
3014 mService.cancelIntentSenderLocked(rec, false);
3015 }
3016 }
3017 r.pendingResults = null;
3018 }
3019
3020 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003021 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003022 }
3023
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003024 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003025 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003026 if (getVisibleBehindActivity() == r) {
3027 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003028 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003029 }
3030
3031 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003032 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003033 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003034 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003035 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003036 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003037 }
3038
Craig Mautner04a0ea62014-01-13 12:51:26 -08003039 private void removeActivityFromHistoryLocked(ActivityRecord r) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003040 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003041 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
3042 r.makeFinishing();
3043 if (DEBUG_ADD_REMOVE) {
3044 RuntimeException here = new RuntimeException("here");
3045 here.fillInStackTrace();
3046 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003047 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003048 r.takeFromHistory();
3049 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003050 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003051 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003052 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003053 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003054 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003055 if (VALIDATE_TOKENS) {
3056 validateAppTokensLocked();
3057 }
Craig Mautner312ba862014-02-10 17:55:01 -08003058 final TaskRecord task = r.task;
3059 if (task != null && task.removeActivity(r)) {
3060 if (DEBUG_STACK) Slog.i(TAG,
3061 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003062 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3063 task.isOverHomeStack()) {
3064 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner312ba862014-02-10 17:55:01 -08003065 }
Craig Mautner41db4a72014-05-07 17:20:56 -07003066 removeTask(task);
Craig Mautner312ba862014-02-10 17:55:01 -08003067 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003068 cleanUpActivityServicesLocked(r);
3069 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003070 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003071
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003072 /**
3073 * Perform clean-up of service connections in an activity record.
3074 */
3075 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3076 // Throw away any services that have been bound by this activity.
3077 if (r.connections != null) {
3078 Iterator<ConnectionRecord> it = r.connections.iterator();
3079 while (it.hasNext()) {
3080 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003081 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003082 }
3083 r.connections = null;
3084 }
3085 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003086
Craig Mautneree2e45a2014-06-27 12:10:03 -07003087 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003088 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003089 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003090 mHandler.sendMessage(msg);
3091 }
3092
Craig Mautneree2e45a2014-06-27 12:10:03 -07003093 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003094 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003095 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003096 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3097 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3098 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3099 final ActivityRecord r = activities.get(activityNdx);
3100 if (r.finishing) {
3101 continue;
3102 }
3103 if (r.fullscreen) {
3104 lastIsOpaque = true;
3105 }
3106 if (owner != null && r.app != owner) {
3107 continue;
3108 }
3109 if (!lastIsOpaque) {
3110 continue;
3111 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003112 if (r.isDestroyable()) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003113 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3114 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003115 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003116 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003117 activityRemoved = true;
3118 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003119 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003120 }
3121 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003122 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003123 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003124 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003125 }
3126
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003127 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3128 if (r.isDestroyable()) {
3129 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3130 + " resumed=" + mResumedActivity
3131 + " pausing=" + mPausingActivity + " for reason " + reason);
3132 return destroyActivityLocked(r, true, reason);
3133 }
3134 return false;
3135 }
3136
3137 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3138 String reason) {
3139 // Iterate over tasks starting at the back (oldest) first.
3140 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3141 int maxTasks = tasks.size() / 4;
3142 if (maxTasks < 1) {
3143 maxTasks = 1;
3144 }
3145 int numReleased = 0;
3146 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3147 final TaskRecord task = mTaskHistory.get(taskNdx);
3148 if (!tasks.contains(task)) {
3149 continue;
3150 }
3151 if (DEBUG_RELEASE) Slog.d(TAG, "Looking for activities to release in " + task);
3152 int curNum = 0;
3153 final ArrayList<ActivityRecord> activities = task.mActivities;
3154 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3155 final ActivityRecord activity = activities.get(actNdx);
3156 if (activity.app == app && activity.isDestroyable()) {
3157 if (DEBUG_RELEASE) Slog.v(TAG, "Destroying " + activity
3158 + " in state " + activity.state + " resumed=" + mResumedActivity
3159 + " pausing=" + mPausingActivity + " for reason " + reason);
3160 destroyActivityLocked(activity, true, reason);
3161 if (activities.get(actNdx) != activity) {
3162 // Was removed from list, back up so we don't miss the next one.
3163 actNdx--;
3164 }
3165 curNum++;
3166 }
3167 }
3168 if (curNum > 0) {
3169 numReleased += curNum;
3170 maxTasks--;
3171 if (mTaskHistory.get(taskNdx) != task) {
3172 // The entire task got removed, back up so we don't miss the next one.
3173 taskNdx--;
3174 }
3175 }
3176 }
3177 if (DEBUG_RELEASE) Slog.d(TAG, "Done releasing: did " + numReleased + " activities");
3178 return numReleased;
3179 }
3180
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003181 /**
3182 * Destroy the current CLIENT SIDE instance of an activity. This may be
3183 * called both when actually finishing an activity, or when performing
3184 * a configuration switch where we destroy the current client-side object
3185 * but then create a new client-side object for this same HistoryRecord.
3186 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003187 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003188 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003189 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003190 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3191 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003192 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003193 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003194
3195 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003196
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003197 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003198
3199 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003200
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003201 if (hadApp) {
3202 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003203 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003204 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3205 mService.mHeavyWeightProcess = null;
3206 mService.mHandler.sendEmptyMessage(
3207 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3208 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003209 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003210 // Update any services we are bound to that might care about whether
3211 // their client may have activities.
3212 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003213 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003214 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003215 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003216 }
3217 }
3218
3219 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003220
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003221 try {
3222 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003223 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003224 r.configChangeFlags);
3225 } catch (Exception e) {
3226 // We can just ignore exceptions here... if the process
3227 // has crashed, our death notification will clean things
3228 // up.
3229 //Slog.w(TAG, "Exception thrown during finish", e);
3230 if (r.finishing) {
3231 removeActivityFromHistoryLocked(r);
3232 removedFromHistory = true;
3233 skipDestroy = true;
3234 }
3235 }
3236
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003237 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003238
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003239 // If the activity is finishing, we need to wait on removing it
3240 // from the list to give it a chance to do its cleanup. During
3241 // that time it may make calls back with its token so we need to
3242 // be able to find it on the list and so we don't want to remove
3243 // it from the list yet. Otherwise, we can just immediately put
3244 // it in the destroyed state since we are not removing it from the
3245 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003246 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003247 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3248 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003249 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003250 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003251 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3252 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003253 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003254 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003255 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003256 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003257 }
3258 } else {
3259 // remove this record from the history.
3260 if (r.finishing) {
3261 removeActivityFromHistoryLocked(r);
3262 removedFromHistory = true;
3263 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003264 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003265 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003266 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003267 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003268 }
3269 }
3270
3271 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003272
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003273 if (!mLRUActivities.remove(r) && hadApp) {
3274 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3275 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003276
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003277 return removedFromHistory;
3278 }
3279
Craig Mautnerd2328952013-03-05 12:46:26 -08003280 final void activityDestroyedLocked(IBinder token) {
3281 final long origId = Binder.clearCallingIdentity();
3282 try {
3283 ActivityRecord r = ActivityRecord.forToken(token);
3284 if (r != null) {
3285 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003286 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003287 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003288
3289 if (isInStackLocked(token) != null) {
3290 if (r.state == ActivityState.DESTROYING) {
3291 cleanUpActivityLocked(r, true, false);
3292 removeActivityFromHistoryLocked(r);
3293 }
3294 }
Craig Mautner05d29032013-05-03 13:40:13 -07003295 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003296 } finally {
3297 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003298 }
3299 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003300
Jose Lima4b6c6692014-08-12 17:41:12 -07003301 void releaseBackgroundResources() {
3302 if (hasVisibleBehindActivity() &&
3303 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
3304 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautner64ccb702014-10-01 09:38:40 -07003305 if (r == topRunningActivityLocked(null)) {
3306 // Don't release the top activity if it has requested to run behind the next
3307 // activity.
3308 return;
3309 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003310 if (DEBUG_STATES) Slog.d(TAG, "releaseBackgroundResources activtyDisplay=" +
3311 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003312 " thread=" + r.app.thread);
3313 if (r != null && r.app != null && r.app.thread != null) {
3314 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003315 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003316 } catch (RemoteException e) {
3317 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003318 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003319 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003320 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
3321 backgroundResourcesReleased(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003322 }
3323 }
3324 }
3325
Jose Lima4b6c6692014-08-12 17:41:12 -07003326 final void backgroundResourcesReleased(IBinder token) {
3327 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3328 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003329 if (r != null) {
3330 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003331 setVisibleBehindActivity(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003332 }
3333 mStackSupervisor.resumeTopActivitiesLocked();
3334 }
3335
Jose Lima4b6c6692014-08-12 17:41:12 -07003336 boolean hasVisibleBehindActivity() {
3337 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003338 }
3339
Jose Lima4b6c6692014-08-12 17:41:12 -07003340 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003341 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003342 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003343 }
3344 }
3345
Jose Lima4b6c6692014-08-12 17:41:12 -07003346 ActivityRecord getVisibleBehindActivity() {
3347 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003348 }
3349
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003350 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3351 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003352 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003353 if (DEBUG_CLEANUP) Slog.v(
3354 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003355 + " with " + i + " entries");
3356 while (i > 0) {
3357 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003358 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003359 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003360 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003361 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003362 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003363 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003364 }
3365 }
3366 }
3367
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003368 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3369 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003370 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3371 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003372 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3373 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003374 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003375 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003376 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3377 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003378
3379 boolean hasVisibleActivities = false;
3380
3381 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003382 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003383 if (DEBUG_CLEANUP) Slog.v(
3384 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003385 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3386 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3387 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3388 final ActivityRecord r = activities.get(activityNdx);
3389 --i;
3390 if (DEBUG_CLEANUP) Slog.v(
3391 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3392 if (r.app == app) {
3393 boolean remove;
3394 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3395 // Don't currently have state for the activity, or
3396 // it is finishing -- always remove it.
3397 remove = true;
3398 } else if (r.launchCount > 2 &&
3399 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3400 // We have launched this activity too many times since it was
3401 // able to run, so give up and remove it.
3402 remove = true;
3403 } else {
3404 // The process may be gone, but the activity lives on!
3405 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003406 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003407 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003408 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003409 RuntimeException here = new RuntimeException("here");
3410 here.fillInStackTrace();
3411 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3412 + ": haveState=" + r.haveState
3413 + " stateNotNeeded=" + r.stateNotNeeded
3414 + " finishing=" + r.finishing
3415 + " state=" + r.state, here);
3416 }
3417 if (!r.finishing) {
3418 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3419 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3420 r.userId, System.identityHashCode(r),
3421 r.task.taskId, r.shortComponentName,
3422 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003423 if (r.state == ActivityState.RESUMED) {
3424 mService.updateUsageStats(r, false);
3425 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003426 }
3427 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003428
Craig Mautner0247fc82013-02-28 14:32:06 -08003429 } else {
3430 // We have the current state for this activity, so
3431 // it can be restarted later when needed.
3432 if (localLOGV) Slog.v(
3433 TAG, "Keeping entry, setting app to null");
3434 if (r.visible) {
3435 hasVisibleActivities = true;
3436 }
3437 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3438 + r);
3439 r.app = null;
3440 r.nowVisible = false;
3441 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003442 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003443 "App died, clearing saved state of " + r);
3444 r.icicle = null;
3445 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003446 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003447
Craig Mautnerd2328952013-03-05 12:46:26 -08003448 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003449 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003450 }
3451 }
3452
3453 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003454 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003455
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003456 final void updateTransitLocked(int transit, Bundle options) {
3457 if (options != null) {
3458 ActivityRecord r = topRunningActivityLocked(null);
3459 if (r != null && r.state != ActivityState.RESUMED) {
3460 r.updateOptionsLocked(options);
3461 } else {
3462 ActivityOptions.abort(options);
3463 }
3464 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003465 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003466 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003467
Craig Mautner21d24a22014-04-23 11:45:37 -07003468 void updateTaskMovement(TaskRecord task, boolean toFront) {
3469 if (task.isPersistable) {
3470 task.mLastTimeMoved = System.currentTimeMillis();
3471 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3472 // recently will be most negative, tasks sent to the bottom before that will be less
3473 // negative. Similarly for recent tasks moved to the top which will be most positive.
3474 if (!toFront) {
3475 task.mLastTimeMoved *= -1;
3476 }
3477 }
3478 }
3479
Craig Mautner84984fa2014-06-19 11:19:20 -07003480 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003481 final int top = mTaskHistory.size() - 1;
3482 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3483 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003484 if (task.taskType == homeStackTaskType) {
3485 if (DEBUG_TASKS || DEBUG_STACK)
3486 Slog.d(TAG, "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003487 mTaskHistory.remove(taskNdx);
3488 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003489 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003490 mWindowManager.moveTaskToTop(task.taskId);
3491 return;
3492 }
3493 }
3494 }
3495
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003496 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003497 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003498
Craig Mautner11bf9a52013-02-19 14:08:51 -08003499 final int numTasks = mTaskHistory.size();
3500 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003501 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003502 // nothing to do!
3503 if (reason != null &&
3504 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3505 ActivityOptions.abort(options);
3506 } else {
3507 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3508 }
3509 return;
3510 }
3511
Craig Mautnere0a38842013-12-16 16:14:02 -08003512 moveToFront();
Craig Mautnerfb1e20d2013-06-23 21:24:13 -07003513
Craig Mautner11bf9a52013-02-19 14:08:51 -08003514 // Shift all activities with this task up to the top
3515 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003516 insertTaskAtTop(tr);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003517
3518 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003519 if (reason != null &&
3520 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003521 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003522 ActivityRecord r = topRunningActivityLocked(null);
3523 if (r != null) {
3524 mNoAnimActivities.add(r);
3525 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003526 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003527 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003528 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003529 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003530
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003531 mWindowManager.moveTaskToTop(tr.taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003532
Craig Mautner05d29032013-05-03 13:40:13 -07003533 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003534 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003535
3536 if (VALIDATE_TOKENS) {
3537 validateAppTokensLocked();
3538 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003539 }
3540
3541 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003542 * Worker method for rearranging history stack. Implements the function of moving all
3543 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003544 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003545 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003546 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3547 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003548 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003549 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003550 * @return Returns true if the move completed, false if not.
3551 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003552 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003553 final TaskRecord tr = taskForIdLocked(taskId);
3554 if (tr == null) {
3555 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3556 return false;
3557 }
3558
3559 Slog.i(TAG, "moveTaskToBack: " + tr);
3560
3561 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003562
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003563 // If we have a watcher, preflight the move before committing to it. First check
3564 // for *other* available tasks, but if none are available, then try again allowing the
3565 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003566 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003567 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003568 if (next == null) {
3569 next = topRunningActivityLocked(null, 0);
3570 }
3571 if (next != null) {
3572 // ask watcher if this is allowed
3573 boolean moveOK = true;
3574 try {
3575 moveOK = mService.mController.activityResuming(next.packageName);
3576 } catch (RemoteException e) {
3577 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003578 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003579 }
3580 if (!moveOK) {
3581 return false;
3582 }
3583 }
3584 }
3585
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003586 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003587 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003588
Craig Mautner11bf9a52013-02-19 14:08:51 -08003589 mTaskHistory.remove(tr);
3590 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003591 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003592
Craig Mautnerc8143c62013-09-03 12:15:57 -07003593 // There is an assumption that moving a task to the back moves it behind the home activity.
3594 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003595 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003596 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3597 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003598 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003599 break;
3600 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003601 if (taskNdx == 1) {
3602 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003603 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003604 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003605 }
3606
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003607 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003608 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3609 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003610 ActivityRecord r = topRunningActivityLocked(null);
3611 if (r != null) {
3612 mNoAnimActivities.add(r);
3613 }
3614 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003615 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003616 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003617 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003618
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003619 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003620 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003621 }
3622
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003623 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Craig Mautner84984fa2014-06-19 11:19:20 -07003624 if (task == tr && tr.isOverHomeStack() || numTasks <= 1 && isOnHomeDisplay()) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003625 if (!mService.mBooting && !mService.mBooted) {
3626 // Not ready yet!
3627 return false;
3628 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003629 final int taskToReturnTo = tr.getTaskToReturnTo();
3630 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
3631 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003632 }
3633
Craig Mautner05d29032013-05-03 13:40:13 -07003634 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003635 return true;
3636 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003637
Craig Mautner8849a5e2013-04-02 16:41:03 -07003638 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003639 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003640 final Uri data = r.intent.getData();
3641 final String strData = data != null ? data.toSafeString() : null;
3642
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003643 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003644 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003645 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003646 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003647 }
3648
3649 /**
3650 * Make sure the given activity matches the current configuration. Returns
3651 * false if the activity had to be destroyed. Returns true if the
3652 * configuration is the same, or the activity will remain running as-is
3653 * for whatever reason. Ensures the HistoryRecord is updated with the
3654 * correct configuration and all other bookkeeping is handled.
3655 */
3656 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3657 int globalChanges) {
3658 if (mConfigWillChange) {
3659 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3660 "Skipping config check (will change): " + r);
3661 return true;
3662 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003663
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003664 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3665 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003666
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003667 // Short circuit: if the two configurations are the exact same
3668 // object (the common case), then there is nothing to do.
3669 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003670 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003671 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3672 "Configuration unchanged in " + r);
3673 return true;
3674 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003675
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003676 // We don't worry about activities that are finishing.
3677 if (r.finishing) {
3678 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3679 "Configuration doesn't matter in finishing " + r);
3680 r.stopFreezingScreenLocked(false);
3681 return true;
3682 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003683
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003684 // Okay we now are going to make this activity have the new config.
3685 // But then we need to figure out how it needs to deal with that.
3686 Configuration oldConfig = r.configuration;
3687 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003688
3689 // Determine what has changed. May be nothing, if this is a config
3690 // that has come back from the app after going idle. In that case
3691 // we just want to leave the official config object now in the
3692 // activity and do nothing else.
3693 final int changes = oldConfig.diff(newConfig);
3694 if (changes == 0 && !r.forceNewConfig) {
3695 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3696 "Configuration no differences in " + r);
3697 return true;
3698 }
3699
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003700 // If the activity isn't currently running, just leave the new
3701 // configuration and it will pick that up next time it starts.
3702 if (r.app == null || r.app.thread == null) {
3703 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3704 "Configuration doesn't matter not running " + r);
3705 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003706 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003707 return true;
3708 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003709
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003710 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003711 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3712 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3713 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003714 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003715 + ", newConfig=" + newConfig);
3716 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003717 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003718 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3719 r.configChangeFlags |= changes;
3720 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003721 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003722 if (r.app == null || r.app.thread == null) {
3723 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003724 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003725 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003726 } else if (r.state == ActivityState.PAUSING) {
3727 // A little annoying: we are waiting for this activity to
3728 // finish pausing. Let's not do anything now, but just
3729 // flag that it needs to be restarted when done pausing.
3730 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003731 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003732 r.configDestroy = true;
3733 return true;
3734 } else if (r.state == ActivityState.RESUMED) {
3735 // Try to optimize this case: the configuration is changing
3736 // and we need to restart the top, resumed activity.
3737 // Instead of doing the normal handshaking, just say
3738 // "restart!".
3739 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003740 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003741 relaunchActivityLocked(r, r.configChangeFlags, true);
3742 r.configChangeFlags = 0;
3743 } else {
3744 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003745 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003746 relaunchActivityLocked(r, r.configChangeFlags, false);
3747 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003748 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003749
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003750 // All done... tell the caller we weren't able to keep this
3751 // activity around.
3752 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003753 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003754
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003755 // Default case: the activity can handle this new configuration, so
3756 // hand it over. Note that we don't need to give it the new
3757 // configuration, since we always send configuration changes to all
3758 // process when they happen so it can just use whatever configuration
3759 // it last got.
3760 if (r.app != null && r.app.thread != null) {
3761 try {
3762 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003763 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003764 } catch (RemoteException e) {
3765 // If process died, whatever.
3766 }
3767 }
3768 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003769
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003770 return true;
3771 }
3772
Craig Mautnerc8143c62013-09-03 12:15:57 -07003773 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003774 int changes, boolean andResume) {
3775 List<ResultInfo> results = null;
3776 List<Intent> newIntents = null;
3777 if (andResume) {
3778 results = r.results;
3779 newIntents = r.newIntents;
3780 }
3781 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3782 + " with results=" + results + " newIntents=" + newIntents
3783 + " andResume=" + andResume);
3784 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003785 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003786 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003787
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003788 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003789
Craig Mautner34b73df2014-01-12 21:11:08 -08003790 mStackSupervisor.removeChildActivityContainers(r);
3791
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003792 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003793 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3794 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3795 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003796 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003797 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003798 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003799 // Note: don't need to call pauseIfSleepingLocked() here, because
3800 // the caller will only pass in 'andResume' if this activity is
3801 // currently resumed, which implies we aren't sleeping.
3802 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003803 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003804 }
3805
3806 if (andResume) {
3807 r.results = null;
3808 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003809 r.state = ActivityState.RESUMED;
3810 } else {
3811 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3812 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003813 }
3814
3815 return true;
3816 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003817
3818 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003819 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3820 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3821 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3822 final ActivityRecord r = activities.get(activityNdx);
3823 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003824 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003825 }
3826 if (r.fullscreen && !r.finishing) {
3827 return false;
3828 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003829 }
3830 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003831 final ActivityRecord r = ActivityRecord.forToken(token);
3832 if (r == null) {
3833 return false;
3834 }
3835 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3836 + " would have returned true for r=" + r);
3837 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003838 }
3839
3840 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003841 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3842 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3843 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3844 final ActivityRecord r = activities.get(activityNdx);
3845 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003846 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003847 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003848 }
3849 }
3850 }
3851
3852 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3853 boolean didSomething = false;
3854 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003855 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003856 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3857 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3858 int numActivities = activities.size();
3859 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3860 ActivityRecord r = activities.get(activityNdx);
3861 final boolean samePackage = r.packageName.equals(name)
3862 || (name == null && r.userId == userId);
3863 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3864 && (samePackage || r.task == lastTask)
3865 && (r.app == null || evenPersistent || !r.app.persistent)) {
3866 if (!doit) {
3867 if (r.finishing) {
3868 // If this activity is just finishing, then it is not
3869 // interesting as far as something to stop.
3870 continue;
3871 }
3872 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003873 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003874 if (r.isHomeActivity()) {
3875 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
3876 Slog.i(TAG, "Skip force-stop again " + r);
3877 continue;
3878 } else {
3879 homeActivity = r.realActivity;
3880 }
3881 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003882 didSomething = true;
3883 Slog.i(TAG, " Force finishing activity " + r);
3884 if (samePackage) {
3885 if (r.app != null) {
3886 r.app.removed = true;
3887 }
3888 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003889 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003890 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003891 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3892 true)) {
3893 // r has been deleted from mActivities, accommodate.
3894 --numActivities;
3895 --activityNdx;
3896 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003897 }
3898 }
3899 }
3900 return didSomething;
3901 }
3902
Dianne Hackborn09233282014-04-30 11:33:59 -07003903 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003904 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003905 final TaskRecord task = mTaskHistory.get(taskNdx);
3906 ActivityRecord r = null;
3907 ActivityRecord top = null;
3908 int numActivities = 0;
3909 int numRunning = 0;
3910 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003911 if (activities.isEmpty()) {
3912 continue;
3913 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07003914 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07003915 continue;
3916 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003917 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3918 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003919
Craig Mautneraab647e2013-02-28 16:31:36 -08003920 // Initialize state for next task if needed.
3921 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3922 top = r;
3923 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003924 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003925
3926 // Add 'r' into the current task.
3927 numActivities++;
3928 if (r.app != null && r.app.thread != null) {
3929 numRunning++;
3930 }
3931
3932 if (localLOGV) Slog.v(
3933 TAG, r.intent.getComponent().flattenToShortString()
3934 + ": task=" + r.task);
3935 }
3936
3937 RunningTaskInfo ci = new RunningTaskInfo();
3938 ci.id = task.taskId;
3939 ci.baseActivity = r.intent.getComponent();
3940 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003941 ci.lastActiveTime = task.lastActiveTime;
3942
Craig Mautnerc0ffce52014-07-01 12:38:52 -07003943 if (top.task != null) {
3944 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08003945 }
3946 ci.numActivities = numActivities;
3947 ci.numRunning = numRunning;
3948 //System.out.println(
3949 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08003950 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003951 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003952 }
3953
3954 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003955 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003956 if (DEBUG_SWITCH) Slog.d(
3957 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003958 if (top >= 0) {
3959 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3960 int activityTop = activities.size() - 1;
3961 if (activityTop > 0) {
3962 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3963 "unhandled-back", true);
3964 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003965 }
3966 }
3967
Craig Mautner6b74cb52013-09-27 17:02:21 -07003968 /**
3969 * Reset local parameters because an app's activity died.
3970 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003971 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003972 */
3973 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003974 if (mPausingActivity != null && mPausingActivity.app == app) {
3975 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3976 "App died while pausing: " + mPausingActivity);
3977 mPausingActivity = null;
3978 }
3979 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3980 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003981 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003982 }
3983
Craig Mautner19091252013-10-05 00:03:53 -07003984 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003985 }
3986
Craig Mautnercae015f2013-02-08 14:31:27 -08003987 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003988 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3989 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3990 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3991 final ActivityRecord r = activities.get(activityNdx);
3992 if (r.app == app) {
3993 Slog.w(TAG, " Force finishing activity "
3994 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07003995 // Force the destroy to skip right to removal.
3996 r.app = null;
3997 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003998 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003999 }
4000 }
4001 }
4002
Dianne Hackborn390517b2013-05-30 15:03:32 -07004003 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004004 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004005 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004006 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4007 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004008 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4009 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004010 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07004011 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004012 if (printed) {
4013 header = null;
4014 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004015 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004016 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004017 }
4018
4019 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4020 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4021
4022 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004023 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4024 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004025 }
4026 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004027 final int top = mTaskHistory.size() - 1;
4028 if (top >= 0) {
4029 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4030 int listTop = list.size() - 1;
4031 if (listTop >= 0) {
4032 activities.add(list.get(listTop));
4033 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004034 }
4035 } else {
4036 ItemMatcher matcher = new ItemMatcher();
4037 matcher.build(name);
4038
Craig Mautneraab647e2013-02-28 16:31:36 -08004039 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4040 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4041 if (matcher.match(r1, r1.intent.getComponent())) {
4042 activities.add(r1);
4043 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004044 }
4045 }
4046 }
4047
4048 return activities;
4049 }
4050
4051 ActivityRecord restartPackage(String packageName) {
4052 ActivityRecord starting = topRunningActivityLocked(null);
4053
4054 // All activities that came from the package must be
4055 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004056 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4057 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4058 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4059 final ActivityRecord a = activities.get(activityNdx);
4060 if (a.info.packageName.equals(packageName)) {
4061 a.forceNewConfig = true;
4062 if (starting != null && a == starting && a.visible) {
4063 a.startFreezingScreenLocked(starting.app,
4064 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4065 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004066 }
4067 }
4068 }
4069
4070 return starting;
4071 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004072
Craig Mautner41db4a72014-05-07 17:20:56 -07004073 void removeTask(TaskRecord task) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004074 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08004075 mWindowManager.removeTask(task.taskId);
4076 final ActivityRecord r = mResumedActivity;
4077 if (r != null && r.task == task) {
4078 mResumedActivity = null;
4079 }
4080
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004081 final int taskNdx = mTaskHistory.indexOf(task);
4082 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004083 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4084 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4085 if (!nextTask.isOverHomeStack()) {
4086 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4087 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004088 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004089 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004090 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004091
4092 if (task.mActivities.isEmpty()) {
4093 final boolean isVoiceSession = task.voiceSession != null;
4094 if (isVoiceSession) {
4095 try {
4096 task.voiceSession.taskFinished(task.intent, task.taskId);
4097 } catch (RemoteException e) {
4098 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004099 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004100 if (task.autoRemoveFromRecents() || isVoiceSession) {
4101 // Task creator asked to remove this when done, or this task was a voice
4102 // interaction, so it should not remain on the recent tasks list.
4103 mService.mRecentTasks.remove(task);
Dianne Hackborn852975d2014-08-22 17:42:43 -07004104 task.removedFromRecents(mService.mTaskPersister);
Craig Mautner41db4a72014-05-07 17:20:56 -07004105 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004106 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004107
4108 if (mTaskHistory.isEmpty()) {
4109 if (DEBUG_STACK) Slog.i(TAG, "removeTask: moving to back stack=" + this);
4110 if (isOnHomeDisplay()) {
4111 mStackSupervisor.moveHomeStack(!isHomeStack());
4112 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004113 if (mStacks != null) {
4114 mStacks.remove(this);
4115 mStacks.add(0, this);
4116 }
Craig Mautnerd163e752014-06-13 17:18:47 -07004117 mActivityContainer.onTaskListEmptyLocked();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004118 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004119 }
4120
Dianne Hackborn91097de2014-04-04 18:02:06 -07004121 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4122 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4123 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004124 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4125 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004126 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004127 return task;
4128 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004129
4130 ArrayList<TaskRecord> getAllTasks() {
4131 return new ArrayList<TaskRecord>(mTaskHistory);
4132 }
4133
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004134 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004135 task.stack = this;
4136 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07004137 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004138 } else {
4139 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004140 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004141 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004142 if (!moving && task.voiceSession != null) {
4143 try {
4144 task.voiceSession.taskStarted(task.intent, task.taskId);
4145 } catch (RemoteException e) {
4146 }
4147 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004148 }
4149
4150 public int getStackId() {
4151 return mStackId;
4152 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004153
4154 @Override
4155 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004156 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4157 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004158 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004159}