blob: a27adf7fcf1613252b6b9cc454c87cf3d00c7a56 [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 Ogunwale06579d62016-04-30 15:29:06 -070076import static com.android.server.am.ActivityStackSupervisor.ON_TOP;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070077import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070078import static com.android.server.wm.AppTransition.TRANSIT_ACTIVITY_CLOSE;
79import static com.android.server.wm.AppTransition.TRANSIT_ACTIVITY_OPEN;
80import static com.android.server.wm.AppTransition.TRANSIT_NONE;
81import static com.android.server.wm.AppTransition.TRANSIT_TASK_CLOSE;
82import static com.android.server.wm.AppTransition.TRANSIT_TASK_OPEN;
83import static com.android.server.wm.AppTransition.TRANSIT_TASK_OPEN_BEHIND;
84import static com.android.server.wm.AppTransition.TRANSIT_TASK_TO_BACK;
85import static com.android.server.wm.AppTransition.TRANSIT_TASK_TO_FRONT;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070086
87import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070088import android.app.ActivityManager;
Jorim Jaggiaf80db42016-04-07 19:19:15 -070089import android.app.ActivityManager.RunningTaskInfo;
Wale Ogunwale3797c222015-10-27 14:21:58 -070090import android.app.ActivityManager.StackId;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070091import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070092import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080093import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070094import android.app.ResultInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070095import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070096import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070097import android.content.pm.ActivityInfo;
Todd Kennedy39bfee52016-02-24 10:28:21 -080098import android.content.pm.ApplicationInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070099import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800100import android.graphics.Bitmap;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700101import android.graphics.Point;
102import android.graphics.Rect;
Santos Cordon73ff7d82013-03-06 17:24:11 -0800103import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700104import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700105import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -0800106import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700107import android.os.Handler;
108import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +0900109import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700110import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -0700111import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700112import android.os.RemoteException;
113import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -0800114import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700115import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -0700116import android.service.voice.IVoiceInteractionSession;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700117import android.util.ArraySet;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700118import android.util.EventLog;
Jorim Jaggi023da532016-04-20 22:42:32 -0700119import android.util.Log;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700120import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -0700121import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700122
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700123import com.android.internal.app.IVoiceInteractor;
124import com.android.internal.content.ReferrerIntent;
125import com.android.internal.os.BatteryStatsImpl;
126import com.android.server.Watchdog;
127import com.android.server.am.ActivityManagerService.ItemMatcher;
128import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
129import com.android.server.wm.TaskGroup;
130import com.android.server.wm.WindowManagerService;
131
Craig Mautnercae015f2013-02-08 14:31:27 -0800132import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -0800133import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700134import java.lang.ref.WeakReference;
135import java.util.ArrayList;
136import java.util.Iterator;
137import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -0800138import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700139import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700140
141/**
142 * State and management of a single stack of activities.
143 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700144final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800145
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800146 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700147 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
148 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800149 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700150 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700151 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700152 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700153 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700154 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700155 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
156 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700157 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700158 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700159 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
160 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
161 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
162 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
163 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700164
165 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800166
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700167 // Ticks during which we check progress while waiting for an app to launch.
168 static final int LAUNCH_TICK = 500;
169
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700170 // How long we wait until giving up on the last activity to pause. This
171 // is short because it directly impacts the responsiveness of starting the
172 // next activity.
173 static final int PAUSE_TIMEOUT = 500;
174
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700175 // How long we wait for the activity to tell us it has stopped before
176 // giving up. This is a good amount of time because we really need this
177 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700178 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700179
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700180 // How long we wait until giving up on an activity telling us it has
181 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700182 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800183
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700184 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800185 // disabled.
186 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800187
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700188 // How long between activity launches that we consider safe to not warn
189 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700190 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800191
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700192 // Set to false to disable the preview that is shown while a new activity
193 // is being started.
194 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800195
Craig Mautner5eda9b32013-07-02 11:58:16 -0700196 // How long to wait for all background Activities to redraw following a call to
197 // convertToTranslucent().
198 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
199
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -0800200 // How many activities have to be scheduled to stop to force a stop pass.
201 private static final int MAX_STOPPING_TO_FORCE = 3;
202
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700203 enum ActivityState {
204 INITIALIZING,
205 RESUMED,
206 PAUSING,
207 PAUSED,
208 STOPPING,
209 STOPPED,
210 FINISHING,
211 DESTROYING,
212 DESTROYED
213 }
214
Filip Gruszczynski0e381e22016-01-14 16:31:33 -0800215 // Stack is not considered visible.
216 static final int STACK_INVISIBLE = 0;
217 // Stack is considered visible
218 static final int STACK_VISIBLE = 1;
219 // Stack is considered visible, but only becuase it has activity that is visible behind other
220 // activities and there is a specific combination of stacks.
221 static final int STACK_VISIBLE_ACTIVITY_BEHIND = 2;
222
Wale Ogunwale06579d62016-04-30 15:29:06 -0700223 /* The various modes for the method {@link #removeTask}. */
224 // Task is being completely removed from all stacks in the system.
225 static final int REMOVE_TASK_MODE_DESTROYING = 0;
226 // Task is being removed from this stack so we can add it to another stack. In the case we are
227 // moving we don't want to perform some operations on the task like removing it from window
228 // manager or recents.
229 static final int REMOVE_TASK_MODE_MOVING = 1;
230 // Similar to {@link #REMOVE_TASK_MODE_MOVING} and the task will be added to the top of its new
231 // stack and the new stack will be on top of all stacks.
232 static final int REMOVE_TASK_MODE_MOVING_TO_TOP = 2;
233
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700234 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700235 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800236 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800237
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700238 /**
239 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800240 * running) activities. It contains #TaskRecord objects.
241 */
Todd Kennedy39bfee52016-02-24 10:28:21 -0800242 private final ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800243
244 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800245 * Used for validating app tokens with window manager.
246 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800247 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800248
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700249 /**
250 * List of running activities, sorted by recent usage.
251 * The first entry in the list is the least recently used.
252 * It contains HistoryRecord objects.
253 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800254 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700255
256 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700257 * Animations that for the current transition have requested not to
258 * be considered for the transition animation.
259 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800260 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700261
262 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700263 * When we are in the process of pausing an activity, before starting the
264 * next one, this variable holds the activity that is currently being paused.
265 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800266 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700267
268 /**
269 * This is the last activity that we put into the paused state. This is
270 * used to determine if we need to do an activity transition while sleeping,
271 * when we normally hold the top activity paused.
272 */
273 ActivityRecord mLastPausedActivity = null;
274
275 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700276 * Activities that specify No History must be removed once the user navigates away from them.
277 * If the device goes to sleep with such an activity in the paused state then we save it here
278 * and finish it later if another activity replaces it on wakeup.
279 */
280 ActivityRecord mLastNoHistoryActivity = null;
281
282 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700283 * Current activity that is resumed, or null if there is none.
284 */
285 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800286
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700287 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700288 * This is the last activity that has been started. It is only used to
289 * identify when multiple activities are started at once so that the user
290 * can be warned they may not be in the activity they think they are.
291 */
292 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800293
Craig Mautner5eda9b32013-07-02 11:58:16 -0700294 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
295 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
296 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
297 // Activity in mTranslucentActivityWaiting is notified via
298 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
299 // background activity being drawn then the same call will be made with a true value.
300 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700301 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700302
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700303 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700304 * Set when we know we are going to be calling updateConfiguration()
305 * soon, so want to skip intermediate config checks.
306 */
307 boolean mConfigWillChange;
308
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700309 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800310 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700311 // Current bounds of the stack or null if fullscreen.
312 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800313
Jorim Jaggi192086e2016-03-11 17:17:03 +0100314 boolean mUpdateBoundsDeferred;
315 boolean mUpdateBoundsDeferredCalled;
316 final Rect mDeferredBounds = new Rect();
317 final Rect mDeferredTaskBounds = new Rect();
318 final Rect mDeferredTaskInsetBounds = new Rect();
319
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700320 long mLaunchStartTime = 0;
321 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800322
Craig Mautner858d8a62013-04-23 17:08:34 -0700323 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700324
Craig Mautnerc00204b2013-03-05 15:02:14 -0800325 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800326 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800327 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
328 ArrayList<ActivityStack> mStacks;
329 /** The attached Display's unique identifier, or -1 if detached */
330 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800331
Craig Mautner27084302013-03-25 08:05:25 -0700332 /** Run all ActivityStacks through this */
333 final ActivityStackSupervisor mStackSupervisor;
334
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700335 private final LaunchingTaskPositioner mTaskPositioner;
336
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700337 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700338 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
339 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
340 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
341 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700342 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700343 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700344 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700345
346 static class ScheduleDestroyArgs {
347 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700348 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700349 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700350 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700351 mReason = reason;
352 }
353 }
354
Zoran Marcetaf958b322012-08-09 20:27:12 +0900355 final Handler mHandler;
356
357 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800358
Craig Mautnerc8143c62013-09-03 12:15:57 -0700359 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900360 super(looper);
361 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700362
Zoran Marcetaf958b322012-08-09 20:27:12 +0900363 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700364 public void handleMessage(Message msg) {
365 switch (msg.what) {
366 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800367 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700368 // We don't at this point know if the activity is fullscreen,
369 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800370 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700371 synchronized (mService) {
372 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700373 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700374 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700375 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800376 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700377 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700378 case LAUNCH_TICK_MSG: {
379 ActivityRecord r = (ActivityRecord)msg.obj;
380 synchronized (mService) {
381 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700382 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700383 }
384 }
385 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700386 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800387 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700388 // We don't at this point know if the activity is fullscreen,
389 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800390 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800391 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800392 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800393 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700394 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700395 case STOP_TIMEOUT_MSG: {
396 ActivityRecord r = (ActivityRecord)msg.obj;
397 // We don't at this point know if the activity is fullscreen,
398 // so we need to be conservative and assume it isn't.
399 Slog.w(TAG, "Activity stop timeout for " + r);
400 synchronized (mService) {
401 if (r.isInHistory()) {
402 activityStoppedLocked(r, null, null, null);
403 }
404 }
405 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700406 case DESTROY_ACTIVITIES_MSG: {
407 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
408 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700409 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700410 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700411 } break;
412 case TRANSLUCENT_TIMEOUT_MSG: {
413 synchronized (mService) {
414 notifyActivityDrawnLocked(null);
415 }
416 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700417 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700418 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700419 final ActivityRecord r = getVisibleBehindActivity();
420 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700421 if (r != null) {
422 mService.killAppAtUsersRequest(r.app, null);
423 }
424 }
425 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700426 }
427 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800428 }
429
Craig Mautner34b73df2014-01-12 21:11:08 -0800430 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800431 int count = 0;
432 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
433 count += mTaskHistory.get(taskNdx).mActivities.size();
434 }
435 return count;
436 }
437
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800438 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
439 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800440 mActivityContainer = activityContainer;
441 mStackSupervisor = activityContainer.getOuter();
442 mService = mStackSupervisor.mService;
443 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
444 mWindowManager = mService.mWindowManager;
445 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700446 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800447 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700448 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
449 ? new LaunchingTaskPositioner() : null;
450 }
451
452 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
453 mDisplayId = activityDisplay.mDisplayId;
454 mStacks = activityDisplay.mStacks;
455 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
456 mFullscreen = mBounds == null;
457 if (mTaskPositioner != null) {
458 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
459 mTaskPositioner.configure(mBounds);
460 }
Wale Ogunwale961f4852016-02-01 20:25:54 -0800461
462 if (mStackId == DOCKED_STACK_ID) {
463 // If we created a docked stack we want to resize it so it resizes all other stacks
464 // in the system.
465 mStackSupervisor.resizeDockedStackLocked(
466 mBounds, null, null, null, null, PRESERVE_WINDOWS);
467 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700468 }
469
470 void detachDisplay() {
471 mDisplayId = Display.INVALID_DISPLAY;
472 mStacks = null;
473 if (mTaskPositioner != null) {
474 mTaskPositioner.reset();
475 }
476 mWindowManager.detachStack(mStackId);
Jorim Jaggi899327f2016-02-25 20:44:18 -0500477 if (mStackId == DOCKED_STACK_ID) {
478 // If we removed a docked stack we want to resize it so it resizes all other stacks
479 // in the system to fullscreen.
480 mStackSupervisor.resizeDockedStackLocked(
481 null, null, null, null, null, PRESERVE_WINDOWS);
482 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700483 }
484
Winsonc809cbb2015-11-02 12:06:15 -0800485 public void getDisplaySize(Point out) {
486 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
487 }
488
Jorim Jaggi192086e2016-03-11 17:17:03 +0100489 /**
490 * Defers updating the bounds of the stack. If the stack was resized/repositioned while
491 * deferring, the bounds will update in {@link #continueUpdateBounds()}.
492 */
493 void deferUpdateBounds() {
494 if (!mUpdateBoundsDeferred) {
495 mUpdateBoundsDeferred = true;
496 mUpdateBoundsDeferredCalled = false;
497 }
498 }
499
500 /**
501 * Continues updating bounds after updates have been deferred. If there was a resize attempt
502 * between {@link #deferUpdateBounds()} and {@link #continueUpdateBounds()}, the stack will
503 * be resized to that bounds.
504 */
505 void continueUpdateBounds() {
506 final boolean wasDeferred = mUpdateBoundsDeferred;
507 mUpdateBoundsDeferred = false;
508 if (wasDeferred && mUpdateBoundsDeferredCalled) {
509 mStackSupervisor.resizeStackUncheckedLocked(this,
510 mDeferredBounds.isEmpty() ? null : mDeferredBounds,
511 mDeferredTaskBounds.isEmpty() ? null : mDeferredTaskBounds,
512 mDeferredTaskInsetBounds.isEmpty() ? null : mDeferredTaskInsetBounds);
513 }
514 }
515
516 boolean updateBoundsAllowed(Rect bounds, Rect tempTaskBounds,
517 Rect tempTaskInsetBounds) {
518 if (!mUpdateBoundsDeferred) {
519 return true;
520 }
521 if (bounds != null) {
522 mDeferredBounds.set(bounds);
523 } else {
524 mDeferredBounds.setEmpty();
525 }
526 if (tempTaskBounds != null) {
527 mDeferredTaskBounds.set(tempTaskBounds);
528 } else {
529 mDeferredTaskBounds.setEmpty();
530 }
531 if (tempTaskInsetBounds != null) {
532 mDeferredTaskInsetBounds.set(tempTaskInsetBounds);
533 } else {
534 mDeferredTaskInsetBounds.setEmpty();
535 }
536 mUpdateBoundsDeferredCalled = true;
537 return false;
538 }
539
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700540 void setBounds(Rect bounds) {
541 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700542 if (mTaskPositioner != null) {
543 mTaskPositioner.configure(bounds);
544 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700545 }
Craig Mautner5962b122012-10-05 14:45:52 -0700546
Amith Yamasani734983f2014-03-04 16:48:05 -0800547 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700548 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700549 }
550
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700551 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800552 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700553 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700554 if (r != null) {
555 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800556 }
557 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700558 return null;
559 }
560
561 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800562 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
563 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800564 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800565 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
566 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800567 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800568 return r;
569 }
570 }
571 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700572 return null;
573 }
574
575 /**
576 * This is a simplified version of topRunningActivityLocked that provides a number of
577 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800578 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700579 * @param token If non-null, any history records matching this token will be skipped.
580 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800581 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700582 * @return Returns the HistoryRecord of the next activity on the stack.
583 */
584 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800585 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
586 TaskRecord task = mTaskHistory.get(taskNdx);
587 if (task.taskId == taskId) {
588 continue;
589 }
590 ArrayList<ActivityRecord> activities = task.mActivities;
591 for (int i = activities.size() - 1; i >= 0; --i) {
592 final ActivityRecord r = activities.get(i);
593 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800594 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800595 return r;
596 }
597 }
598 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700599 return null;
600 }
601
Craig Mautner8849a5e2013-04-02 16:41:03 -0700602 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700603 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
604 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700605 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
606 final ActivityRecord r = activities.get(activityNdx);
607 if (!r.finishing) {
608 return r;
609 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700610 }
611 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700612 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700613 }
614
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700615 final TaskRecord topTask() {
616 final int size = mTaskHistory.size();
617 if (size > 0) {
618 return mTaskHistory.get(size - 1);
619 }
620 return null;
621 }
622
Craig Mautnerd2328952013-03-05 12:46:26 -0800623 TaskRecord taskForIdLocked(int id) {
624 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
625 final TaskRecord task = mTaskHistory.get(taskNdx);
626 if (task.taskId == id) {
627 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800628 }
629 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700630 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700631 }
632
Craig Mautnerd2328952013-03-05 12:46:26 -0800633 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700634 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800635 return isInStackLocked(r);
636 }
637
638 ActivityRecord isInStackLocked(ActivityRecord r) {
639 if (r == null) {
640 return null;
641 }
642 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700643 if (task != null && task.stack != null
644 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800645 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800646 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800647 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800648 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800649 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800650 }
651
Craig Mautner2420ead2013-04-01 17:13:20 -0700652 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700653 final boolean hadit = mLRUActivities.remove(r);
654 mLRUActivities.add(r);
655 return hadit;
656 }
657
Craig Mautnerde4ef022013-04-07 19:01:33 -0700658 final boolean isHomeStack() {
659 return mStackId == HOME_STACK_ID;
660 }
661
Winson Chung0583d3d2015-12-18 10:03:12 -0500662 final boolean isDockedStack() {
663 return mStackId == DOCKED_STACK_ID;
664 }
665
666 final boolean isPinnedStack() {
667 return mStackId == PINNED_STACK_ID;
668 }
669
Craig Mautnere0a38842013-12-16 16:14:02 -0800670 final boolean isOnHomeDisplay() {
671 return isAttached() &&
672 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
673 }
674
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700675 void moveToFront(String reason) {
676 moveToFront(reason, null);
677 }
678
679 /**
680 * @param reason The reason for moving the stack to the front.
681 * @param task If non-null, the task will be moved to the top of the stack.
682 * */
683 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700684 if (!isAttached()) {
685 return;
686 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700687
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700688 mStacks.remove(this);
689 int addIndex = mStacks.size();
690
691 if (addIndex > 0) {
692 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800693 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
694 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700695 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800696 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700697 }
698
699 mStacks.add(addIndex, this);
700
701 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
702 if (isOnHomeDisplay()) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800703 mStackSupervisor.setFocusStackUnchecked(reason, this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700704 }
705 if (task != null) {
706 insertTaskAtTop(task, null);
707 } else {
708 task = topTask();
709 }
710 if (task != null) {
711 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800712 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800713 }
714
Wale Ogunwaled046a012015-12-24 13:05:59 -0800715 boolean isFocusable() {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800716 if (StackId.canReceiveKeys(mStackId)) {
717 return true;
718 }
719 // The stack isn't focusable. See if its top activity is focusable to force focus on the
720 // stack.
721 final ActivityRecord r = topRunningActivityLocked();
722 return r != null && r.isFocusable();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800723 }
724
Craig Mautnere0a38842013-12-16 16:14:02 -0800725 final boolean isAttached() {
726 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800727 }
728
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700729 /**
Wale Ogunwale39381972015-12-17 17:15:29 -0800730 * Returns the top activity in any existing task matching the given Intent in the input result.
731 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700732 */
Wale Ogunwale39381972015-12-17 17:15:29 -0800733 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700734 Intent intent = target.intent;
735 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700736 ComponentName cls = intent.getComponent();
737 if (info.targetActivity != null) {
738 cls = new ComponentName(info.packageName, info.targetActivity);
739 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700740 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700741 boolean isDocument = intent != null & intent.isDocument();
742 // If documentData is non-null then it must match the existing task data.
743 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800744
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700745 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800746 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
747 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700748 if (task.voiceSession != null) {
749 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700750 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700751 continue;
752 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700753 if (task.userId != userId) {
754 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700755 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700756 continue;
757 }
Craig Mautner000f0022013-02-26 15:04:29 -0800758 final ActivityRecord r = task.getTopActivity();
759 if (r == null || r.finishing || r.userId != userId ||
760 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700761 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800762 continue;
763 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700764 if (r.mActivityType != target.mActivityType) {
765 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
766 continue;
767 }
Craig Mautner000f0022013-02-26 15:04:29 -0800768
Craig Mautnerd00f4742014-03-12 14:17:26 -0700769 final Intent taskIntent = task.intent;
770 final Intent affinityIntent = task.affinityIntent;
771 final boolean taskIsDocument;
772 final Uri taskDocumentData;
773 if (taskIntent != null && taskIntent.isDocument()) {
774 taskIsDocument = true;
775 taskDocumentData = taskIntent.getData();
776 } else if (affinityIntent != null && affinityIntent.isDocument()) {
777 taskIsDocument = true;
778 taskDocumentData = affinityIntent.getData();
779 } else {
780 taskIsDocument = false;
781 taskDocumentData = null;
782 }
783
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700784 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700785 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700786 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700787 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700788 // TODO Refactor to remove duplications. Check if logic can be simplified.
789 if (taskIntent != null && taskIntent.getComponent() != null &&
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700790 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700791 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700792 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800793 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700794 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
795 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800796 result.r = r;
797 result.matchedByRootAffinity = false;
798 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700799 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
800 affinityIntent.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;
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700809 } else if (!isDocument && !taskIsDocument
810 && result.r == null && task.canMatchRootAffinity()) {
811 if (task.rootAffinity.equals(target.taskAffinity)) {
812 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
813 // It is possible for multiple tasks to have the same root affinity especially
814 // if they are in separate stacks. We save off this candidate, but keep looking
815 // to see if there is a better candidate.
816 result.r = r;
817 result.matchedByRootAffinity = true;
818 }
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 */
Andrii Kuliand3bbb132016-06-16 16:00:20 -0700828 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
829 boolean compareIntentFilters) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700830 ComponentName cls = intent.getComponent();
831 if (info.targetActivity != null) {
832 cls = new ComponentName(info.packageName, info.targetActivity);
833 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700834 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700835
Craig Mautner000f0022013-02-26 15:04:29 -0800836 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700837 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700838 final boolean notCurrentUserTask =
839 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700840 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700841
Craig Mautner000f0022013-02-26 15:04:29 -0800842 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
843 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700844 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700845 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700846 }
Andrii Kuliand3bbb132016-06-16 16:00:20 -0700847 if (!r.finishing && r.userId == userId) {
848 if (compareIntentFilters) {
849 if (r.intent.filterEquals(intent)) {
850 return r;
851 }
852 } else {
853 if (r.intent.getComponent().equals(cls)) {
854 return r;
855 }
856 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700857 }
858 }
859 }
860
861 return null;
862 }
863
Amith Yamasani742a6712011-05-04 14:49:28 -0700864 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700865 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700866 */
Craig Mautner93529a42013-10-04 15:03:13 -0700867 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800868 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700869 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800870 }
871 mCurrentUser = userId;
872
873 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800874 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800875 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700876 final TaskRecord task = mTaskHistory.get(i);
877
878 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
879 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700880 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700881 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700882 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700883 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800884 mTaskHistory.remove(i);
885 mTaskHistory.add(task);
886 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800887 // Use same value for i.
888 } else {
889 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800890 }
891 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700892 if (VALIDATE_TOKENS) {
893 validateAppTokensLocked();
894 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700895 }
896
Craig Mautner2420ead2013-04-01 17:13:20 -0700897 void minimalResumeActivityLocked(ActivityRecord r) {
898 r.state = ActivityState.RESUMED;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800899 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + r + " (starting new instance)"
900 + " callers=" + Debug.getCallers(5));
Craig Mautner2420ead2013-04-01 17:13:20 -0700901 mResumedActivity = r;
902 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800903 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700904 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700905 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700906 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700907 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
908 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700909 }
910
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -0800911 void addRecentActivityLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700912 if (r != null) {
913 mRecentTasks.addLocked(r.task);
914 r.task.touchActiveTime();
915 }
916 }
917
Narayan Kamath7829c812015-06-08 17:39:43 +0100918 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700919 if (mFullyDrawnStartTime != 0) {
920 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
921 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100922 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700923 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
924 }
925
926 private void stopFullyDrawnTraceIfNeeded() {
927 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
928 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
929 mFullyDrawnStartTime = 0;
930 }
931 }
932
Craig Mautnere79d42682013-04-01 19:01:53 -0700933 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700934 if (r.displayStartTime == 0) {
935 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
936 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100937 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700938 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700939 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700940 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100941 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700942 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700943 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700944 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800945
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700946 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700947 // Make sure that there is no activity waiting for this to launch.
948 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
949 r.displayStartTime = r.fullyDrawnStartTime = 0;
950 } else {
951 mStackSupervisor.removeTimeoutsForActivityLocked(r);
952 mStackSupervisor.scheduleIdleTimeoutLocked(r);
953 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700954 }
955
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800956 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800957 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800958 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
959 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
960 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
961 activities.get(activityNdx).setSleeping(false);
962 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800963 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800964 if (mPausingActivity != null) {
965 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
966 activityPausedLocked(mPausingActivity.appToken, true);
967 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800968 }
969
Todd Kennedy39bfee52016-02-24 10:28:21 -0800970 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
971 final String packageName = aInfo.packageName;
972 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
973 final List<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
974 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
975 if (packageName.equals(activities.get(activityNdx).packageName)) {
976 activities.get(activityNdx).info.applicationInfo = aInfo;
977 }
978 }
979 }
980 }
981
Craig Mautner0eea92c2013-05-16 13:35:39 -0700982 /**
983 * @return true if something must be done before going to sleep.
984 */
985 boolean checkReadyForSleepLocked() {
986 if (mResumedActivity != null) {
987 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700988 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
989 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
990 "Sleep => pause with userLeaving=false");
Wale Ogunwale950faff2016-08-08 09:51:04 -0700991 startPausingLocked(false, true, null, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700992 return true;
993 }
994 if (mPausingActivity != null) {
995 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700996 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700997 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800998 }
999
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001000 if (hasVisibleBehindActivity()) {
1001 // Stop visible behind activity before going to sleep.
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001002 final ActivityRecord r = getVisibleBehindActivity();
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001003 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001004 if (DEBUG_STATES) Slog.v(TAG_STATES, "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001005 return true;
1006 }
1007
Craig Mautner0eea92c2013-05-16 13:35:39 -07001008 return false;
1009 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001010
Craig Mautner0eea92c2013-05-16 13:35:39 -07001011 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001012 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001013
Andrii Kulianf9949d52016-05-06 12:53:25 -07001014 // Make sure any paused or stopped but visible activities are now sleeping.
Craig Mautner0eea92c2013-05-16 13:35:39 -07001015 // This ensures that the activity's onStop() is called.
1016 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1017 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1018 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1019 final ActivityRecord r = activities.get(activityNdx);
Andrii Kulianf9949d52016-05-06 12:53:25 -07001020 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED
1021 || r.state == ActivityState.PAUSED || r.state == ActivityState.PAUSING) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001022 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001023 }
1024 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001025 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001026 }
Craig Mautner59c00972012-07-30 12:10:24 -07001027
Winson8b1871d2015-11-20 09:56:20 -08001028 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
1029 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -08001030 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001031 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -08001032 return null;
1033 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001034
Winson Chung083baf92014-07-11 10:32:42 -07001035 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -07001036 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -08001037 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001038 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -07001039 return null;
1040 }
1041
Winson Chung48a10a52014-08-27 14:36:51 -07001042 int w = mService.mThumbnailWidth;
1043 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -08001044 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001045 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001046 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -08001047
1048 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -08001049 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
1050 // use within SystemUI while keeping memory usage low.
Winson Chungead5c0f2015-12-14 11:18:57 -05001051 if (ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS) {
Winson8b1871d2015-11-20 09:56:20 -08001052 w = h = -1;
Winson21700932016-03-24 17:26:23 -07001053 scale = mService.mFullscreenThumbnailScale;
Winson8b1871d2015-11-20 09:56:20 -08001054 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -08001055 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -08001056 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001057 }
Winson Chung376543b2014-05-21 17:43:28 -07001058 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001059 return null;
1060 }
1061
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001062 /**
1063 * Start pausing the currently resumed activity. It is an error to call this if there
1064 * is already an activity being paused or there is no resumed activity.
1065 *
1066 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
1067 * @param uiSleeping True if this is happening with the user interface going to sleep (the
1068 * screen turning off).
Wale Ogunwale950faff2016-08-08 09:51:04 -07001069 * @param resuming The activity we are currently trying to resume or null if this is not being
1070 * called as part of resuming the top activity, so we shouldn't try to instigate
1071 * a resume here if not null.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001072 * @param dontWait True if the caller does not want to wait for the pause to complete. If
1073 * set to true, we will immediately complete the pause here before returning.
1074 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
1075 * it to tell us when it is done.
1076 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001077 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping,
1078 ActivityRecord resuming, boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001079 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001080 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
1081 + " state=" + mPausingActivity.state);
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07001082 if (!mService.isSleepingLocked()) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001083 // Avoid recursion among check for sleep and complete pause during sleeping.
1084 // Because activity will be paused immediately after resume, just let pause
1085 // be completed by the order of activity paused from clients.
Wale Ogunwale950faff2016-08-08 09:51:04 -07001086 completePauseLocked(false, resuming);
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001087 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001088 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001089 ActivityRecord prev = mResumedActivity;
1090 if (prev == null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07001091 if (resuming == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001092 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001093 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001094 }
1095 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001096 }
Craig Mautnerdf88d732014-01-27 09:21:32 -08001097
1098 if (mActivityContainer.mParentActivity == null) {
1099 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001100 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001101 }
1102
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001103 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001104 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001105 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001106 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001107 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -07001108 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1109 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001110 prev.state = ActivityState.PAUSING;
1111 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001112 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -07001113 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -07001114 if (mService.mHasRecents
1115 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001116 prev.mUpdateTaskThumbnailWhenHidden = true;
Craig Mautner6f6d56f2013-10-24 16:02:07 -07001117 }
Dianne Hackborncee04b52013-07-03 17:01:28 -07001118 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001119
1120 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -08001121
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001122 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001123 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001124 try {
1125 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001126 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001127 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -07001128 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001129 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001130 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001131 } catch (Exception e) {
1132 // Ignore exception, if process died other code will cleanup.
1133 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001134 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001135 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001136 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001137 }
1138 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001139 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001140 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001141 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001142 }
1143
1144 // If we are not going to sleep, we want to ensure the device is
1145 // awake until the next activity is started.
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07001146 if (!uiSleeping && !mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001147 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001148 }
1149
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001150 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001151 // Have the window manager pause its key dispatching until the new
1152 // activity has started. If we're pausing the activity just because
1153 // the screen is being turned off and the UI is sleeping, don't interrupt
1154 // key dispatch; the same activity will pick it up again on wakeup.
1155 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001156 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001157 } else if (DEBUG_PAUSE) {
1158 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001159 }
1160
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001161 if (dontWait) {
1162 // If the caller said they don't want to wait for the pause, then complete
1163 // the pause now.
Wale Ogunwale950faff2016-08-08 09:51:04 -07001164 completePauseLocked(false, resuming);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001165 return false;
1166
1167 } else {
1168 // Schedule a pause timeout in case the app doesn't respond.
1169 // We don't give it much time because this directly impacts the
1170 // responsiveness seen by the user.
1171 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1172 msg.obj = prev;
1173 prev.pauseTime = SystemClock.uptimeMillis();
1174 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001175 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001176 return true;
1177 }
1178
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001179 } else {
1180 // This activity failed to schedule the
1181 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001182 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Wale Ogunwale950faff2016-08-08 09:51:04 -07001183 if (resuming == null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001184 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001185 }
1186 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001187 }
1188 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001189
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001190 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001191 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1192 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001193
Craig Mautnerd2328952013-03-05 12:46:26 -08001194 final ActivityRecord r = isInStackLocked(token);
1195 if (r != null) {
1196 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1197 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001198 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001199 + (timeout ? " (due to timeout)" : " (pause complete)"));
Wale Ogunwale950faff2016-08-08 09:51:04 -07001200 completePauseLocked(true, null);
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001201 return;
Craig Mautnerd2328952013-03-05 12:46:26 -08001202 } else {
1203 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1204 r.userId, System.identityHashCode(r), r.shortComponentName,
1205 mPausingActivity != null
1206 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001207 if (r.state == ActivityState.PAUSING) {
1208 r.state = ActivityState.PAUSED;
1209 if (r.finishing) {
1210 if (DEBUG_PAUSE) Slog.v(TAG,
1211 "Executing finish of failed to pause activity: " + r);
1212 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1213 }
louis_chang047dfd42015-04-08 16:35:55 +08001214 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001215 }
1216 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001217 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001218 }
1219
Chong Zhangd78ddb42016-03-02 17:01:14 -08001220 final void activityResumedLocked(IBinder token) {
1221 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
1222 if (DEBUG_SAVED_STATE) Slog.i(TAG_STATES, "Resumed activity; dropping state of: " + r);
1223 r.icicle = null;
1224 r.haveState = false;
1225 }
1226
Craig Mautnera0026042014-04-23 11:45:37 -07001227 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1228 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001229 if (r.state != ActivityState.STOPPING) {
1230 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1231 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1232 return;
1233 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001234 if (persistentState != null) {
1235 r.persistentState = persistentState;
1236 mService.notifyTaskPersisterLocked(r.task, false);
1237 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001238 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001239 if (icicle != null) {
1240 // If icicle is null, this is happening due to a timeout, so we
1241 // haven't really saved the state.
1242 r.icicle = icicle;
1243 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001244 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001245 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001246 }
1247 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001248 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001249 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1250 r.stopped = true;
1251 r.state = ActivityState.STOPPED;
Robert Carre12aece2016-02-02 22:43:27 -08001252
Chong Zhang45e6d2d2016-07-20 18:33:56 -07001253 mWindowManager.notifyAppStopped(r.appToken);
Robert Carre12aece2016-02-02 22:43:27 -08001254
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001255 if (getVisibleBehindActivity() == r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001256 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001257 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001258 if (r.finishing) {
1259 r.clearOptionsLocked();
1260 } else {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001261 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001262 destroyActivityLocked(r, true, "stop-config");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001263 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001264 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001265 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001266 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001267 }
1268 }
1269 }
1270
Wale Ogunwale950faff2016-08-08 09:51:04 -07001271 private void completePauseLocked(boolean resumeNext, ActivityRecord resuming) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001272 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001273 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001274
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001275 if (prev != null) {
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001276 final boolean wasStopping = prev.state == ActivityState.STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001277 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001278 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001279 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001280 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001281 } else if (prev.app != null) {
Wale Ogunwaled8026642016-02-09 20:40:18 -08001282 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueue pending stop if needed: " + prev
1283 + " wasStopping=" + wasStopping + " visible=" + prev.visible);
Craig Mautner8c14c152015-01-15 17:32:07 -08001284 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001285 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1286 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001287 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001288 if (prev.deferRelaunchUntilPaused) {
1289 // Complete the deferred relaunch that was waiting for pause to complete.
1290 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
1291 relaunchActivityLocked(prev, prev.configChangeFlags, false,
1292 prev.preserveWindowOnDeferredRelaunch);
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001293 } else if (wasStopping) {
1294 // We are also stopping, the stop request must have gone soon after the pause.
1295 // We can't clobber it, because the stop confirmation will not be handled.
1296 // We don't need to schedule another stop, we only need to let it happen.
1297 prev.state = ActivityState.STOPPING;
Wale Ogunwaled8026642016-02-09 20:40:18 -08001298 } else if ((!prev.visible && !hasVisibleBehindActivity())
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07001299 || mService.isSleepingOrShuttingDownLocked()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001300 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001301 // stopping.
Chong Zhang46b1ac62016-02-18 17:53:57 -08001302 addToStopping(prev, true /* immediate */);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001303 }
1304 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001305 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001306 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001307 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001308 // It is possible the activity was freezing the screen before it was paused.
1309 // In that case go ahead and remove the freeze this activity has on the screen
1310 // since it is no longer visible.
1311 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001312 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001313 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001314
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001315 if (resumeNext) {
1316 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07001317 if (!mService.isSleepingOrShuttingDownLocked()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001318 mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001319 } else {
1320 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001321 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001322 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001323 // If there are no more activities available to run, do resume anyway to start
1324 // something. Also if the top activity on the stack is not the just paused
1325 // activity, we need to go ahead and resume it to ensure we complete an
1326 // in-flight app switch.
1327 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001328 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001329 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001330 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001331
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001332 if (prev != null) {
1333 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001334
Craig Mautner525f3d92013-05-07 14:01:50 -07001335 if (prev.app != null && prev.cpuTimeAtResume > 0
1336 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001337 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1338 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001339 if (diff > 0) {
1340 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1341 synchronized (bsi) {
1342 BatteryStatsImpl.Uid.Proc ps =
1343 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001344 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001345 if (ps != null) {
1346 ps.addForegroundTimeLocked(diff);
1347 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001348 }
1349 }
1350 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001351 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001352 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001353
Andrii Kulian8290f8f2016-06-30 17:51:32 -07001354 // Notify when the task stack has changed, but only if visibilities changed (not just
1355 // focus). Also if there is an active pinned stack - we always want to notify it about
1356 // task stack changes, because its positioning may depend on it.
1357 if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause
1358 || mService.mStackSupervisor.getStack(PINNED_STACK_ID) != null) {
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001359 mService.notifyTaskStackChangedLocked();
1360 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
1361 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001362
Wale Ogunwale950faff2016-08-08 09:51:04 -07001363 mStackSupervisor.ensureActivitiesVisibleLocked(resuming, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001364 }
1365
Chong Zhang46b1ac62016-02-18 17:53:57 -08001366 private void addToStopping(ActivityRecord r, boolean immediate) {
1367 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1368 mStackSupervisor.mStoppingActivities.add(r);
1369 }
1370
1371 // If we already have a few activities waiting to stop, then give up
1372 // on things going idle and start clearing them out. Or if r is the
1373 // last of activity of the last task the stack will be empty and must
1374 // be cleared immediately.
1375 boolean forceIdle = mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE
1376 || (r.frontOfTask && mTaskHistory.size() <= 1);
1377
1378 if (immediate || forceIdle) {
1379 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Scheduling idle now: forceIdle="
1380 + forceIdle + "immediate=" + immediate);
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001381 mStackSupervisor.scheduleIdleLocked();
1382 } else {
1383 mStackSupervisor.checkReadyForSleepLocked();
1384 }
1385 }
1386
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001387 /**
1388 * Once we know that we have asked an application to put an activity in
1389 * the resumed state (either by launching it or explicitly telling it),
1390 * this function updates the rest of our state to match that fact.
1391 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001392 private void completeResumeLocked(ActivityRecord next) {
Chong Zhang2b79af12016-02-10 18:47:06 -08001393 next.visible = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001394 next.idle = false;
1395 next.results = null;
1396 next.newIntents = null;
Chong Zhang2b79af12016-02-10 18:47:06 -08001397 next.stopped = false;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001398
Chong Zhang85ee6542015-10-02 13:36:38 -07001399 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001400 ProcessRecord app = next.task.mActivities.get(0).app;
1401 if (app != null && app != mService.mHomeProcess) {
1402 mService.mHomeProcess = app;
1403 }
1404 }
1405
Craig Mautner07566322013-09-26 16:42:55 -07001406 if (next.nowVisible) {
1407 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Chong Zhang5022da32016-06-21 16:31:37 -07001408 mStackSupervisor.reportActivityVisibleLocked(next);
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001409 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001410 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001411
1412 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001413 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001414
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001415 mStackSupervisor.reportResumedActivityLocked(next);
1416
1417 next.resumeKeyDispatchingLocked();
1418 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001419
1420 // Mark the point when the activity is resuming
1421 // TODO: To be more accurate, the mark should be before the onCreate,
1422 // not after the onResume. But for subsequent starts, onResume is fine.
1423 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001424 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001425 } else {
1426 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1427 }
Winson Chung376543b2014-05-21 17:43:28 -07001428
George Mount6ba042b2014-07-28 11:12:28 -07001429 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001430
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001431 if (getVisibleBehindActivity() == next) {
Craig Mautner64ccb702014-10-01 09:38:40 -07001432 // When resuming an activity, require it to call requestVisibleBehind() again.
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001433 setVisibleBehindActivity(null);
Craig Mautner64ccb702014-10-01 09:38:40 -07001434 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001435 }
1436
Craig Mautner2568c3a2015-03-26 14:22:34 -07001437 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001438 r.visible = visible;
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001439 if (!visible && r.mUpdateTaskThumbnailWhenHidden) {
1440 r.updateThumbnailLocked(r.task.stack.screenshotActivitiesLocked(r), null);
1441 r.mUpdateTaskThumbnailWhenHidden = false;
1442 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001443 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001444 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001445 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001446 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001447 container.setVisible(visible);
1448 }
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001449 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001450 }
1451
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001452 // Find the first visible activity above the passed activity and if it is translucent return it
1453 // otherwise return null;
1454 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1455 TaskRecord task = r.task;
1456 if (task == null) {
1457 return null;
1458 }
1459
1460 ActivityStack stack = task.stack;
1461 if (stack == null) {
1462 return null;
1463 }
1464
1465 int stackNdx = mStacks.indexOf(stack);
1466
1467 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1468 int taskNdx = tasks.indexOf(task);
1469
1470 ArrayList<ActivityRecord> activities = task.mActivities;
1471 int activityNdx = activities.indexOf(r) + 1;
1472
1473 final int numStacks = mStacks.size();
1474 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001475 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001476 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001477 final int numTasks = tasks.size();
1478 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001479 final TaskRecord currentTask = tasks.get(taskNdx);
1480 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001481 final int numActivities = activities.size();
1482 while (activityNdx < numActivities) {
1483 final ActivityRecord activity = activities.get(activityNdx);
1484 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001485 return historyStack.mFullscreen
1486 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001487 }
1488 ++activityNdx;
1489 }
1490 activityNdx = 0;
1491 ++taskNdx;
1492 }
1493 taskNdx = 0;
1494 ++stackNdx;
1495 }
1496
1497 return null;
1498 }
1499
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001500 ActivityStack getNextFocusableStackLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001501 ArrayList<ActivityStack> stacks = mStacks;
1502 final ActivityRecord parent = mActivityContainer.mParentActivity;
1503 if (parent != null) {
1504 stacks = parent.task.stack.mStacks;
1505 }
1506 if (stacks != null) {
1507 for (int i = stacks.size() - 1; i >= 0; --i) {
1508 ActivityStack stack = stacks.get(i);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001509 if (stack != this && stack.isFocusable()
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001510 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001511 return stack;
1512 }
1513 }
1514 }
1515 return null;
1516 }
1517
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001518 /** Returns true if the stack contains a fullscreen task. */
1519 private boolean hasFullscreenTask() {
1520 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1521 final TaskRecord task = mTaskHistory.get(i);
1522 if (task.mFullscreen) {
1523 return true;
1524 }
1525 }
1526 return false;
1527 }
1528
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001529 /**
1530 * Returns true if the stack is translucent and can have other contents visible behind it if
1531 * needed. A stack is considered translucent if it don't contain a visible or
1532 * starting (about to be visible) activity that is fullscreen (opaque).
1533 * @param starting The currently starting activity or null if there is none.
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001534 * @param stackBehindId The id of the stack directly behind this one.
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001535 */
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001536 private boolean isStackTranslucent(ActivityRecord starting, int stackBehindId) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001537 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1538 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001539 final ArrayList<ActivityRecord> activities = task.mActivities;
1540 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1541 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001542
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001543 if (r.finishing) {
1544 // We don't factor in finishing activities when determining translucency since
1545 // they will be gone soon.
1546 continue;
1547 }
1548
1549 if (!r.visible && r != starting) {
1550 // Also ignore invisible activities that are not the currently starting
1551 // activity (about to be visible).
1552 continue;
1553 }
1554
1555 if (r.fullscreen) {
1556 // Stack isn't translucent if it has at least one fullscreen activity
1557 // that is visible.
1558 return false;
1559 }
1560
1561 if (!isHomeStack() && r.frontOfTask
1562 && task.isOverHomeStack() && stackBehindId != HOME_STACK_ID) {
1563 // Stack isn't translucent if it's top activity should have the home stack
1564 // behind it and the stack currently behind it isn't the home stack.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001565 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001566 }
1567 }
1568 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001569 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001570 }
1571
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001572 /**
1573 * Returns stack's visibility: {@link #STACK_INVISIBLE}, {@link #STACK_VISIBLE} or
1574 * {@link #STACK_VISIBLE_ACTIVITY_BEHIND}.
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001575 * @param starting The currently starting activity or null if there is none.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001576 */
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001577 int getStackVisibilityLocked(ActivityRecord starting) {
Jose Lima7ba71252014-04-30 12:59:27 -07001578 if (!isAttached()) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001579 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001580 }
1581
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001582 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001583 return STACK_VISIBLE;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001584 }
1585
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001586 final int stackIndex = mStacks.indexOf(this);
1587
1588 if (stackIndex == mStacks.size() - 1) {
1589 Slog.wtf(TAG,
1590 "Stack=" + this + " isn't front stack but is at the top of the stack list");
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001591 return STACK_INVISIBLE;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001592 }
1593
Chong Zhang75b37202015-12-04 14:16:36 -08001594 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1595 if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001596 return STACK_INVISIBLE;
Chong Zhang75b37202015-12-04 14:16:36 -08001597 }
1598
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001599 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1600 final int focusedStackId = focusedStack.mStackId;
1601
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001602 if (mStackId == FULLSCREEN_WORKSPACE_STACK_ID
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001603 && hasVisibleBehindActivity() && focusedStackId == HOME_STACK_ID
1604 && !focusedStack.topActivity().fullscreen) {
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001605 // The fullscreen stack should be visible if it has a visible behind activity behind
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001606 // the home stack that is translucent.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001607 return STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001608 }
1609
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001610 if (mStackId == DOCKED_STACK_ID) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001611 // Docked stack is always visible, except in the case where the top running activity
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001612 // task in the focus stack doesn't support any form of resizing but we show it for the
1613 // home task even though it's not resizable.
Wale Ogunwaled26176f2016-01-25 20:04:04 -08001614 final ActivityRecord r = focusedStack.topRunningActivityLocked();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001615 final TaskRecord task = r != null ? r.task : null;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001616 return task == null || task.canGoInDockedStack() || task.isHomeTask() ? STACK_VISIBLE
1617 : STACK_INVISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001618 }
1619
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001620 // Find the first stack behind focused stack that actually got something visible.
1621 int stackBehindFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1622 while (stackBehindFocusedIndex >= 0 &&
1623 mStacks.get(stackBehindFocusedIndex).topRunningActivityLocked() == null) {
1624 stackBehindFocusedIndex--;
Chong Zhangb16cf342015-11-12 17:22:40 -08001625 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001626 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001627 && stackIndex == stackBehindFocusedIndex) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001628 // Stacks directly behind the docked or pinned stack are always visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001629 return STACK_VISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001630 }
1631
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001632 final int stackBehindFocusedId = (stackBehindFocusedIndex >= 0)
1633 ? mStacks.get(stackBehindFocusedIndex).mStackId : INVALID_STACK_ID;
1634
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001635 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001636 && focusedStack.isStackTranslucent(starting, stackBehindFocusedId)) {
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001637 // Stacks behind the fullscreen stack with a translucent activity are always
1638 // visible so they can act as a backdrop to the translucent activity.
1639 // For example, dialog activities
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001640 if (stackIndex == stackBehindFocusedIndex) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001641 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001642 }
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001643 if (stackBehindFocusedIndex >= 0) {
1644 if ((stackBehindFocusedId == DOCKED_STACK_ID
1645 || stackBehindFocusedId == PINNED_STACK_ID)
1646 && stackIndex == (stackBehindFocusedIndex - 1)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001647 // The stack behind the docked or pinned stack is also visible so we can have a
1648 // complete backdrop to the translucent activity when the docked stack is up.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001649 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001650 }
1651 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001652 }
1653
Wale Ogunwale3797c222015-10-27 14:21:58 -07001654 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001655 // Visibility of any static stack should have been determined by the conditions above.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001656 return STACK_INVISIBLE;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001657 }
1658
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001659 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001660 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001661
1662 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1663 continue;
1664 }
1665
Wale Ogunwale3797c222015-10-27 14:21:58 -07001666 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001667 // These stacks can't have any dynamic stacks visible behind them.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001668 return STACK_INVISIBLE;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001669 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001670
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001671 if (!stack.isStackTranslucent(starting, INVALID_STACK_ID)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001672 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001673 }
1674 }
1675
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001676 return STACK_VISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001677 }
1678
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001679 final int rankTaskLayers(int baseLayer) {
1680 int layer = 0;
1681 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1682 final TaskRecord task = mTaskHistory.get(taskNdx);
1683 ActivityRecord r = task.topRunningActivityLocked();
1684 if (r == null || r.finishing || !r.visible) {
1685 task.mLayerRank = -1;
1686 } else {
1687 task.mLayerRank = baseLayer + layer++;
1688 }
1689 }
1690 return layer;
1691 }
1692
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001693 /**
1694 * Make sure that all activities that need to be visible (that is, they
1695 * currently can be seen by the user) actually are.
1696 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001697 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1698 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001699 ActivityRecord top = topRunningActivityLocked();
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001700 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001701 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001702 if (top != null) {
1703 checkTranslucentActivityWaiting(top);
1704 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001705
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001706 // If the top activity is not fullscreen, then we need to
1707 // make sure any activities under it are now visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001708 boolean aboveTop = top != null;
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001709 final int stackVisibility = getStackVisibilityLocked(starting);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001710 final boolean stackInvisible = stackVisibility != STACK_VISIBLE;
1711 final boolean stackVisibleBehind = stackVisibility == STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001712 boolean behindFullscreenActivity = stackInvisible;
Andrii Kulianc11ce7b2016-06-27 10:53:53 -07001713 boolean resumeNextActivity = mStackSupervisor.isFocusedStack(this)
1714 && (isInStackLocked(starting) == null);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001715 boolean behindTranslucentActivity = false;
1716 final ActivityRecord visibleBehind = getVisibleBehindActivity();
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001717 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001718 final TaskRecord task = mTaskHistory.get(taskNdx);
1719 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001720 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1721 final ActivityRecord r = activities.get(activityNdx);
1722 if (r.finishing) {
Chong Zhang22bc8512016-04-07 13:47:18 -07001723 // Normally the screenshot will be taken in makeInvisible(). When an activity
1724 // is finishing, we no longer change its visibility, but we still need to take
1725 // the screenshots if startPausingLocked decided it should be taken.
1726 if (r.mUpdateTaskThumbnailWhenHidden) {
1727 r.updateThumbnailLocked(screenshotActivitiesLocked(r), null);
1728 r.mUpdateTaskThumbnailWhenHidden = false;
1729 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001730 continue;
1731 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001732 final boolean isTop = r == top;
1733 if (aboveTop && !isTop) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001734 continue;
1735 }
1736 aboveTop = false;
Wale Ogunwalee76407c2016-05-16 17:04:37 -07001737
1738 if (shouldBeVisible(r, behindTranslucentActivity, stackVisibleBehind,
1739 visibleBehind, behindFullscreenActivity)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001740 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
1741 + " finishing=" + r.finishing + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001742 // First: if this is not the current activity being started, make
1743 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001744 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001745 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001746 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001747
1748 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001749 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001750 resumeNextActivity, r)) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001751 if (activityNdx >= activities.size()) {
1752 // Record may be removed if its process needs to restart.
1753 activityNdx = activities.size() - 1;
1754 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001755 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001756 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001757 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001758 } else if (r.visible) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001759 // If this activity is already visible, then there is nothing to do here.
Chong Zhang371c4422016-05-11 10:48:32 -07001760 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1761 "Skipping: already visible at " + r);
1762
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001763 if (handleAlreadyVisible(r)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001764 resumeNextActivity = false;
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001765 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001766 } else {
Jorim Jaggie66edb12016-02-05 12:41:17 -08001767 makeVisibleIfNeeded(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001768 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001769 // Aggregate current change flags.
1770 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001771 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1772 behindFullscreenActivity, task, r);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001773 if (behindFullscreenActivity && !r.fullscreen) {
1774 behindTranslucentActivity = true;
1775 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001776 } else {
Wale Ogunwale834c2362016-01-23 18:14:58 -08001777 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001778 + " finishing=" + r.finishing + " state=" + r.state + " stackInvisible="
1779 + stackInvisible + " behindFullscreenActivity="
1780 + behindFullscreenActivity + " mLaunchTaskBehind="
1781 + r.mLaunchTaskBehind);
1782 makeInvisible(r, visibleBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001783 }
1784 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001785 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1786 // The visibility of tasks and the activities they contain in freeform stack are
1787 // determined individually unlike other stacks where the visibility or fullscreen
1788 // status of an activity in a previous task affects other.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001789 behindFullscreenActivity = stackVisibility == STACK_INVISIBLE;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001790 } else if (mStackId == HOME_STACK_ID) {
1791 if (task.isHomeTask()) {
1792 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home task: at " + task
1793 + " stackInvisible=" + stackInvisible
1794 + " behindFullscreenActivity=" + behindFullscreenActivity);
1795 // No other task in the home stack should be visible behind the home activity.
1796 // Home activities is usually a translucent activity with the wallpaper behind
1797 // them. However, when they don't have the wallpaper behind them, we want to
1798 // show activities in the next application stack behind them vs. another
1799 // task in the home stack like recents.
1800 behindFullscreenActivity = true;
1801 } else if (task.isRecentsTask()
1802 && task.getTaskToReturnTo() == APPLICATION_ACTIVITY_TYPE) {
1803 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1804 "Recents task returning to app: at " + task
1805 + " stackInvisible=" + stackInvisible
1806 + " behindFullscreenActivity=" + behindFullscreenActivity);
1807 // We don't want any other tasks in the home stack visible if the recents
1808 // activity is going to be returning to an application activity type.
1809 // We do this to preserve the visible order the user used to get into the
1810 // recents activity. The recents activity is normally translucent and if it
1811 // doesn't have the wallpaper behind it the next activity in the home stack
1812 // shouldn't be visible when the home stack is brought to the front to display
1813 // the recents activity from an app.
1814 behindFullscreenActivity = true;
1815 }
1816
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001817 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001818 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001819
1820 if (mTranslucentActivityWaiting != null &&
1821 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1822 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1823 notifyActivityDrawnLocked(null);
1824 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001825 }
Craig Mautner58547802013-03-05 08:23:53 -08001826
Wale Ogunwalee76407c2016-05-16 17:04:37 -07001827 /** Return true if the input activity should be made visible */
1828 private boolean shouldBeVisible(ActivityRecord r, boolean behindTranslucentActivity,
1829 boolean stackVisibleBehind, ActivityRecord visibleBehind,
1830 boolean behindFullscreenActivity) {
Wale Ogunwalee76407c2016-05-16 17:04:37 -07001831
Wale Ogunwale60920d52016-06-20 10:45:13 -07001832 if (!okToShowLocked(r)) {
Wale Ogunwalee76407c2016-05-16 17:04:37 -07001833 return false;
1834 }
1835
Wale Ogunwale8c09c7d2016-05-23 09:10:24 -07001836 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1837 // but must be drawn initially for the animation as though they were visible.
1838 final boolean activityVisibleBehind =
1839 (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == r;
1840
Wale Ogunwalee76407c2016-05-16 17:04:37 -07001841 boolean isVisible =
1842 !behindFullscreenActivity || r.mLaunchTaskBehind || activityVisibleBehind;
1843
Wale Ogunwaleca2af9e2016-05-26 14:43:19 -07001844 if (mService.mSupportsLeanbackOnly && isVisible && r.isRecentsActivity()) {
1845 // On devices that support leanback only (Android TV), Recents activity can only be
1846 // visible if the home stack is the focused stack or we are in split-screen mode.
Wale Ogunwale8c09c7d2016-05-23 09:10:24 -07001847 isVisible = mStackSupervisor.getStack(DOCKED_STACK_ID) != null
1848 || mStackSupervisor.isFocusedStack(this);
Wale Ogunwalee76407c2016-05-16 17:04:37 -07001849 }
1850
1851 return isVisible;
1852 }
1853
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001854 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1855 if (mTranslucentActivityWaiting != top) {
1856 mUndrawnActivitiesBelowTopTranslucent.clear();
1857 if (mTranslucentActivityWaiting != null) {
1858 // Call the callback with a timeout indication.
1859 notifyActivityDrawnLocked(null);
1860 mTranslucentActivityWaiting = null;
1861 }
1862 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1863 }
1864 }
1865
1866 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001867 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001868 // We need to make sure the app is running if it's the top, or it is just made visible from
1869 // invisible. If the app is already visible, it must have died while it was visible. In this
1870 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1871 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001872 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001873 // This activity needs to be visible, but isn't even running...
1874 // get it started and resume if no other stack in this stack is resumed.
1875 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1876 if (r != starting) {
1877 r.startFreezingScreenLocked(r.app, configChanges);
1878 }
1879 if (!r.visible || r.mLaunchTaskBehind) {
1880 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1881 setVisible(r, true);
1882 }
1883 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001884 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001885 return true;
1886 }
1887 }
1888 return false;
1889 }
1890
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001891 private void makeInvisible(ActivityRecord r, ActivityRecord visibleBehind) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001892 if (!r.visible) {
1893 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1894 return;
1895 }
1896 // Now for any activities that aren't visible to the user, make sure they no longer are
1897 // keeping the screen frozen.
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001898 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.state);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001899 try {
1900 setVisible(r, false);
1901 switch (r.state) {
1902 case STOPPING:
1903 case STOPPED:
1904 if (r.app != null && r.app.thread != null) {
1905 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1906 "Scheduling invisibility: " + r);
1907 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1908 }
1909 break;
1910
1911 case INITIALIZING:
1912 case RESUMED:
1913 case PAUSING:
1914 case PAUSED:
1915 // This case created for transitioning activities from
1916 // translucent to opaque {@link Activity#convertToOpaque}.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001917 if (visibleBehind == r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001918 releaseBackgroundResources(r);
1919 } else {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001920 addToStopping(r, true /* immediate */);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001921 }
1922 break;
1923
1924 default:
1925 break;
1926 }
1927 } catch (Exception e) {
1928 // Just skip on any failure; we'll make it visible when it next restarts.
1929 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1930 }
1931 }
1932
1933 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1934 TaskRecord task, ActivityRecord r) {
1935 if (r.fullscreen) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001936 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwale673cbd22016-01-30 18:30:55 -08001937 + " stackInvisible=" + stackInvisible
1938 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001939 // At this point, nothing else needs to be shown in this task.
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001940 behindFullscreenActivity = true;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001941 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001942 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1943 + " stackInvisible=" + stackInvisible
1944 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001945 behindFullscreenActivity = true;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001946 }
1947 return behindFullscreenActivity;
1948 }
1949
Jorim Jaggie66edb12016-02-05 12:41:17 -08001950 private void makeVisibleIfNeeded(ActivityRecord starting, ActivityRecord r) {
1951
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001952 // This activity is not currently visible, but is running. Tell it to become visible.
Jorim Jaggie66edb12016-02-05 12:41:17 -08001953 if (r.state == ActivityState.RESUMED || r == starting) {
Chong Zhange05db742016-02-16 16:58:37 -08001954 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
1955 "Not making visible, r=" + r + " state=" + r.state + " starting=" + starting);
Jorim Jaggie66edb12016-02-05 12:41:17 -08001956 return;
1957 }
1958
1959 // If this activity is paused, tell it to now show its window.
1960 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1961 "Making visible and scheduling visibility: " + r);
1962 try {
1963 if (mTranslucentActivityWaiting != null) {
1964 r.updateOptionsLocked(r.returningOptions);
1965 mUndrawnActivitiesBelowTopTranslucent.add(r);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001966 }
Jorim Jaggie66edb12016-02-05 12:41:17 -08001967 setVisible(r, true);
1968 r.sleeping = false;
1969 r.app.pendingUiClean = true;
1970 r.app.thread.scheduleWindowVisibility(r.appToken, true);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001971 // The activity may be waiting for stop, but that is no longer
1972 // appropriate for it.
1973 mStackSupervisor.mStoppingActivities.remove(r);
1974 mStackSupervisor.mGoingToSleepActivities.remove(r);
Jorim Jaggie66edb12016-02-05 12:41:17 -08001975 } catch (Exception e) {
1976 // Just skip on any failure; we'll make it
1977 // visible when it next restarts.
1978 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001979 }
Chong Zhang371c4422016-05-11 10:48:32 -07001980 handleAlreadyVisible(r);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001981 }
1982
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001983 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001984 r.stopFreezingScreenLocked(false);
1985 try {
1986 if (r.returningOptions != null) {
1987 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1988 }
1989 } catch(RemoteException e) {
1990 }
1991 return r.state == ActivityState.RESUMED;
1992 }
1993
Todd Kennedyaab56db2015-01-30 09:39:53 -08001994 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001995 mTranslucentActivityWaiting = r;
1996 mUndrawnActivitiesBelowTopTranslucent.clear();
1997 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1998 }
1999
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002000 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2001 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2002 final TaskRecord task = mTaskHistory.get(taskNdx);
2003 final ArrayList<ActivityRecord> activities = task.mActivities;
2004 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2005 final ActivityRecord r = activities.get(activityNdx);
2006 if ( r.appTimeTracker != except) {
2007 r.appTimeTracker = null;
2008 }
2009 }
2010 }
2011 }
2012
Craig Mautner5eda9b32013-07-02 11:58:16 -07002013 /**
2014 * Called as activities below the top translucent activity are redrawn. When the last one is
2015 * redrawn notify the top activity by calling
2016 * {@link Activity#onTranslucentConversionComplete}.
2017 *
2018 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
2019 * occurred and the activity will be notified immediately.
2020 */
2021 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07002022 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07002023 if ((r == null)
2024 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
2025 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
2026 // The last undrawn activity below the top has just been drawn. If there is an
2027 // opaque activity at the top, notify it that it can become translucent safely now.
2028 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
2029 mTranslucentActivityWaiting = null;
2030 mUndrawnActivitiesBelowTopTranslucent.clear();
2031 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
2032
Craig Mautner71dd1b62014-02-18 15:48:52 -08002033 if (waitingActivity != null) {
2034 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
2035 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
2036 try {
2037 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
2038 waitingActivity.appToken, r != null);
2039 } catch (RemoteException e) {
2040 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002041 }
2042 }
2043 }
2044 }
2045
Craig Mautnera61bc652013-10-28 15:43:18 -07002046 /** If any activities below the top running one are in the INITIALIZING state and they have a
2047 * starting window displayed then remove that starting window. It is possible that the activity
2048 * in this state will never resumed in which case that starting window will be orphaned. */
2049 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002050 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07002051 boolean aboveTop = true;
Wale Ogunwale68741142016-05-17 09:40:02 -07002052 // We don't want to clear starting window for activities that aren't behind fullscreen
2053 // activities as we need to display their starting window until they are done initializing.
2054 boolean behindFullscreenActivity = false;
Wale Ogunwale98742a52016-07-12 10:29:12 -07002055
2056 if (getStackVisibilityLocked(null) == STACK_INVISIBLE) {
2057 // The stack is not visible, so no activity in it should be displaying a starting
2058 // window. Mark all activities below top and behind fullscreen.
2059 aboveTop = false;
2060 behindFullscreenActivity = true;
2061 }
2062
Craig Mautnera61bc652013-10-28 15:43:18 -07002063 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2064 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2065 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2066 final ActivityRecord r = activities.get(activityNdx);
2067 if (aboveTop) {
2068 if (r == topActivity) {
2069 aboveTop = false;
2070 }
Wale Ogunwale68741142016-05-17 09:40:02 -07002071 behindFullscreenActivity |= r.fullscreen;
Craig Mautnera61bc652013-10-28 15:43:18 -07002072 continue;
2073 }
2074
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002075 if (r.state == ActivityState.INITIALIZING
Wale Ogunwale68741142016-05-17 09:40:02 -07002076 && r.mStartingWindowState == STARTING_WINDOW_SHOWN
2077 && behindFullscreenActivity) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002078 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
2079 "Found orphaned starting window " + r);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002080 r.mStartingWindowState = STARTING_WINDOW_REMOVED;
Craig Mautnera61bc652013-10-28 15:43:18 -07002081 mWindowManager.removeAppStartingWindow(r.appToken);
2082 }
Wale Ogunwale68741142016-05-17 09:40:02 -07002083
2084 behindFullscreenActivity |= r.fullscreen;
Craig Mautnera61bc652013-10-28 15:43:18 -07002085 }
2086 }
2087 }
2088
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002089 /**
2090 * Ensure that the top activity in the stack is resumed.
2091 *
2092 * @param prev The previously resumed activity, for when in the process
2093 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002094 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002095 *
2096 * @return Returns true if something is being resumed, or false if
2097 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002098 *
2099 * NOTE: It is not safe to call this method directly as it can cause an activity in a
2100 * non-focused stack to be resumed.
2101 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
2102 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002103 */
Wale Ogunwaled046a012015-12-24 13:05:59 -08002104 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08002105 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07002106 // Don't even start recursing.
2107 return false;
2108 }
2109
2110 boolean result = false;
2111 try {
2112 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08002113 mStackSupervisor.inResumeTopActivity = true;
2114 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
2115 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08002116 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08002117 }
Craig Mautner544efa72014-09-04 16:41:20 -07002118 result = resumeTopActivityInnerLocked(prev, options);
2119 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08002120 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07002121 }
2122 return result;
2123 }
2124
Chong Zhang280d3322015-11-03 17:27:26 -08002125 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002126 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07002127
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002128 if (!mService.mBooting && !mService.mBooted) {
2129 // Not ready yet!
2130 return false;
2131 }
2132
Craig Mautnerdf88d732014-01-27 09:21:32 -08002133 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002134 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07002135 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08002136 // Do not resume this stack if its parent is not resumed.
2137 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
2138 return false;
2139 }
2140
Wale Ogunwale2be760d2016-02-17 11:16:10 -08002141 mStackSupervisor.cancelInitializingActivities();
Craig Mautnera61bc652013-10-28 15:43:18 -07002142
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002143 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002144 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002145
2146 // Remember how we'll process this pause/resume situation, and ensure
2147 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002148 final boolean userLeaving = mStackSupervisor.mUserLeaving;
2149 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002150
Craig Mautner84984fa2014-06-19 11:19:20 -07002151 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002152 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002153 // There are no more activities!
2154 final String reason = "noMoreActivities";
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07002155 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack()
2156 ? HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
2157 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(returnTaskType, reason)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002158 // Try to move focus to the next visible stack with a running activity if this
2159 // stack is not covering the entire screen.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002160 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
2161 mStackSupervisor.getFocusedStack(), prev, null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002162 }
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002163
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002164 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07002165 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002166 if (DEBUG_STATES) Slog.d(TAG_STATES,
2167 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07002168 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08002169 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07002170 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002171 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002172 }
2173
2174 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08002175
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002176 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002177 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
2178 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002179 // Make sure we have executed any pending transitions, since there
2180 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002181 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002182 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07002183 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002184 if (DEBUG_STATES) Slog.d(TAG_STATES,
2185 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07002186 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002187 return false;
2188 }
2189
Craig Mautner525f3d92013-05-07 14:01:50 -07002190 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07002191 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07002192 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002193 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07002194 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002195 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07002196 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07002197 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07002198 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07002199 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07002200 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08002201 } else if (!isOnHomeDisplay()) {
2202 return false;
2203 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002204 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08002205 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07002206 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
2207 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
2208 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08002209 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07002210 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002211 }
2212
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002213 // If we are sleeping, and there is no resumed activity, and the top
2214 // activity is paused, well that is the state we want.
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002215 if (mService.isSleepingOrShuttingDownLocked()
p13451dbad2872012-04-18 11:39:23 +09002216 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07002217 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002218 // Make sure we have executed any pending transitions, since there
2219 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002220 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002221 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07002222 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002223 if (DEBUG_STATES) Slog.d(TAG_STATES,
2224 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07002225 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002226 return false;
2227 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002228
2229 // Make sure that the user who owns this activity is started. If not,
2230 // we will just leave it as is because someone should be bringing
2231 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002232 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002233 Slog.w(TAG, "Skipping resume of top activity " + next
2234 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07002235 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002236 return false;
2237 }
2238
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002239 // The activity may be waiting for stop, but that is no longer
2240 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002241 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002242 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002243 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002244 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002245
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002246 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002247
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002248 // If we are currently pausing an activity, then don't do anything until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07002249 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002250 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002251 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07002252 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002253 return false;
2254 }
2255
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002256 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2257
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002258 // We need to start pausing the current activity so the top one can be resumed...
2259 final boolean dontWaitForPause = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0;
Wale Ogunwale950faff2016-08-08 09:51:04 -07002260 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, next, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002261 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002262 if (DEBUG_STATES) Slog.d(TAG_STATES,
2263 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Wale Ogunwale950faff2016-08-08 09:51:04 -07002264 pausing |= startPausingLocked(userLeaving, false, next, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002265 }
2266 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002267 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002268 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002269 // At this point we want to put the upcoming activity's process
2270 // at the top of the LRU list, since we know we will be needing it
2271 // very soon and it would be a waste to let it get killed if it
2272 // happens to be sitting towards the end.
2273 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07002274 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002275 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002276 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002277 return true;
Wale Ogunwalecac5c322016-05-23 10:56:33 -07002278 } else if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
2279 mStackSupervisor.allResumedActivitiesComplete()) {
2280 // It is possible for the activity to be resumed when we paused back stacks above if the
2281 // next activity doesn't have to wait for pause to complete.
2282 // So, nothing else to-do except:
2283 // Make sure we have executed any pending transitions, since there
2284 // should be nothing left to do at this point.
2285 mWindowManager.executeAppTransition();
2286 mNoAnimActivities.clear();
2287 ActivityOptions.abort(options);
2288 if (DEBUG_STATES) Slog.d(TAG_STATES,
2289 "resumeTopActivityLocked: Top activity resumed (dontWaitForPause) " + next);
2290 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
2291 return true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002292 }
2293
Christopher Tated3f175c2012-06-14 14:16:54 -07002294 // If the most recent activity was noHistory but was only stopped rather
2295 // than stopped+finished because the device went to sleep, we need to make
2296 // sure to finish it as we're making a new activity topmost.
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07002297 if (mService.isSleepingLocked() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002298 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002299 if (DEBUG_STATES) Slog.d(TAG_STATES,
2300 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002301 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002302 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002303 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002304 }
2305
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002306 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002307 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2308 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002309 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002310 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2311 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002312 } else {
2313 // The next activity is already visible, so hide the previous
2314 // activity's windows right now so we can show the new one ASAP.
2315 // We only do this if the previous is finishing, which should mean
2316 // it is on top of the one being resumed so hiding it quickly
2317 // is good. Otherwise, we want to do the normal route of allowing
2318 // the resumed activity to be shown so we can decide if the
2319 // previous should actually be hidden depending on whether the
2320 // new one is found to be full-screen or not.
2321 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002322 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002323 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2324 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08002325 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002326 + ", nowVisible=" + next.nowVisible);
2327 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002328 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002329 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002330 + ", waitingVisible="
2331 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2332 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002333 }
2334 }
2335 }
2336
Dianne Hackborne7f97212011-02-24 14:40:20 -08002337 // Launching this app's activity, make sure the app is no longer
2338 // considered stopped.
2339 try {
2340 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002341 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002342 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002343 } catch (IllegalArgumentException e) {
2344 Slog.w(TAG, "Failed trying to unstop package "
2345 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002346 }
2347
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002348 // We are starting up the next activity, so tell the window manager
2349 // that the previous one will be hidden soon. This way it can know
2350 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002351 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002352 if (prev != null) {
2353 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002354 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002355 "Prepare close transition: prev=" + prev);
2356 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002357 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002358 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002359 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002360 mWindowManager.prepareAppTransition(prev.task == next.task
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002361 ? TRANSIT_ACTIVITY_CLOSE
2362 : TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002363 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002364 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002365 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002366 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2367 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002368 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002369 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002370 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002371 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002372 mWindowManager.prepareAppTransition(prev.task == next.task
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002373 ? TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002374 : next.mLaunchTaskBehind
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002375 ? TRANSIT_TASK_OPEN_BEHIND
2376 : TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002377 }
2378 }
Craig Mautner967212c2013-04-13 21:10:58 -07002379 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002380 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002381 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002382 anim = false;
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002383 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002384 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002385 mWindowManager.prepareAppTransition(TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002386 }
2387 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002388
2389 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002390 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002391 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2392 if (opts != null) {
2393 resumeAnimOptions = opts.toBundle();
2394 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002395 next.applyOptionsLocked();
2396 } else {
2397 next.clearOptionsLocked();
2398 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002399
Craig Mautnercf910b02013-04-23 11:23:27 -07002400 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002401 if (next.app != null && next.app.thread != null) {
Chong Zhangdea4bd92016-03-15 12:50:03 -07002402 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next
2403 + " stopped=" + next.stopped + " visible=" + next.visible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002404
Chong Zhangd127c6d2016-05-02 16:36:41 -07002405 // If the previous activity is translucent, force a visibility update of
2406 // the next activity, so that it's added to WM's opening app list, and
2407 // transition animation can be set up properly.
2408 // For example, pressing Home button with a translucent activity in focus.
2409 // Launcher is already visible in this case. If we don't add it to opening
2410 // apps, maybeUpdateTransitToWallpaper() will fail to identify this as a
2411 // TRANSIT_WALLPAPER_OPEN animation, and run some funny animation.
2412 final boolean lastActivityTranslucent = lastStack != null
2413 && (!lastStack.mFullscreen
2414 || (lastStack.mLastPausedActivity != null
2415 && !lastStack.mLastPausedActivity.fullscreen));
2416
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002417 // This activity is now becoming visible.
Chong Zhangd127c6d2016-05-02 16:36:41 -07002418 if (!next.visible || next.stopped || lastActivityTranslucent) {
Jorim Jaggi1b025a62016-02-03 19:27:49 -08002419 mWindowManager.setAppVisibility(next.appToken, true);
2420 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002421
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002422 // schedule launch ticks to collect information about slow apps.
2423 next.startLaunchTickingLocked();
2424
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002425 ActivityRecord lastResumedActivity =
2426 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002427 ActivityState lastState = next.state;
2428
2429 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002430
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002431 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002432 next.state = ActivityState.RESUMED;
2433 mResumedActivity = next;
2434 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002435 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002436 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002437 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002438 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002439
2440 // Have the window manager re-evaluate the orientation of
2441 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002442 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002443 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002444 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002445 mService.mConfiguration,
2446 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2447 if (config != null) {
2448 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002449 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002450 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002451 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002452
Craig Mautner525f3d92013-05-07 14:01:50 -07002453 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002454 // The configuration update wasn't able to keep the existing
2455 // instance of the activity, and instead started a new one.
2456 // We should be all done, but let's just make sure our activity
2457 // is still at the top and schedule another run if something
2458 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002459 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002460 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002461 "Activity config changed during resume: " + next
2462 + ", new next: " + nextNext);
2463 if (nextNext != next) {
2464 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002465 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002466 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002467 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002468 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002469 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002470 return true;
2471 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002472 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002473 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002474 }
Craig Mautner58547802013-03-05 08:23:53 -08002475
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002476 try {
2477 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002478 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002479 if (a != null) {
2480 final int N = a.size();
2481 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002482 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2483 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002484 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002485 }
2486 }
2487
2488 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002489 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002490 }
2491
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002492 // Well the app will no longer be stopped.
2493 // Clear app token stopped state in window manager if needed.
Chong Zhang45e6d2d2016-07-20 18:33:56 -07002494 mWindowManager.notifyAppResumed(next.appToken, next.stopped);
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002495
Craig Mautner299f9602015-01-26 09:47:33 -08002496 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2497 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002498
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002499 next.sleeping = false;
Alan Viverette5f31b982016-06-21 14:46:14 -04002500 mService.showUnsupportedZoomDialogIfNeededLocked(next);
Craig Mautner2420ead2013-04-01 17:13:20 -07002501 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002502 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002503 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002504 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002505 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002506 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002507
Craig Mautner0eea92c2013-05-16 13:35:39 -07002508 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002509
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002510 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002511 } catch (Exception e) {
2512 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002513 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002514 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002515 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002516 if (lastStack != null) {
2517 lastStack.mResumedActivity = lastResumedActivity;
2518 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002519 Slog.i(TAG, "Restarting because process died: " + next);
2520 if (!next.hasBeenLaunched) {
2521 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002522 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2523 mStackSupervisor.isFrontStack(lastStack)) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07002524 next.showStartingWindow(null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002525 }
George Mount2c92c972014-03-20 09:38:23 -07002526 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002527 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002528 return true;
2529 }
2530
2531 // From this point on, if something goes wrong there is no way
2532 // to recover the activity.
2533 try {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002534 completeResumeLocked(next);
2535 } catch (Exception e) {
2536 // If any exception gets thrown, toss away this
2537 // activity and try the next one.
2538 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002539 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002540 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002541 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002542 return true;
2543 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002544 } else {
2545 // Whoops, need to restart this activity!
2546 if (!next.hasBeenLaunched) {
2547 next.hasBeenLaunched = true;
2548 } else {
2549 if (SHOW_APP_STARTING_PREVIEW) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07002550 next.showStartingWindow(null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002551 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002552 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002553 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002554 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002555 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002556 }
2557
Craig Mautnercf910b02013-04-23 11:23:27 -07002558 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002559 return true;
2560 }
2561
riddle_hsuc215a4f2014-12-27 12:10:45 +08002562 private TaskRecord getNextTask(TaskRecord targetTask) {
2563 final int index = mTaskHistory.indexOf(targetTask);
2564 if (index >= 0) {
2565 final int numTasks = mTaskHistory.size();
2566 for (int i = index + 1; i < numTasks; ++i) {
2567 TaskRecord task = mTaskHistory.get(i);
2568 if (task.userId == targetTask.userId) {
2569 return task;
2570 }
2571 }
2572 }
2573 return null;
2574 }
2575
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002576 private void insertTaskAtPosition(TaskRecord task, int position) {
2577 if (position >= mTaskHistory.size()) {
2578 insertTaskAtTop(task, null);
2579 return;
2580 }
2581 // Calculate maximum possible position for this task.
2582 int maxPosition = mTaskHistory.size();
2583 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002584 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002585 // Put non-current user tasks below current user tasks.
2586 while (maxPosition > 0) {
2587 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2588 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002589 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002590 break;
2591 }
2592 maxPosition--;
2593 }
2594 }
2595 position = Math.min(position, maxPosition);
2596 mTaskHistory.remove(task);
2597 mTaskHistory.add(position, task);
2598 updateTaskMovement(task, true);
2599 }
2600
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002601 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
Andrii Kuliandc0f8932016-06-28 16:34:02 -07002602 boolean isLastTaskOverHome = false;
riddle_hsuc215a4f2014-12-27 12:10:45 +08002603 // If the moving task is over home stack, transfer its return type to next task
2604 if (task.isOverHomeStack()) {
2605 final TaskRecord nextTask = getNextTask(task);
2606 if (nextTask != null) {
2607 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
Andrii Kuliandc0f8932016-06-28 16:34:02 -07002608 } else {
2609 isLastTaskOverHome = true;
riddle_hsuc215a4f2014-12-27 12:10:45 +08002610 }
2611 }
2612
Craig Mautner9c85c202013-10-04 20:11:26 -07002613 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002614 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002615 if (isOnHomeDisplay()) {
2616 ActivityStack lastStack = mStackSupervisor.getLastStack();
2617 final boolean fromHome = lastStack.isHomeStack();
2618 if (!isHomeStack() && (fromHome || topTask() != task)) {
Andrii Kuliandc0f8932016-06-28 16:34:02 -07002619 // If it's a last task over home - we default to keep its return to type not to
2620 // make underlying task focused when this one will be finished.
2621 int returnToType = isLastTaskOverHome
2622 ? task.getTaskToReturnTo() : APPLICATION_ACTIVITY_TYPE;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002623 if (fromHome && StackId.allowTopTaskToReturnHome(mStackId)) {
2624 returnToType = lastStack.topTask() == null
2625 ? HOME_ACTIVITY_TYPE : lastStack.topTask().taskType;
2626 }
2627 task.setTaskToReturnTo(returnToType);
Craig Mautnere0a38842013-12-16 16:14:02 -08002628 }
2629 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002630 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002631 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002632
Craig Mautnerac6f8432013-07-17 13:24:59 -07002633 mTaskHistory.remove(task);
2634 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002635 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002636 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002637 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002638 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002639 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002640 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002641 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002642 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002643 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002644 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002645 break;
2646 }
2647 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002648 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002649 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002650 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002651 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002652 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002653
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002654 final void startActivityLocked(ActivityRecord r, boolean newTask, boolean keepCurTransition,
2655 ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002656 TaskRecord rTask = r.task;
2657 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002658 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2659 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002660 // Last activity in task had been removed or ActivityManagerService is reusing task.
2661 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002662 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002663 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002664 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002665 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002666 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002667 if (!newTask) {
2668 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002669 boolean startIt = true;
2670 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2671 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002672 if (task.getTopActivity() == null) {
2673 // All activities in task are finishing.
2674 continue;
2675 }
Craig Mautner70a86932013-02-28 22:37:44 -08002676 if (task == r.task) {
2677 // Here it is! Now, if this is not yet visible to the
2678 // user, then just add it without starting; it will
2679 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002680 if (!startIt) {
2681 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2682 + task, new RuntimeException("here").fillInStackTrace());
2683 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002684 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002685 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002686 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002687 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002688 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002689 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002690 return;
2691 }
2692 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002693 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002694 startIt = false;
2695 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002696 }
2697 }
2698
2699 // Place a new activity at top of stack, so it is next to interact
2700 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002701
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002702 // If we are not placing the new activity frontmost, we do not want
2703 // to deliver the onUserLeaving callback to the actual frontmost
2704 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002705 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002706 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002707 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002708 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002709 }
Craig Mautner70a86932013-02-28 22:37:44 -08002710
2711 task = r.task;
2712
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002713 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002714 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002715 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002716 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002717 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002718
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002719 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002720 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002721 // We want to show the starting preview window if we are
2722 // switching to a new task, or the next activity's process is
2723 // not currently running.
2724 boolean showStartingIcon = newTask;
2725 ProcessRecord proc = r.app;
2726 if (proc == null) {
2727 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2728 }
2729 if (proc == null || proc.thread == null) {
2730 showStartingIcon = true;
2731 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002732 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002733 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002734 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002735 mWindowManager.prepareAppTransition(TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002736 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002737 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002738 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002739 ? r.mLaunchTaskBehind
Jorim Jaggiaf80db42016-04-07 19:19:15 -07002740 ? TRANSIT_TASK_OPEN_BEHIND
2741 : TRANSIT_TASK_OPEN
2742 : TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002743 mNoAnimActivities.remove(r);
2744 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002745 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002746 boolean doShow = true;
2747 if (newTask) {
2748 // Even though this activity is starting fresh, we still need
2749 // to reset it to make sure we apply affinities to move any
2750 // existing activities from other tasks in to it.
2751 // If the caller has requested that the target task be
2752 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002753 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002754 resetTaskIfNeededLocked(r, r);
2755 doShow = topRunningNonDelayedActivityLocked(null) == r;
2756 }
Chong Zhang280d3322015-11-03 17:27:26 -08002757 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002758 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2759 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002760 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002761 if (r.mLaunchTaskBehind) {
2762 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2763 // tell WindowManager that r is visible even though it is at the back of the stack.
2764 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002765 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002766 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002767 // Figure out if we are transitioning from another activity that is
2768 // "has the same starting icon" as the next one. This allows the
2769 // window manager to keep the previous window it had previously
2770 // created, if it still had one.
Wale Ogunwale3b232392016-05-13 15:37:13 -07002771 ActivityRecord prev = r.task.topRunningActivityWithStartingWindowLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002772 if (prev != null) {
2773 // We don't want to reuse the previous starting preview if:
2774 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002775 if (prev.task != r.task) {
2776 prev = null;
2777 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002778 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002779 else if (prev.nowVisible) {
2780 prev = null;
2781 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002782 }
Wale Ogunwale3b232392016-05-13 15:37:13 -07002783 r.showStartingWindow(prev, showStartingIcon);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002784 }
2785 } else {
2786 // If this is the first activity, don't do any fancy animations,
2787 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002788 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002789 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002790 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002791 }
2792 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002793 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002794 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002795 }
2796
Dianne Hackbornbe707852011-11-11 14:32:10 -08002797 final void validateAppTokensLocked() {
2798 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002799 mValidateAppTokens.ensureCapacity(numActivities());
2800 final int numTasks = mTaskHistory.size();
2801 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2802 TaskRecord task = mTaskHistory.get(taskNdx);
2803 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002804 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002805 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002806 }
Craig Mautner000f0022013-02-26 15:04:29 -08002807 TaskGroup group = new TaskGroup();
2808 group.taskId = task.taskId;
2809 mValidateAppTokens.add(group);
2810 final int numActivities = activities.size();
2811 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2812 final ActivityRecord r = activities.get(activityNdx);
2813 group.tokens.add(r.appToken);
2814 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002815 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002816 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002817 }
2818
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002819 /**
2820 * Perform a reset of the given task, if needed as part of launching it.
2821 * Returns the new HistoryRecord at the top of the task.
2822 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002823 /**
2824 * Helper method for #resetTaskIfNeededLocked.
2825 * We are inside of the task being reset... we'll either finish this activity, push it out
2826 * for another task, or leave it as-is.
2827 * @param task The task containing the Activity (taskTop) that might be reset.
2828 * @param forceReset
2829 * @return An ActivityOptions that needs to be processed.
2830 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002831 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002832 ActivityOptions topOptions = null;
2833
2834 int replyChainEnd = -1;
2835 boolean canMoveOptions = true;
2836
2837 // We only do this for activities that are not the root of the task (since if we finish
2838 // the root, we may no longer have the task!).
2839 final ArrayList<ActivityRecord> activities = task.mActivities;
2840 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002841 final int rootActivityNdx = task.findEffectiveRootIndex();
2842 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002843 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002844 if (target.frontOfTask)
2845 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002846
2847 final int flags = target.info.flags;
2848 final boolean finishOnTaskLaunch =
2849 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2850 final boolean allowTaskReparenting =
2851 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2852 final boolean clearWhenTaskReset =
2853 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2854
2855 if (!finishOnTaskLaunch
2856 && !clearWhenTaskReset
2857 && target.resultTo != null) {
2858 // If this activity is sending a reply to a previous
2859 // activity, we can't do anything with it now until
2860 // we reach the start of the reply chain.
2861 // XXX note that we are assuming the result is always
2862 // to the previous activity, which is almost always
2863 // the case but we really shouldn't count on.
2864 if (replyChainEnd < 0) {
2865 replyChainEnd = i;
2866 }
2867 } else if (!finishOnTaskLaunch
2868 && !clearWhenTaskReset
2869 && allowTaskReparenting
2870 && target.taskAffinity != null
2871 && !target.taskAffinity.equals(task.affinity)) {
2872 // If this activity has an affinity for another
2873 // task, then we need to move it out of here. We will
2874 // move it as far out of the way as possible, to the
2875 // bottom of the activity stack. This also keeps it
2876 // correctly ordered with any activities we previously
2877 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002878 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002879 final ActivityRecord bottom =
2880 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002881 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002882 if (bottom != null && target.taskAffinity != null
2883 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002884 // If the activity currently at the bottom has the
2885 // same task affinity as the one we are moving,
2886 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002887 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002888 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002889 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002890 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002891 targetTask = createTaskRecord(
2892 mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
2893 target.info, null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002894 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002895 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002896 + " out to new task " + target.task);
2897 }
2898
Wale Ogunwale706ed792015-08-02 10:29:44 -07002899 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002900
Craig Mautner525f3d92013-05-07 14:01:50 -07002901 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002902 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2903 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002904 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002905 if (p.finishing) {
2906 continue;
2907 }
2908
Craig Mautnere3a74d52013-02-22 14:14:58 -08002909 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002910 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002911 topOptions = p.takeOptionsLocked();
2912 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002913 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002914 }
2915 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002916 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2917 "Removing activity " + p + " from task=" + task + " adding to task="
2918 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002919 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2920 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002921 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002922 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002923
Wale Ogunwale706ed792015-08-02 10:29:44 -07002924 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002925 }
2926
Wale Ogunwale706ed792015-08-02 10:29:44 -07002927 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002928 if (VALIDATE_TOKENS) {
2929 validateAppTokensLocked();
2930 }
2931
2932 replyChainEnd = -1;
2933 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2934 // If the activity should just be removed -- either
2935 // because it asks for it, or the task should be
2936 // cleared -- then finish it and anything that is
2937 // part of its reply chain.
2938 int end;
2939 if (clearWhenTaskReset) {
2940 // In this case, we want to finish this activity
2941 // and everything above it, so be sneaky and pretend
2942 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002943 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002944 } else if (replyChainEnd < 0) {
2945 end = i;
2946 } else {
2947 end = replyChainEnd;
2948 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002949 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002950 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002951 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002952 if (p.finishing) {
2953 continue;
2954 }
2955 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002956 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002957 topOptions = p.takeOptionsLocked();
2958 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002959 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002960 }
2961 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002962 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002963 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002964 if (finishActivityLocked(
2965 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002966 end--;
2967 srcPos--;
2968 }
2969 }
2970 replyChainEnd = -1;
2971 } else {
2972 // If we were in the middle of a chain, well the
2973 // activity that started it all doesn't want anything
2974 // special, so leave it all as-is.
2975 replyChainEnd = -1;
2976 }
2977 }
2978
2979 return topOptions;
2980 }
2981
2982 /**
2983 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2984 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2985 * @param affinityTask The task we are looking for an affinity to.
2986 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2987 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2988 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2989 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002990 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002991 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002992 int replyChainEnd = -1;
2993 final int taskId = task.taskId;
2994 final String taskAffinity = task.affinity;
2995
2996 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2997 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002998 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2999
3000 // Do not operate on or below the effective root Activity.
3001 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003002 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00003003 if (target.frontOfTask)
3004 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003005
3006 final int flags = target.info.flags;
3007 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
3008 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
3009
3010 if (target.resultTo != null) {
3011 // If this activity is sending a reply to a previous
3012 // activity, we can't do anything with it now until
3013 // we reach the start of the reply chain.
3014 // XXX note that we are assuming the result is always
3015 // to the previous activity, which is almost always
3016 // the case but we really shouldn't count on.
3017 if (replyChainEnd < 0) {
3018 replyChainEnd = i;
3019 }
3020 } else if (topTaskIsHigher
3021 && allowTaskReparenting
3022 && taskAffinity != null
3023 && taskAffinity.equals(target.taskAffinity)) {
3024 // This activity has an affinity for our task. Either remove it if we are
3025 // clearing or move it over to our task. Note that
3026 // we currently punt on the case where we are resetting a
3027 // task that is not at the top but who has activities above
3028 // with an affinity to it... this is really not a normal
3029 // case, and we will need to later pull that task to the front
3030 // and usually at that point we will do the reset and pick
3031 // up those remaining activities. (This only happens if
3032 // someone starts an activity in a new task from an activity
3033 // in a task that is not currently on top.)
3034 if (forceReset || finishOnTaskLaunch) {
3035 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003036 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
3037 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003038 for (int srcPos = start; srcPos >= i; --srcPos) {
3039 final ActivityRecord p = activities.get(srcPos);
3040 if (p.finishing) {
3041 continue;
3042 }
Todd Kennedy539db512014-12-15 09:57:55 -08003043 finishActivityLocked(
3044 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003045 }
3046 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003047 if (taskInsertionPoint < 0) {
3048 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08003049
Craig Mautner77878772013-03-04 19:46:24 -08003050 }
Craig Mautner77878772013-03-04 19:46:24 -08003051
3052 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003053 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
3054 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
3055 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08003056 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003057 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07003058 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08003059 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003060
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003061 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3062 "Removing and adding activity " + p + " to stack at " + task
3063 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003064 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
3065 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003066 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003067 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003068 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08003069 if (VALIDATE_TOKENS) {
3070 validateAppTokensLocked();
3071 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08003072
3073 // Now we've moved it in to place... but what if this is
3074 // a singleTop activity and we have put it on top of another
3075 // instance of the same activity? Then we drop the instance
3076 // below so it remains singleTop.
3077 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
3078 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003079 int targetNdx = taskActivities.indexOf(target);
3080 if (targetNdx > 0) {
3081 ActivityRecord p = taskActivities.get(targetNdx - 1);
3082 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08003083 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
3084 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003085 }
3086 }
3087 }
3088 }
3089
3090 replyChainEnd = -1;
3091 }
3092 }
Craig Mautner77878772013-03-04 19:46:24 -08003093 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003094 }
3095
Craig Mautner8849a5e2013-04-02 16:41:03 -07003096 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08003097 ActivityRecord newActivity) {
3098 boolean forceReset =
3099 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
3100 if (ACTIVITY_INACTIVE_RESET_TIME > 0
3101 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
3102 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
3103 forceReset = true;
3104 }
3105 }
3106
3107 final TaskRecord task = taskTop.task;
3108
3109 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
3110 * for remaining tasks. Used for later tasks to reparent to task. */
3111 boolean taskFound = false;
3112
3113 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
3114 ActivityOptions topOptions = null;
3115
Craig Mautner77878772013-03-04 19:46:24 -08003116 // Preserve the location for reparenting in the new task.
3117 int reparentInsertionPoint = -1;
3118
Craig Mautnere3a74d52013-02-22 14:14:58 -08003119 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
3120 final TaskRecord targetTask = mTaskHistory.get(i);
3121
3122 if (targetTask == task) {
3123 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
3124 taskFound = true;
3125 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003126 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
3127 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003128 }
3129 }
3130
Craig Mautner70a86932013-02-28 22:37:44 -08003131 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08003132 if (taskNdx >= 0) {
3133 do {
3134 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
3135 } while (taskTop == null && taskNdx >= 0);
3136 }
Craig Mautner70a86932013-02-28 22:37:44 -08003137
Craig Mautnere3a74d52013-02-22 14:14:58 -08003138 if (topOptions != null) {
3139 // If we got some ActivityOptions from an activity on top that
3140 // was removed from the task, propagate them to the new real top.
3141 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003142 taskTop.updateOptionsLocked(topOptions);
3143 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003144 topOptions.abort();
3145 }
3146 }
3147
3148 return taskTop;
3149 }
3150
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003151 void sendActivityResultLocked(int callingUid, ActivityRecord r,
3152 String resultWho, int requestCode, int resultCode, Intent data) {
3153
3154 if (callingUid > 0) {
3155 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003156 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003157 }
3158
3159 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
3160 + " : who=" + resultWho + " req=" + requestCode
3161 + " res=" + resultCode + " data=" + data);
3162 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
3163 try {
3164 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
3165 list.add(new ResultInfo(resultWho, requestCode,
3166 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08003167 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003168 return;
3169 } catch (Exception e) {
3170 Slog.w(TAG, "Exception thrown sending result to " + r, e);
3171 }
3172 }
3173
3174 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
3175 }
3176
Craig Mautner299f9602015-01-26 09:47:33 -08003177 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003178 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
3179 return;
3180 }
3181
3182 final ActivityRecord next = topRunningActivityLocked();
3183 final String myReason = reason + " adjustFocus";
3184 if (next != r) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003185 if (next != null && StackId.keepFocusInStackIfPossible(mStackId) && isFocusable()) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003186 // For freeform, docked, and pinned stacks we always keep the focus within the
3187 // stack as long as there is a running activity in the stack that we can adjust
3188 // focus to.
3189 mService.setFocusedActivityLocked(next, myReason);
3190 return;
3191 } else {
3192 final TaskRecord task = r.task;
3193 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003194 final int taskToReturnTo = task.getTaskToReturnTo();
3195
Wale Ogunwaled045c822015-12-02 09:14:28 -08003196 // For non-fullscreen stack, we want to move the focus to the next visible
3197 // stack to prevent the home screen from moving to the top and obscuring
3198 // other visible stacks.
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003199 if (!mFullscreen
3200 && adjustFocusToNextFocusableStackLocked(taskToReturnTo, myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003201 return;
3202 }
3203 // Move the home stack to the top if this stack is fullscreen or there is no
3204 // other visible stack.
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003205 if (mStackSupervisor.moveHomeStackTaskToTop(taskToReturnTo, myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003206 // Activity focus was already adjusted. Nothing else to do...
3207 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003208 }
Craig Mautner04f0b702013-10-22 12:31:01 -07003209 }
3210 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08003211 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003212
Wale Ogunwaled046a012015-12-24 13:05:59 -08003213 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07003214 }
3215
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003216 private boolean adjustFocusToNextFocusableStackLocked(int taskToReturnTo, String reason) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003217 final ActivityStack stack = getNextFocusableStackLocked();
3218 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003219 if (stack == null) {
3220 return false;
3221 }
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003222
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003223 final ActivityRecord top = stack.topRunningActivityLocked();
3224
3225 if (stack.isHomeStack() && (top == null || !top.visible)) {
3226 // If we will be focusing on the home stack next and its current top activity isn't
3227 // visible, then use the task return to value to determine the home task to display next.
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003228 return mStackSupervisor.moveHomeStackTaskToTop(taskToReturnTo, reason);
3229 }
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003230 return mService.setFocusedActivityLocked(top, myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003231 }
3232
Craig Mautnerf3333272013-04-22 10:55:53 -07003233 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003234 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003235 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3236 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3237 if (!r.finishing) {
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003238 if (!mService.isSleepingLocked()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003239 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003240 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
3241 "stop-no-history", false)) {
3242 // Activity was finished, no need to continue trying to schedule stop.
3243 adjustFocusedActivityLocked(r, "stopActivityFinished");
3244 r.resumeKeyDispatchingLocked();
3245 return;
3246 }
Christopher Tated3f175c2012-06-14 14:16:54 -07003247 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003248 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07003249 + " on stop because we're just sleeping");
3250 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003251 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003252 }
3253
3254 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08003255 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003256 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003257 try {
3258 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003259 if (DEBUG_STATES) Slog.v(TAG_STATES,
3260 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003261 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003262 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3263 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003264 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003265 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003266 }
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003267 EventLogTags.writeAmStopActivity(
3268 r.userId, System.identityHashCode(r), r.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003269 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Fyodor Kupolov9b80b942016-06-16 16:29:05 -07003270 if (mService.isSleepingOrShuttingDownLocked()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003271 r.setSleeping(true);
3272 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003273 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003274 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003275 } catch (Exception e) {
3276 // Maybe just ignore exceptions here... if the process
3277 // has crashed, our death notification will clean things
3278 // up.
3279 Slog.w(TAG, "Exception thrown during pause", e);
3280 // Just in case, assume it to be stopped.
3281 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003282 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003283 r.state = ActivityState.STOPPED;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003284 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003285 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003286 }
3287 }
3288 }
3289 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003290
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003291 /**
3292 * @return Returns true if the activity is being finished, false if for
3293 * some reason it is being left as-is.
3294 */
3295 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003296 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003297 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003298 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3299 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003300 + ", result=" + resultCode + ", data=" + resultData
3301 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003302 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003303 return false;
3304 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003305
Craig Mautnerd44711d2013-02-23 11:24:36 -08003306 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003307 return true;
3308 }
3309
Craig Mautnerd2328952013-03-05 12:46:26 -08003310 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003311 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3312 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3313 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3314 ActivityRecord r = activities.get(activityNdx);
3315 if (r.resultTo == self && r.requestCode == requestCode) {
3316 if ((r.resultWho == null && resultWho == null) ||
3317 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3318 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3319 false);
3320 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003321 }
3322 }
3323 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003324 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003325 }
3326
Adrian Roos20d7df32016-01-12 18:59:43 +01003327 final TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003328 ActivityRecord r = topRunningActivityLocked();
Adrian Roos20d7df32016-01-12 18:59:43 +01003329 TaskRecord finishedTask = null;
3330 if (r == null || r.app != app) {
3331 return null;
3332 }
3333 Slog.w(TAG, " Force finishing activity "
3334 + r.intent.getComponent().flattenToShortString());
3335 int taskNdx = mTaskHistory.indexOf(r.task);
3336 int activityNdx = r.task.mActivities.indexOf(r);
3337 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
3338 finishedTask = r.task;
3339 // Also terminate any activities below it that aren't yet
3340 // stopped, to avoid a situation where one will get
3341 // re-start our crashing activity once it gets resumed again.
3342 --activityNdx;
3343 if (activityNdx < 0) {
3344 do {
3345 --taskNdx;
3346 if (taskNdx < 0) {
3347 break;
3348 }
3349 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3350 } while (activityNdx < 0);
3351 }
3352 if (activityNdx >= 0) {
3353 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
3354 if (r.state == ActivityState.RESUMED
3355 || r.state == ActivityState.PAUSING
3356 || r.state == ActivityState.PAUSED) {
3357 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
3358 Slog.w(TAG, " Force finishing activity "
3359 + r.intent.getComponent().flattenToShortString());
3360 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003361 }
3362 }
3363 }
Adrian Roos20d7df32016-01-12 18:59:43 +01003364 return finishedTask;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003365 }
3366
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003367 final void finishVoiceTask(IVoiceInteractionSession session) {
3368 IBinder sessionBinder = session.asBinder();
3369 boolean didOne = false;
3370 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3371 TaskRecord tr = mTaskHistory.get(taskNdx);
3372 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3373 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3374 ActivityRecord r = tr.mActivities.get(activityNdx);
3375 if (!r.finishing) {
3376 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3377 false);
3378 didOne = true;
3379 }
3380 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003381 } else {
3382 // Check if any of the activities are using voice
3383 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3384 ActivityRecord r = tr.mActivities.get(activityNdx);
3385 if (r.voiceSession != null
3386 && r.voiceSession.asBinder() == sessionBinder) {
3387 // Inform of cancellation
3388 r.clearVoiceSessionLocked();
3389 try {
3390 r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3391 null);
3392 } catch (RemoteException re) {
3393 // Ok
3394 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003395 mService.finishRunningVoiceLocked();
3396 break;
3397 }
3398 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003399 }
3400 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003401
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003402 if (didOne) {
3403 mService.updateOomAdjLocked();
3404 }
3405 }
3406
Craig Mautnerd2328952013-03-05 12:46:26 -08003407 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003408 ArrayList<ActivityRecord> activities = r.task.mActivities;
3409 for (int index = activities.indexOf(r); index >= 0; --index) {
3410 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003411 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003412 break;
3413 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003414 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003415 }
3416 return true;
3417 }
3418
Dianne Hackborn5c607432012-02-28 14:44:19 -08003419 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3420 // send the result
3421 ActivityRecord resultTo = r.resultTo;
3422 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003423 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003424 + " who=" + r.resultWho + " req=" + r.requestCode
3425 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003426 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003427 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003428 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003429 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003430 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003431 if (r.info.applicationInfo.uid > 0) {
3432 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3433 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003434 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003435 }
3436 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3437 resultData);
3438 r.resultTo = null;
3439 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003440 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003441
3442 // Make sure this HistoryRecord is not holding on to other resources,
3443 // because clients have remote IPC references to this object so we
3444 // can't assume that will go away and want to avoid circular IPC refs.
3445 r.results = null;
3446 r.pendingResults = null;
3447 r.newIntents = null;
3448 r.icicle = null;
3449 }
3450
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003451 /**
3452 * @return Returns true if this activity has been removed from the history
3453 * list, or false if it is still in the list and will be removed later.
3454 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003455 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3456 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003457 if (r.finishing) {
3458 Slog.w(TAG, "Duplicate finish request for " + r);
3459 return false;
3460 }
3461
Wale Ogunwale7d701172015-03-11 15:36:30 -07003462 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003463 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003464 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003465 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003466 task.taskId, r.shortComponentName, reason);
3467 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003468 final int index = activities.indexOf(r);
3469 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003470 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003471 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003472 // If the caller asked that this activity (and all above it)
3473 // be cleared when the task is reset, don't lose that information,
3474 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003475 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003476 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003477 }
3478 }
3479
3480 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003481
Craig Mautner299f9602015-01-26 09:47:33 -08003482 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003483
Dianne Hackborn5c607432012-02-28 14:44:19 -08003484 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003485
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003486 final boolean endTask = index <= 0;
3487 final int transit = endTask ? TRANSIT_TASK_CLOSE : TRANSIT_ACTIVITY_CLOSE;
Craig Mautnerde4ef022013-04-07 19:01:33 -07003488 if (mResumedActivity == r) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003489
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003490 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003491 "Prepare close transition: finishing " + r);
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003492 mWindowManager.prepareAppTransition(transit, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003493
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003494 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003495 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003496
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003497 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003498 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3499 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3500 "finish() => pause with userLeaving=false");
Wale Ogunwale950faff2016-08-08 09:51:04 -07003501 startPausingLocked(false, false, null, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003502 }
3503
Craig Mautneraea74a52014-03-08 14:23:10 -08003504 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003505 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003506 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003507 } else if (r.state != ActivityState.PAUSING) {
3508 // If the activity is PAUSING, we will complete the finish once
3509 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003510 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003511 if (r.visible) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003512 mWindowManager.prepareAppTransition(transit, false);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003513 mWindowManager.setAppVisibility(r.appToken, false);
Jorim Jaggiaf80db42016-04-07 19:19:15 -07003514 mWindowManager.executeAppTransition();
Chong Zhangb8bd0692016-04-27 17:29:28 -07003515 if (!mStackSupervisor.mWaitingVisibleActivities.contains(r)) {
3516 mStackSupervisor.mWaitingVisibleActivities.add(r);
3517 }
Chong Zhang66ed4c62015-11-12 14:56:28 -08003518 }
Chong Zhang4c5f7ff2016-04-22 11:31:51 -07003519 return finishCurrentActivityLocked(r, (r.visible || r.nowVisible) ?
3520 FINISH_AFTER_VISIBLE : FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003521 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003522 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003523 }
3524
3525 return false;
3526 }
3527
Craig Mautnerf3333272013-04-22 10:55:53 -07003528 static final int FINISH_IMMEDIATELY = 0;
3529 static final int FINISH_AFTER_PAUSE = 1;
3530 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003531
Craig Mautnerf3333272013-04-22 10:55:53 -07003532 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003533 // First things first: if this activity is currently visible,
3534 // and the resumed activity is not yet visible, then hold off on
3535 // finishing until the resumed one becomes visible.
Chong Zhang824b6dc2016-04-27 14:11:12 -07003536
3537 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
3538
Chong Zhangefd9a5b2016-04-26 16:21:07 -07003539 if (mode == FINISH_AFTER_VISIBLE && (r.visible || r.nowVisible)
Chong Zhang824b6dc2016-04-27 14:11:12 -07003540 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003541 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08003542 addToStopping(r, false /* immediate */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003543 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003544 if (DEBUG_STATES) Slog.v(TAG_STATES,
3545 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003546 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003547 if (oomAdj) {
3548 mService.updateOomAdjLocked();
3549 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003550 return r;
3551 }
3552
3553 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003554 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003555 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003556 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003557 if (mResumedActivity == r) {
3558 mResumedActivity = null;
3559 }
3560 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003561 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003562 r.state = ActivityState.FINISHING;
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003563 final boolean finishingActivityInNonFocusedStack
3564 = r.task.stack != mStackSupervisor.getFocusedStack()
3565 && prevState == ActivityState.PAUSED && mode == FINISH_AFTER_VISIBLE;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003566
3567 if (mode == FINISH_IMMEDIATELY
Wale Ogunwaleae30f302016-05-02 10:35:45 -07003568 || (prevState == ActivityState.PAUSED
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003569 && (mode == FINISH_AFTER_PAUSE || mStackId == PINNED_STACK_ID))
3570 || finishingActivityInNonFocusedStack
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003571 || prevState == ActivityState.STOPPED
3572 || prevState == ActivityState.INITIALIZING) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003573 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003574 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Andrii Kulian7318d632016-07-20 18:59:28 -07003575
Andrii Kulian995fa2b2016-07-29 12:55:41 -07003576 if (finishingActivityInNonFocusedStack) {
3577 // Finishing activity that was in paused state and it was in not currently focused
3578 // stack, need to make something visible in its place.
Andrii Kulian7318d632016-07-20 18:59:28 -07003579 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3580 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003581 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003582 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003583 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003584 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003585 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3586 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003587 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003588 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003589
3590 // Need to go through the full pause cycle to get this
3591 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003592 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003593 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003594 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003595 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003596 return r;
3597 }
3598
Craig Mautneree36c772014-07-16 14:56:05 -07003599 void finishAllActivitiesLocked(boolean immediately) {
3600 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003601 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3602 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3603 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3604 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003605 noActivitiesInStack = false;
3606 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003607 continue;
3608 }
Craig Mautneree36c772014-07-16 14:56:05 -07003609 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003610 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3611 }
3612 }
Craig Mautneree36c772014-07-16 14:56:05 -07003613 if (noActivitiesInStack) {
3614 mActivityContainer.onTaskListEmptyLocked();
3615 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003616 }
3617
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003618 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3619 // Basic case: for simple app-centric recents, we need to recreate
3620 // the task if the affinity has changed.
3621 if (srec == null || srec.task.affinity == null ||
3622 !srec.task.affinity.equals(destAffinity)) {
3623 return true;
3624 }
3625 // Document-centric case: an app may be split in to multiple documents;
3626 // they need to re-create their task if this current activity is the root
3627 // of a document, unless simply finishing it will return them to the the
3628 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003629 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3630 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003631 // Okay, this activity is at the root of its task. What to do, what to do...
3632 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3633 // Finishing won't return to an application, so we need to recreate.
3634 return true;
3635 }
3636 // We now need to get the task below it to determine what to do.
3637 int taskIdx = mTaskHistory.indexOf(srec.task);
3638 if (taskIdx <= 0) {
3639 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3640 return false;
3641 }
3642 if (taskIdx == 0) {
3643 // At the bottom of the stack, nothing to go back to.
3644 return true;
3645 }
3646 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3647 if (!srec.task.affinity.equals(prevTask.affinity)) {
3648 // These are different apps, so need to recreate.
3649 return true;
3650 }
3651 }
3652 return false;
3653 }
3654
Wale Ogunwale7d701172015-03-11 15:36:30 -07003655 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003656 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003657 final TaskRecord task = srec.task;
3658 final ArrayList<ActivityRecord> activities = task.mActivities;
3659 final int start = activities.indexOf(srec);
3660 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003661 return false;
3662 }
3663 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003664 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003665 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003666 final ComponentName dest = destIntent.getComponent();
3667 if (start > 0 && dest != null) {
3668 for (int i = finishTo; i >= 0; i--) {
3669 ActivityRecord r = activities.get(i);
3670 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003671 r.info.name.equals(dest.getClassName())) {
3672 finishTo = i;
3673 parent = r;
3674 foundParentInTask = true;
3675 break;
3676 }
3677 }
3678 }
3679
3680 IActivityController controller = mService.mController;
3681 if (controller != null) {
3682 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3683 if (next != null) {
3684 // ask watcher if this is allowed
3685 boolean resumeOK = true;
3686 try {
3687 resumeOK = controller.activityResuming(next.packageName);
3688 } catch (RemoteException e) {
3689 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003690 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003691 }
3692
3693 if (!resumeOK) {
3694 return false;
3695 }
3696 }
3697 }
3698 final long origId = Binder.clearCallingIdentity();
3699 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003700 ActivityRecord r = activities.get(i);
3701 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003702 // Only return the supplied result for the first activity finished
3703 resultCode = Activity.RESULT_CANCELED;
3704 resultData = null;
3705 }
3706
3707 if (parent != null && foundParentInTask) {
3708 final int parentLaunchMode = parent.info.launchMode;
3709 final int destIntentFlags = destIntent.getFlags();
3710 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3711 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3712 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3713 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003714 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3715 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003716 } else {
3717 try {
3718 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3719 destIntent.getComponent(), 0, srec.userId);
Filip Gruszczynski303210b2016-01-08 16:28:08 -08003720 int res = mService.mActivityStarter.startActivityLocked(srec.app.thread,
3721 destIntent, null /*ephemeralIntent*/, null, aInfo, null /*rInfo*/, null,
3722 null, parent.appToken, null, 0, -1, parent.launchedFromUid,
Todd Kennedy7440f172015-12-09 14:31:22 -08003723 parent.launchedFromPackage, -1, parent.launchedFromUid, 0, null,
3724 false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003725 foundParentInTask = res == ActivityManager.START_SUCCESS;
3726 } catch (RemoteException e) {
3727 foundParentInTask = false;
3728 }
3729 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003730 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003731 }
3732 }
3733 Binder.restoreCallingIdentity(origId);
3734 return foundParentInTask;
3735 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003736 /**
3737 * Perform the common clean-up of an activity record. This is called both
3738 * as part of destroyActivityLocked() (when destroying the client-side
3739 * representation) and cleaning things up as a result of its hosting
3740 * processing going away, in which case there is no remaining client-side
3741 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003742 *
3743 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003744 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003745 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3746 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003747 if (mResumedActivity == r) {
3748 mResumedActivity = null;
3749 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003750 if (mPausingActivity == r) {
3751 mPausingActivity = null;
3752 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003753 mService.resetFocusedActivityIfNeededLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003754
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003755 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003756 r.frozenBeforeDestroy = false;
3757
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003758 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003759 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003760 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003761 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003762 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003763 }
3764
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003765 // Make sure this record is no longer in the pending finishes list.
3766 // This could happen, for example, if we are trimming activities
3767 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003768 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003769 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003770
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003771 // Remove any pending results.
3772 if (r.finishing && r.pendingResults != null) {
3773 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3774 PendingIntentRecord rec = apr.get();
3775 if (rec != null) {
3776 mService.cancelIntentSenderLocked(rec, false);
3777 }
3778 }
3779 r.pendingResults = null;
3780 }
3781
3782 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003783 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003784 }
3785
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003786 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003787 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003788 if (getVisibleBehindActivity() == r) {
3789 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003790 }
Wale Ogunwale8fd75422016-06-24 14:20:37 -07003791
3792 // Clean-up activities are no longer relaunching (e.g. app process died). Notify window
3793 // manager so it can update its bookkeeping.
3794 mWindowManager.notifyAppRelaunchesCleared(r.appToken);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003795 }
3796
3797 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003798 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003799 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003800 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003801 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003802 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003803 }
3804
Chong Zhange4cf36f2016-06-03 14:38:33 -07003805 private void removeActivityFromHistoryLocked(
3806 ActivityRecord r, TaskRecord oldTop, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003807 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003808 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003809 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003810 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3811 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3812
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003813 r.takeFromHistory();
3814 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003815 if (DEBUG_STATES) Slog.v(TAG_STATES,
3816 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003817 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003818 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003819 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003820 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003821 if (VALIDATE_TOKENS) {
3822 validateAppTokensLocked();
3823 }
Craig Mautner312ba862014-02-10 17:55:01 -08003824 final TaskRecord task = r.task;
Chong Zhange4cf36f2016-06-03 14:38:33 -07003825 final TaskRecord topTask = oldTop != null ? oldTop : topTask();
Craig Mautner312ba862014-02-10 17:55:01 -08003826 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003827 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003828 "removeActivityFromHistoryLocked: last activity removed from " + this);
Chong Zhange4cf36f2016-06-03 14:38:33 -07003829 if (mStackSupervisor.isFocusedStack(this) && task == topTask &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003830 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003831 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003832 }
Craig Mautner299f9602015-01-26 09:47:33 -08003833 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003834 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003835 cleanUpActivityServicesLocked(r);
3836 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003837 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003838
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003839 /**
3840 * Perform clean-up of service connections in an activity record.
3841 */
3842 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3843 // Throw away any services that have been bound by this activity.
3844 if (r.connections != null) {
3845 Iterator<ConnectionRecord> it = r.connections.iterator();
3846 while (it.hasNext()) {
3847 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003848 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003849 }
3850 r.connections = null;
3851 }
3852 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003853
Craig Mautneree2e45a2014-06-27 12:10:03 -07003854 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003855 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003856 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003857 mHandler.sendMessage(msg);
3858 }
3859
Craig Mautneree2e45a2014-06-27 12:10:03 -07003860 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003861 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003862 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003863 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3864 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3865 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3866 final ActivityRecord r = activities.get(activityNdx);
3867 if (r.finishing) {
3868 continue;
3869 }
3870 if (r.fullscreen) {
3871 lastIsOpaque = true;
3872 }
3873 if (owner != null && r.app != owner) {
3874 continue;
3875 }
3876 if (!lastIsOpaque) {
3877 continue;
3878 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003879 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003880 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003881 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003882 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003883 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003884 activityRemoved = true;
3885 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003886 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003887 }
3888 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003889 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003890 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003891 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003892 }
3893
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003894 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3895 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003896 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3897 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003898 + " pausing=" + mPausingActivity + " for reason " + reason);
3899 return destroyActivityLocked(r, true, reason);
3900 }
3901 return false;
3902 }
3903
3904 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3905 String reason) {
3906 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003907 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003908 int maxTasks = tasks.size() / 4;
3909 if (maxTasks < 1) {
3910 maxTasks = 1;
3911 }
3912 int numReleased = 0;
3913 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3914 final TaskRecord task = mTaskHistory.get(taskNdx);
3915 if (!tasks.contains(task)) {
3916 continue;
3917 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003918 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003919 int curNum = 0;
3920 final ArrayList<ActivityRecord> activities = task.mActivities;
3921 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3922 final ActivityRecord activity = activities.get(actNdx);
3923 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003924 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003925 + " in state " + activity.state + " resumed=" + mResumedActivity
3926 + " pausing=" + mPausingActivity + " for reason " + reason);
3927 destroyActivityLocked(activity, true, reason);
3928 if (activities.get(actNdx) != activity) {
3929 // Was removed from list, back up so we don't miss the next one.
3930 actNdx--;
3931 }
3932 curNum++;
3933 }
3934 }
3935 if (curNum > 0) {
3936 numReleased += curNum;
3937 maxTasks--;
3938 if (mTaskHistory.get(taskNdx) != task) {
3939 // The entire task got removed, back up so we don't miss the next one.
3940 taskNdx--;
3941 }
3942 }
3943 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003944 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3945 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003946 return numReleased;
3947 }
3948
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003949 /**
3950 * Destroy the current CLIENT SIDE instance of an activity. This may be
3951 * called both when actually finishing an activity, or when performing
3952 * a configuration switch where we destroy the current client-side object
3953 * but then create a new client-side object for this same HistoryRecord.
3954 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003955 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003956 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3957 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003958 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003959 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003960 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003961 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003962
3963 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003964
Chong Zhange4cf36f2016-06-03 14:38:33 -07003965 // If the activity is finishing, it's no longer considered in topRunningActivityLocked,
3966 // and cleanUpActivityLocked() may change focus to another activity (or task).
3967 // Get the current top task now, as removeActivityFromHistoryLocked() below need this
3968 // to decide whether to return to home stack after removal.
3969 final TaskRecord topTask = topTask();
3970
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003971 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003972
3973 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003974
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003975 if (hadApp) {
3976 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003977 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003978 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3979 mService.mHeavyWeightProcess = null;
3980 mService.mHandler.sendEmptyMessage(
3981 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3982 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003983 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003984 // Update any services we are bound to that might care about whether
3985 // their client may have activities.
3986 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003987 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003988 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003989 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003990 }
3991 }
3992
3993 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003994
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003995 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003996 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003997 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003998 r.configChangeFlags);
3999 } catch (Exception e) {
4000 // We can just ignore exceptions here... if the process
4001 // has crashed, our death notification will clean things
4002 // up.
4003 //Slog.w(TAG, "Exception thrown during finish", e);
4004 if (r.finishing) {
Chong Zhange4cf36f2016-06-03 14:38:33 -07004005 removeActivityFromHistoryLocked(
4006 r, topTask, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004007 removedFromHistory = true;
4008 skipDestroy = true;
4009 }
4010 }
4011
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004012 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004013
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004014 // If the activity is finishing, we need to wait on removing it
4015 // from the list to give it a chance to do its cleanup. During
4016 // that time it may make calls back with its token so we need to
4017 // be able to find it on the list and so we don't want to remove
4018 // it from the list yet. Otherwise, we can just immediately put
4019 // it in the destroyed state since we are not removing it from the
4020 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004021 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004022 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004023 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004024 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07004025 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004026 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
4027 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004028 if (DEBUG_STATES) Slog.v(TAG_STATES,
4029 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004030 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004031 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004032 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004033 }
4034 } else {
4035 // remove this record from the history.
4036 if (r.finishing) {
Chong Zhange4cf36f2016-06-03 14:38:33 -07004037 removeActivityFromHistoryLocked(r, topTask, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004038 removedFromHistory = true;
4039 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004040 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004041 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004042 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004043 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004044 }
4045 }
4046
4047 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004048
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004049 if (!mLRUActivities.remove(r) && hadApp) {
4050 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
4051 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004052
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004053 return removedFromHistory;
4054 }
4055
Craig Mautner299f9602015-01-26 09:47:33 -08004056 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004057 final long origId = Binder.clearCallingIdentity();
4058 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004059 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08004060 if (r != null) {
4061 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004062 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004063 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08004064
Wale Ogunwale60454db2015-01-23 16:05:07 -08004065 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004066 if (r.state == ActivityState.DESTROYING) {
4067 cleanUpActivityLocked(r, true, false);
Chong Zhange4cf36f2016-06-03 14:38:33 -07004068 removeActivityFromHistoryLocked(r, null, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08004069 }
4070 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004071 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08004072 } finally {
4073 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004074 }
4075 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004076
Todd Kennedyaab56db2015-01-30 09:39:53 -08004077 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07004078 if (hasVisibleBehindActivity() &&
4079 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08004080 if (r == topRunningActivityLocked()
4081 && getStackVisibilityLocked(null) == STACK_VISIBLE) {
Craig Mautner64ccb702014-10-01 09:38:40 -07004082 // Don't release the top activity if it has requested to run behind the next
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08004083 // activity and the stack is currently visible.
Craig Mautner64ccb702014-10-01 09:38:40 -07004084 return;
4085 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004086 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07004087 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07004088 " thread=" + r.app.thread);
4089 if (r != null && r.app != null && r.app.thread != null) {
4090 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07004091 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004092 } catch (RemoteException e) {
4093 }
Jose Lima4b6c6692014-08-12 17:41:12 -07004094 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004095 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07004096 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08004097 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004098 }
4099 }
4100 }
4101
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08004102 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07004103 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
4104 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004105 if (r != null) {
4106 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07004107 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08004108 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004109 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004110 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004111 }
4112
Jose Lima4b6c6692014-08-12 17:41:12 -07004113 boolean hasVisibleBehindActivity() {
4114 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07004115 }
4116
Jose Lima4b6c6692014-08-12 17:41:12 -07004117 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07004118 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07004119 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004120 }
4121 }
4122
Jose Lima4b6c6692014-08-12 17:41:12 -07004123 ActivityRecord getVisibleBehindActivity() {
4124 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004125 }
4126
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004127 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
4128 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004129 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004130 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4131 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004132 while (i > 0) {
4133 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004134 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004135 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004136 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004137 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004138 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004139 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004140 }
4141 }
4142 }
4143
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004144 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
4145 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004146 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
4147 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07004148 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
4149 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004150 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004151 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07004152 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
4153 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004154
4155 boolean hasVisibleActivities = false;
4156
4157 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08004158 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004159 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4160 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08004161 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4162 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4163 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4164 final ActivityRecord r = activities.get(activityNdx);
4165 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004166 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4167 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08004168 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08004169 if (r.visible) {
4170 hasVisibleActivities = true;
4171 }
Craig Mautneracebdc82015-02-24 10:53:03 -08004172 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08004173 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
4174 // Don't currently have state for the activity, or
4175 // it is finishing -- always remove it.
4176 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004177 } else if (!r.visible && r.launchCount > 2 &&
4178 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08004179 // We have launched this activity too many times since it was
4180 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08004181 // (Note if the activity is visible, we don't remove the record.
4182 // We leave the dead window on the screen but the process will
4183 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08004184 remove = true;
4185 } else {
4186 // The process may be gone, but the activity lives on!
4187 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004188 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004189 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004190 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
4191 "Removing activity " + r + " from stack at " + i
4192 + ": haveState=" + r.haveState
4193 + " stateNotNeeded=" + r.stateNotNeeded
4194 + " finishing=" + r.finishing
4195 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08004196 if (!r.finishing) {
4197 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
4198 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
4199 r.userId, System.identityHashCode(r),
4200 r.task.taskId, r.shortComponentName,
4201 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07004202 if (r.state == ActivityState.RESUMED) {
4203 mService.updateUsageStats(r, false);
4204 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004205 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004206 } else {
4207 // We have the current state for this activity, so
4208 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004209 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
4210 if (DEBUG_APP) Slog.v(TAG_APP,
4211 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08004212 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004213 // Set nowVisible to previous visible state. If the app was visible while
4214 // it died, we leave the dead window on screen so it's basically visible.
4215 // This is needed when user later tap on the dead window, we need to stop
4216 // other apps when user transfers focus to the restarted activity.
4217 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08004218 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004219 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08004220 "App died, clearing saved state of " + r);
4221 r.icicle = null;
4222 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004223 }
Craig Mautnerd2328952013-03-05 12:46:26 -08004224 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08004225 if (remove) {
Chong Zhange4cf36f2016-06-03 14:38:33 -07004226 removeActivityFromHistoryLocked(r, null, "appDied");
Craig Mautneracebdc82015-02-24 10:53:03 -08004227 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004228 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004229 }
4230 }
4231
4232 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004233 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004234
Chong Zhang280d3322015-11-03 17:27:26 -08004235 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004236 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004237 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004238 if (r != null && r.state != ActivityState.RESUMED) {
4239 r.updateOptionsLocked(options);
4240 } else {
4241 ActivityOptions.abort(options);
4242 }
4243 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004244 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004245 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004246
Craig Mautner21d24a22014-04-23 11:45:37 -07004247 void updateTaskMovement(TaskRecord task, boolean toFront) {
4248 if (task.isPersistable) {
4249 task.mLastTimeMoved = System.currentTimeMillis();
4250 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
4251 // recently will be most negative, tasks sent to the bottom before that will be less
4252 // negative. Similarly for recent tasks moved to the top which will be most positive.
4253 if (!toFront) {
4254 task.mLastTimeMoved *= -1;
4255 }
4256 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07004257 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07004258 }
4259
Craig Mautner84984fa2014-06-19 11:19:20 -07004260 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07004261 final int top = mTaskHistory.size() - 1;
4262 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
4263 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004264 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004265 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
4266 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07004267 mTaskHistory.remove(taskNdx);
4268 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004269 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07004270 return;
4271 }
4272 }
4273 }
4274
Chong Zhang280d3322015-11-03 17:27:26 -08004275 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004276 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004277 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004278
Craig Mautner11bf9a52013-02-19 14:08:51 -08004279 final int numTasks = mTaskHistory.size();
4280 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07004281 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004282 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004283 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004284 ActivityOptions.abort(options);
4285 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004286 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004287 }
4288 return;
4289 }
4290
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004291 if (timeTracker != null) {
4292 // The caller wants a time tracker associated with this task.
4293 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4294 tr.mActivities.get(i).appTimeTracker = timeTracker;
4295 }
4296 }
4297
Craig Mautner11bf9a52013-02-19 14:08:51 -08004298 // Shift all activities with this task up to the top
4299 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004300 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004301
Chong Zhang45c25ce2015-08-10 22:18:26 -07004302 // Don't refocus if invisible to current user
4303 ActivityRecord top = tr.getTopActivity();
4304 if (!okToShowLocked(top)) {
4305 addRecentActivityLocked(top);
4306 ActivityOptions.abort(options);
4307 return;
4308 }
4309
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004310 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004311 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004312 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004313
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004314 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004315 if (noAnimation) {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004316 mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004317 if (r != null) {
4318 mNoAnimActivities.add(r);
4319 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004320 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004321 } else {
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004322 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004323 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004324
Wale Ogunwaled046a012015-12-24 13:05:59 -08004325 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08004326 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004327
4328 if (VALIDATE_TOKENS) {
4329 validateAppTokensLocked();
4330 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004331 }
4332
4333 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004334 * Worker method for rearranging history stack. Implements the function of moving all
4335 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004336 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004337 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004338 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4339 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004340 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004341 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004342 * @return Returns true if the move completed, false if not.
4343 */
Craig Mautner299f9602015-01-26 09:47:33 -08004344 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004345 final TaskRecord tr = taskForIdLocked(taskId);
4346 if (tr == null) {
4347 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4348 return false;
4349 }
4350
4351 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07004352 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004353
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004354 // If we have a watcher, preflight the move before committing to it. First check
4355 // for *other* available tasks, but if none are available, then try again allowing the
4356 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07004357 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004358 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004359 if (next == null) {
4360 next = topRunningActivityLocked(null, 0);
4361 }
4362 if (next != null) {
4363 // ask watcher if this is allowed
4364 boolean moveOK = true;
4365 try {
4366 moveOK = mService.mController.activityResuming(next.packageName);
4367 } catch (RemoteException e) {
4368 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004369 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004370 }
4371 if (!moveOK) {
4372 return false;
4373 }
4374 }
4375 }
4376
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004377 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004378
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004379 if (mStackId == HOME_STACK_ID && topTask().isHomeTask()) {
4380 // For the case where we are moving the home task back and there is an activity visible
4381 // behind it on the fullscreen stack, we want to move the focus to the visible behind
4382 // activity to maintain order with what the user is seeing.
4383 final ActivityStack fullscreenStack =
4384 mStackSupervisor.getStack(FULLSCREEN_WORKSPACE_STACK_ID);
4385 if (fullscreenStack != null && fullscreenStack.hasVisibleBehindActivity()) {
4386 final ActivityRecord visibleBehind = fullscreenStack.getVisibleBehindActivity();
4387 mService.setFocusedActivityLocked(visibleBehind, "moveTaskToBack");
Wale Ogunwaled046a012015-12-24 13:05:59 -08004388 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004389 return true;
4390 }
4391 }
4392
riddle_hsuc215a4f2014-12-27 12:10:45 +08004393 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07004394
4395 // If true, we should resume the home activity next if the task we are moving to the
4396 // back is over the home stack. We force to false if the task we are moving to back
4397 // is the home task and we don't want it resumed after moving to the back.
4398 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
4399 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08004400 final TaskRecord nextTask = getNextTask(tr);
4401 if (nextTask != null) {
4402 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4403 } else {
4404 prevIsHome = true;
4405 }
4406 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004407 mTaskHistory.remove(tr);
4408 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004409 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004410
Craig Mautnerc8143c62013-09-03 12:15:57 -07004411 // There is an assumption that moving a task to the back moves it behind the home activity.
4412 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004413 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004414 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4415 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004416 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004417 break;
4418 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004419 if (taskNdx == 1) {
4420 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004421 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004422 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004423 }
4424
Jorim Jaggiaf80db42016-04-07 19:19:15 -07004425 mWindowManager.prepareAppTransition(TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004426 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004427
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004428 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004429 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004430 }
4431
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004432 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004433 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004434 if (!mService.mBooting && !mService.mBooted) {
4435 // Not ready yet!
4436 return false;
4437 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004438 final int taskToReturnTo = tr.getTaskToReturnTo();
4439 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004440 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004441 }
4442
Wale Ogunwaled046a012015-12-24 13:05:59 -08004443 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004444 return true;
4445 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004446
Craig Mautner8849a5e2013-04-02 16:41:03 -07004447 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004448 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004449 final Uri data = r.intent.getData();
4450 final String strData = data != null ? data.toSafeString() : null;
4451
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004452 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004453 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004454 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004455 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004456 }
4457
4458 /**
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004459 * Ensures all visible activities at or below the input activity have the right configuration.
4460 */
4461 void ensureVisibleActivitiesConfigurationLocked(ActivityRecord start, boolean preserveWindow) {
4462 if (start == null || !start.visible) {
4463 return;
4464 }
4465
4466 final TaskRecord startTask = start.task;
4467 boolean behindFullscreen = false;
4468 boolean updatedConfig = false;
4469
4470 for (int taskIndex = mTaskHistory.indexOf(startTask); taskIndex >= 0; --taskIndex) {
4471 final TaskRecord task = mTaskHistory.get(taskIndex);
4472 final ArrayList<ActivityRecord> activities = task.mActivities;
4473 int activityIndex =
4474 (start.task == task) ? activities.indexOf(start) : activities.size() - 1;
4475 for (; activityIndex >= 0; --activityIndex) {
4476 final ActivityRecord r = activities.get(activityIndex);
4477 updatedConfig |= ensureActivityConfigurationLocked(r, 0, preserveWindow);
4478 if (r.fullscreen) {
4479 behindFullscreen = true;
4480 break;
4481 }
4482 }
4483 if (behindFullscreen) {
4484 break;
4485 }
4486 }
4487 if (updatedConfig) {
Wale Ogunwale089586f2016-06-20 14:16:22 -07004488 // Ensure the resumed state of the focus activity if we updated the configuration of
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004489 // any activity.
4490 mStackSupervisor.resumeFocusedStackTopActivityLocked();
4491 }
4492 }
4493
4494 /**
4495 * Make sure the given activity matches the current configuration. Returns false if the activity
4496 * had to be destroyed. Returns true if the configuration is the same, or the activity will
4497 * remain running as-is for whatever reason. Ensures the HistoryRecord is updated with the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004498 * correct configuration and all other bookkeeping is handled.
4499 */
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004500 boolean ensureActivityConfigurationLocked(
4501 ActivityRecord r, int globalChanges, boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004502 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004503 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004504 "Skipping config check (will change): " + r);
4505 return true;
4506 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004507
Wale Ogunwale089586f2016-06-20 14:16:22 -07004508 // TODO: We could probably make the condition below just check that the activity state is
4509 // stopped, but also checking the sleep state for now to reduce change impact late in
4510 // development cycle.
4511 if (mService.isSleepingOrShuttingDownLocked() && r.state == ActivityState.STOPPED) {
4512 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4513 "Skipping config check (stopped while sleeping): " + r);
4514 return true;
4515 }
4516
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004517 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004518 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004519
Wale Ogunwalef7dab1e2016-04-16 11:38:50 -07004520 // Short circuit: if the two configurations are equal (the common case), then there is
4521 // nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004522 final Configuration newConfig = mService.mConfiguration;
Andrii Kulianf12fce12016-05-27 17:30:16 -07004523 r.task.sanitizeOverrideConfiguration(newConfig);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004524 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwalef7dab1e2016-04-16 11:38:50 -07004525 if (r.configuration.equals(newConfig)
4526 && r.taskConfigOverride.equals(taskConfig)
Wale Ogunwale60454db2015-01-23 16:05:07 -08004527 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004528 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004529 "Configuration unchanged in " + r);
4530 return true;
4531 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004532
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004533 // We don't worry about activities that are finishing.
4534 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004535 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004536 "Configuration doesn't matter in finishing " + r);
4537 r.stopFreezingScreenLocked(false);
4538 return true;
4539 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004540
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004541 // Okay we now are going to make this activity have the new config.
4542 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004543 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004544 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004545 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004546 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004547
Filip Gruszczynski23493322015-07-29 17:02:59 -07004548 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004549 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004550 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004551 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004552 "Configuration no differences in " + r);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004553 // There are no significant differences, so we won't relaunch but should still deliver
4554 // the new configuration to the client process.
Wale Ogunwalef7dab1e2016-04-16 11:38:50 -07004555 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004556 return true;
4557 }
4558
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004559 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4560 "Configuration changes for " + r + " ; taskChanges="
4561 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4562 + Configuration.configurationDiffToString(changes));
4563
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004564 // If the activity isn't currently running, just leave the new
4565 // configuration and it will pick that up next time it starts.
4566 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004567 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004568 "Configuration doesn't matter not running " + r);
4569 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004570 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004571 return true;
4572 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004573
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004574 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004575 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4576 "Checking to restart " + r.info.name + ": changed=0x"
4577 + Integer.toHexString(changes) + ", handles=0x"
Filip Gruszczynskica664812015-12-04 12:43:36 -08004578 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4579 + ", taskConfig=" + taskConfig);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004580
Dianne Hackborne6676352011-06-01 16:51:20 -07004581 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004582 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4583 r.configChangeFlags |= changes;
4584 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004585 r.forceNewConfig = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004586 preserveWindow &= isResizeOnlyChange(changes);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004587 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004588 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004589 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004590 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004591 } else if (r.state == ActivityState.PAUSING) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004592 // A little annoying: we are waiting for this activity to finish pausing. Let's not
4593 // do anything now, but just flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004594 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004595 "Config is skipping already pausing " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004596 r.deferRelaunchUntilPaused = true;
4597 r.preserveWindowOnDeferredRelaunch = preserveWindow;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004598 return true;
4599 } else if (r.state == ActivityState.RESUMED) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004600 // Try to optimize this case: the configuration is changing and we need to restart
4601 // the top, resumed activity. Instead of doing the normal handshaking, just say
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004602 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004603 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004604 "Config is relaunching resumed " + r);
Chong Zhang2b79af12016-02-10 18:47:06 -08004605
4606 if (DEBUG_STATES && !r.visible) {
4607 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + r
4608 + " called by " + Debug.getCallers(4));
4609 }
4610
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004611 relaunchActivityLocked(r, r.configChangeFlags, true, preserveWindow);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004612 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004613 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004614 "Config is relaunching non-resumed " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004615 relaunchActivityLocked(r, r.configChangeFlags, false, preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004616 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004617
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004618 // All done... tell the caller we weren't able to keep this activity around.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004619 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004620 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004621
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004622 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004623 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004624 // changes is always sent to all processes when they happen so it can just use whatever
4625 // system level configuration it last got.
Filip Gruszczynskica664812015-12-04 12:43:36 -08004626 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004627 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004628
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004629 return true;
4630 }
4631
Filip Gruszczynski23493322015-07-29 17:02:59 -07004632 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4633 Configuration oldTaskOverride) {
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08004634
4635 // If we went from full-screen to non-full-screen, make sure to use the correct
4636 // configuration task diff, so the diff stays as small as possible.
4637 if (Configuration.EMPTY.equals(oldTaskOverride)
4638 && !Configuration.EMPTY.equals(taskConfig)) {
4639 oldTaskOverride = record.task.extractOverrideConfig(record.configuration);
4640 }
4641
Jorim Jaggi1b025a62016-02-03 19:27:49 -08004642 // Conversely, do the same when going the other direction.
4643 if (Configuration.EMPTY.equals(taskConfig)
4644 && !Configuration.EMPTY.equals(oldTaskOverride)) {
4645 taskConfig = record.task.extractOverrideConfig(record.configuration);
4646 }
4647
Filip Gruszczynski23493322015-07-29 17:02:59 -07004648 // Determine what has changed. May be nothing, if this is a config
4649 // that has come back from the app after going idle. In that case
4650 // we just want to leave the official config object now in the
4651 // activity and do nothing else.
4652 int taskChanges = oldTaskOverride.diff(taskConfig);
4653 // We don't want to use size changes if they don't cross boundaries that are important to
4654 // the app.
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004655 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004656 final boolean crosses = record.crossesHorizontalSizeThreshold(
4657 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4658 || record.crossesVerticalSizeThreshold(
4659 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4660 if (!crosses) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004661 taskChanges &= ~CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004662 }
4663 }
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004664 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004665 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4666 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004667 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004668 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004669 }
4670 }
4671 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4672 }
4673
4674 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4675 Configuration oldTaskOverride, int taskChanges) {
4676 if (taskChanges == 0) {
4677 // {@link Configuration#diff} doesn't catch changes from unset values.
4678 // Check for changes we care about.
4679 if (oldTaskOverride.orientation != taskConfig.orientation) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004680 taskChanges |= CONFIG_ORIENTATION;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004681 }
4682 // We want to explicitly track situations where the size configuration goes from
4683 // undefined to defined. We don't care about crossing the threshold in that case,
4684 // because there is no threshold.
4685 final int oldHeight = oldTaskOverride.screenHeightDp;
4686 final int newHeight = taskConfig.screenHeightDp;
4687 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4688 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4689 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004690 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004691 }
4692 final int oldWidth = oldTaskOverride.screenWidthDp;
4693 final int newWidth = taskConfig.screenWidthDp;
4694 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4695 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4696 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004697 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004698 }
4699 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4700 final int newSmallest = taskConfig.smallestScreenWidthDp;
4701 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4702 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4703 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004704 taskChanges |= CONFIG_SMALLEST_SCREEN_SIZE;
4705 }
4706 final int oldLayout = oldTaskOverride.screenLayout;
4707 final int newLayout = taskConfig.screenLayout;
4708 if ((oldLayout == SCREENLAYOUT_UNDEFINED && newLayout != SCREENLAYOUT_UNDEFINED)
4709 || (oldLayout != SCREENLAYOUT_UNDEFINED && newLayout == SCREENLAYOUT_UNDEFINED)) {
4710 taskChanges |= CONFIG_SCREEN_LAYOUT;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004711 }
4712 }
4713 return taskChanges;
4714 }
4715
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004716 private static boolean isResizeOnlyChange(int change) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004717 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
4718 | CONFIG_SCREEN_LAYOUT)) == 0;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004719 }
4720
Wale Ogunwale83301a92015-09-24 15:54:08 -07004721 private void relaunchActivityLocked(
4722 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4723 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004724 r.configChangeFlags = 0;
Wale Ogunwale83301a92015-09-24 15:54:08 -07004725 return;
4726 }
4727
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004728 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004729 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004730 if (andResume) {
4731 results = r.results;
4732 newIntents = r.newIntents;
4733 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004734 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4735 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004736 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004737 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004738 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004739 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004740
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004741 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004742
Craig Mautner34b73df2014-01-12 21:11:08 -08004743 mStackSupervisor.removeChildActivityContainers(r);
4744
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004745 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004746 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08004747 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r
4748 + " callers=" + Debug.getCallers(6));
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004749 r.forceNewConfig = false;
Chong Zhangd78ddb42016-03-02 17:01:14 -08004750 mStackSupervisor.activityRelaunchingLocked(r);
Wale Ogunwale60454db2015-01-23 16:05:07 -08004751 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4752 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004753 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004754 // Note: don't need to call pauseIfSleepingLocked() here, because
4755 // the caller will only pass in 'andResume' if this activity is
4756 // currently resumed, which implies we aren't sleeping.
4757 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004758 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004759 }
4760
4761 if (andResume) {
Chong Zhang2b79af12016-02-10 18:47:06 -08004762 if (DEBUG_STATES) {
4763 Slog.d(TAG_STATES, "Resumed after relaunch " + r);
4764 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004765 r.state = ActivityState.RESUMED;
Chong Zhang2b79af12016-02-10 18:47:06 -08004766 // Relaunch-resume could happen either when the app is already in the front,
4767 // or while it's being brought to front. In the latter case, it's marked RESUMED
4768 // but not yet visible (or stopped). We need to complete the resume here as the
4769 // code in resumeTopActivityInnerLocked to complete the resume might be skipped.
4770 if (!r.visible || r.stopped) {
4771 mWindowManager.setAppVisibility(r.appToken, true);
4772 completeResumeLocked(r);
4773 } else {
4774 r.results = null;
4775 r.newIntents = null;
4776 }
Andrii Kulianf42cbe82016-06-22 14:49:58 -07004777 mService.showUnsupportedZoomDialogIfNeededLocked(r);
4778 mService.showAskCompatModeDialogLocked(r);
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004779 } else {
4780 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4781 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004782 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004783
4784 r.configChangeFlags = 0;
Chong Zhang225d1b62016-03-31 14:09:33 -07004785 r.deferRelaunchUntilPaused = false;
4786 r.preserveWindowOnDeferredRelaunch = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004787 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004788
4789 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004790 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4791 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4792 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4793 final ActivityRecord r = activities.get(activityNdx);
4794 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004795 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004796 }
4797 if (r.fullscreen && !r.finishing) {
4798 return false;
4799 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004800 }
4801 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004802 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004803 if (r == null) {
4804 return false;
4805 }
4806 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4807 + " would have returned true for r=" + r);
4808 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004809 }
4810
4811 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004812 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4813 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4814 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4815 final ActivityRecord r = activities.get(activityNdx);
4816 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004817 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004818 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004819 }
4820 }
4821 }
4822
Wale Ogunwale540e1232015-05-01 15:35:39 -07004823 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4824 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004825 boolean didSomething = false;
4826 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004827 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004828 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4829 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4830 int numActivities = activities.size();
4831 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4832 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004833 final boolean sameComponent =
4834 (r.packageName.equals(packageName) && (filterByClasses == null
4835 || filterByClasses.contains(r.realActivity.getClassName())))
4836 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004837 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004838 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004839 && (r.app == null || evenPersistent || !r.app.persistent)) {
4840 if (!doit) {
4841 if (r.finishing) {
4842 // If this activity is just finishing, then it is not
4843 // interesting as far as something to stop.
4844 continue;
4845 }
4846 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004847 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004848 if (r.isHomeActivity()) {
4849 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4850 Slog.i(TAG, "Skip force-stop again " + r);
4851 continue;
4852 } else {
4853 homeActivity = r.realActivity;
4854 }
4855 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004856 didSomething = true;
4857 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004858 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004859 if (r.app != null) {
4860 r.app.removed = true;
4861 }
4862 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004863 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004864 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004865 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4866 true)) {
4867 // r has been deleted from mActivities, accommodate.
4868 --numActivities;
4869 --activityNdx;
4870 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004871 }
4872 }
4873 }
4874 return didSomething;
4875 }
4876
Dianne Hackborn09233282014-04-30 11:33:59 -07004877 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004878 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4879 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004880 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004881 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004882 if (task.getTopActivity() == null) {
4883 continue;
4884 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004885 ActivityRecord r = null;
4886 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004887 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004888 int numActivities = 0;
4889 int numRunning = 0;
4890 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004891 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004892 continue;
4893 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004894 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004895 tmp = activities.get(activityNdx);
4896 if (tmp.finishing) {
4897 continue;
4898 }
4899 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004900
Craig Mautneraab647e2013-02-28 16:31:36 -08004901 // Initialize state for next task if needed.
4902 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4903 top = r;
4904 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004905 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004906
4907 // Add 'r' into the current task.
4908 numActivities++;
4909 if (r.app != null && r.app.thread != null) {
4910 numRunning++;
4911 }
4912
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004913 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004914 TAG, r.intent.getComponent().flattenToShortString()
4915 + ": task=" + r.task);
4916 }
4917
4918 RunningTaskInfo ci = new RunningTaskInfo();
4919 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004920 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004921 ci.baseActivity = r.intent.getComponent();
4922 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004923 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004924 if (focusedStack && topTask) {
4925 // Give the latest time to ensure foreground task can be sorted
4926 // at the first, because lastActiveTime of creating task is 0.
4927 ci.lastActiveTime = System.currentTimeMillis();
4928 topTask = false;
4929 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004930
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004931 if (top.task != null) {
4932 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004933 }
4934 ci.numActivities = numActivities;
4935 ci.numRunning = numRunning;
Wale Ogunwale21b60582016-01-27 12:34:16 -08004936 ci.isDockable = task.canGoInDockedStack();
Jorim Jaggi29379ec2016-04-11 23:43:42 -07004937 ci.resizeMode = task.mResizeMode;
Craig Mautneraab647e2013-02-28 16:31:36 -08004938 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004939 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004940 }
4941
4942 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004943 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004944 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004945 if (top >= 0) {
4946 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4947 int activityTop = activities.size() - 1;
4948 if (activityTop > 0) {
4949 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4950 "unhandled-back", true);
4951 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004952 }
4953 }
4954
Craig Mautner6b74cb52013-09-27 17:02:21 -07004955 /**
4956 * Reset local parameters because an app's activity died.
4957 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004958 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004959 */
4960 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004961 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004962 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004963 "App died while pausing: " + mPausingActivity);
4964 mPausingActivity = null;
4965 }
4966 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4967 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004968 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004969 }
4970
Craig Mautner19091252013-10-05 00:03:53 -07004971 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004972 }
4973
Craig Mautnercae015f2013-02-08 14:31:27 -08004974 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004975 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4976 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4977 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4978 final ActivityRecord r = activities.get(activityNdx);
4979 if (r.app == app) {
4980 Slog.w(TAG, " Force finishing activity "
4981 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004982 // Force the destroy to skip right to removal.
4983 r.app = null;
4984 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004985 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004986 }
4987 }
4988 }
4989
Dianne Hackborn390517b2013-05-30 15:03:32 -07004990 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004991 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004992 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004993 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4994 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004995 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4996 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004997 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004998 " Task id #" + task.taskId + "\n" +
4999 " mFullscreen=" + task.mFullscreen + "\n" +
5000 " mBounds=" + task.mBounds + "\n" +
Andrii Kulianf66a83d2016-05-17 12:17:44 -07005001 " mMinWidth=" + task.mMinWidth + "\n" +
5002 " mMinHeight=" + task.mMinHeight + "\n" +
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07005003 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07005004 if (printed) {
5005 header = null;
5006 }
Craig Mautneraab647e2013-02-28 16:31:36 -08005007 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07005008 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08005009 }
5010
5011 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
5012 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
5013
5014 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08005015 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5016 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08005017 }
5018 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08005019 final int top = mTaskHistory.size() - 1;
5020 if (top >= 0) {
5021 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
5022 int listTop = list.size() - 1;
5023 if (listTop >= 0) {
5024 activities.add(list.get(listTop));
5025 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005026 }
5027 } else {
5028 ItemMatcher matcher = new ItemMatcher();
5029 matcher.build(name);
5030
Craig Mautneraab647e2013-02-28 16:31:36 -08005031 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5032 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
5033 if (matcher.match(r1, r1.intent.getComponent())) {
5034 activities.add(r1);
5035 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005036 }
5037 }
5038 }
5039
5040 return activities;
5041 }
5042
5043 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07005044 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08005045
5046 // All activities that came from the package must be
5047 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08005048 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5049 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
5050 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
5051 final ActivityRecord a = activities.get(activityNdx);
5052 if (a.info.packageName.equals(packageName)) {
5053 a.forceNewConfig = true;
5054 if (starting != null && a == starting && a.visible) {
5055 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08005056 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08005057 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005058 }
5059 }
5060 }
5061
5062 return starting;
5063 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08005064
Craig Mautner299f9602015-01-26 09:47:33 -08005065 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07005066 removeTask(task, reason, REMOVE_TASK_MODE_DESTROYING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08005067 }
5068
Wale Ogunwale000957c2015-04-03 08:19:12 -07005069 /**
5070 * Removes the input task from this stack.
5071 * @param task to remove.
5072 * @param reason for removal.
Wale Ogunwale06579d62016-04-30 15:29:06 -07005073 * @param mode task removal mode. Either {@link #REMOVE_TASK_MODE_DESTROYING},
5074 * {@link #REMOVE_TASK_MODE_MOVING}, {@link #REMOVE_TASK_MODE_MOVING_TO_TOP}.
Wale Ogunwale000957c2015-04-03 08:19:12 -07005075 */
Wale Ogunwale06579d62016-04-30 15:29:06 -07005076 void removeTask(TaskRecord task, String reason, int mode) {
5077 if (mode == REMOVE_TASK_MODE_DESTROYING) {
Craig Mautner15df08a2015-04-01 12:17:18 -07005078 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08005079 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08005080 if (!StackId.persistTaskBounds(mStackId)) {
5081 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
5082 // default configuration the next time it launches.
5083 task.updateOverrideConfiguration(null);
5084 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08005085 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07005086
Craig Mautner04a0ea62014-01-13 12:51:26 -08005087 final ActivityRecord r = mResumedActivity;
5088 if (r != null && r.task == task) {
5089 mResumedActivity = null;
5090 }
5091
Craig Mautnerae7ecab2013-09-18 11:48:14 -07005092 final int taskNdx = mTaskHistory.indexOf(task);
5093 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07005094 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
5095 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
5096 if (!nextTask.isOverHomeStack()) {
5097 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
5098 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07005099 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07005100 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07005101 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07005102
Wale Ogunwale06579d62016-04-30 15:29:06 -07005103 if (mode == REMOVE_TASK_MODE_DESTROYING && task.mActivities.isEmpty()) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08005104 // TODO: VI what about activity?
Craig Mautner41db4a72014-05-07 17:20:56 -07005105 final boolean isVoiceSession = task.voiceSession != null;
5106 if (isVoiceSession) {
5107 try {
5108 task.voiceSession.taskFinished(task.intent, task.taskId);
5109 } catch (RemoteException e) {
5110 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07005111 }
Craig Mautner41db4a72014-05-07 17:20:56 -07005112 if (task.autoRemoveFromRecents() || isVoiceSession) {
5113 // Task creator asked to remove this when done, or this task was a voice
5114 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08005115 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08005116 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07005117 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07005118 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005119
5120 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005121 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Wale Ogunwale06579d62016-04-30 15:29:06 -07005122 // We only need to adjust focused stack if this stack is in focus and we are not in the
5123 // process of moving the task to the top of the stack that will be focused.
5124 if (isOnHomeDisplay() && mode != REMOVE_TASK_MODE_MOVING_TO_TOP
5125 && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08005126 String myReason = reason + " leftTaskHistoryEmpty";
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07005127 if (mFullscreen
5128 || !adjustFocusToNextFocusableStackLocked(
5129 task.getTaskToReturnTo(), myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07005130 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08005131 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005132 }
Craig Mautner593a4e62014-01-15 17:55:51 -08005133 if (mStacks != null) {
5134 mStacks.remove(this);
5135 mStacks.add(0, this);
5136 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07005137 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08005138 mActivityContainer.onTaskListEmptyLocked();
5139 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005140 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07005141
5142 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08005143 }
5144
Dianne Hackborn91097de2014-04-04 18:02:06 -07005145 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
5146 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
5147 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07005148 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
5149 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07005150 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08005151 addTask(task, toTop, "createTaskRecord");
Chong Zhang75b37202015-12-04 14:16:36 -08005152 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
Andrii Kulian2e751b82016-03-16 16:59:32 -07005153 if (!layoutTaskInStack(task, info.windowLayout) && mBounds != null && task.isResizeable()
Chong Zhang75b37202015-12-04 14:16:36 -08005154 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07005155 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07005156 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08005157 return task;
5158 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08005159
Andrii Kulian2e751b82016-03-16 16:59:32 -07005160 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.WindowLayout windowLayout) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08005161 if (mTaskPositioner == null) {
5162 return false;
5163 }
Andrii Kulian2e751b82016-03-16 16:59:32 -07005164 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, windowLayout);
Wale Ogunwale935e5022015-11-10 12:36:10 -08005165 return true;
5166 }
5167
Craig Mautnerc00204b2013-03-05 15:02:14 -08005168 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08005169 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005170 }
5171
Wale Ogunwale5f986092015-12-04 15:35:38 -08005172 void addTask(final TaskRecord task, final boolean toTop, String reason) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07005173 final ActivityStack prevStack = preAddTask(task, reason, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005174
Craig Mautnerc00204b2013-03-05 15:02:14 -08005175 task.stack = this;
5176 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07005177 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005178 } else {
5179 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07005180 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005181 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08005182 postAddTask(task, prevStack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005183 }
5184
Wale Ogunwale5f986092015-12-04 15:35:38 -08005185 void positionTask(final TaskRecord task, int position) {
Jorim Jaggi023da532016-04-20 22:42:32 -07005186 final ActivityRecord topRunningActivity = task.topRunningActivityLocked();
5187 final boolean wasResumed = topRunningActivity == task.stack.mResumedActivity;
Wale Ogunwaleed811d72016-05-06 08:41:19 -07005188 final ActivityStack prevStack = preAddTask(task, "positionTask", !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005189 task.stack = this;
5190 insertTaskAtPosition(task, position);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005191 postAddTask(task, prevStack);
Jorim Jaggi023da532016-04-20 22:42:32 -07005192 if (wasResumed) {
5193 if (mResumedActivity != null) {
5194 Log.wtf(TAG, "mResumedActivity was already set when moving mResumedActivity from"
5195 + " other stack to this stack mResumedActivity=" + mResumedActivity
5196 + " other mResumedActivity=" + topRunningActivity);
5197 }
5198 mResumedActivity = topRunningActivity;
5199 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08005200 }
5201
Wale Ogunwale06579d62016-04-30 15:29:06 -07005202 private ActivityStack preAddTask(TaskRecord task, String reason, boolean toTop) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08005203 final ActivityStack prevStack = task.stack;
5204 if (prevStack != null && prevStack != this) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07005205 prevStack.removeTask(task, reason,
5206 toTop ? REMOVE_TASK_MODE_MOVING_TO_TOP : REMOVE_TASK_MODE_MOVING);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005207 }
5208 return prevStack;
5209 }
5210
5211 private void postAddTask(TaskRecord task, ActivityStack prevStack) {
5212 if (prevStack != null) {
Andrii Kulian933076d2016-03-29 17:04:42 -07005213 mStackSupervisor.scheduleReportPictureInPictureModeChangedIfNeeded(task, prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005214 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005215 try {
5216 task.voiceSession.taskStarted(task.intent, task.taskId);
5217 } catch (RemoteException e) {
5218 }
5219 }
5220 }
5221
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07005222 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08005223 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08005224 // TODO: VI deal with activity
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07005225 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
5226 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6cae7652015-12-26 07:36:26 -08005227 (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId, r.info.configChanges,
5228 task.voiceSession != null, r.mLaunchTaskBehind, bounds, task.mOverrideConfig,
Yorke Lee0e852472016-06-15 10:03:18 -07005229 task.mResizeMode, r.isAlwaysFocusable(), task.isHomeTask(),
Robert Carrfd10cd12016-06-29 16:41:50 -07005230 r.appInfo.targetSdkVersion, r.mRotationAnimationHint);
Wale Ogunwale706ed792015-08-02 10:29:44 -07005231 r.taskConfigOverride = task.mOverrideConfig;
5232 }
5233
Wale Ogunwaled046a012015-12-24 13:05:59 -08005234 void moveToFrontAndResumeStateIfNeeded(
5235 ActivityRecord r, boolean moveToFront, boolean setResume, String reason) {
5236 if (!moveToFront) {
5237 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07005238 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08005239
5240 // If the activity owns the last resumed activity, transfer that together,
5241 // so that we don't resume the same activity again in the new stack.
5242 // Apps may depend on onResume()/onPause() being called in pairs.
5243 if (setResume) {
5244 mResumedActivity = r;
5245 }
5246 // Move the stack in which we are placing the activity to the front. The call will also
5247 // make sure the activity focus is set.
5248 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07005249 }
5250
5251 /**
5252 * Moves the input activity from its current stack to this one.
5253 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
5254 * created on this stack which the activity is added to.
5255 * */
5256 void moveActivityToStack(ActivityRecord r) {
5257 final ActivityStack prevStack = r.task.stack;
5258 if (prevStack.mStackId == mStackId) {
5259 // You are already in the right stack silly...
5260 return;
5261 }
5262
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07005263 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07005264 && (mStackSupervisor.topRunningActivityLocked() == r);
5265 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
5266
5267 final TaskRecord task = createTaskRecord(
Suprabh Shukla09a88f52015-12-02 14:36:31 -08005268 mStackSupervisor.getNextTaskIdForUserLocked(r.userId),
5269 r.info, r.intent, null, null, true);
Wale Ogunwale079a0042015-10-24 11:44:07 -07005270 r.setTask(task, null);
5271 task.addActivityToTop(r);
5272 setAppTask(r, task);
Andrii Kulian933076d2016-03-29 17:04:42 -07005273 mStackSupervisor.scheduleReportPictureInPictureModeChangedIfNeeded(task, prevStack);
Wale Ogunwaled046a012015-12-24 13:05:59 -08005274 moveToFrontAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
5275 if (wasResumed) {
5276 prevStack.mResumedActivity = null;
5277 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07005278 }
5279
Wale Ogunwale706ed792015-08-02 10:29:44 -07005280 private void setAppTask(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08005281 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08005282 mWindowManager.setAppTask(r.appToken, task.taskId, mStackId, bounds, task.mOverrideConfig,
5283 task.mResizeMode, task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07005284 r.taskConfigOverride = task.mOverrideConfig;
5285 }
5286
Craig Mautnerc00204b2013-03-05 15:02:14 -08005287 public int getStackId() {
5288 return mStackId;
5289 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07005290
5291 @Override
5292 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07005293 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
5294 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07005295 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08005296
Craig Mautner15df08a2015-04-01 12:17:18 -07005297 void onLockTaskPackagesUpdatedLocked() {
5298 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5299 mTaskHistory.get(taskNdx).setLockTaskAuth();
5300 }
5301 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005302}