blob: daa38916e8fe1b6a9814b8f0e516086b6213d1b4 [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;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800149
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700150 /**
151 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800152 * running) activities. It contains #TaskRecord objects.
153 */
154 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
155
156 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800157 * Used for validating app tokens with window manager.
158 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800159 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800160
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700161 /**
162 * List of running activities, sorted by recent usage.
163 * The first entry in the list is the least recently used.
164 * It contains HistoryRecord objects.
165 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700166 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700167
168 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700169 * Animations that for the current transition have requested not to
170 * be considered for the transition animation.
171 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700172 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700173
174 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700175 * When we are in the process of pausing an activity, before starting the
176 * next one, this variable holds the activity that is currently being paused.
177 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800178 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700179
180 /**
181 * This is the last activity that we put into the paused state. This is
182 * used to determine if we need to do an activity transition while sleeping,
183 * when we normally hold the top activity paused.
184 */
185 ActivityRecord mLastPausedActivity = null;
186
187 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700188 * Activities that specify No History must be removed once the user navigates away from them.
189 * If the device goes to sleep with such an activity in the paused state then we save it here
190 * and finish it later if another activity replaces it on wakeup.
191 */
192 ActivityRecord mLastNoHistoryActivity = null;
193
194 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700195 * Current activity that is resumed, or null if there is none.
196 */
197 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800198
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700199 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700200 * This is the last activity that has been started. It is only used to
201 * identify when multiple activities are started at once so that the user
202 * can be warned they may not be in the activity they think they are.
203 */
204 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800205
Craig Mautner5eda9b32013-07-02 11:58:16 -0700206 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
207 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
208 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
209 // Activity in mTranslucentActivityWaiting is notified via
210 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
211 // background activity being drawn then the same call will be made with a true value.
212 ActivityRecord mTranslucentActivityWaiting = null;
Craig Mautnereb8abf72014-07-02 15:04:09 -0700213 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
Craig Mautner5eda9b32013-07-02 11:58:16 -0700214 new ArrayList<ActivityRecord>();
215
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700216 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700217 * Set when we know we are going to be calling updateConfiguration()
218 * soon, so want to skip intermediate config checks.
219 */
220 boolean mConfigWillChange;
221
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700222 long mLaunchStartTime = 0;
223 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800224
Craig Mautner858d8a62013-04-23 17:08:34 -0700225 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700226
Craig Mautnerc00204b2013-03-05 15:02:14 -0800227 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800228 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800229 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
230 ArrayList<ActivityStack> mStacks;
231 /** The attached Display's unique identifier, or -1 if detached */
232 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800233
Craig Mautner27084302013-03-25 08:05:25 -0700234 /** Run all ActivityStacks through this */
235 final ActivityStackSupervisor mStackSupervisor;
236
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700237 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700238 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
239 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
240 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
241 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700242 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700243 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700244 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700245
246 static class ScheduleDestroyArgs {
247 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700248 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700249 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700250 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700251 mReason = reason;
252 }
253 }
254
Zoran Marcetaf958b322012-08-09 20:27:12 +0900255 final Handler mHandler;
256
257 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700258 //public Handler() {
259 // if (localLOGV) Slog.v(TAG, "Handler started!");
260 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700261 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900262 super(looper);
263 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700264
Zoran Marcetaf958b322012-08-09 20:27:12 +0900265 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700266 public void handleMessage(Message msg) {
267 switch (msg.what) {
268 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800269 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700270 // We don't at this point know if the activity is fullscreen,
271 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800272 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700273 synchronized (mService) {
274 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700275 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700276 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700277 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800278 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700279 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700280 case LAUNCH_TICK_MSG: {
281 ActivityRecord r = (ActivityRecord)msg.obj;
282 synchronized (mService) {
283 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700284 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700285 }
286 }
287 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700288 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800289 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700290 // We don't at this point know if the activity is fullscreen,
291 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800292 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800293 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800294 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800295 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700296 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700297 case STOP_TIMEOUT_MSG: {
298 ActivityRecord r = (ActivityRecord)msg.obj;
299 // We don't at this point know if the activity is fullscreen,
300 // so we need to be conservative and assume it isn't.
301 Slog.w(TAG, "Activity stop timeout for " + r);
302 synchronized (mService) {
303 if (r.isInHistory()) {
304 activityStoppedLocked(r, null, null, null);
305 }
306 }
307 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700308 case DESTROY_ACTIVITIES_MSG: {
309 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
310 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700311 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700312 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700313 } break;
314 case TRANSLUCENT_TIMEOUT_MSG: {
315 synchronized (mService) {
316 notifyActivityDrawnLocked(null);
317 }
318 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700319 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700320 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700321 final ActivityRecord r = getVisibleBehindActivity();
322 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700323 if (r != null) {
324 mService.killAppAtUsersRequest(r.app, null);
325 }
326 }
327 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700328 }
329 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800330 }
331
Craig Mautner34b73df2014-01-12 21:11:08 -0800332 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800333 int count = 0;
334 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
335 count += mTaskHistory.get(taskNdx).mActivities.size();
336 }
337 return count;
338 }
339
Craig Mautner4a1cb222013-12-04 16:14:06 -0800340 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer) {
341 mActivityContainer = activityContainer;
342 mStackSupervisor = activityContainer.getOuter();
343 mService = mStackSupervisor.mService;
344 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
345 mWindowManager = mService.mWindowManager;
346 mStackId = activityContainer.mStackId;
347 mCurrentUser = mService.mCurrentUserId;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700348 }
Craig Mautner5962b122012-10-05 14:45:52 -0700349
Amith Yamasani734983f2014-03-04 16:48:05 -0800350 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100351 * Checks whether the userid is a profile of the current user.
Amith Yamasani734983f2014-03-04 16:48:05 -0800352 */
Kenny Guy2a764942014-04-02 13:29:20 +0100353 private boolean isCurrentProfileLocked(int userId) {
Kenny Guyf4824a02014-04-02 19:17:41 +0100354 if (userId == mCurrentUser) return true;
Kenny Guy2a764942014-04-02 13:29:20 +0100355 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
356 if (mService.mCurrentProfileIds[i] == userId) return true;
Amith Yamasani734983f2014-03-04 16:48:05 -0800357 }
358 return false;
359 }
360
361 boolean okToShowLocked(ActivityRecord r) {
Kenny Guy2a764942014-04-02 13:29:20 +0100362 return isCurrentProfileLocked(r.userId)
Craig Mautner5962b122012-10-05 14:45:52 -0700363 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
364 }
365
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700366 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800367 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700368 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
369 if (r != null) {
370 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800371 }
372 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700373 return null;
374 }
375
376 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800377 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
378 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800379 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800380 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
381 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800382 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800383 return r;
384 }
385 }
386 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700387 return null;
388 }
389
390 /**
391 * This is a simplified version of topRunningActivityLocked that provides a number of
392 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800393 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700394 * @param token If non-null, any history records matching this token will be skipped.
395 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800396 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700397 * @return Returns the HistoryRecord of the next activity on the stack.
398 */
399 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800400 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
401 TaskRecord task = mTaskHistory.get(taskNdx);
402 if (task.taskId == taskId) {
403 continue;
404 }
405 ArrayList<ActivityRecord> activities = task.mActivities;
406 for (int i = activities.size() - 1; i >= 0; --i) {
407 final ActivityRecord r = activities.get(i);
408 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800409 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800410 return r;
411 }
412 }
413 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700414 return null;
415 }
416
Craig Mautner8849a5e2013-04-02 16:41:03 -0700417 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700418 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
419 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700420 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
421 final ActivityRecord r = activities.get(activityNdx);
422 if (!r.finishing) {
423 return r;
424 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700425 }
426 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700427 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700428 }
429
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700430 final TaskRecord topTask() {
431 final int size = mTaskHistory.size();
432 if (size > 0) {
433 return mTaskHistory.get(size - 1);
434 }
435 return null;
436 }
437
Craig Mautnerd2328952013-03-05 12:46:26 -0800438 TaskRecord taskForIdLocked(int id) {
439 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
440 final TaskRecord task = mTaskHistory.get(taskNdx);
441 if (task.taskId == id) {
442 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800443 }
444 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700445 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700446 }
447
Craig Mautnerd2328952013-03-05 12:46:26 -0800448 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800449 final ActivityRecord r = ActivityRecord.forToken(token);
Craig Mautnerd2328952013-03-05 12:46:26 -0800450 if (r != null) {
451 final TaskRecord task = r.task;
Dianne Hackborn925e2b32014-09-03 16:40:49 -0700452 if (task != null && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Craig Mautnerd2328952013-03-05 12:46:26 -0800453 if (task.stack != this) Slog.w(TAG,
454 "Illegal state! task does not point to stack it is in.");
455 return r;
456 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800457 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800458 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800459 }
460
Craig Mautner2420ead2013-04-01 17:13:20 -0700461 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700462 final boolean hadit = mLRUActivities.remove(r);
463 mLRUActivities.add(r);
464 return hadit;
465 }
466
Craig Mautnerde4ef022013-04-07 19:01:33 -0700467 final boolean isHomeStack() {
468 return mStackId == HOME_STACK_ID;
469 }
470
Craig Mautnere0a38842013-12-16 16:14:02 -0800471 final boolean isOnHomeDisplay() {
472 return isAttached() &&
473 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
474 }
475
Craig Mautner299f9602015-01-26 09:47:33 -0800476 final void moveToFront(String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800477 if (isAttached()) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800478 if (isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -0800479 mStackSupervisor.moveHomeStack(isHomeStack(), reason);
Craig Mautnere0a38842013-12-16 16:14:02 -0800480 }
Craig Mautnerfefeef82014-01-10 08:40:23 -0800481 mStacks.remove(this);
482 mStacks.add(this);
Craig Mautner6b904ef2014-12-17 15:45:03 -0800483 final TaskRecord task = topTask();
484 if (task != null) {
485 mWindowManager.moveTaskToTop(task.taskId);
486 }
Craig Mautner4a1cb222013-12-04 16:14:06 -0800487 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800488 }
489
490 final boolean isAttached() {
491 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800492 }
493
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700494 /**
495 * Returns the top activity in any existing task matching the given
496 * Intent. Returns null if no such task is found.
497 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700498 ActivityRecord findTaskLocked(ActivityRecord target) {
499 Intent intent = target.intent;
500 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700501 ComponentName cls = intent.getComponent();
502 if (info.targetActivity != null) {
503 cls = new ComponentName(info.packageName, info.targetActivity);
504 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700505 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700506 boolean isDocument = intent != null & intent.isDocument();
507 // If documentData is non-null then it must match the existing task data.
508 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800509
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700510 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800511 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
512 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700513 if (task.voiceSession != null) {
514 // We never match voice sessions; those always run independently.
515 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": voice session");
516 continue;
517 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700518 if (task.userId != userId) {
519 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700520 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700521 continue;
522 }
Craig Mautner000f0022013-02-26 15:04:29 -0800523 final ActivityRecord r = task.getTopActivity();
524 if (r == null || r.finishing || r.userId != userId ||
525 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700526 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800527 continue;
528 }
529
Craig Mautnerd00f4742014-03-12 14:17:26 -0700530 final Intent taskIntent = task.intent;
531 final Intent affinityIntent = task.affinityIntent;
532 final boolean taskIsDocument;
533 final Uri taskDocumentData;
534 if (taskIntent != null && taskIntent.isDocument()) {
535 taskIsDocument = true;
536 taskDocumentData = taskIntent.getData();
537 } else if (affinityIntent != null && affinityIntent.isDocument()) {
538 taskIsDocument = true;
539 taskDocumentData = affinityIntent.getData();
540 } else {
541 taskIsDocument = false;
542 taskDocumentData = null;
543 }
544
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700545 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700546 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700547 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700548 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700549 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
550 if (task.rootAffinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700551 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700552 return r;
553 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700554 } else if (taskIntent != null && taskIntent.getComponent() != null &&
555 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700556 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700557 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800558 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700559 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
560 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800561 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700562 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
563 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700564 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700565 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800566 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700567 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
568 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800569 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700570 } else if (DEBUG_TASKS) {
571 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700572 }
573 }
574
575 return null;
576 }
577
578 /**
579 * Returns the first activity (starting from the top of the stack) that
580 * is the same as the given activity. Returns null if no such activity
581 * is found.
582 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700583 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700584 ComponentName cls = intent.getComponent();
585 if (info.targetActivity != null) {
586 cls = new ComponentName(info.packageName, info.targetActivity);
587 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700588 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700589
Craig Mautner000f0022013-02-26 15:04:29 -0800590 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700591 TaskRecord task = mTaskHistory.get(taskNdx);
Kenny Guy2a764942014-04-02 13:29:20 +0100592 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700593 return null;
594 }
595 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800596 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
597 ActivityRecord r = activities.get(activityNdx);
598 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700599 //Slog.i(TAG, "Found matching class!");
600 //dump();
601 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
602 return r;
603 }
604 }
605 }
606
607 return null;
608 }
609
Amith Yamasani742a6712011-05-04 14:49:28 -0700610 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700611 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700612 */
Craig Mautner93529a42013-10-04 15:03:13 -0700613 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800614 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700615 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800616 }
617 mCurrentUser = userId;
618
619 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800620 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800621 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700622 TaskRecord task = mTaskHistory.get(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100623 if (isCurrentProfileLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700624 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
625 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800626 mTaskHistory.remove(i);
627 mTaskHistory.add(task);
628 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800629 // Use same value for i.
630 } else {
631 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800632 }
633 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700634 if (VALIDATE_TOKENS) {
635 validateAppTokensLocked();
636 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700637 }
638
Craig Mautner2420ead2013-04-01 17:13:20 -0700639 void minimalResumeActivityLocked(ActivityRecord r) {
640 r.state = ActivityState.RESUMED;
641 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
642 + " (starting new instance)");
643 r.stopped = false;
644 mResumedActivity = r;
645 r.task.touchActiveTime();
646 mService.addRecentTaskLocked(r.task);
647 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700648 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700649 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700650 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700651 }
652
Dianne Hackborncee04b52013-07-03 17:01:28 -0700653 private void startLaunchTraces() {
654 if (mFullyDrawnStartTime != 0) {
655 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
656 }
657 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
658 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
659 }
660
661 private void stopFullyDrawnTraceIfNeeded() {
662 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
663 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
664 mFullyDrawnStartTime = 0;
665 }
666 }
667
Craig Mautnere79d42682013-04-01 19:01:53 -0700668 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700669 if (r.displayStartTime == 0) {
670 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
671 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700672 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700673 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700674 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700675 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700676 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700677 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700678 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700679 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800680
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700681 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700682 // Make sure that there is no activity waiting for this to launch.
683 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
684 r.displayStartTime = r.fullyDrawnStartTime = 0;
685 } else {
686 mStackSupervisor.removeTimeoutsForActivityLocked(r);
687 mStackSupervisor.scheduleIdleTimeoutLocked(r);
688 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700689 }
690
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800691 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800692 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800693 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
694 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
695 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
696 activities.get(activityNdx).setSleeping(false);
697 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800698 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800699 if (mPausingActivity != null) {
700 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
701 activityPausedLocked(mPausingActivity.appToken, true);
702 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800703 }
704
Craig Mautner0eea92c2013-05-16 13:35:39 -0700705 /**
706 * @return true if something must be done before going to sleep.
707 */
708 boolean checkReadyForSleepLocked() {
709 if (mResumedActivity != null) {
710 // Still have something resumed; can't sleep until it is paused.
711 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
712 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700713 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700714 return true;
715 }
716 if (mPausingActivity != null) {
717 // Still waiting for something to pause; can't sleep yet.
718 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
719 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800720 }
721
Craig Mautner0eea92c2013-05-16 13:35:39 -0700722 return false;
723 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800724
Craig Mautner0eea92c2013-05-16 13:35:39 -0700725 void goToSleep() {
726 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800727
Craig Mautner0eea92c2013-05-16 13:35:39 -0700728 // Make sure any stopped but visible activities are now sleeping.
729 // This ensures that the activity's onStop() is called.
730 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
731 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
732 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
733 final ActivityRecord r = activities.get(activityNdx);
734 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
735 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800736 }
737 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800738 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700739 }
Craig Mautner59c00972012-07-30 12:10:24 -0700740
Dianne Hackbornd2835932010-12-13 16:28:46 -0800741 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700742 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800743 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700744 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800745 return null;
746 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800747
Winson Chung083baf92014-07-11 10:32:42 -0700748 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700749 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwale37f27182014-12-09 14:02:27 -0800750 // we can just go ahead and skip taking the screenshot if this is the home stack.
751 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700752 return null;
753 }
754
Winson Chung48a10a52014-08-27 14:36:51 -0700755 int w = mService.mThumbnailWidth;
756 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800757 if (w > 0) {
Wale Ogunwale37f27182014-12-09 14:02:27 -0800758 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tTaking screenshot");
759 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
760 w, h, SCREENSHOT_FORCE_565);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800761 }
Winson Chung376543b2014-05-21 17:43:28 -0700762 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800763 return null;
764 }
765
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700766 /**
767 * Start pausing the currently resumed activity. It is an error to call this if there
768 * is already an activity being paused or there is no resumed activity.
769 *
770 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
771 * @param uiSleeping True if this is happening with the user interface going to sleep (the
772 * screen turning off).
773 * @param resuming True if this is being called as part of resuming the top activity, so
774 * we shouldn't try to instigate a resume here.
775 * @param dontWait True if the caller does not want to wait for the pause to complete. If
776 * set to true, we will immediately complete the pause here before returning.
777 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
778 * it to tell us when it is done.
779 */
780 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
781 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800782 if (mPausingActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700783 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity);
784 completePauseLocked(false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800785 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700786 ActivityRecord prev = mResumedActivity;
787 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700788 if (!resuming) {
789 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
790 mStackSupervisor.resumeTopActivitiesLocked();
791 }
792 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700793 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800794
795 if (mActivityContainer.mParentActivity == null) {
796 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700797 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800798 }
799
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700800 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
801 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700802 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800803 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700804 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700805 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
806 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700807 prev.state = ActivityState.PAUSING;
808 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700809 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700810 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwaled3e2a082014-11-04 16:09:19 -0800811 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800812 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700813 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700814 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700815
816 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800817
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700818 if (prev.app != null && prev.app.thread != null) {
819 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
820 try {
821 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700822 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700823 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700824 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800825 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700826 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700827 } catch (Exception e) {
828 // Ignore exception, if process died other code will cleanup.
829 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800830 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700831 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700832 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700833 }
834 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800835 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700836 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700837 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700838 }
839
840 // If we are not going to sleep, we want to ensure the device is
841 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700842 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700843 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700844 }
845
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800846 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700847 // Have the window manager pause its key dispatching until the new
848 // activity has started. If we're pausing the activity just because
849 // the screen is being turned off and the UI is sleeping, don't interrupt
850 // key dispatch; the same activity will pick it up again on wakeup.
851 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800852 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700853 } else {
854 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
855 }
856
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700857 if (dontWait) {
858 // If the caller said they don't want to wait for the pause, then complete
859 // the pause now.
860 completePauseLocked(false);
861 return false;
862
863 } else {
864 // Schedule a pause timeout in case the app doesn't respond.
865 // We don't give it much time because this directly impacts the
866 // responsiveness seen by the user.
867 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
868 msg.obj = prev;
869 prev.pauseTime = SystemClock.uptimeMillis();
870 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
871 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
872 return true;
873 }
874
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700875 } else {
876 // This activity failed to schedule the
877 // pause, so just treat it as being paused now.
878 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700879 if (!resuming) {
880 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
881 }
882 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700883 }
884 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700885
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700886 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700887 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800888 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700889
Craig Mautnerd2328952013-03-05 12:46:26 -0800890 final ActivityRecord r = isInStackLocked(token);
891 if (r != null) {
892 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
893 if (mPausingActivity == r) {
894 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
895 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700896 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800897 } else {
898 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
899 r.userId, System.identityHashCode(r), r.shortComponentName,
900 mPausingActivity != null
901 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700902 }
903 }
904 }
905
Craig Mautnera0026042014-04-23 11:45:37 -0700906 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
907 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700908 if (r.state != ActivityState.STOPPING) {
909 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
910 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
911 return;
912 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700913 if (persistentState != null) {
914 r.persistentState = persistentState;
915 mService.notifyTaskPersisterLocked(r.task, false);
916 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700917 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700918 if (icicle != null) {
919 // If icicle is null, this is happening due to a timeout, so we
920 // haven't really saved the state.
921 r.icicle = icicle;
922 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800923 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -0800924 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700925 }
926 if (!r.stopped) {
927 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
928 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
929 r.stopped = true;
930 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -0700931 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
932 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700933 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700934 if (r.finishing) {
935 r.clearOptionsLocked();
936 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700937 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700938 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700939 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700940 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700941 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800942 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700943 }
944 }
945 }
946
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700947 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800948 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700949 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800950
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800951 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700952 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800953 if (prev.finishing) {
954 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700955 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800956 } else if (prev.app != null) {
957 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
958 if (prev.waitingVisible) {
959 prev.waitingVisible = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700960 mStackSupervisor.mWaitingVisibleActivities.remove(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800961 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
962 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800963 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800964 if (prev.configDestroy) {
965 // The previous is being paused because the configuration
966 // is changing, which means it is actually stopping...
967 // To juggle the fact that we are also starting a new
968 // instance right now, we need to first completely stop
969 // the current instance before starting the new one.
970 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700971 destroyActivityLocked(prev, true, "pause-config");
Jose Lima4b6c6692014-08-12 17:41:12 -0700972 } else if (!hasVisibleBehindActivity()) {
973 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -0700974 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700975 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700976 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
977 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800978 // If we already have a few activities waiting to stop,
979 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700980 // them out. Or if r is the last of activity of the last task the stack
981 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800982 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700983 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800984 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -0700985 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800986 }
987 }
988 } else {
989 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
990 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800991 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800992 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700993 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800994
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700995 if (resumeNext) {
996 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
997 if (!mService.isSleepingOrShuttingDown()) {
998 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
999 } else {
1000 mStackSupervisor.checkReadyForSleepLocked();
1001 ActivityRecord top = topStack.topRunningActivityLocked(null);
1002 if (top == null || (prev != null && top != prev)) {
1003 // If there are no more activities available to run,
1004 // do resume anyway to start something. Also if the top
1005 // activity on the stack is not the just paused activity,
1006 // we need to go ahead and resume it to ensure we complete
1007 // an in-flight app switch.
1008 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1009 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001010 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001011 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001012
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001013 if (prev != null) {
1014 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001015
Craig Mautner525f3d92013-05-07 14:01:50 -07001016 if (prev.app != null && prev.cpuTimeAtResume > 0
1017 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001018 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1019 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001020 if (diff > 0) {
1021 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1022 synchronized (bsi) {
1023 BatteryStatsImpl.Uid.Proc ps =
1024 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001025 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001026 if (ps != null) {
1027 ps.addForegroundTimeLocked(diff);
1028 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001029 }
1030 }
1031 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001032 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001033 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001034
1035 // Notfiy when the task stack has changed
1036 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001037 }
1038
1039 /**
1040 * Once we know that we have asked an application to put an activity in
1041 * the resumed state (either by launching it or explicitly telling it),
1042 * this function updates the rest of our state to match that fact.
1043 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001044 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001045 next.idle = false;
1046 next.results = null;
1047 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001048
1049 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1050 ProcessRecord app = next.task.mActivities.get(0).app;
1051 if (app != null && app != mService.mHomeProcess) {
1052 mService.mHomeProcess = app;
1053 }
1054 }
1055
Craig Mautner07566322013-09-26 16:42:55 -07001056 if (next.nowVisible) {
1057 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001058 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001059 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001060
1061 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001062 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001063
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001064 mStackSupervisor.reportResumedActivityLocked(next);
1065
1066 next.resumeKeyDispatchingLocked();
1067 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001068
1069 // Mark the point when the activity is resuming
1070 // TODO: To be more accurate, the mark should be before the onCreate,
1071 // not after the onResume. But for subsequent starts, onResume is fine.
1072 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001073 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001074 } else {
1075 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1076 }
Winson Chung376543b2014-05-21 17:43:28 -07001077
George Mount6ba042b2014-07-28 11:12:28 -07001078 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001079
1080 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1081 // When resuming an activity, require it to call requestVisibleBehind() again.
1082 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1083 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001084 }
1085
Craig Mautnere3a00d72014-04-16 08:31:19 -07001086 private void setVisibile(ActivityRecord r, boolean visible) {
1087 r.visible = visible;
1088 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001089 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001090 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001091 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001092 container.setVisible(visible);
1093 }
1094 }
1095
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001096 // Find the first visible activity above the passed activity and if it is translucent return it
1097 // otherwise return null;
1098 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1099 TaskRecord task = r.task;
1100 if (task == null) {
1101 return null;
1102 }
1103
1104 ActivityStack stack = task.stack;
1105 if (stack == null) {
1106 return null;
1107 }
1108
1109 int stackNdx = mStacks.indexOf(stack);
1110
1111 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1112 int taskNdx = tasks.indexOf(task);
1113
1114 ArrayList<ActivityRecord> activities = task.mActivities;
1115 int activityNdx = activities.indexOf(r) + 1;
1116
1117 final int numStacks = mStacks.size();
1118 while (stackNdx < numStacks) {
1119 tasks = mStacks.get(stackNdx).mTaskHistory;
1120 final int numTasks = tasks.size();
1121 while (taskNdx < numTasks) {
1122 activities = tasks.get(taskNdx).mActivities;
1123 final int numActivities = activities.size();
1124 while (activityNdx < numActivities) {
1125 final ActivityRecord activity = activities.get(activityNdx);
1126 if (!activity.finishing) {
1127 return activity.fullscreen ? null : activity;
1128 }
1129 ++activityNdx;
1130 }
1131 activityNdx = 0;
1132 ++taskNdx;
1133 }
1134 taskNdx = 0;
1135 ++stackNdx;
1136 }
1137
1138 return null;
1139 }
1140
Jose Lima7ba71252014-04-30 12:59:27 -07001141 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1142 // If so, this stack is hidden, otherwise it is visible.
1143 private boolean isStackVisible() {
1144 if (!isAttached()) {
1145 return false;
1146 }
1147
1148 if (mStackSupervisor.isFrontStack(this)) {
1149 return true;
1150 }
1151
Jose Lima729cb232014-05-05 15:59:40 -07001152 /**
1153 * Start at the task above this one and go up, looking for a visible
1154 * fullscreen activity, or a translucent activity that requested the
1155 * wallpaper to be shown behind it.
1156 */
Jose Lima7ba71252014-04-30 12:59:27 -07001157 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
1158 final ArrayList<TaskRecord> tasks = mStacks.get(i).getAllTasks();
1159 for (int taskNdx = 0; taskNdx < tasks.size(); taskNdx++) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001160 final TaskRecord task = tasks.get(taskNdx);
1161 final ArrayList<ActivityRecord> activities = task.mActivities;
Jose Lima7ba71252014-04-30 12:59:27 -07001162 for (int activityNdx = 0; activityNdx < activities.size(); activityNdx++) {
1163 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001164
1165 // Conditions for an activity to obscure the stack we're
1166 // examining:
1167 // 1. Not Finishing AND Visible AND:
1168 // 2. Either:
1169 // - Full Screen Activity OR
1170 // - On top of Home and our stack is NOT home
1171 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001172 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001173 return false;
1174 }
1175 }
1176 }
1177 }
1178
1179 return true;
1180 }
1181
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001182 /**
1183 * Make sure that all activities that need to be visible (that is, they
1184 * currently can be seen by the user) actually are.
1185 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001186 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1187 ActivityRecord top = topRunningActivityLocked(null);
1188 if (top == null) {
1189 return;
1190 }
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001191 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001192 TAG, "ensureActivitiesVisible behind " + top
1193 + " configChanges=0x" + Integer.toHexString(configChanges));
1194
Craig Mautner5eda9b32013-07-02 11:58:16 -07001195 if (mTranslucentActivityWaiting != top) {
1196 mUndrawnActivitiesBelowTopTranslucent.clear();
1197 if (mTranslucentActivityWaiting != null) {
1198 // Call the callback with a timeout indication.
1199 notifyActivityDrawnLocked(null);
1200 mTranslucentActivityWaiting = null;
1201 }
1202 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1203 }
1204
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001205 // If the top activity is not fullscreen, then we need to
1206 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001207 boolean aboveTop = true;
Jose Lima7ba71252014-04-30 12:59:27 -07001208 boolean behindFullscreen = !isStackVisible();
1209
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001210 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001211 final TaskRecord task = mTaskHistory.get(taskNdx);
1212 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001213 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1214 final ActivityRecord r = activities.get(activityNdx);
1215 if (r.finishing) {
1216 continue;
1217 }
1218 if (aboveTop && r != top) {
1219 continue;
1220 }
1221 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001222 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1223 // but must be drawn initially for the animation as though they were visible.
1224 if (!behindFullscreen || r.mLaunchTaskBehind) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001225 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001226 TAG, "Make visible? " + r + " finishing=" + r.finishing
1227 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001228
Craig Mautnerd44711d2013-02-23 11:24:36 -08001229 // First: if this is not the current activity being started, make
1230 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001231 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001232 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001233 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001234
1235 if (r.app == null || r.app.thread == null) {
Craig Mautnerbb742462014-07-07 15:28:55 -07001236 // This activity needs to be visible, but isn't even
1237 // running... get it started, but don't resume it
1238 // at this point.
1239 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
1240 if (r != starting) {
1241 r.startFreezingScreenLocked(r.app, configChanges);
1242 }
1243 if (!r.visible || r.mLaunchTaskBehind) {
1244 if (DEBUG_VISBILITY) Slog.v(
1245 TAG, "Starting and making visible: " + r);
1246 setVisibile(r, true);
1247 }
1248 if (r != starting) {
1249 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001250 }
1251
1252 } else if (r.visible) {
1253 // If this activity is already visible, then there is nothing
1254 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001255 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001256 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001257 try {
George Mount6ba042b2014-07-28 11:12:28 -07001258 if (r.returningOptions != null) {
1259 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1260 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001261 }
1262 } catch(RemoteException e) {
1263 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001264 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001265 // This activity is not currently visible, but is running.
1266 // Tell it to become visible.
1267 r.visible = true;
1268 if (r.state != ActivityState.RESUMED && r != starting) {
1269 // If this activity is paused, tell it
1270 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001271 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001272 TAG, "Making visible and scheduling visibility: " + r);
1273 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001274 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001275 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001276 mUndrawnActivitiesBelowTopTranslucent.add(r);
1277 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001278 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001279 r.sleeping = false;
1280 r.app.pendingUiClean = true;
1281 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1282 r.stopFreezingScreenLocked(false);
1283 } catch (Exception e) {
1284 // Just skip on any failure; we'll make it
1285 // visible when it next restarts.
1286 Slog.w(TAG, "Exception thrown making visibile: "
1287 + r.intent.getComponent(), e);
1288 }
1289 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001290 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001291
Craig Mautnerd44711d2013-02-23 11:24:36 -08001292 // Aggregate current change flags.
1293 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001294
Craig Mautnerd44711d2013-02-23 11:24:36 -08001295 if (r.fullscreen) {
1296 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001297 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1298 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001299 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001300 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001301 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001302 }
1303 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001304 if (DEBUG_VISBILITY) Slog.v(
1305 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1306 + " state=" + r.state
1307 + " behindFullscreen=" + behindFullscreen);
1308 // Now for any activities that aren't visible to the user, make
1309 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001310 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001311 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001312 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001313 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001314 switch (r.state) {
1315 case STOPPING:
1316 case STOPPED:
1317 if (r.app != null && r.app.thread != null) {
1318 if (DEBUG_VISBILITY) Slog.v(
1319 TAG, "Scheduling invisibility: " + r);
1320 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1321 }
1322 break;
1323
1324 case INITIALIZING:
1325 case RESUMED:
1326 case PAUSING:
1327 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001328 // This case created for transitioning activities from
1329 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001330 if (getVisibleBehindActivity() == r) {
1331 releaseBackgroundResources();
Craig Mautneree2e45a2014-06-27 12:10:03 -07001332 } else {
1333 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1334 mStackSupervisor.mStoppingActivities.add(r);
1335 }
1336 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001337 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001338 break;
1339
1340 default:
1341 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001342 }
1343 } catch (Exception e) {
1344 // Just skip on any failure; we'll make it
1345 // visible when it next restarts.
1346 Slog.w(TAG, "Exception thrown making hidden: "
1347 + r.intent.getComponent(), e);
1348 }
1349 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001350 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001351 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001352 }
1353 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001354 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001355
1356 if (mTranslucentActivityWaiting != null &&
1357 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1358 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1359 notifyActivityDrawnLocked(null);
1360 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001361 }
Craig Mautner58547802013-03-05 08:23:53 -08001362
George Mount6ba042b2014-07-28 11:12:28 -07001363 void convertToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001364 mTranslucentActivityWaiting = r;
1365 mUndrawnActivitiesBelowTopTranslucent.clear();
1366 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1367 }
1368
1369 /**
1370 * Called as activities below the top translucent activity are redrawn. When the last one is
1371 * redrawn notify the top activity by calling
1372 * {@link Activity#onTranslucentConversionComplete}.
1373 *
1374 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1375 * occurred and the activity will be notified immediately.
1376 */
1377 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001378 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001379 if ((r == null)
1380 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1381 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1382 // The last undrawn activity below the top has just been drawn. If there is an
1383 // opaque activity at the top, notify it that it can become translucent safely now.
1384 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1385 mTranslucentActivityWaiting = null;
1386 mUndrawnActivitiesBelowTopTranslucent.clear();
1387 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1388
Craig Mautner71dd1b62014-02-18 15:48:52 -08001389 if (waitingActivity != null) {
1390 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1391 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1392 try {
1393 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1394 waitingActivity.appToken, r != null);
1395 } catch (RemoteException e) {
1396 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001397 }
1398 }
1399 }
1400 }
1401
Craig Mautnera61bc652013-10-28 15:43:18 -07001402 /** If any activities below the top running one are in the INITIALIZING state and they have a
1403 * starting window displayed then remove that starting window. It is possible that the activity
1404 * in this state will never resumed in which case that starting window will be orphaned. */
1405 void cancelInitializingActivities() {
1406 final ActivityRecord topActivity = topRunningActivityLocked(null);
1407 boolean aboveTop = true;
1408 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1409 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1410 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1411 final ActivityRecord r = activities.get(activityNdx);
1412 if (aboveTop) {
1413 if (r == topActivity) {
1414 aboveTop = false;
1415 }
1416 continue;
1417 }
1418
1419 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1420 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1421 r.mStartingWindowShown = false;
1422 mWindowManager.removeAppStartingWindow(r.appToken);
1423 }
1424 }
1425 }
1426 }
1427
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001428 /**
1429 * Ensure that the top activity in the stack is resumed.
1430 *
1431 * @param prev The previously resumed activity, for when in the process
1432 * of pausing; can be null to call from elsewhere.
1433 *
1434 * @return Returns true if something is being resumed, or false if
1435 * nothing happened.
1436 */
1437 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001438 return resumeTopActivityLocked(prev, null);
1439 }
1440
1441 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001442 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001443 // Don't even start recursing.
1444 return false;
1445 }
1446
1447 boolean result = false;
1448 try {
1449 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001450 mStackSupervisor.inResumeTopActivity = true;
1451 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1452 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001453 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001454 }
Craig Mautner544efa72014-09-04 16:41:20 -07001455 result = resumeTopActivityInnerLocked(prev, options);
1456 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001457 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001458 }
1459 return result;
1460 }
1461
1462 final boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001463 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1464
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001465 if (!mService.mBooting && !mService.mBooted) {
1466 // Not ready yet!
1467 return false;
1468 }
1469
Craig Mautnerdf88d732014-01-27 09:21:32 -08001470 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001471 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001472 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001473 // Do not resume this stack if its parent is not resumed.
1474 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1475 return false;
1476 }
1477
Craig Mautnera61bc652013-10-28 15:43:18 -07001478 cancelInitializingActivities();
1479
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001480 // Find the first activity that is not finishing.
Craig Mautner94ab4662015-01-20 10:49:22 -08001481 final ActivityRecord next = topRunningActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001482
1483 // Remember how we'll process this pause/resume situation, and ensure
1484 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001485 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1486 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001487
Craig Mautner84984fa2014-06-19 11:19:20 -07001488 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001489 if (next == null) {
1490 // There are no more activities! Let's just start up the
1491 // Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001492 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001493 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001494 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001495 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001496 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1497 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1498 return isOnHomeDisplay() &&
Craig Mautner299f9602015-01-26 09:47:33 -08001499 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "noMoreActivities");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001500 }
1501
1502 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001503
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001504 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001505 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1506 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001507 // Make sure we have executed any pending transitions, since there
1508 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001509 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001510 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001511 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001512 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001513 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001514 return false;
1515 }
1516
Craig Mautner525f3d92013-05-07 14:01:50 -07001517 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001518 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001519 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001520 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001521 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001522 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001523 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001524 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001525 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001526 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001527 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001528 } else if (!isOnHomeDisplay()) {
1529 return false;
1530 } else if (!isHomeStack()){
1531 if (DEBUG_STATES) Slog.d(TAG,
Craig Mautnere0a38842013-12-16 16:14:02 -08001532 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001533 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1534 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
Craig Mautner299f9602015-01-26 09:47:33 -08001535 return mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001536 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001537 }
1538
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001539 // If we are sleeping, and there is no resumed activity, and the top
1540 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001541 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001542 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001543 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001544 // Make sure we have executed any pending transitions, since there
1545 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001546 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001547 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001548 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001549 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001550 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001551 return false;
1552 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001553
1554 // Make sure that the user who owns this activity is started. If not,
1555 // we will just leave it as is because someone should be bringing
1556 // another user's activities to the top of the stack.
1557 if (mService.mStartedUsers.get(next.userId) == null) {
1558 Slog.w(TAG, "Skipping resume of top activity " + next
1559 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001560 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001561 return false;
1562 }
1563
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001564 // The activity may be waiting for stop, but that is no longer
1565 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001566 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001567 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001568 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001569 mStackSupervisor.mWaitingVisibleActivities.remove(next);
louis_chang1110f552014-12-19 17:30:45 +08001570 next.waitingVisible = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001571
1572 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1573
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001574 // If we are currently pausing an activity, then don't do anything
1575 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001576 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001577 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1578 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001579 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001580 return false;
1581 }
1582
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001583 // Okay we are now going to start a switch, to 'next'. We may first
1584 // have to pause the current activity, but this is an important point
1585 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001586 // XXX "App Redirected" dialog is getting too many false positives
1587 // at this point, so turn off for now.
1588 if (false) {
1589 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1590 long now = SystemClock.uptimeMillis();
1591 final boolean inTime = mLastStartedActivity.startTime != 0
1592 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1593 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1594 final int nextUid = next.info.applicationInfo.uid;
1595 if (inTime && lastUid != nextUid
1596 && lastUid != next.launchedFromUid
1597 && mService.checkPermission(
1598 android.Manifest.permission.STOP_APP_SWITCHES,
1599 -1, next.launchedFromUid)
1600 != PackageManager.PERMISSION_GRANTED) {
1601 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1602 } else {
1603 next.startTime = now;
1604 mLastStartedActivity = next;
1605 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001606 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001607 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001608 mLastStartedActivity = next;
1609 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001610 }
Craig Mautner58547802013-03-05 08:23:53 -08001611
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001612 // We need to start pausing the current activity so the top one
1613 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001614 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1615 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001616 if (mResumedActivity != null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001617 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001618 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001619 }
1620 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001621 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1622 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001623 // At this point we want to put the upcoming activity's process
1624 // at the top of the LRU list, since we know we will be needing it
1625 // very soon and it would be a waste to let it get killed if it
1626 // happens to be sitting towards the end.
1627 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001628 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001629 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001630 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001631 return true;
1632 }
1633
Christopher Tated3f175c2012-06-14 14:16:54 -07001634 // If the most recent activity was noHistory but was only stopped rather
1635 // than stopped+finished because the device went to sleep, we need to make
1636 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001637 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001638 !mLastNoHistoryActivity.finishing) {
1639 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1640 " on new resume");
1641 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
1642 null, "no-history", false);
1643 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001644 }
1645
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001646 if (prev != null && prev != next) {
1647 if (!prev.waitingVisible && next != null && !next.nowVisible) {
1648 prev.waitingVisible = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001649 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001650 if (DEBUG_SWITCH) Slog.v(
1651 TAG, "Resuming top, waiting visible to hide: " + prev);
1652 } else {
1653 // The next activity is already visible, so hide the previous
1654 // activity's windows right now so we can show the new one ASAP.
1655 // We only do this if the previous is finishing, which should mean
1656 // it is on top of the one being resumed so hiding it quickly
1657 // is good. Otherwise, we want to do the normal route of allowing
1658 // the resumed activity to be shown so we can decide if the
1659 // previous should actually be hidden depending on whether the
1660 // new one is found to be full-screen or not.
1661 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001662 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001663 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1664 + prev + ", waitingVisible="
1665 + (prev != null ? prev.waitingVisible : null)
1666 + ", nowVisible=" + next.nowVisible);
1667 } else {
1668 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
1669 + prev + ", waitingVisible="
1670 + (prev != null ? prev.waitingVisible : null)
1671 + ", nowVisible=" + next.nowVisible);
1672 }
1673 }
1674 }
1675
Dianne Hackborne7f97212011-02-24 14:40:20 -08001676 // Launching this app's activity, make sure the app is no longer
1677 // considered stopped.
1678 try {
1679 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001680 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001681 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001682 } catch (IllegalArgumentException e) {
1683 Slog.w(TAG, "Failed trying to unstop package "
1684 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001685 }
1686
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001687 // We are starting up the next activity, so tell the window manager
1688 // that the previous one will be hidden soon. This way it can know
1689 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001690 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001691 if (prev != null) {
1692 if (prev.finishing) {
1693 if (DEBUG_TRANSITION) Slog.v(TAG,
1694 "Prepare close transition: prev=" + prev);
1695 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001696 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001697 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001698 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001699 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001700 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1701 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001702 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001703 mWindowManager.setAppWillBeHidden(prev.appToken);
1704 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001705 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001706 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001707 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001708 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001709 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001710 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001711 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001712 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001713 : next.mLaunchTaskBehind
1714 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1715 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001716 }
1717 }
1718 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001719 mWindowManager.setAppWillBeHidden(prev.appToken);
1720 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001721 }
Craig Mautner967212c2013-04-13 21:10:58 -07001722 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001723 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001724 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001725 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001726 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001727 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001728 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001729 }
1730 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001731
1732 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001733 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001734 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1735 if (opts != null) {
1736 resumeAnimOptions = opts.toBundle();
1737 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001738 next.applyOptionsLocked();
1739 } else {
1740 next.clearOptionsLocked();
1741 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001742
Craig Mautnercf910b02013-04-23 11:23:27 -07001743 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001744 if (next.app != null && next.app.thread != null) {
1745 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1746
1747 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001748 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001749
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001750 // schedule launch ticks to collect information about slow apps.
1751 next.startLaunchTickingLocked();
1752
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001753 ActivityRecord lastResumedActivity =
1754 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001755 ActivityState lastState = next.state;
1756
1757 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001758
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001759 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001760 next.state = ActivityState.RESUMED;
1761 mResumedActivity = next;
1762 next.task.touchActiveTime();
Craig Mautnerd2328952013-03-05 12:46:26 -08001763 mService.addRecentTaskLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001764 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001765 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001766 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001767
1768 // Have the window manager re-evaluate the orientation of
1769 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001770 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001771 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001772 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001773 mService.mConfiguration,
1774 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1775 if (config != null) {
1776 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001777 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001778 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001779 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001780
Craig Mautner525f3d92013-05-07 14:01:50 -07001781 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001782 // The configuration update wasn't able to keep the existing
1783 // instance of the activity, and instead started a new one.
1784 // We should be all done, but let's just make sure our activity
1785 // is still at the top and schedule another run if something
1786 // weird happened.
1787 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001788 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001789 "Activity config changed during resume: " + next
1790 + ", new next: " + nextNext);
1791 if (nextNext != next) {
1792 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001793 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001794 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001795 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001796 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001797 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001798 return true;
1799 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001800 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001801 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001802 }
Craig Mautner58547802013-03-05 08:23:53 -08001803
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001804 try {
1805 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001806 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001807 if (a != null) {
1808 final int N = a.size();
1809 if (!next.finishing && N > 0) {
1810 if (DEBUG_RESULTS) Slog.v(
1811 TAG, "Delivering results to " + next
1812 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001813 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001814 }
1815 }
1816
1817 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001818 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001819 }
1820
Craig Mautner299f9602015-01-26 09:47:33 -08001821 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
1822 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001823
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001824 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001825 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001826 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001827 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001828 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001829 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001830 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001831
Craig Mautner0eea92c2013-05-16 13:35:39 -07001832 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001833
Craig Mautnerac6f8432013-07-17 13:24:59 -07001834 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001835 } catch (Exception e) {
1836 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001837 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1838 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001839 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001840 if (lastStack != null) {
1841 lastStack.mResumedActivity = lastResumedActivity;
1842 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001843 Slog.i(TAG, "Restarting because process died: " + next);
1844 if (!next.hasBeenLaunched) {
1845 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001846 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1847 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001848 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001849 next.appToken, next.packageName, next.theme,
1850 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001851 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1852 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001853 }
George Mount2c92c972014-03-20 09:38:23 -07001854 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001855 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001856 return true;
1857 }
1858
1859 // From this point on, if something goes wrong there is no way
1860 // to recover the activity.
1861 try {
1862 next.visible = true;
1863 completeResumeLocked(next);
1864 } catch (Exception e) {
1865 // If any exception gets thrown, toss away this
1866 // activity and try the next one.
1867 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001868 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001869 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001870 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001871 return true;
1872 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001873 next.stopped = false;
1874
1875 } else {
1876 // Whoops, need to restart this activity!
1877 if (!next.hasBeenLaunched) {
1878 next.hasBeenLaunched = true;
1879 } else {
1880 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001881 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001882 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001883 mService.compatibilityInfoForPackageLocked(
1884 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001885 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001886 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001887 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001888 }
1889 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1890 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001891 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001892 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001893 }
1894
Craig Mautnercf910b02013-04-23 11:23:27 -07001895 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001896 return true;
1897 }
1898
riddle_hsuc215a4f2014-12-27 12:10:45 +08001899 private TaskRecord getNextTask(TaskRecord targetTask) {
1900 final int index = mTaskHistory.indexOf(targetTask);
1901 if (index >= 0) {
1902 final int numTasks = mTaskHistory.size();
1903 for (int i = index + 1; i < numTasks; ++i) {
1904 TaskRecord task = mTaskHistory.get(i);
1905 if (task.userId == targetTask.userId) {
1906 return task;
1907 }
1908 }
1909 }
1910 return null;
1911 }
1912
Craig Mautnerac6f8432013-07-17 13:24:59 -07001913 private void insertTaskAtTop(TaskRecord task) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08001914 // If the moving task is over home stack, transfer its return type to next task
1915 if (task.isOverHomeStack()) {
1916 final TaskRecord nextTask = getNextTask(task);
1917 if (nextTask != null) {
1918 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
1919 }
1920 }
1921
Craig Mautner9c85c202013-10-04 20:11:26 -07001922 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08001923 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001924 if (isOnHomeDisplay()) {
1925 ActivityStack lastStack = mStackSupervisor.getLastStack();
1926 final boolean fromHome = lastStack.isHomeStack();
1927 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07001928 task.setTaskToReturnTo(fromHome
1929 ? lastStack.topTask() == null
1930 ? HOME_ACTIVITY_TYPE
1931 : lastStack.topTask().taskType
1932 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08001933 }
1934 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07001935 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07001936 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001937
Craig Mautnerac6f8432013-07-17 13:24:59 -07001938 mTaskHistory.remove(task);
1939 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07001940 int taskNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01001941 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001942 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07001943 while (--taskNdx >= 0) {
1944 if (!isCurrentProfileLocked(mTaskHistory.get(taskNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001945 break;
1946 }
1947 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001948 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001949 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001950 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001951 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001952 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08001953
Craig Mautner8849a5e2013-04-02 16:41:03 -07001954 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001955 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001956 TaskRecord rTask = r.task;
1957 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07001958 // mLaunchTaskBehind tasks get placed at the back of the task stack.
1959 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08001960 // Last activity in task had been removed or ActivityManagerService is reusing task.
1961 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08001962 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07001963 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001964 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08001965 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001966 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001967 if (!newTask) {
1968 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08001969 boolean startIt = true;
1970 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1971 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08001972 if (task.getTopActivity() == null) {
1973 // All activities in task are finishing.
1974 continue;
1975 }
Craig Mautner70a86932013-02-28 22:37:44 -08001976 if (task == r.task) {
1977 // Here it is! Now, if this is not yet visible to the
1978 // user, then just add it without starting; it will
1979 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08001980 if (!startIt) {
1981 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
1982 + task, new RuntimeException("here").fillInStackTrace());
1983 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001984 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001985 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
1986 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001987 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07001988 r.userId, r.info.configChanges, task.voiceSession != null,
1989 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001990 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001991 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001992 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001993 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001994 return;
1995 }
1996 break;
Craig Mautner70a86932013-02-28 22:37:44 -08001997 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001998 startIt = false;
1999 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002000 }
2001 }
2002
2003 // Place a new activity at top of stack, so it is next to interact
2004 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002005
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002006 // If we are not placing the new activity frontmost, we do not want
2007 // to deliver the onUserLeaving callback to the actual frontmost
2008 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002009 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002010 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08002011 if (DEBUG_USER_LEAVING) Slog.v(TAG,
2012 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002013 }
Craig Mautner70a86932013-02-28 22:37:44 -08002014
2015 task = r.task;
2016
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002017 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002018 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002019 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002020 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002021 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002022
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002023 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002024 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002025 // We want to show the starting preview window if we are
2026 // switching to a new task, or the next activity's process is
2027 // not currently running.
2028 boolean showStartingIcon = newTask;
2029 ProcessRecord proc = r.app;
2030 if (proc == null) {
2031 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2032 }
2033 if (proc == null || proc.thread == null) {
2034 showStartingIcon = true;
2035 }
2036 if (DEBUG_TRANSITION) Slog.v(TAG,
2037 "Prepare open transition: starting " + r);
2038 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002039 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002040 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002041 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002042 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002043 ? r.mLaunchTaskBehind
2044 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2045 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002046 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002047 mNoAnimActivities.remove(r);
2048 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002049 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002050 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002051 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002052 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002053 boolean doShow = true;
2054 if (newTask) {
2055 // Even though this activity is starting fresh, we still need
2056 // to reset it to make sure we apply affinities to move any
2057 // existing activities from other tasks in to it.
2058 // If the caller has requested that the target task be
2059 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002060 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002061 resetTaskIfNeededLocked(r, r);
2062 doShow = topRunningNonDelayedActivityLocked(null) == r;
2063 }
George Mount70778d72014-07-01 16:33:45 -07002064 } else if (options != null && new ActivityOptions(options).getAnimationType()
2065 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2066 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002067 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002068 if (r.mLaunchTaskBehind) {
2069 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2070 // tell WindowManager that r is visible even though it is at the back of the stack.
2071 mWindowManager.setAppVisibility(r.appToken, true);
2072 ensureActivitiesVisibleLocked(null, 0);
2073 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002074 // Figure out if we are transitioning from another activity that is
2075 // "has the same starting icon" as the next one. This allows the
2076 // window manager to keep the previous window it had previously
2077 // created, if it still had one.
2078 ActivityRecord prev = mResumedActivity;
2079 if (prev != null) {
2080 // We don't want to reuse the previous starting preview if:
2081 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002082 if (prev.task != r.task) {
2083 prev = null;
2084 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002085 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002086 else if (prev.nowVisible) {
2087 prev = null;
2088 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002089 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002090 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002091 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002092 mService.compatibilityInfoForPackageLocked(
2093 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002094 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002095 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002096 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002097 }
2098 } else {
2099 // If this is the first activity, don't do any fancy animations,
2100 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002101 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002102 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002103 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002104 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002105 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002106 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002107 }
2108 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002109 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002110 }
2111
2112 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002113 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002114 }
2115 }
2116
Dianne Hackbornbe707852011-11-11 14:32:10 -08002117 final void validateAppTokensLocked() {
2118 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002119 mValidateAppTokens.ensureCapacity(numActivities());
2120 final int numTasks = mTaskHistory.size();
2121 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2122 TaskRecord task = mTaskHistory.get(taskNdx);
2123 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002124 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002125 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002126 }
Craig Mautner000f0022013-02-26 15:04:29 -08002127 TaskGroup group = new TaskGroup();
2128 group.taskId = task.taskId;
2129 mValidateAppTokens.add(group);
2130 final int numActivities = activities.size();
2131 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2132 final ActivityRecord r = activities.get(activityNdx);
2133 group.tokens.add(r.appToken);
2134 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002135 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002136 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002137 }
2138
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002139 /**
2140 * Perform a reset of the given task, if needed as part of launching it.
2141 * Returns the new HistoryRecord at the top of the task.
2142 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002143 /**
2144 * Helper method for #resetTaskIfNeededLocked.
2145 * We are inside of the task being reset... we'll either finish this activity, push it out
2146 * for another task, or leave it as-is.
2147 * @param task The task containing the Activity (taskTop) that might be reset.
2148 * @param forceReset
2149 * @return An ActivityOptions that needs to be processed.
2150 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002151 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002152 ActivityOptions topOptions = null;
2153
2154 int replyChainEnd = -1;
2155 boolean canMoveOptions = true;
2156
2157 // We only do this for activities that are not the root of the task (since if we finish
2158 // the root, we may no longer have the task!).
2159 final ArrayList<ActivityRecord> activities = task.mActivities;
2160 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002161 final int rootActivityNdx = task.findEffectiveRootIndex();
2162 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002163 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002164 if (target.frontOfTask)
2165 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002166
2167 final int flags = target.info.flags;
2168 final boolean finishOnTaskLaunch =
2169 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2170 final boolean allowTaskReparenting =
2171 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2172 final boolean clearWhenTaskReset =
2173 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2174
2175 if (!finishOnTaskLaunch
2176 && !clearWhenTaskReset
2177 && target.resultTo != null) {
2178 // If this activity is sending a reply to a previous
2179 // activity, we can't do anything with it now until
2180 // we reach the start of the reply chain.
2181 // XXX note that we are assuming the result is always
2182 // to the previous activity, which is almost always
2183 // the case but we really shouldn't count on.
2184 if (replyChainEnd < 0) {
2185 replyChainEnd = i;
2186 }
2187 } else if (!finishOnTaskLaunch
2188 && !clearWhenTaskReset
2189 && allowTaskReparenting
2190 && target.taskAffinity != null
2191 && !target.taskAffinity.equals(task.affinity)) {
2192 // If this activity has an affinity for another
2193 // task, then we need to move it out of here. We will
2194 // move it as far out of the way as possible, to the
2195 // bottom of the activity stack. This also keeps it
2196 // correctly ordered with any activities we previously
2197 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002198 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002199 final ActivityRecord bottom =
2200 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002201 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002202 if (bottom != null && target.taskAffinity != null
2203 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002204 // If the activity currently at the bottom has the
2205 // same task affinity as the one we are moving,
2206 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002207 targetTask = bottom.task;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002208 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002209 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002210 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002211 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002212 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002213 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002214 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2215 + " out to new task " + target.task);
2216 }
2217
Craig Mautnere3a74d52013-02-22 14:14:58 -08002218 final int targetTaskId = targetTask.taskId;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002219 mWindowManager.setAppGroupId(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002220
Craig Mautner525f3d92013-05-07 14:01:50 -07002221 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002222 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2223 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002224 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002225 if (p.finishing) {
2226 continue;
2227 }
2228
Craig Mautnere3a74d52013-02-22 14:14:58 -08002229 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002230 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002231 topOptions = p.takeOptionsLocked();
2232 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002233 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002234 }
2235 }
2236 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002237 + task + " adding to task=" + targetTask
2238 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002239 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2240 + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002241 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002242 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002243
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002244 mWindowManager.setAppGroupId(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002245 }
2246
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002247 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002248 if (VALIDATE_TOKENS) {
2249 validateAppTokensLocked();
2250 }
2251
2252 replyChainEnd = -1;
2253 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2254 // If the activity should just be removed -- either
2255 // because it asks for it, or the task should be
2256 // cleared -- then finish it and anything that is
2257 // part of its reply chain.
2258 int end;
2259 if (clearWhenTaskReset) {
2260 // In this case, we want to finish this activity
2261 // and everything above it, so be sneaky and pretend
2262 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002263 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002264 } else if (replyChainEnd < 0) {
2265 end = i;
2266 } else {
2267 end = replyChainEnd;
2268 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002269 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002270 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002271 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002272 if (p.finishing) {
2273 continue;
2274 }
2275 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002276 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002277 topOptions = p.takeOptionsLocked();
2278 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002279 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002280 }
2281 }
Craig Mautner58547802013-03-05 08:23:53 -08002282 if (DEBUG_TASKS) Slog.w(TAG,
2283 "resetTaskIntendedTask: calling finishActivity on " + p);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002284 if (finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002285 end--;
2286 srcPos--;
2287 }
2288 }
2289 replyChainEnd = -1;
2290 } else {
2291 // If we were in the middle of a chain, well the
2292 // activity that started it all doesn't want anything
2293 // special, so leave it all as-is.
2294 replyChainEnd = -1;
2295 }
2296 }
2297
2298 return topOptions;
2299 }
2300
2301 /**
2302 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2303 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2304 * @param affinityTask The task we are looking for an affinity to.
2305 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2306 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2307 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2308 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002309 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002310 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002311 int replyChainEnd = -1;
2312 final int taskId = task.taskId;
2313 final String taskAffinity = task.affinity;
2314
2315 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2316 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002317 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2318
2319 // Do not operate on or below the effective root Activity.
2320 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002321 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002322 if (target.frontOfTask)
2323 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002324
2325 final int flags = target.info.flags;
2326 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2327 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2328
2329 if (target.resultTo != null) {
2330 // If this activity is sending a reply to a previous
2331 // activity, we can't do anything with it now until
2332 // we reach the start of the reply chain.
2333 // XXX note that we are assuming the result is always
2334 // to the previous activity, which is almost always
2335 // the case but we really shouldn't count on.
2336 if (replyChainEnd < 0) {
2337 replyChainEnd = i;
2338 }
2339 } else if (topTaskIsHigher
2340 && allowTaskReparenting
2341 && taskAffinity != null
2342 && taskAffinity.equals(target.taskAffinity)) {
2343 // This activity has an affinity for our task. Either remove it if we are
2344 // clearing or move it over to our task. Note that
2345 // we currently punt on the case where we are resetting a
2346 // task that is not at the top but who has activities above
2347 // with an affinity to it... this is really not a normal
2348 // case, and we will need to later pull that task to the front
2349 // and usually at that point we will do the reset and pick
2350 // up those remaining activities. (This only happens if
2351 // someone starts an activity in a new task from an activity
2352 // in a task that is not currently on top.)
2353 if (forceReset || finishOnTaskLaunch) {
2354 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2355 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2356 for (int srcPos = start; srcPos >= i; --srcPos) {
2357 final ActivityRecord p = activities.get(srcPos);
2358 if (p.finishing) {
2359 continue;
2360 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08002361 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "reset", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002362 }
2363 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002364 if (taskInsertionPoint < 0) {
2365 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002366
Craig Mautner77878772013-03-04 19:46:24 -08002367 }
Craig Mautner77878772013-03-04 19:46:24 -08002368
2369 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2370 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2371 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2372 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002373 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002374 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002375 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002376
Craig Mautnere3a74d52013-02-22 14:14:58 -08002377 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2378 + " to stack at " + task,
2379 new RuntimeException("here").fillInStackTrace());
2380 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2381 + " in to resetting task " + task);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002382 mWindowManager.setAppGroupId(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002383 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002384 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002385 if (VALIDATE_TOKENS) {
2386 validateAppTokensLocked();
2387 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002388
2389 // Now we've moved it in to place... but what if this is
2390 // a singleTop activity and we have put it on top of another
2391 // instance of the same activity? Then we drop the instance
2392 // below so it remains singleTop.
2393 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2394 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002395 int targetNdx = taskActivities.indexOf(target);
2396 if (targetNdx > 0) {
2397 ActivityRecord p = taskActivities.get(targetNdx - 1);
2398 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002399 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2400 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002401 }
2402 }
2403 }
2404 }
2405
2406 replyChainEnd = -1;
2407 }
2408 }
Craig Mautner77878772013-03-04 19:46:24 -08002409 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002410 }
2411
Craig Mautner8849a5e2013-04-02 16:41:03 -07002412 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002413 ActivityRecord newActivity) {
2414 boolean forceReset =
2415 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2416 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2417 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2418 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2419 forceReset = true;
2420 }
2421 }
2422
2423 final TaskRecord task = taskTop.task;
2424
2425 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2426 * for remaining tasks. Used for later tasks to reparent to task. */
2427 boolean taskFound = false;
2428
2429 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2430 ActivityOptions topOptions = null;
2431
Craig Mautner77878772013-03-04 19:46:24 -08002432 // Preserve the location for reparenting in the new task.
2433 int reparentInsertionPoint = -1;
2434
Craig Mautnere3a74d52013-02-22 14:14:58 -08002435 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2436 final TaskRecord targetTask = mTaskHistory.get(i);
2437
2438 if (targetTask == task) {
2439 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2440 taskFound = true;
2441 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002442 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2443 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002444 }
2445 }
2446
Craig Mautner70a86932013-02-28 22:37:44 -08002447 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002448 if (taskNdx >= 0) {
2449 do {
2450 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2451 } while (taskTop == null && taskNdx >= 0);
2452 }
Craig Mautner70a86932013-02-28 22:37:44 -08002453
Craig Mautnere3a74d52013-02-22 14:14:58 -08002454 if (topOptions != null) {
2455 // If we got some ActivityOptions from an activity on top that
2456 // was removed from the task, propagate them to the new real top.
2457 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002458 taskTop.updateOptionsLocked(topOptions);
2459 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002460 topOptions.abort();
2461 }
2462 }
2463
2464 return taskTop;
2465 }
2466
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002467 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2468 String resultWho, int requestCode, int resultCode, Intent data) {
2469
2470 if (callingUid > 0) {
2471 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002472 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002473 }
2474
2475 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2476 + " : who=" + resultWho + " req=" + requestCode
2477 + " res=" + resultCode + " data=" + data);
2478 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2479 try {
2480 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2481 list.add(new ResultInfo(resultWho, requestCode,
2482 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002483 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002484 return;
2485 } catch (Exception e) {
2486 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2487 }
2488 }
2489
2490 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2491 }
2492
Craig Mautner299f9602015-01-26 09:47:33 -08002493 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002494 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2495 ActivityRecord next = topRunningActivityLocked(null);
2496 if (next != r) {
2497 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002498 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08002499 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(),
2500 reason + " adjustFocus");
Craig Mautner04f0b702013-10-22 12:31:01 -07002501 }
2502 }
Winson Chung648c83b2014-04-28 15:11:56 -07002503 ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2504 if (top != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002505 mService.setFocusedActivityLocked(top, reason + " adjustTopFocus");
Winson Chung648c83b2014-04-28 15:11:56 -07002506 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002507 }
2508 }
2509
Craig Mautnerf3333272013-04-22 10:55:53 -07002510 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002511 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2512 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2513 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2514 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002515 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002516 if (DEBUG_STATES) {
2517 Slog.d(TAG, "no-history finish of " + r);
2518 }
2519 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002520 "no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002521 } else {
2522 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2523 + " on stop because we're just sleeping");
2524 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002525 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002526 }
2527
2528 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002529 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002530 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002531 try {
2532 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002533 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2534 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002535 r.state = ActivityState.STOPPING;
2536 if (DEBUG_VISBILITY) Slog.v(
2537 TAG, "Stopping visible=" + r.visible + " for " + r);
2538 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002539 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002540 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002541 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002542 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002543 r.setSleeping(true);
2544 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002545 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002546 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002547 } catch (Exception e) {
2548 // Maybe just ignore exceptions here... if the process
2549 // has crashed, our death notification will clean things
2550 // up.
2551 Slog.w(TAG, "Exception thrown during pause", e);
2552 // Just in case, assume it to be stopped.
2553 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002554 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002555 r.state = ActivityState.STOPPED;
2556 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002557 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002558 }
2559 }
2560 }
2561 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002562
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002563 /**
2564 * @return Returns true if the activity is being finished, false if for
2565 * some reason it is being left as-is.
2566 */
2567 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002568 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002569 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002570 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002571 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002572 + ", result=" + resultCode + ", data=" + resultData
2573 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002574 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002575 return false;
2576 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002577
Craig Mautnerd44711d2013-02-23 11:24:36 -08002578 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002579 return true;
2580 }
2581
Craig Mautnerd2328952013-03-05 12:46:26 -08002582 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002583 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2584 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2585 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2586 ActivityRecord r = activities.get(activityNdx);
2587 if (r.resultTo == self && r.requestCode == requestCode) {
2588 if ((r.resultWho == null && resultWho == null) ||
2589 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2590 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2591 false);
2592 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002593 }
2594 }
2595 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002596 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002597 }
2598
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002599 final void finishTopRunningActivityLocked(ProcessRecord app) {
2600 ActivityRecord r = topRunningActivityLocked(null);
2601 if (r != null && r.app == app) {
2602 // If the top running activity is from this crashing
2603 // process, then terminate it to avoid getting in a loop.
Sungsoo Lim48248c82014-12-24 17:30:00 +09002604 Slog.w(TAG, " Force finishing activity 1 "
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002605 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002606 int taskNdx = mTaskHistory.indexOf(r.task);
2607 int activityNdx = r.task.mActivities.indexOf(r);
Craig Mautnerd2328952013-03-05 12:46:26 -08002608 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002609 // Also terminate any activities below it that aren't yet
2610 // stopped, to avoid a situation where one will get
2611 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002612 --activityNdx;
2613 if (activityNdx < 0) {
2614 do {
2615 --taskNdx;
2616 if (taskNdx < 0) {
2617 break;
2618 }
2619 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2620 } while (activityNdx < 0);
2621 }
2622 if (activityNdx >= 0) {
2623 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002624 if (r.state == ActivityState.RESUMED
2625 || r.state == ActivityState.PAUSING
2626 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002627 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Sungsoo Lim48248c82014-12-24 17:30:00 +09002628 Slog.w(TAG, " Force finishing activity 2 "
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002629 + r.intent.getComponent().flattenToShortString());
Craig Mautnerd2328952013-03-05 12:46:26 -08002630 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "crashed", false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002631 }
2632 }
2633 }
2634 }
2635 }
2636
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002637 final void finishVoiceTask(IVoiceInteractionSession session) {
2638 IBinder sessionBinder = session.asBinder();
2639 boolean didOne = false;
2640 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2641 TaskRecord tr = mTaskHistory.get(taskNdx);
2642 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2643 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2644 ActivityRecord r = tr.mActivities.get(activityNdx);
2645 if (!r.finishing) {
2646 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2647 false);
2648 didOne = true;
2649 }
2650 }
2651 }
2652 }
2653 if (didOne) {
2654 mService.updateOomAdjLocked();
2655 }
2656 }
2657
Craig Mautnerd2328952013-03-05 12:46:26 -08002658 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002659 ArrayList<ActivityRecord> activities = r.task.mActivities;
2660 for (int index = activities.indexOf(r); index >= 0; --index) {
2661 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002662 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002663 break;
2664 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002665 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002666 }
2667 return true;
2668 }
2669
Dianne Hackborn5c607432012-02-28 14:44:19 -08002670 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2671 // send the result
2672 ActivityRecord resultTo = r.resultTo;
2673 if (resultTo != null) {
2674 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2675 + " who=" + r.resultWho + " req=" + r.requestCode
2676 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002677 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002678 if (resultData != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002679 resultData.setContentUserHint(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002680 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002681 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002682 if (r.info.applicationInfo.uid > 0) {
2683 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2684 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002685 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002686 }
2687 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2688 resultData);
2689 r.resultTo = null;
2690 }
2691 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2692
2693 // Make sure this HistoryRecord is not holding on to other resources,
2694 // because clients have remote IPC references to this object so we
2695 // can't assume that will go away and want to avoid circular IPC refs.
2696 r.results = null;
2697 r.pendingResults = null;
2698 r.newIntents = null;
2699 r.icicle = null;
2700 }
2701
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002702 /**
2703 * @return Returns true if this activity has been removed from the history
2704 * list, or false if it is still in the list and will be removed later.
2705 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002706 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2707 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002708 if (r.finishing) {
2709 Slog.w(TAG, "Duplicate finish request for " + r);
2710 return false;
2711 }
2712
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002713 r.makeFinishing();
Craig Mautneraea74a52014-03-08 14:23:10 -08002714 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002715 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002716 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002717 task.taskId, r.shortComponentName, reason);
2718 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002719 final int index = activities.indexOf(r);
2720 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002721 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002722 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002723 // If the caller asked that this activity (and all above it)
2724 // be cleared when the task is reset, don't lose that information,
2725 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002726 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002727 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002728 }
2729 }
2730
2731 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002732
Craig Mautner299f9602015-01-26 09:47:33 -08002733 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002734
Dianne Hackborn5c607432012-02-28 14:44:19 -08002735 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002736
Craig Mautnerde4ef022013-04-07 19:01:33 -07002737 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002738 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002739 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002740 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002741 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002742 ? AppTransition.TRANSIT_TASK_CLOSE
2743 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002744
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002745 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002746 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002747
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002748 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002749 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2750 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002751 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002752 }
2753
Craig Mautneraea74a52014-03-08 14:23:10 -08002754 if (endTask) {
2755 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2756 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002757 } else if (r.state != ActivityState.PAUSING) {
2758 // If the activity is PAUSING, we will complete the finish once
2759 // it is done pausing; else we can just directly finish it here.
2760 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002761 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002762 } else {
2763 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2764 }
2765
2766 return false;
2767 }
2768
Craig Mautnerf3333272013-04-22 10:55:53 -07002769 static final int FINISH_IMMEDIATELY = 0;
2770 static final int FINISH_AFTER_PAUSE = 1;
2771 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002772
Craig Mautnerf3333272013-04-22 10:55:53 -07002773 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002774 // First things first: if this activity is currently visible,
2775 // and the resumed activity is not yet visible, then hold off on
2776 // finishing until the resumed one becomes visible.
2777 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002778 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2779 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002780 if (mStackSupervisor.mStoppingActivities.size() > 3
2781 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002782 // If we already have a few activities waiting to stop,
2783 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002784 // them out. Or if r is the last of activity of the last task the stack
2785 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002786 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002787 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002788 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002789 }
2790 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002791 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2792 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002793 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002794 if (oomAdj) {
2795 mService.updateOomAdjLocked();
2796 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002797 return r;
2798 }
2799
2800 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002801 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002802 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002803 mStackSupervisor.mWaitingVisibleActivities.remove(r);
louis_chang1110f552014-12-19 17:30:45 +08002804 r.waitingVisible = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002805 if (mResumedActivity == r) {
2806 mResumedActivity = null;
2807 }
2808 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002809 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002810 r.state = ActivityState.FINISHING;
2811
2812 if (mode == FINISH_IMMEDIATELY
2813 || prevState == ActivityState.STOPPED
2814 || prevState == ActivityState.INITIALIZING) {
2815 // If this activity is already stopped, we can just finish
2816 // it right now.
Craig Mautnerd163e752014-06-13 17:18:47 -07002817 r.makeFinishing();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002818 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002819 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002820 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002821 }
Craig Mautnerd163e752014-06-13 17:18:47 -07002822 if (DEBUG_CONTAINERS) Slog.d(TAG,
2823 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2824 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002825 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002826 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002827
2828 // Need to go through the full pause cycle to get this
2829 // activity into the stopped state and then finish it.
2830 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002831 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002832 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002833 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002834 return r;
2835 }
2836
Craig Mautneree36c772014-07-16 14:56:05 -07002837 void finishAllActivitiesLocked(boolean immediately) {
2838 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002839 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2840 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2841 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2842 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07002843 noActivitiesInStack = false;
2844 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002845 continue;
2846 }
Craig Mautneree36c772014-07-16 14:56:05 -07002847 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002848 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2849 }
2850 }
Craig Mautneree36c772014-07-16 14:56:05 -07002851 if (noActivitiesInStack) {
2852 mActivityContainer.onTaskListEmptyLocked();
2853 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002854 }
2855
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002856 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
2857 // Basic case: for simple app-centric recents, we need to recreate
2858 // the task if the affinity has changed.
2859 if (srec == null || srec.task.affinity == null ||
2860 !srec.task.affinity.equals(destAffinity)) {
2861 return true;
2862 }
2863 // Document-centric case: an app may be split in to multiple documents;
2864 // they need to re-create their task if this current activity is the root
2865 // of a document, unless simply finishing it will return them to the the
2866 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07002867 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
2868 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002869 // Okay, this activity is at the root of its task. What to do, what to do...
2870 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
2871 // Finishing won't return to an application, so we need to recreate.
2872 return true;
2873 }
2874 // We now need to get the task below it to determine what to do.
2875 int taskIdx = mTaskHistory.indexOf(srec.task);
2876 if (taskIdx <= 0) {
2877 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
2878 return false;
2879 }
2880 if (taskIdx == 0) {
2881 // At the bottom of the stack, nothing to go back to.
2882 return true;
2883 }
2884 TaskRecord prevTask = mTaskHistory.get(taskIdx);
2885 if (!srec.task.affinity.equals(prevTask.affinity)) {
2886 // These are different apps, so need to recreate.
2887 return true;
2888 }
2889 }
2890 return false;
2891 }
2892
Craig Mautnerd2328952013-03-05 12:46:26 -08002893 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002894 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002895 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002896 final TaskRecord task = srec.task;
2897 final ArrayList<ActivityRecord> activities = task.mActivities;
2898 final int start = activities.indexOf(srec);
2899 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002900 return false;
2901 }
2902 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002903 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002904 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002905 final ComponentName dest = destIntent.getComponent();
2906 if (start > 0 && dest != null) {
2907 for (int i = finishTo; i >= 0; i--) {
2908 ActivityRecord r = activities.get(i);
2909 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002910 r.info.name.equals(dest.getClassName())) {
2911 finishTo = i;
2912 parent = r;
2913 foundParentInTask = true;
2914 break;
2915 }
2916 }
2917 }
2918
2919 IActivityController controller = mService.mController;
2920 if (controller != null) {
2921 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2922 if (next != null) {
2923 // ask watcher if this is allowed
2924 boolean resumeOK = true;
2925 try {
2926 resumeOK = controller.activityResuming(next.packageName);
2927 } catch (RemoteException e) {
2928 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07002929 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002930 }
2931
2932 if (!resumeOK) {
2933 return false;
2934 }
2935 }
2936 }
2937 final long origId = Binder.clearCallingIdentity();
2938 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002939 ActivityRecord r = activities.get(i);
2940 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002941 // Only return the supplied result for the first activity finished
2942 resultCode = Activity.RESULT_CANCELED;
2943 resultData = null;
2944 }
2945
2946 if (parent != null && foundParentInTask) {
2947 final int parentLaunchMode = parent.info.launchMode;
2948 final int destIntentFlags = destIntent.getFlags();
2949 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
2950 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
2951 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
2952 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002953 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
2954 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002955 } else {
2956 try {
2957 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
2958 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07002959 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002960 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002961 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborn95465202014-09-15 16:21:55 -07002962 -1, parent.launchedFromUid, 0, null, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002963 foundParentInTask = res == ActivityManager.START_SUCCESS;
2964 } catch (RemoteException e) {
2965 foundParentInTask = false;
2966 }
2967 requestFinishActivityLocked(parent.appToken, resultCode,
2968 resultData, "navigate-up", true);
2969 }
2970 }
2971 Binder.restoreCallingIdentity(origId);
2972 return foundParentInTask;
2973 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002974 /**
2975 * Perform the common clean-up of an activity record. This is called both
2976 * as part of destroyActivityLocked() (when destroying the client-side
2977 * representation) and cleaning things up as a result of its hosting
2978 * processing going away, in which case there is no remaining client-side
2979 * state to destroy so only the cleanup here is needed.
2980 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002981 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
2982 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002983 if (mResumedActivity == r) {
2984 mResumedActivity = null;
2985 }
Craig Mautner1872ce32014-03-28 23:05:42 +00002986 if (mPausingActivity == r) {
2987 mPausingActivity = null;
2988 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002989 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002990
2991 r.configDestroy = false;
2992 r.frozenBeforeDestroy = false;
2993
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002994 if (setState) {
2995 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
2996 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08002997 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07002998 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002999 }
3000
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003001 // Make sure this record is no longer in the pending finishes list.
3002 // This could happen, for example, if we are trimming activities
3003 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003004 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003005 mStackSupervisor.mWaitingVisibleActivities.remove(r);
louis_chang1110f552014-12-19 17:30:45 +08003006 r.waitingVisible = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07003007
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003008 // Remove any pending results.
3009 if (r.finishing && r.pendingResults != null) {
3010 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3011 PendingIntentRecord rec = apr.get();
3012 if (rec != null) {
3013 mService.cancelIntentSenderLocked(rec, false);
3014 }
3015 }
3016 r.pendingResults = null;
3017 }
3018
3019 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003020 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003021 }
3022
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003023 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003024 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003025 if (getVisibleBehindActivity() == r) {
3026 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003027 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003028 }
3029
3030 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003031 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003032 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003033 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003034 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003035 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003036 }
3037
Craig Mautner299f9602015-01-26 09:47:33 -08003038 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003039 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003040 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
3041 r.makeFinishing();
3042 if (DEBUG_ADD_REMOVE) {
3043 RuntimeException here = new RuntimeException("here");
3044 here.fillInStackTrace();
3045 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003046 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003047 r.takeFromHistory();
3048 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003049 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003050 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003051 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003052 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003053 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003054 if (VALIDATE_TOKENS) {
3055 validateAppTokensLocked();
3056 }
Craig Mautner312ba862014-02-10 17:55:01 -08003057 final TaskRecord task = r.task;
3058 if (task != null && task.removeActivity(r)) {
3059 if (DEBUG_STACK) Slog.i(TAG,
3060 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003061 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3062 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003063 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003064 }
Craig Mautner299f9602015-01-26 09:47:33 -08003065 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003066 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003067 cleanUpActivityServicesLocked(r);
3068 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003069 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003070
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003071 /**
3072 * Perform clean-up of service connections in an activity record.
3073 */
3074 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3075 // Throw away any services that have been bound by this activity.
3076 if (r.connections != null) {
3077 Iterator<ConnectionRecord> it = r.connections.iterator();
3078 while (it.hasNext()) {
3079 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003080 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003081 }
3082 r.connections = null;
3083 }
3084 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003085
Craig Mautneree2e45a2014-06-27 12:10:03 -07003086 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003087 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003088 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003089 mHandler.sendMessage(msg);
3090 }
3091
Craig Mautneree2e45a2014-06-27 12:10:03 -07003092 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003093 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003094 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003095 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3096 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3097 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3098 final ActivityRecord r = activities.get(activityNdx);
3099 if (r.finishing) {
3100 continue;
3101 }
3102 if (r.fullscreen) {
3103 lastIsOpaque = true;
3104 }
3105 if (owner != null && r.app != owner) {
3106 continue;
3107 }
3108 if (!lastIsOpaque) {
3109 continue;
3110 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003111 if (r.isDestroyable()) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003112 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3113 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003114 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003115 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003116 activityRemoved = true;
3117 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003118 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003119 }
3120 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003121 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003122 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003123 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003124 }
3125
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003126 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3127 if (r.isDestroyable()) {
3128 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3129 + " resumed=" + mResumedActivity
3130 + " pausing=" + mPausingActivity + " for reason " + reason);
3131 return destroyActivityLocked(r, true, reason);
3132 }
3133 return false;
3134 }
3135
3136 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3137 String reason) {
3138 // Iterate over tasks starting at the back (oldest) first.
3139 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3140 int maxTasks = tasks.size() / 4;
3141 if (maxTasks < 1) {
3142 maxTasks = 1;
3143 }
3144 int numReleased = 0;
3145 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3146 final TaskRecord task = mTaskHistory.get(taskNdx);
3147 if (!tasks.contains(task)) {
3148 continue;
3149 }
3150 if (DEBUG_RELEASE) Slog.d(TAG, "Looking for activities to release in " + task);
3151 int curNum = 0;
3152 final ArrayList<ActivityRecord> activities = task.mActivities;
3153 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3154 final ActivityRecord activity = activities.get(actNdx);
3155 if (activity.app == app && activity.isDestroyable()) {
3156 if (DEBUG_RELEASE) Slog.v(TAG, "Destroying " + activity
3157 + " in state " + activity.state + " resumed=" + mResumedActivity
3158 + " pausing=" + mPausingActivity + " for reason " + reason);
3159 destroyActivityLocked(activity, true, reason);
3160 if (activities.get(actNdx) != activity) {
3161 // Was removed from list, back up so we don't miss the next one.
3162 actNdx--;
3163 }
3164 curNum++;
3165 }
3166 }
3167 if (curNum > 0) {
3168 numReleased += curNum;
3169 maxTasks--;
3170 if (mTaskHistory.get(taskNdx) != task) {
3171 // The entire task got removed, back up so we don't miss the next one.
3172 taskNdx--;
3173 }
3174 }
3175 }
3176 if (DEBUG_RELEASE) Slog.d(TAG, "Done releasing: did " + numReleased + " activities");
3177 return numReleased;
3178 }
3179
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003180 /**
3181 * Destroy the current CLIENT SIDE instance of an activity. This may be
3182 * called both when actually finishing an activity, or when performing
3183 * a configuration switch where we destroy the current client-side object
3184 * but then create a new client-side object for this same HistoryRecord.
3185 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003186 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003187 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003188 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003189 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3190 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003191 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003192 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003193
3194 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003195
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003196 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003197
3198 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003199
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003200 if (hadApp) {
3201 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003202 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003203 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3204 mService.mHeavyWeightProcess = null;
3205 mService.mHandler.sendEmptyMessage(
3206 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3207 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003208 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003209 // Update any services we are bound to that might care about whether
3210 // their client may have activities.
3211 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003212 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003213 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003214 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003215 }
3216 }
3217
3218 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003219
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003220 try {
3221 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003222 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003223 r.configChangeFlags);
3224 } catch (Exception e) {
3225 // We can just ignore exceptions here... if the process
3226 // has crashed, our death notification will clean things
3227 // up.
3228 //Slog.w(TAG, "Exception thrown during finish", e);
3229 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003230 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003231 removedFromHistory = true;
3232 skipDestroy = true;
3233 }
3234 }
3235
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003236 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003237
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003238 // If the activity is finishing, we need to wait on removing it
3239 // from the list to give it a chance to do its cleanup. During
3240 // that time it may make calls back with its token so we need to
3241 // be able to find it on the list and so we don't want to remove
3242 // it from the list yet. Otherwise, we can just immediately put
3243 // it in the destroyed state since we are not removing it from the
3244 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003245 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003246 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3247 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003248 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003249 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003250 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3251 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003252 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003253 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003254 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003255 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003256 }
3257 } else {
3258 // remove this record from the history.
3259 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003260 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003261 removedFromHistory = true;
3262 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003263 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003264 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003265 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003266 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003267 }
3268 }
3269
3270 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003271
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003272 if (!mLRUActivities.remove(r) && hadApp) {
3273 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3274 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003275
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003276 return removedFromHistory;
3277 }
3278
Craig Mautner299f9602015-01-26 09:47:33 -08003279 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003280 final long origId = Binder.clearCallingIdentity();
3281 try {
3282 ActivityRecord r = ActivityRecord.forToken(token);
3283 if (r != null) {
3284 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003285 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003286 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003287
3288 if (isInStackLocked(token) != null) {
3289 if (r.state == ActivityState.DESTROYING) {
3290 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003291 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003292 }
3293 }
Craig Mautner05d29032013-05-03 13:40:13 -07003294 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003295 } finally {
3296 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003297 }
3298 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003299
Jose Lima4b6c6692014-08-12 17:41:12 -07003300 void releaseBackgroundResources() {
3301 if (hasVisibleBehindActivity() &&
3302 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
3303 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautner64ccb702014-10-01 09:38:40 -07003304 if (r == topRunningActivityLocked(null)) {
3305 // Don't release the top activity if it has requested to run behind the next
3306 // activity.
3307 return;
3308 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003309 if (DEBUG_STATES) Slog.d(TAG, "releaseBackgroundResources activtyDisplay=" +
3310 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003311 " thread=" + r.app.thread);
3312 if (r != null && r.app != null && r.app.thread != null) {
3313 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003314 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003315 } catch (RemoteException e) {
3316 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003317 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003318 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003319 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003320 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003321 }
3322 }
3323 }
3324
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003325 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003326 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3327 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003328 if (r != null) {
3329 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003330 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003331 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003332 }
3333 mStackSupervisor.resumeTopActivitiesLocked();
3334 }
3335
Jose Lima4b6c6692014-08-12 17:41:12 -07003336 boolean hasVisibleBehindActivity() {
3337 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003338 }
3339
Jose Lima4b6c6692014-08-12 17:41:12 -07003340 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003341 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003342 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003343 }
3344 }
3345
Jose Lima4b6c6692014-08-12 17:41:12 -07003346 ActivityRecord getVisibleBehindActivity() {
3347 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003348 }
3349
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003350 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3351 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003352 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003353 if (DEBUG_CLEANUP) Slog.v(
3354 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003355 + " with " + i + " entries");
3356 while (i > 0) {
3357 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003358 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003359 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003360 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003361 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003362 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003363 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003364 }
3365 }
3366 }
3367
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003368 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3369 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003370 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3371 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003372 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3373 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003374 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003375 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003376 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3377 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003378
3379 boolean hasVisibleActivities = false;
3380
3381 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003382 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003383 if (DEBUG_CLEANUP) Slog.v(
3384 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003385 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3386 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3387 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3388 final ActivityRecord r = activities.get(activityNdx);
3389 --i;
3390 if (DEBUG_CLEANUP) Slog.v(
3391 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3392 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003393 if (r.visible) {
3394 hasVisibleActivities = true;
3395 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003396 boolean remove;
3397 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3398 // Don't currently have state for the activity, or
3399 // it is finishing -- always remove it.
3400 remove = true;
3401 } else if (r.launchCount > 2 &&
3402 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3403 // We have launched this activity too many times since it was
3404 // able to run, so give up and remove it.
3405 remove = true;
3406 } else {
3407 // The process may be gone, but the activity lives on!
3408 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003409 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003410 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003411 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003412 RuntimeException here = new RuntimeException("here");
3413 here.fillInStackTrace();
3414 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3415 + ": haveState=" + r.haveState
3416 + " stateNotNeeded=" + r.stateNotNeeded
3417 + " finishing=" + r.finishing
3418 + " state=" + r.state, here);
3419 }
3420 if (!r.finishing) {
3421 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3422 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3423 r.userId, System.identityHashCode(r),
3424 r.task.taskId, r.shortComponentName,
3425 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003426 if (r.state == ActivityState.RESUMED) {
3427 mService.updateUsageStats(r, false);
3428 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003429 }
Craig Mautner299f9602015-01-26 09:47:33 -08003430 removeActivityFromHistoryLocked(r, "appDied");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003431
Craig Mautner0247fc82013-02-28 14:32:06 -08003432 } else {
3433 // We have the current state for this activity, so
3434 // it can be restarted later when needed.
3435 if (localLOGV) Slog.v(
3436 TAG, "Keeping entry, setting app to null");
Craig Mautner0247fc82013-02-28 14:32:06 -08003437 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3438 + r);
3439 r.app = null;
3440 r.nowVisible = false;
3441 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003442 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003443 "App died, clearing saved state of " + r);
3444 r.icicle = null;
3445 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003446 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003447
Craig Mautnerd2328952013-03-05 12:46:26 -08003448 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003449 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003450 }
3451 }
3452
3453 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003454 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003455
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003456 final void updateTransitLocked(int transit, Bundle options) {
3457 if (options != null) {
3458 ActivityRecord r = topRunningActivityLocked(null);
3459 if (r != null && r.state != ActivityState.RESUMED) {
3460 r.updateOptionsLocked(options);
3461 } else {
3462 ActivityOptions.abort(options);
3463 }
3464 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003465 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003466 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003467
Craig Mautner21d24a22014-04-23 11:45:37 -07003468 void updateTaskMovement(TaskRecord task, boolean toFront) {
3469 if (task.isPersistable) {
3470 task.mLastTimeMoved = System.currentTimeMillis();
3471 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3472 // recently will be most negative, tasks sent to the bottom before that will be less
3473 // negative. Similarly for recent tasks moved to the top which will be most positive.
3474 if (!toFront) {
3475 task.mLastTimeMoved *= -1;
3476 }
3477 }
3478 }
3479
Craig Mautner84984fa2014-06-19 11:19:20 -07003480 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003481 final int top = mTaskHistory.size() - 1;
3482 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3483 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003484 if (task.taskType == homeStackTaskType) {
3485 if (DEBUG_TASKS || DEBUG_STACK)
3486 Slog.d(TAG, "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003487 mTaskHistory.remove(taskNdx);
3488 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003489 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003490 mWindowManager.moveTaskToTop(task.taskId);
3491 return;
3492 }
3493 }
3494 }
3495
Craig Mautner299f9602015-01-26 09:47:33 -08003496 final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord source, Bundle options,
3497 String reason) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003498 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003499
Craig Mautner11bf9a52013-02-19 14:08:51 -08003500 final int numTasks = mTaskHistory.size();
3501 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003502 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003503 // nothing to do!
Craig Mautner299f9602015-01-26 09:47:33 -08003504 if (source != null &&
3505 (source.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003506 ActivityOptions.abort(options);
3507 } else {
3508 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3509 }
3510 return;
3511 }
3512
3513 // Shift all activities with this task up to the top
3514 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003515 insertTaskAtTop(tr);
Craig Mautner299f9602015-01-26 09:47:33 -08003516 moveToFront(reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003517
3518 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Craig Mautner299f9602015-01-26 09:47:33 -08003519 if (source != null &&
3520 (source.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003521 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003522 ActivityRecord r = topRunningActivityLocked(null);
3523 if (r != null) {
3524 mNoAnimActivities.add(r);
3525 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003526 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003527 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003528 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003529 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003530
Craig Mautner05d29032013-05-03 13:40:13 -07003531 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003532 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003533
3534 if (VALIDATE_TOKENS) {
3535 validateAppTokensLocked();
3536 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003537 }
3538
3539 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003540 * Worker method for rearranging history stack. Implements the function of moving all
3541 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003542 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003543 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003544 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3545 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003546 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003547 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003548 * @return Returns true if the move completed, false if not.
3549 */
Craig Mautner299f9602015-01-26 09:47:33 -08003550 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003551 final TaskRecord tr = taskForIdLocked(taskId);
3552 if (tr == null) {
3553 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3554 return false;
3555 }
3556
3557 Slog.i(TAG, "moveTaskToBack: " + tr);
3558
3559 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003560
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003561 // If we have a watcher, preflight the move before committing to it. First check
3562 // for *other* available tasks, but if none are available, then try again allowing the
3563 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003564 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003565 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003566 if (next == null) {
3567 next = topRunningActivityLocked(null, 0);
3568 }
3569 if (next != null) {
3570 // ask watcher if this is allowed
3571 boolean moveOK = true;
3572 try {
3573 moveOK = mService.mController.activityResuming(next.packageName);
3574 } catch (RemoteException e) {
3575 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003576 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003577 }
3578 if (!moveOK) {
3579 return false;
3580 }
3581 }
3582 }
3583
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003584 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003585 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003586
riddle_hsuc215a4f2014-12-27 12:10:45 +08003587 boolean prevIsHome = false;
3588 if (tr.isOverHomeStack()) {
3589 final TaskRecord nextTask = getNextTask(tr);
3590 if (nextTask != null) {
3591 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3592 } else {
3593 prevIsHome = true;
3594 }
3595 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003596 mTaskHistory.remove(tr);
3597 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003598 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003599
Craig Mautnerc8143c62013-09-03 12:15:57 -07003600 // There is an assumption that moving a task to the back moves it behind the home activity.
3601 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003602 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003603 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3604 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003605 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003606 break;
3607 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003608 if (taskNdx == 1) {
3609 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003610 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003611 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003612 }
3613
Craig Mautner299f9602015-01-26 09:47:33 -08003614 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003615 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003616
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003617 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003618 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003619 }
3620
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003621 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
riddle_hsuc215a4f2014-12-27 12:10:45 +08003622 if (prevIsHome || task == tr && tr.isOverHomeStack()
3623 || numTasks <= 1 && isOnHomeDisplay()) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003624 if (!mService.mBooting && !mService.mBooted) {
3625 // Not ready yet!
3626 return false;
3627 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003628 final int taskToReturnTo = tr.getTaskToReturnTo();
3629 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003630 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003631 }
3632
Craig Mautner05d29032013-05-03 13:40:13 -07003633 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003634 return true;
3635 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003636
Craig Mautner8849a5e2013-04-02 16:41:03 -07003637 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003638 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003639 final Uri data = r.intent.getData();
3640 final String strData = data != null ? data.toSafeString() : null;
3641
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003642 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003643 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003644 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003645 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003646 }
3647
3648 /**
3649 * Make sure the given activity matches the current configuration. Returns
3650 * false if the activity had to be destroyed. Returns true if the
3651 * configuration is the same, or the activity will remain running as-is
3652 * for whatever reason. Ensures the HistoryRecord is updated with the
3653 * correct configuration and all other bookkeeping is handled.
3654 */
3655 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3656 int globalChanges) {
3657 if (mConfigWillChange) {
3658 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3659 "Skipping config check (will change): " + r);
3660 return true;
3661 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003662
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003663 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3664 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003665
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003666 // Short circuit: if the two configurations are the exact same
3667 // object (the common case), then there is nothing to do.
3668 Configuration newConfig = mService.mConfiguration;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003669 if (r.configuration == newConfig && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003670 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3671 "Configuration unchanged in " + r);
3672 return true;
3673 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003674
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003675 // We don't worry about activities that are finishing.
3676 if (r.finishing) {
3677 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3678 "Configuration doesn't matter in finishing " + r);
3679 r.stopFreezingScreenLocked(false);
3680 return true;
3681 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003682
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003683 // Okay we now are going to make this activity have the new config.
3684 // But then we need to figure out how it needs to deal with that.
3685 Configuration oldConfig = r.configuration;
3686 r.configuration = newConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003687
3688 // Determine what has changed. May be nothing, if this is a config
3689 // that has come back from the app after going idle. In that case
3690 // we just want to leave the official config object now in the
3691 // activity and do nothing else.
3692 final int changes = oldConfig.diff(newConfig);
3693 if (changes == 0 && !r.forceNewConfig) {
3694 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3695 "Configuration no differences in " + r);
3696 return true;
3697 }
3698
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003699 // If the activity isn't currently running, just leave the new
3700 // configuration and it will pick that up next time it starts.
3701 if (r.app == null || r.app.thread == null) {
3702 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3703 "Configuration doesn't matter not running " + r);
3704 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003705 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003706 return true;
3707 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003708
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003709 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003710 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3711 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3712 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003713 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003714 + ", newConfig=" + newConfig);
3715 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003716 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003717 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3718 r.configChangeFlags |= changes;
3719 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003720 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003721 if (r.app == null || r.app.thread == null) {
3722 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003723 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003724 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003725 } else if (r.state == ActivityState.PAUSING) {
3726 // A little annoying: we are waiting for this activity to
3727 // finish pausing. Let's not do anything now, but just
3728 // flag that it needs to be restarted when done pausing.
3729 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003730 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003731 r.configDestroy = true;
3732 return true;
3733 } else if (r.state == ActivityState.RESUMED) {
3734 // Try to optimize this case: the configuration is changing
3735 // and we need to restart the top, resumed activity.
3736 // Instead of doing the normal handshaking, just say
3737 // "restart!".
3738 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003739 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003740 relaunchActivityLocked(r, r.configChangeFlags, true);
3741 r.configChangeFlags = 0;
3742 } else {
3743 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003744 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003745 relaunchActivityLocked(r, r.configChangeFlags, false);
3746 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003747 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003748
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003749 // All done... tell the caller we weren't able to keep this
3750 // activity around.
3751 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003752 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003753
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003754 // Default case: the activity can handle this new configuration, so
3755 // hand it over. Note that we don't need to give it the new
3756 // configuration, since we always send configuration changes to all
3757 // process when they happen so it can just use whatever configuration
3758 // it last got.
3759 if (r.app != null && r.app.thread != null) {
3760 try {
3761 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003762 r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003763 } catch (RemoteException e) {
3764 // If process died, whatever.
3765 }
3766 }
3767 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003768
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003769 return true;
3770 }
3771
Craig Mautnerc8143c62013-09-03 12:15:57 -07003772 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003773 int changes, boolean andResume) {
3774 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003775 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003776 if (andResume) {
3777 results = r.results;
3778 newIntents = r.newIntents;
3779 }
3780 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3781 + " with results=" + results + " newIntents=" + newIntents
3782 + " andResume=" + andResume);
3783 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003784 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003785 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003786
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003787 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003788
Craig Mautner34b73df2014-01-12 21:11:08 -08003789 mStackSupervisor.removeChildActivityContainers(r);
3790
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003791 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003792 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3793 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3794 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003795 r.forceNewConfig = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08003796 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
Dianne Hackborn813075a62011-11-14 17:45:19 -08003797 changes, !andResume, new Configuration(mService.mConfiguration));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003798 // Note: don't need to call pauseIfSleepingLocked() here, because
3799 // the caller will only pass in 'andResume' if this activity is
3800 // currently resumed, which implies we aren't sleeping.
3801 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003802 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003803 }
3804
3805 if (andResume) {
3806 r.results = null;
3807 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003808 r.state = ActivityState.RESUMED;
3809 } else {
3810 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3811 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003812 }
3813
3814 return true;
3815 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003816
3817 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003818 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3819 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3820 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3821 final ActivityRecord r = activities.get(activityNdx);
3822 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003823 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003824 }
3825 if (r.fullscreen && !r.finishing) {
3826 return false;
3827 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003828 }
3829 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003830 final ActivityRecord r = ActivityRecord.forToken(token);
3831 if (r == null) {
3832 return false;
3833 }
3834 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3835 + " would have returned true for r=" + r);
3836 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003837 }
3838
3839 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003840 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3841 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3842 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3843 final ActivityRecord r = activities.get(activityNdx);
3844 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003845 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003846 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003847 }
3848 }
3849 }
3850
3851 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3852 boolean didSomething = false;
3853 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003854 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003855 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3856 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3857 int numActivities = activities.size();
3858 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3859 ActivityRecord r = activities.get(activityNdx);
3860 final boolean samePackage = r.packageName.equals(name)
3861 || (name == null && r.userId == userId);
3862 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3863 && (samePackage || r.task == lastTask)
3864 && (r.app == null || evenPersistent || !r.app.persistent)) {
3865 if (!doit) {
3866 if (r.finishing) {
3867 // If this activity is just finishing, then it is not
3868 // interesting as far as something to stop.
3869 continue;
3870 }
3871 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003872 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003873 if (r.isHomeActivity()) {
3874 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
3875 Slog.i(TAG, "Skip force-stop again " + r);
3876 continue;
3877 } else {
3878 homeActivity = r.realActivity;
3879 }
3880 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003881 didSomething = true;
Sungsoo Lim48248c82014-12-24 17:30:00 +09003882 Slog.i(TAG, " Force finishing activity 3 " + r);
Craig Mautner56f52db2013-02-25 10:03:01 -08003883 if (samePackage) {
3884 if (r.app != null) {
3885 r.app.removed = true;
3886 }
3887 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003888 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003889 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003890 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3891 true)) {
3892 // r has been deleted from mActivities, accommodate.
3893 --numActivities;
3894 --activityNdx;
3895 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003896 }
3897 }
3898 }
3899 return didSomething;
3900 }
3901
Dianne Hackborn09233282014-04-30 11:33:59 -07003902 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08003903 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
3904 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003905 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08003906 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08003907 if (task.getTopActivity() == null) {
3908 continue;
3909 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003910 ActivityRecord r = null;
3911 ActivityRecord top = null;
3912 int numActivities = 0;
3913 int numRunning = 0;
3914 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07003915 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07003916 continue;
3917 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003918 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3919 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08003920
Craig Mautneraab647e2013-02-28 16:31:36 -08003921 // Initialize state for next task if needed.
3922 if (top == null || (top.state == ActivityState.INITIALIZING)) {
3923 top = r;
3924 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08003925 }
Craig Mautneraab647e2013-02-28 16:31:36 -08003926
3927 // Add 'r' into the current task.
3928 numActivities++;
3929 if (r.app != null && r.app.thread != null) {
3930 numRunning++;
3931 }
3932
3933 if (localLOGV) Slog.v(
3934 TAG, r.intent.getComponent().flattenToShortString()
3935 + ": task=" + r.task);
3936 }
3937
3938 RunningTaskInfo ci = new RunningTaskInfo();
3939 ci.id = task.taskId;
3940 ci.baseActivity = r.intent.getComponent();
3941 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003942 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08003943 if (focusedStack && topTask) {
3944 // Give the latest time to ensure foreground task can be sorted
3945 // at the first, because lastActiveTime of creating task is 0.
3946 ci.lastActiveTime = System.currentTimeMillis();
3947 topTask = false;
3948 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07003949
Craig Mautnerc0ffce52014-07-01 12:38:52 -07003950 if (top.task != null) {
3951 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08003952 }
3953 ci.numActivities = numActivities;
3954 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08003955 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08003956 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003957 }
3958
3959 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08003960 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08003961 if (DEBUG_SWITCH) Slog.d(
3962 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08003963 if (top >= 0) {
3964 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
3965 int activityTop = activities.size() - 1;
3966 if (activityTop > 0) {
3967 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
3968 "unhandled-back", true);
3969 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003970 }
3971 }
3972
Craig Mautner6b74cb52013-09-27 17:02:21 -07003973 /**
3974 * Reset local parameters because an app's activity died.
3975 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07003976 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07003977 */
3978 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07003979 if (mPausingActivity != null && mPausingActivity.app == app) {
3980 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
3981 "App died while pausing: " + mPausingActivity);
3982 mPausingActivity = null;
3983 }
3984 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
3985 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07003986 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07003987 }
3988
Craig Mautner19091252013-10-05 00:03:53 -07003989 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07003990 }
3991
Craig Mautnercae015f2013-02-08 14:31:27 -08003992 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003993 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3994 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3995 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3996 final ActivityRecord r = activities.get(activityNdx);
3997 if (r.app == app) {
Sungsoo Lim48248c82014-12-24 17:30:00 +09003998 Slog.w(TAG, " Force finishing activity 4 "
Craig Mautnerd44711d2013-02-23 11:24:36 -08003999 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004000 // Force the destroy to skip right to removal.
4001 r.app = null;
4002 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004003 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004004 }
4005 }
4006 }
4007
Dianne Hackborn390517b2013-05-30 15:03:32 -07004008 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004009 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004010 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004011 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4012 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004013 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4014 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004015 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07004016 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004017 if (printed) {
4018 header = null;
4019 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004020 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004021 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004022 }
4023
4024 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4025 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4026
4027 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004028 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4029 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004030 }
4031 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004032 final int top = mTaskHistory.size() - 1;
4033 if (top >= 0) {
4034 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4035 int listTop = list.size() - 1;
4036 if (listTop >= 0) {
4037 activities.add(list.get(listTop));
4038 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004039 }
4040 } else {
4041 ItemMatcher matcher = new ItemMatcher();
4042 matcher.build(name);
4043
Craig Mautneraab647e2013-02-28 16:31:36 -08004044 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4045 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4046 if (matcher.match(r1, r1.intent.getComponent())) {
4047 activities.add(r1);
4048 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004049 }
4050 }
4051 }
4052
4053 return activities;
4054 }
4055
4056 ActivityRecord restartPackage(String packageName) {
4057 ActivityRecord starting = topRunningActivityLocked(null);
4058
4059 // All activities that came from the package must be
4060 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004061 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4062 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4063 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4064 final ActivityRecord a = activities.get(activityNdx);
4065 if (a.info.packageName.equals(packageName)) {
4066 a.forceNewConfig = true;
4067 if (starting != null && a == starting && a.visible) {
4068 a.startFreezingScreenLocked(starting.app,
4069 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4070 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004071 }
4072 }
4073 }
4074
4075 return starting;
4076 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004077
Craig Mautner299f9602015-01-26 09:47:33 -08004078 void removeTask(TaskRecord task, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004079 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08004080 mWindowManager.removeTask(task.taskId);
4081 final ActivityRecord r = mResumedActivity;
4082 if (r != null && r.task == task) {
4083 mResumedActivity = null;
4084 }
4085
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004086 final int taskNdx = mTaskHistory.indexOf(task);
4087 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004088 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4089 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4090 if (!nextTask.isOverHomeStack()) {
4091 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4092 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004093 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004094 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004095 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004096
4097 if (task.mActivities.isEmpty()) {
4098 final boolean isVoiceSession = task.voiceSession != null;
4099 if (isVoiceSession) {
4100 try {
4101 task.voiceSession.taskFinished(task.intent, task.taskId);
4102 } catch (RemoteException e) {
4103 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004104 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004105 if (task.autoRemoveFromRecents() || isVoiceSession) {
4106 // Task creator asked to remove this when done, or this task was a voice
4107 // interaction, so it should not remain on the recent tasks list.
4108 mService.mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004109 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004110 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004111 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004112
4113 if (mTaskHistory.isEmpty()) {
4114 if (DEBUG_STACK) Slog.i(TAG, "removeTask: moving to back stack=" + this);
4115 if (isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08004116 mStackSupervisor.moveHomeStack(!isHomeStack(), reason + " leftTaskHistoryEmpty");
Craig Mautner04a0ea62014-01-13 12:51:26 -08004117 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004118 if (mStacks != null) {
4119 mStacks.remove(this);
4120 mStacks.add(0, this);
4121 }
Craig Mautnerd163e752014-06-13 17:18:47 -07004122 mActivityContainer.onTaskListEmptyLocked();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004123 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004124 }
4125
Dianne Hackborn91097de2014-04-04 18:02:06 -07004126 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4127 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4128 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004129 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4130 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004131 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004132 return task;
4133 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004134
4135 ArrayList<TaskRecord> getAllTasks() {
4136 return new ArrayList<TaskRecord>(mTaskHistory);
4137 }
4138
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004139 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004140 task.stack = this;
4141 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07004142 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004143 } else {
4144 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004145 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004146 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004147 if (!moving && task.voiceSession != null) {
4148 try {
4149 task.voiceSession.taskStarted(task.intent, task.taskId);
4150 } catch (RemoteException e) {
4151 }
4152 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004153 }
4154
4155 public int getStackId() {
4156 return mStackId;
4157 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004158
4159 @Override
4160 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004161 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4162 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004163 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004164}