blob: 8b637e0ae44d4b633eacca1e20c67010cf4d718f [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
Wale Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
20import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
21import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
22import static android.app.ActivityManager.StackId.HOME_STACK_ID;
Wale Ogunwale56d75cf2016-03-09 15:14:47 -080023import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070024import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -080025import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
26import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
27import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
28import static android.content.pm.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -080029import static android.content.pm.ActivityInfo.FLAG_RESUME_WHILE_PAUSING;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070030import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -080031import static android.content.res.Configuration.SCREENLAYOUT_UNDEFINED;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070032import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ADD_REMOVE;
33import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
34import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_APP;
35import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CLEANUP;
36import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONFIGURATION;
37import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONTAINERS;
38import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKSCREEN;
39import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_PAUSE;
40import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RELEASE;
41import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RESULTS;
42import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SAVED_STATE;
43import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SCREENSHOTS;
44import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STACK;
45import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
46import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TRANSITION;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_USER_LEAVING;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBILITY;
51import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_ADD_REMOVE;
52import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_APP;
53import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CLEANUP;
54import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONFIGURATION;
55import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONTAINERS;
56import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_PAUSE;
57import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RELEASE;
58import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RESULTS;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SAVED_STATE;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SCREENSHOTS;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STACK;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TRANSITION;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_USER_LEAVING;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBILITY;
68import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
69import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Chong Zhang75b37202015-12-04 14:16:36 -080070import static com.android.server.am.ActivityManagerService.LOCK_SCREEN_SHOWN;
Craig Mautner84984fa2014-06-19 11:19:20 -070071import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070072import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Wale Ogunwalef40c11b2016-02-26 08:16:02 -080073import static com.android.server.am.ActivityRecord.STARTING_WINDOW_REMOVED;
74import static com.android.server.am.ActivityRecord.STARTING_WINDOW_SHOWN;
Wale Ogunwale39381972015-12-17 17:15:29 -080075import static com.android.server.am.ActivityStackSupervisor.FindTaskResult;
Wale Ogunwale040b4702015-08-06 18:10:50 -070076import static com.android.server.am.ActivityStackSupervisor.MOVING;
Wale Ogunwale06579d62016-04-30 15:29:06 -070077import static com.android.server.am.ActivityStackSupervisor.ON_TOP;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070078import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070079import static com.android.server.wm.AppTransition.TRANSIT_ACTIVITY_CLOSE;
80import static com.android.server.wm.AppTransition.TRANSIT_ACTIVITY_OPEN;
81import static com.android.server.wm.AppTransition.TRANSIT_NONE;
82import static com.android.server.wm.AppTransition.TRANSIT_TASK_CLOSE;
83import static com.android.server.wm.AppTransition.TRANSIT_TASK_OPEN;
84import static com.android.server.wm.AppTransition.TRANSIT_TASK_OPEN_BEHIND;
85import static com.android.server.wm.AppTransition.TRANSIT_TASK_TO_BACK;
86import static com.android.server.wm.AppTransition.TRANSIT_TASK_TO_FRONT;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070087
88import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070089import android.app.ActivityManager;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070090import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -070091import android.app.ActivityManager.StackId;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070092import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070093import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080094import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070095import android.app.ResultInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070096import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070097import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070098import android.content.pm.ActivityInfo;
Todd Kennedy39bfee52016-02-24 10:28:21 -080099import android.content.pm.ApplicationInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700100import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800101import android.graphics.Bitmap;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700102import android.graphics.Point;
103import android.graphics.Rect;
Santos Cordon73ff7d82013-03-06 17:24:11 -0800104import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700105import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700106import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -0800107import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700108import android.os.Handler;
109import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +0900110import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700111import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -0700112import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700113import android.os.RemoteException;
114import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -0800115import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700116import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -0700117import android.service.voice.IVoiceInteractionSession;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700118import android.util.ArraySet;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700119import android.util.EventLog;
Jorim Jaggi023da532016-04-20 22:42:32 -0700120import android.util.Log;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700121import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -0700122import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700123
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700124import com.android.internal.app.IVoiceInteractor;
125import com.android.internal.content.ReferrerIntent;
126import com.android.internal.os.BatteryStatsImpl;
127import com.android.server.Watchdog;
128import com.android.server.am.ActivityManagerService.ItemMatcher;
129import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
130import com.android.server.wm.TaskGroup;
131import com.android.server.wm.WindowManagerService;
132
Craig Mautnercae015f2013-02-08 14:31:27 -0800133import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -0800134import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700135import java.lang.ref.WeakReference;
136import java.util.ArrayList;
137import java.util.Iterator;
138import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -0800139import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700140import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700141
142/**
143 * State and management of a single stack of activities.
144 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700145final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800146
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800147 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700148 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
149 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800150 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700151 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700152 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700153 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700154 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700155 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700156 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
157 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700158 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700159 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700160 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
161 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
162 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
163 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
164 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700165
166 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800167
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700168 // Ticks during which we check progress while waiting for an app to launch.
169 static final int LAUNCH_TICK = 500;
170
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700171 // How long we wait until giving up on the last activity to pause. This
172 // is short because it directly impacts the responsiveness of starting the
173 // next activity.
174 static final int PAUSE_TIMEOUT = 500;
175
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700176 // How long we wait for the activity to tell us it has stopped before
177 // giving up. This is a good amount of time because we really need this
178 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700179 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700180
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700181 // How long we wait until giving up on an activity telling us it has
182 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700183 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800184
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700185 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800186 // disabled.
187 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800188
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700189 // How long between activity launches that we consider safe to not warn
190 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700191 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800192
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700193 // Set to false to disable the preview that is shown while a new activity
194 // is being started.
195 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800196
Craig Mautner5eda9b32013-07-02 11:58:16 -0700197 // How long to wait for all background Activities to redraw following a call to
198 // convertToTranslucent().
199 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
200
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -0800201 // How many activities have to be scheduled to stop to force a stop pass.
202 private static final int MAX_STOPPING_TO_FORCE = 3;
203
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700204 enum ActivityState {
205 INITIALIZING,
206 RESUMED,
207 PAUSING,
208 PAUSED,
209 STOPPING,
210 STOPPED,
211 FINISHING,
212 DESTROYING,
213 DESTROYED
214 }
215
Filip Gruszczynski0e381e22016-01-14 16:31:33 -0800216 // Stack is not considered visible.
217 static final int STACK_INVISIBLE = 0;
218 // Stack is considered visible
219 static final int STACK_VISIBLE = 1;
220 // Stack is considered visible, but only becuase it has activity that is visible behind other
221 // activities and there is a specific combination of stacks.
222 static final int STACK_VISIBLE_ACTIVITY_BEHIND = 2;
223
Wale Ogunwale06579d62016-04-30 15:29:06 -0700224 /* The various modes for the method {@link #removeTask}. */
225 // Task is being completely removed from all stacks in the system.
226 static final int REMOVE_TASK_MODE_DESTROYING = 0;
227 // Task is being removed from this stack so we can add it to another stack. In the case we are
228 // moving we don't want to perform some operations on the task like removing it from window
229 // manager or recents.
230 static final int REMOVE_TASK_MODE_MOVING = 1;
231 // Similar to {@link #REMOVE_TASK_MODE_MOVING} and the task will be added to the top of its new
232 // stack and the new stack will be on top of all stacks.
233 static final int REMOVE_TASK_MODE_MOVING_TO_TOP = 2;
234
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700235 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700236 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800237 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800238
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700239 /**
240 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800241 * running) activities. It contains #TaskRecord objects.
242 */
Todd Kennedy39bfee52016-02-24 10:28:21 -0800243 private final ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800244
245 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800246 * Used for validating app tokens with window manager.
247 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800248 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800249
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700250 /**
251 * List of running activities, sorted by recent usage.
252 * The first entry in the list is the least recently used.
253 * It contains HistoryRecord objects.
254 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800255 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700256
257 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700258 * Animations that for the current transition have requested not to
259 * be considered for the transition animation.
260 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800261 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700262
263 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700264 * When we are in the process of pausing an activity, before starting the
265 * next one, this variable holds the activity that is currently being paused.
266 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800267 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700268
269 /**
270 * This is the last activity that we put into the paused state. This is
271 * used to determine if we need to do an activity transition while sleeping,
272 * when we normally hold the top activity paused.
273 */
274 ActivityRecord mLastPausedActivity = null;
275
276 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700277 * Activities that specify No History must be removed once the user navigates away from them.
278 * If the device goes to sleep with such an activity in the paused state then we save it here
279 * and finish it later if another activity replaces it on wakeup.
280 */
281 ActivityRecord mLastNoHistoryActivity = null;
282
283 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700284 * Current activity that is resumed, or null if there is none.
285 */
286 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800287
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700288 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700289 * This is the last activity that has been started. It is only used to
290 * identify when multiple activities are started at once so that the user
291 * can be warned they may not be in the activity they think they are.
292 */
293 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800294
Craig Mautner5eda9b32013-07-02 11:58:16 -0700295 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
296 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
297 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
298 // Activity in mTranslucentActivityWaiting is notified via
299 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
300 // background activity being drawn then the same call will be made with a true value.
301 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700302 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700303
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700304 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700305 * Set when we know we are going to be calling updateConfiguration()
306 * soon, so want to skip intermediate config checks.
307 */
308 boolean mConfigWillChange;
309
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700310 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800311 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700312 // Current bounds of the stack or null if fullscreen.
313 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800314
Jorim Jaggi192086e2016-03-11 17:17:03 +0100315 boolean mUpdateBoundsDeferred;
316 boolean mUpdateBoundsDeferredCalled;
317 final Rect mDeferredBounds = new Rect();
318 final Rect mDeferredTaskBounds = new Rect();
319 final Rect mDeferredTaskInsetBounds = new Rect();
320
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700321 long mLaunchStartTime = 0;
322 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800323
Craig Mautner858d8a62013-04-23 17:08:34 -0700324 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700325
Craig Mautnerc00204b2013-03-05 15:02:14 -0800326 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800327 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800328 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
329 ArrayList<ActivityStack> mStacks;
330 /** The attached Display's unique identifier, or -1 if detached */
331 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800332
Craig Mautner27084302013-03-25 08:05:25 -0700333 /** Run all ActivityStacks through this */
334 final ActivityStackSupervisor mStackSupervisor;
335
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700336 private final LaunchingTaskPositioner mTaskPositioner;
337
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700338 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700339 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
340 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
341 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
342 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700343 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700344 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700345 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700346
347 static class ScheduleDestroyArgs {
348 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700349 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700350 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700351 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700352 mReason = reason;
353 }
354 }
355
Zoran Marcetaf958b322012-08-09 20:27:12 +0900356 final Handler mHandler;
357
358 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800359
Craig Mautnerc8143c62013-09-03 12:15:57 -0700360 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900361 super(looper);
362 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700363
Zoran Marcetaf958b322012-08-09 20:27:12 +0900364 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700365 public void handleMessage(Message msg) {
366 switch (msg.what) {
367 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800368 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700369 // We don't at this point know if the activity is fullscreen,
370 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800371 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700372 synchronized (mService) {
373 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700374 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700375 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700376 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800377 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700378 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700379 case LAUNCH_TICK_MSG: {
380 ActivityRecord r = (ActivityRecord)msg.obj;
381 synchronized (mService) {
382 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700383 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700384 }
385 }
386 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700387 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800388 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700389 // We don't at this point know if the activity is fullscreen,
390 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800391 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800392 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800393 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800394 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700395 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700396 case STOP_TIMEOUT_MSG: {
397 ActivityRecord r = (ActivityRecord)msg.obj;
398 // We don't at this point know if the activity is fullscreen,
399 // so we need to be conservative and assume it isn't.
400 Slog.w(TAG, "Activity stop timeout for " + r);
401 synchronized (mService) {
402 if (r.isInHistory()) {
403 activityStoppedLocked(r, null, null, null);
404 }
405 }
406 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700407 case DESTROY_ACTIVITIES_MSG: {
408 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
409 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700410 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700411 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700412 } break;
413 case TRANSLUCENT_TIMEOUT_MSG: {
414 synchronized (mService) {
415 notifyActivityDrawnLocked(null);
416 }
417 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700418 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700419 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700420 final ActivityRecord r = getVisibleBehindActivity();
421 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700422 if (r != null) {
423 mService.killAppAtUsersRequest(r.app, null);
424 }
425 }
426 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700427 }
428 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800429 }
430
Craig Mautner34b73df2014-01-12 21:11:08 -0800431 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800432 int count = 0;
433 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
434 count += mTaskHistory.get(taskNdx).mActivities.size();
435 }
436 return count;
437 }
438
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800439 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
440 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800441 mActivityContainer = activityContainer;
442 mStackSupervisor = activityContainer.getOuter();
443 mService = mStackSupervisor.mService;
444 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
445 mWindowManager = mService.mWindowManager;
446 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700447 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800448 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700449 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
450 ? new LaunchingTaskPositioner() : null;
451 }
452
453 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
454 mDisplayId = activityDisplay.mDisplayId;
455 mStacks = activityDisplay.mStacks;
456 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
457 mFullscreen = mBounds == null;
458 if (mTaskPositioner != null) {
459 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
460 mTaskPositioner.configure(mBounds);
461 }
Wale Ogunwale961f4852016-02-01 20:25:54 -0800462
463 if (mStackId == DOCKED_STACK_ID) {
464 // If we created a docked stack we want to resize it so it resizes all other stacks
465 // in the system.
466 mStackSupervisor.resizeDockedStackLocked(
467 mBounds, null, null, null, null, PRESERVE_WINDOWS);
468 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700469 }
470
471 void detachDisplay() {
472 mDisplayId = Display.INVALID_DISPLAY;
473 mStacks = null;
474 if (mTaskPositioner != null) {
475 mTaskPositioner.reset();
476 }
477 mWindowManager.detachStack(mStackId);
Jorim Jaggi899327f2016-02-25 20:44:18 -0500478 if (mStackId == DOCKED_STACK_ID) {
479 // If we removed a docked stack we want to resize it so it resizes all other stacks
480 // in the system to fullscreen.
481 mStackSupervisor.resizeDockedStackLocked(
482 null, null, null, null, null, PRESERVE_WINDOWS);
483 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700484 }
485
Winsonc809cbb2015-11-02 12:06:15 -0800486 public void getDisplaySize(Point out) {
487 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
488 }
489
Jorim Jaggi192086e2016-03-11 17:17:03 +0100490 /**
491 * Defers updating the bounds of the stack. If the stack was resized/repositioned while
492 * deferring, the bounds will update in {@link #continueUpdateBounds()}.
493 */
494 void deferUpdateBounds() {
495 if (!mUpdateBoundsDeferred) {
496 mUpdateBoundsDeferred = true;
497 mUpdateBoundsDeferredCalled = false;
498 }
499 }
500
501 /**
502 * Continues updating bounds after updates have been deferred. If there was a resize attempt
503 * between {@link #deferUpdateBounds()} and {@link #continueUpdateBounds()}, the stack will
504 * be resized to that bounds.
505 */
506 void continueUpdateBounds() {
507 final boolean wasDeferred = mUpdateBoundsDeferred;
508 mUpdateBoundsDeferred = false;
509 if (wasDeferred && mUpdateBoundsDeferredCalled) {
510 mStackSupervisor.resizeStackUncheckedLocked(this,
511 mDeferredBounds.isEmpty() ? null : mDeferredBounds,
512 mDeferredTaskBounds.isEmpty() ? null : mDeferredTaskBounds,
513 mDeferredTaskInsetBounds.isEmpty() ? null : mDeferredTaskInsetBounds);
514 }
515 }
516
517 boolean updateBoundsAllowed(Rect bounds, Rect tempTaskBounds,
518 Rect tempTaskInsetBounds) {
519 if (!mUpdateBoundsDeferred) {
520 return true;
521 }
522 if (bounds != null) {
523 mDeferredBounds.set(bounds);
524 } else {
525 mDeferredBounds.setEmpty();
526 }
527 if (tempTaskBounds != null) {
528 mDeferredTaskBounds.set(tempTaskBounds);
529 } else {
530 mDeferredTaskBounds.setEmpty();
531 }
532 if (tempTaskInsetBounds != null) {
533 mDeferredTaskInsetBounds.set(tempTaskInsetBounds);
534 } else {
535 mDeferredTaskInsetBounds.setEmpty();
536 }
537 mUpdateBoundsDeferredCalled = true;
538 return false;
539 }
540
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700541 void setBounds(Rect bounds) {
542 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700543 if (mTaskPositioner != null) {
544 mTaskPositioner.configure(bounds);
545 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700546 }
Craig Mautner5962b122012-10-05 14:45:52 -0700547
Amith Yamasani734983f2014-03-04 16:48:05 -0800548 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700549 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700550 }
551
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700552 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800553 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700554 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700555 if (r != null) {
556 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800557 }
558 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700559 return null;
560 }
561
562 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800563 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
564 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800565 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800566 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
567 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800568 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800569 return r;
570 }
571 }
572 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700573 return null;
574 }
575
576 /**
577 * This is a simplified version of topRunningActivityLocked that provides a number of
578 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800579 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700580 * @param token If non-null, any history records matching this token will be skipped.
581 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800582 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700583 * @return Returns the HistoryRecord of the next activity on the stack.
584 */
585 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800586 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
587 TaskRecord task = mTaskHistory.get(taskNdx);
588 if (task.taskId == taskId) {
589 continue;
590 }
591 ArrayList<ActivityRecord> activities = task.mActivities;
592 for (int i = activities.size() - 1; i >= 0; --i) {
593 final ActivityRecord r = activities.get(i);
594 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800595 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800596 return r;
597 }
598 }
599 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700600 return null;
601 }
602
Craig Mautner8849a5e2013-04-02 16:41:03 -0700603 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700604 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
605 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700606 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
607 final ActivityRecord r = activities.get(activityNdx);
608 if (!r.finishing) {
609 return r;
610 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700611 }
612 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700613 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700614 }
615
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700616 final TaskRecord topTask() {
617 final int size = mTaskHistory.size();
618 if (size > 0) {
619 return mTaskHistory.get(size - 1);
620 }
621 return null;
622 }
623
Craig Mautnerd2328952013-03-05 12:46:26 -0800624 TaskRecord taskForIdLocked(int id) {
625 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
626 final TaskRecord task = mTaskHistory.get(taskNdx);
627 if (task.taskId == id) {
628 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800629 }
630 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700631 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700632 }
633
Craig Mautnerd2328952013-03-05 12:46:26 -0800634 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700635 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800636 return isInStackLocked(r);
637 }
638
639 ActivityRecord isInStackLocked(ActivityRecord r) {
640 if (r == null) {
641 return null;
642 }
643 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700644 if (task != null && task.stack != null
645 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800646 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800647 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800648 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800649 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800650 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800651 }
652
Craig Mautner2420ead2013-04-01 17:13:20 -0700653 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700654 final boolean hadit = mLRUActivities.remove(r);
655 mLRUActivities.add(r);
656 return hadit;
657 }
658
Craig Mautnerde4ef022013-04-07 19:01:33 -0700659 final boolean isHomeStack() {
660 return mStackId == HOME_STACK_ID;
661 }
662
Winson Chung0583d3d2015-12-18 10:03:12 -0500663 final boolean isDockedStack() {
664 return mStackId == DOCKED_STACK_ID;
665 }
666
667 final boolean isPinnedStack() {
668 return mStackId == PINNED_STACK_ID;
669 }
670
Craig Mautnere0a38842013-12-16 16:14:02 -0800671 final boolean isOnHomeDisplay() {
672 return isAttached() &&
673 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
674 }
675
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700676 void moveToFront(String reason) {
677 moveToFront(reason, null);
678 }
679
680 /**
681 * @param reason The reason for moving the stack to the front.
682 * @param task If non-null, the task will be moved to the top of the stack.
683 * */
684 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700685 if (!isAttached()) {
686 return;
687 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700688
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700689 mStacks.remove(this);
690 int addIndex = mStacks.size();
691
692 if (addIndex > 0) {
693 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800694 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
695 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700696 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800697 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700698 }
699
700 mStacks.add(addIndex, this);
701
702 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
703 if (isOnHomeDisplay()) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800704 mStackSupervisor.setFocusStackUnchecked(reason, this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700705 }
706 if (task != null) {
707 insertTaskAtTop(task, null);
708 } else {
709 task = topTask();
710 }
711 if (task != null) {
712 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800713 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800714 }
715
Wale Ogunwaled046a012015-12-24 13:05:59 -0800716 boolean isFocusable() {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800717 if (StackId.canReceiveKeys(mStackId)) {
718 return true;
719 }
720 // The stack isn't focusable. See if its top activity is focusable to force focus on the
721 // stack.
722 final ActivityRecord r = topRunningActivityLocked();
723 return r != null && r.isFocusable();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800724 }
725
Craig Mautnere0a38842013-12-16 16:14:02 -0800726 final boolean isAttached() {
727 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800728 }
729
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700730 /**
Wale Ogunwale39381972015-12-17 17:15:29 -0800731 * Returns the top activity in any existing task matching the given Intent in the input result.
732 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700733 */
Wale Ogunwale39381972015-12-17 17:15:29 -0800734 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700735 Intent intent = target.intent;
736 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700737 ComponentName cls = intent.getComponent();
738 if (info.targetActivity != null) {
739 cls = new ComponentName(info.packageName, info.targetActivity);
740 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700741 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700742 boolean isDocument = intent != null & intent.isDocument();
743 // If documentData is non-null then it must match the existing task data.
744 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800745
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700746 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800747 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
748 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700749 if (task.voiceSession != null) {
750 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700751 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700752 continue;
753 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700754 if (task.userId != userId) {
755 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700756 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700757 continue;
758 }
Craig Mautner000f0022013-02-26 15:04:29 -0800759 final ActivityRecord r = task.getTopActivity();
760 if (r == null || r.finishing || r.userId != userId ||
761 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700762 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800763 continue;
764 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700765 if (r.mActivityType != target.mActivityType) {
766 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
767 continue;
768 }
Craig Mautner000f0022013-02-26 15:04:29 -0800769
Craig Mautnerd00f4742014-03-12 14:17:26 -0700770 final Intent taskIntent = task.intent;
771 final Intent affinityIntent = task.affinityIntent;
772 final boolean taskIsDocument;
773 final Uri taskDocumentData;
774 if (taskIntent != null && taskIntent.isDocument()) {
775 taskIsDocument = true;
776 taskDocumentData = taskIntent.getData();
777 } else if (affinityIntent != null && affinityIntent.isDocument()) {
778 taskIsDocument = true;
779 taskDocumentData = affinityIntent.getData();
780 } else {
781 taskIsDocument = false;
782 taskDocumentData = null;
783 }
784
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700785 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700786 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700787 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700788 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Wale Ogunwale39381972015-12-17 17:15:29 -0800789 if (!isDocument && !taskIsDocument
790 && result.r == null && task.canMatchRootAffinity()) {
Dianne Hackborn79228822014-09-16 11:11:23 -0700791 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwale39381972015-12-17 17:15:29 -0800792 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
793 // It is possible for multiple tasks to have the same root affinity especially
794 // if they are in separate stacks. We save off this candidate, but keep looking
795 // to see if there is a better candidate.
796 result.r = r;
797 result.matchedByRootAffinity = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700798 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700799 } else if (taskIntent != null && taskIntent.getComponent() != null &&
800 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700801 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700802 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800803 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700804 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
805 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800806 result.r = r;
807 result.matchedByRootAffinity = false;
808 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700809 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
810 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700811 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700812 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800813 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700814 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
815 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800816 result.r = r;
817 result.matchedByRootAffinity = false;
818 break;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700819 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700820 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700821 }
822
823 /**
824 * Returns the first activity (starting from the top of the stack) that
825 * is the same as the given activity. Returns null if no such activity
826 * is found.
827 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700828 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700829 ComponentName cls = intent.getComponent();
830 if (info.targetActivity != null) {
831 cls = new ComponentName(info.packageName, info.targetActivity);
832 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700833 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700834
Craig Mautner000f0022013-02-26 15:04:29 -0800835 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700836 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700837 final boolean notCurrentUserTask =
838 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700839 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700840
Craig Mautner000f0022013-02-26 15:04:29 -0800841 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
842 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700843 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700844 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700845 }
Craig Mautner000f0022013-02-26 15:04:29 -0800846 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700847 return r;
848 }
849 }
850 }
851
852 return null;
853 }
854
Amith Yamasani742a6712011-05-04 14:49:28 -0700855 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700856 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700857 */
Craig Mautner93529a42013-10-04 15:03:13 -0700858 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800859 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700860 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800861 }
862 mCurrentUser = userId;
863
864 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800865 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800866 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700867 final TaskRecord task = mTaskHistory.get(i);
868
869 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
870 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700871 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700872 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700873 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700874 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800875 mTaskHistory.remove(i);
876 mTaskHistory.add(task);
877 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800878 // Use same value for i.
879 } else {
880 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800881 }
882 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700883 if (VALIDATE_TOKENS) {
884 validateAppTokensLocked();
885 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700886 }
887
Craig Mautner2420ead2013-04-01 17:13:20 -0700888 void minimalResumeActivityLocked(ActivityRecord r) {
889 r.state = ActivityState.RESUMED;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800890 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + r + " (starting new instance)"
891 + " callers=" + Debug.getCallers(5));
Craig Mautner2420ead2013-04-01 17:13:20 -0700892 mResumedActivity = r;
893 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800894 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700895 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700896 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700897 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700898 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
899 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700900 }
901
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -0800902 void addRecentActivityLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700903 if (r != null) {
904 mRecentTasks.addLocked(r.task);
905 r.task.touchActiveTime();
906 }
907 }
908
Narayan Kamath7829c812015-06-08 17:39:43 +0100909 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700910 if (mFullyDrawnStartTime != 0) {
911 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
912 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100913 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700914 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
915 }
916
917 private void stopFullyDrawnTraceIfNeeded() {
918 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
919 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
920 mFullyDrawnStartTime = 0;
921 }
922 }
923
Craig Mautnere79d42682013-04-01 19:01:53 -0700924 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700925 if (r.displayStartTime == 0) {
926 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
Chong Zhangafb776d2016-04-23 14:33:55 -0700927 if (r.task != null) {
928 r.task.isLaunching = true;
929 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700930 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100931 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700932 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700933 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700934 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100935 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700936 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700937 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700938 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800939
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700940 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700941 // Make sure that there is no activity waiting for this to launch.
942 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
943 r.displayStartTime = r.fullyDrawnStartTime = 0;
Chong Zhangafb776d2016-04-23 14:33:55 -0700944 if (r.task != null) {
945 r.task.isLaunching = false;
946 }
Craig Mautner5c494542013-09-06 11:59:38 -0700947 } else {
948 mStackSupervisor.removeTimeoutsForActivityLocked(r);
949 mStackSupervisor.scheduleIdleTimeoutLocked(r);
950 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700951 }
952
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800953 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800954 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800955 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
956 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
957 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
958 activities.get(activityNdx).setSleeping(false);
959 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800960 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800961 if (mPausingActivity != null) {
962 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
963 activityPausedLocked(mPausingActivity.appToken, true);
964 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800965 }
966
Todd Kennedy39bfee52016-02-24 10:28:21 -0800967 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
968 final String packageName = aInfo.packageName;
969 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
970 final List<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
971 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
972 if (packageName.equals(activities.get(activityNdx).packageName)) {
973 activities.get(activityNdx).info.applicationInfo = aInfo;
974 }
975 }
976 }
977 }
978
Craig Mautner0eea92c2013-05-16 13:35:39 -0700979 /**
980 * @return true if something must be done before going to sleep.
981 */
982 boolean checkReadyForSleepLocked() {
983 if (mResumedActivity != null) {
984 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700985 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
986 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
987 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700988 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700989 return true;
990 }
991 if (mPausingActivity != null) {
992 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700993 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700994 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800995 }
996
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700997 if (hasVisibleBehindActivity()) {
998 // Stop visible behind activity before going to sleep.
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800999 final ActivityRecord r = getVisibleBehindActivity();
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001000 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001001 if (DEBUG_STATES) Slog.v(TAG_STATES, "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001002 return true;
1003 }
1004
Craig Mautner0eea92c2013-05-16 13:35:39 -07001005 return false;
1006 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001007
Craig Mautner0eea92c2013-05-16 13:35:39 -07001008 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001009 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001010
Andrii Kulianf9949d52016-05-06 12:53:25 -07001011 // Make sure any paused or stopped but visible activities are now sleeping.
Craig Mautner0eea92c2013-05-16 13:35:39 -07001012 // This ensures that the activity's onStop() is called.
1013 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1014 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1015 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1016 final ActivityRecord r = activities.get(activityNdx);
Andrii Kulianf9949d52016-05-06 12:53:25 -07001017 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED
1018 || r.state == ActivityState.PAUSED || r.state == ActivityState.PAUSING) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001019 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001020 }
1021 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001022 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001023 }
Craig Mautner59c00972012-07-30 12:10:24 -07001024
Winson8b1871d2015-11-20 09:56:20 -08001025 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
1026 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -08001027 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001028 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -08001029 return null;
1030 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001031
Winson Chung083baf92014-07-11 10:32:42 -07001032 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -07001033 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -08001034 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001035 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -07001036 return null;
1037 }
1038
Winson Chung48a10a52014-08-27 14:36:51 -07001039 int w = mService.mThumbnailWidth;
1040 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -08001041 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001042 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001043 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -08001044
1045 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -08001046 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
1047 // use within SystemUI while keeping memory usage low.
Winson Chungead5c0f2015-12-14 11:18:57 -05001048 if (ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS) {
Winson8b1871d2015-11-20 09:56:20 -08001049 w = h = -1;
Winson21700932016-03-24 17:26:23 -07001050 scale = mService.mFullscreenThumbnailScale;
Winson8b1871d2015-11-20 09:56:20 -08001051 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -08001052 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -08001053 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001054 }
Winson Chung376543b2014-05-21 17:43:28 -07001055 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001056 return null;
1057 }
1058
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001059 /**
1060 * Start pausing the currently resumed activity. It is an error to call this if there
1061 * is already an activity being paused or there is no resumed activity.
1062 *
1063 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
1064 * @param uiSleeping True if this is happening with the user interface going to sleep (the
1065 * screen turning off).
1066 * @param resuming True if this is being called as part of resuming the top activity, so
1067 * we shouldn't try to instigate a resume here.
1068 * @param dontWait True if the caller does not want to wait for the pause to complete. If
1069 * set to true, we will immediately complete the pause here before returning.
1070 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
1071 * it to tell us when it is done.
1072 */
1073 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
1074 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001075 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001076 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
1077 + " state=" + mPausingActivity.state);
1078 if (!mService.isSleeping()) {
1079 // Avoid recursion among check for sleep and complete pause during sleeping.
1080 // Because activity will be paused immediately after resume, just let pause
1081 // be completed by the order of activity paused from clients.
1082 completePauseLocked(false);
1083 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001084 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001085 ActivityRecord prev = mResumedActivity;
1086 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001087 if (!resuming) {
1088 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001089 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001090 }
1091 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001092 }
Craig Mautnerdf88d732014-01-27 09:21:32 -08001093
1094 if (mActivityContainer.mParentActivity == null) {
1095 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001096 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001097 }
1098
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001099 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001100 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001101 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001102 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001103 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -07001104 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1105 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001106 prev.state = ActivityState.PAUSING;
1107 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001108 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -07001109 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -07001110 if (mService.mHasRecents
1111 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001112 prev.mUpdateTaskThumbnailWhenHidden = true;
Craig Mautner6f6d56f2013-10-24 16:02:07 -07001113 }
Dianne Hackborncee04b52013-07-03 17:01:28 -07001114 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001115
1116 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -08001117
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001118 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001119 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001120 try {
1121 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001122 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001123 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -07001124 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001125 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001126 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001127 } catch (Exception e) {
1128 // Ignore exception, if process died other code will cleanup.
1129 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001130 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001131 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001132 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001133 }
1134 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001135 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001136 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001137 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001138 }
1139
1140 // If we are not going to sleep, we want to ensure the device is
1141 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001142 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001143 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001144 }
1145
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001146 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001147 // Have the window manager pause its key dispatching until the new
1148 // activity has started. If we're pausing the activity just because
1149 // the screen is being turned off and the UI is sleeping, don't interrupt
1150 // key dispatch; the same activity will pick it up again on wakeup.
1151 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001152 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001153 } else if (DEBUG_PAUSE) {
1154 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001155 }
1156
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001157 if (dontWait) {
1158 // If the caller said they don't want to wait for the pause, then complete
1159 // the pause now.
1160 completePauseLocked(false);
1161 return false;
1162
1163 } else {
1164 // Schedule a pause timeout in case the app doesn't respond.
1165 // We don't give it much time because this directly impacts the
1166 // responsiveness seen by the user.
1167 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1168 msg.obj = prev;
1169 prev.pauseTime = SystemClock.uptimeMillis();
1170 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001171 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001172 return true;
1173 }
1174
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001175 } else {
1176 // This activity failed to schedule the
1177 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001178 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001179 if (!resuming) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001180 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001181 }
1182 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001183 }
1184 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001185
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001186 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001187 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1188 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001189
Craig Mautnerd2328952013-03-05 12:46:26 -08001190 final ActivityRecord r = isInStackLocked(token);
1191 if (r != null) {
1192 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1193 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001194 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001195 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001196 completePauseLocked(true);
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001197 return;
Craig Mautnerd2328952013-03-05 12:46:26 -08001198 } else {
1199 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1200 r.userId, System.identityHashCode(r), r.shortComponentName,
1201 mPausingActivity != null
1202 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001203 if (r.state == ActivityState.PAUSING) {
1204 r.state = ActivityState.PAUSED;
1205 if (r.finishing) {
1206 if (DEBUG_PAUSE) Slog.v(TAG,
1207 "Executing finish of failed to pause activity: " + r);
1208 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1209 }
louis_chang047dfd42015-04-08 16:35:55 +08001210 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001211 }
1212 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001213 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001214 }
1215
Chong Zhangd78ddb42016-03-02 17:01:14 -08001216 final void activityResumedLocked(IBinder token) {
1217 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
1218 if (DEBUG_SAVED_STATE) Slog.i(TAG_STATES, "Resumed activity; dropping state of: " + r);
1219 r.icicle = null;
1220 r.haveState = false;
1221 }
1222
Craig Mautnera0026042014-04-23 11:45:37 -07001223 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1224 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001225 if (r.state != ActivityState.STOPPING) {
1226 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1227 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1228 return;
1229 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001230 if (persistentState != null) {
1231 r.persistentState = persistentState;
1232 mService.notifyTaskPersisterLocked(r.task, false);
1233 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001234 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001235 if (icicle != null) {
1236 // If icicle is null, this is happening due to a timeout, so we
1237 // haven't really saved the state.
1238 r.icicle = icicle;
1239 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001240 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001241 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001242 }
1243 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001244 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001245 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1246 r.stopped = true;
1247 r.state = ActivityState.STOPPED;
Robert Carre12aece2016-02-02 22:43:27 -08001248
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08001249 mWindowManager.notifyAppStopped(r.appToken, true);
Robert Carre12aece2016-02-02 22:43:27 -08001250
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001251 if (getVisibleBehindActivity() == r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001252 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001253 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001254 if (r.finishing) {
1255 r.clearOptionsLocked();
1256 } else {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001257 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001258 destroyActivityLocked(r, true, "stop-config");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001259 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001260 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001261 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001262 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001263 }
1264 }
1265 }
1266
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001267 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001268 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001269 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001270
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001271 if (prev != null) {
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001272 final boolean wasStopping = prev.state == ActivityState.STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001273 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001274 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001275 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001276 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001277 } else if (prev.app != null) {
Wale Ogunwaled8026642016-02-09 20:40:18 -08001278 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueue pending stop if needed: " + prev
1279 + " wasStopping=" + wasStopping + " visible=" + prev.visible);
Craig Mautner8c14c152015-01-15 17:32:07 -08001280 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001281 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1282 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001283 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001284 if (prev.deferRelaunchUntilPaused) {
1285 // Complete the deferred relaunch that was waiting for pause to complete.
1286 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
1287 relaunchActivityLocked(prev, prev.configChangeFlags, false,
1288 prev.preserveWindowOnDeferredRelaunch);
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001289 } else if (wasStopping) {
1290 // We are also stopping, the stop request must have gone soon after the pause.
1291 // We can't clobber it, because the stop confirmation will not be handled.
1292 // We don't need to schedule another stop, we only need to let it happen.
1293 prev.state = ActivityState.STOPPING;
Wale Ogunwaled8026642016-02-09 20:40:18 -08001294 } else if ((!prev.visible && !hasVisibleBehindActivity())
1295 || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001296 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001297 // stopping.
Chong Zhang46b1ac62016-02-18 17:53:57 -08001298 addToStopping(prev, true /* immediate */);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001299 }
1300 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001301 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001302 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001303 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001304 // It is possible the activity was freezing the screen before it was paused.
1305 // In that case go ahead and remove the freeze this activity has on the screen
1306 // since it is no longer visible.
1307 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001308 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001309 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001310
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001311 if (resumeNext) {
1312 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1313 if (!mService.isSleepingOrShuttingDown()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001314 mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001315 } else {
1316 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001317 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001318 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001319 // If there are no more activities available to run, do resume anyway to start
1320 // something. Also if the top activity on the stack is not the just paused
1321 // activity, we need to go ahead and resume it to ensure we complete an
1322 // in-flight app switch.
1323 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001324 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001325 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001326 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001327
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001328 if (prev != null) {
1329 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001330
Craig Mautner525f3d92013-05-07 14:01:50 -07001331 if (prev.app != null && prev.cpuTimeAtResume > 0
1332 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001333 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1334 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001335 if (diff > 0) {
1336 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1337 synchronized (bsi) {
1338 BatteryStatsImpl.Uid.Proc ps =
1339 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001340 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001341 if (ps != null) {
1342 ps.addForegroundTimeLocked(diff);
1343 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001344 }
1345 }
1346 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001347 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001348 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001349
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001350 // Notify when the task stack has changed, but only if visibilities changed (not just focus)
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001351 if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause) {
1352 mService.notifyTaskStackChangedLocked();
1353 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
1354 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001355
1356 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001357 }
1358
Chong Zhang46b1ac62016-02-18 17:53:57 -08001359 private void addToStopping(ActivityRecord r, boolean immediate) {
1360 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1361 mStackSupervisor.mStoppingActivities.add(r);
1362 }
1363
1364 // If we already have a few activities waiting to stop, then give up
1365 // on things going idle and start clearing them out. Or if r is the
1366 // last of activity of the last task the stack will be empty and must
1367 // be cleared immediately.
1368 boolean forceIdle = mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE
1369 || (r.frontOfTask && mTaskHistory.size() <= 1);
1370
1371 if (immediate || forceIdle) {
1372 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Scheduling idle now: forceIdle="
1373 + forceIdle + "immediate=" + immediate);
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001374 mStackSupervisor.scheduleIdleLocked();
1375 } else {
1376 mStackSupervisor.checkReadyForSleepLocked();
1377 }
1378 }
1379
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001380 /**
1381 * Once we know that we have asked an application to put an activity in
1382 * the resumed state (either by launching it or explicitly telling it),
1383 * this function updates the rest of our state to match that fact.
1384 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001385 private void completeResumeLocked(ActivityRecord next) {
Chong Zhang2b79af12016-02-10 18:47:06 -08001386 next.visible = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001387 next.idle = false;
1388 next.results = null;
1389 next.newIntents = null;
Chong Zhang2b79af12016-02-10 18:47:06 -08001390 next.stopped = false;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001391
Chong Zhang85ee6542015-10-02 13:36:38 -07001392 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001393 ProcessRecord app = next.task.mActivities.get(0).app;
1394 if (app != null && app != mService.mHomeProcess) {
1395 mService.mHomeProcess = app;
1396 }
1397 }
1398
Craig Mautner07566322013-09-26 16:42:55 -07001399 if (next.nowVisible) {
1400 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001401 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001402 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001403
1404 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001405 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001406
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001407 mStackSupervisor.reportResumedActivityLocked(next);
1408
1409 next.resumeKeyDispatchingLocked();
1410 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001411
1412 // Mark the point when the activity is resuming
1413 // TODO: To be more accurate, the mark should be before the onCreate,
1414 // not after the onResume. But for subsequent starts, onResume is fine.
1415 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001416 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001417 } else {
1418 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1419 }
Winson Chung376543b2014-05-21 17:43:28 -07001420
George Mount6ba042b2014-07-28 11:12:28 -07001421 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001422
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001423 if (getVisibleBehindActivity() == next) {
Craig Mautner64ccb702014-10-01 09:38:40 -07001424 // When resuming an activity, require it to call requestVisibleBehind() again.
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001425 setVisibleBehindActivity(null);
Craig Mautner64ccb702014-10-01 09:38:40 -07001426 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001427 }
1428
Craig Mautner2568c3a2015-03-26 14:22:34 -07001429 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001430 r.visible = visible;
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001431 if (!visible && r.mUpdateTaskThumbnailWhenHidden) {
1432 r.updateThumbnailLocked(r.task.stack.screenshotActivitiesLocked(r), null);
1433 r.mUpdateTaskThumbnailWhenHidden = false;
1434 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001435 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001436 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001437 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001438 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001439 container.setVisible(visible);
1440 }
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001441 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001442 }
1443
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001444 // Find the first visible activity above the passed activity and if it is translucent return it
1445 // otherwise return null;
1446 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1447 TaskRecord task = r.task;
1448 if (task == null) {
1449 return null;
1450 }
1451
1452 ActivityStack stack = task.stack;
1453 if (stack == null) {
1454 return null;
1455 }
1456
1457 int stackNdx = mStacks.indexOf(stack);
1458
1459 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1460 int taskNdx = tasks.indexOf(task);
1461
1462 ArrayList<ActivityRecord> activities = task.mActivities;
1463 int activityNdx = activities.indexOf(r) + 1;
1464
1465 final int numStacks = mStacks.size();
1466 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001467 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001468 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001469 final int numTasks = tasks.size();
1470 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001471 final TaskRecord currentTask = tasks.get(taskNdx);
1472 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001473 final int numActivities = activities.size();
1474 while (activityNdx < numActivities) {
1475 final ActivityRecord activity = activities.get(activityNdx);
1476 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001477 return historyStack.mFullscreen
1478 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001479 }
1480 ++activityNdx;
1481 }
1482 activityNdx = 0;
1483 ++taskNdx;
1484 }
1485 taskNdx = 0;
1486 ++stackNdx;
1487 }
1488
1489 return null;
1490 }
1491
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001492 ActivityStack getNextFocusableStackLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001493 ArrayList<ActivityStack> stacks = mStacks;
1494 final ActivityRecord parent = mActivityContainer.mParentActivity;
1495 if (parent != null) {
1496 stacks = parent.task.stack.mStacks;
1497 }
1498 if (stacks != null) {
1499 for (int i = stacks.size() - 1; i >= 0; --i) {
1500 ActivityStack stack = stacks.get(i);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001501 if (stack != this && stack.isFocusable()
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001502 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001503 return stack;
1504 }
1505 }
1506 }
1507 return null;
1508 }
1509
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001510 /** Returns true if the stack contains a fullscreen task. */
1511 private boolean hasFullscreenTask() {
1512 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1513 final TaskRecord task = mTaskHistory.get(i);
1514 if (task.mFullscreen) {
1515 return true;
1516 }
1517 }
1518 return false;
1519 }
1520
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001521 /**
1522 * Returns true if the stack is translucent and can have other contents visible behind it if
1523 * needed. A stack is considered translucent if it don't contain a visible or
1524 * starting (about to be visible) activity that is fullscreen (opaque).
1525 * @param starting The currently starting activity or null if there is none.
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001526 * @param stackBehindId The id of the stack directly behind this one.
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001527 */
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001528 private boolean isStackTranslucent(ActivityRecord starting, int stackBehindId) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001529 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1530 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001531 final ArrayList<ActivityRecord> activities = task.mActivities;
1532 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1533 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001534
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001535 if (r.finishing) {
1536 // We don't factor in finishing activities when determining translucency since
1537 // they will be gone soon.
1538 continue;
1539 }
1540
1541 if (!r.visible && r != starting) {
1542 // Also ignore invisible activities that are not the currently starting
1543 // activity (about to be visible).
1544 continue;
1545 }
1546
1547 if (r.fullscreen) {
1548 // Stack isn't translucent if it has at least one fullscreen activity
1549 // that is visible.
1550 return false;
1551 }
1552
1553 if (!isHomeStack() && r.frontOfTask
1554 && task.isOverHomeStack() && stackBehindId != HOME_STACK_ID) {
1555 // Stack isn't translucent if it's top activity should have the home stack
1556 // behind it and the stack currently behind it isn't the home stack.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001557 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001558 }
1559 }
1560 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001561 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001562 }
1563
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001564 /**
1565 * Returns stack's visibility: {@link #STACK_INVISIBLE}, {@link #STACK_VISIBLE} or
1566 * {@link #STACK_VISIBLE_ACTIVITY_BEHIND}.
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001567 * @param starting The currently starting activity or null if there is none.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001568 */
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001569 int getStackVisibilityLocked(ActivityRecord starting) {
Jose Lima7ba71252014-04-30 12:59:27 -07001570 if (!isAttached()) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001571 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001572 }
1573
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001574 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001575 return STACK_VISIBLE;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001576 }
1577
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001578 final int stackIndex = mStacks.indexOf(this);
1579
1580 if (stackIndex == mStacks.size() - 1) {
1581 Slog.wtf(TAG,
1582 "Stack=" + this + " isn't front stack but is at the top of the stack list");
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001583 return STACK_INVISIBLE;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001584 }
1585
Chong Zhang75b37202015-12-04 14:16:36 -08001586 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1587 if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001588 return STACK_INVISIBLE;
Chong Zhang75b37202015-12-04 14:16:36 -08001589 }
1590
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001591 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1592 final int focusedStackId = focusedStack.mStackId;
1593
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001594 if (mStackId == FULLSCREEN_WORKSPACE_STACK_ID
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001595 && hasVisibleBehindActivity() && focusedStackId == HOME_STACK_ID
1596 && !focusedStack.topActivity().fullscreen) {
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001597 // The fullscreen stack should be visible if it has a visible behind activity behind
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001598 // the home stack that is translucent.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001599 return STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001600 }
1601
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001602 if (mStackId == DOCKED_STACK_ID) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001603 // Docked stack is always visible, except in the case where the top running activity
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001604 // task in the focus stack doesn't support any form of resizing but we show it for the
1605 // home task even though it's not resizable.
Wale Ogunwaled26176f2016-01-25 20:04:04 -08001606 final ActivityRecord r = focusedStack.topRunningActivityLocked();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001607 final TaskRecord task = r != null ? r.task : null;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001608 return task == null || task.canGoInDockedStack() || task.isHomeTask() ? STACK_VISIBLE
1609 : STACK_INVISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001610 }
1611
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001612 // Find the first stack behind focused stack that actually got something visible.
1613 int stackBehindFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1614 while (stackBehindFocusedIndex >= 0 &&
1615 mStacks.get(stackBehindFocusedIndex).topRunningActivityLocked() == null) {
1616 stackBehindFocusedIndex--;
Chong Zhangb16cf342015-11-12 17:22:40 -08001617 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001618 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001619 && stackIndex == stackBehindFocusedIndex) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001620 // Stacks directly behind the docked or pinned stack are always visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001621 return STACK_VISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001622 }
1623
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001624 final int stackBehindFocusedId = (stackBehindFocusedIndex >= 0)
1625 ? mStacks.get(stackBehindFocusedIndex).mStackId : INVALID_STACK_ID;
1626
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001627 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001628 && focusedStack.isStackTranslucent(starting, stackBehindFocusedId)) {
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001629 // Stacks behind the fullscreen stack with a translucent activity are always
1630 // visible so they can act as a backdrop to the translucent activity.
1631 // For example, dialog activities
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001632 if (stackIndex == stackBehindFocusedIndex) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001633 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001634 }
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001635 if (stackBehindFocusedIndex >= 0) {
1636 if ((stackBehindFocusedId == DOCKED_STACK_ID
1637 || stackBehindFocusedId == PINNED_STACK_ID)
1638 && stackIndex == (stackBehindFocusedIndex - 1)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001639 // The stack behind the docked or pinned stack is also visible so we can have a
1640 // complete backdrop to the translucent activity when the docked stack is up.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001641 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001642 }
1643 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001644 }
1645
Wale Ogunwale3797c222015-10-27 14:21:58 -07001646 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001647 // Visibility of any static stack should have been determined by the conditions above.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001648 return STACK_INVISIBLE;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001649 }
1650
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001651 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001652 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001653
1654 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1655 continue;
1656 }
1657
Wale Ogunwale3797c222015-10-27 14:21:58 -07001658 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001659 // These stacks can't have any dynamic stacks visible behind them.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001660 return STACK_INVISIBLE;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001661 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001662
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001663 if (!stack.isStackTranslucent(starting, INVALID_STACK_ID)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001664 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001665 }
1666 }
1667
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001668 return STACK_VISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001669 }
1670
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001671 final int rankTaskLayers(int baseLayer) {
1672 int layer = 0;
1673 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1674 final TaskRecord task = mTaskHistory.get(taskNdx);
1675 ActivityRecord r = task.topRunningActivityLocked();
1676 if (r == null || r.finishing || !r.visible) {
1677 task.mLayerRank = -1;
1678 } else {
1679 task.mLayerRank = baseLayer + layer++;
1680 }
1681 }
1682 return layer;
1683 }
1684
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001685 /**
1686 * Make sure that all activities that need to be visible (that is, they
1687 * currently can be seen by the user) actually are.
1688 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001689 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1690 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001691 ActivityRecord top = topRunningActivityLocked();
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001692 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001693 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001694 if (top != null) {
1695 checkTranslucentActivityWaiting(top);
1696 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001697
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001698 // If the top activity is not fullscreen, then we need to
1699 // make sure any activities under it are now visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001700 boolean aboveTop = top != null;
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001701 final int stackVisibility = getStackVisibilityLocked(starting);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001702 final boolean stackInvisible = stackVisibility != STACK_VISIBLE;
1703 final boolean stackVisibleBehind = stackVisibility == STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001704 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaled046a012015-12-24 13:05:59 -08001705 boolean resumeNextActivity = isFocusable() && (isInStackLocked(starting) == null);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001706 boolean behindTranslucentActivity = false;
1707 final ActivityRecord visibleBehind = getVisibleBehindActivity();
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001708 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001709 final TaskRecord task = mTaskHistory.get(taskNdx);
1710 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001711 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1712 final ActivityRecord r = activities.get(activityNdx);
1713 if (r.finishing) {
Chong Zhang22bc8512016-04-07 13:47:18 -07001714 // Normally the screenshot will be taken in makeInvisible(). When an activity
1715 // is finishing, we no longer change its visibility, but we still need to take
1716 // the screenshots if startPausingLocked decided it should be taken.
1717 if (r.mUpdateTaskThumbnailWhenHidden) {
1718 r.updateThumbnailLocked(screenshotActivitiesLocked(r), null);
1719 r.mUpdateTaskThumbnailWhenHidden = false;
1720 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001721 continue;
1722 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001723 final boolean isTop = r == top;
1724 if (aboveTop && !isTop) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001725 continue;
1726 }
1727 aboveTop = false;
Wale Ogunwalee76407c2016-05-16 17:04:37 -07001728
1729 if (shouldBeVisible(r, behindTranslucentActivity, stackVisibleBehind,
1730 visibleBehind, behindFullscreenActivity)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001731 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
1732 + " finishing=" + r.finishing + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001733 // First: if this is not the current activity being started, make
1734 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001735 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001736 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001737 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001738
1739 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001740 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001741 resumeNextActivity, r)) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001742 if (activityNdx >= activities.size()) {
1743 // Record may be removed if its process needs to restart.
1744 activityNdx = activities.size() - 1;
1745 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001746 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001747 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001748 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001749 } else if (r.visible) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001750 // If this activity is already visible, then there is nothing to do here.
Chong Zhang371c4422016-05-11 10:48:32 -07001751 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1752 "Skipping: already visible at " + r);
1753
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001754 if (handleAlreadyVisible(r)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001755 resumeNextActivity = false;
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001756 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001757 } else {
Jorim Jaggie66edb12016-02-05 12:41:17 -08001758 makeVisibleIfNeeded(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001759 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001760 // Aggregate current change flags.
1761 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001762 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1763 behindFullscreenActivity, task, r);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001764 if (behindFullscreenActivity && !r.fullscreen) {
1765 behindTranslucentActivity = true;
1766 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001767 } else {
Wale Ogunwale834c2362016-01-23 18:14:58 -08001768 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001769 + " finishing=" + r.finishing + " state=" + r.state + " stackInvisible="
1770 + stackInvisible + " behindFullscreenActivity="
1771 + behindFullscreenActivity + " mLaunchTaskBehind="
1772 + r.mLaunchTaskBehind);
1773 makeInvisible(r, visibleBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001774 }
1775 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001776 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1777 // The visibility of tasks and the activities they contain in freeform stack are
1778 // determined individually unlike other stacks where the visibility or fullscreen
1779 // status of an activity in a previous task affects other.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001780 behindFullscreenActivity = stackVisibility == STACK_INVISIBLE;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001781 } else if (mStackId == HOME_STACK_ID) {
1782 if (task.isHomeTask()) {
1783 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home task: at " + task
1784 + " stackInvisible=" + stackInvisible
1785 + " behindFullscreenActivity=" + behindFullscreenActivity);
1786 // No other task in the home stack should be visible behind the home activity.
1787 // Home activities is usually a translucent activity with the wallpaper behind
1788 // them. However, when they don't have the wallpaper behind them, we want to
1789 // show activities in the next application stack behind them vs. another
1790 // task in the home stack like recents.
1791 behindFullscreenActivity = true;
1792 } else if (task.isRecentsTask()
1793 && task.getTaskToReturnTo() == APPLICATION_ACTIVITY_TYPE) {
1794 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1795 "Recents task returning to app: at " + task
1796 + " stackInvisible=" + stackInvisible
1797 + " behindFullscreenActivity=" + behindFullscreenActivity);
1798 // We don't want any other tasks in the home stack visible if the recents
1799 // activity is going to be returning to an application activity type.
1800 // We do this to preserve the visible order the user used to get into the
1801 // recents activity. The recents activity is normally translucent and if it
1802 // doesn't have the wallpaper behind it the next activity in the home stack
1803 // shouldn't be visible when the home stack is brought to the front to display
1804 // the recents activity from an app.
1805 behindFullscreenActivity = true;
1806 }
1807
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001808 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001809 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001810
1811 if (mTranslucentActivityWaiting != null &&
1812 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1813 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1814 notifyActivityDrawnLocked(null);
1815 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001816 }
Craig Mautner58547802013-03-05 08:23:53 -08001817
Wale Ogunwalee76407c2016-05-16 17:04:37 -07001818 /** Return true if the input activity should be made visible */
1819 private boolean shouldBeVisible(ActivityRecord r, boolean behindTranslucentActivity,
1820 boolean stackVisibleBehind, ActivityRecord visibleBehind,
1821 boolean behindFullscreenActivity) {
Wale Ogunwalee76407c2016-05-16 17:04:37 -07001822
1823 if (!okToShowLocked(r)) {
1824 return false;
1825 }
1826
Wale Ogunwale8c09c7d2016-05-23 09:10:24 -07001827 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1828 // but must be drawn initially for the animation as though they were visible.
1829 final boolean activityVisibleBehind =
1830 (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == r;
1831
Wale Ogunwalee76407c2016-05-16 17:04:37 -07001832 boolean isVisible =
1833 !behindFullscreenActivity || r.mLaunchTaskBehind || activityVisibleBehind;
1834
1835 if (isVisible && r.isRecentsActivity()) {
Wale Ogunwale8c09c7d2016-05-23 09:10:24 -07001836 // Recents activity can only be visible if the home stack is the focused stack or we are
1837 // in split-screen mode.
1838 isVisible = mStackSupervisor.getStack(DOCKED_STACK_ID) != null
1839 || mStackSupervisor.isFocusedStack(this);
Wale Ogunwalee76407c2016-05-16 17:04:37 -07001840 }
1841
1842 return isVisible;
1843 }
1844
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001845 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1846 if (mTranslucentActivityWaiting != top) {
1847 mUndrawnActivitiesBelowTopTranslucent.clear();
1848 if (mTranslucentActivityWaiting != null) {
1849 // Call the callback with a timeout indication.
1850 notifyActivityDrawnLocked(null);
1851 mTranslucentActivityWaiting = null;
1852 }
1853 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1854 }
1855 }
1856
1857 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001858 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001859 // We need to make sure the app is running if it's the top, or it is just made visible from
1860 // invisible. If the app is already visible, it must have died while it was visible. In this
1861 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1862 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001863 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001864 // This activity needs to be visible, but isn't even running...
1865 // get it started and resume if no other stack in this stack is resumed.
1866 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1867 if (r != starting) {
1868 r.startFreezingScreenLocked(r.app, configChanges);
1869 }
1870 if (!r.visible || r.mLaunchTaskBehind) {
1871 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1872 setVisible(r, true);
1873 }
1874 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001875 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001876 return true;
1877 }
1878 }
1879 return false;
1880 }
1881
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001882 private void makeInvisible(ActivityRecord r, ActivityRecord visibleBehind) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001883 if (!r.visible) {
1884 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1885 return;
1886 }
1887 // Now for any activities that aren't visible to the user, make sure they no longer are
1888 // keeping the screen frozen.
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001889 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.state);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001890 try {
1891 setVisible(r, false);
1892 switch (r.state) {
1893 case STOPPING:
1894 case STOPPED:
1895 if (r.app != null && r.app.thread != null) {
1896 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1897 "Scheduling invisibility: " + r);
1898 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1899 }
1900 break;
1901
1902 case INITIALIZING:
1903 case RESUMED:
1904 case PAUSING:
1905 case PAUSED:
1906 // This case created for transitioning activities from
1907 // translucent to opaque {@link Activity#convertToOpaque}.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001908 if (visibleBehind == r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001909 releaseBackgroundResources(r);
1910 } else {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001911 addToStopping(r, true /* immediate */);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001912 }
1913 break;
1914
1915 default:
1916 break;
1917 }
1918 } catch (Exception e) {
1919 // Just skip on any failure; we'll make it visible when it next restarts.
1920 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1921 }
1922 }
1923
1924 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1925 TaskRecord task, ActivityRecord r) {
1926 if (r.fullscreen) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001927 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwale673cbd22016-01-30 18:30:55 -08001928 + " stackInvisible=" + stackInvisible
1929 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001930 // At this point, nothing else needs to be shown in this task.
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001931 behindFullscreenActivity = true;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001932 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001933 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1934 + " stackInvisible=" + stackInvisible
1935 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001936 behindFullscreenActivity = true;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001937 }
1938 return behindFullscreenActivity;
1939 }
1940
Jorim Jaggie66edb12016-02-05 12:41:17 -08001941 private void makeVisibleIfNeeded(ActivityRecord starting, ActivityRecord r) {
1942
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001943 // This activity is not currently visible, but is running. Tell it to become visible.
Jorim Jaggie66edb12016-02-05 12:41:17 -08001944 if (r.state == ActivityState.RESUMED || r == starting) {
Chong Zhange05db742016-02-16 16:58:37 -08001945 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
1946 "Not making visible, r=" + r + " state=" + r.state + " starting=" + starting);
Jorim Jaggie66edb12016-02-05 12:41:17 -08001947 return;
1948 }
1949
1950 // If this activity is paused, tell it to now show its window.
1951 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1952 "Making visible and scheduling visibility: " + r);
1953 try {
1954 if (mTranslucentActivityWaiting != null) {
1955 r.updateOptionsLocked(r.returningOptions);
1956 mUndrawnActivitiesBelowTopTranslucent.add(r);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001957 }
Jorim Jaggie66edb12016-02-05 12:41:17 -08001958 setVisible(r, true);
1959 r.sleeping = false;
1960 r.app.pendingUiClean = true;
1961 r.app.thread.scheduleWindowVisibility(r.appToken, true);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001962 // The activity may be waiting for stop, but that is no longer
1963 // appropriate for it.
1964 mStackSupervisor.mStoppingActivities.remove(r);
1965 mStackSupervisor.mGoingToSleepActivities.remove(r);
Jorim Jaggie66edb12016-02-05 12:41:17 -08001966 } catch (Exception e) {
1967 // Just skip on any failure; we'll make it
1968 // visible when it next restarts.
1969 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001970 }
Chong Zhang371c4422016-05-11 10:48:32 -07001971 handleAlreadyVisible(r);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001972 }
1973
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001974 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001975 r.stopFreezingScreenLocked(false);
1976 try {
1977 if (r.returningOptions != null) {
1978 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1979 }
1980 } catch(RemoteException e) {
1981 }
1982 return r.state == ActivityState.RESUMED;
1983 }
1984
Todd Kennedyaab56db2015-01-30 09:39:53 -08001985 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001986 mTranslucentActivityWaiting = r;
1987 mUndrawnActivitiesBelowTopTranslucent.clear();
1988 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1989 }
1990
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001991 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1992 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1993 final TaskRecord task = mTaskHistory.get(taskNdx);
1994 final ArrayList<ActivityRecord> activities = task.mActivities;
1995 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1996 final ActivityRecord r = activities.get(activityNdx);
1997 if ( r.appTimeTracker != except) {
1998 r.appTimeTracker = null;
1999 }
2000 }
2001 }
2002 }
2003
Craig Mautner5eda9b32013-07-02 11:58:16 -07002004 /**
2005 * Called as activities below the top translucent activity are redrawn. When the last one is
2006 * redrawn notify the top activity by calling
2007 * {@link Activity#onTranslucentConversionComplete}.
2008 *
2009 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
2010 * occurred and the activity will be notified immediately.
2011 */
2012 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07002013 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07002014 if ((r == null)
2015 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
2016 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
2017 // The last undrawn activity below the top has just been drawn. If there is an
2018 // opaque activity at the top, notify it that it can become translucent safely now.
2019 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
2020 mTranslucentActivityWaiting = null;
2021 mUndrawnActivitiesBelowTopTranslucent.clear();
2022 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
2023
Craig Mautner71dd1b62014-02-18 15:48:52 -08002024 if (waitingActivity != null) {
2025 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
2026 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
2027 try {
2028 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
2029 waitingActivity.appToken, r != null);
2030 } catch (RemoteException e) {
2031 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002032 }
2033 }
2034 }
2035 }
2036
Craig Mautnera61bc652013-10-28 15:43:18 -07002037 /** If any activities below the top running one are in the INITIALIZING state and they have a
2038 * starting window displayed then remove that starting window. It is possible that the activity
2039 * in this state will never resumed in which case that starting window will be orphaned. */
2040 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002041 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07002042 boolean aboveTop = true;
Wale Ogunwale68741142016-05-17 09:40:02 -07002043 // We don't want to clear starting window for activities that aren't behind fullscreen
2044 // activities as we need to display their starting window until they are done initializing.
2045 boolean behindFullscreenActivity = false;
Craig Mautnera61bc652013-10-28 15:43:18 -07002046 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2047 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2048 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2049 final ActivityRecord r = activities.get(activityNdx);
2050 if (aboveTop) {
2051 if (r == topActivity) {
2052 aboveTop = false;
2053 }
Wale Ogunwale68741142016-05-17 09:40:02 -07002054 behindFullscreenActivity |= r.fullscreen;
Craig Mautnera61bc652013-10-28 15:43:18 -07002055 continue;
2056 }
2057
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002058 if (r.state == ActivityState.INITIALIZING
Wale Ogunwale68741142016-05-17 09:40:02 -07002059 && r.mStartingWindowState == STARTING_WINDOW_SHOWN
2060 && behindFullscreenActivity) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002061 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
2062 "Found orphaned starting window " + r);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002063 r.mStartingWindowState = STARTING_WINDOW_REMOVED;
Craig Mautnera61bc652013-10-28 15:43:18 -07002064 mWindowManager.removeAppStartingWindow(r.appToken);
2065 }
Wale Ogunwale68741142016-05-17 09:40:02 -07002066
2067 behindFullscreenActivity |= r.fullscreen;
Craig Mautnera61bc652013-10-28 15:43:18 -07002068 }
2069 }
2070 }
2071
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002072 /**
2073 * Ensure that the top activity in the stack is resumed.
2074 *
2075 * @param prev The previously resumed activity, for when in the process
2076 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002077 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002078 *
2079 * @return Returns true if something is being resumed, or false if
2080 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002081 *
2082 * NOTE: It is not safe to call this method directly as it can cause an activity in a
2083 * non-focused stack to be resumed.
2084 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
2085 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002086 */
Wale Ogunwaled046a012015-12-24 13:05:59 -08002087 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08002088 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07002089 // Don't even start recursing.
2090 return false;
2091 }
2092
2093 boolean result = false;
2094 try {
2095 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08002096 mStackSupervisor.inResumeTopActivity = true;
2097 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
2098 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08002099 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08002100 }
Craig Mautner544efa72014-09-04 16:41:20 -07002101 result = resumeTopActivityInnerLocked(prev, options);
2102 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08002103 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07002104 }
2105 return result;
2106 }
2107
Chong Zhang280d3322015-11-03 17:27:26 -08002108 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002109 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07002110
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002111 if (!mService.mBooting && !mService.mBooted) {
2112 // Not ready yet!
2113 return false;
2114 }
2115
Craig Mautnerdf88d732014-01-27 09:21:32 -08002116 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002117 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07002118 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08002119 // Do not resume this stack if its parent is not resumed.
2120 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
2121 return false;
2122 }
2123
Wale Ogunwale2be760d2016-02-17 11:16:10 -08002124 mStackSupervisor.cancelInitializingActivities();
Craig Mautnera61bc652013-10-28 15:43:18 -07002125
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002126 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002127 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002128
2129 // Remember how we'll process this pause/resume situation, and ensure
2130 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002131 final boolean userLeaving = mStackSupervisor.mUserLeaving;
2132 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002133
Craig Mautner84984fa2014-06-19 11:19:20 -07002134 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002135 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002136 // There are no more activities!
2137 final String reason = "noMoreActivities";
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07002138 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack()
2139 ? HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
2140 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(returnTaskType, reason)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002141 // Try to move focus to the next visible stack with a running activity if this
2142 // stack is not covering the entire screen.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002143 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
2144 mStackSupervisor.getFocusedStack(), prev, null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002145 }
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002146
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002147 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07002148 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002149 if (DEBUG_STATES) Slog.d(TAG_STATES,
2150 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07002151 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08002152 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07002153 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002154 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002155 }
2156
2157 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08002158
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002159 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002160 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
2161 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002162 // Make sure we have executed any pending transitions, since there
2163 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002164 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002165 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07002166 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002167 if (DEBUG_STATES) Slog.d(TAG_STATES,
2168 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07002169 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002170 return false;
2171 }
2172
Craig Mautner525f3d92013-05-07 14:01:50 -07002173 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07002174 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07002175 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002176 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07002177 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002178 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07002179 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07002180 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07002181 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07002182 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07002183 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08002184 } else if (!isOnHomeDisplay()) {
2185 return false;
2186 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002187 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08002188 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07002189 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
2190 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
2191 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08002192 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07002193 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002194 }
2195
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002196 // If we are sleeping, and there is no resumed activity, and the top
2197 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07002198 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09002199 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07002200 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002201 // Make sure we have executed any pending transitions, since there
2202 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002203 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002204 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07002205 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002206 if (DEBUG_STATES) Slog.d(TAG_STATES,
2207 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07002208 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002209 return false;
2210 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002211
2212 // Make sure that the user who owns this activity is started. If not,
2213 // we will just leave it as is because someone should be bringing
2214 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002215 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002216 Slog.w(TAG, "Skipping resume of top activity " + next
2217 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07002218 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002219 return false;
2220 }
2221
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002222 // The activity may be waiting for stop, but that is no longer
2223 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002224 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002225 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002226 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002227 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002228
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002229 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002230
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002231 // If we are currently pausing an activity, then don't do anything until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07002232 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002233 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002234 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07002235 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002236 return false;
2237 }
2238
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002239 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2240
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002241 // We need to start pausing the current activity so the top one can be resumed...
2242 final boolean dontWaitForPause = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002243 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002244 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002245 if (DEBUG_STATES) Slog.d(TAG_STATES,
2246 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08002247 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002248 }
2249 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002250 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002251 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002252 // At this point we want to put the upcoming activity's process
2253 // at the top of the LRU list, since we know we will be needing it
2254 // very soon and it would be a waste to let it get killed if it
2255 // happens to be sitting towards the end.
2256 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07002257 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002258 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002259 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002260 return true;
Wale Ogunwalecac5c322016-05-23 10:56:33 -07002261 } else if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
2262 mStackSupervisor.allResumedActivitiesComplete()) {
2263 // It is possible for the activity to be resumed when we paused back stacks above if the
2264 // next activity doesn't have to wait for pause to complete.
2265 // So, nothing else to-do except:
2266 // Make sure we have executed any pending transitions, since there
2267 // should be nothing left to do at this point.
2268 mWindowManager.executeAppTransition();
2269 mNoAnimActivities.clear();
2270 ActivityOptions.abort(options);
2271 if (DEBUG_STATES) Slog.d(TAG_STATES,
2272 "resumeTopActivityLocked: Top activity resumed (dontWaitForPause) " + next);
2273 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2274 return true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002275 }
2276
Christopher Tated3f175c2012-06-14 14:16:54 -07002277 // If the most recent activity was noHistory but was only stopped rather
2278 // than stopped+finished because the device went to sleep, we need to make
2279 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07002280 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002281 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002282 if (DEBUG_STATES) Slog.d(TAG_STATES,
2283 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002284 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002285 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002286 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002287 }
2288
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002289 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002290 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2291 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002292 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002293 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2294 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002295 } else {
2296 // The next activity is already visible, so hide the previous
2297 // activity's windows right now so we can show the new one ASAP.
2298 // We only do this if the previous is finishing, which should mean
2299 // it is on top of the one being resumed so hiding it quickly
2300 // is good. Otherwise, we want to do the normal route of allowing
2301 // the resumed activity to be shown so we can decide if the
2302 // previous should actually be hidden depending on whether the
2303 // new one is found to be full-screen or not.
2304 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002305 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002306 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2307 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08002308 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002309 + ", nowVisible=" + next.nowVisible);
2310 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002311 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002312 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002313 + ", waitingVisible="
2314 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2315 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002316 }
2317 }
2318 }
2319
Dianne Hackborne7f97212011-02-24 14:40:20 -08002320 // Launching this app's activity, make sure the app is no longer
2321 // considered stopped.
2322 try {
2323 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002324 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002325 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002326 } catch (IllegalArgumentException e) {
2327 Slog.w(TAG, "Failed trying to unstop package "
2328 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002329 }
2330
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002331 // We are starting up the next activity, so tell the window manager
2332 // that the previous one will be hidden soon. This way it can know
2333 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002334 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002335 if (prev != null) {
2336 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002337 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002338 "Prepare close transition: prev=" + prev);
2339 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002340 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002341 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002342 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002343 mWindowManager.prepareAppTransition(prev.task == next.task
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002344 ? TRANSIT_ACTIVITY_CLOSE
2345 : TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002346 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002347 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002348 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002349 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2350 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002351 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002352 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002353 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002354 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002355 mWindowManager.prepareAppTransition(prev.task == next.task
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002356 ? TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002357 : next.mLaunchTaskBehind
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002358 ? TRANSIT_TASK_OPEN_BEHIND
2359 : TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002360 }
2361 }
Craig Mautner967212c2013-04-13 21:10:58 -07002362 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002363 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002364 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002365 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002366 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002367 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002368 mWindowManager.prepareAppTransition(TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002369 }
2370 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002371
2372 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002373 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002374 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2375 if (opts != null) {
2376 resumeAnimOptions = opts.toBundle();
2377 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002378 next.applyOptionsLocked();
2379 } else {
2380 next.clearOptionsLocked();
2381 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002382
Craig Mautnercf910b02013-04-23 11:23:27 -07002383 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002384 if (next.app != null && next.app.thread != null) {
Chong Zhangdea4bd92016-03-15 12:50:03 -07002385 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next
2386 + " stopped=" + next.stopped + " visible=" + next.visible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002387
Chong Zhangd127c6d2016-05-02 16:36:41 -07002388 // If the previous activity is translucent, force a visibility update of
2389 // the next activity, so that it's added to WM's opening app list, and
2390 // transition animation can be set up properly.
2391 // For example, pressing Home button with a translucent activity in focus.
2392 // Launcher is already visible in this case. If we don't add it to opening
2393 // apps, maybeUpdateTransitToWallpaper() will fail to identify this as a
2394 // TRANSIT_WALLPAPER_OPEN animation, and run some funny animation.
2395 final boolean lastActivityTranslucent = lastStack != null
2396 && (!lastStack.mFullscreen
2397 || (lastStack.mLastPausedActivity != null
2398 && !lastStack.mLastPausedActivity.fullscreen));
2399
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002400 // This activity is now becoming visible.
Chong Zhangd127c6d2016-05-02 16:36:41 -07002401 if (!next.visible || next.stopped || lastActivityTranslucent) {
Jorim Jaggi1b025a62016-02-03 19:27:49 -08002402 mWindowManager.setAppVisibility(next.appToken, true);
2403 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002404
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002405 // schedule launch ticks to collect information about slow apps.
2406 next.startLaunchTickingLocked();
2407
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002408 ActivityRecord lastResumedActivity =
2409 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002410 ActivityState lastState = next.state;
2411
2412 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002413
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002414 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002415 next.state = ActivityState.RESUMED;
2416 mResumedActivity = next;
2417 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002418 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002419 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002420 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002421 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002422
2423 // Have the window manager re-evaluate the orientation of
2424 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002425 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002426 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002427 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002428 mService.mConfiguration,
2429 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2430 if (config != null) {
2431 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002432 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002433 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002434 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002435
Craig Mautner525f3d92013-05-07 14:01:50 -07002436 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002437 // The configuration update wasn't able to keep the existing
2438 // instance of the activity, and instead started a new one.
2439 // We should be all done, but let's just make sure our activity
2440 // is still at the top and schedule another run if something
2441 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002442 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002443 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002444 "Activity config changed during resume: " + next
2445 + ", new next: " + nextNext);
2446 if (nextNext != next) {
2447 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002448 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002449 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002450 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002451 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002452 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002453 return true;
2454 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002455 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002456 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002457 }
Craig Mautner58547802013-03-05 08:23:53 -08002458
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002459 try {
2460 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002461 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002462 if (a != null) {
2463 final int N = a.size();
2464 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002465 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2466 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002467 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002468 }
2469 }
2470
2471 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002472 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002473 }
2474
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002475 // Well the app will no longer be stopped.
2476 // Clear app token stopped state in window manager if needed.
2477 mWindowManager.notifyAppStopped(next.appToken, false);
2478
Craig Mautner299f9602015-01-26 09:47:33 -08002479 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2480 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002481
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002482 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002483 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002484 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002485 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002486 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002487 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002488 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002489
Craig Mautner0eea92c2013-05-16 13:35:39 -07002490 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002491
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002492 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002493 } catch (Exception e) {
2494 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002495 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002496 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002497 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002498 if (lastStack != null) {
2499 lastStack.mResumedActivity = lastResumedActivity;
2500 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002501 Slog.i(TAG, "Restarting because process died: " + next);
2502 if (!next.hasBeenLaunched) {
2503 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002504 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2505 mStackSupervisor.isFrontStack(lastStack)) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07002506 next.showStartingWindow(null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002507 }
George Mount2c92c972014-03-20 09:38:23 -07002508 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002509 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002510 return true;
2511 }
2512
2513 // From this point on, if something goes wrong there is no way
2514 // to recover the activity.
2515 try {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002516 completeResumeLocked(next);
2517 } catch (Exception e) {
2518 // If any exception gets thrown, toss away this
2519 // activity and try the next one.
2520 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002521 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002522 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002523 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002524 return true;
2525 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002526 } else {
2527 // Whoops, need to restart this activity!
2528 if (!next.hasBeenLaunched) {
2529 next.hasBeenLaunched = true;
2530 } else {
2531 if (SHOW_APP_STARTING_PREVIEW) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07002532 next.showStartingWindow(null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002533 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002534 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002535 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002536 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002537 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002538 }
2539
Craig Mautnercf910b02013-04-23 11:23:27 -07002540 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002541 return true;
2542 }
2543
riddle_hsuc215a4f2014-12-27 12:10:45 +08002544 private TaskRecord getNextTask(TaskRecord targetTask) {
2545 final int index = mTaskHistory.indexOf(targetTask);
2546 if (index >= 0) {
2547 final int numTasks = mTaskHistory.size();
2548 for (int i = index + 1; i < numTasks; ++i) {
2549 TaskRecord task = mTaskHistory.get(i);
2550 if (task.userId == targetTask.userId) {
2551 return task;
2552 }
2553 }
2554 }
2555 return null;
2556 }
2557
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002558 private void insertTaskAtPosition(TaskRecord task, int position) {
2559 if (position >= mTaskHistory.size()) {
2560 insertTaskAtTop(task, null);
2561 return;
2562 }
2563 // Calculate maximum possible position for this task.
2564 int maxPosition = mTaskHistory.size();
2565 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002566 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002567 // Put non-current user tasks below current user tasks.
2568 while (maxPosition > 0) {
2569 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2570 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002571 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002572 break;
2573 }
2574 maxPosition--;
2575 }
2576 }
2577 position = Math.min(position, maxPosition);
2578 mTaskHistory.remove(task);
2579 mTaskHistory.add(position, task);
2580 updateTaskMovement(task, true);
2581 }
2582
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002583 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002584 // If the moving task is over home stack, transfer its return type to next task
2585 if (task.isOverHomeStack()) {
2586 final TaskRecord nextTask = getNextTask(task);
2587 if (nextTask != null) {
2588 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2589 }
2590 }
2591
Craig Mautner9c85c202013-10-04 20:11:26 -07002592 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002593 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002594 if (isOnHomeDisplay()) {
2595 ActivityStack lastStack = mStackSupervisor.getLastStack();
2596 final boolean fromHome = lastStack.isHomeStack();
2597 if (!isHomeStack() && (fromHome || topTask() != task)) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002598 int returnToType = APPLICATION_ACTIVITY_TYPE;
2599 if (fromHome && StackId.allowTopTaskToReturnHome(mStackId)) {
2600 returnToType = lastStack.topTask() == null
2601 ? HOME_ACTIVITY_TYPE : lastStack.topTask().taskType;
2602 }
2603 task.setTaskToReturnTo(returnToType);
Craig Mautnere0a38842013-12-16 16:14:02 -08002604 }
2605 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002606 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002607 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002608
Craig Mautnerac6f8432013-07-17 13:24:59 -07002609 mTaskHistory.remove(task);
2610 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002611 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002612 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002613 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002614 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002615 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002616 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002617 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002618 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002619 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002620 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002621 break;
2622 }
2623 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002624 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002625 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002626 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002627 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002628 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002629
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002630 final void startActivityLocked(ActivityRecord r, boolean newTask, boolean keepCurTransition,
2631 ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002632 TaskRecord rTask = r.task;
2633 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002634 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2635 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002636 // Last activity in task had been removed or ActivityManagerService is reusing task.
2637 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002638 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002639 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002640 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002641 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002642 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002643 if (!newTask) {
2644 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002645 boolean startIt = true;
2646 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2647 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002648 if (task.getTopActivity() == null) {
2649 // All activities in task are finishing.
2650 continue;
2651 }
Craig Mautner70a86932013-02-28 22:37:44 -08002652 if (task == r.task) {
2653 // Here it is! Now, if this is not yet visible to the
2654 // user, then just add it without starting; it will
2655 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002656 if (!startIt) {
2657 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2658 + task, new RuntimeException("here").fillInStackTrace());
2659 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002660 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002661 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002662 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002663 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002664 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002665 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002666 return;
2667 }
2668 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002669 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002670 startIt = false;
2671 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002672 }
2673 }
2674
2675 // Place a new activity at top of stack, so it is next to interact
2676 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002677
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002678 // If we are not placing the new activity frontmost, we do not want
2679 // to deliver the onUserLeaving callback to the actual frontmost
2680 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002681 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002682 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002683 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002684 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002685 }
Craig Mautner70a86932013-02-28 22:37:44 -08002686
2687 task = r.task;
2688
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002689 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002690 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002691 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002692 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002693 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002694
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002695 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002696 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002697 // We want to show the starting preview window if we are
2698 // switching to a new task, or the next activity's process is
2699 // not currently running.
2700 boolean showStartingIcon = newTask;
2701 ProcessRecord proc = r.app;
2702 if (proc == null) {
2703 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2704 }
2705 if (proc == null || proc.thread == null) {
2706 showStartingIcon = true;
2707 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002708 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002709 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002710 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002711 mWindowManager.prepareAppTransition(TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002712 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002713 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002714 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002715 ? r.mLaunchTaskBehind
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002716 ? TRANSIT_TASK_OPEN_BEHIND
2717 : TRANSIT_TASK_OPEN
2718 : TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002719 mNoAnimActivities.remove(r);
2720 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002721 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002722 boolean doShow = true;
2723 if (newTask) {
2724 // Even though this activity is starting fresh, we still need
2725 // to reset it to make sure we apply affinities to move any
2726 // existing activities from other tasks in to it.
2727 // If the caller has requested that the target task be
2728 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002729 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002730 resetTaskIfNeededLocked(r, r);
2731 doShow = topRunningNonDelayedActivityLocked(null) == r;
2732 }
Chong Zhang280d3322015-11-03 17:27:26 -08002733 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002734 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2735 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002736 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002737 if (r.mLaunchTaskBehind) {
2738 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2739 // tell WindowManager that r is visible even though it is at the back of the stack.
2740 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002741 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002742 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002743 // Figure out if we are transitioning from another activity that is
2744 // "has the same starting icon" as the next one. This allows the
2745 // window manager to keep the previous window it had previously
2746 // created, if it still had one.
Wale Ogunwale3b232392016-05-13 15:37:13 -07002747 ActivityRecord prev = r.task.topRunningActivityWithStartingWindowLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002748 if (prev != null) {
2749 // We don't want to reuse the previous starting preview if:
2750 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002751 if (prev.task != r.task) {
2752 prev = null;
2753 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002754 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002755 else if (prev.nowVisible) {
2756 prev = null;
2757 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002758 }
Wale Ogunwale3b232392016-05-13 15:37:13 -07002759 r.showStartingWindow(prev, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002760 }
2761 } else {
2762 // If this is the first activity, don't do any fancy animations,
2763 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002764 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002765 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002766 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002767 }
2768 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002769 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002770 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002771 }
2772
Dianne Hackbornbe707852011-11-11 14:32:10 -08002773 final void validateAppTokensLocked() {
2774 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002775 mValidateAppTokens.ensureCapacity(numActivities());
2776 final int numTasks = mTaskHistory.size();
2777 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2778 TaskRecord task = mTaskHistory.get(taskNdx);
2779 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002780 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002781 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002782 }
Craig Mautner000f0022013-02-26 15:04:29 -08002783 TaskGroup group = new TaskGroup();
2784 group.taskId = task.taskId;
2785 mValidateAppTokens.add(group);
2786 final int numActivities = activities.size();
2787 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2788 final ActivityRecord r = activities.get(activityNdx);
2789 group.tokens.add(r.appToken);
2790 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002791 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002792 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002793 }
2794
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002795 /**
2796 * Perform a reset of the given task, if needed as part of launching it.
2797 * Returns the new HistoryRecord at the top of the task.
2798 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002799 /**
2800 * Helper method for #resetTaskIfNeededLocked.
2801 * We are inside of the task being reset... we'll either finish this activity, push it out
2802 * for another task, or leave it as-is.
2803 * @param task The task containing the Activity (taskTop) that might be reset.
2804 * @param forceReset
2805 * @return An ActivityOptions that needs to be processed.
2806 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002807 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002808 ActivityOptions topOptions = null;
2809
2810 int replyChainEnd = -1;
2811 boolean canMoveOptions = true;
2812
2813 // We only do this for activities that are not the root of the task (since if we finish
2814 // the root, we may no longer have the task!).
2815 final ArrayList<ActivityRecord> activities = task.mActivities;
2816 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002817 final int rootActivityNdx = task.findEffectiveRootIndex();
2818 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002819 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002820 if (target.frontOfTask)
2821 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002822
2823 final int flags = target.info.flags;
2824 final boolean finishOnTaskLaunch =
2825 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2826 final boolean allowTaskReparenting =
2827 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2828 final boolean clearWhenTaskReset =
2829 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2830
2831 if (!finishOnTaskLaunch
2832 && !clearWhenTaskReset
2833 && target.resultTo != null) {
2834 // If this activity is sending a reply to a previous
2835 // activity, we can't do anything with it now until
2836 // we reach the start of the reply chain.
2837 // XXX note that we are assuming the result is always
2838 // to the previous activity, which is almost always
2839 // the case but we really shouldn't count on.
2840 if (replyChainEnd < 0) {
2841 replyChainEnd = i;
2842 }
2843 } else if (!finishOnTaskLaunch
2844 && !clearWhenTaskReset
2845 && allowTaskReparenting
2846 && target.taskAffinity != null
2847 && !target.taskAffinity.equals(task.affinity)) {
2848 // If this activity has an affinity for another
2849 // task, then we need to move it out of here. We will
2850 // move it as far out of the way as possible, to the
2851 // bottom of the activity stack. This also keeps it
2852 // correctly ordered with any activities we previously
2853 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002854 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002855 final ActivityRecord bottom =
2856 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002857 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002858 if (bottom != null && target.taskAffinity != null
2859 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002860 // If the activity currently at the bottom has the
2861 // same task affinity as the one we are moving,
2862 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002863 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002864 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002865 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002866 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002867 targetTask = createTaskRecord(
2868 mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
2869 target.info, null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002870 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002871 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002872 + " out to new task " + target.task);
2873 }
2874
Wale Ogunwale706ed792015-08-02 10:29:44 -07002875 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002876
Craig Mautner525f3d92013-05-07 14:01:50 -07002877 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002878 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2879 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002880 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002881 if (p.finishing) {
2882 continue;
2883 }
2884
Craig Mautnere3a74d52013-02-22 14:14:58 -08002885 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002886 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002887 topOptions = p.takeOptionsLocked();
2888 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002889 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002890 }
2891 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002892 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2893 "Removing activity " + p + " from task=" + task + " adding to task="
2894 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002895 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2896 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002897 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002898 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002899
Wale Ogunwale706ed792015-08-02 10:29:44 -07002900 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002901 }
2902
Wale Ogunwale706ed792015-08-02 10:29:44 -07002903 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002904 if (VALIDATE_TOKENS) {
2905 validateAppTokensLocked();
2906 }
2907
2908 replyChainEnd = -1;
2909 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2910 // If the activity should just be removed -- either
2911 // because it asks for it, or the task should be
2912 // cleared -- then finish it and anything that is
2913 // part of its reply chain.
2914 int end;
2915 if (clearWhenTaskReset) {
2916 // In this case, we want to finish this activity
2917 // and everything above it, so be sneaky and pretend
2918 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002919 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002920 } else if (replyChainEnd < 0) {
2921 end = i;
2922 } else {
2923 end = replyChainEnd;
2924 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002925 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002926 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002927 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002928 if (p.finishing) {
2929 continue;
2930 }
2931 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002932 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002933 topOptions = p.takeOptionsLocked();
2934 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002935 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002936 }
2937 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002938 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002939 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002940 if (finishActivityLocked(
2941 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002942 end--;
2943 srcPos--;
2944 }
2945 }
2946 replyChainEnd = -1;
2947 } else {
2948 // If we were in the middle of a chain, well the
2949 // activity that started it all doesn't want anything
2950 // special, so leave it all as-is.
2951 replyChainEnd = -1;
2952 }
2953 }
2954
2955 return topOptions;
2956 }
2957
2958 /**
2959 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2960 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2961 * @param affinityTask The task we are looking for an affinity to.
2962 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2963 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2964 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2965 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002966 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002967 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002968 int replyChainEnd = -1;
2969 final int taskId = task.taskId;
2970 final String taskAffinity = task.affinity;
2971
2972 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2973 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002974 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2975
2976 // Do not operate on or below the effective root Activity.
2977 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002978 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002979 if (target.frontOfTask)
2980 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002981
2982 final int flags = target.info.flags;
2983 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2984 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2985
2986 if (target.resultTo != null) {
2987 // If this activity is sending a reply to a previous
2988 // activity, we can't do anything with it now until
2989 // we reach the start of the reply chain.
2990 // XXX note that we are assuming the result is always
2991 // to the previous activity, which is almost always
2992 // the case but we really shouldn't count on.
2993 if (replyChainEnd < 0) {
2994 replyChainEnd = i;
2995 }
2996 } else if (topTaskIsHigher
2997 && allowTaskReparenting
2998 && taskAffinity != null
2999 && taskAffinity.equals(target.taskAffinity)) {
3000 // This activity has an affinity for our task. Either remove it if we are
3001 // clearing or move it over to our task. Note that
3002 // we currently punt on the case where we are resetting a
3003 // task that is not at the top but who has activities above
3004 // with an affinity to it... this is really not a normal
3005 // case, and we will need to later pull that task to the front
3006 // and usually at that point we will do the reset and pick
3007 // up those remaining activities. (This only happens if
3008 // someone starts an activity in a new task from an activity
3009 // in a task that is not currently on top.)
3010 if (forceReset || finishOnTaskLaunch) {
3011 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003012 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
3013 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003014 for (int srcPos = start; srcPos >= i; --srcPos) {
3015 final ActivityRecord p = activities.get(srcPos);
3016 if (p.finishing) {
3017 continue;
3018 }
Todd Kennedy539db512014-12-15 09:57:55 -08003019 finishActivityLocked(
3020 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003021 }
3022 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003023 if (taskInsertionPoint < 0) {
3024 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08003025
Craig Mautner77878772013-03-04 19:46:24 -08003026 }
Craig Mautner77878772013-03-04 19:46:24 -08003027
3028 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003029 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
3030 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
3031 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08003032 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003033 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07003034 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08003035 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003036
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003037 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3038 "Removing and adding activity " + p + " to stack at " + task
3039 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003040 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
3041 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003042 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003043 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003044 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08003045 if (VALIDATE_TOKENS) {
3046 validateAppTokensLocked();
3047 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08003048
3049 // Now we've moved it in to place... but what if this is
3050 // a singleTop activity and we have put it on top of another
3051 // instance of the same activity? Then we drop the instance
3052 // below so it remains singleTop.
3053 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
3054 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003055 int targetNdx = taskActivities.indexOf(target);
3056 if (targetNdx > 0) {
3057 ActivityRecord p = taskActivities.get(targetNdx - 1);
3058 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08003059 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
3060 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003061 }
3062 }
3063 }
3064 }
3065
3066 replyChainEnd = -1;
3067 }
3068 }
Craig Mautner77878772013-03-04 19:46:24 -08003069 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003070 }
3071
Craig Mautner8849a5e2013-04-02 16:41:03 -07003072 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08003073 ActivityRecord newActivity) {
3074 boolean forceReset =
3075 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
3076 if (ACTIVITY_INACTIVE_RESET_TIME > 0
3077 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
3078 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
3079 forceReset = true;
3080 }
3081 }
3082
3083 final TaskRecord task = taskTop.task;
3084
3085 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
3086 * for remaining tasks. Used for later tasks to reparent to task. */
3087 boolean taskFound = false;
3088
3089 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
3090 ActivityOptions topOptions = null;
3091
Craig Mautner77878772013-03-04 19:46:24 -08003092 // Preserve the location for reparenting in the new task.
3093 int reparentInsertionPoint = -1;
3094
Craig Mautnere3a74d52013-02-22 14:14:58 -08003095 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
3096 final TaskRecord targetTask = mTaskHistory.get(i);
3097
3098 if (targetTask == task) {
3099 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
3100 taskFound = true;
3101 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003102 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
3103 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003104 }
3105 }
3106
Craig Mautner70a86932013-02-28 22:37:44 -08003107 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08003108 if (taskNdx >= 0) {
3109 do {
3110 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
3111 } while (taskTop == null && taskNdx >= 0);
3112 }
Craig Mautner70a86932013-02-28 22:37:44 -08003113
Craig Mautnere3a74d52013-02-22 14:14:58 -08003114 if (topOptions != null) {
3115 // If we got some ActivityOptions from an activity on top that
3116 // was removed from the task, propagate them to the new real top.
3117 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003118 taskTop.updateOptionsLocked(topOptions);
3119 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003120 topOptions.abort();
3121 }
3122 }
3123
3124 return taskTop;
3125 }
3126
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003127 void sendActivityResultLocked(int callingUid, ActivityRecord r,
3128 String resultWho, int requestCode, int resultCode, Intent data) {
3129
3130 if (callingUid > 0) {
3131 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003132 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003133 }
3134
3135 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
3136 + " : who=" + resultWho + " req=" + requestCode
3137 + " res=" + resultCode + " data=" + data);
3138 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
3139 try {
3140 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
3141 list.add(new ResultInfo(resultWho, requestCode,
3142 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08003143 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003144 return;
3145 } catch (Exception e) {
3146 Slog.w(TAG, "Exception thrown sending result to " + r, e);
3147 }
3148 }
3149
3150 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
3151 }
3152
Craig Mautner299f9602015-01-26 09:47:33 -08003153 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003154 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
3155 return;
3156 }
3157
3158 final ActivityRecord next = topRunningActivityLocked();
3159 final String myReason = reason + " adjustFocus";
3160 if (next != r) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003161 if (next != null && StackId.keepFocusInStackIfPossible(mStackId) && isFocusable()) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003162 // For freeform, docked, and pinned stacks we always keep the focus within the
3163 // stack as long as there is a running activity in the stack that we can adjust
3164 // focus to.
3165 mService.setFocusedActivityLocked(next, myReason);
3166 return;
3167 } else {
3168 final TaskRecord task = r.task;
3169 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003170 final int taskToReturnTo = task.getTaskToReturnTo();
3171
Wale Ogunwaled045c822015-12-02 09:14:28 -08003172 // For non-fullscreen stack, we want to move the focus to the next visible
3173 // stack to prevent the home screen from moving to the top and obscuring
3174 // other visible stacks.
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003175 if (!mFullscreen
3176 && adjustFocusToNextFocusableStackLocked(taskToReturnTo, myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003177 return;
3178 }
3179 // Move the home stack to the top if this stack is fullscreen or there is no
3180 // other visible stack.
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003181 if (mStackSupervisor.moveHomeStackTaskToTop(taskToReturnTo, myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003182 // Activity focus was already adjusted. Nothing else to do...
3183 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003184 }
Craig Mautner04f0b702013-10-22 12:31:01 -07003185 }
3186 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08003187 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003188
Wale Ogunwaled046a012015-12-24 13:05:59 -08003189 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07003190 }
3191
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003192 private boolean adjustFocusToNextFocusableStackLocked(int taskToReturnTo, String reason) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003193 final ActivityStack stack = getNextFocusableStackLocked();
3194 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003195 if (stack == null) {
3196 return false;
3197 }
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003198
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003199 final ActivityRecord top = stack.topRunningActivityLocked();
3200
3201 if (stack.isHomeStack() && (top == null || !top.visible)) {
3202 // If we will be focusing on the home stack next and its current top activity isn't
3203 // visible, then use the task return to value to determine the home task to display next.
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003204 return mStackSupervisor.moveHomeStackTaskToTop(taskToReturnTo, reason);
3205 }
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003206 return mService.setFocusedActivityLocked(top, myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003207 }
3208
Craig Mautnerf3333272013-04-22 10:55:53 -07003209 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003210 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003211 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3212 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3213 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07003214 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003215 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003216 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
3217 "stop-no-history", false)) {
3218 // Activity was finished, no need to continue trying to schedule stop.
3219 adjustFocusedActivityLocked(r, "stopActivityFinished");
3220 r.resumeKeyDispatchingLocked();
3221 return;
3222 }
Christopher Tated3f175c2012-06-14 14:16:54 -07003223 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003224 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07003225 + " on stop because we're just sleeping");
3226 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003227 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003228 }
3229
3230 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08003231 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003232 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003233 try {
3234 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003235 if (DEBUG_STATES) Slog.v(TAG_STATES,
3236 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003237 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003238 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3239 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003240 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003241 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003242 }
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003243 EventLogTags.writeAmStopActivity(
3244 r.userId, System.identityHashCode(r), r.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003245 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07003246 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003247 r.setSleeping(true);
3248 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003249 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003250 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003251 } catch (Exception e) {
3252 // Maybe just ignore exceptions here... if the process
3253 // has crashed, our death notification will clean things
3254 // up.
3255 Slog.w(TAG, "Exception thrown during pause", e);
3256 // Just in case, assume it to be stopped.
3257 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003258 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003259 r.state = ActivityState.STOPPED;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003260 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003261 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003262 }
3263 }
3264 }
3265 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003266
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003267 /**
3268 * @return Returns true if the activity is being finished, false if for
3269 * some reason it is being left as-is.
3270 */
3271 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003272 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003273 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003274 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3275 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003276 + ", result=" + resultCode + ", data=" + resultData
3277 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003278 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003279 return false;
3280 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003281
Craig Mautnerd44711d2013-02-23 11:24:36 -08003282 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003283 return true;
3284 }
3285
Craig Mautnerd2328952013-03-05 12:46:26 -08003286 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003287 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3288 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3289 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3290 ActivityRecord r = activities.get(activityNdx);
3291 if (r.resultTo == self && r.requestCode == requestCode) {
3292 if ((r.resultWho == null && resultWho == null) ||
3293 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3294 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3295 false);
3296 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003297 }
3298 }
3299 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003300 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003301 }
3302
Adrian Roos20d7df32016-01-12 18:59:43 +01003303 final TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003304 ActivityRecord r = topRunningActivityLocked();
Adrian Roos20d7df32016-01-12 18:59:43 +01003305 TaskRecord finishedTask = null;
3306 if (r == null || r.app != app) {
3307 return null;
3308 }
3309 Slog.w(TAG, " Force finishing activity "
3310 + r.intent.getComponent().flattenToShortString());
3311 int taskNdx = mTaskHistory.indexOf(r.task);
3312 int activityNdx = r.task.mActivities.indexOf(r);
3313 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
3314 finishedTask = r.task;
3315 // Also terminate any activities below it that aren't yet
3316 // stopped, to avoid a situation where one will get
3317 // re-start our crashing activity once it gets resumed again.
3318 --activityNdx;
3319 if (activityNdx < 0) {
3320 do {
3321 --taskNdx;
3322 if (taskNdx < 0) {
3323 break;
3324 }
3325 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3326 } while (activityNdx < 0);
3327 }
3328 if (activityNdx >= 0) {
3329 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
3330 if (r.state == ActivityState.RESUMED
3331 || r.state == ActivityState.PAUSING
3332 || r.state == ActivityState.PAUSED) {
3333 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
3334 Slog.w(TAG, " Force finishing activity "
3335 + r.intent.getComponent().flattenToShortString());
3336 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003337 }
3338 }
3339 }
Adrian Roos20d7df32016-01-12 18:59:43 +01003340 return finishedTask;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003341 }
3342
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003343 final void finishVoiceTask(IVoiceInteractionSession session) {
3344 IBinder sessionBinder = session.asBinder();
3345 boolean didOne = false;
3346 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3347 TaskRecord tr = mTaskHistory.get(taskNdx);
3348 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3349 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3350 ActivityRecord r = tr.mActivities.get(activityNdx);
3351 if (!r.finishing) {
3352 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3353 false);
3354 didOne = true;
3355 }
3356 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003357 } else {
3358 // Check if any of the activities are using voice
3359 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3360 ActivityRecord r = tr.mActivities.get(activityNdx);
3361 if (r.voiceSession != null
3362 && r.voiceSession.asBinder() == sessionBinder) {
3363 // Inform of cancellation
3364 r.clearVoiceSessionLocked();
3365 try {
3366 r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3367 null);
3368 } catch (RemoteException re) {
3369 // Ok
3370 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003371 mService.finishRunningVoiceLocked();
3372 break;
3373 }
3374 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003375 }
3376 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003377
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003378 if (didOne) {
3379 mService.updateOomAdjLocked();
3380 }
3381 }
3382
Craig Mautnerd2328952013-03-05 12:46:26 -08003383 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003384 ArrayList<ActivityRecord> activities = r.task.mActivities;
3385 for (int index = activities.indexOf(r); index >= 0; --index) {
3386 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003387 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003388 break;
3389 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003390 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003391 }
3392 return true;
3393 }
3394
Dianne Hackborn5c607432012-02-28 14:44:19 -08003395 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3396 // send the result
3397 ActivityRecord resultTo = r.resultTo;
3398 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003399 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003400 + " who=" + r.resultWho + " req=" + r.requestCode
3401 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003402 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003403 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003404 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003405 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003406 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003407 if (r.info.applicationInfo.uid > 0) {
3408 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3409 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003410 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003411 }
3412 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3413 resultData);
3414 r.resultTo = null;
3415 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003416 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003417
3418 // Make sure this HistoryRecord is not holding on to other resources,
3419 // because clients have remote IPC references to this object so we
3420 // can't assume that will go away and want to avoid circular IPC refs.
3421 r.results = null;
3422 r.pendingResults = null;
3423 r.newIntents = null;
3424 r.icicle = null;
3425 }
3426
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003427 /**
3428 * @return Returns true if this activity has been removed from the history
3429 * list, or false if it is still in the list and will be removed later.
3430 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003431 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3432 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003433 if (r.finishing) {
3434 Slog.w(TAG, "Duplicate finish request for " + r);
3435 return false;
3436 }
3437
Wale Ogunwale7d701172015-03-11 15:36:30 -07003438 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003439 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003440 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003441 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003442 task.taskId, r.shortComponentName, reason);
3443 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003444 final int index = activities.indexOf(r);
3445 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003446 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003447 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003448 // If the caller asked that this activity (and all above it)
3449 // be cleared when the task is reset, don't lose that information,
3450 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003451 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003452 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003453 }
3454 }
3455
3456 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003457
Craig Mautner299f9602015-01-26 09:47:33 -08003458 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003459
Dianne Hackborn5c607432012-02-28 14:44:19 -08003460 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003461
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003462 final boolean endTask = index <= 0;
3463 final int transit = endTask ? TRANSIT_TASK_CLOSE : TRANSIT_ACTIVITY_CLOSE;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003464 if (mResumedActivity == r) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003465
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003466 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003467 "Prepare close transition: finishing " + r);
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003468 mWindowManager.prepareAppTransition(transit, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003469
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003470 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003471 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003472
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003473 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003474 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3475 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3476 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003477 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003478 }
3479
Craig Mautneraea74a52014-03-08 14:23:10 -08003480 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003481 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003482 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003483 } else if (r.state != ActivityState.PAUSING) {
3484 // If the activity is PAUSING, we will complete the finish once
3485 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003486 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003487 if (r.visible) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003488 mWindowManager.prepareAppTransition(transit, false);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003489 mWindowManager.setAppVisibility(r.appToken, false);
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003490 mWindowManager.executeAppTransition();
Chong Zhangb8bd0692016-04-27 17:29:28 -07003491 if (!mStackSupervisor.mWaitingVisibleActivities.contains(r)) {
3492 mStackSupervisor.mWaitingVisibleActivities.add(r);
3493 }
Chong Zhang66ed4c62015-11-12 14:56:28 -08003494 }
Chong Zhang4c5f7ff2016-04-22 11:31:51 -07003495 return finishCurrentActivityLocked(r, (r.visible || r.nowVisible) ?
3496 FINISH_AFTER_VISIBLE : FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003497 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003498 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003499 }
3500
3501 return false;
3502 }
3503
Craig Mautnerf3333272013-04-22 10:55:53 -07003504 static final int FINISH_IMMEDIATELY = 0;
3505 static final int FINISH_AFTER_PAUSE = 1;
3506 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003507
Craig Mautnerf3333272013-04-22 10:55:53 -07003508 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003509 // First things first: if this activity is currently visible,
3510 // and the resumed activity is not yet visible, then hold off on
3511 // finishing until the resumed one becomes visible.
Chong Zhang824b6dc2016-04-27 14:11:12 -07003512
3513 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
3514
Chong Zhangefd9a5b2016-04-26 16:21:07 -07003515 if (mode == FINISH_AFTER_VISIBLE && (r.visible || r.nowVisible)
Chong Zhang824b6dc2016-04-27 14:11:12 -07003516 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003517 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08003518 addToStopping(r, false /* immediate */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003519 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003520 if (DEBUG_STATES) Slog.v(TAG_STATES,
3521 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003522 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003523 if (oomAdj) {
3524 mService.updateOomAdjLocked();
3525 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003526 return r;
3527 }
3528
3529 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003530 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003531 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003532 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003533 if (mResumedActivity == r) {
3534 mResumedActivity = null;
3535 }
3536 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003537 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003538 r.state = ActivityState.FINISHING;
3539
3540 if (mode == FINISH_IMMEDIATELY
Wale Ogunwaleae30f302016-05-02 10:35:45 -07003541 || (prevState == ActivityState.PAUSED
3542 && (mode == FINISH_AFTER_PAUSE || mStackId == PINNED_STACK_ID))
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003543 || prevState == ActivityState.STOPPED
3544 || prevState == ActivityState.INITIALIZING) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003545 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003546 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003547 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003548 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003549 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003550 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003551 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3552 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003553 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003554 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003555
3556 // Need to go through the full pause cycle to get this
3557 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003558 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003559 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003560 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003561 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003562 return r;
3563 }
3564
Craig Mautneree36c772014-07-16 14:56:05 -07003565 void finishAllActivitiesLocked(boolean immediately) {
3566 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003567 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3568 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3569 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3570 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003571 noActivitiesInStack = false;
3572 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003573 continue;
3574 }
Craig Mautneree36c772014-07-16 14:56:05 -07003575 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003576 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3577 }
3578 }
Craig Mautneree36c772014-07-16 14:56:05 -07003579 if (noActivitiesInStack) {
3580 mActivityContainer.onTaskListEmptyLocked();
3581 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003582 }
3583
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003584 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3585 // Basic case: for simple app-centric recents, we need to recreate
3586 // the task if the affinity has changed.
3587 if (srec == null || srec.task.affinity == null ||
3588 !srec.task.affinity.equals(destAffinity)) {
3589 return true;
3590 }
3591 // Document-centric case: an app may be split in to multiple documents;
3592 // they need to re-create their task if this current activity is the root
3593 // of a document, unless simply finishing it will return them to the the
3594 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003595 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3596 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003597 // Okay, this activity is at the root of its task. What to do, what to do...
3598 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3599 // Finishing won't return to an application, so we need to recreate.
3600 return true;
3601 }
3602 // We now need to get the task below it to determine what to do.
3603 int taskIdx = mTaskHistory.indexOf(srec.task);
3604 if (taskIdx <= 0) {
3605 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3606 return false;
3607 }
3608 if (taskIdx == 0) {
3609 // At the bottom of the stack, nothing to go back to.
3610 return true;
3611 }
3612 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3613 if (!srec.task.affinity.equals(prevTask.affinity)) {
3614 // These are different apps, so need to recreate.
3615 return true;
3616 }
3617 }
3618 return false;
3619 }
3620
Wale Ogunwale7d701172015-03-11 15:36:30 -07003621 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003622 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003623 final TaskRecord task = srec.task;
3624 final ArrayList<ActivityRecord> activities = task.mActivities;
3625 final int start = activities.indexOf(srec);
3626 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003627 return false;
3628 }
3629 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003630 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003631 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003632 final ComponentName dest = destIntent.getComponent();
3633 if (start > 0 && dest != null) {
3634 for (int i = finishTo; i >= 0; i--) {
3635 ActivityRecord r = activities.get(i);
3636 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003637 r.info.name.equals(dest.getClassName())) {
3638 finishTo = i;
3639 parent = r;
3640 foundParentInTask = true;
3641 break;
3642 }
3643 }
3644 }
3645
3646 IActivityController controller = mService.mController;
3647 if (controller != null) {
3648 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3649 if (next != null) {
3650 // ask watcher if this is allowed
3651 boolean resumeOK = true;
3652 try {
3653 resumeOK = controller.activityResuming(next.packageName);
3654 } catch (RemoteException e) {
3655 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003656 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003657 }
3658
3659 if (!resumeOK) {
3660 return false;
3661 }
3662 }
3663 }
3664 final long origId = Binder.clearCallingIdentity();
3665 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003666 ActivityRecord r = activities.get(i);
3667 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003668 // Only return the supplied result for the first activity finished
3669 resultCode = Activity.RESULT_CANCELED;
3670 resultData = null;
3671 }
3672
3673 if (parent != null && foundParentInTask) {
3674 final int parentLaunchMode = parent.info.launchMode;
3675 final int destIntentFlags = destIntent.getFlags();
3676 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3677 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3678 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3679 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003680 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3681 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003682 } else {
3683 try {
3684 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3685 destIntent.getComponent(), 0, srec.userId);
Filip Gruszczynski303210b2016-01-08 16:28:08 -08003686 int res = mService.mActivityStarter.startActivityLocked(srec.app.thread,
3687 destIntent, null /*ephemeralIntent*/, null, aInfo, null /*rInfo*/, null,
3688 null, parent.appToken, null, 0, -1, parent.launchedFromUid,
Todd Kennedy7440f172015-12-09 14:31:22 -08003689 parent.launchedFromPackage, -1, parent.launchedFromUid, 0, null,
3690 false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003691 foundParentInTask = res == ActivityManager.START_SUCCESS;
3692 } catch (RemoteException e) {
3693 foundParentInTask = false;
3694 }
3695 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003696 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003697 }
3698 }
3699 Binder.restoreCallingIdentity(origId);
3700 return foundParentInTask;
3701 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003702 /**
3703 * Perform the common clean-up of an activity record. This is called both
3704 * as part of destroyActivityLocked() (when destroying the client-side
3705 * representation) and cleaning things up as a result of its hosting
3706 * processing going away, in which case there is no remaining client-side
3707 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003708 *
3709 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003710 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003711 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3712 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003713 if (mResumedActivity == r) {
3714 mResumedActivity = null;
3715 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003716 if (mPausingActivity == r) {
3717 mPausingActivity = null;
3718 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003719 mService.resetFocusedActivityIfNeededLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003720
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003721 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003722 r.frozenBeforeDestroy = false;
3723
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003724 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003725 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003726 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003727 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003728 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003729 }
3730
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003731 // Make sure this record is no longer in the pending finishes list.
3732 // This could happen, for example, if we are trimming activities
3733 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003734 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003735 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003736
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003737 // Remove any pending results.
3738 if (r.finishing && r.pendingResults != null) {
3739 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3740 PendingIntentRecord rec = apr.get();
3741 if (rec != null) {
3742 mService.cancelIntentSenderLocked(rec, false);
3743 }
3744 }
3745 r.pendingResults = null;
3746 }
3747
3748 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003749 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003750 }
3751
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003752 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003753 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003754 if (getVisibleBehindActivity() == r) {
3755 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003756 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003757 }
3758
3759 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003760 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003761 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003762 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003763 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003764 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003765 }
3766
Craig Mautner299f9602015-01-26 09:47:33 -08003767 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003768 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003769 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003770 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003771 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3772 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3773
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003774 r.takeFromHistory();
3775 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003776 if (DEBUG_STATES) Slog.v(TAG_STATES,
3777 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003778 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003779 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003780 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003781 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003782 if (VALIDATE_TOKENS) {
3783 validateAppTokensLocked();
3784 }
Craig Mautner312ba862014-02-10 17:55:01 -08003785 final TaskRecord task = r.task;
3786 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003787 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003788 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003789 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003790 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003791 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003792 }
Craig Mautner299f9602015-01-26 09:47:33 -08003793 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003794 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003795 cleanUpActivityServicesLocked(r);
3796 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003797 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003798
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003799 /**
3800 * Perform clean-up of service connections in an activity record.
3801 */
3802 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3803 // Throw away any services that have been bound by this activity.
3804 if (r.connections != null) {
3805 Iterator<ConnectionRecord> it = r.connections.iterator();
3806 while (it.hasNext()) {
3807 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003808 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003809 }
3810 r.connections = null;
3811 }
3812 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003813
Craig Mautneree2e45a2014-06-27 12:10:03 -07003814 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003815 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003816 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003817 mHandler.sendMessage(msg);
3818 }
3819
Craig Mautneree2e45a2014-06-27 12:10:03 -07003820 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003821 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003822 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003823 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3824 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3825 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3826 final ActivityRecord r = activities.get(activityNdx);
3827 if (r.finishing) {
3828 continue;
3829 }
3830 if (r.fullscreen) {
3831 lastIsOpaque = true;
3832 }
3833 if (owner != null && r.app != owner) {
3834 continue;
3835 }
3836 if (!lastIsOpaque) {
3837 continue;
3838 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003839 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003840 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003841 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003842 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003843 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003844 activityRemoved = true;
3845 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003846 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003847 }
3848 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003849 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003850 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003851 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003852 }
3853
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003854 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3855 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003856 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3857 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003858 + " pausing=" + mPausingActivity + " for reason " + reason);
3859 return destroyActivityLocked(r, true, reason);
3860 }
3861 return false;
3862 }
3863
3864 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3865 String reason) {
3866 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003867 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003868 int maxTasks = tasks.size() / 4;
3869 if (maxTasks < 1) {
3870 maxTasks = 1;
3871 }
3872 int numReleased = 0;
3873 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3874 final TaskRecord task = mTaskHistory.get(taskNdx);
3875 if (!tasks.contains(task)) {
3876 continue;
3877 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003878 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003879 int curNum = 0;
3880 final ArrayList<ActivityRecord> activities = task.mActivities;
3881 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3882 final ActivityRecord activity = activities.get(actNdx);
3883 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003884 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003885 + " in state " + activity.state + " resumed=" + mResumedActivity
3886 + " pausing=" + mPausingActivity + " for reason " + reason);
3887 destroyActivityLocked(activity, true, reason);
3888 if (activities.get(actNdx) != activity) {
3889 // Was removed from list, back up so we don't miss the next one.
3890 actNdx--;
3891 }
3892 curNum++;
3893 }
3894 }
3895 if (curNum > 0) {
3896 numReleased += curNum;
3897 maxTasks--;
3898 if (mTaskHistory.get(taskNdx) != task) {
3899 // The entire task got removed, back up so we don't miss the next one.
3900 taskNdx--;
3901 }
3902 }
3903 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003904 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3905 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003906 return numReleased;
3907 }
3908
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003909 /**
3910 * Destroy the current CLIENT SIDE instance of an activity. This may be
3911 * called both when actually finishing an activity, or when performing
3912 * a configuration switch where we destroy the current client-side object
3913 * but then create a new client-side object for this same HistoryRecord.
3914 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003915 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003916 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3917 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003918 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003919 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003920 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003921 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003922
3923 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003924
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003925 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003926
3927 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003928
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003929 if (hadApp) {
3930 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003931 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003932 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3933 mService.mHeavyWeightProcess = null;
3934 mService.mHandler.sendEmptyMessage(
3935 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3936 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003937 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003938 // Update any services we are bound to that might care about whether
3939 // their client may have activities.
3940 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003941 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003942 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003943 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003944 }
3945 }
3946
3947 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003948
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003949 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003950 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003951 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003952 r.configChangeFlags);
3953 } catch (Exception e) {
3954 // We can just ignore exceptions here... if the process
3955 // has crashed, our death notification will clean things
3956 // up.
3957 //Slog.w(TAG, "Exception thrown during finish", e);
3958 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003959 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003960 removedFromHistory = true;
3961 skipDestroy = true;
3962 }
3963 }
3964
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003965 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003966
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003967 // If the activity is finishing, we need to wait on removing it
3968 // from the list to give it a chance to do its cleanup. During
3969 // that time it may make calls back with its token so we need to
3970 // be able to find it on the list and so we don't want to remove
3971 // it from the list yet. Otherwise, we can just immediately put
3972 // it in the destroyed state since we are not removing it from the
3973 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003974 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003975 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003976 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003977 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003978 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003979 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3980 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003981 if (DEBUG_STATES) Slog.v(TAG_STATES,
3982 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003983 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003984 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003985 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003986 }
3987 } else {
3988 // remove this record from the history.
3989 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003990 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003991 removedFromHistory = true;
3992 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003993 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003994 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003995 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003996 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003997 }
3998 }
3999
4000 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004001
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004002 if (!mLRUActivities.remove(r) && hadApp) {
4003 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
4004 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004005
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004006 return removedFromHistory;
4007 }
4008
Craig Mautner299f9602015-01-26 09:47:33 -08004009 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004010 final long origId = Binder.clearCallingIdentity();
4011 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004012 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08004013 if (r != null) {
4014 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004015 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004016 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08004017
Wale Ogunwale60454db2015-01-23 16:05:07 -08004018 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004019 if (r.state == ActivityState.DESTROYING) {
4020 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08004021 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08004022 }
4023 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004024 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08004025 } finally {
4026 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004027 }
4028 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004029
Todd Kennedyaab56db2015-01-30 09:39:53 -08004030 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07004031 if (hasVisibleBehindActivity() &&
4032 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08004033 if (r == topRunningActivityLocked()
4034 && getStackVisibilityLocked(null) == STACK_VISIBLE) {
Craig Mautner64ccb702014-10-01 09:38:40 -07004035 // Don't release the top activity if it has requested to run behind the next
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08004036 // activity and the stack is currently visible.
Craig Mautner64ccb702014-10-01 09:38:40 -07004037 return;
4038 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004039 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07004040 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07004041 " thread=" + r.app.thread);
4042 if (r != null && r.app != null && r.app.thread != null) {
4043 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07004044 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004045 } catch (RemoteException e) {
4046 }
Jose Lima4b6c6692014-08-12 17:41:12 -07004047 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004048 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07004049 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08004050 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004051 }
4052 }
4053 }
4054
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08004055 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07004056 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
4057 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004058 if (r != null) {
4059 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07004060 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08004061 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004062 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004063 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004064 }
4065
Jose Lima4b6c6692014-08-12 17:41:12 -07004066 boolean hasVisibleBehindActivity() {
4067 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004068 }
4069
Jose Lima4b6c6692014-08-12 17:41:12 -07004070 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07004071 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07004072 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004073 }
4074 }
4075
Jose Lima4b6c6692014-08-12 17:41:12 -07004076 ActivityRecord getVisibleBehindActivity() {
4077 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004078 }
4079
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004080 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
4081 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004082 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004083 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4084 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004085 while (i > 0) {
4086 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004087 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004088 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004089 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004090 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004091 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004092 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004093 }
4094 }
4095 }
4096
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004097 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
4098 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004099 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
4100 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07004101 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
4102 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004103 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004104 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07004105 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
4106 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004107
4108 boolean hasVisibleActivities = false;
4109
4110 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08004111 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004112 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4113 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08004114 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4115 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4116 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4117 final ActivityRecord r = activities.get(activityNdx);
4118 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004119 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4120 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08004121 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08004122 if (r.visible) {
4123 hasVisibleActivities = true;
4124 }
Craig Mautneracebdc82015-02-24 10:53:03 -08004125 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08004126 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
4127 // Don't currently have state for the activity, or
4128 // it is finishing -- always remove it.
4129 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004130 } else if (!r.visible && r.launchCount > 2 &&
4131 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08004132 // We have launched this activity too many times since it was
4133 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08004134 // (Note if the activity is visible, we don't remove the record.
4135 // We leave the dead window on the screen but the process will
4136 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08004137 remove = true;
4138 } else {
4139 // The process may be gone, but the activity lives on!
4140 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004141 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004142 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004143 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
4144 "Removing activity " + r + " from stack at " + i
4145 + ": haveState=" + r.haveState
4146 + " stateNotNeeded=" + r.stateNotNeeded
4147 + " finishing=" + r.finishing
4148 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08004149 if (!r.finishing) {
4150 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
4151 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
4152 r.userId, System.identityHashCode(r),
4153 r.task.taskId, r.shortComponentName,
4154 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07004155 if (r.state == ActivityState.RESUMED) {
4156 mService.updateUsageStats(r, false);
4157 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004158 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004159 } else {
4160 // We have the current state for this activity, so
4161 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004162 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
4163 if (DEBUG_APP) Slog.v(TAG_APP,
4164 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08004165 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004166 // Set nowVisible to previous visible state. If the app was visible while
4167 // it died, we leave the dead window on screen so it's basically visible.
4168 // This is needed when user later tap on the dead window, we need to stop
4169 // other apps when user transfers focus to the restarted activity.
4170 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08004171 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004172 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08004173 "App died, clearing saved state of " + r);
4174 r.icicle = null;
4175 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004176 }
Craig Mautnerd2328952013-03-05 12:46:26 -08004177 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08004178 if (remove) {
4179 removeActivityFromHistoryLocked(r, "appDied");
4180 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004181 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004182 }
4183 }
4184
4185 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004186 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004187
Chong Zhang280d3322015-11-03 17:27:26 -08004188 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004189 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004190 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004191 if (r != null && r.state != ActivityState.RESUMED) {
4192 r.updateOptionsLocked(options);
4193 } else {
4194 ActivityOptions.abort(options);
4195 }
4196 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004197 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004198 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004199
Craig Mautner21d24a22014-04-23 11:45:37 -07004200 void updateTaskMovement(TaskRecord task, boolean toFront) {
4201 if (task.isPersistable) {
4202 task.mLastTimeMoved = System.currentTimeMillis();
4203 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
4204 // recently will be most negative, tasks sent to the bottom before that will be less
4205 // negative. Similarly for recent tasks moved to the top which will be most positive.
4206 if (!toFront) {
4207 task.mLastTimeMoved *= -1;
4208 }
4209 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07004210 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07004211 }
4212
Craig Mautner84984fa2014-06-19 11:19:20 -07004213 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07004214 final int top = mTaskHistory.size() - 1;
4215 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
4216 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004217 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004218 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
4219 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07004220 mTaskHistory.remove(taskNdx);
4221 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004222 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07004223 return;
4224 }
4225 }
4226 }
4227
Chong Zhang280d3322015-11-03 17:27:26 -08004228 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004229 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004230 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004231
Craig Mautner11bf9a52013-02-19 14:08:51 -08004232 final int numTasks = mTaskHistory.size();
4233 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07004234 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004235 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004236 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004237 ActivityOptions.abort(options);
4238 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004239 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004240 }
4241 return;
4242 }
4243
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004244 if (timeTracker != null) {
4245 // The caller wants a time tracker associated with this task.
4246 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4247 tr.mActivities.get(i).appTimeTracker = timeTracker;
4248 }
4249 }
4250
Craig Mautner11bf9a52013-02-19 14:08:51 -08004251 // Shift all activities with this task up to the top
4252 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004253 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004254
Chong Zhang45c25ce2015-08-10 22:18:26 -07004255 // Don't refocus if invisible to current user
4256 ActivityRecord top = tr.getTopActivity();
4257 if (!okToShowLocked(top)) {
4258 addRecentActivityLocked(top);
4259 ActivityOptions.abort(options);
4260 return;
4261 }
4262
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004263 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004264 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004265 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004266
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004267 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004268 if (noAnimation) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004269 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004270 if (r != null) {
4271 mNoAnimActivities.add(r);
4272 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004273 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004274 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004275 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004276 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004277
Wale Ogunwaled046a012015-12-24 13:05:59 -08004278 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08004279 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004280
4281 if (VALIDATE_TOKENS) {
4282 validateAppTokensLocked();
4283 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004284 }
4285
4286 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004287 * Worker method for rearranging history stack. Implements the function of moving all
4288 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004289 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004290 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004291 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4292 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004293 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004294 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004295 * @return Returns true if the move completed, false if not.
4296 */
Craig Mautner299f9602015-01-26 09:47:33 -08004297 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004298 final TaskRecord tr = taskForIdLocked(taskId);
4299 if (tr == null) {
4300 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4301 return false;
4302 }
4303
4304 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07004305 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004306
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004307 // If we have a watcher, preflight the move before committing to it. First check
4308 // for *other* available tasks, but if none are available, then try again allowing the
4309 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07004310 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004311 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004312 if (next == null) {
4313 next = topRunningActivityLocked(null, 0);
4314 }
4315 if (next != null) {
4316 // ask watcher if this is allowed
4317 boolean moveOK = true;
4318 try {
4319 moveOK = mService.mController.activityResuming(next.packageName);
4320 } catch (RemoteException e) {
4321 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004322 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004323 }
4324 if (!moveOK) {
4325 return false;
4326 }
4327 }
4328 }
4329
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004330 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004331
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004332 if (mStackId == HOME_STACK_ID && topTask().isHomeTask()) {
4333 // For the case where we are moving the home task back and there is an activity visible
4334 // behind it on the fullscreen stack, we want to move the focus to the visible behind
4335 // activity to maintain order with what the user is seeing.
4336 final ActivityStack fullscreenStack =
4337 mStackSupervisor.getStack(FULLSCREEN_WORKSPACE_STACK_ID);
4338 if (fullscreenStack != null && fullscreenStack.hasVisibleBehindActivity()) {
4339 final ActivityRecord visibleBehind = fullscreenStack.getVisibleBehindActivity();
4340 mService.setFocusedActivityLocked(visibleBehind, "moveTaskToBack");
Wale Ogunwaled046a012015-12-24 13:05:59 -08004341 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004342 return true;
4343 }
4344 }
4345
riddle_hsuc215a4f2014-12-27 12:10:45 +08004346 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07004347
4348 // If true, we should resume the home activity next if the task we are moving to the
4349 // back is over the home stack. We force to false if the task we are moving to back
4350 // is the home task and we don't want it resumed after moving to the back.
4351 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
4352 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08004353 final TaskRecord nextTask = getNextTask(tr);
4354 if (nextTask != null) {
4355 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4356 } else {
4357 prevIsHome = true;
4358 }
4359 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004360 mTaskHistory.remove(tr);
4361 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004362 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004363
Craig Mautnerc8143c62013-09-03 12:15:57 -07004364 // There is an assumption that moving a task to the back moves it behind the home activity.
4365 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004366 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004367 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4368 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004369 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004370 break;
4371 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004372 if (taskNdx == 1) {
4373 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004374 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004375 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004376 }
4377
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004378 mWindowManager.prepareAppTransition(TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004379 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004380
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004381 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004382 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004383 }
4384
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004385 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004386 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004387 if (!mService.mBooting && !mService.mBooted) {
4388 // Not ready yet!
4389 return false;
4390 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004391 final int taskToReturnTo = tr.getTaskToReturnTo();
4392 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004393 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004394 }
4395
Wale Ogunwaled046a012015-12-24 13:05:59 -08004396 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004397 return true;
4398 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004399
Craig Mautner8849a5e2013-04-02 16:41:03 -07004400 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004401 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004402 final Uri data = r.intent.getData();
4403 final String strData = data != null ? data.toSafeString() : null;
4404
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004405 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004406 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004407 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004408 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004409 }
4410
4411 /**
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004412 * Ensures all visible activities at or below the input activity have the right configuration.
4413 */
4414 void ensureVisibleActivitiesConfigurationLocked(ActivityRecord start, boolean preserveWindow) {
4415 if (start == null || !start.visible) {
4416 return;
4417 }
4418
4419 final TaskRecord startTask = start.task;
4420 boolean behindFullscreen = false;
4421 boolean updatedConfig = false;
4422
4423 for (int taskIndex = mTaskHistory.indexOf(startTask); taskIndex >= 0; --taskIndex) {
4424 final TaskRecord task = mTaskHistory.get(taskIndex);
4425 final ArrayList<ActivityRecord> activities = task.mActivities;
4426 int activityIndex =
4427 (start.task == task) ? activities.indexOf(start) : activities.size() - 1;
4428 for (; activityIndex >= 0; --activityIndex) {
4429 final ActivityRecord r = activities.get(activityIndex);
4430 updatedConfig |= ensureActivityConfigurationLocked(r, 0, preserveWindow);
4431 if (r.fullscreen) {
4432 behindFullscreen = true;
4433 break;
4434 }
4435 }
4436 if (behindFullscreen) {
4437 break;
4438 }
4439 }
4440 if (updatedConfig) {
4441 // Ensure the resumed state of the focus activity if we updated the confiugaration of
4442 // any activity.
4443 mStackSupervisor.resumeFocusedStackTopActivityLocked();
4444 }
4445 }
4446
4447 /**
4448 * Make sure the given activity matches the current configuration. Returns false if the activity
4449 * had to be destroyed. Returns true if the configuration is the same, or the activity will
4450 * remain running as-is for whatever reason. Ensures the HistoryRecord is updated with the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004451 * correct configuration and all other bookkeeping is handled.
4452 */
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004453 boolean ensureActivityConfigurationLocked(
4454 ActivityRecord r, int globalChanges, boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004455 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004456 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004457 "Skipping config check (will change): " + r);
4458 return true;
4459 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004460
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004461 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004462 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004463
Wale Ogunwalef7dab1e2016-04-16 11:38:50 -07004464 // Short circuit: if the two configurations are equal (the common case), then there is
4465 // nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004466 final Configuration newConfig = mService.mConfiguration;
4467 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwalef7dab1e2016-04-16 11:38:50 -07004468 if (r.configuration.equals(newConfig)
4469 && r.taskConfigOverride.equals(taskConfig)
Wale Ogunwale60454db2015-01-23 16:05:07 -08004470 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004471 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004472 "Configuration unchanged in " + r);
4473 return true;
4474 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004475
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004476 // We don't worry about activities that are finishing.
4477 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004478 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004479 "Configuration doesn't matter in finishing " + r);
4480 r.stopFreezingScreenLocked(false);
4481 return true;
4482 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004483
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004484 // Okay we now are going to make this activity have the new config.
4485 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004486 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004487 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004488 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004489 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004490
Filip Gruszczynski23493322015-07-29 17:02:59 -07004491 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004492 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004493 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004494 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004495 "Configuration no differences in " + r);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004496 // There are no significant differences, so we won't relaunch but should still deliver
4497 // the new configuration to the client process.
Wale Ogunwalef7dab1e2016-04-16 11:38:50 -07004498 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004499 return true;
4500 }
4501
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004502 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4503 "Configuration changes for " + r + " ; taskChanges="
4504 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4505 + Configuration.configurationDiffToString(changes));
4506
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004507 // If the activity isn't currently running, just leave the new
4508 // configuration and it will pick that up next time it starts.
4509 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004510 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004511 "Configuration doesn't matter not running " + r);
4512 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004513 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004514 return true;
4515 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004516
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004517 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004518 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4519 "Checking to restart " + r.info.name + ": changed=0x"
4520 + Integer.toHexString(changes) + ", handles=0x"
Filip Gruszczynskica664812015-12-04 12:43:36 -08004521 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4522 + ", taskConfig=" + taskConfig);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004523
Dianne Hackborne6676352011-06-01 16:51:20 -07004524 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004525 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4526 r.configChangeFlags |= changes;
4527 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004528 r.forceNewConfig = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004529 preserveWindow &= isResizeOnlyChange(changes);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004530 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004531 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004532 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004533 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004534 } else if (r.state == ActivityState.PAUSING) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004535 // A little annoying: we are waiting for this activity to finish pausing. Let's not
4536 // do anything now, but just flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004537 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004538 "Config is skipping already pausing " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004539 r.deferRelaunchUntilPaused = true;
4540 r.preserveWindowOnDeferredRelaunch = preserveWindow;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004541 return true;
4542 } else if (r.state == ActivityState.RESUMED) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004543 // Try to optimize this case: the configuration is changing and we need to restart
4544 // the top, resumed activity. Instead of doing the normal handshaking, just say
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004545 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004546 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004547 "Config is relaunching resumed " + r);
Chong Zhang2b79af12016-02-10 18:47:06 -08004548
4549 if (DEBUG_STATES && !r.visible) {
4550 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + r
4551 + " called by " + Debug.getCallers(4));
4552 }
4553
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004554 relaunchActivityLocked(r, r.configChangeFlags, true, preserveWindow);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004555 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004556 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004557 "Config is relaunching non-resumed " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004558 relaunchActivityLocked(r, r.configChangeFlags, false, preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004559 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004560
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004561 // All done... tell the caller we weren't able to keep this activity around.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004562 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004563 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004564
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004565 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004566 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004567 // changes is always sent to all processes when they happen so it can just use whatever
4568 // system level configuration it last got.
Filip Gruszczynskica664812015-12-04 12:43:36 -08004569 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004570 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004571
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004572 return true;
4573 }
4574
Filip Gruszczynski23493322015-07-29 17:02:59 -07004575 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4576 Configuration oldTaskOverride) {
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08004577
4578 // If we went from full-screen to non-full-screen, make sure to use the correct
4579 // configuration task diff, so the diff stays as small as possible.
4580 if (Configuration.EMPTY.equals(oldTaskOverride)
4581 && !Configuration.EMPTY.equals(taskConfig)) {
4582 oldTaskOverride = record.task.extractOverrideConfig(record.configuration);
4583 }
4584
Jorim Jaggi1b025a62016-02-03 19:27:49 -08004585 // Conversely, do the same when going the other direction.
4586 if (Configuration.EMPTY.equals(taskConfig)
4587 && !Configuration.EMPTY.equals(oldTaskOverride)) {
4588 taskConfig = record.task.extractOverrideConfig(record.configuration);
4589 }
4590
Filip Gruszczynski23493322015-07-29 17:02:59 -07004591 // Determine what has changed. May be nothing, if this is a config
4592 // that has come back from the app after going idle. In that case
4593 // we just want to leave the official config object now in the
4594 // activity and do nothing else.
4595 int taskChanges = oldTaskOverride.diff(taskConfig);
4596 // We don't want to use size changes if they don't cross boundaries that are important to
4597 // the app.
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004598 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004599 final boolean crosses = record.crossesHorizontalSizeThreshold(
4600 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4601 || record.crossesVerticalSizeThreshold(
4602 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4603 if (!crosses) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004604 taskChanges &= ~CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004605 }
4606 }
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004607 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004608 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4609 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004610 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004611 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004612 }
4613 }
4614 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4615 }
4616
4617 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4618 Configuration oldTaskOverride, int taskChanges) {
4619 if (taskChanges == 0) {
4620 // {@link Configuration#diff} doesn't catch changes from unset values.
4621 // Check for changes we care about.
4622 if (oldTaskOverride.orientation != taskConfig.orientation) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004623 taskChanges |= CONFIG_ORIENTATION;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004624 }
4625 // We want to explicitly track situations where the size configuration goes from
4626 // undefined to defined. We don't care about crossing the threshold in that case,
4627 // because there is no threshold.
4628 final int oldHeight = oldTaskOverride.screenHeightDp;
4629 final int newHeight = taskConfig.screenHeightDp;
4630 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4631 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4632 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004633 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004634 }
4635 final int oldWidth = oldTaskOverride.screenWidthDp;
4636 final int newWidth = taskConfig.screenWidthDp;
4637 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4638 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4639 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004640 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004641 }
4642 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4643 final int newSmallest = taskConfig.smallestScreenWidthDp;
4644 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4645 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4646 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004647 taskChanges |= CONFIG_SMALLEST_SCREEN_SIZE;
4648 }
4649 final int oldLayout = oldTaskOverride.screenLayout;
4650 final int newLayout = taskConfig.screenLayout;
4651 if ((oldLayout == SCREENLAYOUT_UNDEFINED && newLayout != SCREENLAYOUT_UNDEFINED)
4652 || (oldLayout != SCREENLAYOUT_UNDEFINED && newLayout == SCREENLAYOUT_UNDEFINED)) {
4653 taskChanges |= CONFIG_SCREEN_LAYOUT;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004654 }
4655 }
4656 return taskChanges;
4657 }
4658
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004659 private static boolean isResizeOnlyChange(int change) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004660 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
4661 | CONFIG_SCREEN_LAYOUT)) == 0;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004662 }
4663
Wale Ogunwale83301a92015-09-24 15:54:08 -07004664 private void relaunchActivityLocked(
4665 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4666 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004667 r.configChangeFlags = 0;
Wale Ogunwale83301a92015-09-24 15:54:08 -07004668 return;
4669 }
4670
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004671 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004672 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004673 if (andResume) {
4674 results = r.results;
4675 newIntents = r.newIntents;
4676 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004677 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4678 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004679 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004680 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004681 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004682 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004683
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004684 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004685
Craig Mautner34b73df2014-01-12 21:11:08 -08004686 mStackSupervisor.removeChildActivityContainers(r);
4687
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004688 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004689 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08004690 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r
4691 + " callers=" + Debug.getCallers(6));
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004692 r.forceNewConfig = false;
Chong Zhangd78ddb42016-03-02 17:01:14 -08004693 mStackSupervisor.activityRelaunchingLocked(r);
Wale Ogunwale60454db2015-01-23 16:05:07 -08004694 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4695 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004696 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004697 // Note: don't need to call pauseIfSleepingLocked() here, because
4698 // the caller will only pass in 'andResume' if this activity is
4699 // currently resumed, which implies we aren't sleeping.
4700 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004701 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004702 }
4703
4704 if (andResume) {
Chong Zhang2b79af12016-02-10 18:47:06 -08004705 if (DEBUG_STATES) {
4706 Slog.d(TAG_STATES, "Resumed after relaunch " + r);
4707 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004708 r.state = ActivityState.RESUMED;
Chong Zhang2b79af12016-02-10 18:47:06 -08004709 // Relaunch-resume could happen either when the app is already in the front,
4710 // or while it's being brought to front. In the latter case, it's marked RESUMED
4711 // but not yet visible (or stopped). We need to complete the resume here as the
4712 // code in resumeTopActivityInnerLocked to complete the resume might be skipped.
4713 if (!r.visible || r.stopped) {
4714 mWindowManager.setAppVisibility(r.appToken, true);
4715 completeResumeLocked(r);
4716 } else {
4717 r.results = null;
4718 r.newIntents = null;
4719 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004720 } else {
4721 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4722 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004723 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004724
4725 r.configChangeFlags = 0;
Chong Zhang225d1b62016-03-31 14:09:33 -07004726 r.deferRelaunchUntilPaused = false;
4727 r.preserveWindowOnDeferredRelaunch = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004728 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004729
4730 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004731 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4732 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4733 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4734 final ActivityRecord r = activities.get(activityNdx);
4735 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004736 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004737 }
4738 if (r.fullscreen && !r.finishing) {
4739 return false;
4740 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004741 }
4742 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004743 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004744 if (r == null) {
4745 return false;
4746 }
4747 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4748 + " would have returned true for r=" + r);
4749 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004750 }
4751
4752 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004753 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4754 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4755 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4756 final ActivityRecord r = activities.get(activityNdx);
4757 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004758 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004759 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004760 }
4761 }
4762 }
4763
Wale Ogunwale540e1232015-05-01 15:35:39 -07004764 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4765 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004766 boolean didSomething = false;
4767 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004768 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004769 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4770 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4771 int numActivities = activities.size();
4772 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4773 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004774 final boolean sameComponent =
4775 (r.packageName.equals(packageName) && (filterByClasses == null
4776 || filterByClasses.contains(r.realActivity.getClassName())))
4777 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004778 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004779 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004780 && (r.app == null || evenPersistent || !r.app.persistent)) {
4781 if (!doit) {
4782 if (r.finishing) {
4783 // If this activity is just finishing, then it is not
4784 // interesting as far as something to stop.
4785 continue;
4786 }
4787 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004788 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004789 if (r.isHomeActivity()) {
4790 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4791 Slog.i(TAG, "Skip force-stop again " + r);
4792 continue;
4793 } else {
4794 homeActivity = r.realActivity;
4795 }
4796 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004797 didSomething = true;
4798 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004799 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004800 if (r.app != null) {
4801 r.app.removed = true;
4802 }
4803 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004804 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004805 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004806 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4807 true)) {
4808 // r has been deleted from mActivities, accommodate.
4809 --numActivities;
4810 --activityNdx;
4811 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004812 }
4813 }
4814 }
4815 return didSomething;
4816 }
4817
Dianne Hackborn09233282014-04-30 11:33:59 -07004818 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004819 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4820 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004821 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004822 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004823 if (task.getTopActivity() == null) {
4824 continue;
4825 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004826 ActivityRecord r = null;
4827 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004828 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004829 int numActivities = 0;
4830 int numRunning = 0;
4831 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004832 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004833 continue;
4834 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004835 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004836 tmp = activities.get(activityNdx);
4837 if (tmp.finishing) {
4838 continue;
4839 }
4840 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004841
Craig Mautneraab647e2013-02-28 16:31:36 -08004842 // Initialize state for next task if needed.
4843 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4844 top = r;
4845 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004846 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004847
4848 // Add 'r' into the current task.
4849 numActivities++;
4850 if (r.app != null && r.app.thread != null) {
4851 numRunning++;
4852 }
4853
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004854 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004855 TAG, r.intent.getComponent().flattenToShortString()
4856 + ": task=" + r.task);
4857 }
4858
4859 RunningTaskInfo ci = new RunningTaskInfo();
4860 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004861 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004862 ci.baseActivity = r.intent.getComponent();
4863 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004864 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004865 if (focusedStack && topTask) {
4866 // Give the latest time to ensure foreground task can be sorted
4867 // at the first, because lastActiveTime of creating task is 0.
4868 ci.lastActiveTime = System.currentTimeMillis();
4869 topTask = false;
4870 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004871
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004872 if (top.task != null) {
4873 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004874 }
4875 ci.numActivities = numActivities;
4876 ci.numRunning = numRunning;
Wale Ogunwale21b60582016-01-27 12:34:16 -08004877 ci.isDockable = task.canGoInDockedStack();
Jorim Jaggi29379ec2016-04-11 23:43:42 -07004878 ci.resizeMode = task.mResizeMode;
Craig Mautneraab647e2013-02-28 16:31:36 -08004879 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004880 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004881 }
4882
4883 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004884 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004885 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004886 if (top >= 0) {
4887 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4888 int activityTop = activities.size() - 1;
4889 if (activityTop > 0) {
4890 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4891 "unhandled-back", true);
4892 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004893 }
4894 }
4895
Craig Mautner6b74cb52013-09-27 17:02:21 -07004896 /**
4897 * Reset local parameters because an app's activity died.
4898 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004899 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004900 */
4901 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004902 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004903 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004904 "App died while pausing: " + mPausingActivity);
4905 mPausingActivity = null;
4906 }
4907 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4908 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004909 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004910 }
4911
Craig Mautner19091252013-10-05 00:03:53 -07004912 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004913 }
4914
Craig Mautnercae015f2013-02-08 14:31:27 -08004915 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004916 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4917 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4918 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4919 final ActivityRecord r = activities.get(activityNdx);
4920 if (r.app == app) {
4921 Slog.w(TAG, " Force finishing activity "
4922 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004923 // Force the destroy to skip right to removal.
4924 r.app = null;
4925 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004926 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004927 }
4928 }
4929 }
4930
Dianne Hackborn390517b2013-05-30 15:03:32 -07004931 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004932 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004933 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004934 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4935 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004936 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4937 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004938 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004939 " Task id #" + task.taskId + "\n" +
4940 " mFullscreen=" + task.mFullscreen + "\n" +
4941 " mBounds=" + task.mBounds + "\n" +
Andrii Kulianf66a83d2016-05-17 12:17:44 -07004942 " mMinWidth=" + task.mMinWidth + "\n" +
4943 " mMinHeight=" + task.mMinHeight + "\n" +
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004944 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004945 if (printed) {
4946 header = null;
4947 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004948 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004949 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004950 }
4951
4952 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4953 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4954
4955 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004956 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4957 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004958 }
4959 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004960 final int top = mTaskHistory.size() - 1;
4961 if (top >= 0) {
4962 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4963 int listTop = list.size() - 1;
4964 if (listTop >= 0) {
4965 activities.add(list.get(listTop));
4966 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004967 }
4968 } else {
4969 ItemMatcher matcher = new ItemMatcher();
4970 matcher.build(name);
4971
Craig Mautneraab647e2013-02-28 16:31:36 -08004972 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4973 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4974 if (matcher.match(r1, r1.intent.getComponent())) {
4975 activities.add(r1);
4976 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004977 }
4978 }
4979 }
4980
4981 return activities;
4982 }
4983
4984 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004985 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004986
4987 // All activities that came from the package must be
4988 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004989 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4990 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4991 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4992 final ActivityRecord a = activities.get(activityNdx);
4993 if (a.info.packageName.equals(packageName)) {
4994 a.forceNewConfig = true;
4995 if (starting != null && a == starting && a.visible) {
4996 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004997 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08004998 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004999 }
5000 }
5001 }
5002
5003 return starting;
5004 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08005005
Craig Mautner299f9602015-01-26 09:47:33 -08005006 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07005007 removeTask(task, reason, REMOVE_TASK_MODE_DESTROYING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08005008 }
5009
Wale Ogunwale000957c2015-04-03 08:19:12 -07005010 /**
5011 * Removes the input task from this stack.
5012 * @param task to remove.
5013 * @param reason for removal.
Wale Ogunwale06579d62016-04-30 15:29:06 -07005014 * @param mode task removal mode. Either {@link #REMOVE_TASK_MODE_DESTROYING},
5015 * {@link #REMOVE_TASK_MODE_MOVING}, {@link #REMOVE_TASK_MODE_MOVING_TO_TOP}.
Wale Ogunwale000957c2015-04-03 08:19:12 -07005016 */
Wale Ogunwale06579d62016-04-30 15:29:06 -07005017 void removeTask(TaskRecord task, String reason, int mode) {
5018 if (mode == REMOVE_TASK_MODE_DESTROYING) {
Craig Mautner15df08a2015-04-01 12:17:18 -07005019 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08005020 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08005021 if (!StackId.persistTaskBounds(mStackId)) {
5022 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
5023 // default configuration the next time it launches.
5024 task.updateOverrideConfiguration(null);
5025 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08005026 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07005027
Craig Mautner04a0ea62014-01-13 12:51:26 -08005028 final ActivityRecord r = mResumedActivity;
5029 if (r != null && r.task == task) {
5030 mResumedActivity = null;
5031 }
5032
Craig Mautnerae7ecab2013-09-18 11:48:14 -07005033 final int taskNdx = mTaskHistory.indexOf(task);
5034 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07005035 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
5036 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
5037 if (!nextTask.isOverHomeStack()) {
5038 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
5039 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07005040 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07005041 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07005042 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07005043
Wale Ogunwale06579d62016-04-30 15:29:06 -07005044 if (mode == REMOVE_TASK_MODE_DESTROYING && task.mActivities.isEmpty()) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08005045 // TODO: VI what about activity?
Craig Mautner41db4a72014-05-07 17:20:56 -07005046 final boolean isVoiceSession = task.voiceSession != null;
5047 if (isVoiceSession) {
5048 try {
5049 task.voiceSession.taskFinished(task.intent, task.taskId);
5050 } catch (RemoteException e) {
5051 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07005052 }
Craig Mautner41db4a72014-05-07 17:20:56 -07005053 if (task.autoRemoveFromRecents() || isVoiceSession) {
5054 // Task creator asked to remove this when done, or this task was a voice
5055 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08005056 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08005057 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07005058 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07005059 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005060
5061 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005062 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Wale Ogunwale06579d62016-04-30 15:29:06 -07005063 // We only need to adjust focused stack if this stack is in focus and we are not in the
5064 // process of moving the task to the top of the stack that will be focused.
5065 if (isOnHomeDisplay() && mode != REMOVE_TASK_MODE_MOVING_TO_TOP
5066 && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08005067 String myReason = reason + " leftTaskHistoryEmpty";
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07005068 if (mFullscreen
5069 || !adjustFocusToNextFocusableStackLocked(
5070 task.getTaskToReturnTo(), myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07005071 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08005072 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005073 }
Craig Mautner593a4e62014-01-15 17:55:51 -08005074 if (mStacks != null) {
5075 mStacks.remove(this);
5076 mStacks.add(0, this);
5077 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07005078 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08005079 mActivityContainer.onTaskListEmptyLocked();
5080 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005081 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07005082
5083 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08005084 }
5085
Dianne Hackborn91097de2014-04-04 18:02:06 -07005086 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
5087 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
5088 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07005089 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
5090 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07005091 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08005092 addTask(task, toTop, "createTaskRecord");
Chong Zhang75b37202015-12-04 14:16:36 -08005093 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
Andrii Kulian2e751b82016-03-16 16:59:32 -07005094 if (!layoutTaskInStack(task, info.windowLayout) && mBounds != null && task.isResizeable()
Chong Zhang75b37202015-12-04 14:16:36 -08005095 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07005096 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07005097 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08005098 return task;
5099 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08005100
Andrii Kulian2e751b82016-03-16 16:59:32 -07005101 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.WindowLayout windowLayout) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08005102 if (mTaskPositioner == null) {
5103 return false;
5104 }
Andrii Kulian2e751b82016-03-16 16:59:32 -07005105 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, windowLayout);
Wale Ogunwale935e5022015-11-10 12:36:10 -08005106 return true;
5107 }
5108
Craig Mautnerc00204b2013-03-05 15:02:14 -08005109 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08005110 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005111 }
5112
Wale Ogunwale5f986092015-12-04 15:35:38 -08005113 void addTask(final TaskRecord task, final boolean toTop, String reason) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07005114 final ActivityStack prevStack = preAddTask(task, reason, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005115
Craig Mautnerc00204b2013-03-05 15:02:14 -08005116 task.stack = this;
5117 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07005118 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005119 } else {
5120 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07005121 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005122 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08005123 postAddTask(task, prevStack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005124 }
5125
Wale Ogunwale5f986092015-12-04 15:35:38 -08005126 void positionTask(final TaskRecord task, int position) {
Jorim Jaggi023da532016-04-20 22:42:32 -07005127 final ActivityRecord topRunningActivity = task.topRunningActivityLocked();
5128 final boolean wasResumed = topRunningActivity == task.stack.mResumedActivity;
Wale Ogunwaleed811d72016-05-06 08:41:19 -07005129 final ActivityStack prevStack = preAddTask(task, "positionTask", !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005130 task.stack = this;
5131 insertTaskAtPosition(task, position);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005132 postAddTask(task, prevStack);
Jorim Jaggi023da532016-04-20 22:42:32 -07005133 if (wasResumed) {
5134 if (mResumedActivity != null) {
5135 Log.wtf(TAG, "mResumedActivity was already set when moving mResumedActivity from"
5136 + " other stack to this stack mResumedActivity=" + mResumedActivity
5137 + " other mResumedActivity=" + topRunningActivity);
5138 }
5139 mResumedActivity = topRunningActivity;
5140 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08005141 }
5142
Wale Ogunwale06579d62016-04-30 15:29:06 -07005143 private ActivityStack preAddTask(TaskRecord task, String reason, boolean toTop) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08005144 final ActivityStack prevStack = task.stack;
5145 if (prevStack != null && prevStack != this) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07005146 prevStack.removeTask(task, reason,
5147 toTop ? REMOVE_TASK_MODE_MOVING_TO_TOP : REMOVE_TASK_MODE_MOVING);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005148 }
5149 return prevStack;
5150 }
5151
5152 private void postAddTask(TaskRecord task, ActivityStack prevStack) {
5153 if (prevStack != null) {
Andrii Kulian933076d2016-03-29 17:04:42 -07005154 mStackSupervisor.scheduleReportPictureInPictureModeChangedIfNeeded(task, prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005155 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005156 try {
5157 task.voiceSession.taskStarted(task.intent, task.taskId);
5158 } catch (RemoteException e) {
5159 }
5160 }
5161 }
5162
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07005163 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08005164 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08005165 // TODO: VI deal with activity
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07005166 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
5167 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6cae7652015-12-26 07:36:26 -08005168 (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId, r.info.configChanges,
5169 task.voiceSession != null, r.mLaunchTaskBehind, bounds, task.mOverrideConfig,
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08005170 task.mResizeMode, r.isAlwaysFocusable(), task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07005171 r.taskConfigOverride = task.mOverrideConfig;
5172 }
5173
Wale Ogunwaled046a012015-12-24 13:05:59 -08005174 void moveToFrontAndResumeStateIfNeeded(
5175 ActivityRecord r, boolean moveToFront, boolean setResume, String reason) {
5176 if (!moveToFront) {
5177 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07005178 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08005179
5180 // If the activity owns the last resumed activity, transfer that together,
5181 // so that we don't resume the same activity again in the new stack.
5182 // Apps may depend on onResume()/onPause() being called in pairs.
5183 if (setResume) {
5184 mResumedActivity = r;
5185 }
5186 // Move the stack in which we are placing the activity to the front. The call will also
5187 // make sure the activity focus is set.
5188 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07005189 }
5190
5191 /**
5192 * Moves the input activity from its current stack to this one.
5193 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
5194 * created on this stack which the activity is added to.
5195 * */
5196 void moveActivityToStack(ActivityRecord r) {
5197 final ActivityStack prevStack = r.task.stack;
5198 if (prevStack.mStackId == mStackId) {
5199 // You are already in the right stack silly...
5200 return;
5201 }
5202
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07005203 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07005204 && (mStackSupervisor.topRunningActivityLocked() == r);
5205 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
5206
5207 final TaskRecord task = createTaskRecord(
Suprabh Shukla09a88f52015-12-02 14:36:31 -08005208 mStackSupervisor.getNextTaskIdForUserLocked(r.userId),
5209 r.info, r.intent, null, null, true);
Wale Ogunwale079a0042015-10-24 11:44:07 -07005210 r.setTask(task, null);
5211 task.addActivityToTop(r);
5212 setAppTask(r, task);
Andrii Kulian933076d2016-03-29 17:04:42 -07005213 mStackSupervisor.scheduleReportPictureInPictureModeChangedIfNeeded(task, prevStack);
Wale Ogunwaled046a012015-12-24 13:05:59 -08005214 moveToFrontAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
5215 if (wasResumed) {
5216 prevStack.mResumedActivity = null;
5217 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07005218 }
5219
Wale Ogunwale706ed792015-08-02 10:29:44 -07005220 private void setAppTask(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08005221 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08005222 mWindowManager.setAppTask(r.appToken, task.taskId, mStackId, bounds, task.mOverrideConfig,
5223 task.mResizeMode, task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07005224 r.taskConfigOverride = task.mOverrideConfig;
5225 }
5226
Craig Mautnerc00204b2013-03-05 15:02:14 -08005227 public int getStackId() {
5228 return mStackId;
5229 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07005230
5231 @Override
5232 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07005233 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
5234 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07005235 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08005236
Craig Mautner15df08a2015-04-01 12:17:18 -07005237 void onLockTaskPackagesUpdatedLocked() {
5238 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5239 mTaskHistory.get(taskNdx).setLockTaskAuth();
5240 }
5241 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005242}