blob: c8b7205f806cf79f89739ff0d19fb502a32d46fb [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 Hackborn85d558c2014-11-04 10:31:54 -080047import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070048import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070049import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080050import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080051import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080052import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080053import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070054import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055
56import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070057import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070058import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080060import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080062import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070067import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080068import android.content.res.Resources;
69import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080070import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070071import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070072import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080073import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070074import android.os.Handler;
75import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090076import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070078import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079import android.os.RemoteException;
80import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080081import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070082import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070083import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070084import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070085import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070086import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070087
Craig Mautnercae015f2013-02-08 14:31:27 -080088import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080089import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070090import java.lang.ref.WeakReference;
91import java.util.ArrayList;
92import java.util.Iterator;
93import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080094import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070095
96/**
97 * State and management of a single stack of activities.
98 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070099final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800100
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700101 // Ticks during which we check progress while waiting for an app to launch.
102 static final int LAUNCH_TICK = 500;
103
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700104 // How long we wait until giving up on the last activity to pause. This
105 // is short because it directly impacts the responsiveness of starting the
106 // next activity.
107 static final int PAUSE_TIMEOUT = 500;
108
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700109 // How long we wait for the activity to tell us it has stopped before
110 // giving up. This is a good amount of time because we really need this
111 // from the application in order to get its saved state.
112 static final int STOP_TIMEOUT = 10*1000;
113
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700114 // How long we wait until giving up on an activity telling us it has
115 // finished destroying itself.
116 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800117
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700118 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800119 // disabled.
120 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800121
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700122 // How long between activity launches that we consider safe to not warn
123 // the user about an unexpected activity being launched on top.
124 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800125
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700126 // Set to false to disable the preview that is shown while a new activity
127 // is being started.
128 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800129
Craig Mautner5eda9b32013-07-02 11:58:16 -0700130 // How long to wait for all background Activities to redraw following a call to
131 // convertToTranslucent().
132 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
133
Craig Mautnera61bc652013-10-28 15:43:18 -0700134 static final boolean SCREENSHOT_FORCE_565 = ActivityManager.isLowRamDeviceStatic();
John Reck172e87c2013-10-02 16:55:16 -0700135
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700136 enum ActivityState {
137 INITIALIZING,
138 RESUMED,
139 PAUSING,
140 PAUSED,
141 STOPPING,
142 STOPPED,
143 FINISHING,
144 DESTROYING,
145 DESTROYED
146 }
147
148 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700149 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800150
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700151 /**
152 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800153 * running) activities. It contains #TaskRecord objects.
154 */
155 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
156
157 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800158 * Used for validating app tokens with window manager.
159 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800160 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800161
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700162 /**
163 * List of running activities, sorted by recent usage.
164 * The first entry in the list is the least recently used.
165 * It contains HistoryRecord objects.
166 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700167 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700168
169 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700170 * Animations that for the current transition have requested not to
171 * be considered for the transition animation.
172 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700173 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700174
175 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700176 * When we are in the process of pausing an activity, before starting the
177 * next one, this variable holds the activity that is currently being paused.
178 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800179 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700180
181 /**
182 * This is the last activity that we put into the paused state. This is
183 * used to determine if we need to do an activity transition while sleeping,
184 * when we normally hold the top activity paused.
185 */
186 ActivityRecord mLastPausedActivity = null;
187
188 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700189 * Activities that specify No History must be removed once the user navigates away from them.
190 * If the device goes to sleep with such an activity in the paused state then we save it here
191 * and finish it later if another activity replaces it on wakeup.
192 */
193 ActivityRecord mLastNoHistoryActivity = null;
194
195 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700196 * Current activity that is resumed, or null if there is none.
197 */
198 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800199
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700200 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700201 * This is the last activity that has been started. It is only used to
202 * identify when multiple activities are started at once so that the user
203 * can be warned they may not be in the activity they think they are.
204 */
205 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800206
Craig Mautner5eda9b32013-07-02 11:58:16 -0700207 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
208 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
209 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
210 // Activity in mTranslucentActivityWaiting is notified via
211 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
212 // background activity being drawn then the same call will be made with a true value.
213 ActivityRecord mTranslucentActivityWaiting = null;
Craig Mautnereb8abf72014-07-02 15:04:09 -0700214 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
Craig Mautner5eda9b32013-07-02 11:58:16 -0700215 new ArrayList<ActivityRecord>();
216
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700217 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700218 * Set when we know we are going to be calling updateConfiguration()
219 * soon, so want to skip intermediate config checks.
220 */
221 boolean mConfigWillChange;
222
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700223 long mLaunchStartTime = 0;
224 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800225
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800226 /**
Craig Mautnerb12428a2012-12-20 16:07:06 -0800227 * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
228 * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
229 */
230 private ActivityRecord mLastScreenshotActivity = null;
231 private Bitmap mLastScreenshotBitmap = null;
232
Craig Mautner858d8a62013-04-23 17:08:34 -0700233 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700234
Craig Mautnerc00204b2013-03-05 15:02:14 -0800235 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800236 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800237 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
238 ArrayList<ActivityStack> mStacks;
239 /** The attached Display's unique identifier, or -1 if detached */
240 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800241
Craig Mautner27084302013-03-25 08:05:25 -0700242 /** Run all ActivityStacks through this */
243 final ActivityStackSupervisor mStackSupervisor;
244
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700245 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700246 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
247 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
248 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
249 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700250 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700251 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700252 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700253
254 static class ScheduleDestroyArgs {
255 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700256 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700257 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700258 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700259 mReason = reason;
260 }
261 }
262
Zoran Marcetaf958b322012-08-09 20:27:12 +0900263 final Handler mHandler;
264
265 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700266 //public Handler() {
267 // if (localLOGV) Slog.v(TAG, "Handler started!");
268 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700269 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900270 super(looper);
271 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700272
Zoran Marcetaf958b322012-08-09 20:27:12 +0900273 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700274 public void handleMessage(Message msg) {
275 switch (msg.what) {
276 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800277 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700278 // We don't at this point know if the activity is fullscreen,
279 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800280 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700281 synchronized (mService) {
282 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700283 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700284 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700285 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800286 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700287 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700288 case LAUNCH_TICK_MSG: {
289 ActivityRecord r = (ActivityRecord)msg.obj;
290 synchronized (mService) {
291 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700292 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700293 }
294 }
295 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700296 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800297 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700298 // We don't at this point know if the activity is fullscreen,
299 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800300 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800301 synchronized (mService) {
302 activityDestroyedLocked(r != null ? r.appToken : null);
303 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700304 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700305 case STOP_TIMEOUT_MSG: {
306 ActivityRecord r = (ActivityRecord)msg.obj;
307 // We don't at this point know if the activity is fullscreen,
308 // so we need to be conservative and assume it isn't.
309 Slog.w(TAG, "Activity stop timeout for " + r);
310 synchronized (mService) {
311 if (r.isInHistory()) {
312 activityStoppedLocked(r, null, null, null);
313 }
314 }
315 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700316 case DESTROY_ACTIVITIES_MSG: {
317 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
318 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700319 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700320 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700321 } break;
322 case TRANSLUCENT_TIMEOUT_MSG: {
323 synchronized (mService) {
324 notifyActivityDrawnLocked(null);
325 }
326 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700327 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700328 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700329 final ActivityRecord r = getVisibleBehindActivity();
330 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700331 if (r != null) {
332 mService.killAppAtUsersRequest(r.app, null);
333 }
334 }
335 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700336 }
337 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800338 }
339
Craig Mautner34b73df2014-01-12 21:11:08 -0800340 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800341 int count = 0;
342 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
343 count += mTaskHistory.get(taskNdx).mActivities.size();
344 }
345 return count;
346 }
347
Craig Mautner4a1cb222013-12-04 16:14:06 -0800348 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer) {
349 mActivityContainer = activityContainer;
350 mStackSupervisor = activityContainer.getOuter();
351 mService = mStackSupervisor.mService;
352 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
353 mWindowManager = mService.mWindowManager;
354 mStackId = activityContainer.mStackId;
355 mCurrentUser = mService.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700356 }
Craig Mautner5962b122012-10-05 14:45:52 -0700357
Amith Yamasani734983f2014-03-04 16:48:05 -0800358 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100359 * Checks whether the userid is a profile of the current user.
Amith Yamasani734983f2014-03-04 16:48:05 -0800360 */
Kenny Guy2a764942014-04-02 13:29:20 +0100361 private boolean isCurrentProfileLocked(int userId) {
Kenny Guyf4824a02014-04-02 19:17:41 +0100362 if (userId == mCurrentUser) return true;
Kenny Guy2a764942014-04-02 13:29:20 +0100363 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
364 if (mService.mCurrentProfileIds[i] == userId) return true;
Amith Yamasani734983f2014-03-04 16:48:05 -0800365 }
366 return false;
367 }
368
369 boolean okToShowLocked(ActivityRecord r) {
Kenny Guy2a764942014-04-02 13:29:20 +0100370 return isCurrentProfileLocked(r.userId)
Craig Mautner5962b122012-10-05 14:45:52 -0700371 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
372 }
373
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700374 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800375 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700376 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
377 if (r != null) {
378 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800379 }
380 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700381 return null;
382 }
383
384 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800385 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
386 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800387 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800388 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
389 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800390 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800391 return r;
392 }
393 }
394 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700395 return null;
396 }
397
398 /**
399 * This is a simplified version of topRunningActivityLocked that provides a number of
400 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800401 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700402 * @param token If non-null, any history records matching this token will be skipped.
403 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800404 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700405 * @return Returns the HistoryRecord of the next activity on the stack.
406 */
407 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800408 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
409 TaskRecord task = mTaskHistory.get(taskNdx);
410 if (task.taskId == taskId) {
411 continue;
412 }
413 ArrayList<ActivityRecord> activities = task.mActivities;
414 for (int i = activities.size() - 1; i >= 0; --i) {
415 final ActivityRecord r = activities.get(i);
416 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800417 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800418 return r;
419 }
420 }
421 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700422 return null;
423 }
424
Craig Mautner8849a5e2013-04-02 16:41:03 -0700425 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700426 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
427 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700428 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
429 final ActivityRecord r = activities.get(activityNdx);
430 if (!r.finishing) {
431 return r;
432 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700433 }
434 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700435 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700436 }
437
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700438 final TaskRecord topTask() {
439 final int size = mTaskHistory.size();
440 if (size > 0) {
441 return mTaskHistory.get(size - 1);
442 }
443 return null;
444 }
445
Craig Mautnerd2328952013-03-05 12:46:26 -0800446 TaskRecord taskForIdLocked(int id) {
447 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
448 final TaskRecord task = mTaskHistory.get(taskNdx);
449 if (task.taskId == id) {
450 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800451 }
452 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700453 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700454 }
455
Craig Mautnerd2328952013-03-05 12:46:26 -0800456 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800457 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800458 if (r != null) {
459 final TaskRecord task = r.task;
Dianne Hackborn925e2b32014-09-03 16:40:49 -0700460 if (task != null && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Craig Mautnerd2328952013-03-05 12:46:26 -0800461 if (task.stack != this) Slog.w(TAG,
462 "Illegal state! task does not point to stack it is in.");
463 return r;
464 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800465 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800466 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800467 }
468
Craig Mautner2420ead2013-04-01 17:13:20 -0700469 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700470 final boolean hadit = mLRUActivities.remove(r);
471 mLRUActivities.add(r);
472 return hadit;
473 }
474
Craig Mautnerde4ef022013-04-07 19:01:33 -0700475 final boolean isHomeStack() {
476 return mStackId == HOME_STACK_ID;
477 }
478
Craig Mautnere0a38842013-12-16 16:14:02 -0800479 final boolean isOnHomeDisplay() {
480 return isAttached() &&
481 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
482 }
483
484 final void moveToFront() {
485 if (isAttached()) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800486 if (isOnHomeDisplay()) {
487 mStackSupervisor.moveHomeStack(isHomeStack());
488 }
Craig Mautnerfefeef82014-01-10 08:40:23 -0800489 mStacks.remove(this);
490 mStacks.add(this);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800491 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800492 }
493
494 final boolean isAttached() {
495 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800496 }
497
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700498 /**
499 * Returns the top activity in any existing task matching the given
500 * Intent. Returns null if no such task is found.
501 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700502 ActivityRecord findTaskLocked(ActivityRecord target) {
503 Intent intent = target.intent;
504 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700505 ComponentName cls = intent.getComponent();
506 if (info.targetActivity != null) {
507 cls = new ComponentName(info.packageName, info.targetActivity);
508 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700509 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700510 boolean isDocument = intent != null & intent.isDocument();
511 // If documentData is non-null then it must match the existing task data.
512 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800513
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700514 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800515 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
516 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700517 if (task.voiceSession != null) {
518 // We never match voice sessions; those always run independently.
519 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": voice session");
520 continue;
521 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700522 if (task.userId != userId) {
523 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700524 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700525 continue;
526 }
Craig Mautner000f0022013-02-26 15:04:29 -0800527 final ActivityRecord r = task.getTopActivity();
528 if (r == null || r.finishing || r.userId != userId ||
529 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700530 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800531 continue;
532 }
533
Craig Mautnerd00f4742014-03-12 14:17:26 -0700534 final Intent taskIntent = task.intent;
535 final Intent affinityIntent = task.affinityIntent;
536 final boolean taskIsDocument;
537 final Uri taskDocumentData;
538 if (taskIntent != null && taskIntent.isDocument()) {
539 taskIsDocument = true;
540 taskDocumentData = taskIntent.getData();
541 } else if (affinityIntent != null && affinityIntent.isDocument()) {
542 taskIsDocument = true;
543 taskDocumentData = affinityIntent.getData();
544 } else {
545 taskIsDocument = false;
546 taskDocumentData = null;
547 }
548
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700549 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700550 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700551 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700552 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700553 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
554 if (task.rootAffinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700555 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700556 return r;
557 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700558 } else if (taskIntent != null && taskIntent.getComponent() != null &&
559 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700560 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700561 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800562 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700563 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
564 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800565 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700566 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
567 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700568 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700569 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800570 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700571 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
572 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800573 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700574 } else if (DEBUG_TASKS) {
575 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700576 }
577 }
578
579 return null;
580 }
581
582 /**
583 * Returns the first activity (starting from the top of the stack) that
584 * is the same as the given activity. Returns null if no such activity
585 * is found.
586 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700587 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700588 ComponentName cls = intent.getComponent();
589 if (info.targetActivity != null) {
590 cls = new ComponentName(info.packageName, info.targetActivity);
591 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700592 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700593
Craig Mautner000f0022013-02-26 15:04:29 -0800594 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700595 TaskRecord task = mTaskHistory.get(taskNdx);
Kenny Guy2a764942014-04-02 13:29:20 +0100596 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700597 return null;
598 }
599 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800600 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
601 ActivityRecord r = activities.get(activityNdx);
602 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700603 //Slog.i(TAG, "Found matching class!");
604 //dump();
605 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
606 return r;
607 }
608 }
609 }
610
611 return null;
612 }
613
Amith Yamasani742a6712011-05-04 14:49:28 -0700614 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700615 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700616 */
Craig Mautner93529a42013-10-04 15:03:13 -0700617 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800618 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700619 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800620 }
621 mCurrentUser = userId;
622
623 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800624 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800625 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700626 TaskRecord task = mTaskHistory.get(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100627 if (isCurrentProfileLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700628 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
629 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800630 mTaskHistory.remove(i);
631 mTaskHistory.add(task);
632 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800633 // Use same value for i.
634 } else {
635 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800636 }
637 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700638 if (VALIDATE_TOKENS) {
639 validateAppTokensLocked();
640 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700641 }
642
Craig Mautner2420ead2013-04-01 17:13:20 -0700643 void minimalResumeActivityLocked(ActivityRecord r) {
644 r.state = ActivityState.RESUMED;
645 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
646 + " (starting new instance)");
647 r.stopped = false;
648 mResumedActivity = r;
649 r.task.touchActiveTime();
650 mService.addRecentTaskLocked(r.task);
651 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700652 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700653 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700654 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700655 }
656
Dianne Hackborncee04b52013-07-03 17:01:28 -0700657 private void startLaunchTraces() {
658 if (mFullyDrawnStartTime != 0) {
659 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
660 }
661 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
662 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
663 }
664
665 private void stopFullyDrawnTraceIfNeeded() {
666 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
667 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
668 mFullyDrawnStartTime = 0;
669 }
670 }
671
Craig Mautnere79d42682013-04-01 19:01:53 -0700672 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700673 if (r.displayStartTime == 0) {
674 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
675 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700676 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700677 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700678 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700679 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700680 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700681 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700682 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700683 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800684
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700685 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700686 // Make sure that there is no activity waiting for this to launch.
687 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
688 r.displayStartTime = r.fullyDrawnStartTime = 0;
689 } else {
690 mStackSupervisor.removeTimeoutsForActivityLocked(r);
691 mStackSupervisor.scheduleIdleTimeoutLocked(r);
692 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700693 }
694
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800695 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800696 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800697 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
698 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
699 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
700 activities.get(activityNdx).setSleeping(false);
701 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800702 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800703 }
704
Craig Mautner0eea92c2013-05-16 13:35:39 -0700705 /**
706 * @return true if something must be done before going to sleep.
707 */
708 boolean checkReadyForSleepLocked() {
709 if (mResumedActivity != null) {
710 // Still have something resumed; can't sleep until it is paused.
711 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
712 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700713 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700714 return true;
715 }
716 if (mPausingActivity != null) {
717 // Still waiting for something to pause; can't sleep yet.
718 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
719 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800720 }
721
Craig Mautner0eea92c2013-05-16 13:35:39 -0700722 return false;
723 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800724
Craig Mautner0eea92c2013-05-16 13:35:39 -0700725 void goToSleep() {
726 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800727
Craig Mautner0eea92c2013-05-16 13:35:39 -0700728 // Make sure any stopped but visible activities are now sleeping.
729 // This ensures that the activity's onStop() is called.
730 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
731 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
732 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
733 final ActivityRecord r = activities.get(activityNdx);
734 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
735 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800736 }
737 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800738 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700739 }
Craig Mautner59c00972012-07-30 12:10:24 -0700740
Winson Chung376543b2014-05-21 17:43:28 -0700741 /**
742 * This resets the saved state from the last screenshot, forcing a new screenshot to be taken
743 * again when requested.
744 */
745 private void invalidateLastScreenshot() {
746 mLastScreenshotActivity = null;
747 if (mLastScreenshotBitmap != null) {
748 mLastScreenshotBitmap.recycle();
749 }
750 mLastScreenshotBitmap = null;
751 }
752
Dianne Hackbornd2835932010-12-13 16:28:46 -0800753 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700754 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800755 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700756 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800757 return null;
758 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800759
Winson Chung083baf92014-07-11 10:32:42 -0700760 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700761 // This is an optimization -- since we never show Home or Recents within Recents itself,
762 // we can just go ahead and skip taking the screenshot if this is the home stack. In
763 // the case where the most recent task is not the task that was supplied, then the stack
764 // has changed, so invalidate the last screenshot().
765 invalidateLastScreenshot();
766 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tIs Home stack? " + isHomeStack());
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700767 return null;
768 }
769
Winson Chung48a10a52014-08-27 14:36:51 -0700770 int w = mService.mThumbnailWidth;
771 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800772 if (w > 0) {
Craig Mautnerb12428a2012-12-20 16:07:06 -0800773 if (who != mLastScreenshotActivity || mLastScreenshotBitmap == null
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700774 || mLastScreenshotActivity.state == ActivityState.RESUMED
Craig Mautnerb12428a2012-12-20 16:07:06 -0800775 || mLastScreenshotBitmap.getWidth() != w
776 || mLastScreenshotBitmap.getHeight() != h) {
Winson Chung376543b2014-05-21 17:43:28 -0700777 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tUpdating screenshot");
Craig Mautnerb12428a2012-12-20 16:07:06 -0800778 mLastScreenshotActivity = who;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700779 mLastScreenshotBitmap = mWindowManager.screenshotApplications(
John Reck172e87c2013-10-02 16:55:16 -0700780 who.appToken, Display.DEFAULT_DISPLAY, w, h, SCREENSHOT_FORCE_565);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800781 }
782 if (mLastScreenshotBitmap != null) {
Winson Chung376543b2014-05-21 17:43:28 -0700783 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tReusing last screenshot");
John Reck172e87c2013-10-02 16:55:16 -0700784 return mLastScreenshotBitmap.copy(mLastScreenshotBitmap.getConfig(), true);
Craig Mautnerb12428a2012-12-20 16:07:06 -0800785 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800786 }
Winson Chung376543b2014-05-21 17:43:28 -0700787 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800788 return null;
789 }
790
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700791 /**
792 * Start pausing the currently resumed activity. It is an error to call this if there
793 * is already an activity being paused or there is no resumed activity.
794 *
795 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
796 * @param uiSleeping True if this is happening with the user interface going to sleep (the
797 * screen turning off).
798 * @param resuming True if this is being called as part of resuming the top activity, so
799 * we shouldn't try to instigate a resume here.
800 * @param dontWait True if the caller does not want to wait for the pause to complete. If
801 * set to true, we will immediately complete the pause here before returning.
802 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
803 * it to tell us when it is done.
804 */
805 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
806 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800807 if (mPausingActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700808 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity);
809 completePauseLocked(false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800810 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700811 ActivityRecord prev = mResumedActivity;
812 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700813 if (!resuming) {
814 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
815 mStackSupervisor.resumeTopActivitiesLocked();
816 }
817 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700818 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800819
820 if (mActivityContainer.mParentActivity == null) {
821 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700822 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800823 }
824
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700825 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
826 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700827 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800828 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700829 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700830 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
831 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700832 prev.state = ActivityState.PAUSING;
833 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700834 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700835 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwaled3e2a082014-11-04 16:09:19 -0800836 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700837 prev.updateThumbnail(screenshotActivities(prev), null);
838 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700839 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700840
841 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800842
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700843 if (prev.app != null && prev.app.thread != null) {
844 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
845 try {
846 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700847 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700848 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700849 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800850 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700851 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700852 } catch (Exception e) {
853 // Ignore exception, if process died other code will cleanup.
854 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800855 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700856 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700857 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700858 }
859 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800860 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700861 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700862 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700863 }
864
865 // If we are not going to sleep, we want to ensure the device is
866 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700867 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700868 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700869 }
870
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800871 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700872 // Have the window manager pause its key dispatching until the new
873 // activity has started. If we're pausing the activity just because
874 // the screen is being turned off and the UI is sleeping, don't interrupt
875 // key dispatch; the same activity will pick it up again on wakeup.
876 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800877 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700878 } else {
879 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
880 }
881
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700882 if (dontWait) {
883 // If the caller said they don't want to wait for the pause, then complete
884 // the pause now.
885 completePauseLocked(false);
886 return false;
887
888 } else {
889 // Schedule a pause timeout in case the app doesn't respond.
890 // We don't give it much time because this directly impacts the
891 // responsiveness seen by the user.
892 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
893 msg.obj = prev;
894 prev.pauseTime = SystemClock.uptimeMillis();
895 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
896 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
897 return true;
898 }
899
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700900 } else {
901 // This activity failed to schedule the
902 // pause, so just treat it as being paused now.
903 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700904 if (!resuming) {
905 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
906 }
907 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700908 }
909 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700910
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700911 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700912 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800913 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700914
Craig Mautnerd2328952013-03-05 12:46:26 -0800915 final ActivityRecord r = isInStackLocked(token);
916 if (r != null) {
917 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
918 if (mPausingActivity == r) {
919 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
920 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700921 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800922 } else {
923 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
924 r.userId, System.identityHashCode(r), r.shortComponentName,
925 mPausingActivity != null
926 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700927 }
928 }
929 }
930
Craig Mautnera0026042014-04-23 11:45:37 -0700931 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
932 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700933 if (r.state != ActivityState.STOPPING) {
934 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
935 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
936 return;
937 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700938 if (persistentState != null) {
939 r.persistentState = persistentState;
940 mService.notifyTaskPersisterLocked(r.task, false);
941 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700942 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700943 if (icicle != null) {
944 // If icicle is null, this is happening due to a timeout, so we
945 // haven't really saved the state.
946 r.icicle = icicle;
947 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800948 r.launchCount = 0;
Craig Mautnera0026042014-04-23 11:45:37 -0700949 r.updateThumbnail(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700950 }
951 if (!r.stopped) {
952 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
953 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
954 r.stopped = true;
955 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -0700956 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
957 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700958 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700959 if (r.finishing) {
960 r.clearOptionsLocked();
961 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700962 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700963 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700964 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700965 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700966 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800967 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700968 }
969 }
970 }
971
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700972 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800973 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700974 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800975
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800976 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700977 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800978 if (prev.finishing) {
979 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700980 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800981 } else if (prev.app != null) {
982 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
983 if (prev.waitingVisible) {
984 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700985 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800986 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
987 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800988 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800989 if (prev.configDestroy) {
990 // The previous is being paused because the configuration
991 // is changing, which means it is actually stopping...
992 // To juggle the fact that we are also starting a new
993 // instance right now, we need to first completely stop
994 // the current instance before starting the new one.
995 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700996 destroyActivityLocked(prev, true, "pause-config");
Jose Lima4b6c6692014-08-12 17:41:12 -0700997 } else if (!hasVisibleBehindActivity()) {
998 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -0700999 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001000 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001001 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1002 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001003 // If we already have a few activities waiting to stop,
1004 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001005 // them out. Or if r is the last of activity of the last task the stack
1006 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001007 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001008 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001009 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001010 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001011 }
1012 }
1013 } else {
1014 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
1015 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001016 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001017 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001018 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001019
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001020 if (resumeNext) {
1021 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1022 if (!mService.isSleepingOrShuttingDown()) {
1023 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1024 } else {
1025 mStackSupervisor.checkReadyForSleepLocked();
1026 ActivityRecord top = topStack.topRunningActivityLocked(null);
1027 if (top == null || (prev != null && top != prev)) {
1028 // If there are no more activities available to run,
1029 // do resume anyway to start something. Also if the top
1030 // activity on the stack is not the just paused activity,
1031 // we need to go ahead and resume it to ensure we complete
1032 // an in-flight app switch.
1033 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1034 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001035 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001036 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001037
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001038 if (prev != null) {
1039 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001040
Craig Mautner525f3d92013-05-07 14:01:50 -07001041 if (prev.app != null && prev.cpuTimeAtResume > 0
1042 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001043 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1044 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001045 if (diff > 0) {
1046 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1047 synchronized (bsi) {
1048 BatteryStatsImpl.Uid.Proc ps =
1049 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001050 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001051 if (ps != null) {
1052 ps.addForegroundTimeLocked(diff);
1053 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001054 }
1055 }
1056 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001057 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001058 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001059 }
1060
1061 /**
1062 * Once we know that we have asked an application to put an activity in
1063 * the resumed state (either by launching it or explicitly telling it),
1064 * this function updates the rest of our state to match that fact.
1065 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001066 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001067 next.idle = false;
1068 next.results = null;
1069 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001070
1071 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1072 ProcessRecord app = next.task.mActivities.get(0).app;
1073 if (app != null && app != mService.mHomeProcess) {
1074 mService.mHomeProcess = app;
1075 }
1076 }
1077
Craig Mautner07566322013-09-26 16:42:55 -07001078 if (next.nowVisible) {
1079 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001080 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001081 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001082
1083 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001084 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001085
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001086 mStackSupervisor.reportResumedActivityLocked(next);
1087
1088 next.resumeKeyDispatchingLocked();
1089 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001090
1091 // Mark the point when the activity is resuming
1092 // TODO: To be more accurate, the mark should be before the onCreate,
1093 // not after the onResume. But for subsequent starts, onResume is fine.
1094 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001095 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001096 } else {
1097 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1098 }
Winson Chung376543b2014-05-21 17:43:28 -07001099
1100 // If we are resuming the activity that we had last screenshotted, then we know it will be
1101 // updated, so invalidate the last screenshot to ensure we take a fresh one when requested
1102 if (next == mLastScreenshotActivity) {
1103 invalidateLastScreenshot();
1104 }
George Mount6ba042b2014-07-28 11:12:28 -07001105 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001106
1107 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1108 // When resuming an activity, require it to call requestVisibleBehind() again.
1109 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1110 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001111 }
1112
Craig Mautnere3a00d72014-04-16 08:31:19 -07001113 private void setVisibile(ActivityRecord r, boolean visible) {
1114 r.visible = visible;
1115 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001116 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001117 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001118 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001119 container.setVisible(visible);
1120 }
1121 }
1122
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001123 // Find the first visible activity above the passed activity and if it is translucent return it
1124 // otherwise return null;
1125 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1126 TaskRecord task = r.task;
1127 if (task == null) {
1128 return null;
1129 }
1130
1131 ActivityStack stack = task.stack;
1132 if (stack == null) {
1133 return null;
1134 }
1135
1136 int stackNdx = mStacks.indexOf(stack);
1137
1138 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1139 int taskNdx = tasks.indexOf(task);
1140
1141 ArrayList<ActivityRecord> activities = task.mActivities;
1142 int activityNdx = activities.indexOf(r) + 1;
1143
1144 final int numStacks = mStacks.size();
1145 while (stackNdx < numStacks) {
1146 tasks = mStacks.get(stackNdx).mTaskHistory;
1147 final int numTasks = tasks.size();
1148 while (taskNdx < numTasks) {
1149 activities = tasks.get(taskNdx).mActivities;
1150 final int numActivities = activities.size();
1151 while (activityNdx < numActivities) {
1152 final ActivityRecord activity = activities.get(activityNdx);
1153 if (!activity.finishing) {
1154 return activity.fullscreen ? null : activity;
1155 }
1156 ++activityNdx;
1157 }
1158 activityNdx = 0;
1159 ++taskNdx;
1160 }
1161 taskNdx = 0;
1162 ++stackNdx;
1163 }
1164
1165 return null;
1166 }
1167
Jose Lima7ba71252014-04-30 12:59:27 -07001168 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1169 // If so, this stack is hidden, otherwise it is visible.
1170 private boolean isStackVisible() {
1171 if (!isAttached()) {
1172 return false;
1173 }
1174
1175 if (mStackSupervisor.isFrontStack(this)) {
1176 return true;
1177 }
1178
Jose Lima729cb232014-05-05 15:59:40 -07001179 /**
1180 * Start at the task above this one and go up, looking for a visible
1181 * fullscreen activity, or a translucent activity that requested the
1182 * wallpaper to be shown behind it.
1183 */
Jose Lima7ba71252014-04-30 12:59:27 -07001184 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
1185 final ArrayList<TaskRecord> tasks = mStacks.get(i).getAllTasks();
1186 for (int taskNdx = 0; taskNdx < tasks.size(); taskNdx++) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001187 final TaskRecord task = tasks.get(taskNdx);
1188 final ArrayList<ActivityRecord> activities = task.mActivities;
Jose Lima7ba71252014-04-30 12:59:27 -07001189 for (int activityNdx = 0; activityNdx < activities.size(); activityNdx++) {
1190 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001191
1192 // Conditions for an activity to obscure the stack we're
1193 // examining:
1194 // 1. Not Finishing AND Visible AND:
1195 // 2. Either:
1196 // - Full Screen Activity OR
1197 // - On top of Home and our stack is NOT home
1198 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001199 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001200 return false;
1201 }
1202 }
1203 }
1204 }
1205
1206 return true;
1207 }
1208
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001209 /**
1210 * Make sure that all activities that need to be visible (that is, they
1211 * currently can be seen by the user) actually are.
1212 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001213 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1214 ActivityRecord top = topRunningActivityLocked(null);
1215 if (top == null) {
1216 return;
1217 }
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001218 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001219 TAG, "ensureActivitiesVisible behind " + top
1220 + " configChanges=0x" + Integer.toHexString(configChanges));
1221
Craig Mautner5eda9b32013-07-02 11:58:16 -07001222 if (mTranslucentActivityWaiting != top) {
1223 mUndrawnActivitiesBelowTopTranslucent.clear();
1224 if (mTranslucentActivityWaiting != null) {
1225 // Call the callback with a timeout indication.
1226 notifyActivityDrawnLocked(null);
1227 mTranslucentActivityWaiting = null;
1228 }
1229 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1230 }
1231
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001232 // If the top activity is not fullscreen, then we need to
1233 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001234 boolean aboveTop = true;
Jose Lima7ba71252014-04-30 12:59:27 -07001235 boolean behindFullscreen = !isStackVisible();
1236
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001237 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001238 final TaskRecord task = mTaskHistory.get(taskNdx);
1239 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001240 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1241 final ActivityRecord r = activities.get(activityNdx);
1242 if (r.finishing) {
1243 continue;
1244 }
1245 if (aboveTop && r != top) {
1246 continue;
1247 }
1248 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001249 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1250 // but must be drawn initially for the animation as though they were visible.
1251 if (!behindFullscreen || r.mLaunchTaskBehind) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001252 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001253 TAG, "Make visible? " + r + " finishing=" + r.finishing
1254 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001255
Craig Mautnerd44711d2013-02-23 11:24:36 -08001256 // First: if this is not the current activity being started, make
1257 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001258 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001259 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001260 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001261
1262 if (r.app == null || r.app.thread == null) {
Craig Mautnerbb742462014-07-07 15:28:55 -07001263 // This activity needs to be visible, but isn't even
1264 // running... get it started, but don't resume it
1265 // at this point.
1266 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
1267 if (r != starting) {
1268 r.startFreezingScreenLocked(r.app, configChanges);
1269 }
1270 if (!r.visible || r.mLaunchTaskBehind) {
1271 if (DEBUG_VISBILITY) Slog.v(
1272 TAG, "Starting and making visible: " + r);
1273 setVisibile(r, true);
1274 }
1275 if (r != starting) {
1276 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001277 }
1278
1279 } else if (r.visible) {
1280 // If this activity is already visible, then there is nothing
1281 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001282 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001283 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001284 try {
George Mount6ba042b2014-07-28 11:12:28 -07001285 if (r.returningOptions != null) {
1286 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1287 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001288 }
1289 } catch(RemoteException e) {
1290 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001291 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001292 // This activity is not currently visible, but is running.
1293 // Tell it to become visible.
1294 r.visible = true;
1295 if (r.state != ActivityState.RESUMED && r != starting) {
1296 // If this activity is paused, tell it
1297 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001298 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001299 TAG, "Making visible and scheduling visibility: " + r);
1300 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001301 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001302 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001303 mUndrawnActivitiesBelowTopTranslucent.add(r);
1304 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001305 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001306 r.sleeping = false;
1307 r.app.pendingUiClean = true;
1308 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1309 r.stopFreezingScreenLocked(false);
1310 } catch (Exception e) {
1311 // Just skip on any failure; we'll make it
1312 // visible when it next restarts.
1313 Slog.w(TAG, "Exception thrown making visibile: "
1314 + r.intent.getComponent(), e);
1315 }
1316 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001317 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001318
Craig Mautnerd44711d2013-02-23 11:24:36 -08001319 // Aggregate current change flags.
1320 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001321
Craig Mautnerd44711d2013-02-23 11:24:36 -08001322 if (r.fullscreen) {
1323 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001324 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1325 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001326 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001327 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001328 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001329 }
1330 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001331 if (DEBUG_VISBILITY) Slog.v(
1332 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1333 + " state=" + r.state
1334 + " behindFullscreen=" + behindFullscreen);
1335 // Now for any activities that aren't visible to the user, make
1336 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001337 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001338 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001339 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001340 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001341 switch (r.state) {
1342 case STOPPING:
1343 case STOPPED:
1344 if (r.app != null && r.app.thread != null) {
1345 if (DEBUG_VISBILITY) Slog.v(
1346 TAG, "Scheduling invisibility: " + r);
1347 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1348 }
1349 break;
1350
1351 case INITIALIZING:
1352 case RESUMED:
1353 case PAUSING:
1354 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001355 // This case created for transitioning activities from
1356 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001357 if (getVisibleBehindActivity() == r) {
1358 releaseBackgroundResources();
Craig Mautneree2e45a2014-06-27 12:10:03 -07001359 } else {
1360 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1361 mStackSupervisor.mStoppingActivities.add(r);
1362 }
1363 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001364 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001365 break;
1366
1367 default:
1368 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001369 }
1370 } catch (Exception e) {
1371 // Just skip on any failure; we'll make it
1372 // visible when it next restarts.
1373 Slog.w(TAG, "Exception thrown making hidden: "
1374 + r.intent.getComponent(), e);
1375 }
1376 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001377 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001378 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001379 }
1380 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001381 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001382
1383 if (mTranslucentActivityWaiting != null &&
1384 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1385 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1386 notifyActivityDrawnLocked(null);
1387 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001388 }
Craig Mautner58547802013-03-05 08:23:53 -08001389
George Mount6ba042b2014-07-28 11:12:28 -07001390 void convertToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001391 mTranslucentActivityWaiting = r;
1392 mUndrawnActivitiesBelowTopTranslucent.clear();
1393 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1394 }
1395
1396 /**
1397 * Called as activities below the top translucent activity are redrawn. When the last one is
1398 * redrawn notify the top activity by calling
1399 * {@link Activity#onTranslucentConversionComplete}.
1400 *
1401 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1402 * occurred and the activity will be notified immediately.
1403 */
1404 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001405 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001406 if ((r == null)
1407 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1408 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1409 // The last undrawn activity below the top has just been drawn. If there is an
1410 // opaque activity at the top, notify it that it can become translucent safely now.
1411 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1412 mTranslucentActivityWaiting = null;
1413 mUndrawnActivitiesBelowTopTranslucent.clear();
1414 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1415
Craig Mautner71dd1b62014-02-18 15:48:52 -08001416 if (waitingActivity != null) {
1417 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1418 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1419 try {
1420 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1421 waitingActivity.appToken, r != null);
1422 } catch (RemoteException e) {
1423 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001424 }
1425 }
1426 }
1427 }
1428
Craig Mautnera61bc652013-10-28 15:43:18 -07001429 /** If any activities below the top running one are in the INITIALIZING state and they have a
1430 * starting window displayed then remove that starting window. It is possible that the activity
1431 * in this state will never resumed in which case that starting window will be orphaned. */
1432 void cancelInitializingActivities() {
1433 final ActivityRecord topActivity = topRunningActivityLocked(null);
1434 boolean aboveTop = true;
1435 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1436 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1437 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1438 final ActivityRecord r = activities.get(activityNdx);
1439 if (aboveTop) {
1440 if (r == topActivity) {
1441 aboveTop = false;
1442 }
1443 continue;
1444 }
1445
1446 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1447 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1448 r.mStartingWindowShown = false;
1449 mWindowManager.removeAppStartingWindow(r.appToken);
1450 }
1451 }
1452 }
1453 }
1454
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001455 /**
1456 * Ensure that the top activity in the stack is resumed.
1457 *
1458 * @param prev The previously resumed activity, for when in the process
1459 * of pausing; can be null to call from elsewhere.
1460 *
1461 * @return Returns true if something is being resumed, or false if
1462 * nothing happened.
1463 */
1464 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001465 return resumeTopActivityLocked(prev, null);
1466 }
1467
1468 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001469 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001470 // Don't even start recursing.
1471 return false;
1472 }
1473
1474 boolean result = false;
1475 try {
1476 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001477 mStackSupervisor.inResumeTopActivity = true;
1478 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1479 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
1480 mService.comeOutOfSleepIfNeededLocked();
1481 }
Craig Mautner544efa72014-09-04 16:41:20 -07001482 result = resumeTopActivityInnerLocked(prev, options);
1483 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001484 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001485 }
1486 return result;
1487 }
1488
1489 final boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001490 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1491
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001492 if (!mService.mBooting && !mService.mBooted) {
1493 // Not ready yet!
1494 return false;
1495 }
1496
Craig Mautnerdf88d732014-01-27 09:21:32 -08001497 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001498 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001499 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001500 // Do not resume this stack if its parent is not resumed.
1501 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1502 return false;
1503 }
1504
Craig Mautnera61bc652013-10-28 15:43:18 -07001505 cancelInitializingActivities();
1506
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001507 // Find the first activity that is not finishing.
1508 ActivityRecord next = topRunningActivityLocked(null);
1509
1510 // Remember how we'll process this pause/resume situation, and ensure
1511 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001512 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1513 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001514
Craig Mautner84984fa2014-06-19 11:19:20 -07001515 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001516 if (next == null) {
1517 // There are no more activities! Let's just start up the
1518 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001519 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001520 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001521 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001522 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001523 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1524 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1525 return isOnHomeDisplay() &&
1526 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001527 }
1528
1529 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001530
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001531 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001532 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1533 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001534 // Make sure we have executed any pending transitions, since there
1535 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001536 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001537 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001538 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001539 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001540 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001541 return false;
1542 }
1543
Craig Mautner525f3d92013-05-07 14:01:50 -07001544 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001545 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001546 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001547 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001548 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001549 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001550 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001551 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001552 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001553 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001554 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001555 } else {
Craig Mautnere0a38842013-12-16 16:14:02 -08001556 if (DEBUG_STATES && isOnHomeDisplay()) Slog.d(TAG,
1557 "resumeTopActivityLocked: Launching home next");
1558 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001559 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1560 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1561 return isOnHomeDisplay() &&
1562 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001563 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001564 }
1565
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001566 // If we are sleeping, and there is no resumed activity, and the top
1567 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001568 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001569 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001570 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001571 // Make sure we have executed any pending transitions, since there
1572 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001573 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001574 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001575 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001576 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001577 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001578 return false;
1579 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001580
1581 // Make sure that the user who owns this activity is started. If not,
1582 // we will just leave it as is because someone should be bringing
1583 // another user's activities to the top of the stack.
1584 if (mService.mStartedUsers.get(next.userId) == null) {
1585 Slog.w(TAG, "Skipping resume of top activity " + next
1586 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001587 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001588 return false;
1589 }
1590
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001591 // The activity may be waiting for stop, but that is no longer
1592 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001593 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001594 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001595 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001596 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001597
1598 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1599
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001600 // If we are currently pausing an activity, then don't do anything
1601 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001602 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001603 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1604 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001605 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001606 return false;
1607 }
1608
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001609 // Okay we are now going to start a switch, to 'next'. We may first
1610 // have to pause the current activity, but this is an important point
1611 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001612 // XXX "App Redirected" dialog is getting too many false positives
1613 // at this point, so turn off for now.
1614 if (false) {
1615 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1616 long now = SystemClock.uptimeMillis();
1617 final boolean inTime = mLastStartedActivity.startTime != 0
1618 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1619 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1620 final int nextUid = next.info.applicationInfo.uid;
1621 if (inTime && lastUid != nextUid
1622 && lastUid != next.launchedFromUid
1623 && mService.checkPermission(
1624 android.Manifest.permission.STOP_APP_SWITCHES,
1625 -1, next.launchedFromUid)
1626 != PackageManager.PERMISSION_GRANTED) {
1627 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1628 } else {
1629 next.startTime = now;
1630 mLastStartedActivity = next;
1631 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001632 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001633 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001634 mLastStartedActivity = next;
1635 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001636 }
Craig Mautner58547802013-03-05 08:23:53 -08001637
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001638 // We need to start pausing the current activity so the top one
1639 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001640 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1641 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001642 if (mResumedActivity != null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001643 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001644 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001645 }
1646 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001647 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1648 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001649 // At this point we want to put the upcoming activity's process
1650 // at the top of the LRU list, since we know we will be needing it
1651 // very soon and it would be a waste to let it get killed if it
1652 // happens to be sitting towards the end.
1653 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001654 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001655 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001656 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001657 return true;
1658 }
1659
Christopher Tated3f175c2012-06-14 14:16:54 -07001660 // If the most recent activity was noHistory but was only stopped rather
1661 // than stopped+finished because the device went to sleep, we need to make
1662 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001663 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001664 !mLastNoHistoryActivity.finishing) {
1665 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1666 " on new resume");
1667 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1668 null, "no-history", false);
1669 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001670 }
1671
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001672 if (prev != null && prev != next) {
1673 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1674 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001675 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001676 if (DEBUG_SWITCH) Slog.v(
1677 TAG, "Resuming top, waiting visible to hide: " + prev);
1678 } else {
1679 // The next activity is already visible, so hide the previous
1680 // activity's windows right now so we can show the new one ASAP.
1681 // We only do this if the previous is finishing, which should mean
1682 // it is on top of the one being resumed so hiding it quickly
1683 // is good. Otherwise, we want to do the normal route of allowing
1684 // the resumed activity to be shown so we can decide if the
1685 // previous should actually be hidden depending on whether the
1686 // new one is found to be full-screen or not.
1687 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001688 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001689 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1690 + prev + ", waitingVisible="
1691 + (prev != null ? prev.waitingVisible : null)
1692 + ", nowVisible=" + next.nowVisible);
1693 } else {
1694 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1695 + prev + ", waitingVisible="
1696 + (prev != null ? prev.waitingVisible : null)
1697 + ", nowVisible=" + next.nowVisible);
1698 }
1699 }
1700 }
1701
Dianne Hackborne7f97212011-02-24 14:40:20 -08001702 // Launching this app's activity, make sure the app is no longer
1703 // considered stopped.
1704 try {
1705 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001706 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001707 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001708 } catch (IllegalArgumentException e) {
1709 Slog.w(TAG, "Failed trying to unstop package "
1710 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001711 }
1712
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001713 // We are starting up the next activity, so tell the window manager
1714 // that the previous one will be hidden soon. This way it can know
1715 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001716 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001717 if (prev != null) {
1718 if (prev.finishing) {
1719 if (DEBUG_TRANSITION) Slog.v(TAG,
1720 "Prepare close transition: prev=" + prev);
1721 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001722 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001723 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001724 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001725 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001726 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1727 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001728 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001729 mWindowManager.setAppWillBeHidden(prev.appToken);
1730 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001731 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001732 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001733 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001734 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001735 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001736 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001737 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001738 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001739 : next.mLaunchTaskBehind
1740 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1741 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001742 }
1743 }
1744 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001745 mWindowManager.setAppWillBeHidden(prev.appToken);
1746 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001747 }
Craig Mautner967212c2013-04-13 21:10:58 -07001748 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001749 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001750 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001751 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001752 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001753 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001754 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001755 }
1756 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001757
1758 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001759 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001760 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1761 if (opts != null) {
1762 resumeAnimOptions = opts.toBundle();
1763 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001764 next.applyOptionsLocked();
1765 } else {
1766 next.clearOptionsLocked();
1767 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001768
Craig Mautnercf910b02013-04-23 11:23:27 -07001769 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001770 if (next.app != null && next.app.thread != null) {
1771 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1772
1773 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001774 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001775
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001776 // schedule launch ticks to collect information about slow apps.
1777 next.startLaunchTickingLocked();
1778
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001779 ActivityRecord lastResumedActivity =
1780 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001781 ActivityState lastState = next.state;
1782
1783 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001784
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001785 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001786 next.state = ActivityState.RESUMED;
1787 mResumedActivity = next;
1788 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001789 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001790 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001791 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001792 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001793
1794 // Have the window manager re-evaluate the orientation of
1795 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001796 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001797 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001798 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001799 mService.mConfiguration,
1800 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1801 if (config != null) {
1802 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001803 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001804 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001805 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001806
Craig Mautner525f3d92013-05-07 14:01:50 -07001807 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001808 // The configuration update wasn't able to keep the existing
1809 // instance of the activity, and instead started a new one.
1810 // We should be all done, but let's just make sure our activity
1811 // is still at the top and schedule another run if something
1812 // weird happened.
1813 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001814 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001815 "Activity config changed during resume: " + next
1816 + ", new next: " + nextNext);
1817 if (nextNext != next) {
1818 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001819 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001820 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001821 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001822 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001823 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001824 return true;
1825 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001826 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001827 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001828 }
Craig Mautner58547802013-03-05 08:23:53 -08001829
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001830 try {
1831 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001832 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001833 if (a != null) {
1834 final int N = a.size();
1835 if (!next.finishing && N > 0) {
1836 if (DEBUG_RESULTS) Slog.v(
1837 TAG, "Delivering results to " + next
1838 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001839 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001840 }
1841 }
1842
1843 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001844 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001845 }
1846
1847 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001848 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001849 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001850
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001851 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001852 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001853 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001854 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001855 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001856 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001857 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001858
Craig Mautner0eea92c2013-05-16 13:35:39 -07001859 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001860
Craig Mautnerac6f8432013-07-17 13:24:59 -07001861 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001862 } catch (Exception e) {
1863 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001864 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1865 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001866 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001867 if (lastStack != null) {
1868 lastStack.mResumedActivity = lastResumedActivity;
1869 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001870 Slog.i(TAG, "Restarting because process died: " + next);
1871 if (!next.hasBeenLaunched) {
1872 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001873 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1874 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001875 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001876 next.appToken, next.packageName, next.theme,
1877 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001878 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1879 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001880 }
George Mount2c92c972014-03-20 09:38:23 -07001881 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001882 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001883 return true;
1884 }
1885
1886 // From this point on, if something goes wrong there is no way
1887 // to recover the activity.
1888 try {
1889 next.visible = true;
1890 completeResumeLocked(next);
1891 } catch (Exception e) {
1892 // If any exception gets thrown, toss away this
1893 // activity and try the next one.
1894 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001895 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001896 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001897 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001898 return true;
1899 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001900 next.stopped = false;
1901
1902 } else {
1903 // Whoops, need to restart this activity!
1904 if (!next.hasBeenLaunched) {
1905 next.hasBeenLaunched = true;
1906 } else {
1907 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001908 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001909 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001910 mService.compatibilityInfoForPackageLocked(
1911 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001912 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001913 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001914 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001915 }
1916 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1917 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001918 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001919 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001920 }
1921
Craig Mautnercf910b02013-04-23 11:23:27 -07001922 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001923 return true;
1924 }
1925
Craig Mautnerac6f8432013-07-17 13:24:59 -07001926 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001927 // If this is being moved to the top by another activity or being launched from the home
1928 // activity, set mOnTopOfHome accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001929 if (isOnHomeDisplay()) {
1930 ActivityStack lastStack = mStackSupervisor.getLastStack();
1931 final boolean fromHome = lastStack.isHomeStack();
1932 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07001933 task.setTaskToReturnTo(fromHome
1934 ? lastStack.topTask() == null
1935 ? HOME_ACTIVITY_TYPE
1936 : lastStack.topTask().taskType
1937 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08001938 }
1939 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07001940 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07001941 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001942
Craig Mautnerac6f8432013-07-17 13:24:59 -07001943 mTaskHistory.remove(task);
1944 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07001945 int taskNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01001946 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001947 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07001948 while (--taskNdx >= 0) {
1949 if (!isCurrentProfileLocked(mTaskHistory.get(taskNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001950 break;
1951 }
1952 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001953 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001954 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001955 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001956 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001957 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001958
Craig Mautner8849a5e2013-04-02 16:41:03 -07001959 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001960 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001961 TaskRecord rTask = r.task;
1962 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07001963 // mLaunchTaskBehind tasks get placed at the back of the task stack.
1964 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08001965 // Last activity in task had been removed or ActivityManagerService is reusing task.
1966 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001967 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001968 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001969 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001970 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001971 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001972 if (!newTask) {
1973 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001974 boolean startIt = true;
1975 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1976 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08001977 if (task.getTopActivity() == null) {
1978 // All activities in task are finishing.
1979 continue;
1980 }
Craig Mautner70a86932013-02-28 22:37:44 -08001981 if (task == r.task) {
1982 // Here it is! Now, if this is not yet visible to the
1983 // user, then just add it without starting; it will
1984 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001985 if (!startIt) {
1986 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1987 + task, new RuntimeException("here").fillInStackTrace());
1988 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001989 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001990 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1991 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001992 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07001993 r.userId, r.info.configChanges, task.voiceSession != null,
1994 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001995 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001996 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001997 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001998 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001999 return;
2000 }
2001 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002002 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002003 startIt = false;
2004 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002005 }
2006 }
2007
2008 // Place a new activity at top of stack, so it is next to interact
2009 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002010
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002011 // If we are not placing the new activity frontmost, we do not want
2012 // to deliver the onUserLeaving callback to the actual frontmost
2013 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002014 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002015 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08002016 if (DEBUG_USER_LEAVING) Slog.v(TAG,
2017 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002018 }
Craig Mautner70a86932013-02-28 22:37:44 -08002019
2020 task = r.task;
2021
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002022 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002023 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002024 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002025 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002026 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002027
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002028 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002029 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002030 // We want to show the starting preview window if we are
2031 // switching to a new task, or the next activity's process is
2032 // not currently running.
2033 boolean showStartingIcon = newTask;
2034 ProcessRecord proc = r.app;
2035 if (proc == null) {
2036 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2037 }
2038 if (proc == null || proc.thread == null) {
2039 showStartingIcon = true;
2040 }
2041 if (DEBUG_TRANSITION) Slog.v(TAG,
2042 "Prepare open transition: starting " + r);
2043 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002044 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002045 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002046 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002047 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002048 ? r.mLaunchTaskBehind
2049 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2050 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002051 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002052 mNoAnimActivities.remove(r);
2053 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002054 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002055 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002056 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002057 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002058 boolean doShow = true;
2059 if (newTask) {
2060 // Even though this activity is starting fresh, we still need
2061 // to reset it to make sure we apply affinities to move any
2062 // existing activities from other tasks in to it.
2063 // If the caller has requested that the target task be
2064 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002065 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002066 resetTaskIfNeededLocked(r, r);
2067 doShow = topRunningNonDelayedActivityLocked(null) == r;
2068 }
George Mount70778d72014-07-01 16:33:45 -07002069 } else if (options != null && new ActivityOptions(options).getAnimationType()
2070 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2071 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002072 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002073 if (r.mLaunchTaskBehind) {
2074 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2075 // tell WindowManager that r is visible even though it is at the back of the stack.
2076 mWindowManager.setAppVisibility(r.appToken, true);
2077 ensureActivitiesVisibleLocked(null, 0);
2078 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002079 // Figure out if we are transitioning from another activity that is
2080 // "has the same starting icon" as the next one. This allows the
2081 // window manager to keep the previous window it had previously
2082 // created, if it still had one.
2083 ActivityRecord prev = mResumedActivity;
2084 if (prev != null) {
2085 // We don't want to reuse the previous starting preview if:
2086 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002087 if (prev.task != r.task) {
2088 prev = null;
2089 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002090 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002091 else if (prev.nowVisible) {
2092 prev = null;
2093 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002094 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002095 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002096 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002097 mService.compatibilityInfoForPackageLocked(
2098 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002099 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002100 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002101 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002102 }
2103 } else {
2104 // If this is the first activity, don't do any fancy animations,
2105 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002106 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002107 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002108 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002109 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002110 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002111 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002112 }
2113 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002114 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002115 }
2116
2117 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002118 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002119 }
2120 }
2121
Dianne Hackbornbe707852011-11-11 14:32:10 -08002122 final void validateAppTokensLocked() {
2123 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002124 mValidateAppTokens.ensureCapacity(numActivities());
2125 final int numTasks = mTaskHistory.size();
2126 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2127 TaskRecord task = mTaskHistory.get(taskNdx);
2128 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002129 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002130 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002131 }
Craig Mautner000f0022013-02-26 15:04:29 -08002132 TaskGroup group = new TaskGroup();
2133 group.taskId = task.taskId;
2134 mValidateAppTokens.add(group);
2135 final int numActivities = activities.size();
2136 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2137 final ActivityRecord r = activities.get(activityNdx);
2138 group.tokens.add(r.appToken);
2139 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002140 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002141 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002142 }
2143
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002144 /**
2145 * Perform a reset of the given task, if needed as part of launching it.
2146 * Returns the new HistoryRecord at the top of the task.
2147 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002148 /**
2149 * Helper method for #resetTaskIfNeededLocked.
2150 * We are inside of the task being reset... we'll either finish this activity, push it out
2151 * for another task, or leave it as-is.
2152 * @param task The task containing the Activity (taskTop) that might be reset.
2153 * @param forceReset
2154 * @return An ActivityOptions that needs to be processed.
2155 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002156 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002157 ActivityOptions topOptions = null;
2158
2159 int replyChainEnd = -1;
2160 boolean canMoveOptions = true;
2161
2162 // We only do this for activities that are not the root of the task (since if we finish
2163 // the root, we may no longer have the task!).
2164 final ArrayList<ActivityRecord> activities = task.mActivities;
2165 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002166 final int rootActivityNdx = task.findEffectiveRootIndex();
2167 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002168 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002169 if (target.frontOfTask)
2170 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002171
2172 final int flags = target.info.flags;
2173 final boolean finishOnTaskLaunch =
2174 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2175 final boolean allowTaskReparenting =
2176 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2177 final boolean clearWhenTaskReset =
2178 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2179
2180 if (!finishOnTaskLaunch
2181 && !clearWhenTaskReset
2182 && target.resultTo != null) {
2183 // If this activity is sending a reply to a previous
2184 // activity, we can't do anything with it now until
2185 // we reach the start of the reply chain.
2186 // XXX note that we are assuming the result is always
2187 // to the previous activity, which is almost always
2188 // the case but we really shouldn't count on.
2189 if (replyChainEnd < 0) {
2190 replyChainEnd = i;
2191 }
2192 } else if (!finishOnTaskLaunch
2193 && !clearWhenTaskReset
2194 && allowTaskReparenting
2195 && target.taskAffinity != null
2196 && !target.taskAffinity.equals(task.affinity)) {
2197 // If this activity has an affinity for another
2198 // task, then we need to move it out of here. We will
2199 // move it as far out of the way as possible, to the
2200 // bottom of the activity stack. This also keeps it
2201 // correctly ordered with any activities we previously
2202 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002203 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002204 final ActivityRecord bottom =
2205 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002206 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002207 if (bottom != null && target.taskAffinity != null
2208 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002209 // If the activity currently at the bottom has the
2210 // same task affinity as the one we are moving,
2211 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002212 targetTask = bottom.task;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002213 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002214 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002215 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002216 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002217 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002218 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002219 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2220 + " out to new task " + target.task);
2221 }
2222
Craig Mautnere3a74d52013-02-22 14:14:58 -08002223 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002224 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002225
Craig Mautner525f3d92013-05-07 14:01:50 -07002226 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002227 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2228 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002229 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002230 if (p.finishing) {
2231 continue;
2232 }
2233
Craig Mautnere3a74d52013-02-22 14:14:58 -08002234 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002235 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002236 topOptions = p.takeOptionsLocked();
2237 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002238 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002239 }
2240 }
2241 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002242 + task + " adding to task=" + targetTask
2243 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002244 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2245 + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002246 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002247 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002248
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002249 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002250 }
2251
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002252 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002253 if (VALIDATE_TOKENS) {
2254 validateAppTokensLocked();
2255 }
2256
2257 replyChainEnd = -1;
2258 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2259 // If the activity should just be removed -- either
2260 // because it asks for it, or the task should be
2261 // cleared -- then finish it and anything that is
2262 // part of its reply chain.
2263 int end;
2264 if (clearWhenTaskReset) {
2265 // In this case, we want to finish this activity
2266 // and everything above it, so be sneaky and pretend
2267 // like these are all in the reply chain.
2268 end = numActivities - 1;
2269 } else if (replyChainEnd < 0) {
2270 end = i;
2271 } else {
2272 end = replyChainEnd;
2273 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002274 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002275 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002276 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002277 if (p.finishing) {
2278 continue;
2279 }
2280 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002281 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002282 topOptions = p.takeOptionsLocked();
2283 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002284 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002285 }
2286 }
Craig Mautner58547802013-03-05 08:23:53 -08002287 if (DEBUG_TASKS) Slog.w(TAG,
2288 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002289 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002290 end--;
2291 srcPos--;
2292 }
2293 }
2294 replyChainEnd = -1;
2295 } else {
2296 // If we were in the middle of a chain, well the
2297 // activity that started it all doesn't want anything
2298 // special, so leave it all as-is.
2299 replyChainEnd = -1;
2300 }
2301 }
2302
2303 return topOptions;
2304 }
2305
2306 /**
2307 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2308 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2309 * @param affinityTask The task we are looking for an affinity to.
2310 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2311 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2312 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2313 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002314 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002315 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002316 int replyChainEnd = -1;
2317 final int taskId = task.taskId;
2318 final String taskAffinity = task.affinity;
2319
2320 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2321 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002322 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2323
2324 // Do not operate on or below the effective root Activity.
2325 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002326 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002327 if (target.frontOfTask)
2328 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002329
2330 final int flags = target.info.flags;
2331 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2332 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2333
2334 if (target.resultTo != null) {
2335 // If this activity is sending a reply to a previous
2336 // activity, we can't do anything with it now until
2337 // we reach the start of the reply chain.
2338 // XXX note that we are assuming the result is always
2339 // to the previous activity, which is almost always
2340 // the case but we really shouldn't count on.
2341 if (replyChainEnd < 0) {
2342 replyChainEnd = i;
2343 }
2344 } else if (topTaskIsHigher
2345 && allowTaskReparenting
2346 && taskAffinity != null
2347 && taskAffinity.equals(target.taskAffinity)) {
2348 // This activity has an affinity for our task. Either remove it if we are
2349 // clearing or move it over to our task. Note that
2350 // we currently punt on the case where we are resetting a
2351 // task that is not at the top but who has activities above
2352 // with an affinity to it... this is really not a normal
2353 // case, and we will need to later pull that task to the front
2354 // and usually at that point we will do the reset and pick
2355 // up those remaining activities. (This only happens if
2356 // someone starts an activity in a new task from an activity
2357 // in a task that is not currently on top.)
2358 if (forceReset || finishOnTaskLaunch) {
2359 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2360 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2361 for (int srcPos = start; srcPos >= i; --srcPos) {
2362 final ActivityRecord p = activities.get(srcPos);
2363 if (p.finishing) {
2364 continue;
2365 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002366 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002367 }
2368 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002369 if (taskInsertionPoint < 0) {
2370 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002371
Craig Mautner77878772013-03-04 19:46:24 -08002372 }
Craig Mautner77878772013-03-04 19:46:24 -08002373
2374 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2375 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2376 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2377 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002378 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002379 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002380 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002381
Craig Mautnere3a74d52013-02-22 14:14:58 -08002382 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2383 + " to stack at " + task,
2384 new RuntimeException("here").fillInStackTrace());
2385 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2386 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002387 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002388 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002389 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002390 if (VALIDATE_TOKENS) {
2391 validateAppTokensLocked();
2392 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002393
2394 // Now we've moved it in to place... but what if this is
2395 // a singleTop activity and we have put it on top of another
2396 // instance of the same activity? Then we drop the instance
2397 // below so it remains singleTop.
2398 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2399 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002400 int targetNdx = taskActivities.indexOf(target);
2401 if (targetNdx > 0) {
2402 ActivityRecord p = taskActivities.get(targetNdx - 1);
2403 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002404 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2405 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002406 }
2407 }
2408 }
2409 }
2410
2411 replyChainEnd = -1;
2412 }
2413 }
Craig Mautner77878772013-03-04 19:46:24 -08002414 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002415 }
2416
Craig Mautner8849a5e2013-04-02 16:41:03 -07002417 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002418 ActivityRecord newActivity) {
2419 boolean forceReset =
2420 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2421 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2422 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2423 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2424 forceReset = true;
2425 }
2426 }
2427
2428 final TaskRecord task = taskTop.task;
2429
2430 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2431 * for remaining tasks. Used for later tasks to reparent to task. */
2432 boolean taskFound = false;
2433
2434 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2435 ActivityOptions topOptions = null;
2436
Craig Mautner77878772013-03-04 19:46:24 -08002437 // Preserve the location for reparenting in the new task.
2438 int reparentInsertionPoint = -1;
2439
Craig Mautnere3a74d52013-02-22 14:14:58 -08002440 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2441 final TaskRecord targetTask = mTaskHistory.get(i);
2442
2443 if (targetTask == task) {
2444 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2445 taskFound = true;
2446 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002447 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2448 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002449 }
2450 }
2451
Craig Mautner70a86932013-02-28 22:37:44 -08002452 int taskNdx = mTaskHistory.indexOf(task);
2453 do {
2454 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2455 } while (taskTop == null && taskNdx >= 0);
2456
Craig Mautnere3a74d52013-02-22 14:14:58 -08002457 if (topOptions != null) {
2458 // If we got some ActivityOptions from an activity on top that
2459 // was removed from the task, propagate them to the new real top.
2460 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002461 taskTop.updateOptionsLocked(topOptions);
2462 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002463 topOptions.abort();
2464 }
2465 }
2466
2467 return taskTop;
2468 }
2469
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002470 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2471 String resultWho, int requestCode, int resultCode, Intent data) {
2472
2473 if (callingUid > 0) {
2474 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002475 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002476 }
2477
2478 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2479 + " : who=" + resultWho + " req=" + requestCode
2480 + " res=" + resultCode + " data=" + data);
2481 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2482 try {
2483 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2484 list.add(new ResultInfo(resultWho, requestCode,
2485 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002486 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002487 return;
2488 } catch (Exception e) {
2489 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2490 }
2491 }
2492
2493 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2494 }
2495
Craig Mautner04f0b702013-10-22 12:31:01 -07002496 private void adjustFocusedActivityLocked(ActivityRecord r) {
2497 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2498 ActivityRecord next = topRunningActivityLocked(null);
2499 if (next != r) {
2500 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002501 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2502 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner04f0b702013-10-22 12:31:01 -07002503 }
2504 }
Winson Chung648c83b2014-04-28 15:11:56 -07002505 ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2506 if (top != null) {
2507 mService.setFocusedActivityLocked(top);
2508 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002509 }
2510 }
2511
Craig Mautnerf3333272013-04-22 10:55:53 -07002512 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002513 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2514 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2515 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2516 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002517 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002518 if (DEBUG_STATES) {
2519 Slog.d(TAG, "no-history finish of " + r);
2520 }
2521 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002522 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002523 } else {
2524 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2525 + " on stop because we're just sleeping");
2526 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002527 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002528 }
2529
2530 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002531 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002532 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002533 try {
2534 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002535 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2536 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002537 r.state = ActivityState.STOPPING;
2538 if (DEBUG_VISBILITY) Slog.v(
2539 TAG, "Stopping visible=" + r.visible + " for " + r);
2540 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002541 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002542 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002543 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002544 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002545 r.setSleeping(true);
2546 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002547 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002548 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002549 } catch (Exception e) {
2550 // Maybe just ignore exceptions here... if the process
2551 // has crashed, our death notification will clean things
2552 // up.
2553 Slog.w(TAG, "Exception thrown during pause", e);
2554 // Just in case, assume it to be stopped.
2555 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002556 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002557 r.state = ActivityState.STOPPED;
2558 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002559 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002560 }
2561 }
2562 }
2563 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002564
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002565 /**
2566 * @return Returns true if the activity is being finished, false if for
2567 * some reason it is being left as-is.
2568 */
2569 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002570 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002571 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002572 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002573 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002574 + ", result=" + resultCode + ", data=" + resultData
2575 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002576 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002577 return false;
2578 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002579
Craig Mautnerd44711d2013-02-23 11:24:36 -08002580 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002581 return true;
2582 }
2583
Craig Mautnerd2328952013-03-05 12:46:26 -08002584 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002585 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2586 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2587 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2588 ActivityRecord r = activities.get(activityNdx);
2589 if (r.resultTo == self && r.requestCode == requestCode) {
2590 if ((r.resultWho == null && resultWho == null) ||
2591 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2592 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2593 false);
2594 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002595 }
2596 }
2597 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002598 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002599 }
2600
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002601 final void finishTopRunningActivityLocked(ProcessRecord app) {
2602 ActivityRecord r = topRunningActivityLocked(null);
2603 if (r != null && r.app == app) {
2604 // If the top running activity is from this crashing
2605 // process, then terminate it to avoid getting in a loop.
2606 Slog.w(TAG, " Force finishing activity "
2607 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002608 int taskNdx = mTaskHistory.indexOf(r.task);
2609 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002610 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002611 // Also terminate any activities below it that aren't yet
2612 // stopped, to avoid a situation where one will get
2613 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002614 --activityNdx;
2615 if (activityNdx < 0) {
2616 do {
2617 --taskNdx;
2618 if (taskNdx < 0) {
2619 break;
2620 }
2621 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2622 } while (activityNdx < 0);
2623 }
2624 if (activityNdx >= 0) {
2625 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002626 if (r.state == ActivityState.RESUMED
2627 || r.state == ActivityState.PAUSING
2628 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002629 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002630 Slog.w(TAG, " Force finishing activity "
2631 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002632 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002633 }
2634 }
2635 }
2636 }
2637 }
2638
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002639 final void finishVoiceTask(IVoiceInteractionSession session) {
2640 IBinder sessionBinder = session.asBinder();
2641 boolean didOne = false;
2642 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2643 TaskRecord tr = mTaskHistory.get(taskNdx);
2644 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2645 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2646 ActivityRecord r = tr.mActivities.get(activityNdx);
2647 if (!r.finishing) {
2648 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2649 false);
2650 didOne = true;
2651 }
2652 }
2653 }
2654 }
2655 if (didOne) {
2656 mService.updateOomAdjLocked();
2657 }
2658 }
2659
Craig Mautnerd2328952013-03-05 12:46:26 -08002660 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002661 ArrayList<ActivityRecord> activities = r.task.mActivities;
2662 for (int index = activities.indexOf(r); index >= 0; --index) {
2663 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002664 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002665 break;
2666 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002667 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002668 }
2669 return true;
2670 }
2671
Dianne Hackborn5c607432012-02-28 14:44:19 -08002672 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2673 // send the result
2674 ActivityRecord resultTo = r.resultTo;
2675 if (resultTo != null) {
2676 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2677 + " who=" + r.resultWho + " req=" + r.requestCode
2678 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002679 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002680 if (resultData != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002681 resultData.setContentUserHint(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002682 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002683 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002684 if (r.info.applicationInfo.uid > 0) {
2685 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2686 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002687 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002688 }
2689 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2690 resultData);
2691 r.resultTo = null;
2692 }
2693 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2694
2695 // Make sure this HistoryRecord is not holding on to other resources,
2696 // because clients have remote IPC references to this object so we
2697 // can't assume that will go away and want to avoid circular IPC refs.
2698 r.results = null;
2699 r.pendingResults = null;
2700 r.newIntents = null;
2701 r.icicle = null;
2702 }
2703
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002704 /**
2705 * @return Returns true if this activity has been removed from the history
2706 * list, or false if it is still in the list and will be removed later.
2707 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002708 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2709 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002710 if (r.finishing) {
2711 Slog.w(TAG, "Duplicate finish request for " + r);
2712 return false;
2713 }
2714
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002715 r.makeFinishing();
Craig Mautneraea74a52014-03-08 14:23:10 -08002716 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002717 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002718 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002719 task.taskId, r.shortComponentName, reason);
2720 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002721 final int index = activities.indexOf(r);
2722 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002723 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002724 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002725 // If the caller asked that this activity (and all above it)
2726 // be cleared when the task is reset, don't lose that information,
2727 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002728 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002729 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002730 }
2731 }
2732
2733 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002734
2735 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002736
Dianne Hackborn5c607432012-02-28 14:44:19 -08002737 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002738
Craig Mautnerde4ef022013-04-07 19:01:33 -07002739 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002740 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002741 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002742 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002743 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002744 ? AppTransition.TRANSIT_TASK_CLOSE
2745 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002746
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002747 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002748 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002749
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002750 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002751 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2752 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002753 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002754 }
2755
Craig Mautneraea74a52014-03-08 14:23:10 -08002756 if (endTask) {
2757 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2758 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002759 } else if (r.state != ActivityState.PAUSING) {
2760 // If the activity is PAUSING, we will complete the finish once
2761 // it is done pausing; else we can just directly finish it here.
2762 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002763 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002764 } else {
2765 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2766 }
2767
2768 return false;
2769 }
2770
Craig Mautnerf3333272013-04-22 10:55:53 -07002771 static final int FINISH_IMMEDIATELY = 0;
2772 static final int FINISH_AFTER_PAUSE = 1;
2773 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002774
Craig Mautnerf3333272013-04-22 10:55:53 -07002775 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002776 // First things first: if this activity is currently visible,
2777 // and the resumed activity is not yet visible, then hold off on
2778 // finishing until the resumed one becomes visible.
2779 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002780 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2781 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002782 if (mStackSupervisor.mStoppingActivities.size() > 3
2783 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002784 // If we already have a few activities waiting to stop,
2785 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002786 // them out. Or if r is the last of activity of the last task the stack
2787 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002788 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002789 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002790 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002791 }
2792 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002793 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2794 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002795 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002796 if (oomAdj) {
2797 mService.updateOomAdjLocked();
2798 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002799 return r;
2800 }
2801
2802 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002803 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002804 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002805 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002806 if (mResumedActivity == r) {
2807 mResumedActivity = null;
2808 }
2809 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002810 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002811 r.state = ActivityState.FINISHING;
2812
2813 if (mode == FINISH_IMMEDIATELY
2814 || prevState == ActivityState.STOPPED
2815 || prevState == ActivityState.INITIALIZING) {
2816 // If this activity is already stopped, we can just finish
2817 // it right now.
Craig Mautnerd163e752014-06-13 17:18:47 -07002818 r.makeFinishing();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002819 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002820 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002821 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002822 }
Craig Mautnerd163e752014-06-13 17:18:47 -07002823 if (DEBUG_CONTAINERS) Slog.d(TAG,
2824 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2825 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002826 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002827 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002828
2829 // Need to go through the full pause cycle to get this
2830 // activity into the stopped state and then finish it.
2831 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002832 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002833 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002834 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002835 return r;
2836 }
2837
Craig Mautneree36c772014-07-16 14:56:05 -07002838 void finishAllActivitiesLocked(boolean immediately) {
2839 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002840 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2841 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2842 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2843 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07002844 noActivitiesInStack = false;
2845 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002846 continue;
2847 }
Craig Mautneree36c772014-07-16 14:56:05 -07002848 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002849 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2850 }
2851 }
Craig Mautneree36c772014-07-16 14:56:05 -07002852 if (noActivitiesInStack) {
2853 mActivityContainer.onTaskListEmptyLocked();
2854 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002855 }
2856
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002857 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
2858 // Basic case: for simple app-centric recents, we need to recreate
2859 // the task if the affinity has changed.
2860 if (srec == null || srec.task.affinity == null ||
2861 !srec.task.affinity.equals(destAffinity)) {
2862 return true;
2863 }
2864 // Document-centric case: an app may be split in to multiple documents;
2865 // they need to re-create their task if this current activity is the root
2866 // of a document, unless simply finishing it will return them to the the
2867 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07002868 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
2869 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002870 // Okay, this activity is at the root of its task. What to do, what to do...
2871 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
2872 // Finishing won't return to an application, so we need to recreate.
2873 return true;
2874 }
2875 // We now need to get the task below it to determine what to do.
2876 int taskIdx = mTaskHistory.indexOf(srec.task);
2877 if (taskIdx <= 0) {
2878 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
2879 return false;
2880 }
2881 if (taskIdx == 0) {
2882 // At the bottom of the stack, nothing to go back to.
2883 return true;
2884 }
2885 TaskRecord prevTask = mTaskHistory.get(taskIdx);
2886 if (!srec.task.affinity.equals(prevTask.affinity)) {
2887 // These are different apps, so need to recreate.
2888 return true;
2889 }
2890 }
2891 return false;
2892 }
2893
Craig Mautnerd2328952013-03-05 12:46:26 -08002894 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002895 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002896 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002897 final TaskRecord task = srec.task;
2898 final ArrayList<ActivityRecord> activities = task.mActivities;
2899 final int start = activities.indexOf(srec);
2900 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002901 return false;
2902 }
2903 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002904 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002905 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002906 final ComponentName dest = destIntent.getComponent();
2907 if (start > 0 && dest != null) {
2908 for (int i = finishTo; i >= 0; i--) {
2909 ActivityRecord r = activities.get(i);
2910 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002911 r.info.name.equals(dest.getClassName())) {
2912 finishTo = i;
2913 parent = r;
2914 foundParentInTask = true;
2915 break;
2916 }
2917 }
2918 }
2919
2920 IActivityController controller = mService.mController;
2921 if (controller != null) {
2922 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2923 if (next != null) {
2924 // ask watcher if this is allowed
2925 boolean resumeOK = true;
2926 try {
2927 resumeOK = controller.activityResuming(next.packageName);
2928 } catch (RemoteException e) {
2929 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002930 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002931 }
2932
2933 if (!resumeOK) {
2934 return false;
2935 }
2936 }
2937 }
2938 final long origId = Binder.clearCallingIdentity();
2939 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002940 ActivityRecord r = activities.get(i);
2941 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002942 // Only return the supplied result for the first activity finished
2943 resultCode = Activity.RESULT_CANCELED;
2944 resultData = null;
2945 }
2946
2947 if (parent != null && foundParentInTask) {
2948 final int parentLaunchMode = parent.info.launchMode;
2949 final int destIntentFlags = destIntent.getFlags();
2950 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2951 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2952 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2953 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002954 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
2955 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002956 } else {
2957 try {
2958 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2959 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002960 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002961 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002962 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborn95465202014-09-15 16:21:55 -07002963 -1, parent.launchedFromUid, 0, null, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002964 foundParentInTask = res == ActivityManager.START_SUCCESS;
2965 } catch (RemoteException e) {
2966 foundParentInTask = false;
2967 }
2968 requestFinishActivityLocked(parent.appToken, resultCode,
2969 resultData, "navigate-up", true);
2970 }
2971 }
2972 Binder.restoreCallingIdentity(origId);
2973 return foundParentInTask;
2974 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002975 /**
2976 * Perform the common clean-up of an activity record. This is called both
2977 * as part of destroyActivityLocked() (when destroying the client-side
2978 * representation) and cleaning things up as a result of its hosting
2979 * processing going away, in which case there is no remaining client-side
2980 * state to destroy so only the cleanup here is needed.
2981 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002982 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2983 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002984 if (mResumedActivity == r) {
2985 mResumedActivity = null;
2986 }
Craig Mautner1872ce32014-03-28 23:05:42 +00002987 if (mPausingActivity == r) {
2988 mPausingActivity = null;
2989 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002990 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002991
2992 r.configDestroy = false;
2993 r.frozenBeforeDestroy = false;
2994
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002995 if (setState) {
2996 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2997 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002998 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002999 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003000 }
3001
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003002 // Make sure this record is no longer in the pending finishes list.
3003 // This could happen, for example, if we are trimming activities
3004 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003005 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003006 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003007
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003008 // Remove any pending results.
3009 if (r.finishing && r.pendingResults != null) {
3010 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3011 PendingIntentRecord rec = apr.get();
3012 if (rec != null) {
3013 mService.cancelIntentSenderLocked(rec, false);
3014 }
3015 }
3016 r.pendingResults = null;
3017 }
3018
3019 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003020 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003021 }
3022
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003023 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003024 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003025 if (getVisibleBehindActivity() == r) {
3026 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003027 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003028 }
3029
3030 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003031 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003032 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003033 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003034 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003035 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003036 }
3037
Craig Mautner04a0ea62014-01-13 12:51:26 -08003038 private void removeActivityFromHistoryLocked(ActivityRecord r) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003039 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003040 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
3041 r.makeFinishing();
3042 if (DEBUG_ADD_REMOVE) {
3043 RuntimeException here = new RuntimeException("here");
3044 here.fillInStackTrace();
3045 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003046 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003047 r.takeFromHistory();
3048 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003049 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003050 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003051 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003052 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003053 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003054 if (VALIDATE_TOKENS) {
3055 validateAppTokensLocked();
3056 }
Craig Mautner312ba862014-02-10 17:55:01 -08003057 final TaskRecord task = r.task;
3058 if (task != null && task.removeActivity(r)) {
3059 if (DEBUG_STACK) Slog.i(TAG,
3060 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003061 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3062 task.isOverHomeStack()) {
3063 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner312ba862014-02-10 17:55:01 -08003064 }
Craig Mautner41db4a72014-05-07 17:20:56 -07003065 removeTask(task);
Craig Mautner312ba862014-02-10 17:55:01 -08003066 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003067 cleanUpActivityServicesLocked(r);
3068 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003069 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003070
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003071 /**
3072 * Perform clean-up of service connections in an activity record.
3073 */
3074 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3075 // Throw away any services that have been bound by this activity.
3076 if (r.connections != null) {
3077 Iterator<ConnectionRecord> it = r.connections.iterator();
3078 while (it.hasNext()) {
3079 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003080 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003081 }
3082 r.connections = null;
3083 }
3084 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003085
Craig Mautneree2e45a2014-06-27 12:10:03 -07003086 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003087 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003088 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003089 mHandler.sendMessage(msg);
3090 }
3091
Craig Mautneree2e45a2014-06-27 12:10:03 -07003092 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003093 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003094 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003095 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3096 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3097 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3098 final ActivityRecord r = activities.get(activityNdx);
3099 if (r.finishing) {
3100 continue;
3101 }
3102 if (r.fullscreen) {
3103 lastIsOpaque = true;
3104 }
3105 if (owner != null && r.app != owner) {
3106 continue;
3107 }
3108 if (!lastIsOpaque) {
3109 continue;
3110 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003111 if (r.isDestroyable()) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003112 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3113 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003114 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003115 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003116 activityRemoved = true;
3117 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003118 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003119 }
3120 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003121 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003122 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003123 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003124 }
3125
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003126 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3127 if (r.isDestroyable()) {
3128 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3129 + " resumed=" + mResumedActivity
3130 + " pausing=" + mPausingActivity + " for reason " + reason);
3131 return destroyActivityLocked(r, true, reason);
3132 }
3133 return false;
3134 }
3135
3136 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3137 String reason) {
3138 // Iterate over tasks starting at the back (oldest) first.
3139 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3140 int maxTasks = tasks.size() / 4;
3141 if (maxTasks < 1) {
3142 maxTasks = 1;
3143 }
3144 int numReleased = 0;
3145 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3146 final TaskRecord task = mTaskHistory.get(taskNdx);
3147 if (!tasks.contains(task)) {
3148 continue;
3149 }
3150 if (DEBUG_RELEASE) Slog.d(TAG, "Looking for activities to release in " + task);
3151 int curNum = 0;
3152 final ArrayList<ActivityRecord> activities = task.mActivities;
3153 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3154 final ActivityRecord activity = activities.get(actNdx);
3155 if (activity.app == app && activity.isDestroyable()) {
3156 if (DEBUG_RELEASE) Slog.v(TAG, "Destroying " + activity
3157 + " in state " + activity.state + " resumed=" + mResumedActivity
3158 + " pausing=" + mPausingActivity + " for reason " + reason);
3159 destroyActivityLocked(activity, true, reason);
3160 if (activities.get(actNdx) != activity) {
3161 // Was removed from list, back up so we don't miss the next one.
3162 actNdx--;
3163 }
3164 curNum++;
3165 }
3166 }
3167 if (curNum > 0) {
3168 numReleased += curNum;
3169 maxTasks--;
3170 if (mTaskHistory.get(taskNdx) != task) {
3171 // The entire task got removed, back up so we don't miss the next one.
3172 taskNdx--;
3173 }
3174 }
3175 }
3176 if (DEBUG_RELEASE) Slog.d(TAG, "Done releasing: did " + numReleased + " activities");
3177 return numReleased;
3178 }
3179
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003180 /**
3181 * Destroy the current CLIENT SIDE instance of an activity. This may be
3182 * called both when actually finishing an activity, or when performing
3183 * a configuration switch where we destroy the current client-side object
3184 * but then create a new client-side object for this same HistoryRecord.
3185 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003186 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003187 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003188 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003189 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3190 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003191 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003192 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003193
3194 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003195
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003196 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003197
3198 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003199
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003200 if (hadApp) {
3201 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003202 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003203 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3204 mService.mHeavyWeightProcess = null;
3205 mService.mHandler.sendEmptyMessage(
3206 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3207 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003208 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003209 // Update any services we are bound to that might care about whether
3210 // their client may have activities.
3211 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003212 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003213 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003214 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003215 }
3216 }
3217
3218 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003219
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003220 try {
3221 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003222 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003223 r.configChangeFlags);
3224 } catch (Exception e) {
3225 // We can just ignore exceptions here... if the process
3226 // has crashed, our death notification will clean things
3227 // up.
3228 //Slog.w(TAG, "Exception thrown during finish", e);
3229 if (r.finishing) {
3230 removeActivityFromHistoryLocked(r);
3231 removedFromHistory = true;
3232 skipDestroy = true;
3233 }
3234 }
3235
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003236 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003237
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003238 // If the activity is finishing, we need to wait on removing it
3239 // from the list to give it a chance to do its cleanup. During
3240 // that time it may make calls back with its token so we need to
3241 // be able to find it on the list and so we don't want to remove
3242 // it from the list yet. Otherwise, we can just immediately put
3243 // it in the destroyed state since we are not removing it from the
3244 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003245 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003246 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3247 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003248 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003249 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003250 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3251 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003252 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003253 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003254 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003255 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003256 }
3257 } else {
3258 // remove this record from the history.
3259 if (r.finishing) {
3260 removeActivityFromHistoryLocked(r);
3261 removedFromHistory = true;
3262 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003263 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003264 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003265 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003266 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003267 }
3268 }
3269
3270 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003271
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003272 if (!mLRUActivities.remove(r) && hadApp) {
3273 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3274 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003275
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003276 return removedFromHistory;
3277 }
3278
Craig Mautnerd2328952013-03-05 12:46:26 -08003279 final void activityDestroyedLocked(IBinder token) {
3280 final long origId = Binder.clearCallingIdentity();
3281 try {
3282 ActivityRecord r = ActivityRecord.forToken(token);
3283 if (r != null) {
3284 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003285 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003286 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003287
3288 if (isInStackLocked(token) != null) {
3289 if (r.state == ActivityState.DESTROYING) {
3290 cleanUpActivityLocked(r, true, false);
3291 removeActivityFromHistoryLocked(r);
3292 }
3293 }
Craig Mautner05d29032013-05-03 13:40:13 -07003294 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003295 } finally {
3296 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003297 }
3298 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003299
Jose Lima4b6c6692014-08-12 17:41:12 -07003300 void releaseBackgroundResources() {
3301 if (hasVisibleBehindActivity() &&
3302 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
3303 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautner64ccb702014-10-01 09:38:40 -07003304 if (r == topRunningActivityLocked(null)) {
3305 // Don't release the top activity if it has requested to run behind the next
3306 // activity.
3307 return;
3308 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003309 if (DEBUG_STATES) Slog.d(TAG, "releaseBackgroundResources activtyDisplay=" +
3310 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003311 " thread=" + r.app.thread);
3312 if (r != null && r.app != null && r.app.thread != null) {
3313 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003314 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003315 } catch (RemoteException e) {
3316 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003317 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003318 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003319 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003320 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003321 }
3322 }
3323 }
3324
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003325 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003326 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3327 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003328 if (r != null) {
3329 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003330 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003331 mStackSupervisor.scheduleIdleTimeoutLocked(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;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003776 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003777 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}