blob: 607af86a4a9882762eae5409a28518c9d0077622 [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner0eea92c2013-05-16 13:35:39 -070019import static com.android.server.am.ActivityManagerService.TAG;
20import static com.android.server.am.ActivityManagerService.localLOGV;
21import static com.android.server.am.ActivityManagerService.DEBUG_CLEANUP;
22import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
23import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
24import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
25import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
26import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
27import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
28import static com.android.server.am.ActivityManagerService.DEBUG_TRANSITION;
29import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
30import static com.android.server.am.ActivityManagerService.DEBUG_VISBILITY;
31import static com.android.server.am.ActivityManagerService.VALIDATE_TOKENS;
32
Craig Mautner84984fa2014-06-19 11:19:20 -070033import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
34import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070035
Craig Mautner0eea92c2013-05-16 13:35:39 -070036import static com.android.server.am.ActivityStackSupervisor.DEBUG_ADD_REMOVE;
37import static com.android.server.am.ActivityStackSupervisor.DEBUG_APP;
Craig Mautnerd163e752014-06-13 17:18:47 -070038import static com.android.server.am.ActivityStackSupervisor.DEBUG_CONTAINERS;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070039import static com.android.server.am.ActivityStackSupervisor.DEBUG_RELEASE;
Craig Mautner0eea92c2013-05-16 13:35:39 -070040import static com.android.server.am.ActivityStackSupervisor.DEBUG_SAVED_STATE;
Winson Chung376543b2014-05-21 17:43:28 -070041import static com.android.server.am.ActivityStackSupervisor.DEBUG_SCREENSHOTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070042import static com.android.server.am.ActivityStackSupervisor.DEBUG_STATES;
Craig Mautnerde4ef022013-04-07 19:01:33 -070043import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
44
Dianne Hackborn89ad4562014-08-24 16:45:38 -070045import android.util.ArraySet;
Dianne Hackborn91097de2014-04-04 18:02:06 -070046import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080047import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070048import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070049import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080050import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080051import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080052import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080053import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070054import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055
56import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070057import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070058import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080060import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080062import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070067import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080068import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080069import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070070import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070071import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080072import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070073import android.os.Handler;
74import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090075import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070077import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078import android.os.RemoteException;
79import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080080import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070081import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070082import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070083import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070084import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070085import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070086
Craig Mautnercae015f2013-02-08 14:31:27 -080087import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080088import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070089import java.lang.ref.WeakReference;
90import java.util.ArrayList;
91import java.util.Iterator;
92import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080093import java.util.Objects;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070094
95/**
96 * State and management of a single stack of activities.
97 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070098final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080099
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700100 // Ticks during which we check progress while waiting for an app to launch.
101 static final int LAUNCH_TICK = 500;
102
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700103 // How long we wait until giving up on the last activity to pause. This
104 // is short because it directly impacts the responsiveness of starting the
105 // next activity.
106 static final int PAUSE_TIMEOUT = 500;
107
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700108 // How long we wait for the activity to tell us it has stopped before
109 // giving up. This is a good amount of time because we really need this
110 // from the application in order to get its saved state.
111 static final int STOP_TIMEOUT = 10*1000;
112
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700113 // How long we wait until giving up on an activity telling us it has
114 // finished destroying itself.
115 static final int DESTROY_TIMEOUT = 10*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800116
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700117 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800118 // disabled.
119 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800120
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700121 // How long between activity launches that we consider safe to not warn
122 // the user about an unexpected activity being launched on top.
123 static final long START_WARN_TIME = 5*1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800124
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700125 // Set to false to disable the preview that is shown while a new activity
126 // is being started.
127 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800128
Craig Mautner5eda9b32013-07-02 11:58:16 -0700129 // How long to wait for all background Activities to redraw following a call to
130 // convertToTranslucent().
131 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
132
Craig Mautnera61bc652013-10-28 15:43:18 -0700133 static final boolean SCREENSHOT_FORCE_565 = ActivityManager.isLowRamDeviceStatic();
John Reck172e87c2013-10-02 16:55:16 -0700134
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700135 enum ActivityState {
136 INITIALIZING,
137 RESUMED,
138 PAUSING,
139 PAUSED,
140 STOPPING,
141 STOPPED,
142 FINISHING,
143 DESTROYING,
144 DESTROYED
145 }
146
147 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700148 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800149 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800150
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700151 /**
152 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800153 * running) activities. It contains #TaskRecord objects.
154 */
155 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<TaskRecord>();
156
157 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800158 * Used for validating app tokens with window manager.
159 */
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800160 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<TaskGroup>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800161
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700162 /**
163 * List of running activities, sorted by recent usage.
164 * The first entry in the list is the least recently used.
165 * It contains HistoryRecord objects.
166 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700167 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700168
169 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700170 * Animations that for the current transition have requested not to
171 * be considered for the transition animation.
172 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700173 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<ActivityRecord>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700174
175 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700176 * When we are in the process of pausing an activity, before starting the
177 * next one, this variable holds the activity that is currently being paused.
178 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800179 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700180
181 /**
182 * This is the last activity that we put into the paused state. This is
183 * used to determine if we need to do an activity transition while sleeping,
184 * when we normally hold the top activity paused.
185 */
186 ActivityRecord mLastPausedActivity = null;
187
188 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700189 * Activities that specify No History must be removed once the user navigates away from them.
190 * If the device goes to sleep with such an activity in the paused state then we save it here
191 * and finish it later if another activity replaces it on wakeup.
192 */
193 ActivityRecord mLastNoHistoryActivity = null;
194
195 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700196 * Current activity that is resumed, or null if there is none.
197 */
198 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800199
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700200 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700201 * This is the last activity that has been started. It is only used to
202 * identify when multiple activities are started at once so that the user
203 * can be warned they may not be in the activity they think they are.
204 */
205 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800206
Craig Mautner5eda9b32013-07-02 11:58:16 -0700207 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
208 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
209 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
210 // Activity in mTranslucentActivityWaiting is notified via
211 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
212 // background activity being drawn then the same call will be made with a true value.
213 ActivityRecord mTranslucentActivityWaiting = null;
Craig Mautnereb8abf72014-07-02 15:04:09 -0700214 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent =
Craig Mautner5eda9b32013-07-02 11:58:16 -0700215 new ArrayList<ActivityRecord>();
216
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700217 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700218 * Set when we know we are going to be calling updateConfiguration()
219 * soon, so want to skip intermediate config checks.
220 */
221 boolean mConfigWillChange;
222
Todd Kennedyaab56db2015-01-30 09:39:53 -0800223 // Whether or not this stack covers the entire screen; by default stacks are full screen
224 boolean mFullscreen = true;
225
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700226 long mLaunchStartTime = 0;
227 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800228
Craig Mautner858d8a62013-04-23 17:08:34 -0700229 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700230
Craig Mautnerc00204b2013-03-05 15:02:14 -0800231 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800232 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800233 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
234 ArrayList<ActivityStack> mStacks;
235 /** The attached Display's unique identifier, or -1 if detached */
236 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800237
Craig Mautner27084302013-03-25 08:05:25 -0700238 /** Run all ActivityStacks through this */
239 final ActivityStackSupervisor mStackSupervisor;
240
Wale Ogunwale60454db2015-01-23 16:05:07 -0800241 Configuration mOverrideConfig;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800242 /** True if the stack was forced to full screen because {@link TaskRecord#mResizeable} is false
243 * and the stack was previously resized. */
244 private boolean mForcedFullscreen = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800245
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700246 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700247 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
248 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
249 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
250 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700251 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700252 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700253 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700254
255 static class ScheduleDestroyArgs {
256 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700257 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700258 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700259 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700260 mReason = reason;
261 }
262 }
263
Zoran Marcetaf958b322012-08-09 20:27:12 +0900264 final Handler mHandler;
265
266 final class ActivityStackHandler extends Handler {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700267 //public Handler() {
268 // if (localLOGV) Slog.v(TAG, "Handler started!");
269 //}
Craig Mautnerc8143c62013-09-03 12:15:57 -0700270 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900271 super(looper);
272 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700273
Zoran Marcetaf958b322012-08-09 20:27:12 +0900274 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700275 public void handleMessage(Message msg) {
276 switch (msg.what) {
277 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800278 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700279 // We don't at this point know if the activity is fullscreen,
280 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800281 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700282 synchronized (mService) {
283 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700284 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700285 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700286 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800287 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700288 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700289 case LAUNCH_TICK_MSG: {
290 ActivityRecord r = (ActivityRecord)msg.obj;
291 synchronized (mService) {
292 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700293 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700294 }
295 }
296 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700297 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800298 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700299 // We don't at this point know if the activity is fullscreen,
300 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800301 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800302 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800303 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800304 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700305 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700306 case STOP_TIMEOUT_MSG: {
307 ActivityRecord r = (ActivityRecord)msg.obj;
308 // We don't at this point know if the activity is fullscreen,
309 // so we need to be conservative and assume it isn't.
310 Slog.w(TAG, "Activity stop timeout for " + r);
311 synchronized (mService) {
312 if (r.isInHistory()) {
313 activityStoppedLocked(r, null, null, null);
314 }
315 }
316 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700317 case DESTROY_ACTIVITIES_MSG: {
318 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
319 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700320 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700321 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700322 } break;
323 case TRANSLUCENT_TIMEOUT_MSG: {
324 synchronized (mService) {
325 notifyActivityDrawnLocked(null);
326 }
327 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700328 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700329 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700330 final ActivityRecord r = getVisibleBehindActivity();
331 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700332 if (r != null) {
333 mService.killAppAtUsersRequest(r.app, null);
334 }
335 }
336 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700337 }
338 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800339 }
340
Craig Mautner34b73df2014-01-12 21:11:08 -0800341 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800342 int count = 0;
343 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
344 count += mTaskHistory.get(taskNdx).mActivities.size();
345 }
346 return count;
347 }
348
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800349 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
350 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800351 mActivityContainer = activityContainer;
352 mStackSupervisor = activityContainer.getOuter();
353 mService = mStackSupervisor.mService;
354 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
355 mWindowManager = mService.mWindowManager;
356 mStackId = activityContainer.mStackId;
357 mCurrentUser = mService.mCurrentUserId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800358 mRecentTasks = recentTasks;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800359 mOverrideConfig = Configuration.EMPTY;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700360 }
Craig Mautner5962b122012-10-05 14:45:52 -0700361
Amith Yamasani734983f2014-03-04 16:48:05 -0800362 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100363 * Checks whether the userid is a profile of the current user.
Amith Yamasani734983f2014-03-04 16:48:05 -0800364 */
Kenny Guy2a764942014-04-02 13:29:20 +0100365 private boolean isCurrentProfileLocked(int userId) {
Kenny Guyf4824a02014-04-02 19:17:41 +0100366 if (userId == mCurrentUser) return true;
Kenny Guy2a764942014-04-02 13:29:20 +0100367 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
368 if (mService.mCurrentProfileIds[i] == userId) return true;
Amith Yamasani734983f2014-03-04 16:48:05 -0800369 }
370 return false;
371 }
372
373 boolean okToShowLocked(ActivityRecord r) {
Kenny Guy2a764942014-04-02 13:29:20 +0100374 return isCurrentProfileLocked(r.userId)
Craig Mautner5962b122012-10-05 14:45:52 -0700375 || (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0;
376 }
377
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700378 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800379 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700380 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
381 if (r != null) {
382 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800383 }
384 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700385 return null;
386 }
387
388 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800389 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
390 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800391 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800392 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
393 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800394 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800395 return r;
396 }
397 }
398 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700399 return null;
400 }
401
402 /**
403 * This is a simplified version of topRunningActivityLocked that provides a number of
404 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800405 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700406 * @param token If non-null, any history records matching this token will be skipped.
407 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800408 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700409 * @return Returns the HistoryRecord of the next activity on the stack.
410 */
411 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800412 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
413 TaskRecord task = mTaskHistory.get(taskNdx);
414 if (task.taskId == taskId) {
415 continue;
416 }
417 ArrayList<ActivityRecord> activities = task.mActivities;
418 for (int i = activities.size() - 1; i >= 0; --i) {
419 final ActivityRecord r = activities.get(i);
420 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800421 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800422 return r;
423 }
424 }
425 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700426 return null;
427 }
428
Craig Mautner8849a5e2013-04-02 16:41:03 -0700429 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700430 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
431 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700432 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
433 final ActivityRecord r = activities.get(activityNdx);
434 if (!r.finishing) {
435 return r;
436 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700437 }
438 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700439 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700440 }
441
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700442 final TaskRecord topTask() {
443 final int size = mTaskHistory.size();
444 if (size > 0) {
445 return mTaskHistory.get(size - 1);
446 }
447 return null;
448 }
449
Craig Mautnerd2328952013-03-05 12:46:26 -0800450 TaskRecord taskForIdLocked(int id) {
451 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
452 final TaskRecord task = mTaskHistory.get(taskNdx);
453 if (task.taskId == id) {
454 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800455 }
456 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700457 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700458 }
459
Craig Mautnerd2328952013-03-05 12:46:26 -0800460 ActivityRecord isInStackLocked(IBinder token) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800461 final ActivityRecord r = ActivityRecord.forToken(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800462 return isInStackLocked(r);
463 }
464
465 ActivityRecord isInStackLocked(ActivityRecord r) {
466 if (r == null) {
467 return null;
468 }
469 final TaskRecord task = r.task;
470 if (task != null && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
471 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800472 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800473 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800474 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800475 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800476 }
477
Craig Mautner2420ead2013-04-01 17:13:20 -0700478 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700479 final boolean hadit = mLRUActivities.remove(r);
480 mLRUActivities.add(r);
481 return hadit;
482 }
483
Craig Mautnerde4ef022013-04-07 19:01:33 -0700484 final boolean isHomeStack() {
485 return mStackId == HOME_STACK_ID;
486 }
487
Craig Mautnere0a38842013-12-16 16:14:02 -0800488 final boolean isOnHomeDisplay() {
489 return isAttached() &&
490 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
491 }
492
Craig Mautner299f9602015-01-26 09:47:33 -0800493 final void moveToFront(String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800494 if (isAttached()) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800495 if (isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -0800496 mStackSupervisor.moveHomeStack(isHomeStack(), reason);
Craig Mautnere0a38842013-12-16 16:14:02 -0800497 }
Craig Mautnerfefeef82014-01-10 08:40:23 -0800498 mStacks.remove(this);
499 mStacks.add(this);
Craig Mautner6b904ef2014-12-17 15:45:03 -0800500 final TaskRecord task = topTask();
501 if (task != null) {
502 mWindowManager.moveTaskToTop(task.taskId);
503 }
Craig Mautner4a1cb222013-12-04 16:14:06 -0800504 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800505 }
506
507 final boolean isAttached() {
508 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800509 }
510
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700511 /**
512 * Returns the top activity in any existing task matching the given
513 * Intent. Returns null if no such task is found.
514 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700515 ActivityRecord findTaskLocked(ActivityRecord target) {
516 Intent intent = target.intent;
517 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700518 ComponentName cls = intent.getComponent();
519 if (info.targetActivity != null) {
520 cls = new ComponentName(info.packageName, info.targetActivity);
521 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700522 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700523 boolean isDocument = intent != null & intent.isDocument();
524 // If documentData is non-null then it must match the existing task data.
525 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800526
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700527 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800528 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
529 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700530 if (task.voiceSession != null) {
531 // We never match voice sessions; those always run independently.
532 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": voice session");
533 continue;
534 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700535 if (task.userId != userId) {
536 // Looking for a different task.
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700537 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700538 continue;
539 }
Craig Mautner000f0022013-02-26 15:04:29 -0800540 final ActivityRecord r = task.getTopActivity();
541 if (r == null || r.finishing || r.userId != userId ||
542 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700543 if (DEBUG_TASKS) Slog.d(TAG, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800544 continue;
545 }
546
Craig Mautnerd00f4742014-03-12 14:17:26 -0700547 final Intent taskIntent = task.intent;
548 final Intent affinityIntent = task.affinityIntent;
549 final boolean taskIsDocument;
550 final Uri taskDocumentData;
551 if (taskIntent != null && taskIntent.isDocument()) {
552 taskIsDocument = true;
553 taskDocumentData = taskIntent.getData();
554 } else if (affinityIntent != null && affinityIntent.isDocument()) {
555 taskIsDocument = true;
556 taskDocumentData = affinityIntent.getData();
557 } else {
558 taskIsDocument = false;
559 taskDocumentData = null;
560 }
561
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700562 if (DEBUG_TASKS) Slog.d(TAG, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700563 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700564 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700565 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700566 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
567 if (task.rootAffinity.equals(target.taskAffinity)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700568 if (DEBUG_TASKS) Slog.d(TAG, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700569 return r;
570 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700571 } else if (taskIntent != null && taskIntent.getComponent() != null &&
572 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700573 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700574 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800575 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700576 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
577 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800578 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700579 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
580 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700581 Objects.equals(documentData, taskDocumentData)) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700582 if (DEBUG_TASKS) Slog.d(TAG, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800583 //dump();
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700584 if (DEBUG_TASKS) Slog.d(TAG, "For Intent " + intent + " bringing to top: "
585 + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800586 return r;
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700587 } else if (DEBUG_TASKS) {
588 Slog.d(TAG, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700589 }
590 }
591
592 return null;
593 }
594
595 /**
596 * Returns the first activity (starting from the top of the stack) that
597 * is the same as the given activity. Returns null if no such activity
598 * is found.
599 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700600 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700601 ComponentName cls = intent.getComponent();
602 if (info.targetActivity != null) {
603 cls = new ComponentName(info.packageName, info.targetActivity);
604 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700605 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700606
Craig Mautner000f0022013-02-26 15:04:29 -0800607 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700608 TaskRecord task = mTaskHistory.get(taskNdx);
Kenny Guy2a764942014-04-02 13:29:20 +0100609 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700610 return null;
611 }
612 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner000f0022013-02-26 15:04:29 -0800613 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
614 ActivityRecord r = activities.get(activityNdx);
615 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700616 //Slog.i(TAG, "Found matching class!");
617 //dump();
618 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
619 return r;
620 }
621 }
622 }
623
624 return null;
625 }
626
Amith Yamasani742a6712011-05-04 14:49:28 -0700627 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700628 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700629 */
Craig Mautner93529a42013-10-04 15:03:13 -0700630 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800631 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700632 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800633 }
634 mCurrentUser = userId;
635
636 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800637 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800638 for (int i = 0; i < index; ) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700639 TaskRecord task = mTaskHistory.get(i);
Kenny Guy2a764942014-04-02 13:29:20 +0100640 if (isCurrentProfileLocked(task.userId)) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700641 if (DEBUG_TASKS) Slog.d(TAG, "switchUserLocked: stack=" + getStackId() +
642 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800643 mTaskHistory.remove(i);
644 mTaskHistory.add(task);
645 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800646 // Use same value for i.
647 } else {
648 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800649 }
650 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700651 if (VALIDATE_TOKENS) {
652 validateAppTokensLocked();
653 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700654 }
655
Craig Mautner2420ead2013-04-01 17:13:20 -0700656 void minimalResumeActivityLocked(ActivityRecord r) {
657 r.state = ActivityState.RESUMED;
658 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + r
659 + " (starting new instance)");
660 r.stopped = false;
661 mResumedActivity = r;
662 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800663 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700664 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700665 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700666 setLaunchTime(r);
Craig Mautner2420ead2013-04-01 17:13:20 -0700667 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700668 }
669
Dianne Hackborncee04b52013-07-03 17:01:28 -0700670 private void startLaunchTraces() {
671 if (mFullyDrawnStartTime != 0) {
672 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
673 }
674 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching", 0);
675 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
676 }
677
678 private void stopFullyDrawnTraceIfNeeded() {
679 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
680 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
681 mFullyDrawnStartTime = 0;
682 }
683 }
684
Craig Mautnere79d42682013-04-01 19:01:53 -0700685 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700686 if (r.displayStartTime == 0) {
687 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
688 if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700689 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700690 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700691 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700692 } else if (mLaunchStartTime == 0) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700693 startLaunchTraces();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700694 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700695 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700696 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800697
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700698 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700699 // Make sure that there is no activity waiting for this to launch.
700 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
701 r.displayStartTime = r.fullyDrawnStartTime = 0;
702 } else {
703 mStackSupervisor.removeTimeoutsForActivityLocked(r);
704 mStackSupervisor.scheduleIdleTimeoutLocked(r);
705 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700706 }
707
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800708 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800709 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800710 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
711 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
712 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
713 activities.get(activityNdx).setSleeping(false);
714 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800715 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800716 if (mPausingActivity != null) {
717 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
718 activityPausedLocked(mPausingActivity.appToken, true);
719 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800720 }
721
Craig Mautner0eea92c2013-05-16 13:35:39 -0700722 /**
723 * @return true if something must be done before going to sleep.
724 */
725 boolean checkReadyForSleepLocked() {
726 if (mResumedActivity != null) {
727 // Still have something resumed; can't sleep until it is paused.
728 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause " + mResumedActivity);
729 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700730 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700731 return true;
732 }
733 if (mPausingActivity != null) {
734 // Still waiting for something to pause; can't sleep yet.
735 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
736 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800737 }
738
Craig Mautner0eea92c2013-05-16 13:35:39 -0700739 return false;
740 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800741
Craig Mautner0eea92c2013-05-16 13:35:39 -0700742 void goToSleep() {
743 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800744
Craig Mautner0eea92c2013-05-16 13:35:39 -0700745 // Make sure any stopped but visible activities are now sleeping.
746 // This ensures that the activity's onStop() is called.
747 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
748 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
749 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
750 final ActivityRecord r = activities.get(activityNdx);
751 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
752 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800753 }
754 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800755 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700756 }
Craig Mautner59c00972012-07-30 12:10:24 -0700757
Dianne Hackbornd2835932010-12-13 16:28:46 -0800758 public final Bitmap screenshotActivities(ActivityRecord who) {
Winson Chung376543b2014-05-21 17:43:28 -0700759 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800760 if (who.noDisplay) {
Winson Chung376543b2014-05-21 17:43:28 -0700761 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800762 return null;
763 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800764
Winson Chung083baf92014-07-11 10:32:42 -0700765 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700766 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800767 // we can just go ahead and skip taking the screenshot if this is the home stack.
768 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700769 return null;
770 }
771
Winson Chung48a10a52014-08-27 14:36:51 -0700772 int w = mService.mThumbnailWidth;
773 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800774 if (w > 0) {
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800775 if (DEBUG_SCREENSHOTS) Slog.d(TAG, "\tTaking screenshot");
776 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
777 w, h, SCREENSHOT_FORCE_565);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800778 }
Winson Chung376543b2014-05-21 17:43:28 -0700779 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800780 return null;
781 }
782
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700783 /**
784 * Start pausing the currently resumed activity. It is an error to call this if there
785 * is already an activity being paused or there is no resumed activity.
786 *
787 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
788 * @param uiSleeping True if this is happening with the user interface going to sleep (the
789 * screen turning off).
790 * @param resuming True if this is being called as part of resuming the top activity, so
791 * we shouldn't try to instigate a resume here.
792 * @param dontWait True if the caller does not want to wait for the pause to complete. If
793 * set to true, we will immediately complete the pause here before returning.
794 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
795 * it to tell us when it is done.
796 */
797 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
798 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800799 if (mPausingActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700800 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity);
801 completePauseLocked(false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800802 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700803 ActivityRecord prev = mResumedActivity;
804 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700805 if (!resuming) {
806 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
807 mStackSupervisor.resumeTopActivitiesLocked();
808 }
809 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700810 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800811
812 if (mActivityContainer.mParentActivity == null) {
813 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700814 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800815 }
816
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700817 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
818 else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700819 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800820 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700821 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700822 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
823 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700824 prev.state = ActivityState.PAUSING;
825 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700826 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700827 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwaled3e2a082014-11-04 16:09:19 -0800828 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800829 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700830 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700831 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700832
833 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800834
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700835 if (prev.app != null && prev.app.thread != null) {
836 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
837 try {
838 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700839 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700840 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700841 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800842 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700843 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700844 } catch (Exception e) {
845 // Ignore exception, if process died other code will cleanup.
846 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800847 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700848 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700849 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700850 }
851 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800852 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700853 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700854 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700855 }
856
857 // If we are not going to sleep, we want to ensure the device is
858 // awake until the next activity is started.
Craig Mautnere11f2b72013-04-01 12:37:17 -0700859 if (!mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700860 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700861 }
862
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800863 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700864 // Have the window manager pause its key dispatching until the new
865 // activity has started. If we're pausing the activity just because
866 // the screen is being turned off and the UI is sleeping, don't interrupt
867 // key dispatch; the same activity will pick it up again on wakeup.
868 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800869 prev.pauseKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700870 } else {
871 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
872 }
873
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700874 if (dontWait) {
875 // If the caller said they don't want to wait for the pause, then complete
876 // the pause now.
877 completePauseLocked(false);
878 return false;
879
880 } else {
881 // Schedule a pause timeout in case the app doesn't respond.
882 // We don't give it much time because this directly impacts the
883 // responsiveness seen by the user.
884 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
885 msg.obj = prev;
886 prev.pauseTime = SystemClock.uptimeMillis();
887 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
888 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
889 return true;
890 }
891
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700892 } else {
893 // This activity failed to schedule the
894 // pause, so just treat it as being paused now.
895 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700896 if (!resuming) {
897 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
898 }
899 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700900 }
901 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700902
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700903 final void activityPausedLocked(IBinder token, boolean timeout) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700904 if (DEBUG_PAUSE) Slog.v(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800905 TAG, "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700906
Craig Mautnerd2328952013-03-05 12:46:26 -0800907 final ActivityRecord r = isInStackLocked(token);
908 if (r != null) {
909 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
910 if (mPausingActivity == r) {
911 if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
912 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700913 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800914 } else {
915 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
916 r.userId, System.identityHashCode(r), r.shortComponentName,
917 mPausingActivity != null
918 ? mPausingActivity.shortComponentName : "(none)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700919 }
920 }
921 }
922
Craig Mautnera0026042014-04-23 11:45:37 -0700923 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
924 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700925 if (r.state != ActivityState.STOPPING) {
926 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
927 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
928 return;
929 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700930 if (persistentState != null) {
931 r.persistentState = persistentState;
932 mService.notifyTaskPersisterLocked(r.task, false);
933 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700934 if (DEBUG_SAVED_STATE) Slog.i(TAG, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700935 if (icicle != null) {
936 // If icicle is null, this is happening due to a timeout, so we
937 // haven't really saved the state.
938 r.icicle = icicle;
939 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800940 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -0800941 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700942 }
943 if (!r.stopped) {
944 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
945 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
946 r.stopped = true;
947 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -0700948 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
949 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700950 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700951 if (r.finishing) {
952 r.clearOptionsLocked();
953 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700954 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700955 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -0700956 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700957 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700958 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -0800959 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700960 }
961 }
962 }
963
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700964 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800965 ActivityRecord prev = mPausingActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700966 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -0800967
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800968 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700969 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800970 if (prev.finishing) {
971 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700972 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800973 } else if (prev.app != null) {
974 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -0800975 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800976 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
977 TAG, "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -0800978 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800979 if (prev.configDestroy) {
980 // The previous is being paused because the configuration
981 // is changing, which means it is actually stopping...
982 // To juggle the fact that we are also starting a new
983 // instance right now, we need to first completely stop
984 // the current instance before starting the new one.
985 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700986 destroyActivityLocked(prev, true, "pause-config");
Jose Lima4b6c6692014-08-12 17:41:12 -0700987 } else if (!hasVisibleBehindActivity()) {
988 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -0700989 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700990 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -0700991 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
992 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800993 // If we already have a few activities waiting to stop,
994 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -0700995 // them out. Or if r is the last of activity of the last task the stack
996 // will be empty and must be cleared immediately.
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800997 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -0700998 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800999 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001000 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001001 }
1002 }
1003 } else {
1004 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
1005 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001006 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001007 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001008 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001009
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001010 if (resumeNext) {
1011 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1012 if (!mService.isSleepingOrShuttingDown()) {
1013 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1014 } else {
1015 mStackSupervisor.checkReadyForSleepLocked();
1016 ActivityRecord top = topStack.topRunningActivityLocked(null);
1017 if (top == null || (prev != null && top != prev)) {
1018 // If there are no more activities available to run,
1019 // do resume anyway to start something. Also if the top
1020 // activity on the stack is not the just paused activity,
1021 // we need to go ahead and resume it to ensure we complete
1022 // an in-flight app switch.
1023 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1024 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001025 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001026 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001027
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001028 if (prev != null) {
1029 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001030
Craig Mautner525f3d92013-05-07 14:01:50 -07001031 if (prev.app != null && prev.cpuTimeAtResume > 0
1032 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001033 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1034 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001035 if (diff > 0) {
1036 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1037 synchronized (bsi) {
1038 BatteryStatsImpl.Uid.Proc ps =
1039 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001040 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001041 if (ps != null) {
1042 ps.addForegroundTimeLocked(diff);
1043 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001044 }
1045 }
1046 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001047 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001048 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001049
1050 // Notfiy when the task stack has changed
1051 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001052 }
1053
1054 /**
1055 * Once we know that we have asked an application to put an activity in
1056 * the resumed state (either by launching it or explicitly telling it),
1057 * this function updates the rest of our state to match that fact.
1058 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001059 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001060 next.idle = false;
1061 next.results = null;
1062 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001063
1064 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1065 ProcessRecord app = next.task.mActivities.get(0).app;
1066 if (app != null && app != mService.mHomeProcess) {
1067 mService.mHomeProcess = app;
1068 }
1069 }
1070
Craig Mautner07566322013-09-26 16:42:55 -07001071 if (next.nowVisible) {
1072 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001073 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001074 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001075
1076 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001077 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001078
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001079 mStackSupervisor.reportResumedActivityLocked(next);
1080
1081 next.resumeKeyDispatchingLocked();
1082 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001083
1084 // Mark the point when the activity is resuming
1085 // TODO: To be more accurate, the mark should be before the onCreate,
1086 // not after the onResume. But for subsequent starts, onResume is fine.
1087 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001088 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001089 } else {
1090 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1091 }
Winson Chung376543b2014-05-21 17:43:28 -07001092
George Mount6ba042b2014-07-28 11:12:28 -07001093 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001094
1095 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1096 // When resuming an activity, require it to call requestVisibleBehind() again.
1097 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1098 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001099 }
1100
Craig Mautnere3a00d72014-04-16 08:31:19 -07001101 private void setVisibile(ActivityRecord r, boolean visible) {
1102 r.visible = visible;
1103 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001104 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001105 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001106 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001107 container.setVisible(visible);
1108 }
1109 }
1110
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001111 // Find the first visible activity above the passed activity and if it is translucent return it
1112 // otherwise return null;
1113 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1114 TaskRecord task = r.task;
1115 if (task == null) {
1116 return null;
1117 }
1118
1119 ActivityStack stack = task.stack;
1120 if (stack == null) {
1121 return null;
1122 }
1123
1124 int stackNdx = mStacks.indexOf(stack);
1125
1126 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1127 int taskNdx = tasks.indexOf(task);
1128
1129 ArrayList<ActivityRecord> activities = task.mActivities;
1130 int activityNdx = activities.indexOf(r) + 1;
1131
1132 final int numStacks = mStacks.size();
1133 while (stackNdx < numStacks) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001134 ActivityStack historyStack = mStacks.get(stackNdx);
1135 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001136 final int numTasks = tasks.size();
1137 while (taskNdx < numTasks) {
1138 activities = tasks.get(taskNdx).mActivities;
1139 final int numActivities = activities.size();
1140 while (activityNdx < numActivities) {
1141 final ActivityRecord activity = activities.get(activityNdx);
1142 if (!activity.finishing) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001143 return historyStack.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001144 }
1145 ++activityNdx;
1146 }
1147 activityNdx = 0;
1148 ++taskNdx;
1149 }
1150 taskNdx = 0;
1151 ++stackNdx;
1152 }
1153
1154 return null;
1155 }
1156
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001157 private ActivityStack getNextVisibleStackLocked() {
1158 ArrayList<ActivityStack> stacks = mStacks;
1159 final ActivityRecord parent = mActivityContainer.mParentActivity;
1160 if (parent != null) {
1161 stacks = parent.task.stack.mStacks;
1162 }
1163 if (stacks != null) {
1164 for (int i = stacks.size() - 1; i >= 0; --i) {
1165 ActivityStack stack = stacks.get(i);
1166 if (stack != this && stack.isStackVisibleLocked()) {
1167 return stack;
1168 }
1169 }
1170 }
1171 return null;
1172 }
1173
Jose Lima7ba71252014-04-30 12:59:27 -07001174 // Checks if any of the stacks above this one has a fullscreen activity behind it.
1175 // If so, this stack is hidden, otherwise it is visible.
Todd Kennedyaab56db2015-01-30 09:39:53 -08001176 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001177 if (!isAttached()) {
1178 return false;
1179 }
1180
1181 if (mStackSupervisor.isFrontStack(this)) {
1182 return true;
1183 }
1184
Jose Lima729cb232014-05-05 15:59:40 -07001185 /**
1186 * Start at the task above this one and go up, looking for a visible
1187 * fullscreen activity, or a translucent activity that requested the
1188 * wallpaper to be shown behind it.
1189 */
Jose Lima7ba71252014-04-30 12:59:27 -07001190 for (int i = mStacks.indexOf(this) + 1; i < mStacks.size(); i++) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001191 ActivityStack stack = mStacks.get(i);
1192 // stack above isn't full screen, so, we assume we're still visible. at some point
1193 // we should look at the stack bounds to see if we're occluded even if the stack
1194 // isn't fullscreen
1195 if (!stack.mFullscreen) {
1196 continue;
1197 }
1198 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1199 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001200 final TaskRecord task = tasks.get(taskNdx);
1201 final ArrayList<ActivityRecord> activities = task.mActivities;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001202 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Jose Lima7ba71252014-04-30 12:59:27 -07001203 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001204
1205 // Conditions for an activity to obscure the stack we're
1206 // examining:
1207 // 1. Not Finishing AND Visible AND:
1208 // 2. Either:
1209 // - Full Screen Activity OR
1210 // - On top of Home and our stack is NOT home
1211 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001212 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001213 return false;
1214 }
1215 }
1216 }
1217 }
1218
1219 return true;
1220 }
1221
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001222 /**
1223 * Make sure that all activities that need to be visible (that is, they
1224 * currently can be seen by the user) actually are.
1225 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001226 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1227 ActivityRecord top = topRunningActivityLocked(null);
1228 if (top == null) {
1229 return;
1230 }
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001231 if (DEBUG_VISBILITY) Slog.v(
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001232 TAG, "ensureActivitiesVisible behind " + top
1233 + " configChanges=0x" + Integer.toHexString(configChanges));
1234
Craig Mautner5eda9b32013-07-02 11:58:16 -07001235 if (mTranslucentActivityWaiting != top) {
1236 mUndrawnActivitiesBelowTopTranslucent.clear();
1237 if (mTranslucentActivityWaiting != null) {
1238 // Call the callback with a timeout indication.
1239 notifyActivityDrawnLocked(null);
1240 mTranslucentActivityWaiting = null;
1241 }
1242 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1243 }
1244
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001245 // If the top activity is not fullscreen, then we need to
1246 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001247 boolean aboveTop = true;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001248 boolean behindFullscreen = !isStackVisibleLocked();
Jose Lima7ba71252014-04-30 12:59:27 -07001249
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001250 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001251 final TaskRecord task = mTaskHistory.get(taskNdx);
1252 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001253 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1254 final ActivityRecord r = activities.get(activityNdx);
1255 if (r.finishing) {
1256 continue;
1257 }
1258 if (aboveTop && r != top) {
1259 continue;
1260 }
1261 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001262 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1263 // but must be drawn initially for the animation as though they were visible.
1264 if (!behindFullscreen || r.mLaunchTaskBehind) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001265 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001266 TAG, "Make visible? " + r + " finishing=" + r.finishing
1267 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001268
Craig Mautnerd44711d2013-02-23 11:24:36 -08001269 // First: if this is not the current activity being started, make
1270 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001271 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001272 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001273 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001274
1275 if (r.app == null || r.app.thread == null) {
Craig Mautnerbb742462014-07-07 15:28:55 -07001276 // This activity needs to be visible, but isn't even
1277 // running... get it started, but don't resume it
1278 // at this point.
1279 if (DEBUG_VISBILITY) Slog.v(TAG, "Start and freeze screen for " + r);
1280 if (r != starting) {
1281 r.startFreezingScreenLocked(r.app, configChanges);
1282 }
1283 if (!r.visible || r.mLaunchTaskBehind) {
1284 if (DEBUG_VISBILITY) Slog.v(
1285 TAG, "Starting and making visible: " + r);
1286 setVisibile(r, true);
1287 }
1288 if (r != starting) {
1289 mStackSupervisor.startSpecificActivityLocked(r, false, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001290 }
1291
1292 } else if (r.visible) {
1293 // If this activity is already visible, then there is nothing
1294 // else to do here.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001295 if (DEBUG_VISBILITY) Slog.v(TAG, "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001296 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001297 try {
George Mount6ba042b2014-07-28 11:12:28 -07001298 if (r.returningOptions != null) {
1299 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1300 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001301 }
1302 } catch(RemoteException e) {
1303 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001304 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001305 // This activity is not currently visible, but is running.
1306 // Tell it to become visible.
1307 r.visible = true;
1308 if (r.state != ActivityState.RESUMED && r != starting) {
1309 // If this activity is paused, tell it
1310 // to now show its window.
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001311 if (DEBUG_VISBILITY) Slog.v(
Craig Mautnerd44711d2013-02-23 11:24:36 -08001312 TAG, "Making visible and scheduling visibility: " + r);
1313 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001314 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001315 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001316 mUndrawnActivitiesBelowTopTranslucent.add(r);
1317 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001318 setVisibile(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001319 r.sleeping = false;
1320 r.app.pendingUiClean = true;
1321 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1322 r.stopFreezingScreenLocked(false);
1323 } catch (Exception e) {
1324 // Just skip on any failure; we'll make it
1325 // visible when it next restarts.
1326 Slog.w(TAG, "Exception thrown making visibile: "
1327 + r.intent.getComponent(), e);
1328 }
1329 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001330 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001331
Craig Mautnerd44711d2013-02-23 11:24:36 -08001332 // Aggregate current change flags.
1333 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001334
Craig Mautnerd44711d2013-02-23 11:24:36 -08001335 if (r.fullscreen) {
1336 // At this point, nothing else needs to be shown
Craig Mautner580ea812013-04-25 12:58:38 -07001337 if (DEBUG_VISBILITY) Slog.v(TAG, "Fullscreen: at " + r);
1338 behindFullscreen = true;
Craig Mautner84984fa2014-06-19 11:19:20 -07001339 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Craig Mautner39e1c5a2013-10-23 15:14:22 -07001340 if (DEBUG_VISBILITY) Slog.v(TAG, "Showing home: at " + r);
Jose Lima729cb232014-05-05 15:59:40 -07001341 behindFullscreen = true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001342 }
1343 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001344 if (DEBUG_VISBILITY) Slog.v(
1345 TAG, "Make invisible? " + r + " finishing=" + r.finishing
1346 + " state=" + r.state
1347 + " behindFullscreen=" + behindFullscreen);
1348 // Now for any activities that aren't visible to the user, make
1349 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001350 if (r.visible) {
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001351 if (DEBUG_VISBILITY) Slog.v(TAG, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001352 try {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001353 setVisibile(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001354 switch (r.state) {
1355 case STOPPING:
1356 case STOPPED:
1357 if (r.app != null && r.app.thread != null) {
1358 if (DEBUG_VISBILITY) Slog.v(
1359 TAG, "Scheduling invisibility: " + r);
1360 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1361 }
1362 break;
1363
1364 case INITIALIZING:
1365 case RESUMED:
1366 case PAUSING:
1367 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001368 // This case created for transitioning activities from
1369 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001370 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001371 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001372 } else {
1373 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1374 mStackSupervisor.mStoppingActivities.add(r);
1375 }
1376 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001377 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001378 break;
1379
1380 default:
1381 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001382 }
1383 } catch (Exception e) {
1384 // Just skip on any failure; we'll make it
1385 // visible when it next restarts.
1386 Slog.w(TAG, "Exception thrown making hidden: "
1387 + r.intent.getComponent(), e);
1388 }
1389 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001390 if (DEBUG_VISBILITY) Slog.v(TAG, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001391 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001392 }
1393 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001394 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001395
1396 if (mTranslucentActivityWaiting != null &&
1397 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1398 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1399 notifyActivityDrawnLocked(null);
1400 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001401 }
Craig Mautner58547802013-03-05 08:23:53 -08001402
Todd Kennedyaab56db2015-01-30 09:39:53 -08001403 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001404 mTranslucentActivityWaiting = r;
1405 mUndrawnActivitiesBelowTopTranslucent.clear();
1406 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1407 }
1408
1409 /**
1410 * Called as activities below the top translucent activity are redrawn. When the last one is
1411 * redrawn notify the top activity by calling
1412 * {@link Activity#onTranslucentConversionComplete}.
1413 *
1414 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1415 * occurred and the activity will be notified immediately.
1416 */
1417 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001418 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001419 if ((r == null)
1420 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1421 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1422 // The last undrawn activity below the top has just been drawn. If there is an
1423 // opaque activity at the top, notify it that it can become translucent safely now.
1424 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1425 mTranslucentActivityWaiting = null;
1426 mUndrawnActivitiesBelowTopTranslucent.clear();
1427 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1428
Craig Mautner71dd1b62014-02-18 15:48:52 -08001429 if (waitingActivity != null) {
1430 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1431 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1432 try {
1433 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1434 waitingActivity.appToken, r != null);
1435 } catch (RemoteException e) {
1436 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001437 }
1438 }
1439 }
1440 }
1441
Craig Mautnera61bc652013-10-28 15:43:18 -07001442 /** If any activities below the top running one are in the INITIALIZING state and they have a
1443 * starting window displayed then remove that starting window. It is possible that the activity
1444 * in this state will never resumed in which case that starting window will be orphaned. */
1445 void cancelInitializingActivities() {
1446 final ActivityRecord topActivity = topRunningActivityLocked(null);
1447 boolean aboveTop = true;
1448 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1449 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1450 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1451 final ActivityRecord r = activities.get(activityNdx);
1452 if (aboveTop) {
1453 if (r == topActivity) {
1454 aboveTop = false;
1455 }
1456 continue;
1457 }
1458
1459 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
1460 if (DEBUG_VISBILITY) Slog.w(TAG, "Found orphaned starting window " + r);
1461 r.mStartingWindowShown = false;
1462 mWindowManager.removeAppStartingWindow(r.appToken);
1463 }
1464 }
1465 }
1466 }
1467
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001468 /**
1469 * Ensure that the top activity in the stack is resumed.
1470 *
1471 * @param prev The previously resumed activity, for when in the process
1472 * of pausing; can be null to call from elsewhere.
1473 *
1474 * @return Returns true if something is being resumed, or false if
1475 * nothing happened.
1476 */
1477 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001478 return resumeTopActivityLocked(prev, null);
1479 }
1480
1481 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001482 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001483 // Don't even start recursing.
1484 return false;
1485 }
1486
1487 boolean result = false;
1488 try {
1489 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001490 mStackSupervisor.inResumeTopActivity = true;
1491 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1492 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001493 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001494 }
Craig Mautner544efa72014-09-04 16:41:20 -07001495 result = resumeTopActivityInnerLocked(prev, options);
1496 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001497 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001498 }
1499 return result;
1500 }
1501
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001502 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Craig Mautner5314a402013-09-26 12:40:16 -07001503 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
1504
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001505 if (!mService.mBooting && !mService.mBooted) {
1506 // Not ready yet!
1507 return false;
1508 }
1509
Craig Mautnerdf88d732014-01-27 09:21:32 -08001510 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001511 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001512 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001513 // Do not resume this stack if its parent is not resumed.
1514 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1515 return false;
1516 }
1517
Craig Mautnera61bc652013-10-28 15:43:18 -07001518 cancelInitializingActivities();
1519
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001520 // Find the first activity that is not finishing.
Craig Mautner94ab4662015-01-20 10:49:22 -08001521 final ActivityRecord next = topRunningActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001522
1523 // Remember how we'll process this pause/resume situation, and ensure
1524 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001525 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1526 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001527
Craig Mautner84984fa2014-06-19 11:19:20 -07001528 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001529 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001530 // There are no more activities!
1531 final String reason = "noMoreActivities";
1532 if (!mFullscreen) {
1533 // Try to move focus to the next visible stack with a running activity if this
1534 // stack is not covering the entire screen.
1535 final ActivityStack stack = getNextVisibleStackLocked();
1536 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1537 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1538 }
1539 }
1540 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001541 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001542 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001543 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001544 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001545 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1546 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1547 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001548 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001549 }
1550
1551 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001552
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001553 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001554 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1555 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001556 // Make sure we have executed any pending transitions, since there
1557 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001558 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001559 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001560 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001561 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001562 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001563 return false;
1564 }
1565
Craig Mautner525f3d92013-05-07 14:01:50 -07001566 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001567 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001568 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001569 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001570 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001571 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001572 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001573 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001574 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001575 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001576 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001577 } else if (!isOnHomeDisplay()) {
1578 return false;
1579 } else if (!isHomeStack()){
1580 if (DEBUG_STATES) Slog.d(TAG,
Craig Mautnere0a38842013-12-16 16:14:02 -08001581 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001582 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1583 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
Craig Mautner299f9602015-01-26 09:47:33 -08001584 return mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001585 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001586 }
1587
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001588 // If we are sleeping, and there is no resumed activity, and the top
1589 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001590 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001591 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001592 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001593 // Make sure we have executed any pending transitions, since there
1594 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001595 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001596 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001597 ActivityOptions.abort(options);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001598 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001599 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001600 return false;
1601 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001602
1603 // Make sure that the user who owns this activity is started. If not,
1604 // we will just leave it as is because someone should be bringing
1605 // another user's activities to the top of the stack.
1606 if (mService.mStartedUsers.get(next.userId) == null) {
1607 Slog.w(TAG, "Skipping resume of top activity " + next
1608 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001609 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001610 return false;
1611 }
1612
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001613 // The activity may be waiting for stop, but that is no longer
1614 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001615 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001616 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001617 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001618 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001619
1620 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
1621
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001622 // If we are currently pausing an activity, then don't do anything
1623 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001624 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001625 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG,
1626 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001627 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001628 return false;
1629 }
1630
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001631 // Okay we are now going to start a switch, to 'next'. We may first
1632 // have to pause the current activity, but this is an important point
1633 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001634 // XXX "App Redirected" dialog is getting too many false positives
1635 // at this point, so turn off for now.
1636 if (false) {
1637 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1638 long now = SystemClock.uptimeMillis();
1639 final boolean inTime = mLastStartedActivity.startTime != 0
1640 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1641 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1642 final int nextUid = next.info.applicationInfo.uid;
1643 if (inTime && lastUid != nextUid
1644 && lastUid != next.launchedFromUid
1645 && mService.checkPermission(
1646 android.Manifest.permission.STOP_APP_SWITCHES,
1647 -1, next.launchedFromUid)
1648 != PackageManager.PERMISSION_GRANTED) {
1649 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1650 } else {
1651 next.startTime = now;
1652 mLastStartedActivity = next;
1653 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001654 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001655 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001656 mLastStartedActivity = next;
1657 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001658 }
Craig Mautner58547802013-03-05 08:23:53 -08001659
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001660 // We need to start pausing the current activity so the top one
1661 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001662 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1663 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001664 if (mResumedActivity != null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001665 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001666 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001667 }
1668 if (pausing) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001669 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
1670 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001671 // At this point we want to put the upcoming activity's process
1672 // at the top of the LRU list, since we know we will be needing it
1673 // very soon and it would be a waste to let it get killed if it
1674 // happens to be sitting towards the end.
1675 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001676 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001677 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001678 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001679 return true;
1680 }
1681
Christopher Tated3f175c2012-06-14 14:16:54 -07001682 // If the most recent activity was noHistory but was only stopped rather
1683 // than stopped+finished because the device went to sleep, we need to make
1684 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001685 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001686 !mLastNoHistoryActivity.finishing) {
1687 if (DEBUG_STATES) Slog.d(TAG, "no-history finish of " + mLastNoHistoryActivity +
1688 " on new resume");
1689 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001690 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001691 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001692 }
1693
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001694 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001695 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1696 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001697 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001698 if (DEBUG_SWITCH) Slog.v(
1699 TAG, "Resuming top, waiting visible to hide: " + prev);
1700 } else {
1701 // The next activity is already visible, so hide the previous
1702 // activity's windows right now so we can show the new one ASAP.
1703 // We only do this if the previous is finishing, which should mean
1704 // it is on top of the one being resumed so hiding it quickly
1705 // is good. Otherwise, we want to do the normal route of allowing
1706 // the resumed activity to be shown so we can decide if the
1707 // previous should actually be hidden depending on whether the
1708 // new one is found to be full-screen or not.
1709 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001710 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001711 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
1712 + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001713 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001714 + ", nowVisible=" + next.nowVisible);
1715 } else {
Craig Mautner8c14c152015-01-15 17:32:07 -08001716 if (DEBUG_SWITCH) Slog.v(TAG,
1717 "Previous already visible but still waiting to hide: " + prev
1718 + ", waitingVisible="
1719 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1720 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001721 }
1722 }
1723 }
1724
Dianne Hackborne7f97212011-02-24 14:40:20 -08001725 // Launching this app's activity, make sure the app is no longer
1726 // considered stopped.
1727 try {
1728 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001729 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001730 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001731 } catch (IllegalArgumentException e) {
1732 Slog.w(TAG, "Failed trying to unstop package "
1733 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001734 }
1735
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001736 // We are starting up the next activity, so tell the window manager
1737 // that the previous one will be hidden soon. This way it can know
1738 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001739 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001740 if (prev != null) {
1741 if (prev.finishing) {
1742 if (DEBUG_TRANSITION) Slog.v(TAG,
1743 "Prepare close transition: prev=" + prev);
1744 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001745 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001746 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001747 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001748 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001749 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1750 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001751 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001752 mWindowManager.setAppWillBeHidden(prev.appToken);
1753 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001754 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001755 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001756 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001757 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001758 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001759 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001760 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001761 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001762 : next.mLaunchTaskBehind
1763 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1764 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001765 }
1766 }
1767 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001768 mWindowManager.setAppWillBeHidden(prev.appToken);
1769 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001770 }
Craig Mautner967212c2013-04-13 21:10:58 -07001771 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001772 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001773 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001774 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001775 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001776 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001777 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001778 }
1779 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001780
1781 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001782 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001783 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1784 if (opts != null) {
1785 resumeAnimOptions = opts.toBundle();
1786 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001787 next.applyOptionsLocked();
1788 } else {
1789 next.clearOptionsLocked();
1790 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001791
Craig Mautnercf910b02013-04-23 11:23:27 -07001792 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001793 if (next.app != null && next.app.thread != null) {
1794 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
1795
1796 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001797 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001798
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001799 // schedule launch ticks to collect information about slow apps.
1800 next.startLaunchTickingLocked();
1801
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001802 ActivityRecord lastResumedActivity =
1803 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001804 ActivityState lastState = next.state;
1805
1806 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001807
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001808 if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001809 next.state = ActivityState.RESUMED;
1810 mResumedActivity = next;
1811 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08001812 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001813 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001814 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001815 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001816
1817 // Have the window manager re-evaluate the orientation of
1818 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001819 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001820 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001821 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001822 mService.mConfiguration,
1823 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1824 if (config != null) {
1825 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001826 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001827 notUpdated = !mService.updateConfigurationLocked(config, next, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001828 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001829
Craig Mautner525f3d92013-05-07 14:01:50 -07001830 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001831 // The configuration update wasn't able to keep the existing
1832 // instance of the activity, and instead started a new one.
1833 // We should be all done, but let's just make sure our activity
1834 // is still at the top and schedule another run if something
1835 // weird happened.
1836 ActivityRecord nextNext = topRunningActivityLocked(null);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001837 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001838 "Activity config changed during resume: " + next
1839 + ", new next: " + nextNext);
1840 if (nextNext != next) {
1841 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07001842 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001843 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001844 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001845 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07001846 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001847 return true;
1848 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001849 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001850 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001851 }
Craig Mautner58547802013-03-05 08:23:53 -08001852
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001853 try {
1854 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08001855 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001856 if (a != null) {
1857 final int N = a.size();
1858 if (!next.finishing && N > 0) {
1859 if (DEBUG_RESULTS) Slog.v(
1860 TAG, "Delivering results to " + next
1861 + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001862 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001863 }
1864 }
1865
1866 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001867 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001868 }
1869
Craig Mautner299f9602015-01-26 09:47:33 -08001870 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
1871 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08001872
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001873 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07001874 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001875 next.app.pendingUiClean = true;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001876 next.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
George Mount2c92c972014-03-20 09:38:23 -07001877 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07001878 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08001879 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08001880
Craig Mautner0eea92c2013-05-16 13:35:39 -07001881 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001882
Craig Mautnerac6f8432013-07-17 13:24:59 -07001883 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001884 } catch (Exception e) {
1885 // Whoops, need to restart this activity!
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001886 if (DEBUG_STATES) Slog.v(TAG, "Resume failed; resetting state to "
1887 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001888 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001889 if (lastStack != null) {
1890 lastStack.mResumedActivity = lastResumedActivity;
1891 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001892 Slog.i(TAG, "Restarting because process died: " + next);
1893 if (!next.hasBeenLaunched) {
1894 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001895 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
1896 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001897 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001898 next.appToken, next.packageName, next.theme,
1899 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07001900 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
1901 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001902 }
George Mount2c92c972014-03-20 09:38:23 -07001903 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07001904 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001905 return true;
1906 }
1907
1908 // From this point on, if something goes wrong there is no way
1909 // to recover the activity.
1910 try {
1911 next.visible = true;
1912 completeResumeLocked(next);
1913 } catch (Exception e) {
1914 // If any exception gets thrown, toss away this
1915 // activity and try the next one.
1916 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001917 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001918 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07001919 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001920 return true;
1921 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001922 next.stopped = false;
1923
1924 } else {
1925 // Whoops, need to restart this activity!
1926 if (!next.hasBeenLaunched) {
1927 next.hasBeenLaunched = true;
1928 } else {
1929 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001930 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08001931 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001932 mService.compatibilityInfoForPackageLocked(
1933 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001934 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07001935 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001936 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001937 }
1938 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
1939 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001940 if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07001941 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001942 }
1943
Craig Mautnercf910b02013-04-23 11:23:27 -07001944 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001945 return true;
1946 }
1947
riddle_hsuc215a4f2014-12-27 12:10:45 +08001948 private TaskRecord getNextTask(TaskRecord targetTask) {
1949 final int index = mTaskHistory.indexOf(targetTask);
1950 if (index >= 0) {
1951 final int numTasks = mTaskHistory.size();
1952 for (int i = index + 1; i < numTasks; ++i) {
1953 TaskRecord task = mTaskHistory.get(i);
1954 if (task.userId == targetTask.userId) {
1955 return task;
1956 }
1957 }
1958 }
1959 return null;
1960 }
1961
Craig Mautnerac6f8432013-07-17 13:24:59 -07001962 private void insertTaskAtTop(TaskRecord task) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08001963 // If the moving task is over home stack, transfer its return type to next task
1964 if (task.isOverHomeStack()) {
1965 final TaskRecord nextTask = getNextTask(task);
1966 if (nextTask != null) {
1967 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
1968 }
1969 }
1970
Craig Mautner9c85c202013-10-04 20:11:26 -07001971 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08001972 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08001973 if (isOnHomeDisplay()) {
1974 ActivityStack lastStack = mStackSupervisor.getLastStack();
1975 final boolean fromHome = lastStack.isHomeStack();
1976 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07001977 task.setTaskToReturnTo(fromHome
1978 ? lastStack.topTask() == null
1979 ? HOME_ACTIVITY_TYPE
1980 : lastStack.topTask().taskType
1981 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08001982 }
1983 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07001984 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07001985 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07001986
Craig Mautnerac6f8432013-07-17 13:24:59 -07001987 mTaskHistory.remove(task);
1988 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07001989 int taskNdx = mTaskHistory.size();
Kenny Guy2a764942014-04-02 13:29:20 +01001990 if (!isCurrentProfileLocked(task.userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001991 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07001992 while (--taskNdx >= 0) {
1993 if (!isCurrentProfileLocked(mTaskHistory.get(taskNdx).userId)) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001994 break;
1995 }
1996 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001997 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001998 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001999 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002000 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002001 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002002
Craig Mautner8849a5e2013-04-02 16:41:03 -07002003 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002004 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002005 TaskRecord rTask = r.task;
2006 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002007 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2008 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002009 // Last activity in task had been removed or ActivityManagerService is reusing task.
2010 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002011 // Might not even be in.
Craig Mautnerac6f8432013-07-17 13:24:59 -07002012 insertTaskAtTop(rTask);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002013 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002014 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002015 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002016 if (!newTask) {
2017 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002018 boolean startIt = true;
2019 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2020 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002021 if (task.getTopActivity() == null) {
2022 // All activities in task are finishing.
2023 continue;
2024 }
Craig Mautner70a86932013-02-28 22:37:44 -08002025 if (task == r.task) {
2026 // Here it is! Now, if this is not yet visible to the
2027 // user, then just add it without starting; it will
2028 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002029 if (!startIt) {
2030 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2031 + task, new RuntimeException("here").fillInStackTrace());
2032 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002033 r.putInHistory();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002034 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
2035 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002036 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07002037 r.userId, r.info.configChanges, task.voiceSession != null,
2038 r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002039 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002040 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002041 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002042 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002043 return;
2044 }
2045 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002046 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002047 startIt = false;
2048 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002049 }
2050 }
2051
2052 // Place a new activity at top of stack, so it is next to interact
2053 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002054
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002055 // If we are not placing the new activity frontmost, we do not want
2056 // to deliver the onUserLeaving callback to the actual frontmost
2057 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002058 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002059 mStackSupervisor.mUserLeaving = false;
Craig Mautner70a86932013-02-28 22:37:44 -08002060 if (DEBUG_USER_LEAVING) Slog.v(TAG,
2061 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002062 }
Craig Mautner70a86932013-02-28 22:37:44 -08002063
2064 task = r.task;
2065
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002066 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002067 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002068 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002069 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002070 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002071
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002072 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002073 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002074 // We want to show the starting preview window if we are
2075 // switching to a new task, or the next activity's process is
2076 // not currently running.
2077 boolean showStartingIcon = newTask;
2078 ProcessRecord proc = r.app;
2079 if (proc == null) {
2080 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2081 }
2082 if (proc == null || proc.thread == null) {
2083 showStartingIcon = true;
2084 }
2085 if (DEBUG_TRANSITION) Slog.v(TAG,
2086 "Prepare open transition: starting " + r);
2087 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002088 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002089 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002090 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002091 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002092 ? r.mLaunchTaskBehind
2093 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2094 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002095 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002096 mNoAnimActivities.remove(r);
2097 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002098 mWindowManager.addAppToken(task.mActivities.indexOf(r),
Craig Mautnerc00204b2013-03-05 15:02:14 -08002099 r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002100 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002101 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002102 boolean doShow = true;
2103 if (newTask) {
2104 // Even though this activity is starting fresh, we still need
2105 // to reset it to make sure we apply affinities to move any
2106 // existing activities from other tasks in to it.
2107 // If the caller has requested that the target task be
2108 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002109 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002110 resetTaskIfNeededLocked(r, r);
2111 doShow = topRunningNonDelayedActivityLocked(null) == r;
2112 }
George Mount70778d72014-07-01 16:33:45 -07002113 } else if (options != null && new ActivityOptions(options).getAnimationType()
2114 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2115 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002116 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002117 if (r.mLaunchTaskBehind) {
2118 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2119 // tell WindowManager that r is visible even though it is at the back of the stack.
2120 mWindowManager.setAppVisibility(r.appToken, true);
2121 ensureActivitiesVisibleLocked(null, 0);
2122 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002123 // Figure out if we are transitioning from another activity that is
2124 // "has the same starting icon" as the next one. This allows the
2125 // window manager to keep the previous window it had previously
2126 // created, if it still had one.
2127 ActivityRecord prev = mResumedActivity;
2128 if (prev != null) {
2129 // We don't want to reuse the previous starting preview if:
2130 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002131 if (prev.task != r.task) {
2132 prev = null;
2133 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002134 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002135 else if (prev.nowVisible) {
2136 prev = null;
2137 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002138 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002139 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002140 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002141 mService.compatibilityInfoForPackageLocked(
2142 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002143 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002144 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002145 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002146 }
2147 } else {
2148 // If this is the first activity, don't do any fancy animations,
2149 // because there is nothing for it to animate on top of.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002150 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
Craig Mautnerc00204b2013-03-05 15:02:14 -08002151 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Craig Mautner5d9f5472013-11-12 14:02:52 -08002152 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId,
Craig Mautnerbb742462014-07-07 15:28:55 -07002153 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002154 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002155 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002156 }
2157 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002158 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002159 }
2160
2161 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002162 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002163 }
2164 }
2165
Dianne Hackbornbe707852011-11-11 14:32:10 -08002166 final void validateAppTokensLocked() {
2167 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002168 mValidateAppTokens.ensureCapacity(numActivities());
2169 final int numTasks = mTaskHistory.size();
2170 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2171 TaskRecord task = mTaskHistory.get(taskNdx);
2172 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002173 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002174 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002175 }
Craig Mautner000f0022013-02-26 15:04:29 -08002176 TaskGroup group = new TaskGroup();
2177 group.taskId = task.taskId;
2178 mValidateAppTokens.add(group);
2179 final int numActivities = activities.size();
2180 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2181 final ActivityRecord r = activities.get(activityNdx);
2182 group.tokens.add(r.appToken);
2183 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002184 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002185 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002186 }
2187
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002188 /**
2189 * Perform a reset of the given task, if needed as part of launching it.
2190 * Returns the new HistoryRecord at the top of the task.
2191 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002192 /**
2193 * Helper method for #resetTaskIfNeededLocked.
2194 * We are inside of the task being reset... we'll either finish this activity, push it out
2195 * for another task, or leave it as-is.
2196 * @param task The task containing the Activity (taskTop) that might be reset.
2197 * @param forceReset
2198 * @return An ActivityOptions that needs to be processed.
2199 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002200 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002201 ActivityOptions topOptions = null;
2202
2203 int replyChainEnd = -1;
2204 boolean canMoveOptions = true;
2205
2206 // We only do this for activities that are not the root of the task (since if we finish
2207 // the root, we may no longer have the task!).
2208 final ArrayList<ActivityRecord> activities = task.mActivities;
2209 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002210 final int rootActivityNdx = task.findEffectiveRootIndex();
2211 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002212 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002213 if (target.frontOfTask)
2214 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002215
2216 final int flags = target.info.flags;
2217 final boolean finishOnTaskLaunch =
2218 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2219 final boolean allowTaskReparenting =
2220 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2221 final boolean clearWhenTaskReset =
2222 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2223
2224 if (!finishOnTaskLaunch
2225 && !clearWhenTaskReset
2226 && target.resultTo != null) {
2227 // If this activity is sending a reply to a previous
2228 // activity, we can't do anything with it now until
2229 // we reach the start of the reply chain.
2230 // XXX note that we are assuming the result is always
2231 // to the previous activity, which is almost always
2232 // the case but we really shouldn't count on.
2233 if (replyChainEnd < 0) {
2234 replyChainEnd = i;
2235 }
2236 } else if (!finishOnTaskLaunch
2237 && !clearWhenTaskReset
2238 && allowTaskReparenting
2239 && target.taskAffinity != null
2240 && !target.taskAffinity.equals(task.affinity)) {
2241 // If this activity has an affinity for another
2242 // task, then we need to move it out of here. We will
2243 // move it as far out of the way as possible, to the
2244 // bottom of the activity stack. This also keeps it
2245 // correctly ordered with any activities we previously
2246 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002247 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002248 final ActivityRecord bottom =
2249 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002250 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002251 if (bottom != null && target.taskAffinity != null
2252 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002253 // If the activity currently at the bottom has the
2254 // same task affinity as the one we are moving,
2255 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002256 targetTask = bottom.task;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002257 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002258 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002259 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002260 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002261 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002262 targetTask.affinityIntent = target.intent;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002263 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
2264 + " out to new task " + target.task);
2265 }
2266
Craig Mautnere3a74d52013-02-22 14:14:58 -08002267 final int targetTaskId = targetTask.taskId;
Craig Mautner83162a92015-01-26 14:43:30 -08002268 mWindowManager.setAppTask(target.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002269
Craig Mautner525f3d92013-05-07 14:01:50 -07002270 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002271 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2272 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002273 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002274 if (p.finishing) {
2275 continue;
2276 }
2277
Craig Mautnere3a74d52013-02-22 14:14:58 -08002278 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002279 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002280 topOptions = p.takeOptionsLocked();
2281 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002282 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002283 }
2284 }
2285 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing activity " + p + " from task="
Craig Mautner329f4122013-11-07 09:10:42 -08002286 + task + " adding to task=" + targetTask
2287 + " Callers=" + Debug.getCallers(4));
Craig Mautnere3a74d52013-02-22 14:14:58 -08002288 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
2289 + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002290 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002291 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002292
Craig Mautner83162a92015-01-26 14:43:30 -08002293 mWindowManager.setAppTask(p.appToken, targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002294 }
2295
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002296 mWindowManager.moveTaskToBottom(targetTaskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002297 if (VALIDATE_TOKENS) {
2298 validateAppTokensLocked();
2299 }
2300
2301 replyChainEnd = -1;
2302 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2303 // If the activity should just be removed -- either
2304 // because it asks for it, or the task should be
2305 // cleared -- then finish it and anything that is
2306 // part of its reply chain.
2307 int end;
2308 if (clearWhenTaskReset) {
2309 // In this case, we want to finish this activity
2310 // and everything above it, so be sneaky and pretend
2311 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002312 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002313 } else if (replyChainEnd < 0) {
2314 end = i;
2315 } else {
2316 end = replyChainEnd;
2317 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002318 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002319 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002320 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002321 if (p.finishing) {
2322 continue;
2323 }
2324 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002325 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002326 topOptions = p.takeOptionsLocked();
2327 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002328 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002329 }
2330 }
Craig Mautner58547802013-03-05 08:23:53 -08002331 if (DEBUG_TASKS) Slog.w(TAG,
2332 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002333 if (finishActivityLocked(
2334 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002335 end--;
2336 srcPos--;
2337 }
2338 }
2339 replyChainEnd = -1;
2340 } else {
2341 // If we were in the middle of a chain, well the
2342 // activity that started it all doesn't want anything
2343 // special, so leave it all as-is.
2344 replyChainEnd = -1;
2345 }
2346 }
2347
2348 return topOptions;
2349 }
2350
2351 /**
2352 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2353 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2354 * @param affinityTask The task we are looking for an affinity to.
2355 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2356 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2357 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2358 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002359 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002360 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002361 int replyChainEnd = -1;
2362 final int taskId = task.taskId;
2363 final String taskAffinity = task.affinity;
2364
2365 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2366 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002367 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2368
2369 // Do not operate on or below the effective root Activity.
2370 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002371 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002372 if (target.frontOfTask)
2373 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002374
2375 final int flags = target.info.flags;
2376 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2377 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2378
2379 if (target.resultTo != null) {
2380 // If this activity is sending a reply to a previous
2381 // activity, we can't do anything with it now until
2382 // we reach the start of the reply chain.
2383 // XXX note that we are assuming the result is always
2384 // to the previous activity, which is almost always
2385 // the case but we really shouldn't count on.
2386 if (replyChainEnd < 0) {
2387 replyChainEnd = i;
2388 }
2389 } else if (topTaskIsHigher
2390 && allowTaskReparenting
2391 && taskAffinity != null
2392 && taskAffinity.equals(target.taskAffinity)) {
2393 // This activity has an affinity for our task. Either remove it if we are
2394 // clearing or move it over to our task. Note that
2395 // we currently punt on the case where we are resetting a
2396 // task that is not at the top but who has activities above
2397 // with an affinity to it... this is really not a normal
2398 // case, and we will need to later pull that task to the front
2399 // and usually at that point we will do the reset and pick
2400 // up those remaining activities. (This only happens if
2401 // someone starts an activity in a new task from an activity
2402 // in a task that is not currently on top.)
2403 if (forceReset || finishOnTaskLaunch) {
2404 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2405 if (DEBUG_TASKS) Slog.v(TAG, "Finishing task at index " + start + " to " + i);
2406 for (int srcPos = start; srcPos >= i; --srcPos) {
2407 final ActivityRecord p = activities.get(srcPos);
2408 if (p.finishing) {
2409 continue;
2410 }
Todd Kennedy539db512014-12-15 09:57:55 -08002411 finishActivityLocked(
2412 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002413 }
2414 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002415 if (taskInsertionPoint < 0) {
2416 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002417
Craig Mautner77878772013-03-04 19:46:24 -08002418 }
Craig Mautner77878772013-03-04 19:46:24 -08002419
2420 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
2421 if (DEBUG_TASKS) Slog.v(TAG, "Reparenting from task=" + affinityTask + ":"
2422 + start + "-" + i + " to task=" + task + ":" + taskInsertionPoint);
2423 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002424 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002425 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002426 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002427
Craig Mautnere3a74d52013-02-22 14:14:58 -08002428 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Removing and adding activity " + p
2429 + " to stack at " + task,
2430 new RuntimeException("here").fillInStackTrace());
2431 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p + " from " + srcPos
2432 + " in to resetting task " + task);
Craig Mautner83162a92015-01-26 14:43:30 -08002433 mWindowManager.setAppTask(p.appToken, taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002434 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002435 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002436 if (VALIDATE_TOKENS) {
2437 validateAppTokensLocked();
2438 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002439
2440 // Now we've moved it in to place... but what if this is
2441 // a singleTop activity and we have put it on top of another
2442 // instance of the same activity? Then we drop the instance
2443 // below so it remains singleTop.
2444 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2445 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002446 int targetNdx = taskActivities.indexOf(target);
2447 if (targetNdx > 0) {
2448 ActivityRecord p = taskActivities.get(targetNdx - 1);
2449 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002450 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2451 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002452 }
2453 }
2454 }
2455 }
2456
2457 replyChainEnd = -1;
2458 }
2459 }
Craig Mautner77878772013-03-04 19:46:24 -08002460 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002461 }
2462
Craig Mautner8849a5e2013-04-02 16:41:03 -07002463 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002464 ActivityRecord newActivity) {
2465 boolean forceReset =
2466 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2467 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2468 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2469 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2470 forceReset = true;
2471 }
2472 }
2473
2474 final TaskRecord task = taskTop.task;
2475
2476 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2477 * for remaining tasks. Used for later tasks to reparent to task. */
2478 boolean taskFound = false;
2479
2480 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2481 ActivityOptions topOptions = null;
2482
Craig Mautner77878772013-03-04 19:46:24 -08002483 // Preserve the location for reparenting in the new task.
2484 int reparentInsertionPoint = -1;
2485
Craig Mautnere3a74d52013-02-22 14:14:58 -08002486 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2487 final TaskRecord targetTask = mTaskHistory.get(i);
2488
2489 if (targetTask == task) {
2490 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2491 taskFound = true;
2492 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002493 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2494 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002495 }
2496 }
2497
Craig Mautner70a86932013-02-28 22:37:44 -08002498 int taskNdx = mTaskHistory.indexOf(task);
2499 do {
2500 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2501 } while (taskTop == null && taskNdx >= 0);
2502
Craig Mautnere3a74d52013-02-22 14:14:58 -08002503 if (topOptions != null) {
2504 // If we got some ActivityOptions from an activity on top that
2505 // was removed from the task, propagate them to the new real top.
2506 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002507 taskTop.updateOptionsLocked(topOptions);
2508 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002509 topOptions.abort();
2510 }
2511 }
2512
2513 return taskTop;
2514 }
2515
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002516 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2517 String resultWho, int requestCode, int resultCode, Intent data) {
2518
2519 if (callingUid > 0) {
2520 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002521 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002522 }
2523
2524 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2525 + " : who=" + resultWho + " req=" + requestCode
2526 + " res=" + resultCode + " data=" + data);
2527 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2528 try {
2529 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2530 list.add(new ResultInfo(resultWho, requestCode,
2531 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002532 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002533 return;
2534 } catch (Exception e) {
2535 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2536 }
2537 }
2538
2539 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2540 }
2541
Craig Mautner299f9602015-01-26 09:47:33 -08002542 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002543 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2544 ActivityRecord next = topRunningActivityLocked(null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002545 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002546 if (next != r) {
2547 final TaskRecord task = r.task;
Craig Mautner84984fa2014-06-19 11:19:20 -07002548 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002549 // For non-fullscreen stack, we want to move the focus to the next visible
2550 // stack to prevent the home screen from moving to the top and obscuring
2551 // other visible stacks.
2552 if (!mFullscreen
2553 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2554 return;
2555 }
2556 // Move the home stack to the top if this stack is fullscreen or there is no
2557 // other visible stack.
2558 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07002559 }
2560 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002561
2562 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002563 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002564 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002565 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002566 }
2567 }
2568
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002569 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2570 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2571 final String myReason = reason + " adjustFocusToNextVisibleStack";
2572 if (stack == null) {
2573 return false;
2574 }
2575 final ActivityRecord top = stack.topRunningActivityLocked(null);
2576 if (top == null) {
2577 return false;
2578 }
2579 stack.moveToFront(myReason);
2580 mService.setFocusedActivityLocked(top, myReason);
2581 return true;
2582 }
2583
Craig Mautnerf3333272013-04-22 10:55:53 -07002584 final void stopActivityLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002585 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
2586 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2587 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2588 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002589 if (!mService.isSleeping()) {
Christopher Tated3f175c2012-06-14 14:16:54 -07002590 if (DEBUG_STATES) {
2591 Slog.d(TAG, "no-history finish of " + r);
2592 }
2593 requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
Todd Kennedy539db512014-12-15 09:57:55 -08002594 "stop-no-history", false);
Christopher Tated3f175c2012-06-14 14:16:54 -07002595 } else {
2596 if (DEBUG_STATES) Slog.d(TAG, "Not finishing noHistory " + r
2597 + " on stop because we're just sleeping");
2598 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002599 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002600 }
2601
2602 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002603 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002604 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002605 try {
2606 r.stopped = false;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002607 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2608 + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002609 r.state = ActivityState.STOPPING;
2610 if (DEBUG_VISBILITY) Slog.v(
2611 TAG, "Stopping visible=" + r.visible + " for " + r);
2612 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002613 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002614 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002615 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002616 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002617 r.setSleeping(true);
2618 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002619 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002620 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002621 } catch (Exception e) {
2622 // Maybe just ignore exceptions here... if the process
2623 // has crashed, our death notification will clean things
2624 // up.
2625 Slog.w(TAG, "Exception thrown during pause", e);
2626 // Just in case, assume it to be stopped.
2627 r.stopped = true;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002628 if (DEBUG_STATES) Slog.v(TAG, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002629 r.state = ActivityState.STOPPED;
2630 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002631 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002632 }
2633 }
2634 }
2635 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002636
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002637 /**
2638 * @return Returns true if the activity is being finished, false if for
2639 * some reason it is being left as-is.
2640 */
2641 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002642 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002643 ActivityRecord r = isInStackLocked(token);
Christopher Tated3f175c2012-06-14 14:16:54 -07002644 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002645 TAG, "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002646 + ", result=" + resultCode + ", data=" + resultData
2647 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002648 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002649 return false;
2650 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002651
Craig Mautnerd44711d2013-02-23 11:24:36 -08002652 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002653 return true;
2654 }
2655
Craig Mautnerd2328952013-03-05 12:46:26 -08002656 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002657 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2658 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2659 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2660 ActivityRecord r = activities.get(activityNdx);
2661 if (r.resultTo == self && r.requestCode == requestCode) {
2662 if ((r.resultWho == null && resultWho == null) ||
2663 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2664 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2665 false);
2666 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002667 }
2668 }
2669 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002670 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002671 }
2672
Todd Kennedy539db512014-12-15 09:57:55 -08002673 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002674 ActivityRecord r = topRunningActivityLocked(null);
2675 if (r != null && r.app == app) {
2676 // If the top running activity is from this crashing
2677 // process, then terminate it to avoid getting in a loop.
2678 Slog.w(TAG, " Force finishing activity "
2679 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002680 int taskNdx = mTaskHistory.indexOf(r.task);
2681 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002682 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002683 // Also terminate any activities below it that aren't yet
2684 // stopped, to avoid a situation where one will get
2685 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002686 --activityNdx;
2687 if (activityNdx < 0) {
2688 do {
2689 --taskNdx;
2690 if (taskNdx < 0) {
2691 break;
2692 }
2693 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2694 } while (activityNdx < 0);
2695 }
2696 if (activityNdx >= 0) {
2697 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002698 if (r.state == ActivityState.RESUMED
2699 || r.state == ActivityState.PAUSING
2700 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002701 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002702 Slog.w(TAG, " Force finishing activity "
2703 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002704 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002705 }
2706 }
2707 }
2708 }
2709 }
2710
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002711 final void finishVoiceTask(IVoiceInteractionSession session) {
2712 IBinder sessionBinder = session.asBinder();
2713 boolean didOne = false;
2714 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2715 TaskRecord tr = mTaskHistory.get(taskNdx);
2716 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2717 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2718 ActivityRecord r = tr.mActivities.get(activityNdx);
2719 if (!r.finishing) {
2720 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2721 false);
2722 didOne = true;
2723 }
2724 }
2725 }
2726 }
2727 if (didOne) {
2728 mService.updateOomAdjLocked();
2729 }
2730 }
2731
Craig Mautnerd2328952013-03-05 12:46:26 -08002732 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002733 ArrayList<ActivityRecord> activities = r.task.mActivities;
2734 for (int index = activities.indexOf(r); index >= 0; --index) {
2735 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002736 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002737 break;
2738 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002739 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002740 }
2741 return true;
2742 }
2743
Dianne Hackborn5c607432012-02-28 14:44:19 -08002744 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2745 // send the result
2746 ActivityRecord resultTo = r.resultTo;
2747 if (resultTo != null) {
2748 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
2749 + " who=" + r.resultWho + " req=" + r.requestCode
2750 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002751 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002752 if (resultData != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002753 resultData.setContentUserHint(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002754 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002755 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002756 if (r.info.applicationInfo.uid > 0) {
2757 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2758 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002759 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002760 }
2761 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2762 resultData);
2763 r.resultTo = null;
2764 }
2765 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
2766
2767 // Make sure this HistoryRecord is not holding on to other resources,
2768 // because clients have remote IPC references to this object so we
2769 // can't assume that will go away and want to avoid circular IPC refs.
2770 r.results = null;
2771 r.pendingResults = null;
2772 r.newIntents = null;
2773 r.icicle = null;
2774 }
2775
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002776 /**
2777 * @return Returns true if this activity has been removed from the history
2778 * list, or false if it is still in the list and will be removed later.
2779 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002780 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2781 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002782 if (r.finishing) {
2783 Slog.w(TAG, "Duplicate finish request for " + r);
2784 return false;
2785 }
2786
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08002787 r.makeFinishing();
Craig Mautneraea74a52014-03-08 14:23:10 -08002788 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002789 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002790 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002791 task.taskId, r.shortComponentName, reason);
2792 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002793 final int index = activities.indexOf(r);
2794 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002795 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002796 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002797 // If the caller asked that this activity (and all above it)
2798 // be cleared when the task is reset, don't lose that information,
2799 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002800 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002801 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002802 }
2803 }
2804
2805 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07002806
Craig Mautner299f9602015-01-26 09:47:33 -08002807 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002808
Dianne Hackborn5c607432012-02-28 14:44:19 -08002809 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07002810
Craig Mautnerde4ef022013-04-07 19:01:33 -07002811 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08002812 boolean endTask = index <= 0;
Craig Mautner323f7802013-10-01 21:16:22 -07002813 if (DEBUG_VISBILITY || DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002814 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002815 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08002816 ? AppTransition.TRANSIT_TASK_CLOSE
2817 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08002818
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002819 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002820 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07002821
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002822 if (mPausingActivity == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002823 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
2824 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002825 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002826 }
2827
Craig Mautneraea74a52014-03-08 14:23:10 -08002828 if (endTask) {
2829 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
2830 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002831 } else if (r.state != ActivityState.PAUSING) {
2832 // If the activity is PAUSING, we will complete the finish once
2833 // it is done pausing; else we can just directly finish it here.
2834 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002835 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002836 } else {
2837 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
2838 }
2839
2840 return false;
2841 }
2842
Craig Mautnerf3333272013-04-22 10:55:53 -07002843 static final int FINISH_IMMEDIATELY = 0;
2844 static final int FINISH_AFTER_PAUSE = 1;
2845 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002846
Craig Mautnerf3333272013-04-22 10:55:53 -07002847 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002848 // First things first: if this activity is currently visible,
2849 // and the resumed activity is not yet visible, then hold off on
2850 // finishing until the resumed one becomes visible.
2851 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002852 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
2853 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07002854 if (mStackSupervisor.mStoppingActivities.size() > 3
2855 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002856 // If we already have a few activities waiting to stop,
2857 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07002858 // them out. Or if r is the last of activity of the last task the stack
2859 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07002860 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002861 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002862 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002863 }
2864 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002865 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
2866 + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002867 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002868 if (oomAdj) {
2869 mService.updateOomAdjLocked();
2870 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002871 return r;
2872 }
2873
2874 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002875 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002876 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002877 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002878 if (mResumedActivity == r) {
2879 mResumedActivity = null;
2880 }
2881 final ActivityState prevState = r.state;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002882 if (DEBUG_STATES) Slog.v(TAG, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002883 r.state = ActivityState.FINISHING;
2884
2885 if (mode == FINISH_IMMEDIATELY
2886 || prevState == ActivityState.STOPPED
2887 || prevState == ActivityState.INITIALIZING) {
2888 // If this activity is already stopped, we can just finish
2889 // it right now.
Craig Mautnerd163e752014-06-13 17:18:47 -07002890 r.makeFinishing();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002891 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002892 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002893 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002894 }
Craig Mautner8c14c152015-01-15 17:32:07 -08002895 if (DEBUG_CONTAINERS) Slog.d(TAG,
Craig Mautnerd163e752014-06-13 17:18:47 -07002896 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
2897 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07002898 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002899 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002900
2901 // Need to go through the full pause cycle to get this
2902 // activity into the stopped state and then finish it.
2903 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07002904 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02002905 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002906 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002907 return r;
2908 }
2909
Craig Mautneree36c772014-07-16 14:56:05 -07002910 void finishAllActivitiesLocked(boolean immediately) {
2911 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002912 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2913 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2914 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2915 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07002916 noActivitiesInStack = false;
2917 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002918 continue;
2919 }
Craig Mautneree36c772014-07-16 14:56:05 -07002920 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002921 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
2922 }
2923 }
Craig Mautneree36c772014-07-16 14:56:05 -07002924 if (noActivitiesInStack) {
2925 mActivityContainer.onTaskListEmptyLocked();
2926 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002927 }
2928
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002929 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
2930 // Basic case: for simple app-centric recents, we need to recreate
2931 // the task if the affinity has changed.
2932 if (srec == null || srec.task.affinity == null ||
2933 !srec.task.affinity.equals(destAffinity)) {
2934 return true;
2935 }
2936 // Document-centric case: an app may be split in to multiple documents;
2937 // they need to re-create their task if this current activity is the root
2938 // of a document, unless simply finishing it will return them to the the
2939 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07002940 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
2941 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07002942 // Okay, this activity is at the root of its task. What to do, what to do...
2943 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
2944 // Finishing won't return to an application, so we need to recreate.
2945 return true;
2946 }
2947 // We now need to get the task below it to determine what to do.
2948 int taskIdx = mTaskHistory.indexOf(srec.task);
2949 if (taskIdx <= 0) {
2950 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
2951 return false;
2952 }
2953 if (taskIdx == 0) {
2954 // At the bottom of the stack, nothing to go back to.
2955 return true;
2956 }
2957 TaskRecord prevTask = mTaskHistory.get(taskIdx);
2958 if (!srec.task.affinity.equals(prevTask.affinity)) {
2959 // These are different apps, so need to recreate.
2960 return true;
2961 }
2962 }
2963 return false;
2964 }
2965
Craig Mautnerd2328952013-03-05 12:46:26 -08002966 final boolean navigateUpToLocked(IBinder token, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002967 Intent resultData) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002968 final ActivityRecord srec = ActivityRecord.forToken(token);
Craig Mautner0247fc82013-02-28 14:32:06 -08002969 final TaskRecord task = srec.task;
2970 final ArrayList<ActivityRecord> activities = task.mActivities;
2971 final int start = activities.indexOf(srec);
2972 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002973 return false;
2974 }
2975 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08002976 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002977 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08002978 final ComponentName dest = destIntent.getComponent();
2979 if (start > 0 && dest != null) {
2980 for (int i = finishTo; i >= 0; i--) {
2981 ActivityRecord r = activities.get(i);
2982 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002983 r.info.name.equals(dest.getClassName())) {
2984 finishTo = i;
2985 parent = r;
2986 foundParentInTask = true;
2987 break;
2988 }
2989 }
2990 }
2991
2992 IActivityController controller = mService.mController;
2993 if (controller != null) {
2994 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
2995 if (next != null) {
2996 // ask watcher if this is allowed
2997 boolean resumeOK = true;
2998 try {
2999 resumeOK = controller.activityResuming(next.packageName);
3000 } catch (RemoteException e) {
3001 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003002 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003003 }
3004
3005 if (!resumeOK) {
3006 return false;
3007 }
3008 }
3009 }
3010 final long origId = Binder.clearCallingIdentity();
3011 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003012 ActivityRecord r = activities.get(i);
3013 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003014 // Only return the supplied result for the first activity finished
3015 resultCode = Activity.RESULT_CANCELED;
3016 resultData = null;
3017 }
3018
3019 if (parent != null && foundParentInTask) {
3020 final int parentLaunchMode = parent.info.launchMode;
3021 final int destIntentFlags = destIntent.getFlags();
3022 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3023 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3024 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3025 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003026 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3027 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003028 } else {
3029 try {
3030 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3031 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003032 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003033 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003034 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborn95465202014-09-15 16:21:55 -07003035 -1, parent.launchedFromUid, 0, null, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003036 foundParentInTask = res == ActivityManager.START_SUCCESS;
3037 } catch (RemoteException e) {
3038 foundParentInTask = false;
3039 }
3040 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003041 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003042 }
3043 }
3044 Binder.restoreCallingIdentity(origId);
3045 return foundParentInTask;
3046 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003047 /**
3048 * Perform the common clean-up of an activity record. This is called both
3049 * as part of destroyActivityLocked() (when destroying the client-side
3050 * representation) and cleaning things up as a result of its hosting
3051 * processing going away, in which case there is no remaining client-side
3052 * state to destroy so only the cleanup here is needed.
3053 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003054 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3055 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003056 if (mResumedActivity == r) {
3057 mResumedActivity = null;
3058 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003059 if (mPausingActivity == r) {
3060 mPausingActivity = null;
3061 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003062 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003063
3064 r.configDestroy = false;
3065 r.frozenBeforeDestroy = false;
3066
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003067 if (setState) {
3068 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (cleaning up)");
3069 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003070 if (DEBUG_APP) Slog.v(TAG, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003071 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003072 }
3073
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003074 // Make sure this record is no longer in the pending finishes list.
3075 // This could happen, for example, if we are trimming activities
3076 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003077 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003078 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003079
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003080 // Remove any pending results.
3081 if (r.finishing && r.pendingResults != null) {
3082 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3083 PendingIntentRecord rec = apr.get();
3084 if (rec != null) {
3085 mService.cancelIntentSenderLocked(rec, false);
3086 }
3087 }
3088 r.pendingResults = null;
3089 }
3090
3091 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003092 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003093 }
3094
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003095 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003096 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003097 if (getVisibleBehindActivity() == r) {
3098 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003099 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003100 }
3101
3102 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003103 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003104 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003105 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003106 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003107 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003108 }
3109
Craig Mautner299f9602015-01-26 09:47:33 -08003110 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003111 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003112 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
3113 r.makeFinishing();
3114 if (DEBUG_ADD_REMOVE) {
3115 RuntimeException here = new RuntimeException("here");
3116 here.fillInStackTrace();
3117 Slog.i(TAG, "Removing activity " + r + " from stack");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003118 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003119 r.takeFromHistory();
3120 removeTimeoutsForActivityLocked(r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003121 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003122 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003123 if (DEBUG_APP) Slog.v(TAG, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003124 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003125 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003126 if (VALIDATE_TOKENS) {
3127 validateAppTokensLocked();
3128 }
Craig Mautner312ba862014-02-10 17:55:01 -08003129 final TaskRecord task = r.task;
3130 if (task != null && task.removeActivity(r)) {
3131 if (DEBUG_STACK) Slog.i(TAG,
3132 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003133 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3134 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003135 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003136 }
Craig Mautner299f9602015-01-26 09:47:33 -08003137 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003138 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003139 cleanUpActivityServicesLocked(r);
3140 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003141 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003142
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003143 /**
3144 * Perform clean-up of service connections in an activity record.
3145 */
3146 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3147 // Throw away any services that have been bound by this activity.
3148 if (r.connections != null) {
3149 Iterator<ConnectionRecord> it = r.connections.iterator();
3150 while (it.hasNext()) {
3151 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003152 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003153 }
3154 r.connections = null;
3155 }
3156 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003157
Craig Mautneree2e45a2014-06-27 12:10:03 -07003158 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003159 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003160 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003161 mHandler.sendMessage(msg);
3162 }
3163
Craig Mautneree2e45a2014-06-27 12:10:03 -07003164 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003165 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003166 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003167 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3168 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3169 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3170 final ActivityRecord r = activities.get(activityNdx);
3171 if (r.finishing) {
3172 continue;
3173 }
3174 if (r.fullscreen) {
3175 lastIsOpaque = true;
3176 }
3177 if (owner != null && r.app != owner) {
3178 continue;
3179 }
3180 if (!lastIsOpaque) {
3181 continue;
3182 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003183 if (r.isDestroyable()) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003184 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3185 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003186 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003187 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003188 activityRemoved = true;
3189 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003190 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003191 }
3192 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003193 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003194 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003195 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003196 }
3197
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003198 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3199 if (r.isDestroyable()) {
3200 if (DEBUG_SWITCH) Slog.v(TAG, "Destroying " + r + " in state " + r.state
3201 + " resumed=" + mResumedActivity
3202 + " pausing=" + mPausingActivity + " for reason " + reason);
3203 return destroyActivityLocked(r, true, reason);
3204 }
3205 return false;
3206 }
3207
3208 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3209 String reason) {
3210 // Iterate over tasks starting at the back (oldest) first.
3211 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
3212 int maxTasks = tasks.size() / 4;
3213 if (maxTasks < 1) {
3214 maxTasks = 1;
3215 }
3216 int numReleased = 0;
3217 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3218 final TaskRecord task = mTaskHistory.get(taskNdx);
3219 if (!tasks.contains(task)) {
3220 continue;
3221 }
3222 if (DEBUG_RELEASE) Slog.d(TAG, "Looking for activities to release in " + task);
3223 int curNum = 0;
3224 final ArrayList<ActivityRecord> activities = task.mActivities;
3225 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3226 final ActivityRecord activity = activities.get(actNdx);
3227 if (activity.app == app && activity.isDestroyable()) {
3228 if (DEBUG_RELEASE) Slog.v(TAG, "Destroying " + activity
3229 + " in state " + activity.state + " resumed=" + mResumedActivity
3230 + " pausing=" + mPausingActivity + " for reason " + reason);
3231 destroyActivityLocked(activity, true, reason);
3232 if (activities.get(actNdx) != activity) {
3233 // Was removed from list, back up so we don't miss the next one.
3234 actNdx--;
3235 }
3236 curNum++;
3237 }
3238 }
3239 if (curNum > 0) {
3240 numReleased += curNum;
3241 maxTasks--;
3242 if (mTaskHistory.get(taskNdx) != task) {
3243 // The entire task got removed, back up so we don't miss the next one.
3244 taskNdx--;
3245 }
3246 }
3247 }
3248 if (DEBUG_RELEASE) Slog.d(TAG, "Done releasing: did " + numReleased + " activities");
3249 return numReleased;
3250 }
3251
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003252 /**
3253 * Destroy the current CLIENT SIDE instance of an activity. This may be
3254 * called both when actually finishing an activity, or when performing
3255 * a configuration switch where we destroy the current client-side object
3256 * but then create a new client-side object for this same HistoryRecord.
3257 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003258 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003259 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003260 TAG, "Removing activity from " + reason + ": token=" + r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003261 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
3262 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003263 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003264 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003265
3266 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003267
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003268 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003269
3270 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003271
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003272 if (hadApp) {
3273 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003274 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003275 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3276 mService.mHeavyWeightProcess = null;
3277 mService.mHandler.sendEmptyMessage(
3278 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3279 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003280 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003281 // Update any services we are bound to that might care about whether
3282 // their client may have activities.
3283 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003284 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003285 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003286 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003287 }
3288 }
3289
3290 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003291
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003292 try {
3293 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003294 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003295 r.configChangeFlags);
3296 } catch (Exception e) {
3297 // We can just ignore exceptions here... if the process
3298 // has crashed, our death notification will clean things
3299 // up.
3300 //Slog.w(TAG, "Exception thrown during finish", e);
3301 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003302 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003303 removedFromHistory = true;
3304 skipDestroy = true;
3305 }
3306 }
3307
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003308 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003309
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003310 // If the activity is finishing, we need to wait on removing it
3311 // from the list to give it a chance to do its cleanup. During
3312 // that time it may make calls back with its token so we need to
3313 // be able to find it on the list and so we don't want to remove
3314 // it from the list yet. Otherwise, we can just immediately put
3315 // it in the destroyed state since we are not removing it from the
3316 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003317 if (r.finishing && !skipDestroy) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003318 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYING: " + r
3319 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003320 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003321 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003322 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3323 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003324 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003325 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003326 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003327 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003328 }
3329 } else {
3330 // remove this record from the history.
3331 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003332 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003333 removedFromHistory = true;
3334 } else {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003335 if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003336 r.state = ActivityState.DESTROYED;
Dianne Hackborn07981492013-01-28 11:36:23 -08003337 if (DEBUG_APP) Slog.v(TAG, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003338 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003339 }
3340 }
3341
3342 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003343
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003344 if (!mLRUActivities.remove(r) && hadApp) {
3345 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3346 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003347
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003348 return removedFromHistory;
3349 }
3350
Craig Mautner299f9602015-01-26 09:47:33 -08003351 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003352 final long origId = Binder.clearCallingIdentity();
3353 try {
3354 ActivityRecord r = ActivityRecord.forToken(token);
3355 if (r != null) {
3356 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003357 }
Craig Mautnerd163e752014-06-13 17:18:47 -07003358 if (DEBUG_CONTAINERS) Slog.d(TAG, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003359
Wale Ogunwale60454db2015-01-23 16:05:07 -08003360 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003361 if (r.state == ActivityState.DESTROYING) {
3362 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003363 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003364 }
3365 }
Craig Mautner05d29032013-05-03 13:40:13 -07003366 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003367 } finally {
3368 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003369 }
3370 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003371
Todd Kennedyaab56db2015-01-30 09:39:53 -08003372 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003373 if (hasVisibleBehindActivity() &&
3374 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003375 if (r == topRunningActivityLocked(null)) {
3376 // Don't release the top activity if it has requested to run behind the next
3377 // activity.
3378 return;
3379 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003380 if (DEBUG_STATES) Slog.d(TAG, "releaseBackgroundResources activtyDisplay=" +
3381 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003382 " thread=" + r.app.thread);
3383 if (r != null && r.app != null && r.app.thread != null) {
3384 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003385 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003386 } catch (RemoteException e) {
3387 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003388 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003389 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003390 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003391 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003392 }
3393 }
3394 }
3395
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003396 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003397 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3398 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003399 if (r != null) {
3400 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003401 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003402 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003403 }
3404 mStackSupervisor.resumeTopActivitiesLocked();
3405 }
3406
Jose Lima4b6c6692014-08-12 17:41:12 -07003407 boolean hasVisibleBehindActivity() {
3408 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003409 }
3410
Jose Lima4b6c6692014-08-12 17:41:12 -07003411 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003412 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003413 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003414 }
3415 }
3416
Jose Lima4b6c6692014-08-12 17:41:12 -07003417 ActivityRecord getVisibleBehindActivity() {
3418 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003419 }
3420
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003421 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3422 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003423 int i = list.size();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003424 if (DEBUG_CLEANUP) Slog.v(
3425 TAG, "Removing app " + app + " from list " + listName
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003426 + " with " + i + " entries");
3427 while (i > 0) {
3428 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003429 ActivityRecord r = list.get(i);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003430 if (DEBUG_CLEANUP) Slog.v(TAG, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003431 if (r.app == app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003432 if (DEBUG_CLEANUP) Slog.v(TAG, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003433 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003434 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003435 }
3436 }
3437 }
3438
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003439 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3440 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003441 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3442 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003443 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3444 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003445 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003446 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003447 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3448 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003449
3450 boolean hasVisibleActivities = false;
3451
3452 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003453 int i = numActivities();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003454 if (DEBUG_CLEANUP) Slog.v(
3455 TAG, "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003456 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3457 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3458 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3459 final ActivityRecord r = activities.get(activityNdx);
3460 --i;
3461 if (DEBUG_CLEANUP) Slog.v(
3462 TAG, "Record #" + i + " " + r + ": app=" + r.app);
3463 if (r.app == app) {
3464 boolean remove;
3465 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3466 // Don't currently have state for the activity, or
3467 // it is finishing -- always remove it.
3468 remove = true;
3469 } else if (r.launchCount > 2 &&
3470 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3471 // We have launched this activity too many times since it was
3472 // able to run, so give up and remove it.
3473 remove = true;
3474 } else {
3475 // The process may be gone, but the activity lives on!
3476 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003477 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003478 if (remove) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003479 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003480 RuntimeException here = new RuntimeException("here");
3481 here.fillInStackTrace();
3482 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
3483 + ": haveState=" + r.haveState
3484 + " stateNotNeeded=" + r.stateNotNeeded
3485 + " finishing=" + r.finishing
3486 + " state=" + r.state, here);
3487 }
3488 if (!r.finishing) {
3489 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3490 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3491 r.userId, System.identityHashCode(r),
3492 r.task.taskId, r.shortComponentName,
3493 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003494 if (r.state == ActivityState.RESUMED) {
3495 mService.updateUsageStats(r, false);
3496 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003497 }
Craig Mautner299f9602015-01-26 09:47:33 -08003498 removeActivityFromHistoryLocked(r, "appDied");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003499
Craig Mautner0247fc82013-02-28 14:32:06 -08003500 } else {
3501 // We have the current state for this activity, so
3502 // it can be restarted later when needed.
3503 if (localLOGV) Slog.v(
3504 TAG, "Keeping entry, setting app to null");
3505 if (r.visible) {
3506 hasVisibleActivities = true;
3507 }
3508 if (DEBUG_APP) Slog.v(TAG, "Clearing app during removeHistory for activity "
3509 + r);
3510 r.app = null;
3511 r.nowVisible = false;
3512 if (!r.haveState) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003513 if (DEBUG_SAVED_STATE) Slog.i(TAG,
Craig Mautner0247fc82013-02-28 14:32:06 -08003514 "App died, clearing saved state of " + r);
3515 r.icicle = null;
3516 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003517 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003518
Craig Mautnerd2328952013-03-05 12:46:26 -08003519 cleanUpActivityLocked(r, true, true);
Craig Mautner0247fc82013-02-28 14:32:06 -08003520 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003521 }
3522 }
3523
3524 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003525 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003526
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003527 final void updateTransitLocked(int transit, Bundle options) {
3528 if (options != null) {
3529 ActivityRecord r = topRunningActivityLocked(null);
3530 if (r != null && r.state != ActivityState.RESUMED) {
3531 r.updateOptionsLocked(options);
3532 } else {
3533 ActivityOptions.abort(options);
3534 }
3535 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003536 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003537 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003538
Craig Mautner21d24a22014-04-23 11:45:37 -07003539 void updateTaskMovement(TaskRecord task, boolean toFront) {
3540 if (task.isPersistable) {
3541 task.mLastTimeMoved = System.currentTimeMillis();
3542 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3543 // recently will be most negative, tasks sent to the bottom before that will be less
3544 // negative. Similarly for recent tasks moved to the top which will be most positive.
3545 if (!toFront) {
3546 task.mLastTimeMoved *= -1;
3547 }
3548 }
3549 }
3550
Craig Mautner84984fa2014-06-19 11:19:20 -07003551 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003552 final int top = mTaskHistory.size() - 1;
3553 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3554 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003555 if (task.taskType == homeStackTaskType) {
3556 if (DEBUG_TASKS || DEBUG_STACK)
3557 Slog.d(TAG, "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003558 mTaskHistory.remove(taskNdx);
3559 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003560 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003561 mWindowManager.moveTaskToTop(task.taskId);
3562 return;
3563 }
3564 }
3565 }
3566
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003567 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, Bundle options,
Craig Mautner299f9602015-01-26 09:47:33 -08003568 String reason) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003569 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003570
Craig Mautner11bf9a52013-02-19 14:08:51 -08003571 final int numTasks = mTaskHistory.size();
3572 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003573 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003574 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003575 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003576 ActivityOptions.abort(options);
3577 } else {
3578 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3579 }
3580 return;
3581 }
3582
3583 // Shift all activities with this task up to the top
3584 // of the stack, keeping them in the same internal order.
Craig Mautnerac6f8432013-07-17 13:24:59 -07003585 insertTaskAtTop(tr);
Craig Mautner299f9602015-01-26 09:47:33 -08003586 moveToFront(reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003587
3588 if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003589 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003590 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003591 ActivityRecord r = topRunningActivityLocked(null);
3592 if (r != null) {
3593 mNoAnimActivities.add(r);
3594 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003595 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003596 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003597 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003598 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003599
Craig Mautner05d29032013-05-03 13:40:13 -07003600 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003601 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003602
3603 if (VALIDATE_TOKENS) {
3604 validateAppTokensLocked();
3605 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003606 }
3607
3608 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003609 * Worker method for rearranging history stack. Implements the function of moving all
3610 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003611 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003612 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003613 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3614 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003615 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003616 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003617 * @return Returns true if the move completed, false if not.
3618 */
Craig Mautner299f9602015-01-26 09:47:33 -08003619 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003620 final TaskRecord tr = taskForIdLocked(taskId);
3621 if (tr == null) {
3622 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3623 return false;
3624 }
3625
3626 Slog.i(TAG, "moveTaskToBack: " + tr);
3627
3628 mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003629
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003630 // If we have a watcher, preflight the move before committing to it. First check
3631 // for *other* available tasks, but if none are available, then try again allowing the
3632 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003633 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003634 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003635 if (next == null) {
3636 next = topRunningActivityLocked(null, 0);
3637 }
3638 if (next != null) {
3639 // ask watcher if this is allowed
3640 boolean moveOK = true;
3641 try {
3642 moveOK = mService.mController.activityResuming(next.packageName);
3643 } catch (RemoteException e) {
3644 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003645 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003646 }
3647 if (!moveOK) {
3648 return false;
3649 }
3650 }
3651 }
3652
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003653 if (DEBUG_TRANSITION) Slog.v(TAG,
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003654 "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003655
riddle_hsuc215a4f2014-12-27 12:10:45 +08003656 boolean prevIsHome = false;
3657 if (tr.isOverHomeStack()) {
3658 final TaskRecord nextTask = getNextTask(tr);
3659 if (nextTask != null) {
3660 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3661 } else {
3662 prevIsHome = true;
3663 }
3664 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003665 mTaskHistory.remove(tr);
3666 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003667 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003668
Craig Mautnerc8143c62013-09-03 12:15:57 -07003669 // There is an assumption that moving a task to the back moves it behind the home activity.
3670 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003671 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003672 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3673 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003674 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003675 break;
3676 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003677 if (taskNdx == 1) {
3678 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003679 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003680 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003681 }
3682
Craig Mautner299f9602015-01-26 09:47:33 -08003683 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003684 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003685
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003686 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003687 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003688 }
3689
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003690 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
riddle_hsuc215a4f2014-12-27 12:10:45 +08003691 if (prevIsHome || task == tr && tr.isOverHomeStack()
3692 || numTasks <= 1 && isOnHomeDisplay()) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003693 if (!mService.mBooting && !mService.mBooted) {
3694 // Not ready yet!
3695 return false;
3696 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003697 final int taskToReturnTo = tr.getTaskToReturnTo();
3698 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003699 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003700 }
3701
Craig Mautner05d29032013-05-03 13:40:13 -07003702 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003703 return true;
3704 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003705
Craig Mautner8849a5e2013-04-02 16:41:03 -07003706 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003707 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003708 final Uri data = r.intent.getData();
3709 final String strData = data != null ? data.toSafeString() : null;
3710
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003711 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003712 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003713 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003714 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003715 }
3716
3717 /**
3718 * Make sure the given activity matches the current configuration. Returns
3719 * false if the activity had to be destroyed. Returns true if the
3720 * configuration is the same, or the activity will remain running as-is
3721 * for whatever reason. Ensures the HistoryRecord is updated with the
3722 * correct configuration and all other bookkeeping is handled.
3723 */
3724 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3725 int globalChanges) {
3726 if (mConfigWillChange) {
3727 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3728 "Skipping config check (will change): " + r);
3729 return true;
3730 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003731
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003732 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3733 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003734
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003735 // Make sure the current stack override configuration is supported by the top task
3736 // before continuing.
3737 final TaskRecord topTask = topTask();
3738 if (topTask != null && ((topTask.mResizeable && mForcedFullscreen)
3739 || (!topTask.mResizeable && !mFullscreen))) {
3740 final boolean prevFullscreen = mFullscreen;
3741 final Configuration newOverrideConfig =
3742 mWindowManager.forceStackToFullscreen(mStackId, !topTask.mResizeable);
3743 updateOverrideConfiguration(newOverrideConfig);
3744 mForcedFullscreen = !prevFullscreen && mFullscreen;
3745 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3746 "Updated stack config to support task=" + topTask
3747 + " resizeable=" + topTask.mResizeable
3748 + " mForcedFullscreen=" + mForcedFullscreen
3749 + " prevFullscreen=" + prevFullscreen
3750 + " mFullscreen=" + mFullscreen);
3751 }
3752
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003753 // Short circuit: if the two configurations are the exact same
3754 // object (the common case), then there is nothing to do.
3755 Configuration newConfig = mService.mConfiguration;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003756 if (r.configuration == newConfig
3757 && r.stackConfigOverride == mOverrideConfig
3758 && !r.forceNewConfig) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003759 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3760 "Configuration unchanged in " + r);
3761 return true;
3762 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003763
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003764 // We don't worry about activities that are finishing.
3765 if (r.finishing) {
3766 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3767 "Configuration doesn't matter in finishing " + r);
3768 r.stopFreezingScreenLocked(false);
3769 return true;
3770 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003771
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003772 // Okay we now are going to make this activity have the new config.
3773 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003774 final Configuration oldConfig = r.configuration;
3775 final Configuration oldStackOverride = r.stackConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003776 r.configuration = newConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003777 r.stackConfigOverride = mOverrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003778
3779 // Determine what has changed. May be nothing, if this is a config
3780 // that has come back from the app after going idle. In that case
3781 // we just want to leave the official config object now in the
3782 // activity and do nothing else.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003783 int stackChanges = oldStackOverride.diff(mOverrideConfig);
Wale Ogunwalea9281272015-02-07 14:04:19 -08003784 if (stackChanges == 0) {
3785 // {@link Configuration#diff} doesn't catch changes from unset values.
3786 // Check for changes we care about.
3787 if (oldStackOverride.orientation != mOverrideConfig.orientation) {
3788 stackChanges |= ActivityInfo.CONFIG_ORIENTATION;
3789 }
3790 if (oldStackOverride.screenHeightDp != mOverrideConfig.screenHeightDp
3791 || oldStackOverride.screenWidthDp != mOverrideConfig.screenWidthDp) {
3792 stackChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
3793 }
3794 if (oldStackOverride.smallestScreenWidthDp != mOverrideConfig.smallestScreenWidthDp) {
3795 stackChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3796 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08003797 }
3798 final int changes = oldConfig.diff(newConfig) | stackChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003799 if (changes == 0 && !r.forceNewConfig) {
3800 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3801 "Configuration no differences in " + r);
3802 return true;
3803 }
3804
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003805 // If the activity isn't currently running, just leave the new
3806 // configuration and it will pick that up next time it starts.
3807 if (r.app == null || r.app.thread == null) {
3808 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
3809 "Configuration doesn't matter not running " + r);
3810 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003811 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003812 return true;
3813 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003814
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003815 // Figure out how to handle the changes between the configurations.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003816 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
3817 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
3818 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborne6676352011-06-01 16:51:20 -07003819 + Integer.toHexString(r.info.getRealConfigChanged())
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003820 + ", newConfig=" + newConfig);
3821 }
Dianne Hackborne6676352011-06-01 16:51:20 -07003822 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003823 // Aha, the activity isn't handling the change, so DIE DIE DIE.
3824 r.configChangeFlags |= changes;
3825 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003826 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003827 if (r.app == null || r.app.thread == null) {
3828 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003829 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003830 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003831 } else if (r.state == ActivityState.PAUSING) {
3832 // A little annoying: we are waiting for this activity to
3833 // finish pausing. Let's not do anything now, but just
3834 // flag that it needs to be restarted when done pausing.
3835 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003836 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003837 r.configDestroy = true;
3838 return true;
3839 } else if (r.state == ActivityState.RESUMED) {
3840 // Try to optimize this case: the configuration is changing
3841 // and we need to restart the top, resumed activity.
3842 // Instead of doing the normal handshaking, just say
3843 // "restart!".
3844 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003845 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003846 relaunchActivityLocked(r, r.configChangeFlags, true);
3847 r.configChangeFlags = 0;
3848 } else {
3849 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003850 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003851 relaunchActivityLocked(r, r.configChangeFlags, false);
3852 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003853 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003854
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07003855 // All done... tell the caller we weren't able to keep this
3856 // activity around.
3857 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003858 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003859
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003860 // Default case: the activity can handle this new configuration, so hand it over.
3861 // NOTE: We only forward the stack override configuration as the system level configuration
3862 // changes is always sent to all processes when they happen so it can just use whatever
3863 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003864 if (r.app != null && r.app.thread != null) {
3865 try {
3866 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003867 r.app.thread.scheduleActivityConfigurationChanged(
3868 r.appToken, new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003869 } catch (RemoteException e) {
3870 // If process died, whatever.
3871 }
3872 }
3873 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003874
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003875 return true;
3876 }
3877
Craig Mautnerc8143c62013-09-03 12:15:57 -07003878 private boolean relaunchActivityLocked(ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003879 int changes, boolean andResume) {
3880 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003881 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003882 if (andResume) {
3883 results = r.results;
3884 newIntents = r.newIntents;
3885 }
3886 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
3887 + " with results=" + results + " newIntents=" + newIntents
3888 + " andResume=" + andResume);
3889 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003890 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003891 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003892
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003893 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003894
Craig Mautner34b73df2014-01-12 21:11:08 -08003895 mStackSupervisor.removeChildActivityContainers(r);
3896
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003897 try {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003898 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG,
3899 (andResume ? "Relaunching to RESUMED " : "Relaunching to PAUSED ")
3900 + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003901 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003902 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
3903 !andResume, new Configuration(mService.mConfiguration),
3904 new Configuration(mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003905 // Note: don't need to call pauseIfSleepingLocked() here, because
3906 // the caller will only pass in 'andResume' if this activity is
3907 // currently resumed, which implies we aren't sleeping.
3908 } catch (RemoteException e) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003909 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003910 }
3911
3912 if (andResume) {
3913 r.results = null;
3914 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07003915 r.state = ActivityState.RESUMED;
3916 } else {
3917 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
3918 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003919 }
3920
3921 return true;
3922 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003923
3924 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003925 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3926 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3927 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3928 final ActivityRecord r = activities.get(activityNdx);
3929 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003930 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003931 }
3932 if (r.fullscreen && !r.finishing) {
3933 return false;
3934 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003935 }
3936 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003937 final ActivityRecord r = ActivityRecord.forToken(token);
3938 if (r == null) {
3939 return false;
3940 }
3941 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
3942 + " would have returned true for r=" + r);
3943 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08003944 }
3945
3946 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003947 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3948 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3949 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3950 final ActivityRecord r = activities.get(activityNdx);
3951 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003952 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003953 }
Craig Mautnercae015f2013-02-08 14:31:27 -08003954 }
3955 }
3956 }
3957
3958 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
3959 boolean didSomething = false;
3960 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003961 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08003962 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3963 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3964 int numActivities = activities.size();
3965 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
3966 ActivityRecord r = activities.get(activityNdx);
3967 final boolean samePackage = r.packageName.equals(name)
3968 || (name == null && r.userId == userId);
3969 if ((userId == UserHandle.USER_ALL || r.userId == userId)
3970 && (samePackage || r.task == lastTask)
3971 && (r.app == null || evenPersistent || !r.app.persistent)) {
3972 if (!doit) {
3973 if (r.finishing) {
3974 // If this activity is just finishing, then it is not
3975 // interesting as far as something to stop.
3976 continue;
3977 }
3978 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08003979 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00003980 if (r.isHomeActivity()) {
3981 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
3982 Slog.i(TAG, "Skip force-stop again " + r);
3983 continue;
3984 } else {
3985 homeActivity = r.realActivity;
3986 }
3987 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003988 didSomething = true;
3989 Slog.i(TAG, " Force finishing activity " + r);
3990 if (samePackage) {
3991 if (r.app != null) {
3992 r.app.removed = true;
3993 }
3994 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08003995 }
Craig Mautner56f52db2013-02-25 10:03:01 -08003996 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07003997 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
3998 true)) {
3999 // r has been deleted from mActivities, accommodate.
4000 --numActivities;
4001 --activityNdx;
4002 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004003 }
4004 }
4005 }
4006 return didSomething;
4007 }
4008
Dianne Hackborn09233282014-04-30 11:33:59 -07004009 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004010 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004011 final TaskRecord task = mTaskHistory.get(taskNdx);
4012 ActivityRecord r = null;
4013 ActivityRecord top = null;
4014 int numActivities = 0;
4015 int numRunning = 0;
4016 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautner5cbaaa32013-10-29 13:39:26 -07004017 if (activities.isEmpty()) {
4018 continue;
4019 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004020 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004021 continue;
4022 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004023 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4024 r = activities.get(activityNdx);
Craig Mautnercae015f2013-02-08 14:31:27 -08004025
Craig Mautneraab647e2013-02-28 16:31:36 -08004026 // Initialize state for next task if needed.
4027 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4028 top = r;
4029 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004030 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004031
4032 // Add 'r' into the current task.
4033 numActivities++;
4034 if (r.app != null && r.app.thread != null) {
4035 numRunning++;
4036 }
4037
4038 if (localLOGV) Slog.v(
4039 TAG, r.intent.getComponent().flattenToShortString()
4040 + ": task=" + r.task);
4041 }
4042
4043 RunningTaskInfo ci = new RunningTaskInfo();
4044 ci.id = task.taskId;
4045 ci.baseActivity = r.intent.getComponent();
4046 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004047 ci.lastActiveTime = task.lastActiveTime;
4048
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004049 if (top.task != null) {
4050 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004051 }
4052 ci.numActivities = numActivities;
4053 ci.numRunning = numRunning;
4054 //System.out.println(
4055 // "#" + maxNum + ": " + " descr=" + ci.description);
Craig Mautneraab647e2013-02-28 16:31:36 -08004056 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004057 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004058 }
4059
4060 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004061 final int top = mTaskHistory.size() - 1;
Craig Mautnercae015f2013-02-08 14:31:27 -08004062 if (DEBUG_SWITCH) Slog.d(
4063 TAG, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004064 if (top >= 0) {
4065 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4066 int activityTop = activities.size() - 1;
4067 if (activityTop > 0) {
4068 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4069 "unhandled-back", true);
4070 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004071 }
4072 }
4073
Craig Mautner6b74cb52013-09-27 17:02:21 -07004074 /**
4075 * Reset local parameters because an app's activity died.
4076 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004077 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004078 */
4079 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004080 if (mPausingActivity != null && mPausingActivity.app == app) {
4081 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG,
4082 "App died while pausing: " + mPausingActivity);
4083 mPausingActivity = null;
4084 }
4085 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4086 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004087 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004088 }
4089
Craig Mautner19091252013-10-05 00:03:53 -07004090 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004091 }
4092
Craig Mautnercae015f2013-02-08 14:31:27 -08004093 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004094 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4095 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4096 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4097 final ActivityRecord r = activities.get(activityNdx);
4098 if (r.app == app) {
4099 Slog.w(TAG, " Force finishing activity "
4100 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004101 // Force the destroy to skip right to removal.
4102 r.app = null;
4103 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004104 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004105 }
4106 }
4107 }
4108
Dianne Hackborn390517b2013-05-30 15:03:32 -07004109 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004110 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004111 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004112 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4113 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004114 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4115 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004116 dumpClient, dumpPackage, needSep, header,
Craig Mautnerac6f8432013-07-17 13:24:59 -07004117 " Task id #" + task.taskId);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004118 if (printed) {
4119 header = null;
4120 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004121 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004122 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004123 }
4124
4125 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4126 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4127
4128 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004129 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4130 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004131 }
4132 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004133 final int top = mTaskHistory.size() - 1;
4134 if (top >= 0) {
4135 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4136 int listTop = list.size() - 1;
4137 if (listTop >= 0) {
4138 activities.add(list.get(listTop));
4139 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004140 }
4141 } else {
4142 ItemMatcher matcher = new ItemMatcher();
4143 matcher.build(name);
4144
Craig Mautneraab647e2013-02-28 16:31:36 -08004145 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4146 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4147 if (matcher.match(r1, r1.intent.getComponent())) {
4148 activities.add(r1);
4149 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004150 }
4151 }
4152 }
4153
4154 return activities;
4155 }
4156
4157 ActivityRecord restartPackage(String packageName) {
4158 ActivityRecord starting = topRunningActivityLocked(null);
4159
4160 // All activities that came from the package must be
4161 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004162 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4163 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4164 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4165 final ActivityRecord a = activities.get(activityNdx);
4166 if (a.info.packageName.equals(packageName)) {
4167 a.forceNewConfig = true;
4168 if (starting != null && a == starting && a.visible) {
4169 a.startFreezingScreenLocked(starting.app,
4170 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4171 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004172 }
4173 }
4174 }
4175
4176 return starting;
4177 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004178
Craig Mautner299f9602015-01-26 09:47:33 -08004179 void removeTask(TaskRecord task, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004180 mStackSupervisor.endLockTaskModeIfTaskEnding(task);
Craig Mautner04a0ea62014-01-13 12:51:26 -08004181 mWindowManager.removeTask(task.taskId);
4182 final ActivityRecord r = mResumedActivity;
4183 if (r != null && r.task == task) {
4184 mResumedActivity = null;
4185 }
4186
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004187 final int taskNdx = mTaskHistory.indexOf(task);
4188 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004189 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4190 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4191 if (!nextTask.isOverHomeStack()) {
4192 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4193 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004194 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004195 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004196 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004197
4198 if (task.mActivities.isEmpty()) {
4199 final boolean isVoiceSession = task.voiceSession != null;
4200 if (isVoiceSession) {
4201 try {
4202 task.voiceSession.taskFinished(task.intent, task.taskId);
4203 } catch (RemoteException e) {
4204 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004205 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004206 if (task.autoRemoveFromRecents() || isVoiceSession) {
4207 // Task creator asked to remove this when done, or this task was a voice
4208 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004209 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004210 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004211 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004212 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004213
4214 if (mTaskHistory.isEmpty()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004215 if (DEBUG_STACK) Slog.i(TAG, "removeTask: removing stack=" + this);
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004216 final boolean notHomeStack = !isHomeStack();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004217 if (isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004218 String myReason = reason + " leftTaskHistoryEmpty";
4219 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
4220 mStackSupervisor.moveHomeStack(notHomeStack, myReason);
4221 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004222 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004223 if (mStacks != null) {
4224 mStacks.remove(this);
4225 mStacks.add(0, this);
4226 }
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004227 if (notHomeStack) {
4228 mActivityContainer.onTaskListEmptyLocked();
4229 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004230 }
Wale Ogunwale0f95e3f2015-02-19 16:19:15 -08004231
4232 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004233 }
4234
Dianne Hackborn91097de2014-04-04 18:02:06 -07004235 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4236 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4237 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004238 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4239 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004240 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004241 return task;
4242 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004243
4244 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004245 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004246 }
4247
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004248 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004249 task.stack = this;
4250 if (toTop) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07004251 insertTaskAtTop(task);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004252 } else {
4253 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004254 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004255 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004256 if (!moving && task.voiceSession != null) {
4257 try {
4258 task.voiceSession.taskStarted(task.intent, task.taskId);
4259 } catch (RemoteException e) {
4260 }
4261 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004262 }
4263
4264 public int getStackId() {
4265 return mStackId;
4266 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004267
4268 @Override
4269 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004270 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4271 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004272 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004273
4274 boolean updateOverrideConfiguration(Configuration newConfig) {
4275 Configuration oldConfig = mOverrideConfig;
4276 mOverrideConfig = (newConfig == null) ? Configuration.EMPTY : newConfig;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004277 // We override the configuration only when the stack's dimensions are different from
4278 // the display. In this manner, we know that if the override configuration is empty,
4279 // the stack is necessarily full screen.
Todd Kennedyaab56db2015-01-30 09:39:53 -08004280 mFullscreen = Configuration.EMPTY.equals(mOverrideConfig);
Wale Ogunwale60454db2015-01-23 16:05:07 -08004281 return !mOverrideConfig.equals(oldConfig);
4282 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004283}