blob: 92235037883aaf7625475ff1bf9614fc9f43e6f9 [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.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080069import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070070import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070071import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080072import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070073import android.os.Handler;
74import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090075import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070077import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078import android.os.RemoteException;
79import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080080import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070081import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070082import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070083import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070084import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070085import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070086
Craig Mautnercae015f2013-02-08 14:31:27 -080087import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080088import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070089import java.lang.ref.WeakReference;
90import java.util.ArrayList;
91import java.util.Iterator;
92import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080093import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070094
95/**
96 * State and management of a single stack of activities.
97 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070098final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080099
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700100 // Ticks during which we check progress while waiting for an app to launch.
101 static final int LAUNCH_TICK = 500;
102
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700103 // How long we wait until giving up on the last activity to pause. This
104 // is short because it directly impacts the responsiveness of starting the
105 // next activity.
106 static final int PAUSE_TIMEOUT = 500;
107
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700108 // How long we wait for the activity to tell us it has stopped before
109 // giving up. This is a good amount of time because we really need this
110 // from the application in order to get its saved state.
111 static final int STOP_TIMEOUT = 10*1000;
112
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700113 // How long we wait until giving up on an activity telling us it has
114 // finished destroying itself.
115 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800116
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700117 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800118 // disabled.
119 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800120
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700121 // How long between activity launches that we consider safe to not warn
122 // the user about an unexpected activity being launched on top.
123 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800124
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700125 // Set to false to disable the preview that is shown while a new activity
126 // is being started.
127 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800128
Craig Mautner5eda9b32013-07-02 11:58:16 -0700129 // How long to wait for all background Activities to redraw following a call to
130 // convertToTranslucent().
131 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
132
Craig Mautnera61bc652013-10-28 15:43:18 -0700133 static final boolean SCREENSHOT_FORCE_565 = ActivityManager.isLowRamDeviceStatic();
John Reck172e87c2013-10-02 16:55:16 -0700134
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700135 enum ActivityState {
136 INITIALIZING,
137 RESUMED,
138 PAUSING,
139 PAUSED,
140 STOPPING,
141 STOPPED,
142 FINISHING,
143 DESTROYING,
144 DESTROYED
145 }
146
147 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700148 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800149 private final RecentTasks mRecentTasks;
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
Craig Mautner858d8a62013-04-23 17:08:34 -0700226 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700227
Craig Mautnerc00204b2013-03-05 15:02:14 -0800228 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800229 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800230 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
231 ArrayList<ActivityStack> mStacks;
232 /** The attached Display's unique identifier, or -1 if detached */
233 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800234
Craig Mautner27084302013-03-25 08:05:25 -0700235 /** Run all ActivityStacks through this */
236 final ActivityStackSupervisor mStackSupervisor;
237
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700238 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700239 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
240 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
241 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
242 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700243 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700244 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700245 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700246
247 static class ScheduleDestroyArgs {
248 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700249 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700250 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700251 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700252 mReason = reason;
253 }
254 }
255
Zoran Marcetaf958b322012-08-09 20:27:12 +0900256 final Handler mHandler;
257
258 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700259 //public Handler() {
260 // if (localLOGV) Slog.v(TAG, "Handler started!");
261 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700262 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900263 super(looper);
264 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700265
Zoran Marcetaf958b322012-08-09 20:27:12 +0900266 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700267 public void handleMessage(Message msg) {
268 switch (msg.what) {
269 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800270 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700271 // We don't at this point know if the activity is fullscreen,
272 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800273 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700274 synchronized (mService) {
275 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700276 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700277 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700278 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800279 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700280 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700281 case LAUNCH_TICK_MSG: {
282 ActivityRecord r = (ActivityRecord)msg.obj;
283 synchronized (mService) {
284 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700285 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700286 }
287 }
288 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700289 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800290 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700291 // We don't at this point know if the activity is fullscreen,
292 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800293 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800294 synchronized (mService) {
295 activityDestroyedLocked(r != null ? r.appToken : null);
296 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700297 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700298 case STOP_TIMEOUT_MSG: {
299 ActivityRecord r = (ActivityRecord)msg.obj;
300 // We don't at this point know if the activity is fullscreen,
301 // so we need to be conservative and assume it isn't.
302 Slog.w(TAG, "Activity stop timeout for " + r);
303 synchronized (mService) {
304 if (r.isInHistory()) {
305 activityStoppedLocked(r, null, null, null);
306 }
307 }
308 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700309 case DESTROY_ACTIVITIES_MSG: {
310 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
311 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700312 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700313 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700314 } break;
315 case TRANSLUCENT_TIMEOUT_MSG: {
316 synchronized (mService) {
317 notifyActivityDrawnLocked(null);
318 }
319 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700320 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700321 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700322 final ActivityRecord r = getVisibleBehindActivity();
323 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700324 if (r != null) {
325 mService.killAppAtUsersRequest(r.app, null);
326 }
327 }
328 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700329 }
330 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800331 }
332
Craig Mautner34b73df2014-01-12 21:11:08 -0800333 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800334 int count = 0;
335 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
336 count += mTaskHistory.get(taskNdx).mActivities.size();
337 }
338 return count;
339 }
340
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800341 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
342 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800343 mActivityContainer = activityContainer;
344 mStackSupervisor = activityContainer.getOuter();
345 mService = mStackSupervisor.mService;
346 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
347 mWindowManager = mService.mWindowManager;
348 mStackId = activityContainer.mStackId;
349 mCurrentUser = mService.mCurrentUserId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800350 mRecentTasks = recentTasks;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700351 }
Craig Mautner5962b122012-10-05 14:45:52 -0700352
Amith Yamasani734983f2014-03-04 16:48:05 -0800353 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100354 * Checks whether the userid is a profile of the current user.
Amith Yamasani734983f2014-03-04 16:48:05 -0800355 */
Kenny Guy2a764942014-04-02 13:29:20 +0100356 private boolean isCurrentProfileLocked(int userId) {
Kenny Guyf4824a02014-04-02 19:17:41 +0100357 if (userId == mCurrentUser) return true;
Kenny Guy2a764942014-04-02 13:29:20 +0100358 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
359 if (mService.mCurrentProfileIds[i] == userId) return true;
Amith Yamasani734983f2014-03-04 16:48:05 -0800360 }
361 return false;
362 }
363
364 boolean okToShowLocked(ActivityRecord r) {
Kenny Guy2a764942014-04-02 13:29:20 +0100365 return isCurrentProfileLocked(r.userId)
Craig Mautner5962b122012-10-05 14:45:52 -0700366 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
367 }
368
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700369 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800370 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700371 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
372 if (r != null) {
373 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800374 }
375 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700376 return null;
377 }
378
379 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800380 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
381 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800382 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800383 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
384 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800385 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800386 return r;
387 }
388 }
389 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700390 return null;
391 }
392
393 /**
394 * This is a simplified version of topRunningActivityLocked that provides a number of
395 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800396 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700397 * @param token If non-null, any history records matching this token will be skipped.
398 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800399 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700400 * @return Returns the HistoryRecord of the next activity on the stack.
401 */
402 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800403 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
404 TaskRecord task = mTaskHistory.get(taskNdx);
405 if (task.taskId == taskId) {
406 continue;
407 }
408 ArrayList<ActivityRecord> activities = task.mActivities;
409 for (int i = activities.size() - 1; i >= 0; --i) {
410 final ActivityRecord r = activities.get(i);
411 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800412 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800413 return r;
414 }
415 }
416 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700417 return null;
418 }
419
Craig Mautner8849a5e2013-04-02 16:41:03 -0700420 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700421 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
422 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700423 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
424 final ActivityRecord r = activities.get(activityNdx);
425 if (!r.finishing) {
426 return r;
427 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700428 }
429 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700430 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700431 }
432
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700433 final TaskRecord topTask() {
434 final int size = mTaskHistory.size();
435 if (size > 0) {
436 return mTaskHistory.get(size - 1);
437 }
438 return null;
439 }
440
Craig Mautnerd2328952013-03-05 12:46:26 -0800441 TaskRecord taskForIdLocked(int id) {
442 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
443 final TaskRecord task = mTaskHistory.get(taskNdx);
444 if (task.taskId == id) {
445 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800446 }
447 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700448 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700449 }
450
Craig Mautnerd2328952013-03-05 12:46:26 -0800451 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800452 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800453 if (r != null) {
454 final TaskRecord task = r.task;
Dianne Hackborn925e2b32014-09-03 16:40:49 -0700455 if (task != null && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Craig Mautnerd2328952013-03-05 12:46:26 -0800456 if (task.stack != this) Slog.w(TAG,
457 "Illegal state! task does not point to stack it is in.");
458 return r;
459 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800460 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800461 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800462 }
463
Craig Mautner2420ead2013-04-01 17:13:20 -0700464 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700465 final boolean hadit = mLRUActivities.remove(r);
466 mLRUActivities.add(r);
467 return hadit;
468 }
469
Craig Mautnerde4ef022013-04-07 19:01:33 -0700470 final boolean isHomeStack() {
471 return mStackId == HOME_STACK_ID;
472 }
473
Craig Mautnere0a38842013-12-16 16:14:02 -0800474 final boolean isOnHomeDisplay() {
475 return isAttached() &&
476 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
477 }
478
479 final void moveToFront() {
480 if (isAttached()) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800481 if (isOnHomeDisplay()) {
482 mStackSupervisor.moveHomeStack(isHomeStack());
483 }
Craig Mautnerfefeef82014-01-10 08:40:23 -0800484 mStacks.remove(this);
485 mStacks.add(this);
Craig Mautner6b904ef2014-12-17 15:45:03 -0800486 final TaskRecord task = topTask();
487 if (task != null) {
488 mWindowManager.moveTaskToTop(task.taskId);
489 }
Craig Mautner4a1cb222013-12-04 16:14:06 -0800490 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800491 }
492
493 final boolean isAttached() {
494 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800495 }
496
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700497 /**
498 * Returns the top activity in any existing task matching the given
499 * Intent. Returns null if no such task is found.
500 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700501 ActivityRecord findTaskLocked(ActivityRecord target) {
502 Intent intent = target.intent;
503 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700504 ComponentName cls = intent.getComponent();
505 if (info.targetActivity != null) {
506 cls = new ComponentName(info.packageName, info.targetActivity);
507 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700508 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700509 boolean isDocument = intent != null & intent.isDocument();
510 // If documentData is non-null then it must match the existing task data.
511 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800512
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700513 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800514 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
515 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700516 if (task.voiceSession != null) {
517 // We never match voice sessions; those always run independently.
518 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": voice session");
519 continue;
520 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700521 if (task.userId != userId) {
522 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700523 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700524 continue;
525 }
Craig Mautner000f0022013-02-26 15:04:29 -0800526 final ActivityRecord r = task.getTopActivity();
527 if (r == null || r.finishing || r.userId != userId ||
528 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700529 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800530 continue;
531 }
532
Craig Mautnerd00f4742014-03-12 14:17:26 -0700533 final Intent taskIntent = task.intent;
534 final Intent affinityIntent = task.affinityIntent;
535 final boolean taskIsDocument;
536 final Uri taskDocumentData;
537 if (taskIntent != null && taskIntent.isDocument()) {
538 taskIsDocument = true;
539 taskDocumentData = taskIntent.getData();
540 } else if (affinityIntent != null && affinityIntent.isDocument()) {
541 taskIsDocument = true;
542 taskDocumentData = affinityIntent.getData();
543 } else {
544 taskIsDocument = false;
545 taskDocumentData = null;
546 }
547
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700548 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700549 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700550 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700551 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700552 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
553 if (task.rootAffinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700554 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700555 return r;
556 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700557 } else if (taskIntent != null && taskIntent.getComponent() != null &&
558 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700559 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700560 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800561 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700562 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
563 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800564 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700565 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
566 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700567 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700568 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800569 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700570 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
571 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800572 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700573 } else if (DEBUG_TASKS) {
574 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700575 }
576 }
577
578 return null;
579 }
580
581 /**
582 * Returns the first activity (starting from the top of the stack) that
583 * is the same as the given activity. Returns null if no such activity
584 * is found.
585 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700586 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700587 ComponentName cls = intent.getComponent();
588 if (info.targetActivity != null) {
589 cls = new ComponentName(info.packageName, info.targetActivity);
590 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700591 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700592
Craig Mautner000f0022013-02-26 15:04:29 -0800593 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700594 TaskRecord task = mTaskHistory.get(taskNdx);
Kenny Guy2a764942014-04-02 13:29:20 +0100595 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700596 return null;
597 }
598 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800599 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
600 ActivityRecord r = activities.get(activityNdx);
601 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700602 //Slog.i(TAG, "Found matching class!");
603 //dump();
604 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
605 return r;
606 }
607 }
608 }
609
610 return null;
611 }
612
Amith Yamasani742a6712011-05-04 14:49:28 -0700613 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700614 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700615 */
Craig Mautner93529a42013-10-04 15:03:13 -0700616 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800617 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700618 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800619 }
620 mCurrentUser = userId;
621
622 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800623 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800624 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700625 TaskRecord task = mTaskHistory.get(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100626 if (isCurrentProfileLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700627 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
628 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800629 mTaskHistory.remove(i);
630 mTaskHistory.add(task);
631 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800632 // Use same value for i.
633 } else {
634 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800635 }
636 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700637 if (VALIDATE_TOKENS) {
638 validateAppTokensLocked();
639 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700640 }
641
Craig Mautner2420ead2013-04-01 17:13:20 -0700642 void minimalResumeActivityLocked(ActivityRecord r) {
643 r.state = ActivityState.RESUMED;
644 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
645 + " (starting new instance)");
646 r.stopped = false;
647 mResumedActivity = r;
648 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800649 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700650 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700651 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700652 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700653 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700654 }
655
Dianne Hackborncee04b52013-07-03 17:01:28 -0700656 private void startLaunchTraces() {
657 if (mFullyDrawnStartTime != 0) {
658 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
659 }
660 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
661 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
662 }
663
664 private void stopFullyDrawnTraceIfNeeded() {
665 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
666 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
667 mFullyDrawnStartTime = 0;
668 }
669 }
670
Craig Mautnere79d42682013-04-01 19:01:53 -0700671 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700672 if (r.displayStartTime == 0) {
673 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
674 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700675 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700676 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700677 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700678 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700679 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700680 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700681 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700682 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800683
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700684 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700685 // Make sure that there is no activity waiting for this to launch.
686 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
687 r.displayStartTime = r.fullyDrawnStartTime = 0;
688 } else {
689 mStackSupervisor.removeTimeoutsForActivityLocked(r);
690 mStackSupervisor.scheduleIdleTimeoutLocked(r);
691 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700692 }
693
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800694 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800695 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800696 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
697 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
698 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
699 activities.get(activityNdx).setSleeping(false);
700 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800701 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800702 if (mPausingActivity != null) {
703 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
704 activityPausedLocked(mPausingActivity.appToken, true);
705 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800706 }
707
Craig Mautner0eea92c2013-05-16 13:35:39 -0700708 /**
709 * @return true if something must be done before going to sleep.
710 */
711 boolean checkReadyForSleepLocked() {
712 if (mResumedActivity != null) {
713 // Still have something resumed; can't sleep until it is paused.
714 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
715 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700716 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700717 return true;
718 }
719 if (mPausingActivity != null) {
720 // Still waiting for something to pause; can't sleep yet.
721 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
722 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800723 }
724
Craig Mautner0eea92c2013-05-16 13:35:39 -0700725 return false;
726 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800727
Craig Mautner0eea92c2013-05-16 13:35:39 -0700728 void goToSleep() {
729 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800730
Craig Mautner0eea92c2013-05-16 13:35:39 -0700731 // Make sure any stopped but visible activities are now sleeping.
732 // This ensures that the activity's onStop() is called.
733 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
734 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
735 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
736 final ActivityRecord r = activities.get(activityNdx);
737 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
738 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800739 }
740 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800741 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700742 }
Craig Mautner59c00972012-07-30 12:10:24 -0700743
Dianne Hackbornd2835932010-12-13 16:28:46 -0800744 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700745 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800746 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700747 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800748 return null;
749 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800750
Winson Chung083baf92014-07-11 10:32:42 -0700751 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700752 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800753 // we can just go ahead and skip taking the screenshot if this is the home stack.
754 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700755 return null;
756 }
757
Winson Chung48a10a52014-08-27 14:36:51 -0700758 int w = mService.mThumbnailWidth;
759 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800760 if (w > 0) {
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800761 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tTaking screenshot");
762 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
763 w, h, SCREENSHOT_FORCE_565);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800764 }
Winson Chung376543b2014-05-21 17:43:28 -0700765 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800766 return null;
767 }
768
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700769 /**
770 * Start pausing the currently resumed activity. It is an error to call this if there
771 * is already an activity being paused or there is no resumed activity.
772 *
773 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
774 * @param uiSleeping True if this is happening with the user interface going to sleep (the
775 * screen turning off).
776 * @param resuming True if this is being called as part of resuming the top activity, so
777 * we shouldn't try to instigate a resume here.
778 * @param dontWait True if the caller does not want to wait for the pause to complete. If
779 * set to true, we will immediately complete the pause here before returning.
780 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
781 * it to tell us when it is done.
782 */
783 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
784 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800785 if (mPausingActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700786 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity);
787 completePauseLocked(false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800788 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700789 ActivityRecord prev = mResumedActivity;
790 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700791 if (!resuming) {
792 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
793 mStackSupervisor.resumeTopActivitiesLocked();
794 }
795 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700796 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800797
798 if (mActivityContainer.mParentActivity == null) {
799 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700800 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800801 }
802
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700803 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
804 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700805 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800806 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700807 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700808 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
809 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700810 prev.state = ActivityState.PAUSING;
811 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700812 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700813 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwaled3e2a082014-11-04 16:09:19 -0800814 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800815 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700816 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700817 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700818
819 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800820
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700821 if (prev.app != null && prev.app.thread != null) {
822 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
823 try {
824 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700825 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700826 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700827 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800828 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700829 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700830 } catch (Exception e) {
831 // Ignore exception, if process died other code will cleanup.
832 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800833 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700834 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700835 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700836 }
837 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800838 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700839 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700840 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700841 }
842
843 // If we are not going to sleep, we want to ensure the device is
844 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700845 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700846 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700847 }
848
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800849 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700850 // Have the window manager pause its key dispatching until the new
851 // activity has started. If we're pausing the activity just because
852 // the screen is being turned off and the UI is sleeping, don't interrupt
853 // key dispatch; the same activity will pick it up again on wakeup.
854 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800855 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700856 } else {
857 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
858 }
859
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700860 if (dontWait) {
861 // If the caller said they don't want to wait for the pause, then complete
862 // the pause now.
863 completePauseLocked(false);
864 return false;
865
866 } else {
867 // Schedule a pause timeout in case the app doesn't respond.
868 // We don't give it much time because this directly impacts the
869 // responsiveness seen by the user.
870 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
871 msg.obj = prev;
872 prev.pauseTime = SystemClock.uptimeMillis();
873 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
874 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
875 return true;
876 }
877
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700878 } else {
879 // This activity failed to schedule the
880 // pause, so just treat it as being paused now.
881 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700882 if (!resuming) {
883 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
884 }
885 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700886 }
887 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700888
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700889 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700890 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800891 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700892
Craig Mautnerd2328952013-03-05 12:46:26 -0800893 final ActivityRecord r = isInStackLocked(token);
894 if (r != null) {
895 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
896 if (mPausingActivity == r) {
897 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
898 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700899 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800900 } else {
901 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
902 r.userId, System.identityHashCode(r), r.shortComponentName,
903 mPausingActivity != null
904 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700905 }
906 }
907 }
908
Craig Mautnera0026042014-04-23 11:45:37 -0700909 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
910 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700911 if (r.state != ActivityState.STOPPING) {
912 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
913 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
914 return;
915 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700916 if (persistentState != null) {
917 r.persistentState = persistentState;
918 mService.notifyTaskPersisterLocked(r.task, false);
919 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700920 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700921 if (icicle != null) {
922 // If icicle is null, this is happening due to a timeout, so we
923 // haven't really saved the state.
924 r.icicle = icicle;
925 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800926 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -0800927 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700928 }
929 if (!r.stopped) {
930 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
931 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
932 r.stopped = true;
933 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -0700934 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
935 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700936 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700937 if (r.finishing) {
938 r.clearOptionsLocked();
939 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700940 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700941 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700942 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700943 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700944 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800945 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700946 }
947 }
948 }
949
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700950 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800951 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700952 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800953
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800954 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700955 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800956 if (prev.finishing) {
957 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700958 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800959 } else if (prev.app != null) {
960 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
961 if (prev.waitingVisible) {
962 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700963 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800964 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
965 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800966 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800967 if (prev.configDestroy) {
968 // The previous is being paused because the configuration
969 // is changing, which means it is actually stopping...
970 // To juggle the fact that we are also starting a new
971 // instance right now, we need to first completely stop
972 // the current instance before starting the new one.
973 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700974 destroyActivityLocked(prev, true, "pause-config");
Jose Lima4b6c6692014-08-12 17:41:12 -0700975 } else if (!hasVisibleBehindActivity()) {
976 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -0700977 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700978 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700979 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
980 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800981 // If we already have a few activities waiting to stop,
982 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700983 // them out. Or if r is the last of activity of the last task the stack
984 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800985 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700986 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800987 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700988 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800989 }
990 }
991 } else {
992 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
993 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800994 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800995 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700996 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800997
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700998 if (resumeNext) {
999 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1000 if (!mService.isSleepingOrShuttingDown()) {
1001 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1002 } else {
1003 mStackSupervisor.checkReadyForSleepLocked();
1004 ActivityRecord top = topStack.topRunningActivityLocked(null);
1005 if (top == null || (prev != null && top != prev)) {
1006 // If there are no more activities available to run,
1007 // do resume anyway to start something. Also if the top
1008 // activity on the stack is not the just paused activity,
1009 // we need to go ahead and resume it to ensure we complete
1010 // an in-flight app switch.
1011 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1012 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001013 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001014 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001015
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001016 if (prev != null) {
1017 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001018
Craig Mautner525f3d92013-05-07 14:01:50 -07001019 if (prev.app != null && prev.cpuTimeAtResume > 0
1020 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001021 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1022 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001023 if (diff > 0) {
1024 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1025 synchronized (bsi) {
1026 BatteryStatsImpl.Uid.Proc ps =
1027 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001028 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001029 if (ps != null) {
1030 ps.addForegroundTimeLocked(diff);
1031 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001032 }
1033 }
1034 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001035 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001036 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001037
1038 // Notfiy when the task stack has changed
1039 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001040 }
1041
1042 /**
1043 * Once we know that we have asked an application to put an activity in
1044 * the resumed state (either by launching it or explicitly telling it),
1045 * this function updates the rest of our state to match that fact.
1046 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001047 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001048 next.idle = false;
1049 next.results = null;
1050 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001051
1052 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1053 ProcessRecord app = next.task.mActivities.get(0).app;
1054 if (app != null && app != mService.mHomeProcess) {
1055 mService.mHomeProcess = app;
1056 }
1057 }
1058
Craig Mautner07566322013-09-26 16:42:55 -07001059 if (next.nowVisible) {
1060 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001061 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001062 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001063
1064 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001065 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001066
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001067 mStackSupervisor.reportResumedActivityLocked(next);
1068
1069 next.resumeKeyDispatchingLocked();
1070 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001071
1072 // Mark the point when the activity is resuming
1073 // TODO: To be more accurate, the mark should be before the onCreate,
1074 // not after the onResume. But for subsequent starts, onResume is fine.
1075 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001076 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001077 } else {
1078 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1079 }
Winson Chung376543b2014-05-21 17:43:28 -07001080
George Mount6ba042b2014-07-28 11:12:28 -07001081 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001082
1083 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1084 // When resuming an activity, require it to call requestVisibleBehind() again.
1085 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1086 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001087 }
1088
Craig Mautnere3a00d72014-04-16 08:31:19 -07001089 private void setVisibile(ActivityRecord r, boolean visible) {
1090 r.visible = visible;
1091 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001092 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001093 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001094 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001095 container.setVisible(visible);
1096 }
1097 }
1098
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001099 // Find the first visible activity above the passed activity and if it is translucent return it
1100 // otherwise return null;
1101 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1102 TaskRecord task = r.task;
1103 if (task == null) {
1104 return null;
1105 }
1106
1107 ActivityStack stack = task.stack;
1108 if (stack == null) {
1109 return null;
1110 }
1111
1112 int stackNdx = mStacks.indexOf(stack);
1113
1114 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1115 int taskNdx = tasks.indexOf(task);
1116
1117 ArrayList<ActivityRecord> activities = task.mActivities;
1118 int activityNdx = activities.indexOf(r) + 1;
1119
1120 final int numStacks = mStacks.size();
1121 while (stackNdx < numStacks) {
1122 tasks = mStacks.get(stackNdx).mTaskHistory;
1123 final int numTasks = tasks.size();
1124 while (taskNdx < numTasks) {
1125 activities = tasks.get(taskNdx).mActivities;
1126 final int numActivities = activities.size();
1127 while (activityNdx < numActivities) {
1128 final ActivityRecord activity = activities.get(activityNdx);
1129 if (!activity.finishing) {
1130 return activity.fullscreen ? null : activity;
1131 }
1132 ++activityNdx;
1133 }
1134 activityNdx = 0;
1135 ++taskNdx;
1136 }
1137 taskNdx = 0;
1138 ++stackNdx;
1139 }
1140
1141 return null;
1142 }
1143
Jose Lima7ba71252014-04-30 12:59:27 -07001144 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1145 // If so, this stack is hidden, otherwise it is visible.
1146 private boolean isStackVisible() {
1147 if (!isAttached()) {
1148 return false;
1149 }
1150
1151 if (mStackSupervisor.isFrontStack(this)) {
1152 return true;
1153 }
1154
Jose Lima729cb232014-05-05 15:59:40 -07001155 /**
1156 * Start at the task above this one and go up, looking for a visible
1157 * fullscreen activity, or a translucent activity that requested the
1158 * wallpaper to be shown behind it.
1159 */
Jose Lima7ba71252014-04-30 12:59:27 -07001160 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
1161 final ArrayList<TaskRecord> tasks = mStacks.get(i).getAllTasks();
1162 for (int taskNdx = 0; taskNdx < tasks.size(); taskNdx++) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001163 final TaskRecord task = tasks.get(taskNdx);
1164 final ArrayList<ActivityRecord> activities = task.mActivities;
Jose Lima7ba71252014-04-30 12:59:27 -07001165 for (int activityNdx = 0; activityNdx < activities.size(); activityNdx++) {
1166 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001167
1168 // Conditions for an activity to obscure the stack we're
1169 // examining:
1170 // 1. Not Finishing AND Visible AND:
1171 // 2. Either:
1172 // - Full Screen Activity OR
1173 // - On top of Home and our stack is NOT home
1174 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001175 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001176 return false;
1177 }
1178 }
1179 }
1180 }
1181
1182 return true;
1183 }
1184
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001185 /**
1186 * Make sure that all activities that need to be visible (that is, they
1187 * currently can be seen by the user) actually are.
1188 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001189 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1190 ActivityRecord top = topRunningActivityLocked(null);
1191 if (top == null) {
1192 return;
1193 }
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001194 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001195 TAG, "ensureActivitiesVisible behind " + top
1196 + " configChanges=0x" + Integer.toHexString(configChanges));
1197
Craig Mautner5eda9b32013-07-02 11:58:16 -07001198 if (mTranslucentActivityWaiting != top) {
1199 mUndrawnActivitiesBelowTopTranslucent.clear();
1200 if (mTranslucentActivityWaiting != null) {
1201 // Call the callback with a timeout indication.
1202 notifyActivityDrawnLocked(null);
1203 mTranslucentActivityWaiting = null;
1204 }
1205 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1206 }
1207
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001208 // If the top activity is not fullscreen, then we need to
1209 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001210 boolean aboveTop = true;
Jose Lima7ba71252014-04-30 12:59:27 -07001211 boolean behindFullscreen = !isStackVisible();
1212
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001213 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001214 final TaskRecord task = mTaskHistory.get(taskNdx);
1215 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001216 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1217 final ActivityRecord r = activities.get(activityNdx);
1218 if (r.finishing) {
1219 continue;
1220 }
1221 if (aboveTop && r != top) {
1222 continue;
1223 }
1224 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001225 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1226 // but must be drawn initially for the animation as though they were visible.
1227 if (!behindFullscreen || r.mLaunchTaskBehind) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001228 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001229 TAG, "Make visible? " + r + " finishing=" + r.finishing
1230 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001231
Craig Mautnerd44711d2013-02-23 11:24:36 -08001232 // First: if this is not the current activity being started, make
1233 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001234 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001235 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001236 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001237
1238 if (r.app == null || r.app.thread == null) {
Craig Mautnerbb742462014-07-07 15:28:55 -07001239 // This activity needs to be visible, but isn't even
1240 // running... get it started, but don't resume it
1241 // at this point.
1242 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
1243 if (r != starting) {
1244 r.startFreezingScreenLocked(r.app, configChanges);
1245 }
1246 if (!r.visible || r.mLaunchTaskBehind) {
1247 if (DEBUG_VISBILITY) Slog.v(
1248 TAG, "Starting and making visible: " + r);
1249 setVisibile(r, true);
1250 }
1251 if (r != starting) {
1252 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001253 }
1254
1255 } else if (r.visible) {
1256 // If this activity is already visible, then there is nothing
1257 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001258 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001259 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001260 try {
George Mount6ba042b2014-07-28 11:12:28 -07001261 if (r.returningOptions != null) {
1262 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1263 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001264 }
1265 } catch(RemoteException e) {
1266 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001267 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001268 // This activity is not currently visible, but is running.
1269 // Tell it to become visible.
1270 r.visible = true;
1271 if (r.state != ActivityState.RESUMED && r != starting) {
1272 // If this activity is paused, tell it
1273 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001274 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001275 TAG, "Making visible and scheduling visibility: " + r);
1276 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001277 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001278 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001279 mUndrawnActivitiesBelowTopTranslucent.add(r);
1280 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001281 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001282 r.sleeping = false;
1283 r.app.pendingUiClean = true;
1284 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1285 r.stopFreezingScreenLocked(false);
1286 } catch (Exception e) {
1287 // Just skip on any failure; we'll make it
1288 // visible when it next restarts.
1289 Slog.w(TAG, "Exception thrown making visibile: "
1290 + r.intent.getComponent(), e);
1291 }
1292 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001293 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001294
Craig Mautnerd44711d2013-02-23 11:24:36 -08001295 // Aggregate current change flags.
1296 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001297
Craig Mautnerd44711d2013-02-23 11:24:36 -08001298 if (r.fullscreen) {
1299 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001300 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1301 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001302 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001303 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001304 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001305 }
1306 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001307 if (DEBUG_VISBILITY) Slog.v(
1308 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1309 + " state=" + r.state
1310 + " behindFullscreen=" + behindFullscreen);
1311 // Now for any activities that aren't visible to the user, make
1312 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001313 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001314 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001315 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001316 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001317 switch (r.state) {
1318 case STOPPING:
1319 case STOPPED:
1320 if (r.app != null && r.app.thread != null) {
1321 if (DEBUG_VISBILITY) Slog.v(
1322 TAG, "Scheduling invisibility: " + r);
1323 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1324 }
1325 break;
1326
1327 case INITIALIZING:
1328 case RESUMED:
1329 case PAUSING:
1330 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001331 // This case created for transitioning activities from
1332 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001333 if (getVisibleBehindActivity() == r) {
1334 releaseBackgroundResources();
Craig Mautneree2e45a2014-06-27 12:10:03 -07001335 } else {
1336 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1337 mStackSupervisor.mStoppingActivities.add(r);
1338 }
1339 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001340 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001341 break;
1342
1343 default:
1344 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001345 }
1346 } catch (Exception e) {
1347 // Just skip on any failure; we'll make it
1348 // visible when it next restarts.
1349 Slog.w(TAG, "Exception thrown making hidden: "
1350 + r.intent.getComponent(), e);
1351 }
1352 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001353 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001354 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001355 }
1356 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001357 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001358
1359 if (mTranslucentActivityWaiting != null &&
1360 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1361 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1362 notifyActivityDrawnLocked(null);
1363 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001364 }
Craig Mautner58547802013-03-05 08:23:53 -08001365
George Mount6ba042b2014-07-28 11:12:28 -07001366 void convertToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001367 mTranslucentActivityWaiting = r;
1368 mUndrawnActivitiesBelowTopTranslucent.clear();
1369 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1370 }
1371
1372 /**
1373 * Called as activities below the top translucent activity are redrawn. When the last one is
1374 * redrawn notify the top activity by calling
1375 * {@link Activity#onTranslucentConversionComplete}.
1376 *
1377 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1378 * occurred and the activity will be notified immediately.
1379 */
1380 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001381 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001382 if ((r == null)
1383 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1384 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1385 // The last undrawn activity below the top has just been drawn. If there is an
1386 // opaque activity at the top, notify it that it can become translucent safely now.
1387 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1388 mTranslucentActivityWaiting = null;
1389 mUndrawnActivitiesBelowTopTranslucent.clear();
1390 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1391
Craig Mautner71dd1b62014-02-18 15:48:52 -08001392 if (waitingActivity != null) {
1393 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1394 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1395 try {
1396 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1397 waitingActivity.appToken, r != null);
1398 } catch (RemoteException e) {
1399 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001400 }
1401 }
1402 }
1403 }
1404
Craig Mautnera61bc652013-10-28 15:43:18 -07001405 /** If any activities below the top running one are in the INITIALIZING state and they have a
1406 * starting window displayed then remove that starting window. It is possible that the activity
1407 * in this state will never resumed in which case that starting window will be orphaned. */
1408 void cancelInitializingActivities() {
1409 final ActivityRecord topActivity = topRunningActivityLocked(null);
1410 boolean aboveTop = true;
1411 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1412 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1413 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1414 final ActivityRecord r = activities.get(activityNdx);
1415 if (aboveTop) {
1416 if (r == topActivity) {
1417 aboveTop = false;
1418 }
1419 continue;
1420 }
1421
1422 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1423 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1424 r.mStartingWindowShown = false;
1425 mWindowManager.removeAppStartingWindow(r.appToken);
1426 }
1427 }
1428 }
1429 }
1430
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001431 /**
1432 * Ensure that the top activity in the stack is resumed.
1433 *
1434 * @param prev The previously resumed activity, for when in the process
1435 * of pausing; can be null to call from elsewhere.
1436 *
1437 * @return Returns true if something is being resumed, or false if
1438 * nothing happened.
1439 */
1440 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001441 return resumeTopActivityLocked(prev, null);
1442 }
1443
1444 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001445 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001446 // Don't even start recursing.
1447 return false;
1448 }
1449
1450 boolean result = false;
1451 try {
1452 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001453 mStackSupervisor.inResumeTopActivity = true;
1454 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1455 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001456 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001457 }
Craig Mautner544efa72014-09-04 16:41:20 -07001458 result = resumeTopActivityInnerLocked(prev, options);
1459 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001460 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001461 }
1462 return result;
1463 }
1464
1465 final boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001466 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1467
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001468 if (!mService.mBooting && !mService.mBooted) {
1469 // Not ready yet!
1470 return false;
1471 }
1472
Craig Mautnerdf88d732014-01-27 09:21:32 -08001473 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001474 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001475 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001476 // Do not resume this stack if its parent is not resumed.
1477 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1478 return false;
1479 }
1480
Craig Mautnera61bc652013-10-28 15:43:18 -07001481 cancelInitializingActivities();
1482
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001483 // Find the first activity that is not finishing.
1484 ActivityRecord next = topRunningActivityLocked(null);
1485
1486 // Remember how we'll process this pause/resume situation, and ensure
1487 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001488 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1489 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001490
Craig Mautner84984fa2014-06-19 11:19:20 -07001491 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001492 if (next == null) {
1493 // There are no more activities! Let's just start up the
1494 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001495 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001496 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001497 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001498 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001499 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1500 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1501 return isOnHomeDisplay() &&
1502 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001503 }
1504
1505 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001506
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001507 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001508 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1509 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001510 // Make sure we have executed any pending transitions, since there
1511 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001512 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001513 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001514 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001515 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001516 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001517 return false;
1518 }
1519
Craig Mautner525f3d92013-05-07 14:01:50 -07001520 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001521 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001522 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001523 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001524 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001525 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001526 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001527 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001528 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001529 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001530 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001531 } else {
Craig Mautnere0a38842013-12-16 16:14:02 -08001532 if (DEBUG_STATES && isOnHomeDisplay()) Slog.d(TAG,
1533 "resumeTopActivityLocked: Launching home next");
1534 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001535 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1536 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1537 return isOnHomeDisplay() &&
1538 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev);
Craig Mautnere418ecd2013-05-01 17:02:29 -07001539 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001540 }
1541
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001542 // If we are sleeping, and there is no resumed activity, and the top
1543 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001544 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001545 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001546 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001547 // Make sure we have executed any pending transitions, since there
1548 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001549 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001550 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001551 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001552 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001553 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001554 return false;
1555 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001556
1557 // Make sure that the user who owns this activity is started. If not,
1558 // we will just leave it as is because someone should be bringing
1559 // another user's activities to the top of the stack.
1560 if (mService.mStartedUsers.get(next.userId) == null) {
1561 Slog.w(TAG, "Skipping resume of top activity " + next
1562 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001563 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001564 return false;
1565 }
1566
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001567 // The activity may be waiting for stop, but that is no longer
1568 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001569 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001570 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001571 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001572 mStackSupervisor.mWaitingVisibleActivities.remove(next);
louis_chang1110f552014-12-19 17:30:45 +08001573 next.waitingVisible = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001574
1575 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1576
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001577 // If we are currently pausing an activity, then don't do anything
1578 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001579 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001580 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1581 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001582 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001583 return false;
1584 }
1585
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001586 // Okay we are now going to start a switch, to 'next'. We may first
1587 // have to pause the current activity, but this is an important point
1588 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001589 // XXX "App Redirected" dialog is getting too many false positives
1590 // at this point, so turn off for now.
1591 if (false) {
1592 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1593 long now = SystemClock.uptimeMillis();
1594 final boolean inTime = mLastStartedActivity.startTime != 0
1595 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1596 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1597 final int nextUid = next.info.applicationInfo.uid;
1598 if (inTime && lastUid != nextUid
1599 && lastUid != next.launchedFromUid
1600 && mService.checkPermission(
1601 android.Manifest.permission.STOP_APP_SWITCHES,
1602 -1, next.launchedFromUid)
1603 != PackageManager.PERMISSION_GRANTED) {
1604 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1605 } else {
1606 next.startTime = now;
1607 mLastStartedActivity = next;
1608 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001609 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001610 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001611 mLastStartedActivity = next;
1612 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001613 }
Craig Mautner58547802013-03-05 08:23:53 -08001614
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001615 // We need to start pausing the current activity so the top one
1616 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001617 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1618 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001619 if (mResumedActivity != null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001620 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001621 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001622 }
1623 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001624 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1625 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001626 // At this point we want to put the upcoming activity's process
1627 // at the top of the LRU list, since we know we will be needing it
1628 // very soon and it would be a waste to let it get killed if it
1629 // happens to be sitting towards the end.
1630 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001631 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001632 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001633 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001634 return true;
1635 }
1636
Christopher Tated3f175c2012-06-14 14:16:54 -07001637 // If the most recent activity was noHistory but was only stopped rather
1638 // than stopped+finished because the device went to sleep, we need to make
1639 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001640 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001641 !mLastNoHistoryActivity.finishing) {
1642 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1643 " on new resume");
1644 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001645 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001646 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001647 }
1648
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001649 if (prev != null && prev != next) {
1650 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1651 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001652 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001653 if (DEBUG_SWITCH) Slog.v(
1654 TAG, "Resuming top, waiting visible to hide: " + prev);
1655 } else {
1656 // The next activity is already visible, so hide the previous
1657 // activity's windows right now so we can show the new one ASAP.
1658 // We only do this if the previous is finishing, which should mean
1659 // it is on top of the one being resumed so hiding it quickly
1660 // is good. Otherwise, we want to do the normal route of allowing
1661 // the resumed activity to be shown so we can decide if the
1662 // previous should actually be hidden depending on whether the
1663 // new one is found to be full-screen or not.
1664 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001665 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001666 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1667 + prev + ", waitingVisible="
1668 + (prev != null ? prev.waitingVisible : null)
1669 + ", nowVisible=" + next.nowVisible);
1670 } else {
1671 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1672 + prev + ", waitingVisible="
1673 + (prev != null ? prev.waitingVisible : null)
1674 + ", nowVisible=" + next.nowVisible);
1675 }
1676 }
1677 }
1678
Dianne Hackborne7f97212011-02-24 14:40:20 -08001679 // Launching this app's activity, make sure the app is no longer
1680 // considered stopped.
1681 try {
1682 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001683 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001684 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001685 } catch (IllegalArgumentException e) {
1686 Slog.w(TAG, "Failed trying to unstop package "
1687 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001688 }
1689
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001690 // We are starting up the next activity, so tell the window manager
1691 // that the previous one will be hidden soon. This way it can know
1692 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001693 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001694 if (prev != null) {
1695 if (prev.finishing) {
1696 if (DEBUG_TRANSITION) Slog.v(TAG,
1697 "Prepare close transition: prev=" + prev);
1698 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001699 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001700 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001701 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001702 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001703 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1704 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001705 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001706 mWindowManager.setAppWillBeHidden(prev.appToken);
1707 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001708 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001709 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001710 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001711 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001712 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001713 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001714 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001715 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001716 : next.mLaunchTaskBehind
1717 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1718 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001719 }
1720 }
1721 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001722 mWindowManager.setAppWillBeHidden(prev.appToken);
1723 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001724 }
Craig Mautner967212c2013-04-13 21:10:58 -07001725 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001726 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001727 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001728 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001729 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001730 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001731 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001732 }
1733 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001734
1735 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001736 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001737 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1738 if (opts != null) {
1739 resumeAnimOptions = opts.toBundle();
1740 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001741 next.applyOptionsLocked();
1742 } else {
1743 next.clearOptionsLocked();
1744 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001745
Craig Mautnercf910b02013-04-23 11:23:27 -07001746 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001747 if (next.app != null && next.app.thread != null) {
1748 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1749
1750 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001751 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001752
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001753 // schedule launch ticks to collect information about slow apps.
1754 next.startLaunchTickingLocked();
1755
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001756 ActivityRecord lastResumedActivity =
1757 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001758 ActivityState lastState = next.state;
1759
1760 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001761
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001762 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001763 next.state = ActivityState.RESUMED;
1764 mResumedActivity = next;
1765 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08001766 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001767 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001768 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001769 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001770
1771 // Have the window manager re-evaluate the orientation of
1772 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001773 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001774 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001775 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001776 mService.mConfiguration,
1777 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1778 if (config != null) {
1779 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001780 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001781 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001782 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001783
Craig Mautner525f3d92013-05-07 14:01:50 -07001784 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001785 // The configuration update wasn't able to keep the existing
1786 // instance of the activity, and instead started a new one.
1787 // We should be all done, but let's just make sure our activity
1788 // is still at the top and schedule another run if something
1789 // weird happened.
1790 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001791 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001792 "Activity config changed during resume: " + next
1793 + ", new next: " + nextNext);
1794 if (nextNext != next) {
1795 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001796 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001797 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001798 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001799 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001800 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001801 return true;
1802 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001803 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001804 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001805 }
Craig Mautner58547802013-03-05 08:23:53 -08001806
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001807 try {
1808 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001809 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001810 if (a != null) {
1811 final int N = a.size();
1812 if (!next.finishing && N > 0) {
1813 if (DEBUG_RESULTS) Slog.v(
1814 TAG, "Delivering results to " + next
1815 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001816 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001817 }
1818 }
1819
1820 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001821 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001822 }
1823
1824 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001825 next.userId, System.identityHashCode(next),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001826 next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001827
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001828 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001829 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001830 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001831 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001832 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001833 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001834 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001835
Craig Mautner0eea92c2013-05-16 13:35:39 -07001836 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001837
Craig Mautnerac6f8432013-07-17 13:24:59 -07001838 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001839 } catch (Exception e) {
1840 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001841 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1842 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001843 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001844 if (lastStack != null) {
1845 lastStack.mResumedActivity = lastResumedActivity;
1846 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001847 Slog.i(TAG, "Restarting because process died: " + next);
1848 if (!next.hasBeenLaunched) {
1849 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001850 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1851 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001852 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001853 next.appToken, next.packageName, next.theme,
1854 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001855 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1856 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001857 }
George Mount2c92c972014-03-20 09:38:23 -07001858 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001859 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001860 return true;
1861 }
1862
1863 // From this point on, if something goes wrong there is no way
1864 // to recover the activity.
1865 try {
1866 next.visible = true;
1867 completeResumeLocked(next);
1868 } catch (Exception e) {
1869 // If any exception gets thrown, toss away this
1870 // activity and try the next one.
1871 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001872 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001873 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001874 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001875 return true;
1876 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001877 next.stopped = false;
1878
1879 } else {
1880 // Whoops, need to restart this activity!
1881 if (!next.hasBeenLaunched) {
1882 next.hasBeenLaunched = true;
1883 } else {
1884 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001885 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001886 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001887 mService.compatibilityInfoForPackageLocked(
1888 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001889 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001890 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001891 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001892 }
1893 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1894 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001895 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001896 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001897 }
1898
Craig Mautnercf910b02013-04-23 11:23:27 -07001899 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001900 return true;
1901 }
1902
Craig Mautnerac6f8432013-07-17 13:24:59 -07001903 private void insertTaskAtTop(TaskRecord task) {
Craig Mautner9c85c202013-10-04 20:11:26 -07001904 // If this is being moved to the top by another activity or being launched from the home
1905 // activity, set mOnTopOfHome accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001906 if (isOnHomeDisplay()) {
1907 ActivityStack lastStack = mStackSupervisor.getLastStack();
1908 final boolean fromHome = lastStack.isHomeStack();
1909 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07001910 task.setTaskToReturnTo(fromHome
1911 ? lastStack.topTask() == null
1912 ? HOME_ACTIVITY_TYPE
1913 : lastStack.topTask().taskType
1914 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08001915 }
1916 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07001917 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07001918 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001919
Craig Mautnerac6f8432013-07-17 13:24:59 -07001920 mTaskHistory.remove(task);
1921 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07001922 int taskNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01001923 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001924 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07001925 while (--taskNdx >= 0) {
1926 if (!isCurrentProfileLocked(mTaskHistory.get(taskNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001927 break;
1928 }
1929 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001930 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001931 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001932 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001933 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001934 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001935
Craig Mautner8849a5e2013-04-02 16:41:03 -07001936 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001937 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001938 TaskRecord rTask = r.task;
1939 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07001940 // mLaunchTaskBehind tasks get placed at the back of the task stack.
1941 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08001942 // Last activity in task had been removed or ActivityManagerService is reusing task.
1943 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001944 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001945 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001946 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001947 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001948 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001949 if (!newTask) {
1950 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001951 boolean startIt = true;
1952 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1953 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08001954 if (task.getTopActivity() == null) {
1955 // All activities in task are finishing.
1956 continue;
1957 }
Craig Mautner70a86932013-02-28 22:37:44 -08001958 if (task == r.task) {
1959 // Here it is! Now, if this is not yet visible to the
1960 // user, then just add it without starting; it will
1961 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001962 if (!startIt) {
1963 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1964 + task, new RuntimeException("here").fillInStackTrace());
1965 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001966 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001967 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1968 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001969 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07001970 r.userId, r.info.configChanges, task.voiceSession != null,
1971 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001972 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001973 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001974 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001975 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001976 return;
1977 }
1978 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001979 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001980 startIt = false;
1981 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001982 }
1983 }
1984
1985 // Place a new activity at top of stack, so it is next to interact
1986 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08001987
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001988 // If we are not placing the new activity frontmost, we do not want
1989 // to deliver the onUserLeaving callback to the actual frontmost
1990 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08001991 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001992 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08001993 if (DEBUG_USER_LEAVING) Slog.v(TAG,
1994 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001995 }
Craig Mautner70a86932013-02-28 22:37:44 -08001996
1997 task = r.task;
1998
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001999 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002000 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002001 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002002 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002003 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002004
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002005 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002006 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002007 // We want to show the starting preview window if we are
2008 // switching to a new task, or the next activity's process is
2009 // not currently running.
2010 boolean showStartingIcon = newTask;
2011 ProcessRecord proc = r.app;
2012 if (proc == null) {
2013 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2014 }
2015 if (proc == null || proc.thread == null) {
2016 showStartingIcon = true;
2017 }
2018 if (DEBUG_TRANSITION) Slog.v(TAG,
2019 "Prepare open transition: starting " + r);
2020 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002021 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002022 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002023 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002024 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002025 ? r.mLaunchTaskBehind
2026 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2027 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002028 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002029 mNoAnimActivities.remove(r);
2030 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002031 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002032 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002033 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002034 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002035 boolean doShow = true;
2036 if (newTask) {
2037 // Even though this activity is starting fresh, we still need
2038 // to reset it to make sure we apply affinities to move any
2039 // existing activities from other tasks in to it.
2040 // If the caller has requested that the target task be
2041 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002042 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002043 resetTaskIfNeededLocked(r, r);
2044 doShow = topRunningNonDelayedActivityLocked(null) == r;
2045 }
George Mount70778d72014-07-01 16:33:45 -07002046 } else if (options != null && new ActivityOptions(options).getAnimationType()
2047 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2048 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002049 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002050 if (r.mLaunchTaskBehind) {
2051 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2052 // tell WindowManager that r is visible even though it is at the back of the stack.
2053 mWindowManager.setAppVisibility(r.appToken, true);
2054 ensureActivitiesVisibleLocked(null, 0);
2055 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002056 // Figure out if we are transitioning from another activity that is
2057 // "has the same starting icon" as the next one. This allows the
2058 // window manager to keep the previous window it had previously
2059 // created, if it still had one.
2060 ActivityRecord prev = mResumedActivity;
2061 if (prev != null) {
2062 // We don't want to reuse the previous starting preview if:
2063 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002064 if (prev.task != r.task) {
2065 prev = null;
2066 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002067 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002068 else if (prev.nowVisible) {
2069 prev = null;
2070 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002071 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002072 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002073 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002074 mService.compatibilityInfoForPackageLocked(
2075 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002076 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002077 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002078 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002079 }
2080 } else {
2081 // If this is the first activity, don't do any fancy animations,
2082 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002083 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002084 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002085 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002086 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002087 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002088 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002089 }
2090 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002091 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002092 }
2093
2094 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002095 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002096 }
2097 }
2098
Dianne Hackbornbe707852011-11-11 14:32:10 -08002099 final void validateAppTokensLocked() {
2100 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002101 mValidateAppTokens.ensureCapacity(numActivities());
2102 final int numTasks = mTaskHistory.size();
2103 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2104 TaskRecord task = mTaskHistory.get(taskNdx);
2105 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002106 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002107 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002108 }
Craig Mautner000f0022013-02-26 15:04:29 -08002109 TaskGroup group = new TaskGroup();
2110 group.taskId = task.taskId;
2111 mValidateAppTokens.add(group);
2112 final int numActivities = activities.size();
2113 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2114 final ActivityRecord r = activities.get(activityNdx);
2115 group.tokens.add(r.appToken);
2116 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002117 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002118 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002119 }
2120
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002121 /**
2122 * Perform a reset of the given task, if needed as part of launching it.
2123 * Returns the new HistoryRecord at the top of the task.
2124 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002125 /**
2126 * Helper method for #resetTaskIfNeededLocked.
2127 * We are inside of the task being reset... we'll either finish this activity, push it out
2128 * for another task, or leave it as-is.
2129 * @param task The task containing the Activity (taskTop) that might be reset.
2130 * @param forceReset
2131 * @return An ActivityOptions that needs to be processed.
2132 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002133 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002134 ActivityOptions topOptions = null;
2135
2136 int replyChainEnd = -1;
2137 boolean canMoveOptions = true;
2138
2139 // We only do this for activities that are not the root of the task (since if we finish
2140 // the root, we may no longer have the task!).
2141 final ArrayList<ActivityRecord> activities = task.mActivities;
2142 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002143 final int rootActivityNdx = task.findEffectiveRootIndex();
2144 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002145 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002146 if (target.frontOfTask)
2147 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002148
2149 final int flags = target.info.flags;
2150 final boolean finishOnTaskLaunch =
2151 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2152 final boolean allowTaskReparenting =
2153 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2154 final boolean clearWhenTaskReset =
2155 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2156
2157 if (!finishOnTaskLaunch
2158 && !clearWhenTaskReset
2159 && target.resultTo != null) {
2160 // If this activity is sending a reply to a previous
2161 // activity, we can't do anything with it now until
2162 // we reach the start of the reply chain.
2163 // XXX note that we are assuming the result is always
2164 // to the previous activity, which is almost always
2165 // the case but we really shouldn't count on.
2166 if (replyChainEnd < 0) {
2167 replyChainEnd = i;
2168 }
2169 } else if (!finishOnTaskLaunch
2170 && !clearWhenTaskReset
2171 && allowTaskReparenting
2172 && target.taskAffinity != null
2173 && !target.taskAffinity.equals(task.affinity)) {
2174 // If this activity has an affinity for another
2175 // task, then we need to move it out of here. We will
2176 // move it as far out of the way as possible, to the
2177 // bottom of the activity stack. This also keeps it
2178 // correctly ordered with any activities we previously
2179 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002180 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002181 final ActivityRecord bottom =
2182 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002183 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002184 if (bottom != null && target.taskAffinity != null
2185 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002186 // If the activity currently at the bottom has the
2187 // same task affinity as the one we are moving,
2188 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002189 targetTask = bottom.task;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002190 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002191 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002192 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002193 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002194 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002195 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002196 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2197 + " out to new task " + target.task);
2198 }
2199
Craig Mautnere3a74d52013-02-22 14:14:58 -08002200 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002201 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002202
Craig Mautner525f3d92013-05-07 14:01:50 -07002203 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002204 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2205 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002206 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002207 if (p.finishing) {
2208 continue;
2209 }
2210
Craig Mautnere3a74d52013-02-22 14:14:58 -08002211 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002212 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002213 topOptions = p.takeOptionsLocked();
2214 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002215 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002216 }
2217 }
2218 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002219 + task + " adding to task=" + targetTask
2220 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002221 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2222 + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002223 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002224 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002225
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002226 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002227 }
2228
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002229 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002230 if (VALIDATE_TOKENS) {
2231 validateAppTokensLocked();
2232 }
2233
2234 replyChainEnd = -1;
2235 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2236 // If the activity should just be removed -- either
2237 // because it asks for it, or the task should be
2238 // cleared -- then finish it and anything that is
2239 // part of its reply chain.
2240 int end;
2241 if (clearWhenTaskReset) {
2242 // In this case, we want to finish this activity
2243 // and everything above it, so be sneaky and pretend
2244 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002245 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002246 } else if (replyChainEnd < 0) {
2247 end = i;
2248 } else {
2249 end = replyChainEnd;
2250 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002251 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002252 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002253 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002254 if (p.finishing) {
2255 continue;
2256 }
2257 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002258 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002259 topOptions = p.takeOptionsLocked();
2260 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002261 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002262 }
2263 }
Craig Mautner58547802013-03-05 08:23:53 -08002264 if (DEBUG_TASKS) Slog.w(TAG,
2265 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002266 if (finishActivityLocked(
2267 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002268 end--;
2269 srcPos--;
2270 }
2271 }
2272 replyChainEnd = -1;
2273 } else {
2274 // If we were in the middle of a chain, well the
2275 // activity that started it all doesn't want anything
2276 // special, so leave it all as-is.
2277 replyChainEnd = -1;
2278 }
2279 }
2280
2281 return topOptions;
2282 }
2283
2284 /**
2285 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2286 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2287 * @param affinityTask The task we are looking for an affinity to.
2288 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2289 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2290 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2291 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002292 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002293 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002294 int replyChainEnd = -1;
2295 final int taskId = task.taskId;
2296 final String taskAffinity = task.affinity;
2297
2298 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2299 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002300 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2301
2302 // Do not operate on or below the effective root Activity.
2303 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002304 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002305 if (target.frontOfTask)
2306 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002307
2308 final int flags = target.info.flags;
2309 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2310 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2311
2312 if (target.resultTo != null) {
2313 // If this activity is sending a reply to a previous
2314 // activity, we can't do anything with it now until
2315 // we reach the start of the reply chain.
2316 // XXX note that we are assuming the result is always
2317 // to the previous activity, which is almost always
2318 // the case but we really shouldn't count on.
2319 if (replyChainEnd < 0) {
2320 replyChainEnd = i;
2321 }
2322 } else if (topTaskIsHigher
2323 && allowTaskReparenting
2324 && taskAffinity != null
2325 && taskAffinity.equals(target.taskAffinity)) {
2326 // This activity has an affinity for our task. Either remove it if we are
2327 // clearing or move it over to our task. Note that
2328 // we currently punt on the case where we are resetting a
2329 // task that is not at the top but who has activities above
2330 // with an affinity to it... this is really not a normal
2331 // case, and we will need to later pull that task to the front
2332 // and usually at that point we will do the reset and pick
2333 // up those remaining activities. (This only happens if
2334 // someone starts an activity in a new task from an activity
2335 // in a task that is not currently on top.)
2336 if (forceReset || finishOnTaskLaunch) {
2337 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2338 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2339 for (int srcPos = start; srcPos >= i; --srcPos) {
2340 final ActivityRecord p = activities.get(srcPos);
2341 if (p.finishing) {
2342 continue;
2343 }
Todd Kennedy539db512014-12-15 09:57:55 -08002344 finishActivityLocked(
2345 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002346 }
2347 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002348 if (taskInsertionPoint < 0) {
2349 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002350
Craig Mautner77878772013-03-04 19:46:24 -08002351 }
Craig Mautner77878772013-03-04 19:46:24 -08002352
2353 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2354 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2355 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2356 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002357 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002358 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002359 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002360
Craig Mautnere3a74d52013-02-22 14:14:58 -08002361 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2362 + " to stack at " + task,
2363 new RuntimeException("here").fillInStackTrace());
2364 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2365 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002366 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002367 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002368 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002369 if (VALIDATE_TOKENS) {
2370 validateAppTokensLocked();
2371 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002372
2373 // Now we've moved it in to place... but what if this is
2374 // a singleTop activity and we have put it on top of another
2375 // instance of the same activity? Then we drop the instance
2376 // below so it remains singleTop.
2377 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2378 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002379 int targetNdx = taskActivities.indexOf(target);
2380 if (targetNdx > 0) {
2381 ActivityRecord p = taskActivities.get(targetNdx - 1);
2382 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002383 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2384 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002385 }
2386 }
2387 }
2388 }
2389
2390 replyChainEnd = -1;
2391 }
2392 }
Craig Mautner77878772013-03-04 19:46:24 -08002393 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002394 }
2395
Craig Mautner8849a5e2013-04-02 16:41:03 -07002396 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002397 ActivityRecord newActivity) {
2398 boolean forceReset =
2399 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2400 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2401 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2402 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2403 forceReset = true;
2404 }
2405 }
2406
2407 final TaskRecord task = taskTop.task;
2408
2409 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2410 * for remaining tasks. Used for later tasks to reparent to task. */
2411 boolean taskFound = false;
2412
2413 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2414 ActivityOptions topOptions = null;
2415
Craig Mautner77878772013-03-04 19:46:24 -08002416 // Preserve the location for reparenting in the new task.
2417 int reparentInsertionPoint = -1;
2418
Craig Mautnere3a74d52013-02-22 14:14:58 -08002419 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2420 final TaskRecord targetTask = mTaskHistory.get(i);
2421
2422 if (targetTask == task) {
2423 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2424 taskFound = true;
2425 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002426 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2427 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002428 }
2429 }
2430
Craig Mautner70a86932013-02-28 22:37:44 -08002431 int taskNdx = mTaskHistory.indexOf(task);
2432 do {
2433 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2434 } while (taskTop == null && taskNdx >= 0);
2435
Craig Mautnere3a74d52013-02-22 14:14:58 -08002436 if (topOptions != null) {
2437 // If we got some ActivityOptions from an activity on top that
2438 // was removed from the task, propagate them to the new real top.
2439 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002440 taskTop.updateOptionsLocked(topOptions);
2441 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002442 topOptions.abort();
2443 }
2444 }
2445
2446 return taskTop;
2447 }
2448
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002449 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2450 String resultWho, int requestCode, int resultCode, Intent data) {
2451
2452 if (callingUid > 0) {
2453 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002454 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002455 }
2456
2457 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2458 + " : who=" + resultWho + " req=" + requestCode
2459 + " res=" + resultCode + " data=" + data);
2460 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2461 try {
2462 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2463 list.add(new ResultInfo(resultWho, requestCode,
2464 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002465 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002466 return;
2467 } catch (Exception e) {
2468 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2469 }
2470 }
2471
2472 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2473 }
2474
Craig Mautner04f0b702013-10-22 12:31:01 -07002475 private void adjustFocusedActivityLocked(ActivityRecord r) {
2476 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2477 ActivityRecord next = topRunningActivityLocked(null);
2478 if (next != r) {
2479 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002480 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2481 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner04f0b702013-10-22 12:31:01 -07002482 }
2483 }
Winson Chung648c83b2014-04-28 15:11:56 -07002484 ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2485 if (top != null) {
2486 mService.setFocusedActivityLocked(top);
2487 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002488 }
2489 }
2490
Craig Mautnerf3333272013-04-22 10:55:53 -07002491 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002492 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2493 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2494 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2495 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002496 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002497 if (DEBUG_STATES) {
2498 Slog.d(TAG, "no-history finish of " + r);
2499 }
2500 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Todd Kennedy539db512014-12-15 09:57:55 -08002501 "stop-no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002502 } else {
2503 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2504 + " on stop because we're just sleeping");
2505 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002506 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002507 }
2508
2509 if (r.app != null && r.app.thread != null) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002510 adjustFocusedActivityLocked(r);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002511 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002512 try {
2513 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002514 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2515 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002516 r.state = ActivityState.STOPPING;
2517 if (DEBUG_VISBILITY) Slog.v(
2518 TAG, "Stopping visible=" + r.visible + " for " + r);
2519 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002520 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002521 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002522 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002523 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002524 r.setSleeping(true);
2525 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002526 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002527 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002528 } catch (Exception e) {
2529 // Maybe just ignore exceptions here... if the process
2530 // has crashed, our death notification will clean things
2531 // up.
2532 Slog.w(TAG, "Exception thrown during pause", e);
2533 // Just in case, assume it to be stopped.
2534 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002535 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002536 r.state = ActivityState.STOPPED;
2537 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002538 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002539 }
2540 }
2541 }
2542 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002543
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002544 /**
2545 * @return Returns true if the activity is being finished, false if for
2546 * some reason it is being left as-is.
2547 */
2548 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002549 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002550 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002551 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002552 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002553 + ", result=" + resultCode + ", data=" + resultData
2554 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002555 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002556 return false;
2557 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002558
Craig Mautnerd44711d2013-02-23 11:24:36 -08002559 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002560 return true;
2561 }
2562
Craig Mautnerd2328952013-03-05 12:46:26 -08002563 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002564 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2565 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2566 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2567 ActivityRecord r = activities.get(activityNdx);
2568 if (r.resultTo == self && r.requestCode == requestCode) {
2569 if ((r.resultWho == null && resultWho == null) ||
2570 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2571 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2572 false);
2573 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002574 }
2575 }
2576 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002577 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002578 }
2579
Todd Kennedy539db512014-12-15 09:57:55 -08002580 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002581 ActivityRecord r = topRunningActivityLocked(null);
2582 if (r != null && r.app == app) {
2583 // If the top running activity is from this crashing
2584 // process, then terminate it to avoid getting in a loop.
2585 Slog.w(TAG, " Force finishing activity "
2586 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002587 int taskNdx = mTaskHistory.indexOf(r.task);
2588 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002589 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002590 // Also terminate any activities below it that aren't yet
2591 // stopped, to avoid a situation where one will get
2592 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002593 --activityNdx;
2594 if (activityNdx < 0) {
2595 do {
2596 --taskNdx;
2597 if (taskNdx < 0) {
2598 break;
2599 }
2600 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2601 } while (activityNdx < 0);
2602 }
2603 if (activityNdx >= 0) {
2604 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002605 if (r.state == ActivityState.RESUMED
2606 || r.state == ActivityState.PAUSING
2607 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002608 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002609 Slog.w(TAG, " Force finishing activity "
2610 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002611 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002612 }
2613 }
2614 }
2615 }
2616 }
2617
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002618 final void finishVoiceTask(IVoiceInteractionSession session) {
2619 IBinder sessionBinder = session.asBinder();
2620 boolean didOne = false;
2621 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2622 TaskRecord tr = mTaskHistory.get(taskNdx);
2623 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2624 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2625 ActivityRecord r = tr.mActivities.get(activityNdx);
2626 if (!r.finishing) {
2627 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2628 false);
2629 didOne = true;
2630 }
2631 }
2632 }
2633 }
2634 if (didOne) {
2635 mService.updateOomAdjLocked();
2636 }
2637 }
2638
Craig Mautnerd2328952013-03-05 12:46:26 -08002639 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002640 ArrayList<ActivityRecord> activities = r.task.mActivities;
2641 for (int index = activities.indexOf(r); index >= 0; --index) {
2642 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002643 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002644 break;
2645 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002646 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002647 }
2648 return true;
2649 }
2650
Dianne Hackborn5c607432012-02-28 14:44:19 -08002651 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2652 // send the result
2653 ActivityRecord resultTo = r.resultTo;
2654 if (resultTo != null) {
2655 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2656 + " who=" + r.resultWho + " req=" + r.requestCode
2657 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002658 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002659 if (resultData != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002660 resultData.setContentUserHint(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002661 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002662 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002663 if (r.info.applicationInfo.uid > 0) {
2664 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2665 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002666 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002667 }
2668 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2669 resultData);
2670 r.resultTo = null;
2671 }
2672 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2673
2674 // Make sure this HistoryRecord is not holding on to other resources,
2675 // because clients have remote IPC references to this object so we
2676 // can't assume that will go away and want to avoid circular IPC refs.
2677 r.results = null;
2678 r.pendingResults = null;
2679 r.newIntents = null;
2680 r.icicle = null;
2681 }
2682
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002683 /**
2684 * @return Returns true if this activity has been removed from the history
2685 * list, or false if it is still in the list and will be removed later.
2686 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002687 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2688 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002689 if (r.finishing) {
2690 Slog.w(TAG, "Duplicate finish request for " + r);
2691 return false;
2692 }
2693
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002694 r.makeFinishing();
Craig Mautneraea74a52014-03-08 14:23:10 -08002695 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002696 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002697 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002698 task.taskId, r.shortComponentName, reason);
2699 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002700 final int index = activities.indexOf(r);
2701 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002702 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002703 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002704 // If the caller asked that this activity (and all above it)
2705 // be cleared when the task is reset, don't lose that information,
2706 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002707 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002708 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002709 }
2710 }
2711
2712 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002713
2714 adjustFocusedActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002715
Dianne Hackborn5c607432012-02-28 14:44:19 -08002716 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002717
Craig Mautnerde4ef022013-04-07 19:01:33 -07002718 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002719 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002720 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002721 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002722 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002723 ? AppTransition.TRANSIT_TASK_CLOSE
2724 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002725
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002726 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002727 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002728
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002729 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002730 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2731 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002732 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002733 }
2734
Craig Mautneraea74a52014-03-08 14:23:10 -08002735 if (endTask) {
2736 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2737 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002738 } else if (r.state != ActivityState.PAUSING) {
2739 // If the activity is PAUSING, we will complete the finish once
2740 // it is done pausing; else we can just directly finish it here.
2741 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002742 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002743 } else {
2744 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2745 }
2746
2747 return false;
2748 }
2749
Craig Mautnerf3333272013-04-22 10:55:53 -07002750 static final int FINISH_IMMEDIATELY = 0;
2751 static final int FINISH_AFTER_PAUSE = 1;
2752 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002753
Craig Mautnerf3333272013-04-22 10:55:53 -07002754 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002755 // First things first: if this activity is currently visible,
2756 // and the resumed activity is not yet visible, then hold off on
2757 // finishing until the resumed one becomes visible.
2758 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002759 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2760 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002761 if (mStackSupervisor.mStoppingActivities.size() > 3
2762 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002763 // If we already have a few activities waiting to stop,
2764 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002765 // them out. Or if r is the last of activity of the last task the stack
2766 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002767 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002768 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002769 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002770 }
2771 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002772 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2773 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002774 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002775 if (oomAdj) {
2776 mService.updateOomAdjLocked();
2777 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002778 return r;
2779 }
2780
2781 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002782 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002783 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002784 mStackSupervisor.mWaitingVisibleActivities.remove(r);
louis_chang1110f552014-12-19 17:30:45 +08002785 r.waitingVisible = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002786 if (mResumedActivity == r) {
2787 mResumedActivity = null;
2788 }
2789 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002790 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002791 r.state = ActivityState.FINISHING;
2792
2793 if (mode == FINISH_IMMEDIATELY
2794 || prevState == ActivityState.STOPPED
2795 || prevState == ActivityState.INITIALIZING) {
2796 // If this activity is already stopped, we can just finish
2797 // it right now.
Craig Mautnerd163e752014-06-13 17:18:47 -07002798 r.makeFinishing();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002799 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002800 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002801 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002802 }
Craig Mautnerd163e752014-06-13 17:18:47 -07002803 if (DEBUG_CONTAINERS) Slog.d(TAG,
2804 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2805 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002806 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002807 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002808
2809 // Need to go through the full pause cycle to get this
2810 // activity into the stopped state and then finish it.
2811 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002812 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002813 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002814 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002815 return r;
2816 }
2817
Craig Mautneree36c772014-07-16 14:56:05 -07002818 void finishAllActivitiesLocked(boolean immediately) {
2819 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002820 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2821 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2822 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2823 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07002824 noActivitiesInStack = false;
2825 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002826 continue;
2827 }
Craig Mautneree36c772014-07-16 14:56:05 -07002828 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002829 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2830 }
2831 }
Craig Mautneree36c772014-07-16 14:56:05 -07002832 if (noActivitiesInStack) {
2833 mActivityContainer.onTaskListEmptyLocked();
2834 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002835 }
2836
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002837 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
2838 // Basic case: for simple app-centric recents, we need to recreate
2839 // the task if the affinity has changed.
2840 if (srec == null || srec.task.affinity == null ||
2841 !srec.task.affinity.equals(destAffinity)) {
2842 return true;
2843 }
2844 // Document-centric case: an app may be split in to multiple documents;
2845 // they need to re-create their task if this current activity is the root
2846 // of a document, unless simply finishing it will return them to the the
2847 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07002848 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
2849 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002850 // Okay, this activity is at the root of its task. What to do, what to do...
2851 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
2852 // Finishing won't return to an application, so we need to recreate.
2853 return true;
2854 }
2855 // We now need to get the task below it to determine what to do.
2856 int taskIdx = mTaskHistory.indexOf(srec.task);
2857 if (taskIdx <= 0) {
2858 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
2859 return false;
2860 }
2861 if (taskIdx == 0) {
2862 // At the bottom of the stack, nothing to go back to.
2863 return true;
2864 }
2865 TaskRecord prevTask = mTaskHistory.get(taskIdx);
2866 if (!srec.task.affinity.equals(prevTask.affinity)) {
2867 // These are different apps, so need to recreate.
2868 return true;
2869 }
2870 }
2871 return false;
2872 }
2873
Craig Mautnerd2328952013-03-05 12:46:26 -08002874 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002875 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002876 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002877 final TaskRecord task = srec.task;
2878 final ArrayList<ActivityRecord> activities = task.mActivities;
2879 final int start = activities.indexOf(srec);
2880 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002881 return false;
2882 }
2883 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002884 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002885 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002886 final ComponentName dest = destIntent.getComponent();
2887 if (start > 0 && dest != null) {
2888 for (int i = finishTo; i >= 0; i--) {
2889 ActivityRecord r = activities.get(i);
2890 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002891 r.info.name.equals(dest.getClassName())) {
2892 finishTo = i;
2893 parent = r;
2894 foundParentInTask = true;
2895 break;
2896 }
2897 }
2898 }
2899
2900 IActivityController controller = mService.mController;
2901 if (controller != null) {
2902 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2903 if (next != null) {
2904 // ask watcher if this is allowed
2905 boolean resumeOK = true;
2906 try {
2907 resumeOK = controller.activityResuming(next.packageName);
2908 } catch (RemoteException e) {
2909 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002910 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002911 }
2912
2913 if (!resumeOK) {
2914 return false;
2915 }
2916 }
2917 }
2918 final long origId = Binder.clearCallingIdentity();
2919 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002920 ActivityRecord r = activities.get(i);
2921 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002922 // Only return the supplied result for the first activity finished
2923 resultCode = Activity.RESULT_CANCELED;
2924 resultData = null;
2925 }
2926
2927 if (parent != null && foundParentInTask) {
2928 final int parentLaunchMode = parent.info.launchMode;
2929 final int destIntentFlags = destIntent.getFlags();
2930 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2931 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2932 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2933 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002934 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
2935 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002936 } else {
2937 try {
2938 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2939 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002940 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002941 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002942 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborn95465202014-09-15 16:21:55 -07002943 -1, parent.launchedFromUid, 0, null, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002944 foundParentInTask = res == ActivityManager.START_SUCCESS;
2945 } catch (RemoteException e) {
2946 foundParentInTask = false;
2947 }
2948 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08002949 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002950 }
2951 }
2952 Binder.restoreCallingIdentity(origId);
2953 return foundParentInTask;
2954 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002955 /**
2956 * Perform the common clean-up of an activity record. This is called both
2957 * as part of destroyActivityLocked() (when destroying the client-side
2958 * representation) and cleaning things up as a result of its hosting
2959 * processing going away, in which case there is no remaining client-side
2960 * state to destroy so only the cleanup here is needed.
2961 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002962 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2963 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002964 if (mResumedActivity == r) {
2965 mResumedActivity = null;
2966 }
Craig Mautner1872ce32014-03-28 23:05:42 +00002967 if (mPausingActivity == r) {
2968 mPausingActivity = null;
2969 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002970 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002971
2972 r.configDestroy = false;
2973 r.frozenBeforeDestroy = false;
2974
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002975 if (setState) {
2976 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2977 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002978 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002979 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002980 }
2981
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002982 // Make sure this record is no longer in the pending finishes list.
2983 // This could happen, for example, if we are trimming activities
2984 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07002985 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002986 mStackSupervisor.mWaitingVisibleActivities.remove(r);
louis_chang1110f552014-12-19 17:30:45 +08002987 r.waitingVisible = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002988
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002989 // Remove any pending results.
2990 if (r.finishing && r.pendingResults != null) {
2991 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
2992 PendingIntentRecord rec = apr.get();
2993 if (rec != null) {
2994 mService.cancelIntentSenderLocked(rec, false);
2995 }
2996 }
2997 r.pendingResults = null;
2998 }
2999
3000 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003001 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003002 }
3003
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003004 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003005 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003006 if (getVisibleBehindActivity() == r) {
3007 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003008 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003009 }
3010
3011 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003012 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003013 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003014 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003015 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003016 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003017 }
3018
Craig Mautner04a0ea62014-01-13 12:51:26 -08003019 private void removeActivityFromHistoryLocked(ActivityRecord r) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003020 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003021 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
3022 r.makeFinishing();
3023 if (DEBUG_ADD_REMOVE) {
3024 RuntimeException here = new RuntimeException("here");
3025 here.fillInStackTrace();
3026 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003027 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003028 r.takeFromHistory();
3029 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003030 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003031 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003032 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003033 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003034 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003035 if (VALIDATE_TOKENS) {
3036 validateAppTokensLocked();
3037 }
Craig Mautner312ba862014-02-10 17:55:01 -08003038 final TaskRecord task = r.task;
3039 if (task != null && task.removeActivity(r)) {
3040 if (DEBUG_STACK) Slog.i(TAG,
3041 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003042 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3043 task.isOverHomeStack()) {
3044 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo());
Craig Mautner312ba862014-02-10 17:55:01 -08003045 }
Craig Mautner41db4a72014-05-07 17:20:56 -07003046 removeTask(task);
Craig Mautner312ba862014-02-10 17:55:01 -08003047 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003048 cleanUpActivityServicesLocked(r);
3049 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003050 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003051
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003052 /**
3053 * Perform clean-up of service connections in an activity record.
3054 */
3055 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3056 // Throw away any services that have been bound by this activity.
3057 if (r.connections != null) {
3058 Iterator<ConnectionRecord> it = r.connections.iterator();
3059 while (it.hasNext()) {
3060 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003061 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003062 }
3063 r.connections = null;
3064 }
3065 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003066
Craig Mautneree2e45a2014-06-27 12:10:03 -07003067 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003068 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003069 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003070 mHandler.sendMessage(msg);
3071 }
3072
Craig Mautneree2e45a2014-06-27 12:10:03 -07003073 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003074 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003075 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003076 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3077 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3078 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3079 final ActivityRecord r = activities.get(activityNdx);
3080 if (r.finishing) {
3081 continue;
3082 }
3083 if (r.fullscreen) {
3084 lastIsOpaque = true;
3085 }
3086 if (owner != null && r.app != owner) {
3087 continue;
3088 }
3089 if (!lastIsOpaque) {
3090 continue;
3091 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003092 if (r.isDestroyable()) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003093 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3094 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003095 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003096 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003097 activityRemoved = true;
3098 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003099 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003100 }
3101 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003102 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003103 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003104 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003105 }
3106
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003107 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3108 if (r.isDestroyable()) {
3109 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3110 + " resumed=" + mResumedActivity
3111 + " pausing=" + mPausingActivity + " for reason " + reason);
3112 return destroyActivityLocked(r, true, reason);
3113 }
3114 return false;
3115 }
3116
3117 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3118 String reason) {
3119 // Iterate over tasks starting at the back (oldest) first.
3120 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3121 int maxTasks = tasks.size() / 4;
3122 if (maxTasks < 1) {
3123 maxTasks = 1;
3124 }
3125 int numReleased = 0;
3126 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3127 final TaskRecord task = mTaskHistory.get(taskNdx);
3128 if (!tasks.contains(task)) {
3129 continue;
3130 }
3131 if (DEBUG_RELEASE) Slog.d(TAG, "Looking for activities to release in " + task);
3132 int curNum = 0;
3133 final ArrayList<ActivityRecord> activities = task.mActivities;
3134 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3135 final ActivityRecord activity = activities.get(actNdx);
3136 if (activity.app == app && activity.isDestroyable()) {
3137 if (DEBUG_RELEASE) Slog.v(TAG, "Destroying " + activity
3138 + " in state " + activity.state + " resumed=" + mResumedActivity
3139 + " pausing=" + mPausingActivity + " for reason " + reason);
3140 destroyActivityLocked(activity, true, reason);
3141 if (activities.get(actNdx) != activity) {
3142 // Was removed from list, back up so we don't miss the next one.
3143 actNdx--;
3144 }
3145 curNum++;
3146 }
3147 }
3148 if (curNum > 0) {
3149 numReleased += curNum;
3150 maxTasks--;
3151 if (mTaskHistory.get(taskNdx) != task) {
3152 // The entire task got removed, back up so we don't miss the next one.
3153 taskNdx--;
3154 }
3155 }
3156 }
3157 if (DEBUG_RELEASE) Slog.d(TAG, "Done releasing: did " + numReleased + " activities");
3158 return numReleased;
3159 }
3160
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003161 /**
3162 * Destroy the current CLIENT SIDE instance of an activity. This may be
3163 * called both when actually finishing an activity, or when performing
3164 * a configuration switch where we destroy the current client-side object
3165 * but then create a new client-side object for this same HistoryRecord.
3166 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003167 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003168 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003169 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003170 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3171 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003172 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003173 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003174
3175 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003176
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003177 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003178
3179 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003180
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003181 if (hadApp) {
3182 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003183 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003184 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3185 mService.mHeavyWeightProcess = null;
3186 mService.mHandler.sendEmptyMessage(
3187 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3188 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003189 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003190 // Update any services we are bound to that might care about whether
3191 // their client may have activities.
3192 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003193 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003194 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003195 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003196 }
3197 }
3198
3199 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003200
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003201 try {
3202 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003203 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003204 r.configChangeFlags);
3205 } catch (Exception e) {
3206 // We can just ignore exceptions here... if the process
3207 // has crashed, our death notification will clean things
3208 // up.
3209 //Slog.w(TAG, "Exception thrown during finish", e);
3210 if (r.finishing) {
3211 removeActivityFromHistoryLocked(r);
3212 removedFromHistory = true;
3213 skipDestroy = true;
3214 }
3215 }
3216
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003217 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003218
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003219 // If the activity is finishing, we need to wait on removing it
3220 // from the list to give it a chance to do its cleanup. During
3221 // that time it may make calls back with its token so we need to
3222 // be able to find it on the list and so we don't want to remove
3223 // it from the list yet. Otherwise, we can just immediately put
3224 // it in the destroyed state since we are not removing it from the
3225 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003226 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003227 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3228 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003229 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003230 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003231 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3232 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003233 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003234 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003235 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003236 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003237 }
3238 } else {
3239 // remove this record from the history.
3240 if (r.finishing) {
3241 removeActivityFromHistoryLocked(r);
3242 removedFromHistory = true;
3243 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003244 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003245 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003246 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003247 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003248 }
3249 }
3250
3251 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003252
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003253 if (!mLRUActivities.remove(r) && hadApp) {
3254 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3255 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003256
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003257 return removedFromHistory;
3258 }
3259
Craig Mautnerd2328952013-03-05 12:46:26 -08003260 final void activityDestroyedLocked(IBinder token) {
3261 final long origId = Binder.clearCallingIdentity();
3262 try {
3263 ActivityRecord r = ActivityRecord.forToken(token);
3264 if (r != null) {
3265 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003266 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003267 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003268
3269 if (isInStackLocked(token) != null) {
3270 if (r.state == ActivityState.DESTROYING) {
3271 cleanUpActivityLocked(r, true, false);
3272 removeActivityFromHistoryLocked(r);
3273 }
3274 }
Craig Mautner05d29032013-05-03 13:40:13 -07003275 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003276 } finally {
3277 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003278 }
3279 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003280
Jose Lima4b6c6692014-08-12 17:41:12 -07003281 void releaseBackgroundResources() {
3282 if (hasVisibleBehindActivity() &&
3283 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
3284 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautner64ccb702014-10-01 09:38:40 -07003285 if (r == topRunningActivityLocked(null)) {
3286 // Don't release the top activity if it has requested to run behind the next
3287 // activity.
3288 return;
3289 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003290 if (DEBUG_STATES) Slog.d(TAG, "releaseBackgroundResources activtyDisplay=" +
3291 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003292 " thread=" + r.app.thread);
3293 if (r != null && r.app != null && r.app.thread != null) {
3294 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003295 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003296 } catch (RemoteException e) {
3297 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003298 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003299 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003300 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003301 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003302 }
3303 }
3304 }
3305
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003306 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003307 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3308 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003309 if (r != null) {
3310 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003311 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003312 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003313 }
3314 mStackSupervisor.resumeTopActivitiesLocked();
3315 }
3316
Jose Lima4b6c6692014-08-12 17:41:12 -07003317 boolean hasVisibleBehindActivity() {
3318 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003319 }
3320
Jose Lima4b6c6692014-08-12 17:41:12 -07003321 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003322 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003323 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003324 }
3325 }
3326
Jose Lima4b6c6692014-08-12 17:41:12 -07003327 ActivityRecord getVisibleBehindActivity() {
3328 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003329 }
3330
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003331 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3332 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003333 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003334 if (DEBUG_CLEANUP) Slog.v(
3335 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003336 + " with " + i + " entries");
3337 while (i > 0) {
3338 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003339 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003340 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003341 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003342 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003343 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003344 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003345 }
3346 }
3347 }
3348
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003349 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3350 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003351 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3352 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003353 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3354 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003355 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003356 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003357 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3358 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003359
3360 boolean hasVisibleActivities = false;
3361
3362 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003363 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003364 if (DEBUG_CLEANUP) Slog.v(
3365 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003366 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3367 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3368 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3369 final ActivityRecord r = activities.get(activityNdx);
3370 --i;
3371 if (DEBUG_CLEANUP) Slog.v(
3372 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3373 if (r.app == app) {
3374 boolean remove;
3375 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3376 // Don't currently have state for the activity, or
3377 // it is finishing -- always remove it.
3378 remove = true;
3379 } else if (r.launchCount > 2 &&
3380 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3381 // We have launched this activity too many times since it was
3382 // able to run, so give up and remove it.
3383 remove = true;
3384 } else {
3385 // The process may be gone, but the activity lives on!
3386 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003387 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003388 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003389 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003390 RuntimeException here = new RuntimeException("here");
3391 here.fillInStackTrace();
3392 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3393 + ": haveState=" + r.haveState
3394 + " stateNotNeeded=" + r.stateNotNeeded
3395 + " finishing=" + r.finishing
3396 + " state=" + r.state, here);
3397 }
3398 if (!r.finishing) {
3399 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3400 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3401 r.userId, System.identityHashCode(r),
3402 r.task.taskId, r.shortComponentName,
3403 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003404 if (r.state == ActivityState.RESUMED) {
3405 mService.updateUsageStats(r, false);
3406 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003407 }
3408 removeActivityFromHistoryLocked(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003409
Craig Mautner0247fc82013-02-28 14:32:06 -08003410 } else {
3411 // We have the current state for this activity, so
3412 // it can be restarted later when needed.
3413 if (localLOGV) Slog.v(
3414 TAG, "Keeping entry, setting app to null");
3415 if (r.visible) {
3416 hasVisibleActivities = true;
3417 }
3418 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3419 + r);
3420 r.app = null;
3421 r.nowVisible = false;
3422 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003423 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003424 "App died, clearing saved state of " + r);
3425 r.icicle = null;
3426 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003427 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003428
Craig Mautnerd2328952013-03-05 12:46:26 -08003429 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003430 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003431 }
3432 }
3433
3434 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003435 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003436
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003437 final void updateTransitLocked(int transit, Bundle options) {
3438 if (options != null) {
3439 ActivityRecord r = topRunningActivityLocked(null);
3440 if (r != null && r.state != ActivityState.RESUMED) {
3441 r.updateOptionsLocked(options);
3442 } else {
3443 ActivityOptions.abort(options);
3444 }
3445 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003446 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003447 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003448
Craig Mautner21d24a22014-04-23 11:45:37 -07003449 void updateTaskMovement(TaskRecord task, boolean toFront) {
3450 if (task.isPersistable) {
3451 task.mLastTimeMoved = System.currentTimeMillis();
3452 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3453 // recently will be most negative, tasks sent to the bottom before that will be less
3454 // negative. Similarly for recent tasks moved to the top which will be most positive.
3455 if (!toFront) {
3456 task.mLastTimeMoved *= -1;
3457 }
3458 }
3459 }
3460
Craig Mautner84984fa2014-06-19 11:19:20 -07003461 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003462 final int top = mTaskHistory.size() - 1;
3463 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3464 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003465 if (task.taskType == homeStackTaskType) {
3466 if (DEBUG_TASKS || DEBUG_STACK)
3467 Slog.d(TAG, "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003468 mTaskHistory.remove(taskNdx);
3469 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003470 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003471 mWindowManager.moveTaskToTop(task.taskId);
3472 return;
3473 }
3474 }
3475 }
3476
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003477 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003478 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003479
Craig Mautner11bf9a52013-02-19 14:08:51 -08003480 final int numTasks = mTaskHistory.size();
3481 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003482 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003483 // nothing to do!
3484 if (reason != null &&
3485 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3486 ActivityOptions.abort(options);
3487 } else {
3488 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3489 }
3490 return;
3491 }
3492
3493 // Shift all activities with this task up to the top
3494 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003495 insertTaskAtTop(tr);
Craig Mautner6b904ef2014-12-17 15:45:03 -08003496 moveToFront();
Craig Mautner11bf9a52013-02-19 14:08:51 -08003497
3498 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003499 if (reason != null &&
3500 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003501 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003502 ActivityRecord r = topRunningActivityLocked(null);
3503 if (r != null) {
3504 mNoAnimActivities.add(r);
3505 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003506 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003507 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003508 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003509 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003510
Craig Mautner05d29032013-05-03 13:40:13 -07003511 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003512 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003513
3514 if (VALIDATE_TOKENS) {
3515 validateAppTokensLocked();
3516 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003517 }
3518
3519 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003520 * Worker method for rearranging history stack. Implements the function of moving all
3521 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003522 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003523 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003524 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3525 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003526 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003527 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003528 * @return Returns true if the move completed, false if not.
3529 */
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003530 final boolean moveTaskToBackLocked(int taskId, ActivityRecord reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003531 final TaskRecord tr = taskForIdLocked(taskId);
3532 if (tr == null) {
3533 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3534 return false;
3535 }
3536
3537 Slog.i(TAG, "moveTaskToBack: " + tr);
3538
3539 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003540
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003541 // If we have a watcher, preflight the move before committing to it. First check
3542 // for *other* available tasks, but if none are available, then try again allowing the
3543 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003544 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003545 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003546 if (next == null) {
3547 next = topRunningActivityLocked(null, 0);
3548 }
3549 if (next != null) {
3550 // ask watcher if this is allowed
3551 boolean moveOK = true;
3552 try {
3553 moveOK = mService.mController.activityResuming(next.packageName);
3554 } catch (RemoteException e) {
3555 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003556 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003557 }
3558 if (!moveOK) {
3559 return false;
3560 }
3561 }
3562 }
3563
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003564 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003565 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003566
Craig Mautner11bf9a52013-02-19 14:08:51 -08003567 mTaskHistory.remove(tr);
3568 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003569 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003570
Craig Mautnerc8143c62013-09-03 12:15:57 -07003571 // There is an assumption that moving a task to the back moves it behind the home activity.
3572 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003573 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003574 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3575 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003576 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003577 break;
3578 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003579 if (taskNdx == 1) {
3580 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003581 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003582 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003583 }
3584
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003585 if (reason != null &&
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003586 (reason.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
3587 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003588 ActivityRecord r = topRunningActivityLocked(null);
3589 if (r != null) {
3590 mNoAnimActivities.add(r);
3591 }
3592 } else {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003593 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003594 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003595 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003596
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003597 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003598 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003599 }
3600
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003601 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Craig Mautner84984fa2014-06-19 11:19:20 -07003602 if (task == tr && tr.isOverHomeStack() || numTasks <= 1 && isOnHomeDisplay()) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003603 if (!mService.mBooting && !mService.mBooted) {
3604 // Not ready yet!
3605 return false;
3606 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003607 final int taskToReturnTo = tr.getTaskToReturnTo();
3608 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
3609 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003610 }
3611
Craig Mautner05d29032013-05-03 13:40:13 -07003612 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003613 return true;
3614 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003615
Craig Mautner8849a5e2013-04-02 16:41:03 -07003616 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003617 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003618 final Uri data = r.intent.getData();
3619 final String strData = data != null ? data.toSafeString() : null;
3620
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003621 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003622 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003623 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003624 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003625 }
3626
3627 /**
3628 * Make sure the given activity matches the current configuration. Returns
3629 * false if the activity had to be destroyed. Returns true if the
3630 * configuration is the same, or the activity will remain running as-is
3631 * for whatever reason. Ensures the HistoryRecord is updated with the
3632 * correct configuration and all other bookkeeping is handled.
3633 */
3634 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3635 int globalChanges) {
3636 if (mConfigWillChange) {
3637 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3638 "Skipping config check (will change): " + r);
3639 return true;
3640 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003641
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003642 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3643 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003644
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003645 // Short circuit: if the two configurations are the exact same
3646 // object (the common case), then there is nothing to do.
3647 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003648 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003649 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3650 "Configuration unchanged in " + r);
3651 return true;
3652 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003653
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003654 // We don't worry about activities that are finishing.
3655 if (r.finishing) {
3656 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3657 "Configuration doesn't matter in finishing " + r);
3658 r.stopFreezingScreenLocked(false);
3659 return true;
3660 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003661
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003662 // Okay we now are going to make this activity have the new config.
3663 // But then we need to figure out how it needs to deal with that.
3664 Configuration oldConfig = r.configuration;
3665 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003666
3667 // Determine what has changed. May be nothing, if this is a config
3668 // that has come back from the app after going idle. In that case
3669 // we just want to leave the official config object now in the
3670 // activity and do nothing else.
3671 final int changes = oldConfig.diff(newConfig);
3672 if (changes == 0 && !r.forceNewConfig) {
3673 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3674 "Configuration no differences in " + r);
3675 return true;
3676 }
3677
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003678 // If the activity isn't currently running, just leave the new
3679 // configuration and it will pick that up next time it starts.
3680 if (r.app == null || r.app.thread == null) {
3681 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3682 "Configuration doesn't matter not running " + r);
3683 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003684 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003685 return true;
3686 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003687
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003688 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003689 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3690 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3691 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003692 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003693 + ", newConfig=" + newConfig);
3694 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003695 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003696 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3697 r.configChangeFlags |= changes;
3698 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003699 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003700 if (r.app == null || r.app.thread == null) {
3701 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003702 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003703 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003704 } else if (r.state == ActivityState.PAUSING) {
3705 // A little annoying: we are waiting for this activity to
3706 // finish pausing. Let's not do anything now, but just
3707 // flag that it needs to be restarted when done pausing.
3708 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003709 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003710 r.configDestroy = true;
3711 return true;
3712 } else if (r.state == ActivityState.RESUMED) {
3713 // Try to optimize this case: the configuration is changing
3714 // and we need to restart the top, resumed activity.
3715 // Instead of doing the normal handshaking, just say
3716 // "restart!".
3717 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003718 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003719 relaunchActivityLocked(r, r.configChangeFlags, true);
3720 r.configChangeFlags = 0;
3721 } else {
3722 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003723 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003724 relaunchActivityLocked(r, r.configChangeFlags, false);
3725 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003726 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003727
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003728 // All done... tell the caller we weren't able to keep this
3729 // activity around.
3730 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003731 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003732
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003733 // Default case: the activity can handle this new configuration, so
3734 // hand it over. Note that we don't need to give it the new
3735 // configuration, since we always send configuration changes to all
3736 // process when they happen so it can just use whatever configuration
3737 // it last got.
3738 if (r.app != null && r.app.thread != null) {
3739 try {
3740 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003741 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003742 } catch (RemoteException e) {
3743 // If process died, whatever.
3744 }
3745 }
3746 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003747
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003748 return true;
3749 }
3750
Craig Mautnerc8143c62013-09-03 12:15:57 -07003751 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003752 int changes, boolean andResume) {
3753 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003754 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003755 if (andResume) {
3756 results = r.results;
3757 newIntents = r.newIntents;
3758 }
3759 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3760 + " with results=" + results + " newIntents=" + newIntents
3761 + " andResume=" + andResume);
3762 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003763 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003764 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003765
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003766 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003767
Craig Mautner34b73df2014-01-12 21:11:08 -08003768 mStackSupervisor.removeChildActivityContainers(r);
3769
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003770 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003771 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3772 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3773 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003774 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003775 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003776 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003777 // Note: don't need to call pauseIfSleepingLocked() here, because
3778 // the caller will only pass in 'andResume' if this activity is
3779 // currently resumed, which implies we aren't sleeping.
3780 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003781 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003782 }
3783
3784 if (andResume) {
3785 r.results = null;
3786 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003787 r.state = ActivityState.RESUMED;
3788 } else {
3789 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3790 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003791 }
3792
3793 return true;
3794 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003795
3796 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003797 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3798 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3799 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3800 final ActivityRecord r = activities.get(activityNdx);
3801 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003802 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003803 }
3804 if (r.fullscreen && !r.finishing) {
3805 return false;
3806 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003807 }
3808 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003809 final ActivityRecord r = ActivityRecord.forToken(token);
3810 if (r == null) {
3811 return false;
3812 }
3813 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3814 + " would have returned true for r=" + r);
3815 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003816 }
3817
3818 void closeSystemDialogsLocked() {
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.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003824 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003825 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003826 }
3827 }
3828 }
3829
3830 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3831 boolean didSomething = false;
3832 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003833 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003834 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3835 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3836 int numActivities = activities.size();
3837 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3838 ActivityRecord r = activities.get(activityNdx);
3839 final boolean samePackage = r.packageName.equals(name)
3840 || (name == null && r.userId == userId);
3841 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3842 && (samePackage || r.task == lastTask)
3843 && (r.app == null || evenPersistent || !r.app.persistent)) {
3844 if (!doit) {
3845 if (r.finishing) {
3846 // If this activity is just finishing, then it is not
3847 // interesting as far as something to stop.
3848 continue;
3849 }
3850 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003851 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003852 if (r.isHomeActivity()) {
3853 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
3854 Slog.i(TAG, "Skip force-stop again " + r);
3855 continue;
3856 } else {
3857 homeActivity = r.realActivity;
3858 }
3859 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003860 didSomething = true;
3861 Slog.i(TAG, " Force finishing activity " + r);
3862 if (samePackage) {
3863 if (r.app != null) {
3864 r.app.removed = true;
3865 }
3866 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003867 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003868 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003869 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3870 true)) {
3871 // r has been deleted from mActivities, accommodate.
3872 --numActivities;
3873 --activityNdx;
3874 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003875 }
3876 }
3877 }
3878 return didSomething;
3879 }
3880
Dianne Hackborn09233282014-04-30 11:33:59 -07003881 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003882 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003883 final TaskRecord task = mTaskHistory.get(taskNdx);
3884 ActivityRecord r = null;
3885 ActivityRecord top = null;
3886 int numActivities = 0;
3887 int numRunning = 0;
3888 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07003889 if (activities.isEmpty()) {
3890 continue;
3891 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07003892 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07003893 continue;
3894 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003895 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3896 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003897
Craig Mautneraab647e2013-02-28 16:31:36 -08003898 // Initialize state for next task if needed.
3899 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3900 top = r;
3901 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003902 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003903
3904 // Add 'r' into the current task.
3905 numActivities++;
3906 if (r.app != null && r.app.thread != null) {
3907 numRunning++;
3908 }
3909
3910 if (localLOGV) Slog.v(
3911 TAG, r.intent.getComponent().flattenToShortString()
3912 + ": task=" + r.task);
3913 }
3914
3915 RunningTaskInfo ci = new RunningTaskInfo();
3916 ci.id = task.taskId;
3917 ci.baseActivity = r.intent.getComponent();
3918 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003919 ci.lastActiveTime = task.lastActiveTime;
3920
Craig Mautnerc0ffce52014-07-01 12:38:52 -07003921 if (top.task != null) {
3922 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08003923 }
3924 ci.numActivities = numActivities;
3925 ci.numRunning = numRunning;
3926 //System.out.println(
3927 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08003928 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003929 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003930 }
3931
3932 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003933 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003934 if (DEBUG_SWITCH) Slog.d(
3935 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003936 if (top >= 0) {
3937 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3938 int activityTop = activities.size() - 1;
3939 if (activityTop > 0) {
3940 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3941 "unhandled-back", true);
3942 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003943 }
3944 }
3945
Craig Mautner6b74cb52013-09-27 17:02:21 -07003946 /**
3947 * Reset local parameters because an app's activity died.
3948 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003949 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003950 */
3951 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003952 if (mPausingActivity != null && mPausingActivity.app == app) {
3953 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3954 "App died while pausing: " + mPausingActivity);
3955 mPausingActivity = null;
3956 }
3957 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3958 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003959 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003960 }
3961
Craig Mautner19091252013-10-05 00:03:53 -07003962 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003963 }
3964
Craig Mautnercae015f2013-02-08 14:31:27 -08003965 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003966 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3967 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3968 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3969 final ActivityRecord r = activities.get(activityNdx);
3970 if (r.app == app) {
3971 Slog.w(TAG, " Force finishing activity "
3972 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07003973 // Force the destroy to skip right to removal.
3974 r.app = null;
3975 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003976 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003977 }
3978 }
3979 }
3980
Dianne Hackborn390517b2013-05-30 15:03:32 -07003981 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003982 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003983 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08003984 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3985 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07003986 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
3987 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003988 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07003989 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003990 if (printed) {
3991 header = null;
3992 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003993 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003994 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08003995 }
3996
3997 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
3998 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
3999
4000 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004001 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4002 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004003 }
4004 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004005 final int top = mTaskHistory.size() - 1;
4006 if (top >= 0) {
4007 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4008 int listTop = list.size() - 1;
4009 if (listTop >= 0) {
4010 activities.add(list.get(listTop));
4011 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004012 }
4013 } else {
4014 ItemMatcher matcher = new ItemMatcher();
4015 matcher.build(name);
4016
Craig Mautneraab647e2013-02-28 16:31:36 -08004017 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4018 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4019 if (matcher.match(r1, r1.intent.getComponent())) {
4020 activities.add(r1);
4021 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004022 }
4023 }
4024 }
4025
4026 return activities;
4027 }
4028
4029 ActivityRecord restartPackage(String packageName) {
4030 ActivityRecord starting = topRunningActivityLocked(null);
4031
4032 // All activities that came from the package must be
4033 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004034 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4035 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4036 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4037 final ActivityRecord a = activities.get(activityNdx);
4038 if (a.info.packageName.equals(packageName)) {
4039 a.forceNewConfig = true;
4040 if (starting != null && a == starting && a.visible) {
4041 a.startFreezingScreenLocked(starting.app,
4042 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4043 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004044 }
4045 }
4046 }
4047
4048 return starting;
4049 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004050
Craig Mautner41db4a72014-05-07 17:20:56 -07004051 void removeTask(TaskRecord task) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004052 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08004053 mWindowManager.removeTask(task.taskId);
4054 final ActivityRecord r = mResumedActivity;
4055 if (r != null && r.task == task) {
4056 mResumedActivity = null;
4057 }
4058
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004059 final int taskNdx = mTaskHistory.indexOf(task);
4060 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004061 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4062 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4063 if (!nextTask.isOverHomeStack()) {
4064 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4065 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004066 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004067 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004068 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004069
4070 if (task.mActivities.isEmpty()) {
4071 final boolean isVoiceSession = task.voiceSession != null;
4072 if (isVoiceSession) {
4073 try {
4074 task.voiceSession.taskFinished(task.intent, task.taskId);
4075 } catch (RemoteException e) {
4076 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004077 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004078 if (task.autoRemoveFromRecents() || isVoiceSession) {
4079 // Task creator asked to remove this when done, or this task was a voice
4080 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004081 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004082 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004083 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004084 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004085
4086 if (mTaskHistory.isEmpty()) {
4087 if (DEBUG_STACK) Slog.i(TAG, "removeTask: moving to back stack=" + this);
4088 if (isOnHomeDisplay()) {
4089 mStackSupervisor.moveHomeStack(!isHomeStack());
4090 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004091 if (mStacks != null) {
4092 mStacks.remove(this);
4093 mStacks.add(0, this);
4094 }
Craig Mautnerd163e752014-06-13 17:18:47 -07004095 mActivityContainer.onTaskListEmptyLocked();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004096 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004097 }
4098
Dianne Hackborn91097de2014-04-04 18:02:06 -07004099 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4100 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4101 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004102 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4103 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004104 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004105 return task;
4106 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004107
4108 ArrayList<TaskRecord> getAllTasks() {
4109 return new ArrayList<TaskRecord>(mTaskHistory);
4110 }
4111
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004112 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004113 task.stack = this;
4114 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07004115 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004116 } else {
4117 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004118 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004119 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004120 if (!moving && task.voiceSession != null) {
4121 try {
4122 task.voiceSession.taskStarted(task.intent, task.taskId);
4123 } catch (RemoteException e) {
4124 }
4125 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004126 }
4127
4128 public int getStackId() {
4129 return mStackId;
4130 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004131
4132 @Override
4133 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004134 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4135 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004136 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004137}