blob: ad989704b823d04efad7a4360297314bec6d36e6 [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
Wale Ogunwale59507092018-10-29 09:00:30 -070017package com.android.server.wm;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070018
Wale Ogunwale30e441d2017-11-09 08:28:45 -080019import static android.app.ITaskStackListener.FORCED_RESIZEABLE_REASON_SPLIT_SCREEN;
Matthew Nga51dcaa2018-05-07 15:36:06 -070020import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
Matthew Ng99b3cdc2018-05-01 14:24:38 -070021import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwale30e441d2017-11-09 08:28:45 -080022import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070023import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
24import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070025import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale68278562017-09-23 17:13:55 -070026import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale2b07da82017-11-08 14:52:40 -080027import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -070028import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale68278562017-09-23 17:13:55 -070029import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Wale Ogunwalef75962a2017-08-23 14:58:04 -070030import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070031import static android.app.WindowConfiguration.activityTypeToString;
32import static android.app.WindowConfiguration.windowingModeToString;
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -080033import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -080034import static android.content.pm.ActivityInfo.FLAG_RESUME_WHILE_PAUSING;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070035import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwalef5d1e352016-09-22 08:34:42 -070036import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian7211d2e2017-01-27 15:58:05 -080037import static android.view.Display.FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD;
Andrii Kulian94e82d9b02017-07-13 15:33:06 -070038import static android.view.Display.INVALID_DISPLAY;
Louis Chang7d0037c2018-08-13 12:42:06 +080039import static android.view.WindowManager.TRANSIT_ACTIVITY_CLOSE;
40import static android.view.WindowManager.TRANSIT_ACTIVITY_OPEN;
41import static android.view.WindowManager.TRANSIT_CRASHING_ACTIVITY_CLOSE;
42import static android.view.WindowManager.TRANSIT_NONE;
43import static android.view.WindowManager.TRANSIT_TASK_CLOSE;
44import static android.view.WindowManager.TRANSIT_TASK_OPEN;
45import static android.view.WindowManager.TRANSIT_TASK_OPEN_BEHIND;
46import static android.view.WindowManager.TRANSIT_TASK_TO_BACK;
47import static android.view.WindowManager.TRANSIT_TASK_TO_FRONT;
Winson Chung1dbc8112017-09-28 18:05:31 -070048
Evan Roskye747c3e2018-10-30 20:06:41 -070049import static com.android.server.am.ActivityStackProto.BOUNDS;
50import static com.android.server.am.ActivityStackProto.CONFIGURATION_CONTAINER;
51import static com.android.server.am.ActivityStackProto.DISPLAY_ID;
52import static com.android.server.am.ActivityStackProto.FULLSCREEN;
53import static com.android.server.am.ActivityStackProto.ID;
54import static com.android.server.am.ActivityStackProto.RESUMED_ACTIVITY;
55import static com.android.server.am.ActivityStackProto.TASKS;
Wale Ogunwale59507092018-10-29 09:00:30 -070056import static com.android.server.wm.ActivityDisplay.POSITION_BOTTOM;
57import static com.android.server.wm.ActivityDisplay.POSITION_TOP;
58import static com.android.server.wm.ActivityStack.ActivityState.DESTROYED;
59import static com.android.server.wm.ActivityStack.ActivityState.DESTROYING;
60import static com.android.server.wm.ActivityStack.ActivityState.FINISHING;
61import static com.android.server.wm.ActivityStack.ActivityState.PAUSED;
62import static com.android.server.wm.ActivityStack.ActivityState.PAUSING;
63import static com.android.server.wm.ActivityStack.ActivityState.RESUMED;
64import static com.android.server.wm.ActivityStack.ActivityState.STOPPED;
65import static com.android.server.wm.ActivityStack.ActivityState.STOPPING;
Wale Ogunwale59507092018-10-29 09:00:30 -070066import static com.android.server.wm.ActivityStackSupervisor.PAUSE_IMMEDIATELY;
67import static com.android.server.wm.ActivityStackSupervisor.PRESERVE_WINDOWS;
68import static com.android.server.wm.ActivityStackSupervisor.REMOVE_FROM_RECENTS;
Garfield Tan347bd602018-12-21 15:11:12 -080069import static com.android.server.wm.ActivityStackSupervisor.dumpHistoryList;
70import static com.android.server.wm.ActivityStackSupervisor.printThisActivity;
Wale Ogunwale59507092018-10-29 09:00:30 -070071import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_ADD_REMOVE;
72import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_ALL;
73import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_APP;
74import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_CLEANUP;
75import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_CONTAINERS;
76import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_PAUSE;
77import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_RELEASE;
78import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_RESULTS;
79import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_SAVED_STATE;
80import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_STACK;
81import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_STATES;
82import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_SWITCH;
83import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_TASKS;
84import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_TRANSITION;
85import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_USER_LEAVING;
86import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_VISIBILITY;
87import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_ADD_REMOVE;
88import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_APP;
89import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_CLEANUP;
90import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_CONTAINERS;
91import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_PAUSE;
92import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_RELEASE;
93import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_RESULTS;
94import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_SAVED_STATE;
95import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_STACK;
96import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_STATES;
97import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_SWITCH;
98import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_TASKS;
99import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_TRANSITION;
100import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_USER_LEAVING;
101import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_VISIBILITY;
102import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
103import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
104import static com.android.server.wm.ActivityTaskManagerService.H.FIRST_ACTIVITY_STACK_MSG;
105import static com.android.server.wm.ActivityTaskManagerService.RELAUNCH_REASON_FREE_RESIZE;
106import static com.android.server.wm.ActivityTaskManagerService.RELAUNCH_REASON_WINDOWING_MODE_RESIZE;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800107import static com.android.server.wm.RootActivityContainer.FindTaskResult;
Louis Changf2835df2018-10-17 15:14:45 +0800108
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800109import static java.lang.Integer.MAX_VALUE;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700110
Andrii Kulian0c869cc2019-02-06 19:50:32 -0800111import android.annotation.IntDef;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700112import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700113import android.app.ActivityManager;
Wale Ogunwale53783742018-09-16 10:21:51 -0700114import android.app.ActivityManagerInternal;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700115import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700116import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -0800117import android.app.IActivityController;
Yunfan Chen279f5582018-12-12 15:24:50 -0800118import android.app.RemoteAction;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700119import android.app.ResultInfo;
Winson Chung61c9e5a2017-10-11 10:39:32 -0700120import android.app.WindowConfiguration.ActivityType;
121import android.app.WindowConfiguration.WindowingMode;
Andrii Kulian446e8242017-10-26 15:17:29 -0700122import android.app.servertransaction.ActivityResultItem;
Andrii Kulian9956d892018-02-14 13:48:56 -0800123import android.app.servertransaction.ClientTransaction;
Andrii Kulian446e8242017-10-26 15:17:29 -0700124import android.app.servertransaction.DestroyActivityItem;
Louis Chang7d0037c2018-08-13 12:42:06 +0800125import android.app.servertransaction.NewIntentItem;
Andrii Kulian446e8242017-10-26 15:17:29 -0700126import android.app.servertransaction.PauseActivityItem;
127import android.app.servertransaction.ResumeActivityItem;
128import android.app.servertransaction.StopActivityItem;
Louis Chang7d0037c2018-08-13 12:42:06 +0800129import android.app.servertransaction.WindowVisibilityItem;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700130import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700131import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700132import android.content.pm.ActivityInfo;
Todd Kennedy39bfee52016-02-24 10:28:21 -0800133import android.content.pm.ApplicationInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700134import android.content.res.Configuration;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700135import android.graphics.Rect;
Santos Cordon73ff7d82013-03-06 17:24:11 -0800136import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700137import android.os.Binder;
Craig Mautner329f4122013-11-07 09:10:42 -0800138import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700139import android.os.Handler;
140import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +0900141import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700142import android.os.Message;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700143import android.os.RemoteException;
144import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700145import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -0700146import android.service.voice.IVoiceInteractionSession;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700147import android.util.ArraySet;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700148import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800149import android.util.IntArray;
Jorim Jaggi023da532016-04-20 22:42:32 -0700150import android.util.Log;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700151import android.util.Slog;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700152import android.util.proto.ProtoOutputStream;
Craig Mautner59c00972012-07-30 12:10:24 -0700153import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700154
Andreas Gampea36dc622018-02-05 17:19:22 -0800155import com.android.internal.annotations.GuardedBy;
Bryce Lee840c5662017-04-13 10:02:51 -0700156import com.android.internal.annotations.VisibleForTesting;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700157import com.android.internal.app.IVoiceInteractor;
Wale Ogunwale53783742018-09-16 10:21:51 -0700158import com.android.internal.util.function.pooled.PooledLambda;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700159import com.android.server.Watchdog;
Wale Ogunwale59507092018-10-29 09:00:30 -0700160import com.android.server.am.ActivityManagerService;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700161import com.android.server.am.ActivityManagerService.ItemMatcher;
Wale Ogunwale59507092018-10-29 09:00:30 -0700162import com.android.server.am.AppTimeTracker;
163import com.android.server.am.EventLogTags;
164import com.android.server.am.PendingIntentRecord;
Jorim Jaggiaf80db42016-04-07 19:19:15 -0700165
Craig Mautnercae015f2013-02-08 14:31:27 -0800166import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -0800167import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700168import java.lang.ref.WeakReference;
169import java.util.ArrayList;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700170import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -0800171import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700172import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700173
174/**
175 * State and management of a single stack of activities.
176 */
Yunfan Chen279f5582018-12-12 15:24:50 -0800177class ActivityStack extends ConfigurationContainer {
Wale Ogunwale98875612018-10-12 07:53:02 -0700178 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_ATM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700179 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
180 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800181 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700182 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700183 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700184 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700185 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700186 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700187 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700188 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700189 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
190 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
191 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
192 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
193 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700194
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700195 // Ticks during which we check progress while waiting for an app to launch.
196 static final int LAUNCH_TICK = 500;
197
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700198 // How long we wait until giving up on the last activity to pause. This
199 // is short because it directly impacts the responsiveness of starting the
200 // next activity.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700201 private static final int PAUSE_TIMEOUT = 500;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700202
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700203 // How long we wait for the activity to tell us it has stopped before
204 // giving up. This is a good amount of time because we really need this
Robert Carr3406d462018-03-15 16:19:07 -0700205 // from the application in order to get its saved state. Once the stop
206 // is complete we may start destroying client resources triggering
207 // crashes if the UI thread was hung. We put this timeout one second behind
208 // the ANR timeout so these situations will generate ANR instead of
209 // Surface lost or other errors.
210 private static final int STOP_TIMEOUT = 11 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700211
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700212 // How long we wait until giving up on an activity telling us it has
213 // finished destroying itself.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700214 private static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800215
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700216 // Set to false to disable the preview that is shown while a new activity
217 // is being started.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700218 private static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800219
Craig Mautner5eda9b32013-07-02 11:58:16 -0700220 // How long to wait for all background Activities to redraw following a call to
221 // convertToTranslucent().
Andrii Kulian21713ac2016-10-12 22:05:05 -0700222 private static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700223
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -0800224 // How many activities have to be scheduled to stop to force a stop pass.
225 private static final int MAX_STOPPING_TO_FORCE = 3;
226
Andrii Kulian0c869cc2019-02-06 19:50:32 -0800227 @IntDef(prefix = {"STACK_VISIBILITY"}, value = {
228 STACK_VISIBILITY_VISIBLE,
229 STACK_VISIBILITY_VISIBLE_BEHIND_TRANSLUCENT,
230 STACK_VISIBILITY_INVISIBLE,
231 })
232 @interface StackVisibility {}
233
234 /** Stack is visible. No other stacks on top that fully or partially occlude it. */
235 static final int STACK_VISIBILITY_VISIBLE = 0;
236
237 /** Stack is partially occluded by other translucent stack(s) on top of it. */
238 static final int STACK_VISIBILITY_VISIBLE_BEHIND_TRANSLUCENT = 1;
239
240 /** Stack is completely invisible. */
241 static final int STACK_VISIBILITY_INVISIBLE = 2;
242
Andrii Kulian1779e612016-10-12 21:58:25 -0700243 @Override
244 protected int getChildCount() {
245 return mTaskHistory.size();
246 }
247
248 @Override
Garfield Tanb5cc09f2018-09-28 10:06:52 -0700249 protected TaskRecord getChildAt(int index) {
Andrii Kulian1779e612016-10-12 21:58:25 -0700250 return mTaskHistory.get(index);
251 }
252
253 @Override
Evan Roskyc5abbd82018-10-05 16:02:19 -0700254 protected ActivityDisplay getParent() {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700255 return getDisplay();
Andrii Kulian1779e612016-10-12 21:58:25 -0700256 }
257
Evan Roskyc5abbd82018-10-05 16:02:19 -0700258 void setParent(ActivityDisplay parent) {
259 ActivityDisplay current = getParent();
260 if (current != parent) {
261 mDisplayId = parent.mDisplayId;
262 onParentChanged();
263 }
264 }
265
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800266 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -0700267 protected void onParentChanged() {
Evan Roskyc5abbd82018-10-05 16:02:19 -0700268 ActivityDisplay display = getParent();
269 if (display != null) {
270 // Rotations are relative to the display. This means if there are 2 displays rotated
271 // differently (eg. 2 monitors with one landscape and one portrait), moving a stack
272 // from one to the other could look like a rotation change. To prevent this
273 // apparent rotation change (and corresponding bounds rotation), pretend like our
274 // current rotation is already the same as the new display.
275 // Note, if ActivityStack or related logic ever gets nested, this logic will need
276 // to move to onConfigurationChanged.
277 getConfiguration().windowConfiguration.setRotation(
278 display.getWindowConfiguration().getRotation());
279 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800280 super.onParentChanged();
Evan Roskyc5abbd82018-10-05 16:02:19 -0700281 if (display != null && inSplitScreenPrimaryWindowingMode()) {
282 // If we created a docked stack we want to resize it so it resizes all other stacks
283 // in the system.
284 getStackDockedModeBounds(null, null, mTmpRect2, mTmpRect3);
Evan Roskydfe3da72018-10-26 17:21:06 -0700285 mStackSupervisor.resizeDockedStackLocked(getRequestedOverrideBounds(), mTmpRect2,
286 mTmpRect2, null, null, PRESERVE_WINDOWS);
Evan Roskyc5abbd82018-10-05 16:02:19 -0700287 }
Wale Ogunwaled32da472018-11-16 07:19:28 -0800288 mRootActivityContainer.updateUIDsPresentOnDisplay();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800289 }
290
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700291 enum ActivityState {
292 INITIALIZING,
293 RESUMED,
294 PAUSING,
295 PAUSED,
296 STOPPING,
297 STOPPED,
298 FINISHING,
299 DESTROYING,
Riddle Hsu7b766fd2019-01-28 21:14:59 +0800300 DESTROYED,
301 RESTARTING_PROCESS
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700302 }
303
Bryce Lee840c5662017-04-13 10:02:51 -0700304 @VisibleForTesting
Wale Ogunwale06579d62016-04-30 15:29:06 -0700305 /* The various modes for the method {@link #removeTask}. */
306 // Task is being completely removed from all stacks in the system.
Bryce Lee840c5662017-04-13 10:02:51 -0700307 protected static final int REMOVE_TASK_MODE_DESTROYING = 0;
Wale Ogunwale06579d62016-04-30 15:29:06 -0700308 // Task is being removed from this stack so we can add it to another stack. In the case we are
309 // moving we don't want to perform some operations on the task like removing it from window
310 // manager or recents.
311 static final int REMOVE_TASK_MODE_MOVING = 1;
312 // Similar to {@link #REMOVE_TASK_MODE_MOVING} and the task will be added to the top of its new
313 // stack and the new stack will be on top of all stacks.
Wale Ogunwale56d8d162017-05-30 11:12:20 -0700314 static final int REMOVE_TASK_MODE_MOVING_TO_TOP = 2;
Wale Ogunwale06579d62016-04-30 15:29:06 -0700315
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -0700316 final ActivityTaskManagerService mService;
Yunfan Chen279f5582018-12-12 15:24:50 -0800317 final WindowManagerService mWindowManager;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800318
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700319 /**
320 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800321 * running) activities. It contains #TaskRecord objects.
322 */
Todd Kennedy39bfee52016-02-24 10:28:21 -0800323 private final ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800324
325 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700326 * List of running activities, sorted by recent usage.
327 * The first entry in the list is the least recently used.
328 * It contains HistoryRecord objects.
329 */
Garfield Tan347bd602018-12-21 15:11:12 -0800330 private final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700331
332 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700333 * When we are in the process of pausing an activity, before starting the
334 * next one, this variable holds the activity that is currently being paused.
335 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800336 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700337
338 /**
339 * This is the last activity that we put into the paused state. This is
340 * used to determine if we need to do an activity transition while sleeping,
341 * when we normally hold the top activity paused.
342 */
343 ActivityRecord mLastPausedActivity = null;
344
345 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700346 * Activities that specify No History must be removed once the user navigates away from them.
347 * If the device goes to sleep with such an activity in the paused state then we save it here
348 * and finish it later if another activity replaces it on wakeup.
349 */
350 ActivityRecord mLastNoHistoryActivity = null;
351
352 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700353 * Current activity that is resumed, or null if there is none.
354 */
355 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800356
Craig Mautner5eda9b32013-07-02 11:58:16 -0700357 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
358 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
359 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
360 // Activity in mTranslucentActivityWaiting is notified via
361 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
362 // background activity being drawn then the same call will be made with a true value.
363 ActivityRecord mTranslucentActivityWaiting = null;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700364 ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700365
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700366 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700367 * Set when we know we are going to be calling updateConfiguration()
368 * soon, so want to skip intermediate config checks.
369 */
370 boolean mConfigWillChange;
371
Winson Chung47900652017-04-06 18:44:25 -0700372 /**
373 * When set, will force the stack to report as invisible.
374 */
375 boolean mForceHidden = false;
376
Andrii Kulian6b321512019-01-23 06:37:00 +0000377 /**
378 * Used to keep resumeTopActivityUncheckedLocked() from being entered recursively
379 */
380 boolean mInResumeTopActivity = false;
381
Andrii Kulian21713ac2016-10-12 22:05:05 -0700382 private boolean mUpdateBoundsDeferred;
383 private boolean mUpdateBoundsDeferredCalled;
Evan Rosky1ac84462018-11-13 11:25:30 -0800384 private boolean mUpdateDisplayedBoundsDeferredCalled;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700385 private final Rect mDeferredBounds = new Rect();
Evan Rosky1ac84462018-11-13 11:25:30 -0800386 private final Rect mDeferredDisplayedBounds = new Rect();
Jorim Jaggi192086e2016-03-11 17:17:03 +0100387
Craig Mautner858d8a62013-04-23 17:08:34 -0700388 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700389
Craig Mautnerc00204b2013-03-05 15:02:14 -0800390 final int mStackId;
Craig Mautnere0a38842013-12-16 16:14:02 -0800391 /** The attached Display's unique identifier, or -1 if detached */
392 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800393
Evan Rosky10475742018-09-05 19:02:48 -0700394 /** Stores the override windowing-mode from before a transient mode change (eg. split) */
395 private int mRestoreOverrideWindowingMode = WINDOWING_MODE_UNDEFINED;
396
Evan Roskyc5abbd82018-10-05 16:02:19 -0700397 private final Rect mTmpRect = new Rect();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800398 private final Rect mTmpRect2 = new Rect();
Evan Roskyc5abbd82018-10-05 16:02:19 -0700399 private final Rect mTmpRect3 = new Rect();
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800400 private final ActivityOptions mTmpOptions = ActivityOptions.makeBasic();
Andrii Kulian1e32e022016-09-16 15:29:34 -0700401
Bryce Leefbd263b42018-03-07 10:33:55 -0800402 /** List for processing through a set of activities */
403 private final ArrayList<ActivityRecord> mTmpActivities = new ArrayList<>();
404
Craig Mautner27084302013-03-25 08:05:25 -0700405 /** Run all ActivityStacks through this */
Winson Chung5af42fc2017-03-24 17:11:33 -0700406 protected final ActivityStackSupervisor mStackSupervisor;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800407 protected final RootActivityContainer mRootActivityContainer;
Craig Mautner27084302013-03-25 08:05:25 -0700408
Jorim Jaggife762342016-10-13 14:33:27 +0200409 private boolean mTopActivityOccludesKeyguard;
410 private ActivityRecord mTopDismissingKeyguardActivity;
411
Wale Ogunwale98875612018-10-12 07:53:02 -0700412 static final int PAUSE_TIMEOUT_MSG = FIRST_ACTIVITY_STACK_MSG + 1;
413 static final int DESTROY_TIMEOUT_MSG = FIRST_ACTIVITY_STACK_MSG + 2;
414 static final int LAUNCH_TICK_MSG = FIRST_ACTIVITY_STACK_MSG + 3;
415 static final int STOP_TIMEOUT_MSG = FIRST_ACTIVITY_STACK_MSG + 4;
416 static final int DESTROY_ACTIVITIES_MSG = FIRST_ACTIVITY_STACK_MSG + 5;
417 static final int TRANSLUCENT_TIMEOUT_MSG = FIRST_ACTIVITY_STACK_MSG + 6;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700418
Yunfan Chen279f5582018-12-12 15:24:50 -0800419 // TODO: remove after unification.
420 TaskStack mTaskStack;
421
Andrii Kulian21713ac2016-10-12 22:05:05 -0700422 private static class ScheduleDestroyArgs {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700423 final WindowProcessController mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700424 final String mReason;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700425 ScheduleDestroyArgs(WindowProcessController owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700426 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700427 mReason = reason;
428 }
429 }
430
Zoran Marcetaf958b322012-08-09 20:27:12 +0900431 final Handler mHandler;
432
Andrii Kulian21713ac2016-10-12 22:05:05 -0700433 private class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800434
Craig Mautnerc8143c62013-09-03 12:15:57 -0700435 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900436 super(looper);
437 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700438
Zoran Marcetaf958b322012-08-09 20:27:12 +0900439 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700440 public void handleMessage(Message msg) {
441 switch (msg.what) {
442 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800443 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700444 // We don't at this point know if the activity is fullscreen,
445 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800446 Slog.w(TAG, "Activity pause timeout for " + r);
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -0700447 synchronized (mService.mGlobalLock) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700448 if (r.hasProcess()) {
449 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700450 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700451 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800452 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700453 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700454 case LAUNCH_TICK_MSG: {
455 ActivityRecord r = (ActivityRecord)msg.obj;
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -0700456 synchronized (mService.mGlobalLock) {
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700457 if (r.continueLaunchTickingLocked()) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700458 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700459 }
460 }
461 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700462 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800463 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700464 // We don't at this point know if the activity is fullscreen,
465 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800466 Slog.w(TAG, "Activity destroy timeout for " + r);
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -0700467 synchronized (mService.mGlobalLock) {
Craig Mautner299f9602015-01-26 09:47:33 -0800468 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800469 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700470 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700471 case STOP_TIMEOUT_MSG: {
472 ActivityRecord r = (ActivityRecord)msg.obj;
473 // We don't at this point know if the activity is fullscreen,
474 // so we need to be conservative and assume it isn't.
475 Slog.w(TAG, "Activity stop timeout for " + r);
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -0700476 synchronized (mService.mGlobalLock) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700477 if (r.isInHistory()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -0700478 r.activityStoppedLocked(null /* icicle */,
479 null /* persistentState */, null /* description */);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700480 }
481 }
482 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700483 case DESTROY_ACTIVITIES_MSG: {
484 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -0700485 synchronized (mService.mGlobalLock) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700486 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700487 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700488 } break;
489 case TRANSLUCENT_TIMEOUT_MSG: {
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -0700490 synchronized (mService.mGlobalLock) {
Craig Mautner5eda9b32013-07-02 11:58:16 -0700491 notifyActivityDrawnLocked(null);
492 }
493 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700494 }
495 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800496 }
497
Craig Mautner34b73df2014-01-12 21:11:08 -0800498 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800499 int count = 0;
500 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
501 count += mTaskHistory.get(taskNdx).mActivities.size();
502 }
503 return count;
504 }
505
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700506 ActivityStack(ActivityDisplay display, int stackId, ActivityStackSupervisor supervisor,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700507 int windowingMode, int activityType, boolean onTop) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700508 mStackSupervisor = supervisor;
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700509 mService = supervisor.mService;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800510 mRootActivityContainer = mService.mRootActivityContainer;
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -0700511 mHandler = new ActivityStackHandler(supervisor.mLooper);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800512 mWindowManager = mService.mWindowManager;
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700513 mStackId = stackId;
Wale Ogunwale86b74462018-07-02 08:42:43 -0700514 mCurrentUser = mService.mAmInternal.getCurrentUserId();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800515 mTmpRect2.setEmpty();
Andrii Kulian2fcc4512018-01-25 16:39:27 -0800516 // Set display id before setting activity and window type to make sure it won't affect
517 // stacks on a wrong display.
518 mDisplayId = display.mDisplayId;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700519 setActivityType(activityType);
Yunfan Chen279f5582018-12-12 15:24:50 -0800520 createTaskStack(display.mDisplayId, onTop, mTmpRect2);
Evan Roskyc5abbd82018-10-05 16:02:19 -0700521 setWindowingMode(windowingMode, false /* animate */, false /* showRecents */,
522 false /* enteringSplitScreenMode */, false /* deferEnsuringVisibility */,
523 true /* creating */);
524 display.addChild(this, onTop ? POSITION_TOP : POSITION_BOTTOM);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800525 }
526
Yunfan Chen279f5582018-12-12 15:24:50 -0800527 void createTaskStack(int displayId, boolean onTop, Rect outBounds) {
528 final DisplayContent dc = mWindowManager.mRoot.getDisplayContent(displayId);
529 if (dc == null) {
530 throw new IllegalArgumentException("Trying to add stackId=" + mStackId
531 + " to unknown displayId=" + displayId);
532 }
533 mTaskStack = new TaskStack(mWindowManager, mStackId, this);
534 dc.setStackOnDisplay(mStackId, onTop, mTaskStack);
535 if (mTaskStack.matchParentBounds()) {
536 outBounds.setEmpty();
537 } else {
538 mTaskStack.getRawBounds(outBounds);
539 }
Winson Chung55893332017-02-17 17:13:10 -0800540 }
541
Yunfan Chen279f5582018-12-12 15:24:50 -0800542 TaskStack getTaskStack() {
543 return mTaskStack;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700544 }
545
Bryce Leec4ab62a2018-03-05 14:19:26 -0800546 /**
547 * This should be called when an activity in a child task changes state. This should only
548 * be called from
549 * {@link TaskRecord#onActivityStateChanged(ActivityRecord, ActivityState, String)}.
550 * @param record The {@link ActivityRecord} whose state has changed.
551 * @param state The new state.
552 * @param reason The reason for the change.
553 */
554 void onActivityStateChanged(ActivityRecord record, ActivityState state, String reason) {
555 if (record == mResumedActivity && state != RESUMED) {
Bryce Lee84730a02018-04-03 14:10:04 -0700556 setResumedActivity(null, reason + " - onActivityStateChanged");
Bryce Leec4ab62a2018-03-05 14:19:26 -0800557 }
558
559 if (state == RESUMED) {
560 if (DEBUG_STACK) Slog.v(TAG_STACK, "set resumed activity to:" + record + " reason:"
561 + reason);
Bryce Lee84730a02018-04-03 14:10:04 -0700562 setResumedActivity(record, reason + " - onActivityStateChanged");
Wale Ogunwaled32da472018-11-16 07:19:28 -0800563 if (record == mRootActivityContainer.getTopResumedActivity()) {
Riddle Hsu3026e8d2018-08-03 15:50:53 +0800564 // TODO(b/111361570): Support multiple focused apps in WM
Andrii Kulianab132ee2018-07-24 22:10:21 +0800565 mService.setResumedActivityUncheckLocked(record, reason);
566 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800567 mStackSupervisor.mRecentTasks.add(record.getTaskRecord());
Bryce Leec4ab62a2018-03-05 14:19:26 -0800568 }
569 }
570
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700571 @Override
572 public void onConfigurationChanged(Configuration newParentConfig) {
573 final int prevWindowingMode = getWindowingMode();
Kazuki Takise048e2662018-06-27 17:05:11 +0900574 final boolean prevIsAlwaysOnTop = isAlwaysOnTop();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700575 final ActivityDisplay display = getDisplay();
Evan Roskyc5abbd82018-10-05 16:02:19 -0700576 final int prevRotation = getWindowConfiguration().getRotation();
577 final int prevDensity = getConfiguration().densityDpi;
578 final int prevScreenW = getConfiguration().screenWidthDp;
579 final int prevScreenH = getConfiguration().screenHeightDp;
Evan Roskye747c3e2018-10-30 20:06:41 -0700580
Evan Roskyc5abbd82018-10-05 16:02:19 -0700581 getBounds(mTmpRect); // previous bounds
Evan Roskye747c3e2018-10-30 20:06:41 -0700582
583 super.onConfigurationChanged(newParentConfig);
Kazuki Takise048e2662018-06-27 17:05:11 +0900584 if (display == null) {
Evan Roskyc5abbd82018-10-05 16:02:19 -0700585 return;
586 }
Yunfan Chen279f5582018-12-12 15:24:50 -0800587 if (getTaskStack() == null) {
588 return;
589 }
Evan Roskyc5abbd82018-10-05 16:02:19 -0700590
591 // Update bounds if applicable
592 boolean hasNewOverrideBounds = false;
593 // Use override windowing mode to prevent extra bounds changes if inheriting the mode.
Evan Roskydfe3da72018-10-26 17:21:06 -0700594 if (getRequestedOverrideWindowingMode() == WINDOWING_MODE_PINNED) {
Evan Roskyc5abbd82018-10-05 16:02:19 -0700595 // Pinned calculation already includes rotation
596 mTmpRect2.set(mTmpRect);
Yunfan Chen279f5582018-12-12 15:24:50 -0800597 hasNewOverrideBounds = getTaskStack().calculatePinnedBoundsForConfigChange(mTmpRect2);
Evan Roskyc5abbd82018-10-05 16:02:19 -0700598 } else {
599 final int newRotation = getWindowConfiguration().getRotation();
600 if (!matchParentBounds()) {
601 // If the parent (display) has rotated, rotate our bounds to best-fit where their
602 // bounds were on the pre-rotated display.
603 if (prevRotation != newRotation) {
604 mTmpRect2.set(mTmpRect);
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800605 getDisplay().mDisplayContent
Evan Roskyc5abbd82018-10-05 16:02:19 -0700606 .rotateBounds(newParentConfig.windowConfiguration.getBounds(),
607 prevRotation, newRotation, mTmpRect2);
608 hasNewOverrideBounds = true;
609 }
610
611 // If entering split screen or if something about the available split area changes,
612 // recalculate the split windows to match the new configuration.
613 if (prevRotation != newRotation
614 || prevDensity != getConfiguration().densityDpi
615 || prevWindowingMode != getWindowingMode()
616 || prevScreenW != getConfiguration().screenWidthDp
617 || prevScreenH != getConfiguration().screenHeightDp) {
618 // Use override windowing mode to prevent extra bounds changes if inheriting
619 // the mode.
Evan Roskydfe3da72018-10-26 17:21:06 -0700620 if (getRequestedOverrideWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
621 || getRequestedOverrideWindowingMode()
Evan Roskyc5abbd82018-10-05 16:02:19 -0700622 == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY) {
623 mTmpRect2.set(mTmpRect);
Yunfan Chen279f5582018-12-12 15:24:50 -0800624 getTaskStack()
Evan Roskyc5abbd82018-10-05 16:02:19 -0700625 .calculateDockedBoundsForConfigChange(newParentConfig, mTmpRect2);
626 hasNewOverrideBounds = true;
627 }
628 }
629 }
630 }
631 if (getWindowingMode() != prevWindowingMode) {
632 // Use override windowing mode to prevent extra bounds changes if inheriting the mode.
Evan Roskydfe3da72018-10-26 17:21:06 -0700633 if (getRequestedOverrideWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Evan Roskyc5abbd82018-10-05 16:02:19 -0700634 getStackDockedModeBounds(null, null, mTmpRect2, mTmpRect3);
635 // immediately resize so docked bounds are available in onSplitScreenModeActivated
Evan Rosky1ac84462018-11-13 11:25:30 -0800636 setTaskDisplayedBounds(null);
637 setTaskBounds(mTmpRect2);
638 setBounds(mTmpRect2);
Evan Roskydfe3da72018-10-26 17:21:06 -0700639 } else if (
640 getRequestedOverrideWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY) {
Evan Roskyc5abbd82018-10-05 16:02:19 -0700641 Rect dockedBounds = display.getSplitScreenPrimaryStack().getBounds();
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800642 final boolean isMinimizedDock =
643 getDisplay().mDisplayContent.getDockedDividerController().isMinimizedDock();
Evan Roskyc5abbd82018-10-05 16:02:19 -0700644 if (isMinimizedDock) {
645 TaskRecord topTask = display.getSplitScreenPrimaryStack().topTask();
646 if (topTask != null) {
647 dockedBounds = topTask.getBounds();
648 }
649 }
650 getStackDockedModeBounds(dockedBounds, null, mTmpRect2, mTmpRect3);
651 hasNewOverrideBounds = true;
652 }
Kazuki Takise048e2662018-06-27 17:05:11 +0900653 }
654 if (prevWindowingMode != getWindowingMode()) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700655 display.onStackWindowingModeChanged(this);
656 }
Evan Roskyc5abbd82018-10-05 16:02:19 -0700657 if (hasNewOverrideBounds) {
Wale Ogunwaled32da472018-11-16 07:19:28 -0800658 mRootActivityContainer.resizeStack(this, mTmpRect2, null, null, PRESERVE_WINDOWS,
Evan Roskyc5abbd82018-10-05 16:02:19 -0700659 true /* allowResizeInDockedMode */, true /* deferResume */);
Evan Roskye747c3e2018-10-30 20:06:41 -0700660 }
Kazuki Takise048e2662018-06-27 17:05:11 +0900661 if (prevIsAlwaysOnTop != isAlwaysOnTop()) {
662 // Since always on top is only on when the stack is freeform or pinned, the state
663 // can be toggled when the windowing mode changes. We must make sure the stack is
664 // placed properly when always on top state changes.
Riddle Hsu57831b52018-07-27 00:31:48 +0800665 display.positionChildAtTop(this, false /* includingParents */);
Kazuki Takise048e2662018-06-27 17:05:11 +0900666 }
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700667 }
668
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700669 @Override
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800670 public void setWindowingMode(int windowingMode) {
Winson Chung3e36f822018-01-16 12:06:04 -0800671 setWindowingMode(windowingMode, false /* animate */, false /* showRecents */,
Evan Roskyc5abbd82018-10-05 16:02:19 -0700672 false /* enteringSplitScreenMode */, false /* deferEnsuringVisibility */,
673 false /* creating */);
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800674 }
675
Evan Rosky10475742018-09-05 19:02:48 -0700676 /**
677 * A transient windowing mode is one which activities enter into temporarily. Examples of this
678 * are Split window modes and pip. Non-transient modes are modes that displays can adopt.
679 *
680 * @param windowingMode the windowingMode to test for transient-ness.
681 * @return {@code true} if the windowing mode is transient, {@code false} otherwise.
682 */
683 private static boolean isTransientWindowingMode(int windowingMode) {
684 // TODO(b/114842032): add PIP if/when it uses mode transitions instead of task reparenting
685 return windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
686 || windowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
687 }
688
689 /**
690 * Specialization of {@link #setWindowingMode(int)} for this subclass.
691 *
692 * @param preferredWindowingMode the preferred windowing mode. This may not be honored depending
693 * on the state of things. For example, WINDOWING_MODE_UNDEFINED will resolve to the
694 * previous non-transient mode if this stack is currently in a transient mode.
695 * @param animate Can be used to prevent animation.
696 * @param showRecents Controls whether recents is shown on the other side of a split while
697 * entering split mode.
698 * @param enteringSplitScreenMode {@code true} if entering split mode.
699 * @param deferEnsuringVisibility Whether visibility updates are deferred. This is set when
700 * many operations (which can effect visibility) are being performed in bulk.
Evan Roskyc5abbd82018-10-05 16:02:19 -0700701 * @param creating {@code true} if this is being run during ActivityStack construction.
Evan Rosky10475742018-09-05 19:02:48 -0700702 */
Wale Ogunwaledf262f52017-12-07 18:17:12 -0800703 void setWindowingMode(int preferredWindowingMode, boolean animate, boolean showRecents,
Evan Roskyc5abbd82018-10-05 16:02:19 -0700704 boolean enteringSplitScreenMode, boolean deferEnsuringVisibility, boolean creating) {
Tiger Huang6dcbee62019-02-20 21:45:55 +0800705 mWindowManager.inSurfaceTransaction(() -> setWindowingModeInSurfaceTransaction(
706 preferredWindowingMode, animate, showRecents, enteringSplitScreenMode,
707 deferEnsuringVisibility, creating));
708 }
709
710 private void setWindowingModeInSurfaceTransaction(int preferredWindowingMode, boolean animate,
711 boolean showRecents, boolean enteringSplitScreenMode, boolean deferEnsuringVisibility,
712 boolean creating) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800713 final int currentMode = getWindowingMode();
Evan Roskydfe3da72018-10-26 17:21:06 -0700714 final int currentOverrideMode = getRequestedOverrideWindowingMode();
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800715 final ActivityDisplay display = getDisplay();
716 final TaskRecord topTask = topTask();
717 final ActivityStack splitScreenStack = display.getSplitScreenPrimaryStack();
Evan Rosky9ba524e2018-01-03 16:27:56 -0800718 int windowingMode = preferredWindowingMode;
Evan Rosky10475742018-09-05 19:02:48 -0700719 if (preferredWindowingMode == WINDOWING_MODE_UNDEFINED
720 && isTransientWindowingMode(currentMode)) {
721 // Leaving a transient mode. Interpret UNDEFINED as "restore"
722 windowingMode = mRestoreOverrideWindowingMode;
723 }
724 mTmpOptions.setLaunchWindowingMode(windowingMode);
725
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800726 // Need to make sure windowing mode is supported. If we in the process of creating the stack
727 // no need to resolve the windowing mode again as it is already resolved to the right mode.
Evan Rosky9ba524e2018-01-03 16:27:56 -0800728 if (!creating) {
729 windowingMode = display.validateWindowingMode(windowingMode,
730 null /* ActivityRecord */, topTask, getActivityType());
731 }
732 if (splitScreenStack == this
733 && windowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY) {
734 // Resolution to split-screen secondary for the primary split-screen stack means
Evan Rosky10475742018-09-05 19:02:48 -0700735 // we want to leave split-screen mode.
736 windowingMode = mRestoreOverrideWindowingMode;
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800737 }
738
Wale Ogunwaleac36e4d2017-11-29 13:30:26 -0800739 final boolean alreadyInSplitScreenMode = display.hasSplitScreenPrimaryStack();
740
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800741 // Don't send non-resizeable notifications if the windowing mode changed was a side effect
742 // of us entering split-screen mode.
743 final boolean sendNonResizeableNotification = !enteringSplitScreenMode;
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800744 // Take any required action due to us not supporting the preferred windowing mode.
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800745 if (alreadyInSplitScreenMode && windowingMode == WINDOWING_MODE_FULLSCREEN
746 && sendNonResizeableNotification && isActivityTypeStandardOrUndefined()) {
747 final boolean preferredSplitScreen =
748 preferredWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
749 || preferredWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
750 if (preferredSplitScreen || creating) {
751 // Looks like we can't launch in split screen mode or the stack we are launching
752 // doesn't support split-screen mode, go ahead an dismiss split-screen and display a
753 // warning toast about it.
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -0700754 mService.getTaskChangeNotificationController().notifyActivityDismissingDockedStack();
Evan Roskyc5abbd82018-10-05 16:02:19 -0700755 final ActivityStack primarySplitStack = display.getSplitScreenPrimaryStack();
Tiger Huang6dcbee62019-02-20 21:45:55 +0800756 primarySplitStack.setWindowingModeInSurfaceTransaction(WINDOWING_MODE_UNDEFINED,
Andrii Kulian9da138a2018-04-24 17:12:44 -0700757 false /* animate */, false /* showRecents */,
Evan Roskyc5abbd82018-10-05 16:02:19 -0700758 false /* enteringSplitScreenMode */, true /* deferEnsuringVisibility */,
759 primarySplitStack == this ? creating : false);
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800760 }
761 }
762
763 if (currentMode == windowingMode) {
Evan Rosky10475742018-09-05 19:02:48 -0700764 // You are already in the window mode, so we can skip most of the work below. However,
765 // it's possible that we have inherited the current windowing mode from a parent. So,
766 // fulfill this method's contract by setting the override mode directly.
Evan Roskydfe3da72018-10-26 17:21:06 -0700767 getRequestedOverrideConfiguration().windowConfiguration.setWindowingMode(windowingMode);
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800768 return;
769 }
770
771 final WindowManagerService wm = mService.mWindowManager;
772 final ActivityRecord topActivity = getTopActivity();
773
Evan Rosky10475742018-09-05 19:02:48 -0700774 // For now, assume that the Stack's windowing mode is what will actually be used
775 // by it's activities. In the future, there may be situations where this doesn't
776 // happen; so at that point, this message will need to handle that.
777 int likelyResolvedMode = windowingMode;
778 if (windowingMode == WINDOWING_MODE_UNDEFINED) {
779 final ConfigurationContainer parent = getParent();
780 likelyResolvedMode = parent != null ? parent.getWindowingMode()
781 : WINDOWING_MODE_FULLSCREEN;
782 }
783 if (sendNonResizeableNotification && likelyResolvedMode != WINDOWING_MODE_FULLSCREEN
Wale Ogunwaledf262f52017-12-07 18:17:12 -0800784 && topActivity != null && topActivity.isNonResizableOrForcedResizable()
785 && !topActivity.noDisplay) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800786 // Inform the user that they are starting an app that may not work correctly in
787 // multi-window mode.
788 final String packageName = topActivity.appInfo.packageName;
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -0700789 mService.getTaskChangeNotificationController().notifyActivityForcedResizable(
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800790 topTask.taskId, FORCED_RESIZEABLE_REASON_SPLIT_SCREEN, packageName);
791 }
792
793 wm.deferSurfaceLayout();
794 try {
795 if (!animate && topActivity != null) {
Jorim Jaggifa9ed962018-01-25 00:16:49 +0100796 mStackSupervisor.mNoAnimActivities.add(topActivity);
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800797 }
798 super.setWindowingMode(windowingMode);
Evan Rosky7d0705f2018-07-30 12:30:13 -0700799 // setWindowingMode triggers an onConfigurationChanged cascade which can result in a
800 // different resolved windowing mode (usually when preferredWindowingMode is UNDEFINED).
Evan Rosky9ba524e2018-01-03 16:27:56 -0800801 windowingMode = getWindowingMode();
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800802
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800803 if (creating) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800804 // Nothing else to do if we don't have a window container yet. E.g. call from ctor.
805 return;
806 }
807
808 if (windowingMode == WINDOWING_MODE_PINNED || currentMode == WINDOWING_MODE_PINNED) {
809 // TODO: Need to remove use of PinnedActivityStack for this to be supported.
810 // NOTE: Need to ASS.scheduleUpdatePictureInPictureModeIfNeeded() in
811 // setWindowModeUnchecked() when this support is added. See TaskRecord.reparent()
812 throw new IllegalArgumentException(
813 "Changing pinned windowing mode not currently supported");
814 }
815
816 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY && splitScreenStack != null) {
817 // We already have a split-screen stack in this display, so just move the tasks over.
818 // TODO: Figure-out how to do all the stuff in
819 // AMS.setTaskWindowingModeSplitScreenPrimary
820 throw new IllegalArgumentException("Setting primary split-screen windowing mode"
821 + " while there is already one isn't currently supported");
822 //return;
823 }
Evan Rosky10475742018-09-05 19:02:48 -0700824 if (isTransientWindowingMode(windowingMode) && !isTransientWindowingMode(currentMode)) {
825 mRestoreOverrideWindowingMode = currentOverrideMode;
826 }
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800827
828 mTmpRect2.setEmpty();
829 if (windowingMode != WINDOWING_MODE_FULLSCREEN) {
Yunfan Chen279f5582018-12-12 15:24:50 -0800830 if (mTaskStack.matchParentBounds()) {
831 mTmpRect2.setEmpty();
832 } else {
833 mTaskStack.getRawBounds(mTmpRect2);
834 }
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800835 }
836
Evan Roskydfe3da72018-10-26 17:21:06 -0700837 if (!Objects.equals(getRequestedOverrideBounds(), mTmpRect2)) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800838 resize(mTmpRect2, null /* tempTaskBounds */, null /* tempTaskInsetBounds */);
839 }
840 } finally {
Wale Ogunwalec59b4f62017-11-30 11:05:43 -0800841 if (showRecents && !alreadyInSplitScreenMode && mDisplayId == DEFAULT_DISPLAY
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800842 && windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
843 // Make sure recents stack exist when creating a dock stack as it normally needs to
844 // be on the other side of the docked stack and we make visibility decisions based
845 // on that.
846 // TODO: This is only here to help out with the case where recents stack doesn't
847 // exist yet. For that case the initial size of the split-screen stack will be the
848 // the one where the home stack is visible since recents isn't visible yet, but the
849 // divider will be off. I think we should just make the initial bounds that of home
850 // so that the divider matches and remove this logic.
Winson Chungc1674272018-02-21 10:15:17 -0800851 // TODO: This is currently only called when entering split-screen while in another
852 // task, and from the tests
Matthew Ng30307122018-04-13 11:36:34 -0700853 // TODO (b/78247419): Check if launcher and overview are same then move home stack
854 // instead of recents stack. Then fix the rotation animation from fullscreen to
855 // minimized mode
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800856 final ActivityStack recentStack = display.getOrCreateStack(
857 WINDOWING_MODE_SPLIT_SCREEN_SECONDARY, ACTIVITY_TYPE_RECENTS,
858 true /* onTop */);
859 recentStack.moveToFront("setWindowingMode");
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800860 // If task moved to docked stack - show recents if needed.
Winson Chungdff7a732017-12-11 12:17:06 -0800861 mService.mWindowManager.showRecentApps();
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800862 }
863 wm.continueSurfaceLayout();
864 }
865
Andrii Kulian9da138a2018-04-24 17:12:44 -0700866 if (!deferEnsuringVisibility) {
Wale Ogunwaled32da472018-11-16 07:19:28 -0800867 mRootActivityContainer.ensureActivitiesVisible(null, 0, PRESERVE_WINDOWS);
868 mRootActivityContainer.resumeFocusedStacksTopActivities();
Wale Ogunwale1dbc5c82017-12-08 08:12:20 -0800869 }
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800870 }
871
872 @Override
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700873 public boolean isCompatible(int windowingMode, int activityType) {
874 // TODO: Should we just move this to ConfigurationContainer?
875 if (activityType == ACTIVITY_TYPE_UNDEFINED) {
876 // Undefined activity types end up in a standard stack once the stack is created on a
877 // display, so they should be considered compatible.
878 activityType = ACTIVITY_TYPE_STANDARD;
879 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700880 return super.isCompatible(windowingMode, activityType);
881 }
882
Andrii Kulian839def92016-11-02 10:58:58 -0700883 /** Adds the stack to specified display and calls WindowManager to do the same. */
Riddle Hsu402b4402018-11-06 17:23:15 +0800884 void reparent(ActivityDisplay activityDisplay, boolean onTop, boolean displayRemoved) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700885 // TODO: We should probably resolve the windowing mode for the stack on the new display here
886 // so that it end up in a compatible mode in the new display. e.g. split-screen secondary.
Wale Ogunwale1666e312016-12-16 11:27:18 -0800887 removeFromDisplay();
Winson Chungf34c6022018-01-25 09:44:30 -0800888 // Reparent the window container before we try to update the position when adding it to
889 // the new display below
Wale Ogunwale1666e312016-12-16 11:27:18 -0800890 mTmpRect2.setEmpty();
Yunfan Chen279f5582018-12-12 15:24:50 -0800891 if (mTaskStack == null) {
892 // TODO: Remove after unification.
893 Log.w(TAG, "Task stack is not valid when reparenting.");
894 } else {
895 mTaskStack.reparent(activityDisplay.mDisplayId, mTmpRect2, onTop);
896 }
Evan Roskyc5abbd82018-10-05 16:02:19 -0700897 setBounds(mTmpRect2.isEmpty() ? null : mTmpRect2);
898 activityDisplay.addChild(this, onTop ? POSITION_TOP : POSITION_BOTTOM);
Riddle Hsu402b4402018-11-06 17:23:15 +0800899 if (!displayRemoved) {
900 postReparent();
901 }
902 }
903
904 /** Resume next focusable stack after reparenting to another display. */
905 void postReparent() {
Wale Ogunwale66e16852017-10-19 13:35:52 -0700906 adjustFocusToNextFocusableStack("reparent", true /* allowFocusSelf */);
Wale Ogunwaled32da472018-11-16 07:19:28 -0800907 mRootActivityContainer.resumeFocusedStacksTopActivities();
Andrii Kulian51c1b672017-04-07 18:39:32 -0700908 // Update visibility of activities before notifying WM. This way it won't try to resize
909 // windows that are no longer visible.
Wale Ogunwaled32da472018-11-16 07:19:28 -0800910 mRootActivityContainer.ensureActivitiesVisible(null /* starting */, 0 /* configChanges */,
Andrii Kulian51c1b672017-04-07 18:39:32 -0700911 !PRESERVE_WINDOWS);
Andrii Kulian839def92016-11-02 10:58:58 -0700912 }
913
914 /**
Andrii Kulian839def92016-11-02 10:58:58 -0700915 * Updates the inner state of the stack to remove it from its current parent, so it can be
916 * either destroyed completely or re-parented.
917 */
918 private void removeFromDisplay() {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700919 final ActivityDisplay display = getDisplay();
920 if (display != null) {
921 display.removeChild(this);
922 }
923 mDisplayId = INVALID_DISPLAY;
Andrii Kulian839def92016-11-02 10:58:58 -0700924 }
925
926 /** Removes the stack completely. Also calls WindowManager to do the same on its side. */
927 void remove() {
928 removeFromDisplay();
Yunfan Chen279f5582018-12-12 15:24:50 -0800929 if (mTaskStack != null) {
930 mTaskStack.removeIfPossible();
931 mTaskStack = null;
932 }
Andrii Kulian6d6fb402016-10-26 16:15:25 -0700933 onParentChanged();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700934 }
935
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700936 ActivityDisplay getDisplay() {
Wale Ogunwaled32da472018-11-16 07:19:28 -0800937 return mRootActivityContainer.getActivityDisplay(mDisplayId);
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700938 }
939
Matthew Ngaa2b6202017-02-10 14:48:21 -0800940 /**
Evan Roskyc5abbd82018-10-05 16:02:19 -0700941 * @see #getStackDockedModeBounds(Rect, Rect, Rect, Rect)
Matthew Ngaa2b6202017-02-10 14:48:21 -0800942 */
Evan Roskyc5abbd82018-10-05 16:02:19 -0700943 void getStackDockedModeBounds(Rect dockedBounds, Rect currentTempTaskBounds,
944 Rect outStackBounds, Rect outTempTaskBounds) {
Yunfan Chen279f5582018-12-12 15:24:50 -0800945 if (mTaskStack != null) {
946 mTaskStack.getStackDockedModeBoundsLocked(getParent().getConfiguration(), dockedBounds,
947 currentTempTaskBounds, outStackBounds, outTempTaskBounds);
948 } else {
949 outStackBounds.setEmpty();
950 outTempTaskBounds.setEmpty();
951 }
Wale Ogunwale1666e312016-12-16 11:27:18 -0800952 }
953
954 void prepareFreezingTaskBounds() {
Yunfan Chen279f5582018-12-12 15:24:50 -0800955 if (mTaskStack != null) {
956 // TODO: This cannot be false after unification.
957 mTaskStack.prepareFreezingTaskBounds();
958 }
Wale Ogunwale1666e312016-12-16 11:27:18 -0800959 }
960
Wale Ogunwale1666e312016-12-16 11:27:18 -0800961 void getWindowContainerBounds(Rect outBounds) {
Yunfan Chen279f5582018-12-12 15:24:50 -0800962 if (mTaskStack != null) {
963 mTaskStack.getBounds(outBounds);
Winson Chungb00dc5e2017-01-25 09:44:25 -0800964 return;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800965 }
966 outBounds.setEmpty();
967 }
968
Wale Ogunwale1666e312016-12-16 11:27:18 -0800969 void positionChildWindowContainerAtTop(TaskRecord child) {
Yunfan Chen279f5582018-12-12 15:24:50 -0800970 if (mTaskStack != null) {
971 // TODO: Remove after unification. This cannot be false after that.
972 mTaskStack.positionChildAtTop(child.getTask(), true /* includingParents */);
973 }
Wale Ogunwale1666e312016-12-16 11:27:18 -0800974 }
975
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800976 void positionChildWindowContainerAtBottom(TaskRecord child) {
Riddle Hsu138ef932018-10-26 17:12:06 +0800977 // If there are other focusable stacks on the display, the z-order of the display should not
978 // be changed just because a task was placed at the bottom. E.g. if it is moving the topmost
979 // task to bottom, the next focusable stack on the same display should be focused.
980 final ActivityStack nextFocusableStack = getDisplay().getNextFocusableStack(
981 child.getStack(), true /* ignoreCurrent */);
Yunfan Chen279f5582018-12-12 15:24:50 -0800982 if (mTaskStack != null) {
983 // TODO: Remove after unification. This cannot be false after that.
984 mTaskStack.positionChildAtBottom(child.getTask(),
985 nextFocusableStack == null /* includingParents */);
986 }
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800987 }
988
Jorim Jaggi192086e2016-03-11 17:17:03 +0100989 /**
Winson Chung8bca9e42017-04-16 15:59:43 -0700990 * Returns whether to defer the scheduling of the multi-window mode.
991 */
992 boolean deferScheduleMultiWindowModeChanged() {
Yunfan Chen279f5582018-12-12 15:24:50 -0800993 if (inPinnedWindowingMode()) {
994 // For the pinned stack, the deferring of the multi-window mode changed is tied to the
995 // transition animation into picture-in-picture, and is called once the animation
996 // completes, or is interrupted in a way that would leave the stack in a non-fullscreen
997 // state.
998 // @see BoundsAnimationController
999 // @see BoundsAnimationControllerTests
1000 if (getTaskStack() == null) return false;
1001 return getTaskStack().deferScheduleMultiWindowModeChanged();
1002 }
Winson Chung8bca9e42017-04-16 15:59:43 -07001003 return false;
1004 }
1005
1006 /**
Jorim Jaggi192086e2016-03-11 17:17:03 +01001007 * Defers updating the bounds of the stack. If the stack was resized/repositioned while
1008 * deferring, the bounds will update in {@link #continueUpdateBounds()}.
1009 */
1010 void deferUpdateBounds() {
1011 if (!mUpdateBoundsDeferred) {
1012 mUpdateBoundsDeferred = true;
1013 mUpdateBoundsDeferredCalled = false;
1014 }
1015 }
1016
1017 /**
1018 * Continues updating bounds after updates have been deferred. If there was a resize attempt
1019 * between {@link #deferUpdateBounds()} and {@link #continueUpdateBounds()}, the stack will
1020 * be resized to that bounds.
1021 */
1022 void continueUpdateBounds() {
Evan Rosky1ac84462018-11-13 11:25:30 -08001023 if (mUpdateBoundsDeferred) {
1024 mUpdateBoundsDeferred = false;
1025 if (mUpdateBoundsDeferredCalled) {
1026 setTaskBounds(mDeferredBounds);
1027 setBounds(mDeferredBounds);
1028 }
1029 if (mUpdateDisplayedBoundsDeferredCalled) {
1030 setTaskDisplayedBounds(mDeferredDisplayedBounds);
1031 }
Jorim Jaggi192086e2016-03-11 17:17:03 +01001032 }
1033 }
1034
Evan Rosky1ac84462018-11-13 11:25:30 -08001035 boolean updateBoundsAllowed(Rect bounds) {
Jorim Jaggi192086e2016-03-11 17:17:03 +01001036 if (!mUpdateBoundsDeferred) {
1037 return true;
1038 }
1039 if (bounds != null) {
1040 mDeferredBounds.set(bounds);
1041 } else {
1042 mDeferredBounds.setEmpty();
1043 }
Jorim Jaggi192086e2016-03-11 17:17:03 +01001044 mUpdateBoundsDeferredCalled = true;
1045 return false;
1046 }
1047
Evan Rosky1ac84462018-11-13 11:25:30 -08001048 boolean updateDisplayedBoundsAllowed(Rect bounds) {
1049 if (!mUpdateBoundsDeferred) {
1050 return true;
1051 }
1052 if (bounds != null) {
1053 mDeferredDisplayedBounds.set(bounds);
1054 } else {
1055 mDeferredDisplayedBounds.setEmpty();
1056 }
1057 mUpdateDisplayedBoundsDeferredCalled = true;
1058 return false;
1059 }
1060
Bryce Leef3c6a472017-11-14 14:53:06 -08001061 @Override
1062 public int setBounds(Rect bounds) {
1063 return super.setBounds(!inMultiWindowMode() ? null : bounds);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001064 }
Craig Mautner5962b122012-10-05 14:45:52 -07001065
Wale Ogunwale42f07d92017-05-01 21:32:58 -07001066 ActivityRecord topRunningActivityLocked() {
Winson Chung3f103eb2017-04-12 21:53:59 -07001067 return topRunningActivityLocked(false /* focusableOnly */);
1068 }
1069
Jorim Jaggiea039a82017-08-02 14:37:49 +02001070 void getAllRunningVisibleActivitiesLocked(ArrayList<ActivityRecord> outActivities) {
1071 outActivities.clear();
1072 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1073 mTaskHistory.get(taskNdx).getAllRunningVisibleActivitiesLocked(outActivities);
1074 }
1075 }
1076
Andrii Kulian52d255c2018-07-13 11:32:19 -07001077 ActivityRecord topRunningActivityLocked(boolean focusableOnly) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08001078 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001079 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Winson Chung3f103eb2017-04-12 21:53:59 -07001080 if (r != null && (!focusableOnly || r.isFocusable())) {
Craig Mautner6b74cb52013-09-27 17:02:21 -07001081 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -08001082 }
1083 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001084 return null;
1085 }
1086
Wale Ogunwale42f07d92017-05-01 21:32:58 -07001087 ActivityRecord topRunningNonOverlayTaskActivity() {
1088 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1089 final TaskRecord task = mTaskHistory.get(taskNdx);
1090 final ArrayList<ActivityRecord> activities = task.mActivities;
1091 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1092 final ActivityRecord r = activities.get(activityNdx);
1093 if (!r.finishing && !r.mTaskOverlay) {
1094 return r;
1095 }
1096 }
1097 }
1098 return null;
1099 }
1100
1101 ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08001102 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1103 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -08001104 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -08001105 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1106 ActivityRecord r = activities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -07001107 if (!r.finishing && !r.delayedResume && r != notTop && r.okToShowLocked()) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08001108 return r;
1109 }
1110 }
1111 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001112 return null;
1113 }
1114
1115 /**
Wale Ogunwaled32da472018-11-16 07:19:28 -08001116 * This is a simplified version of topRunningActivity that provides a number of
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001117 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -08001118 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001119 * @param token If non-null, any history records matching this token will be skipped.
1120 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -08001121 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001122 * @return Returns the HistoryRecord of the next activity on the stack.
1123 */
1124 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08001125 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1126 TaskRecord task = mTaskHistory.get(taskNdx);
1127 if (task.taskId == taskId) {
1128 continue;
1129 }
1130 ArrayList<ActivityRecord> activities = task.mActivities;
1131 for (int i = activities.size() - 1; i >= 0; --i) {
1132 final ActivityRecord r = activities.get(i);
1133 // Note: the taskId check depends on real taskId fields being non-zero
Chong Zhang87761972016-08-22 13:53:24 -07001134 if (!r.finishing && (token != r.appToken) && r.okToShowLocked()) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08001135 return r;
1136 }
1137 }
1138 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001139 return null;
1140 }
1141
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001142 ActivityRecord getTopActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001143 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001144 final ActivityRecord r = mTaskHistory.get(taskNdx).getTopActivity();
1145 if (r != null) {
1146 return r;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001147 }
1148 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001149 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001150 }
1151
Craig Mautner9e14d0f2013-05-01 11:26:09 -07001152 final TaskRecord topTask() {
1153 final int size = mTaskHistory.size();
1154 if (size > 0) {
1155 return mTaskHistory.get(size - 1);
1156 }
1157 return null;
1158 }
1159
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001160 private TaskRecord bottomTask() {
Winson Chung1cebea62017-06-26 17:22:27 -07001161 if (mTaskHistory.isEmpty()) {
1162 return null;
1163 }
1164 return mTaskHistory.get(0);
1165 }
1166
Craig Mautnerd2328952013-03-05 12:46:26 -08001167 TaskRecord taskForIdLocked(int id) {
1168 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1169 final TaskRecord task = mTaskHistory.get(taskNdx);
1170 if (task.taskId == id) {
1171 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001172 }
1173 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001174 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001175 }
1176
Craig Mautnerd2328952013-03-05 12:46:26 -08001177 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001178 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -08001179 return isInStackLocked(r);
1180 }
1181
1182 ActivityRecord isInStackLocked(ActivityRecord r) {
1183 if (r == null) {
1184 return null;
1185 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001186 final TaskRecord task = r.getTaskRecord();
1187 final ActivityStack stack = r.getActivityStack();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001188 if (stack != null && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
1189 if (stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -08001190 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -08001191 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001192 }
Craig Mautnerd2328952013-03-05 12:46:26 -08001193 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001194 }
1195
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001196 boolean isInStackLocked(TaskRecord task) {
1197 return mTaskHistory.contains(task);
1198 }
1199
1200 /** Checks if there are tasks with specific UID in the stack. */
1201 boolean isUidPresent(int uid) {
1202 for (TaskRecord task : mTaskHistory) {
David Stevens82ea6cb2017-03-03 16:18:50 -08001203 for (ActivityRecord r : task.mActivities) {
1204 if (r.getUid() == uid) {
1205 return true;
1206 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001207 }
1208 }
1209 return false;
1210 }
1211
1212 /** Get all UIDs that are present in the stack. */
1213 void getPresentUIDs(IntArray presentUIDs) {
1214 for (TaskRecord task : mTaskHistory) {
David Stevens82ea6cb2017-03-03 16:18:50 -08001215 for (ActivityRecord r : task.mActivities) {
1216 presentUIDs.add(r.getUid());
1217 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001218 }
1219 }
1220
Wale Ogunwale9e737db2018-12-17 15:42:37 -08001221 /** @return true if the stack can only contain one task */
1222 boolean isSingleTaskInstance() {
1223 final ActivityDisplay display = getDisplay();
1224 return display != null && display.isSingleTaskInstance();
1225 }
1226
Winson Chungabb433b2017-03-24 09:35:42 -07001227 final void removeActivitiesFromLRUListLocked(TaskRecord task) {
1228 for (ActivityRecord r : task.mActivities) {
1229 mLRUActivities.remove(r);
1230 }
1231 }
1232
Craig Mautner2420ead2013-04-01 17:13:20 -07001233 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001234 final boolean hadit = mLRUActivities.remove(r);
1235 mLRUActivities.add(r);
1236 return hadit;
1237 }
1238
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001239 final boolean isHomeOrRecentsStack() {
Wale Ogunwale68278562017-09-23 17:13:55 -07001240 return isActivityTypeHome() || isActivityTypeRecents();
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001241 }
1242
Craig Mautnere0a38842013-12-16 16:14:02 -08001243 final boolean isOnHomeDisplay() {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001244 return mDisplayId == DEFAULT_DISPLAY;
Craig Mautnere0a38842013-12-16 16:14:02 -08001245 }
1246
Wale Ogunwale66e16852017-10-19 13:35:52 -07001247 private boolean returnsToHomeStack() {
1248 return !inMultiWindowMode()
1249 && !mTaskHistory.isEmpty()
1250 && mTaskHistory.get(0).returnsToHomeStack();
1251 }
1252
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001253 void moveToFront(String reason) {
1254 moveToFront(reason, null);
1255 }
1256
1257 /**
1258 * @param reason The reason for moving the stack to the front.
1259 * @param task If non-null, the task will be moved to the top of the stack.
1260 * */
1261 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001262 if (!isAttached()) {
1263 return;
1264 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07001265
Wale Ogunwalebb285872018-03-01 13:05:30 -08001266 final ActivityDisplay display = getDisplay();
1267
1268 if (inSplitScreenSecondaryWindowingMode()) {
1269 // If the stack is in split-screen seconardy mode, we need to make sure we move the
1270 // primary split-screen stack forward in the case it is currently behind a fullscreen
1271 // stack so both halves of the split-screen appear on-top and the fullscreen stack isn't
1272 // cutting between them.
1273 // TODO(b/70677280): This is a workaround until we can fix as part of b/70677280.
1274 final ActivityStack topFullScreenStack =
1275 display.getTopStackInWindowingMode(WINDOWING_MODE_FULLSCREEN);
1276 if (topFullScreenStack != null) {
1277 final ActivityStack primarySplitScreenStack = display.getSplitScreenPrimaryStack();
1278 if (display.getIndexOf(topFullScreenStack)
1279 > display.getIndexOf(primarySplitScreenStack)) {
1280 primarySplitScreenStack.moveToFront(reason + " splitScreenToTop");
1281 }
1282 }
1283 }
1284
Wale Ogunwale66e16852017-10-19 13:35:52 -07001285 if (!isActivityTypeHome() && returnsToHomeStack()) {
1286 // Make sure the home stack is behind this stack since that is where we should return to
1287 // when this stack is no longer visible.
Louis Changbd48dca2018-08-29 17:44:34 +08001288 display.moveHomeStackToFront(reason + " returnToHome");
Wale Ogunwale66e16852017-10-19 13:35:52 -07001289 }
1290
Riddle Hsubbb63c22018-10-03 12:28:29 +08001291 final boolean movingTask = task != null;
1292 display.positionChildAtTop(this, !movingTask /* includingParents */, reason);
1293 if (movingTask) {
Andrii Kulian52d255c2018-07-13 11:32:19 -07001294 // This also moves the entire hierarchy branch to top, including parents
Riddle Hsubbb63c22018-10-03 12:28:29 +08001295 insertTaskAtTop(task, null /* starting */);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001296 }
Craig Mautnere0a38842013-12-16 16:14:02 -08001297 }
1298
Evan Rosky9c448172017-11-02 14:19:27 -07001299 /**
1300 * @param reason The reason for moving the stack to the back.
1301 * @param task If non-null, the task will be moved to the bottom of the stack.
1302 **/
1303 void moveToBack(String reason, TaskRecord task) {
1304 if (!isAttached()) {
1305 return;
1306 }
1307
Bryce Lee8cab4a02018-01-05 09:00:49 -08001308 /**
1309 * The intent behind moving a primary split screen stack to the back is usually to hide
1310 * behind the home stack. Exit split screen in this case.
1311 */
1312 if (getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Evan Rosky10475742018-09-05 19:02:48 -07001313 setWindowingMode(WINDOWING_MODE_UNDEFINED);
Bryce Lee8cab4a02018-01-05 09:00:49 -08001314 }
1315
Riddle Hsubbb63c22018-10-03 12:28:29 +08001316 getDisplay().positionChildAtBottom(this, reason);
Evan Rosky9c448172017-11-02 14:19:27 -07001317 if (task != null) {
1318 insertTaskAtBottom(task);
Evan Rosky9c448172017-11-02 14:19:27 -07001319 }
1320 }
1321
Wale Ogunwaled046a012015-12-24 13:05:59 -08001322 boolean isFocusable() {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001323 final ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled32da472018-11-16 07:19:28 -08001324 return mRootActivityContainer.isFocusable(this, r != null && r.isFocusable());
Wale Ogunwaled046a012015-12-24 13:05:59 -08001325 }
1326
Riddle Hsu75016992018-09-20 20:37:14 +08001327 boolean isFocusableAndVisible() {
1328 return isFocusable() && shouldBeVisible(null /* starting */);
1329 }
1330
Craig Mautnere0a38842013-12-16 16:14:02 -08001331 final boolean isAttached() {
Riddle Hsu402b4402018-11-06 17:23:15 +08001332 final ActivityDisplay display = getDisplay();
1333 return display != null && !display.isRemoved();
Craig Mautner4a1cb222013-12-04 16:14:06 -08001334 }
1335
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001336 /**
Wale Ogunwale39381972015-12-17 17:15:29 -08001337 * Returns the top activity in any existing task matching the given Intent in the input result.
1338 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001339 */
Wale Ogunwale39381972015-12-17 17:15:29 -08001340 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001341 Intent intent = target.intent;
1342 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001343 ComponentName cls = intent.getComponent();
1344 if (info.targetActivity != null) {
1345 cls = new ComponentName(info.packageName, info.targetActivity);
1346 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001347 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -07001348 boolean isDocument = intent != null & intent.isDocument();
1349 // If documentData is non-null then it must match the existing task data.
1350 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -08001351
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001352 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -08001353 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1354 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001355 if (task.voiceSession != null) {
1356 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001357 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -07001358 continue;
1359 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001360 if (task.userId != userId) {
1361 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001362 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -07001363 continue;
1364 }
Bryce Lee9f6affd2017-09-01 09:18:35 -07001365
1366 // Overlays should not be considered as the task's logical top activity.
1367 final ActivityRecord r = task.getTopActivity(false /* includeOverlays */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001368 if (r == null || r.finishing || r.mUserId != userId ||
Craig Mautner000f0022013-02-26 15:04:29 -08001369 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001370 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -08001371 continue;
1372 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001373 if (!r.hasCompatibleActivityType(target)) {
Chong Zhangb546f7e2015-08-05 14:21:36 -07001374 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
1375 continue;
1376 }
Craig Mautner000f0022013-02-26 15:04:29 -08001377
Craig Mautnerd00f4742014-03-12 14:17:26 -07001378 final Intent taskIntent = task.intent;
1379 final Intent affinityIntent = task.affinityIntent;
1380 final boolean taskIsDocument;
1381 final Uri taskDocumentData;
1382 if (taskIntent != null && taskIntent.isDocument()) {
1383 taskIsDocument = true;
1384 taskDocumentData = taskIntent.getData();
1385 } else if (affinityIntent != null && affinityIntent.isDocument()) {
1386 taskIsDocument = true;
1387 taskDocumentData = affinityIntent.getData();
1388 } else {
1389 taskIsDocument = false;
1390 taskDocumentData = null;
1391 }
1392
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001393 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -07001394 + taskIntent.getComponent().flattenToShortString()
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001395 + "/aff=" + r.getTaskRecord().rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001396 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Andrii Kulian206b9fa2016-06-02 13:18:01 -07001397 // TODO Refactor to remove duplications. Check if logic can be simplified.
1398 if (taskIntent != null && taskIntent.getComponent() != null &&
Craig Mautnerffcfcaa2014-06-05 09:54:38 -07001399 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -07001400 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001401 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -08001402 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001403 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
1404 "For Intent " + intent + " bringing to top: " + r.intent);
Louis Changc85b1a32018-08-14 16:40:53 +08001405 result.mRecord = r;
1406 result.mIdealMatch = true;
Wale Ogunwale39381972015-12-17 17:15:29 -08001407 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -07001408 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
1409 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -07001410 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001411 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -08001412 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001413 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
1414 "For Intent " + intent + " bringing to top: " + r.intent);
Louis Changc85b1a32018-08-14 16:40:53 +08001415 result.mRecord = r;
1416 result.mIdealMatch = true;
Wale Ogunwale39381972015-12-17 17:15:29 -08001417 break;
Andrii Kulian206b9fa2016-06-02 13:18:01 -07001418 } else if (!isDocument && !taskIsDocument
Louis Changc85b1a32018-08-14 16:40:53 +08001419 && result.mRecord == null && task.rootAffinity != null) {
Andrii Kulian206b9fa2016-06-02 13:18:01 -07001420 if (task.rootAffinity.equals(target.taskAffinity)) {
1421 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
1422 // It is possible for multiple tasks to have the same root affinity especially
1423 // if they are in separate stacks. We save off this candidate, but keep looking
1424 // to see if there is a better candidate.
Louis Changc85b1a32018-08-14 16:40:53 +08001425 result.mRecord = r;
1426 result.mIdealMatch = false;
Andrii Kulian206b9fa2016-06-02 13:18:01 -07001427 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001428 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001429 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001430 }
1431
1432 /**
1433 * Returns the first activity (starting from the top of the stack) that
1434 * is the same as the given activity. Returns null if no such activity
1435 * is found.
1436 */
Andrii Kuliand3bbb132016-06-16 16:00:20 -07001437 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info,
1438 boolean compareIntentFilters) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001439 ComponentName cls = intent.getComponent();
1440 if (info.targetActivity != null) {
1441 cls = new ComponentName(info.packageName, info.targetActivity);
1442 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001443 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001444
Craig Mautner000f0022013-02-26 15:04:29 -08001445 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001446 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001447 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001448
Craig Mautner000f0022013-02-26 15:04:29 -08001449 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1450 ActivityRecord r = activities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -07001451 if (!r.okToShowLocked()) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -07001452 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001453 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001454 if (!r.finishing && r.mUserId == userId) {
Andrii Kuliand3bbb132016-06-16 16:00:20 -07001455 if (compareIntentFilters) {
1456 if (r.intent.filterEquals(intent)) {
1457 return r;
1458 }
1459 } else {
1460 if (r.intent.getComponent().equals(cls)) {
1461 return r;
1462 }
1463 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001464 }
1465 }
1466 }
1467
1468 return null;
1469 }
1470
Amith Yamasani742a6712011-05-04 14:49:28 -07001471 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -07001472 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -07001473 */
Craig Mautner93529a42013-10-04 15:03:13 -07001474 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001475 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -07001476 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001477 }
1478 mCurrentUser = userId;
1479
1480 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001481 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -08001482 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07001483 final TaskRecord task = mTaskHistory.get(i);
1484
Chong Zhang87761972016-08-22 13:53:24 -07001485 if (task.okToShowLocked()) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001486 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUser: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -07001487 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001488 mTaskHistory.remove(i);
1489 mTaskHistory.add(task);
1490 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -08001491 // Use same value for i.
1492 } else {
1493 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001494 }
1495 }
Amith Yamasani742a6712011-05-04 14:49:28 -07001496 }
1497
Craig Mautner2420ead2013-04-01 17:13:20 -07001498 void minimalResumeActivityLocked(ActivityRecord r) {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001499 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + r + " (starting new instance)"
1500 + " callers=" + Debug.getCallers(5));
Bryce Leec4ab62a2018-03-05 14:19:26 -08001501 r.setState(RESUMED, "minimalResumeActivityLocked");
Andrii Kulian21713ac2016-10-12 22:05:05 -07001502 r.completeResumeLocked();
Andrii Kulian86e70fc2019-02-12 11:04:10 +00001503 mStackSupervisor.updateTopResumedActivityIfNeeded();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001504 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
1505 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001506 }
1507
Andrii Kulian21713ac2016-10-12 22:05:05 -07001508 private void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -07001509 // Make sure that there is no activity waiting for this to launch.
Vishnu Nair132ee832018-09-28 15:00:05 -07001510 if (!mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
Craig Mautner5c494542013-09-06 11:59:38 -07001511 mStackSupervisor.removeTimeoutsForActivityLocked(r);
1512 mStackSupervisor.scheduleIdleTimeoutLocked(r);
1513 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001514 }
1515
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001516 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001517 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001518 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1519 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1520 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1521 activities.get(activityNdx).setSleeping(false);
1522 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001523 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -08001524 if (mPausingActivity != null) {
1525 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
1526 activityPausedLocked(mPausingActivity.appToken, true);
1527 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001528 }
1529
Todd Kennedy39bfee52016-02-24 10:28:21 -08001530 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
1531 final String packageName = aInfo.packageName;
Makoto Onuki8b9963a2017-05-18 13:56:30 -07001532 final int userId = UserHandle.getUserId(aInfo.uid);
1533
Todd Kennedy39bfee52016-02-24 10:28:21 -08001534 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1535 final List<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1536 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Makoto Onuki8b9963a2017-05-18 13:56:30 -07001537 final ActivityRecord ar = activities.get(activityNdx);
1538
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001539 if ((userId == ar.mUserId) && packageName.equals(ar.packageName)) {
Philip P. Moltmanncff8f0f2018-03-27 12:51:51 -07001540 ar.updateApplicationInfo(aInfo);
Todd Kennedy39bfee52016-02-24 10:28:21 -08001541 }
1542 }
1543 }
1544 }
1545
David Stevens9440dc82017-03-16 19:00:20 -07001546 void checkReadyForSleep() {
1547 if (shouldSleepActivities() && goToSleepIfPossible(false /* shuttingDown */)) {
1548 mStackSupervisor.checkReadyForSleepLocked(true /* allowDelay */);
1549 }
1550 }
1551
Craig Mautner0eea92c2013-05-16 13:35:39 -07001552 /**
David Stevens9440dc82017-03-16 19:00:20 -07001553 * Tries to put the activities in the stack to sleep.
1554 *
1555 * If the stack is not in a state where its activities can be put to sleep, this function will
1556 * start any necessary actions to move the stack into such a state. It is expected that this
1557 * function get called again when those actions complete.
1558 *
1559 * @param shuttingDown true when the called because the device is shutting down.
David Stevens18abd0e2017-08-17 14:55:47 -07001560 * @return true if the stack finished going to sleep, false if the stack only started the
1561 * process of going to sleep (checkReadyForSleep will be called when that process finishes).
Craig Mautner0eea92c2013-05-16 13:35:39 -07001562 */
David Stevens9440dc82017-03-16 19:00:20 -07001563 boolean goToSleepIfPossible(boolean shuttingDown) {
1564 boolean shouldSleep = true;
1565
Craig Mautner0eea92c2013-05-16 13:35:39 -07001566 if (mResumedActivity != null) {
1567 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001568 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
1569 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1570 "Sleep => pause with userLeaving=false");
Bryce Lee5daa3122017-04-19 10:40:42 -07001571
chaviwa1d68532018-03-16 15:33:43 -07001572 startPausingLocked(false, true, null, false);
David Stevens9440dc82017-03-16 19:00:20 -07001573 shouldSleep = false ;
1574 } else if (mPausingActivity != null) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001575 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001576 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
David Stevens9440dc82017-03-16 19:00:20 -07001577 shouldSleep = false;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001578 }
David Stevens9440dc82017-03-16 19:00:20 -07001579
1580 if (!shuttingDown) {
1581 if (containsActivityFromStack(mStackSupervisor.mStoppingActivities)) {
1582 // Still need to tell some activities to stop; can't sleep yet.
1583 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
1584 + mStackSupervisor.mStoppingActivities.size() + " activities");
1585
1586 mStackSupervisor.scheduleIdleLocked();
1587 shouldSleep = false;
1588 }
1589
1590 if (containsActivityFromStack(mStackSupervisor.mGoingToSleepActivities)) {
1591 // Still need to tell some activities to sleep; can't sleep yet.
1592 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
1593 + mStackSupervisor.mGoingToSleepActivities.size() + " activities");
1594 shouldSleep = false;
1595 }
1596 }
1597
1598 if (shouldSleep) {
1599 goToSleep();
1600 }
1601
David Stevens18abd0e2017-08-17 14:55:47 -07001602 return shouldSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -07001603 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001604
David Stevens18abd0e2017-08-17 14:55:47 -07001605 void goToSleep() {
Andrii Kulianf4479ee2018-05-23 17:52:48 -07001606 // Ensure visibility without updating configuration, as activities are about to sleep.
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001607 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
1608 !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001609
Andrii Kulianf9949d52016-05-06 12:53:25 -07001610 // Make sure any paused or stopped but visible activities are now sleeping.
Craig Mautner0eea92c2013-05-16 13:35:39 -07001611 // This ensures that the activity's onStop() is called.
1612 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1613 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1614 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1615 final ActivityRecord r = activities.get(activityNdx);
Bryce Lee7ace3952018-02-16 14:34:32 -08001616 if (r.isState(STOPPING, STOPPED, PAUSED, PAUSING)) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001617 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001618 }
1619 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001620 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001621 }
Craig Mautner59c00972012-07-30 12:10:24 -07001622
David Stevens9440dc82017-03-16 19:00:20 -07001623 private boolean containsActivityFromStack(List<ActivityRecord> rs) {
1624 for (ActivityRecord r : rs) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001625 if (r.getActivityStack() == this) {
David Stevens9440dc82017-03-16 19:00:20 -07001626 return true;
1627 }
1628 }
1629 return false;
1630 }
1631
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001632 /**
Winson Chung4dabf232017-01-25 13:25:22 -08001633 * Schedule a pause timeout in case the app doesn't respond. We don't give it much time because
1634 * this directly impacts the responsiveness seen by the user.
1635 */
1636 private void schedulePauseTimeout(ActivityRecord r) {
1637 final Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1638 msg.obj = r;
1639 r.pauseTime = SystemClock.uptimeMillis();
1640 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
1641 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
1642 }
1643
1644 /**
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001645 * Start pausing the currently resumed activity. It is an error to call this if there
1646 * is already an activity being paused or there is no resumed activity.
1647 *
1648 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
1649 * @param uiSleeping True if this is happening with the user interface going to sleep (the
1650 * screen turning off).
Wale Ogunwale950faff2016-08-08 09:51:04 -07001651 * @param resuming The activity we are currently trying to resume or null if this is not being
1652 * called as part of resuming the top activity, so we shouldn't try to instigate
1653 * a resume here if not null.
Winson Chung6954fc92017-03-24 16:22:12 -07001654 * @param pauseImmediately True if the caller does not want to wait for the activity callback to
1655 * complete pausing.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001656 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
1657 * it to tell us when it is done.
1658 */
Wale Ogunwale950faff2016-08-08 09:51:04 -07001659 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping,
Winson Chung6954fc92017-03-24 16:22:12 -07001660 ActivityRecord resuming, boolean pauseImmediately) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001661 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001662 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
Bryce Lee7ace3952018-02-16 14:34:32 -08001663 + " state=" + mPausingActivity.getState());
David Stevens9440dc82017-03-16 19:00:20 -07001664 if (!shouldSleepActivities()) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001665 // Avoid recursion among check for sleep and complete pause during sleeping.
1666 // Because activity will be paused immediately after resume, just let pause
1667 // be completed by the order of activity paused from clients.
Wale Ogunwale950faff2016-08-08 09:51:04 -07001668 completePauseLocked(false, resuming);
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001669 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001670 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001671 ActivityRecord prev = mResumedActivity;
Bryce Lee5daa3122017-04-19 10:40:42 -07001672
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001673 if (prev == null) {
Wale Ogunwale950faff2016-08-08 09:51:04 -07001674 if (resuming == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001675 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled32da472018-11-16 07:19:28 -08001676 mRootActivityContainer.resumeFocusedStacksTopActivities();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001677 }
1678 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001679 }
Craig Mautnerdf88d732014-01-27 09:21:32 -08001680
Andrii Kulianb1888aa2018-02-16 14:40:42 -08001681 if (prev == resuming) {
1682 Slog.wtf(TAG, "Trying to pause activity that is in process of being resumed");
1683 return false;
1684 }
1685
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001686 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001687 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001688 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001689 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -07001690 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1691 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Bryce Lee7ace3952018-02-16 14:34:32 -08001692 prev.setState(PAUSING, "startPausingLocked");
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001693 prev.getTaskRecord().touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001694 clearLaunchTime(prev);
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001695
Wale Ogunwale53783742018-09-16 10:21:51 -07001696 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -08001697
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001698 if (prev.attachedToProcess()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001699 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001700 try {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001701 EventLogTags.writeAmPauseActivity(prev.mUserId, System.identityHashCode(prev),
Wale Ogunwalec59b4f62017-11-30 11:05:43 -08001702 prev.shortComponentName, "userLeaving=" + userLeaving);
Andrii Kulian446e8242017-10-26 15:17:29 -07001703
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001704 mService.getLifecycleManager().scheduleTransaction(prev.app.getThread(),
1705 prev.appToken, PauseActivityItem.obtain(prev.finishing, userLeaving,
Bryce Lee1d0d5142018-04-12 10:35:07 -07001706 prev.configChangeFlags, pauseImmediately));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001707 } catch (Exception e) {
1708 // Ignore exception, if process died other code will cleanup.
1709 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001710 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001711 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001712 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001713 }
1714 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001715 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001716 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001717 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001718 }
1719
1720 // If we are not going to sleep, we want to ensure the device is
1721 // awake until the next activity is started.
Wale Ogunwalef6733932018-06-27 05:14:34 -07001722 if (!uiSleeping && !mService.isSleepingOrShuttingDownLocked()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001723 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001724 }
1725
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001726 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001727 // Have the window manager pause its key dispatching until the new
1728 // activity has started. If we're pausing the activity just because
1729 // the screen is being turned off and the UI is sleeping, don't interrupt
1730 // key dispatch; the same activity will pick it up again on wakeup.
1731 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001732 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001733 } else if (DEBUG_PAUSE) {
1734 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001735 }
1736
Winson Chung6954fc92017-03-24 16:22:12 -07001737 if (pauseImmediately) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001738 // If the caller said they don't want to wait for the pause, then complete
1739 // the pause now.
Wale Ogunwale950faff2016-08-08 09:51:04 -07001740 completePauseLocked(false, resuming);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001741 return false;
1742
1743 } else {
Winson Chung4dabf232017-01-25 13:25:22 -08001744 schedulePauseTimeout(prev);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001745 return true;
1746 }
1747
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001748 } else {
1749 // This activity failed to schedule the
1750 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001751 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Wale Ogunwale950faff2016-08-08 09:51:04 -07001752 if (resuming == null) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001753 mRootActivityContainer.resumeFocusedStacksTopActivities();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001754 }
1755 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001756 }
1757 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001758
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001759 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001760 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1761 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001762
Craig Mautnerd2328952013-03-05 12:46:26 -08001763 final ActivityRecord r = isInStackLocked(token);
Andrii Kulian6b321512019-01-23 06:37:00 +00001764
Craig Mautnerd2328952013-03-05 12:46:26 -08001765 if (r != null) {
1766 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1767 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001768 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001769 + (timeout ? " (due to timeout)" : " (pause complete)"));
Jorim Jaggife762342016-10-13 14:33:27 +02001770 mService.mWindowManager.deferSurfaceLayout();
1771 try {
Winson Chung3f103eb2017-04-12 21:53:59 -07001772 completePauseLocked(true /* resumeNext */, null /* resumingActivity */);
Jorim Jaggife762342016-10-13 14:33:27 +02001773 } finally {
1774 mService.mWindowManager.continueSurfaceLayout();
1775 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001776 return;
Craig Mautnerd2328952013-03-05 12:46:26 -08001777 } else {
1778 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001779 r.mUserId, System.identityHashCode(r), r.shortComponentName,
Craig Mautnerd2328952013-03-05 12:46:26 -08001780 mPausingActivity != null
1781 ? mPausingActivity.shortComponentName : "(none)");
Bryce Lee7ace3952018-02-16 14:34:32 -08001782 if (r.isState(PAUSING)) {
1783 r.setState(PAUSED, "activityPausedLocked");
riddle_hsu9caeef72015-10-20 16:34:05 +08001784 if (r.finishing) {
1785 if (DEBUG_PAUSE) Slog.v(TAG,
1786 "Executing finish of failed to pause activity: " + r);
Bryce Leef52974c2018-02-14 15:12:01 -08001787 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false,
1788 "activityPausedLocked");
riddle_hsu9caeef72015-10-20 16:34:05 +08001789 }
louis_chang047dfd42015-04-08 16:35:55 +08001790 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001791 }
1792 }
Wale Ogunwaled32da472018-11-16 07:19:28 -08001793 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001794 }
1795
Wale Ogunwale950faff2016-08-08 09:51:04 -07001796 private void completePauseLocked(boolean resumeNext, ActivityRecord resuming) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001797 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001798 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001799
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001800 if (prev != null) {
chaviw4ad54912018-05-30 11:05:44 -07001801 prev.setWillCloseOrEnterPip(false);
Bryce Lee7ace3952018-02-16 14:34:32 -08001802 final boolean wasStopping = prev.isState(STOPPING);
1803 prev.setState(PAUSED, "completePausedLocked");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001804 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001805 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Bryce Leef52974c2018-02-14 15:12:01 -08001806 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false,
1807 "completedPausedLocked");
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001808 } else if (prev.hasProcess()) {
Wale Ogunwaled8026642016-02-09 20:40:18 -08001809 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueue pending stop if needed: " + prev
1810 + " wasStopping=" + wasStopping + " visible=" + prev.visible);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001811 if (prev.deferRelaunchUntilPaused) {
1812 // Complete the deferred relaunch that was waiting for pause to complete.
1813 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001814 prev.relaunchActivityLocked(false /* andResume */,
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001815 prev.preserveWindowOnDeferredRelaunch);
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001816 } else if (wasStopping) {
1817 // We are also stopping, the stop request must have gone soon after the pause.
1818 // We can't clobber it, because the stop confirmation will not be handled.
1819 // We don't need to schedule another stop, we only need to let it happen.
Bryce Lee7ace3952018-02-16 14:34:32 -08001820 prev.setState(STOPPING, "completePausedLocked");
David Stevens9440dc82017-03-16 19:00:20 -07001821 } else if (!prev.visible || shouldSleepOrShutDownActivities()) {
Wale Ogunwaleec950642017-04-25 07:44:21 -07001822 // Clear out any deferred client hide we might currently have.
1823 prev.setDeferHidingClient(false);
Jose Lima4b6c6692014-08-12 17:41:12 -07001824 // If we were visible then resumeTopActivities will release resources before
Wale Ogunwaleec950642017-04-25 07:44:21 -07001825 // stopping.
Winson Chung4dabf232017-01-25 13:25:22 -08001826 addToStopping(prev, true /* scheduleIdle */, false /* idleDelayed */);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001827 }
1828 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001829 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001830 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001831 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001832 // It is possible the activity was freezing the screen before it was paused.
1833 // In that case go ahead and remove the freeze this activity has on the screen
1834 // since it is no longer visible.
Wale Ogunwalee8524002016-09-13 16:34:57 -07001835 if (prev != null) {
1836 prev.stopFreezingScreenLocked(true /*force*/);
1837 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001838 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001839 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001840
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001841 if (resumeNext) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001842 final ActivityStack topStack = mRootActivityContainer.getTopDisplayFocusedStack();
David Stevens9440dc82017-03-16 19:00:20 -07001843 if (!topStack.shouldSleepOrShutDownActivities()) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001844 mRootActivityContainer.resumeFocusedStacksTopActivities(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001845 } else {
David Stevens9440dc82017-03-16 19:00:20 -07001846 checkReadyForSleep();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001847 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001848 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001849 // If there are no more activities available to run, do resume anyway to start
1850 // something. Also if the top activity on the stack is not the just paused
1851 // activity, we need to go ahead and resume it to ensure we complete an
1852 // in-flight app switch.
Wale Ogunwaled32da472018-11-16 07:19:28 -08001853 mRootActivityContainer.resumeFocusedStacksTopActivities();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001854 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001855 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001856 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001857
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001858 if (prev != null) {
1859 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001860
Louis Chang1bb884a2018-10-08 19:29:18 +08001861 if (prev.hasProcess() && prev.cpuTimeAtResume > 0) {
1862 final long diff = prev.app.getCpuTime() - prev.cpuTimeAtResume;
1863 if (diff > 0) {
1864 final Runnable r = PooledLambda.obtainRunnable(
1865 ActivityManagerInternal::updateForegroundTimeIfOnBattery,
1866 mService.mAmInternal, prev.info.packageName,
1867 prev.info.applicationInfo.uid,
1868 diff);
1869 mService.mH.post(r);
1870 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001871 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001872 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001873 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001874
Andrii Kulian8290f8f2016-06-30 17:51:32 -07001875 // Notify when the task stack has changed, but only if visibilities changed (not just
1876 // focus). Also if there is an active pinned stack - we always want to notify it about
1877 // task stack changes, because its positioning may depend on it.
1878 if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause
Louis Chang24f7a032018-12-11 10:49:48 +08001879 || (getDisplay() != null && getDisplay().hasPinnedStack())) {
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -07001880 mService.getTaskChangeNotificationController().notifyTaskStackChanged();
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001881 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
1882 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001883
Wale Ogunwaled32da472018-11-16 07:19:28 -08001884 mRootActivityContainer.ensureActivitiesVisible(resuming, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001885 }
1886
Wale Ogunwale53783742018-09-16 10:21:51 -07001887 private void addToStopping(ActivityRecord r, boolean scheduleIdle, boolean idleDelayed) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001888 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1889 mStackSupervisor.mStoppingActivities.add(r);
1890 }
1891
1892 // If we already have a few activities waiting to stop, then give up
1893 // on things going idle and start clearing them out. Or if r is the
1894 // last of activity of the last task the stack will be empty and must
1895 // be cleared immediately.
1896 boolean forceIdle = mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE
1897 || (r.frontOfTask && mTaskHistory.size() <= 1);
Winson Chung4dabf232017-01-25 13:25:22 -08001898 if (scheduleIdle || forceIdle) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001899 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Scheduling idle now: forceIdle="
Winson Chung4dabf232017-01-25 13:25:22 -08001900 + forceIdle + "immediate=" + !idleDelayed);
1901 if (!idleDelayed) {
1902 mStackSupervisor.scheduleIdleLocked();
1903 } else {
1904 mStackSupervisor.scheduleIdleTimeoutLocked(r);
1905 }
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001906 } else {
David Stevens9440dc82017-03-16 19:00:20 -07001907 checkReadyForSleep();
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001908 }
1909 }
1910
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001911 /**
1912 * Returns true if the stack is translucent and can have other contents visible behind it if
1913 * needed. A stack is considered translucent if it don't contain a visible or
1914 * starting (about to be visible) activity that is fullscreen (opaque).
1915 * @param starting The currently starting activity or null if there is none.
1916 */
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001917 @VisibleForTesting
Wale Ogunwale66e16852017-10-19 13:35:52 -07001918 boolean isStackTranslucent(ActivityRecord starting) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001919 if (!isAttached() || mForceHidden) {
1920 return true;
1921 }
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001922 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1923 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001924 final ArrayList<ActivityRecord> activities = task.mActivities;
1925 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1926 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001927
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001928 if (r.finishing) {
1929 // We don't factor in finishing activities when determining translucency since
1930 // they will be gone soon.
1931 continue;
1932 }
1933
Wale Ogunwale2cca8622017-12-11 08:40:13 -08001934 if (!r.visibleIgnoringKeyguard && r != starting) {
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001935 // Also ignore invisible activities that are not the currently starting
1936 // activity (about to be visible).
1937 continue;
1938 }
1939
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001940 if (r.fullscreen || r.hasWallpaper) {
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001941 // Stack isn't translucent if it has at least one fullscreen activity
1942 // that is visible.
1943 return false;
1944 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001945 }
1946 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001947 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001948 }
1949
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001950 boolean isTopStackOnDisplay() {
Andrii Kulian52d255c2018-07-13 11:32:19 -07001951 final ActivityDisplay display = getDisplay();
1952 return display != null && display.isTopStack(this);
1953 }
1954
1955 /**
1956 * @return {@code true} if this is the focused stack on its current display, {@code false}
1957 * otherwise.
1958 */
1959 boolean isFocusedStackOnDisplay() {
1960 final ActivityDisplay display = getDisplay();
1961 return display != null && this == display.getFocusedStack();
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07001962 }
1963
Adrian Roosa6d6aab2018-04-19 18:58:22 +02001964 boolean isTopActivityVisible() {
1965 final ActivityRecord topActivity = getTopActivity();
1966 return topActivity != null && topActivity.visible;
1967 }
1968
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001969 /**
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001970 * Returns true if the stack should be visible.
Wale Ogunwalecd501ec2017-04-07 08:53:41 -07001971 *
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001972 * @param starting The currently starting activity or null if there is none.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001973 */
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07001974 boolean shouldBeVisible(ActivityRecord starting) {
Andrii Kulian0c869cc2019-02-06 19:50:32 -08001975 return getVisibility(starting) != STACK_VISIBILITY_INVISIBLE;
1976 }
1977
1978 /**
1979 * Returns true if the stack should be visible.
1980 *
1981 * @param starting The currently starting activity or null if there is none.
1982 */
1983 @StackVisibility
1984 int getVisibility(ActivityRecord starting) {
Winson Chung47900652017-04-06 18:44:25 -07001985 if (!isAttached() || mForceHidden) {
Andrii Kulian0c869cc2019-02-06 19:50:32 -08001986 return STACK_VISIBILITY_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001987 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001988
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001989 final ActivityDisplay display = getDisplay();
Wale Ogunwale8e923af2018-03-23 08:48:11 -07001990 boolean gotSplitScreenStack = false;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001991 boolean gotOpaqueSplitScreenPrimary = false;
1992 boolean gotOpaqueSplitScreenSecondary = false;
Andrii Kulian0c869cc2019-02-06 19:50:32 -08001993 boolean gotTranslucentFullscreen = false;
1994 boolean gotTranslucentSplitScreenPrimary = false;
1995 boolean gotTranslucentSplitScreenSecondary = false;
1996 boolean shouldBeVisible = true;
Wale Ogunwale68278562017-09-23 17:13:55 -07001997 final int windowingMode = getWindowingMode();
Wale Ogunwale8e923af2018-03-23 08:48:11 -07001998 final boolean isAssistantType = isActivityTypeAssistant();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001999 for (int i = display.getChildCount() - 1; i >= 0; --i) {
2000 final ActivityStack other = display.getChildAt(i);
Louis Changf2835df2018-10-17 15:14:45 +08002001 final boolean hasRunningActivities = other.topRunningActivityLocked() != null;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002002 if (other == this) {
Louis Changf2835df2018-10-17 15:14:45 +08002003 // Should be visible if there is no other stack occluding it, unless it doesn't
2004 // have any running activities, not starting one and not home stack.
Andrii Kulian0c869cc2019-02-06 19:50:32 -08002005 shouldBeVisible = hasRunningActivities || isInStackLocked(starting) != null
Louis Changf2835df2018-10-17 15:14:45 +08002006 || isActivityTypeHome();
Andrii Kulian0c869cc2019-02-06 19:50:32 -08002007 break;
Louis Changf2835df2018-10-17 15:14:45 +08002008 }
2009
2010 if (!hasRunningActivities) {
2011 continue;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002012 }
Wale Ogunwale68278562017-09-23 17:13:55 -07002013
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002014 final int otherWindowingMode = other.getWindowingMode();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002015
2016 if (otherWindowingMode == WINDOWING_MODE_FULLSCREEN) {
Matthew Ng99b3cdc2018-05-01 14:24:38 -07002017 // In this case the home stack isn't resizeable even though we are in split-screen
2018 // mode. We still want the primary splitscreen stack to be visible as there will be
2019 // a slight hint of it in the status bar area above the non-resizeable home
Matthew Nga51dcaa2018-05-07 15:36:06 -07002020 // activity. In addition, if the fullscreen assistant is over primary splitscreen
2021 // stack, the stack should still be visible in the background as long as the recents
2022 // animation is running.
2023 final int activityType = other.getActivityType();
2024 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
2025 if (activityType == ACTIVITY_TYPE_HOME
2026 || (activityType == ACTIVITY_TYPE_ASSISTANT
Andrii Kulian0c869cc2019-02-06 19:50:32 -08002027 && mWindowManager.getRecentsAnimationController() != null)) {
2028 break;
Matthew Nga51dcaa2018-05-07 15:36:06 -07002029 }
Matthew Ng99b3cdc2018-05-01 14:24:38 -07002030 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07002031 if (other.isStackTranslucent(starting)) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002032 // Can be visible behind a translucent fullscreen stack.
Andrii Kulian0c869cc2019-02-06 19:50:32 -08002033 gotTranslucentFullscreen = true;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002034 continue;
2035 }
Andrii Kulian0c869cc2019-02-06 19:50:32 -08002036 return STACK_VISIBILITY_INVISIBLE;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002037 } else if (otherWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
2038 && !gotOpaqueSplitScreenPrimary) {
Wale Ogunwale8e923af2018-03-23 08:48:11 -07002039 gotSplitScreenStack = true;
Andrii Kulian0c869cc2019-02-06 19:50:32 -08002040 gotTranslucentSplitScreenPrimary = other.isStackTranslucent(starting);
2041 gotOpaqueSplitScreenPrimary = !gotTranslucentSplitScreenPrimary;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002042 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
2043 && gotOpaqueSplitScreenPrimary) {
2044 // Can not be visible behind another opaque stack in split-screen-primary mode.
Andrii Kulian0c869cc2019-02-06 19:50:32 -08002045 return STACK_VISIBILITY_INVISIBLE;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002046 }
2047 } else if (otherWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY
2048 && !gotOpaqueSplitScreenSecondary) {
Wale Ogunwale8e923af2018-03-23 08:48:11 -07002049 gotSplitScreenStack = true;
Andrii Kulian0c869cc2019-02-06 19:50:32 -08002050 gotTranslucentSplitScreenSecondary = other.isStackTranslucent(starting);
2051 gotOpaqueSplitScreenSecondary = !gotTranslucentSplitScreenSecondary;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002052 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY
2053 && gotOpaqueSplitScreenSecondary) {
2054 // Can not be visible behind another opaque stack in split-screen-secondary mode.
Andrii Kulian0c869cc2019-02-06 19:50:32 -08002055 return STACK_VISIBILITY_INVISIBLE;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002056 }
2057 }
2058 if (gotOpaqueSplitScreenPrimary && gotOpaqueSplitScreenSecondary) {
2059 // Can not be visible if we are in split-screen windowing mode and both halves of
2060 // the screen are opaque.
Andrii Kulian0c869cc2019-02-06 19:50:32 -08002061 return STACK_VISIBILITY_INVISIBLE;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002062 }
Wale Ogunwale8e923af2018-03-23 08:48:11 -07002063 if (isAssistantType && gotSplitScreenStack) {
2064 // Assistant stack can't be visible behind split-screen. In addition to this not
2065 // making sense, it also works around an issue here we boost the z-order of the
2066 // assistant window surfaces in window manager whenever it is visible.
Andrii Kulian0c869cc2019-02-06 19:50:32 -08002067 return STACK_VISIBILITY_INVISIBLE;
Wale Ogunwale8e923af2018-03-23 08:48:11 -07002068 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07002069 }
2070
Andrii Kulian0c869cc2019-02-06 19:50:32 -08002071 if (!shouldBeVisible) {
2072 return STACK_VISIBILITY_INVISIBLE;
2073 }
2074
2075 // Handle cases when there can be a translucent split-screen stack on top.
2076 switch (windowingMode) {
2077 case WINDOWING_MODE_FULLSCREEN:
2078 if (gotTranslucentSplitScreenPrimary || gotTranslucentSplitScreenSecondary) {
2079 // At least one of the split-screen stacks that covers this one is translucent.
2080 return STACK_VISIBILITY_VISIBLE_BEHIND_TRANSLUCENT;
2081 }
2082 break;
2083 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY:
2084 if (gotTranslucentSplitScreenPrimary) {
2085 // Covered by translucent primary split-screen on top.
2086 return STACK_VISIBILITY_VISIBLE_BEHIND_TRANSLUCENT;
2087 }
2088 break;
2089 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY:
2090 if (gotTranslucentSplitScreenSecondary) {
2091 // Covered by translucent secondary split-screen on top.
2092 return STACK_VISIBILITY_VISIBLE_BEHIND_TRANSLUCENT;
2093 }
2094 break;
2095 }
2096
2097 // Lastly - check if there is a translucent fullscreen stack on top.
2098 return gotTranslucentFullscreen ? STACK_VISIBILITY_VISIBLE_BEHIND_TRANSLUCENT
2099 : STACK_VISIBILITY_VISIBLE;
Wale Ogunwale68278562017-09-23 17:13:55 -07002100 }
2101
Chong Zhangfdcc4d42015-10-14 16:50:12 -07002102 final int rankTaskLayers(int baseLayer) {
2103 int layer = 0;
2104 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2105 final TaskRecord task = mTaskHistory.get(taskNdx);
2106 ActivityRecord r = task.topRunningActivityLocked();
2107 if (r == null || r.finishing || !r.visible) {
2108 task.mLayerRank = -1;
2109 } else {
2110 task.mLayerRank = baseLayer + layer++;
2111 }
2112 }
2113 return layer;
2114 }
2115
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002116 /**
Andrii Kulianf4479ee2018-05-23 17:52:48 -07002117 * Make sure that all activities that need to be visible in the stack (that is, they
2118 * currently can be seen by the user) actually are and update their configuration.
2119 */
2120 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
2121 boolean preserveWindows) {
2122 ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07002123 true /* notifyClients */);
Andrii Kulianf4479ee2018-05-23 17:52:48 -07002124 }
2125
2126 /**
2127 * Ensure visibility with an option to also update the configuration of visible activities.
2128 * @see #ensureActivitiesVisibleLocked(ActivityRecord, int, boolean)
Wale Ogunwaled32da472018-11-16 07:19:28 -08002129 * @see RootActivityContainer#ensureActivitiesVisible(ActivityRecord, int, boolean)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002130 */
Wale Ogunwale66e16852017-10-19 13:35:52 -07002131 // TODO: Should be re-worked based on the fact that each task as a stack in most cases.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002132 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07002133 boolean preserveWindows, boolean notifyClients) {
Jorim Jaggife762342016-10-13 14:33:27 +02002134 mTopActivityOccludesKeyguard = false;
2135 mTopDismissingKeyguardActivity = null;
Bryce Lee2a3cc462017-10-27 10:57:35 -07002136 mStackSupervisor.getKeyguardController().beginActivityVisibilityUpdate();
Jorim Jaggife762342016-10-13 14:33:27 +02002137 try {
2138 ActivityRecord top = topRunningActivityLocked();
2139 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
2140 + " configChanges=0x" + Integer.toHexString(configChanges));
2141 if (top != null) {
2142 checkTranslucentActivityWaiting(top);
2143 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002144
Jorim Jaggife762342016-10-13 14:33:27 +02002145 // If the top activity is not fullscreen, then we need to
2146 // make sure any activities under it are now visible.
2147 boolean aboveTop = top != null;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002148 final boolean stackShouldBeVisible = shouldBeVisible(starting);
2149 boolean behindFullscreenActivity = !stackShouldBeVisible;
Andrii Kulian6b321512019-01-23 06:37:00 +00002150 boolean resumeNextActivity = isFocusable() && isInStackLocked(starting) == null;
Jorim Jaggife762342016-10-13 14:33:27 +02002151 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2152 final TaskRecord task = mTaskHistory.get(taskNdx);
2153 final ArrayList<ActivityRecord> activities = task.mActivities;
2154 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2155 final ActivityRecord r = activities.get(activityNdx);
2156 if (r.finishing) {
Jorim Jaggife762342016-10-13 14:33:27 +02002157 continue;
Chong Zhang22bc8512016-04-07 13:47:18 -07002158 }
Jorim Jaggife762342016-10-13 14:33:27 +02002159 final boolean isTop = r == top;
2160 if (aboveTop && !isTop) {
2161 continue;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002162 }
Jorim Jaggife762342016-10-13 14:33:27 +02002163 aboveTop = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002164
Jorim Jaggife762342016-10-13 14:33:27 +02002165 // Check whether activity should be visible without Keyguard influence
Jorim Jaggi241ae102016-11-02 21:57:33 -07002166 final boolean visibleIgnoringKeyguard = r.shouldBeVisibleIgnoringKeyguard(
Jorim Jaggi241ae102016-11-02 21:57:33 -07002167 behindFullscreenActivity);
Garfield Tan47e576c2019-01-28 10:26:23 -08002168 final boolean reallyVisible = r.shouldBeVisible(behindFullscreenActivity);
Jorim Jaggi241ae102016-11-02 21:57:33 -07002169 if (visibleIgnoringKeyguard) {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002170 behindFullscreenActivity = updateBehindFullscreen(!stackShouldBeVisible,
Wale Ogunwale66e16852017-10-19 13:35:52 -07002171 behindFullscreenActivity, r);
Jorim Jaggife762342016-10-13 14:33:27 +02002172 }
2173 if (reallyVisible) {
2174 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
Bryce Lee7ace3952018-02-16 14:34:32 -08002175 + " finishing=" + r.finishing + " state=" + r.getState());
Jorim Jaggife762342016-10-13 14:33:27 +02002176 // First: if this is not the current activity being started, make
2177 // sure it matches the current configuration.
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07002178 if (r != starting && notifyClients) {
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08002179 r.ensureActivityConfiguration(0 /* globalChanges */, preserveWindows,
2180 true /* ignoreStopState */);
Jorim Jaggife762342016-10-13 14:33:27 +02002181 }
2182
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002183 if (!r.attachedToProcess()) {
Jorim Jaggife762342016-10-13 14:33:27 +02002184 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
2185 resumeNextActivity, r)) {
2186 if (activityNdx >= activities.size()) {
2187 // Record may be removed if its process needs to restart.
2188 activityNdx = activities.size() - 1;
2189 } else {
2190 resumeNextActivity = false;
2191 }
2192 }
2193 } else if (r.visible) {
2194 // If this activity is already visible, then there is nothing to do here.
2195 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2196 "Skipping: already visible at " + r);
2197
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07002198 if (r.mClientVisibilityDeferred && notifyClients) {
2199 r.makeClientVisible();
2200 }
2201
Jorim Jaggife762342016-10-13 14:33:27 +02002202 if (r.handleAlreadyVisible()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08002203 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08002204 }
Andrii Kulian6b321512019-01-23 06:37:00 +00002205
2206 if (notifyClients) {
2207 r.makeActiveIfNeeded(starting);
2208 }
Jorim Jaggife762342016-10-13 14:33:27 +02002209 } else {
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07002210 r.makeVisibleIfNeeded(starting, notifyClients);
Craig Mautnerd44711d2013-02-23 11:24:36 -08002211 }
Jorim Jaggife762342016-10-13 14:33:27 +02002212 // Aggregate current change flags.
2213 configChanges |= r.configChangeFlags;
Craig Mautnerbb742462014-07-07 15:28:55 -07002214 } else {
Jorim Jaggife762342016-10-13 14:33:27 +02002215 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
Bryce Lee7ace3952018-02-16 14:34:32 -08002216 + " finishing=" + r.finishing + " state=" + r.getState()
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002217 + " stackShouldBeVisible=" + stackShouldBeVisible
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002218 + " behindFullscreenActivity=" + behindFullscreenActivity
2219 + " mLaunchTaskBehind=" + r.mLaunchTaskBehind);
Wale Ogunwalec981ad52017-06-13 11:40:06 -07002220 makeInvisible(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002221 }
Jorim Jaggife762342016-10-13 14:33:27 +02002222 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002223 final int windowingMode = getWindowingMode();
2224 if (windowingMode == WINDOWING_MODE_FREEFORM) {
Jorim Jaggife762342016-10-13 14:33:27 +02002225 // The visibility of tasks and the activities they contain in freeform stack are
2226 // determined individually unlike other stacks where the visibility or fullscreen
2227 // status of an activity in a previous task affects other.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002228 behindFullscreenActivity = !stackShouldBeVisible;
Wale Ogunwale68278562017-09-23 17:13:55 -07002229 } else if (isActivityTypeHome()) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002230 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home task: at " + task
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002231 + " stackShouldBeVisible=" + stackShouldBeVisible
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002232 + " behindFullscreenActivity=" + behindFullscreenActivity);
2233 // No other task in the home stack should be visible behind the home activity.
2234 // Home activities is usually a translucent activity with the wallpaper behind
2235 // them. However, when they don't have the wallpaper behind them, we want to
2236 // show activities in the next application stack behind them vs. another
2237 // task in the home stack like recents.
2238 behindFullscreenActivity = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002239 }
2240 }
Wale Ogunwale74e26592016-02-05 11:48:37 -08002241
Jorim Jaggife762342016-10-13 14:33:27 +02002242 if (mTranslucentActivityWaiting != null &&
2243 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
2244 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
2245 notifyActivityDrawnLocked(null);
2246 }
2247 } finally {
Bryce Lee2a3cc462017-10-27 10:57:35 -07002248 mStackSupervisor.getKeyguardController().endActivityVisibilityUpdate();
Jorim Jaggife762342016-10-13 14:33:27 +02002249 }
2250 }
2251
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01002252 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
2253 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2254 mTaskHistory.get(taskNdx).addStartingWindowsForVisibleActivities(taskSwitch);
2255 }
2256 }
2257
Jorim Jaggife762342016-10-13 14:33:27 +02002258 /**
2259 * @return true if the top visible activity wants to occlude the Keyguard, false otherwise
2260 */
2261 boolean topActivityOccludesKeyguard() {
2262 return mTopActivityOccludesKeyguard;
2263 }
2264
2265 /**
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002266 * Returns true if this stack should be resized to match the bounds specified by
2267 * {@link ActivityOptions#setLaunchBounds} when launching an activity into the stack.
2268 */
2269 boolean resizeStackWithLaunchBounds() {
2270 return inPinnedWindowingMode();
2271 }
2272
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002273 @Override
2274 public boolean supportsSplitScreenWindowingMode() {
2275 final TaskRecord topTask = topTask();
2276 return super.supportsSplitScreenWindowingMode()
2277 && (topTask == null || topTask.supportsSplitScreenWindowingMode());
2278 }
2279
Wale Ogunwale2b07da82017-11-08 14:52:40 -08002280 /** @return True if the resizing of the primary-split-screen stack affects this stack size. */
2281 boolean affectedBySplitScreenResize() {
2282 if (!supportsSplitScreenWindowingMode()) {
2283 return false;
2284 }
2285 final int windowingMode = getWindowingMode();
2286 return windowingMode != WINDOWING_MODE_FREEFORM && windowingMode != WINDOWING_MODE_PINNED;
2287 }
2288
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002289 /**
Jorim Jaggife762342016-10-13 14:33:27 +02002290 * @return the top most visible activity that wants to dismiss Keyguard
2291 */
2292 ActivityRecord getTopDismissingKeyguardActivity() {
2293 return mTopDismissingKeyguardActivity;
2294 }
2295
2296 /**
2297 * Checks whether {@param r} should be visible depending on Keyguard state and updates
2298 * {@link #mTopActivityOccludesKeyguard} and {@link #mTopDismissingKeyguardActivity} if
2299 * necessary.
2300 *
2301 * @return true if {@param r} is visible taken Keyguard state into account, false otherwise
2302 */
Wale Ogunwale2cca8622017-12-11 08:40:13 -08002303 boolean checkKeyguardVisibility(ActivityRecord r, boolean shouldBeVisible, boolean isTop) {
Lucas Dupin47a65c72018-02-15 14:16:18 -08002304 final int displayId = mDisplayId != INVALID_DISPLAY ? mDisplayId : DEFAULT_DISPLAY;
2305 final boolean keyguardOrAodShowing = mStackSupervisor.getKeyguardController()
2306 .isKeyguardOrAodShowing(displayId);
Bryce Lee2a3cc462017-10-27 10:57:35 -07002307 final boolean keyguardLocked = mStackSupervisor.getKeyguardController().isKeyguardLocked();
Wale Ogunwale2cca8622017-12-11 08:40:13 -08002308 final boolean showWhenLocked = r.canShowWhenLocked();
Issei Suzuki74e1eb22018-12-20 17:42:52 +01002309 final boolean dismissKeyguard = r.mAppWindowToken != null
2310 && r.mAppWindowToken.containsDismissKeyguardWindow();
Jorim Jaggife762342016-10-13 14:33:27 +02002311 if (shouldBeVisible) {
Jorim Jaggie69c9312016-10-31 18:24:38 -07002312 if (dismissKeyguard && mTopDismissingKeyguardActivity == null) {
Jorim Jaggife762342016-10-13 14:33:27 +02002313 mTopDismissingKeyguardActivity = r;
2314 }
2315
2316 // Only the top activity may control occluded, as we can't occlude the Keyguard if the
2317 // top app doesn't want to occlude it.
2318 if (isTop) {
2319 mTopActivityOccludesKeyguard |= showWhenLocked;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07002320 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002321
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002322 final boolean canShowWithKeyguard = canShowWithInsecureKeyguard()
Bryce Lee2a3cc462017-10-27 10:57:35 -07002323 && mStackSupervisor.getKeyguardController().canDismissKeyguard();
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002324 if (canShowWithKeyguard) {
2325 return true;
2326 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002327 }
Lucas Dupin47a65c72018-02-15 14:16:18 -08002328 if (keyguardOrAodShowing) {
Jorim Jaggie69c9312016-10-31 18:24:38 -07002329 // If keyguard is showing, nothing is visible, except if we are able to dismiss Keyguard
Lucas Dupin47a65c72018-02-15 14:16:18 -08002330 // right away and AOD isn't visible.
Bryce Lee2a3cc462017-10-27 10:57:35 -07002331 return shouldBeVisible && mStackSupervisor.getKeyguardController()
Jorim Jaggi07961872016-11-23 11:28:57 +01002332 .canShowActivityWhileKeyguardShowing(r, dismissKeyguard);
Jorim Jaggife762342016-10-13 14:33:27 +02002333 } else if (keyguardLocked) {
Bryce Lee2a3cc462017-10-27 10:57:35 -07002334 return shouldBeVisible && mStackSupervisor.getKeyguardController().canShowWhileOccluded(
Jorim Jaggi07961872016-11-23 11:28:57 +01002335 dismissKeyguard, showWhenLocked);
Jorim Jaggife762342016-10-13 14:33:27 +02002336 } else {
2337 return shouldBeVisible;
Craig Mautnereb8abf72014-07-02 15:04:09 -07002338 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002339 }
Craig Mautner58547802013-03-05 08:23:53 -08002340
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002341 /**
2342 * Check if the display to which this stack is attached has
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002343 * {@link Display#FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD} applied.
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002344 */
wilsonshih498a4b82018-12-11 16:10:16 +08002345 boolean canShowWithInsecureKeyguard() {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002346 final ActivityDisplay activityDisplay = getDisplay();
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002347 if (activityDisplay == null) {
2348 throw new IllegalStateException("Stack is not attached to any display, stackId="
2349 + mStackId);
2350 }
2351
2352 final int flags = activityDisplay.mDisplay.getFlags();
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002353 return (flags & FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002354 }
2355
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002356 private void checkTranslucentActivityWaiting(ActivityRecord top) {
2357 if (mTranslucentActivityWaiting != top) {
2358 mUndrawnActivitiesBelowTopTranslucent.clear();
2359 if (mTranslucentActivityWaiting != null) {
2360 // Call the callback with a timeout indication.
2361 notifyActivityDrawnLocked(null);
2362 mTranslucentActivityWaiting = null;
2363 }
2364 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
2365 }
2366 }
2367
2368 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08002369 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002370 // We need to make sure the app is running if it's the top, or it is just made visible from
2371 // invisible. If the app is already visible, it must have died while it was visible. In this
2372 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
2373 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002374 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002375 // This activity needs to be visible, but isn't even running...
2376 // get it started and resume if no other stack in this stack is resumed.
2377 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
2378 if (r != starting) {
2379 r.startFreezingScreenLocked(r.app, configChanges);
2380 }
2381 if (!r.visible || r.mLaunchTaskBehind) {
2382 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002383 r.setVisible(true);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002384 }
2385 if (r != starting) {
Andrii Kulian6b321512019-01-23 06:37:00 +00002386 mStackSupervisor.startSpecificActivityLocked(r, andResume, true /* checkConfig */);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002387 return true;
2388 }
2389 }
2390 return false;
2391 }
2392
Wale Ogunwalec981ad52017-06-13 11:40:06 -07002393 // TODO: Should probably be moved into ActivityRecord.
2394 private void makeInvisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002395 if (!r.visible) {
2396 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
2397 return;
2398 }
2399 // Now for any activities that aren't visible to the user, make sure they no longer are
2400 // keeping the screen frozen.
Bryce Lee7ace3952018-02-16 14:34:32 -08002401 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.getState());
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002402 try {
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07002403 final boolean canEnterPictureInPicture = r.checkEnterPictureInPictureState(
Winson Chung298f95b2017-08-10 15:57:18 -07002404 "makeInvisible", true /* beforeStopping */);
Wale Ogunwale08488a62017-09-05 12:37:03 -07002405 // Defer telling the client it is hidden if it can enter Pip and isn't current paused,
2406 // stopped or stopping. This gives it a chance to enter Pip in onPause().
2407 // TODO: There is still a question surrounding activities in multi-window mode that want
2408 // to enter Pip after they are paused, but are still visible. I they should be okay to
2409 // enter Pip in those cases, but not "auto-Pip" which is what this condition covers and
2410 // the current contract for "auto-Pip" is that the app should enter it before onPause
2411 // returns. Just need to confirm this reasoning makes sense.
Wale Ogunwale89973222017-04-23 18:39:45 -07002412 final boolean deferHidingClient = canEnterPictureInPicture
Bryce Lee7ace3952018-02-16 14:34:32 -08002413 && !r.isState(STOPPING, STOPPED, PAUSED);
Wale Ogunwaleec950642017-04-25 07:44:21 -07002414 r.setDeferHidingClient(deferHidingClient);
2415 r.setVisible(false);
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07002416
Bryce Lee7ace3952018-02-16 14:34:32 -08002417 switch (r.getState()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002418 case STOPPING:
2419 case STOPPED:
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002420 if (r.attachedToProcess()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002421 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2422 "Scheduling invisibility: " + r);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002423 mService.getLifecycleManager().scheduleTransaction(r.app.getThread(),
2424 r.appToken, WindowVisibilityItem.obtain(false /* showWindow */));
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002425 }
Winson Chungc2baac02017-01-11 13:34:47 -08002426
2427 // Reset the flag indicating that an app can enter picture-in-picture once the
2428 // activity is hidden
Winson Chungf7e03e12017-08-22 11:32:16 -07002429 r.supportsEnterPipOnTaskSwitch = false;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002430 break;
2431
2432 case INITIALIZING:
2433 case RESUMED:
2434 case PAUSING:
2435 case PAUSED:
Wale Ogunwalec981ad52017-06-13 11:40:06 -07002436 addToStopping(r, true /* scheduleIdle */,
2437 canEnterPictureInPicture /* idleDelayed */);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002438 break;
2439
2440 default:
2441 break;
2442 }
2443 } catch (Exception e) {
2444 // Just skip on any failure; we'll make it visible when it next restarts.
2445 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
2446 }
2447 }
2448
2449 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
Wale Ogunwale66e16852017-10-19 13:35:52 -07002450 ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002451 if (r.fullscreen) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002452 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwale673cbd22016-01-30 18:30:55 -08002453 + " stackInvisible=" + stackInvisible
2454 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08002455 // At this point, nothing else needs to be shown in this task.
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002456 behindFullscreenActivity = true;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08002457 }
2458 return behindFullscreenActivity;
2459 }
2460
Todd Kennedyaab56db2015-01-30 09:39:53 -08002461 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07002462 mTranslucentActivityWaiting = r;
2463 mUndrawnActivitiesBelowTopTranslucent.clear();
2464 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
2465 }
2466
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002467 void clearOtherAppTimeTrackers(AppTimeTracker except) {
2468 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2469 final TaskRecord task = mTaskHistory.get(taskNdx);
2470 final ArrayList<ActivityRecord> activities = task.mActivities;
2471 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2472 final ActivityRecord r = activities.get(activityNdx);
2473 if ( r.appTimeTracker != except) {
2474 r.appTimeTracker = null;
2475 }
2476 }
2477 }
2478 }
2479
Craig Mautner5eda9b32013-07-02 11:58:16 -07002480 /**
2481 * Called as activities below the top translucent activity are redrawn. When the last one is
2482 * redrawn notify the top activity by calling
2483 * {@link Activity#onTranslucentConversionComplete}.
2484 *
2485 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
2486 * occurred and the activity will be notified immediately.
2487 */
2488 void notifyActivityDrawnLocked(ActivityRecord r) {
2489 if ((r == null)
2490 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
2491 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
2492 // The last undrawn activity below the top has just been drawn. If there is an
2493 // opaque activity at the top, notify it that it can become translucent safely now.
2494 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
2495 mTranslucentActivityWaiting = null;
2496 mUndrawnActivitiesBelowTopTranslucent.clear();
2497 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
2498
Craig Mautner71dd1b62014-02-18 15:48:52 -08002499 if (waitingActivity != null) {
2500 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002501 if (waitingActivity.attachedToProcess()) {
Craig Mautner71dd1b62014-02-18 15:48:52 -08002502 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002503 waitingActivity.app.getThread().scheduleTranslucentConversionComplete(
Craig Mautner71dd1b62014-02-18 15:48:52 -08002504 waitingActivity.appToken, r != null);
2505 } catch (RemoteException e) {
2506 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002507 }
2508 }
2509 }
2510 }
2511
Craig Mautnera61bc652013-10-28 15:43:18 -07002512 /** If any activities below the top running one are in the INITIALIZING state and they have a
2513 * starting window displayed then remove that starting window. It is possible that the activity
2514 * in this state will never resumed in which case that starting window will be orphaned. */
2515 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002516 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07002517 boolean aboveTop = true;
Wale Ogunwale68741142016-05-17 09:40:02 -07002518 // We don't want to clear starting window for activities that aren't behind fullscreen
2519 // activities as we need to display their starting window until they are done initializing.
2520 boolean behindFullscreenActivity = false;
Wale Ogunwale98742a52016-07-12 10:29:12 -07002521
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002522 if (!shouldBeVisible(null)) {
Wale Ogunwale98742a52016-07-12 10:29:12 -07002523 // The stack is not visible, so no activity in it should be displaying a starting
2524 // window. Mark all activities below top and behind fullscreen.
2525 aboveTop = false;
2526 behindFullscreenActivity = true;
2527 }
2528
Craig Mautnera61bc652013-10-28 15:43:18 -07002529 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2530 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2531 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2532 final ActivityRecord r = activities.get(activityNdx);
2533 if (aboveTop) {
2534 if (r == topActivity) {
2535 aboveTop = false;
2536 }
Wale Ogunwale68741142016-05-17 09:40:02 -07002537 behindFullscreenActivity |= r.fullscreen;
Craig Mautnera61bc652013-10-28 15:43:18 -07002538 continue;
2539 }
2540
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002541 r.removeOrphanedStartingWindow(behindFullscreenActivity);
Wale Ogunwale68741142016-05-17 09:40:02 -07002542 behindFullscreenActivity |= r.fullscreen;
Craig Mautnera61bc652013-10-28 15:43:18 -07002543 }
2544 }
2545 }
2546
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002547 /**
2548 * Ensure that the top activity in the stack is resumed.
2549 *
2550 * @param prev The previously resumed activity, for when in the process
2551 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002552 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002553 *
2554 * @return Returns true if something is being resumed, or false if
2555 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08002556 *
2557 * NOTE: It is not safe to call this method directly as it can cause an activity in a
2558 * non-focused stack to be resumed.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002559 * Use {@link RootActivityContainer#resumeFocusedStacksTopActivities} to resume the
Wale Ogunwaled046a012015-12-24 13:05:59 -08002560 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002561 */
Andreas Gampea36dc622018-02-05 17:19:22 -08002562 @GuardedBy("mService")
Wale Ogunwaled046a012015-12-24 13:05:59 -08002563 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Andrii Kulian6b321512019-01-23 06:37:00 +00002564 if (mInResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07002565 // Don't even start recursing.
2566 return false;
2567 }
2568
2569 boolean result = false;
2570 try {
2571 // Protect against recursion.
Andrii Kulian6b321512019-01-23 06:37:00 +00002572 mInResumeTopActivity = true;
Bryce Lee69ad8182017-09-28 10:01:36 -07002573 result = resumeTopActivityInnerLocked(prev, options);
Andrii Kulian86e70fc2019-02-12 11:04:10 +00002574 mStackSupervisor.updateTopResumedActivityIfNeeded();
Bryce Leefc7cedd2018-03-01 15:38:07 -08002575
2576 // When resuming the top activity, it may be necessary to pause the top activity (for
2577 // example, returning to the lock screen. We suppress the normal pause logic in
2578 // {@link #resumeTopActivityUncheckedLocked}, since the top activity is resumed at the
2579 // end. We call the {@link ActivityStackSupervisor#checkReadyForSleepLocked} again here
2580 // to ensure any necessary pause logic occurs. In the case where the Activity will be
2581 // shown regardless of the lock screen, the call to
2582 // {@link ActivityStackSupervisor#checkReadyForSleepLocked} is skipped.
2583 final ActivityRecord next = topRunningActivityLocked(true /* focusableOnly */);
2584 if (next == null || !next.canTurnScreenOn()) {
2585 checkReadyForSleep();
2586 }
Craig Mautner544efa72014-09-04 16:41:20 -07002587 } finally {
Andrii Kulian6b321512019-01-23 06:37:00 +00002588 mInResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07002589 }
chaviw59b98852017-06-13 12:05:44 -07002590
Craig Mautner544efa72014-09-04 16:41:20 -07002591 return result;
2592 }
2593
Bryce Leec4ab62a2018-03-05 14:19:26 -08002594 /**
2595 * Returns the currently resumed activity.
2596 */
2597 protected ActivityRecord getResumedActivity() {
2598 return mResumedActivity;
2599 }
2600
Bryce Lee84730a02018-04-03 14:10:04 -07002601 private void setResumedActivity(ActivityRecord r, String reason) {
2602 if (mResumedActivity == r) {
2603 return;
2604 }
Bryce Leec4ab62a2018-03-05 14:19:26 -08002605
Bryce Lee84730a02018-04-03 14:10:04 -07002606 if (DEBUG_STACK) Slog.d(TAG_STACK, "setResumedActivity stack:" + this + " + from: "
2607 + mResumedActivity + " to:" + r + " reason:" + reason);
2608 mResumedActivity = r;
Chong Zhang6cda19c2016-06-14 19:07:56 -07002609 }
2610
Andreas Gampea36dc622018-02-05 17:19:22 -08002611 @GuardedBy("mService")
Chong Zhang280d3322015-11-03 17:27:26 -08002612 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwale53783742018-09-16 10:21:51 -07002613 if (!mService.isBooting() && !mService.isBooted()) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002614 // Not ready yet!
2615 return false;
2616 }
2617
Winson Chung3f103eb2017-04-12 21:53:59 -07002618 // Find the next top-most activity to resume in this stack that is not finishing and is
2619 // focusable. If it is not focusable, we will fall into the case below to resume the
2620 // top activity in the next focusable task.
Andrii Kulian6b321512019-01-23 06:37:00 +00002621 ActivityRecord next = topRunningActivityLocked(true /* focusableOnly */);
Andrii Kulian7fc22812016-12-28 13:04:11 -08002622
2623 final boolean hasRunningActivity = next != null;
2624
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002625 // TODO: Maybe this entire condition can get removed?
Wale Ogunwale66e16852017-10-19 13:35:52 -07002626 if (hasRunningActivity && !isAttached()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08002627 return false;
2628 }
2629
Wale Ogunwaled32da472018-11-16 07:19:28 -08002630 mRootActivityContainer.cancelInitializingActivities();
Craig Mautnera61bc652013-10-28 15:43:18 -07002631
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002632 // Remember how we'll process this pause/resume situation, and ensure
2633 // that the state is reset however we wind up proceeding.
Wale Ogunwalec59b4f62017-11-30 11:05:43 -08002634 boolean userLeaving = mStackSupervisor.mUserLeaving;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002635 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002636
Andrii Kulian7fc22812016-12-28 13:04:11 -08002637 if (!hasRunningActivity) {
2638 // There are no activities left in the stack, let's look somewhere else.
2639 return resumeTopActivityInNextFocusableStack(prev, options, "noMoreActivities");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002640 }
2641
2642 next.delayedResume = false;
Riddle Hsubbb63c22018-10-03 12:28:29 +08002643 final ActivityDisplay display = getDisplay();
Craig Mautner58547802013-03-05 08:23:53 -08002644
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002645 // If the top activity is the resumed one, nothing to do.
Bryce Lee7ace3952018-02-16 14:34:32 -08002646 if (mResumedActivity == next && next.isState(RESUMED)
Riddle Hsubbb63c22018-10-03 12:28:29 +08002647 && display.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002648 // Make sure we have executed any pending transitions, since there
2649 // should be nothing left to do at this point.
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002650 executeAppTransition(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002651 if (DEBUG_STATES) Slog.d(TAG_STATES,
2652 "resumeTopActivityLocked: Top activity resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002653 return false;
2654 }
2655
Louis Chang77ce34d2019-01-03 15:45:12 +08002656 if (!next.canResumeByCompat()) {
2657 return false;
2658 }
2659
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002660 // If we are sleeping, and there is no resumed activity, and the top
2661 // activity is paused, well that is the state we want.
David Stevens9440dc82017-03-16 19:00:20 -07002662 if (shouldSleepOrShutDownActivities()
p13451dbad2872012-04-18 11:39:23 +09002663 && mLastPausedActivity == next
Wale Ogunwaled32da472018-11-16 07:19:28 -08002664 && mRootActivityContainer.allPausedActivitiesComplete()) {
Riddle Hsuef713772018-09-18 01:28:53 +08002665 // If the current top activity may be able to occlude keyguard but the occluded state
2666 // has not been set, update visibility and check again if we should continue to resume.
2667 boolean nothingToResume = true;
Riddle Hsu32a87b82018-11-21 00:08:19 +08002668 if (!mService.mShuttingDown) {
2669 final boolean canShowWhenLocked = !mTopActivityOccludesKeyguard
2670 && next.canShowWhenLocked();
2671 final boolean mayDismissKeyguard = mTopDismissingKeyguardActivity != next
Issei Suzuki74e1eb22018-12-20 17:42:52 +01002672 && next.mAppWindowToken != null
2673 && next.mAppWindowToken.containsDismissKeyguardWindow();
2674
Riddle Hsu32a87b82018-11-21 00:08:19 +08002675 if (canShowWhenLocked || mayDismissKeyguard) {
2676 ensureActivitiesVisibleLocked(null /* starting */, 0 /* configChanges */,
2677 !PRESERVE_WINDOWS);
2678 nothingToResume = shouldSleepActivities();
2679 }
Riddle Hsuef713772018-09-18 01:28:53 +08002680 }
2681 if (nothingToResume) {
2682 // Make sure we have executed any pending transitions, since there
2683 // should be nothing left to do at this point.
2684 executeAppTransition(options);
2685 if (DEBUG_STATES) Slog.d(TAG_STATES,
2686 "resumeTopActivityLocked: Going to sleep and all paused");
Riddle Hsuef713772018-09-18 01:28:53 +08002687 return false;
2688 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002689 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002690
2691 // Make sure that the user who owns this activity is started. If not,
2692 // we will just leave it as is because someone should be bringing
2693 // another user's activities to the top of the stack.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002694 if (!mService.mAmInternal.hasStartedUserState(next.mUserId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002695 Slog.w(TAG, "Skipping resume of top activity " + next
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002696 + ": user " + next.mUserId + " is stopped");
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002697 return false;
2698 }
2699
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002700 // The activity may be waiting for stop, but that is no longer
2701 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002702 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002703 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002704 next.sleeping = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002705
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002706 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002707
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002708 // If we are currently pausing an activity, then don't do anything until that is done.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002709 if (!mRootActivityContainer.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002710 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002711 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Andrii Kulian6b321512019-01-23 06:37:00 +00002712
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002713 return false;
2714 }
2715
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002716 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2717
Wale Ogunwale89973222017-04-23 18:39:45 -07002718 boolean lastResumedCanPip = false;
chaviw4ad54912018-05-30 11:05:44 -07002719 ActivityRecord lastResumed = null;
Riddle Hsubbb63c22018-10-03 12:28:29 +08002720 final ActivityStack lastFocusedStack = display.getLastFocusedStack();
Wale Ogunwale89973222017-04-23 18:39:45 -07002721 if (lastFocusedStack != null && lastFocusedStack != this) {
2722 // So, why aren't we using prev here??? See the param comment on the method. prev doesn't
2723 // represent the last resumed activity. However, the last focus stack does if it isn't null.
chaviw4ad54912018-05-30 11:05:44 -07002724 lastResumed = lastFocusedStack.mResumedActivity;
Wale Ogunwalec59b4f62017-11-30 11:05:43 -08002725 if (userLeaving && inMultiWindowMode() && lastFocusedStack.shouldBeVisible(next)) {
2726 // The user isn't leaving if this stack is the multi-window mode and the last
2727 // focused stack should still be visible.
2728 if(DEBUG_USER_LEAVING) Slog.i(TAG_USER_LEAVING, "Overriding userLeaving to false"
2729 + " next=" + next + " lastResumed=" + lastResumed);
2730 userLeaving = false;
2731 }
Wale Ogunwale89973222017-04-23 18:39:45 -07002732 lastResumedCanPip = lastResumed != null && lastResumed.checkEnterPictureInPictureState(
Winson Chung298f95b2017-08-10 15:57:18 -07002733 "resumeTopActivity", userLeaving /* beforeStopping */);
Wale Ogunwale89973222017-04-23 18:39:45 -07002734 }
Winson Chungc2baac02017-01-11 13:34:47 -08002735 // If the flag RESUME_WHILE_PAUSING is set, then continue to schedule the previous activity
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07002736 // to be paused, while at the same time resuming the new resume activity only if the
2737 // previous activity can't go into Pip since we want to give Pip activities a chance to
2738 // enter Pip before resuming the next activity.
Wale Ogunwale89973222017-04-23 18:39:45 -07002739 final boolean resumeWhilePausing = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0
2740 && !lastResumedCanPip;
2741
Andrii Kulianab132ee2018-07-24 22:10:21 +08002742 boolean pausing = getDisplay().pauseBackStacks(userLeaving, next, false);
Craig Mautnereb957862013-04-24 15:34:32 -07002743 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002744 if (DEBUG_STATES) Slog.d(TAG_STATES,
2745 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Winson Chungc2baac02017-01-11 13:34:47 -08002746 pausing |= startPausingLocked(userLeaving, false, next, false);
Craig Mautnereb957862013-04-24 15:34:32 -07002747 }
Winson Chungc2baac02017-01-11 13:34:47 -08002748 if (pausing && !resumeWhilePausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002749 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002750 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002751 // At this point we want to put the upcoming activity's process
2752 // at the top of the LRU list, since we know we will be needing it
2753 // very soon and it would be a waste to let it get killed if it
2754 // happens to be sitting towards the end.
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002755 if (next.attachedToProcess()) {
2756 next.app.updateProcessInfo(false /* updateServiceConnectionActivities */,
2757 true /* updateLru */, true /* activityChange */, false /* updateOomAdj */);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002758 }
chaviw4ad54912018-05-30 11:05:44 -07002759 if (lastResumed != null) {
2760 lastResumed.setWillCloseOrEnterPip(true);
2761 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002762 return true;
Bryce Lee7ace3952018-02-16 14:34:32 -08002763 } else if (mResumedActivity == next && next.isState(RESUMED)
Riddle Hsubbb63c22018-10-03 12:28:29 +08002764 && display.allResumedActivitiesComplete()) {
Wale Ogunwalecac5c322016-05-23 10:56:33 -07002765 // It is possible for the activity to be resumed when we paused back stacks above if the
2766 // next activity doesn't have to wait for pause to complete.
2767 // So, nothing else to-do except:
2768 // Make sure we have executed any pending transitions, since there
2769 // should be nothing left to do at this point.
skuhne@google.com1b974dc2016-12-09 13:41:29 -08002770 executeAppTransition(options);
Wale Ogunwalecac5c322016-05-23 10:56:33 -07002771 if (DEBUG_STATES) Slog.d(TAG_STATES,
2772 "resumeTopActivityLocked: Top activity resumed (dontWaitForPause) " + next);
Wale Ogunwalecac5c322016-05-23 10:56:33 -07002773 return true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002774 }
2775
Christopher Tated3f175c2012-06-14 14:16:54 -07002776 // If the most recent activity was noHistory but was only stopped rather
2777 // than stopped+finished because the device went to sleep, we need to make
2778 // sure to finish it as we're making a new activity topmost.
David Stevens9440dc82017-03-16 19:00:20 -07002779 if (shouldSleepActivities() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002780 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002781 if (DEBUG_STATES) Slog.d(TAG_STATES,
2782 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002783 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002784 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002785 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002786 }
2787
Jorim Jaggi9b5e3312019-03-01 18:08:00 +01002788 if (prev != null && prev != next && next.nowVisible) {
2789
2790 // The next activity is already visible, so hide the previous
2791 // activity's windows right now so we can show the new one ASAP.
2792 // We only do this if the previous is finishing, which should mean
2793 // it is on top of the one being resumed so hiding it quickly
2794 // is good. Otherwise, we want to do the normal route of allowing
2795 // the resumed activity to be shown so we can decide if the
2796 // previous should actually be hidden depending on whether the
2797 // new one is found to be full-screen or not.
2798 if (prev.finishing) {
2799 prev.setVisibility(false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002800 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Jorim Jaggi9b5e3312019-03-01 18:08:00 +01002801 "Not waiting for visible to hide: " + prev
2802 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002803 } else {
Jorim Jaggi9b5e3312019-03-01 18:08:00 +01002804 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2805 "Previous already visible but still waiting to hide: " + prev
2806 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002807 }
Jorim Jaggi9b5e3312019-03-01 18:08:00 +01002808
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002809 }
2810
Dianne Hackborne7f97212011-02-24 14:40:20 -08002811 // Launching this app's activity, make sure the app is no longer
2812 // considered stopped.
2813 try {
2814 AppGlobals.getPackageManager().setPackageStoppedState(
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002815 next.packageName, false, next.mUserId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002816 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002817 } catch (IllegalArgumentException e) {
2818 Slog.w(TAG, "Failed trying to unstop package "
2819 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002820 }
2821
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002822 // We are starting up the next activity, so tell the window manager
2823 // that the previous one will be hidden soon. This way it can know
2824 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002825 boolean anim = true;
Wale Ogunwale3a256e62018-12-06 14:41:18 -08002826 final DisplayContent dc = getDisplay().mDisplayContent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002827 if (prev != null) {
2828 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002829 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002830 "Prepare close transition: prev=" + prev);
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002831 if (mStackSupervisor.mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002832 anim = false;
Wale Ogunwale3a256e62018-12-06 14:41:18 -08002833 dc.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002834 } else {
Wale Ogunwale3a256e62018-12-06 14:41:18 -08002835 dc.prepareAppTransition(
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002836 prev.getTaskRecord() == next.getTaskRecord() ? TRANSIT_ACTIVITY_CLOSE
lumark588a3e82018-07-20 18:53:54 +08002837 : TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002838 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002839 prev.setVisibility(false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002840 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002841 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2842 "Prepare open transition: prev=" + prev);
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002843 if (mStackSupervisor.mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002844 anim = false;
Wale Ogunwale3a256e62018-12-06 14:41:18 -08002845 dc.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002846 } else {
Wale Ogunwale3a256e62018-12-06 14:41:18 -08002847 dc.prepareAppTransition(
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002848 prev.getTaskRecord() == next.getTaskRecord() ? TRANSIT_ACTIVITY_OPEN
lumark588a3e82018-07-20 18:53:54 +08002849 : next.mLaunchTaskBehind ? TRANSIT_TASK_OPEN_BEHIND
2850 : TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002851 }
2852 }
Craig Mautner967212c2013-04-13 21:10:58 -07002853 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002854 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002855 if (mStackSupervisor.mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002856 anim = false;
Wale Ogunwale3a256e62018-12-06 14:41:18 -08002857 dc.prepareAppTransition(TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002858 } else {
Wale Ogunwale3a256e62018-12-06 14:41:18 -08002859 dc.prepareAppTransition(TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002860 }
2861 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002862
Craig Mautner525f3d92013-05-07 14:01:50 -07002863 if (anim) {
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002864 next.applyOptionsLocked();
2865 } else {
2866 next.clearOptionsLocked();
2867 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002868
Jorim Jaggifa9ed962018-01-25 00:16:49 +01002869 mStackSupervisor.mNoAnimActivities.clear();
2870
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002871 if (next.attachedToProcess()) {
Chong Zhangdea4bd92016-03-15 12:50:03 -07002872 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next
2873 + " stopped=" + next.stopped + " visible=" + next.visible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002874
Chong Zhangd127c6d2016-05-02 16:36:41 -07002875 // If the previous activity is translucent, force a visibility update of
2876 // the next activity, so that it's added to WM's opening app list, and
2877 // transition animation can be set up properly.
2878 // For example, pressing Home button with a translucent activity in focus.
2879 // Launcher is already visible in this case. If we don't add it to opening
2880 // apps, maybeUpdateTransitToWallpaper() will fail to identify this as a
2881 // TRANSIT_WALLPAPER_OPEN animation, and run some funny animation.
Riddle Hsubbb63c22018-10-03 12:28:29 +08002882 final boolean lastActivityTranslucent = lastFocusedStack != null
2883 && (lastFocusedStack.inMultiWindowMode()
2884 || (lastFocusedStack.mLastPausedActivity != null
2885 && !lastFocusedStack.mLastPausedActivity.fullscreen));
Chong Zhangd127c6d2016-05-02 16:36:41 -07002886
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08002887 // This activity is now becoming visible.
2888 if (!next.visible || next.stopped || lastActivityTranslucent) {
2889 next.setVisibility(true);
2890 }
2891
2892 // schedule launch ticks to collect information about slow apps.
2893 next.startLaunchTickingLocked();
2894
2895 ActivityRecord lastResumedActivity =
2896 lastFocusedStack == null ? null : lastFocusedStack.mResumedActivity;
2897 final ActivityState lastState = next.getState();
2898
2899 mService.updateCpuStats();
2900
2901 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next
2902 + " (in existing)");
2903
2904 next.setState(RESUMED, "resumeTopActivityInnerLocked");
2905
2906 next.app.updateProcessInfo(false /* updateServiceConnectionActivities */,
2907 true /* updateLru */, true /* activityChange */, true /* updateOomAdj */);
2908 updateLRUListLocked(next);
2909
2910 // Have the window manager re-evaluate the orientation of
2911 // the screen based on the new activity order.
2912 boolean notUpdated = true;
2913
Andrii Kulian6b321512019-01-23 06:37:00 +00002914 // Activity should also be visible if set mLaunchTaskBehind to true (see
2915 // ActivityRecord#shouldBeVisibleIgnoringKeyguard()).
2916 if (shouldBeVisible(next)) {
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08002917 // We have special rotation behavior when here is some active activity that
2918 // requests specific orientation or Keyguard is locked. Make sure all activity
2919 // visibilities are set correctly as well as the transition is updated if needed
2920 // to get the correct rotation behavior. Otherwise the following call to update
2921 // the orientation may cause incorrect configurations delivered to client as a
2922 // result of invisible window resize.
2923 // TODO: Remove this once visibilities are set correctly immediately when
2924 // starting an activity.
Wale Ogunwaled32da472018-11-16 07:19:28 -08002925 notUpdated = !mRootActivityContainer.ensureVisibilityAndConfig(next, mDisplayId,
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08002926 true /* markFrozenIfConfigChanged */, false /* deferResume */);
2927 }
2928
2929 if (notUpdated) {
2930 // The configuration update wasn't able to keep the existing
2931 // instance of the activity, and instead started a new one.
2932 // We should be all done, but let's just make sure our activity
2933 // is still at the top and schedule another run if something
2934 // weird happened.
2935 ActivityRecord nextNext = topRunningActivityLocked();
2936 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
2937 "Activity config changed during resume: " + next
2938 + ", new next: " + nextNext);
2939 if (nextNext != next) {
2940 // Do over!
2941 mStackSupervisor.scheduleResumeTopActivities();
2942 }
2943 if (!next.visible || next.stopped) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002944 next.setVisibility(true);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002945 }
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08002946 next.completeResumeLocked();
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08002947 return true;
2948 }
Craig Mautner58547802013-03-05 08:23:53 -08002949
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08002950 try {
2951 final ClientTransaction transaction =
2952 ClientTransaction.obtain(next.app.getThread(), next.appToken);
2953 // Deliver all pending results.
2954 ArrayList<ResultInfo> a = next.results;
2955 if (a != null) {
2956 final int N = a.size();
2957 if (!next.finishing && N > 0) {
2958 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2959 "Delivering results to " + next + ": " + a);
2960 transaction.addCallback(ActivityResultItem.obtain(a));
2961 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002962 }
2963
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08002964 if (next.newIntents != null) {
2965 transaction.addCallback(NewIntentItem.obtain(next.newIntents,
2966 false /* andPause */));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002967 }
2968
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08002969 // Well the app will no longer be stopped.
2970 // Clear app token stopped state in window manager if needed.
2971 next.notifyAppResumed(next.stopped);
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002972
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002973 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.mUserId,
2974 System.identityHashCode(next), next.getTaskRecord().taskId,
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08002975 next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002976
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08002977 next.sleeping = false;
2978 mService.getAppWarningsLocked().onResumeActivity(next);
2979 next.app.setPendingUiCleanAndForceProcessStateUpTo(mService.mTopProcessState);
2980 next.clearOptionsLocked();
2981 transaction.setLifecycleStateRequest(
2982 ResumeActivityItem.obtain(next.app.getReportedProcState(),
Wale Ogunwale3a256e62018-12-06 14:41:18 -08002983 getDisplay().mDisplayContent.isNextTransitionForward()));
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08002984 mService.getLifecycleManager().scheduleTransaction(transaction);
Craig Mautner58547802013-03-05 08:23:53 -08002985
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08002986 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed "
2987 + next);
2988 } catch (Exception e) {
2989 // Whoops, need to restart this activity!
2990 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
2991 + lastState + ": " + next);
2992 next.setState(lastState, "resumeTopActivityInnerLocked");
Bryce Lee69ad8182017-09-28 10:01:36 -07002993
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08002994 // lastResumedActivity being non-null implies there is a lastStack present.
2995 if (lastResumedActivity != null) {
2996 lastResumedActivity.setState(RESUMED, "resumeTopActivityInnerLocked");
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002997 }
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08002998
2999 Slog.i(TAG, "Restarting because process died: " + next);
3000 if (!next.hasBeenLaunched) {
3001 next.hasBeenLaunched = true;
3002 } else if (SHOW_APP_STARTING_PREVIEW && lastFocusedStack != null
3003 && lastFocusedStack.isTopStackOnDisplay()) {
3004 next.showStartingWindow(null /* prev */, false /* newTask */,
3005 false /* taskSwitch */);
3006 }
3007 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08003008 return true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003009 }
3010
3011 // From this point on, if something goes wrong there is no way
3012 // to recover the activity.
3013 try {
Andrii Kulian21713ac2016-10-12 22:05:05 -07003014 next.completeResumeLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003015 } catch (Exception e) {
3016 // If any exception gets thrown, toss away this
3017 // activity and try the next one.
3018 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003019 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003020 "resume-exception", true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003021 return true;
3022 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003023 } else {
3024 // Whoops, need to restart this activity!
3025 if (!next.hasBeenLaunched) {
3026 next.hasBeenLaunched = true;
3027 } else {
3028 if (SHOW_APP_STARTING_PREVIEW) {
Jorim Jaggi02886a82016-12-06 09:10:06 -08003029 next.showStartingWindow(null /* prev */, false /* newTask */,
3030 false /* taskSwich */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003031 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003032 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003033 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003034 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07003035 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003036 }
3037
3038 return true;
3039 }
3040
Andrii Kulian7fc22812016-12-28 13:04:11 -08003041 private boolean resumeTopActivityInNextFocusableStack(ActivityRecord prev,
3042 ActivityOptions options, String reason) {
Andrii Kulianab132ee2018-07-24 22:10:21 +08003043 final ActivityStack nextFocusedStack = adjustFocusToNextFocusableStack(reason);
3044 if (nextFocusedStack != null) {
Andrii Kulian7fc22812016-12-28 13:04:11 -08003045 // Try to move focus to the next visible stack with a running activity if this
3046 // stack is not covering the entire screen or is on a secondary display (with no home
3047 // stack).
Wale Ogunwaled32da472018-11-16 07:19:28 -08003048 return mRootActivityContainer.resumeFocusedStacksTopActivities(nextFocusedStack, prev,
Andrii Kulianab132ee2018-07-24 22:10:21 +08003049 null /* targetOptions */);
Andrii Kulian7fc22812016-12-28 13:04:11 -08003050 }
3051
3052 // Let's just start up the Launcher...
3053 ActivityOptions.abort(options);
3054 if (DEBUG_STATES) Slog.d(TAG_STATES,
3055 "resumeTopActivityInNextFocusableStack: " + reason + ", go home");
Wale Ogunwaled32da472018-11-16 07:19:28 -08003056 return mRootActivityContainer.resumeHomeActivity(prev, reason, mDisplayId);
Andrii Kulian7fc22812016-12-28 13:04:11 -08003057 }
3058
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08003059 /** Returns the position the input task should be placed in this stack. */
3060 int getAdjustedPositionForTask(TaskRecord task, int suggestedPosition,
3061 ActivityRecord starting) {
3062
3063 int maxPosition = mTaskHistory.size();
3064 if ((starting != null && starting.okToShowLocked())
3065 || (starting == null && task.okToShowLocked())) {
3066 // If the task or starting activity can be shown, then whatever position is okay.
3067 return Math.min(suggestedPosition, maxPosition);
3068 }
3069
3070 // The task can't be shown, put non-current user tasks below current user tasks.
3071 while (maxPosition > 0) {
3072 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
3073 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
3074 || tmpTask.topRunningActivityLocked() == null) {
3075 break;
3076 }
3077 maxPosition--;
3078 }
3079
3080 return Math.min(suggestedPosition, maxPosition);
3081 }
3082
Andrii Kuliand2765632016-12-12 22:26:34 -08003083 /**
3084 * Used from {@link ActivityStack#positionTask(TaskRecord, int)}.
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07003085 * @see ActivityTaskManagerService#positionTaskInStack(int, int, int).
Andrii Kuliand2765632016-12-12 22:26:34 -08003086 */
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003087 private void insertTaskAtPosition(TaskRecord task, int position) {
3088 if (position >= mTaskHistory.size()) {
3089 insertTaskAtTop(task, null);
3090 return;
Evan Rosky9c448172017-11-02 14:19:27 -07003091 } else if (position <= 0) {
3092 insertTaskAtBottom(task);
3093 return;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003094 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08003095 position = getAdjustedPositionForTask(task, position, null /* starting */);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003096 mTaskHistory.remove(task);
3097 mTaskHistory.add(position, task);
Yunfan Chen279f5582018-12-12 15:24:50 -08003098 if (mTaskStack != null) {
3099 // TODO: this could not be false after unification.
3100 mTaskStack.positionChildAt(task.getTask(), position);
3101 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003102 updateTaskMovement(task, true);
3103 }
3104
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08003105 private void insertTaskAtTop(TaskRecord task, ActivityRecord starting) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08003106 // TODO: Better place to put all the code below...may be addTask...
3107 mTaskHistory.remove(task);
3108 // Now put task at top.
3109 final int position = getAdjustedPositionForTask(task, mTaskHistory.size(), starting);
3110 mTaskHistory.add(position, task);
3111 updateTaskMovement(task, true);
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003112 positionChildWindowContainerAtTop(task);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08003113 }
3114
Evan Rosky9c448172017-11-02 14:19:27 -07003115 private void insertTaskAtBottom(TaskRecord task) {
Evan Rosky9c448172017-11-02 14:19:27 -07003116 mTaskHistory.remove(task);
3117 final int position = getAdjustedPositionForTask(task, 0, null);
3118 mTaskHistory.add(position, task);
3119 updateTaskMovement(task, true);
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003120 positionChildWindowContainerAtBottom(task);
Evan Rosky9c448172017-11-02 14:19:27 -07003121 }
3122
Bryce Leead5b8322018-03-08 14:28:52 -08003123 void startActivityLocked(ActivityRecord r, ActivityRecord focusedTopActivity,
Winson Chungb5c41b72016-12-07 15:00:47 -08003124 boolean newTask, boolean keepCurTransition, ActivityOptions options) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003125 TaskRecord rTask = r.getTaskRecord();
Craig Mautnerd2328952013-03-05 12:46:26 -08003126 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07003127 // mLaunchTaskBehind tasks get placed at the back of the task stack.
3128 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08003129 // Last activity in task had been removed or ActivityManagerService is reusing task.
3130 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08003131 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003132 insertTaskAtTop(rTask, r);
Craig Mautner77878772013-03-04 19:46:24 -08003133 }
Craig Mautner525f3d92013-05-07 14:01:50 -07003134 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003135 if (!newTask) {
3136 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08003137 boolean startIt = true;
3138 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3139 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08003140 if (task.getTopActivity() == null) {
3141 // All activities in task are finishing.
3142 continue;
3143 }
Bryce Leeaf691c02017-03-20 14:20:22 -07003144 if (task == rTask) {
Craig Mautner70a86932013-02-28 22:37:44 -08003145 // Here it is! Now, if this is not yet visible to the
3146 // user, then just add it without starting; it will
3147 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08003148 if (!startIt) {
3149 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
3150 + task, new RuntimeException("here").fillInStackTrace());
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08003151 r.createAppWindowToken();
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003152 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003153 return;
3154 }
3155 break;
Craig Mautner70a86932013-02-28 22:37:44 -08003156 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003157 startIt = false;
3158 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003159 }
3160 }
3161
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003162 // Place a new activity at top of stack, so it is next to interact with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08003163
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003164 // If we are not placing the new activity frontmost, we do not want to deliver the
3165 // onUserLeaving callback to the actual frontmost activity
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003166 final TaskRecord activityTask = r.getTaskRecord();
Bryce Leeaf691c02017-03-20 14:20:22 -07003167 if (task == activityTask && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003168 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003169 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08003170 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003171 }
Craig Mautner70a86932013-02-28 22:37:44 -08003172
Bryce Leeaf691c02017-03-20 14:20:22 -07003173 task = activityTask;
Craig Mautner70a86932013-02-28 22:37:44 -08003174
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003175 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08003176 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08003177 new RuntimeException("here").fillInStackTrace());
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08003178 // TODO: Need to investigate if it is okay for the controller to already be created by the
3179 // time we get to this point. I think it is, but need to double check.
3180 // Use test in b/34179495 to trace the call path.
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08003181 if (r.mAppWindowToken == null) {
3182 r.createAppWindowToken();
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08003183 }
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08003184
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003185 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08003186
Matthew Ngae1ff4f2016-11-10 15:49:14 -08003187 if (!isHomeOrRecentsStack() || numActivities() > 0) {
Wale Ogunwale3a256e62018-12-06 14:41:18 -08003188 final DisplayContent dc = getDisplay().mDisplayContent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003189 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003190 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003191 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Wale Ogunwale3a256e62018-12-06 14:41:18 -08003192 dc.prepareAppTransition(TRANSIT_NONE, keepCurTransition);
Jorim Jaggifa9ed962018-01-25 00:16:49 +01003193 mStackSupervisor.mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003194 } else {
Winson Chungb5c41b72016-12-07 15:00:47 -08003195 int transit = TRANSIT_ACTIVITY_OPEN;
3196 if (newTask) {
3197 if (r.mLaunchTaskBehind) {
3198 transit = TRANSIT_TASK_OPEN_BEHIND;
3199 } else {
Winson Chungc2baac02017-01-11 13:34:47 -08003200 // If a new task is being launched, then mark the existing top activity as
Winson Chung942a85c2017-07-11 15:07:45 -07003201 // supporting picture-in-picture while pausing only if the starting activity
3202 // would not be considered an overlay on top of the current activity
3203 // (eg. not fullscreen, or the assistant)
Winson Chungf7e03e12017-08-22 11:32:16 -07003204 if (canEnterPipOnTaskSwitch(focusedTopActivity,
3205 null /* toFrontTask */, r, options)) {
3206 focusedTopActivity.supportsEnterPipOnTaskSwitch = true;
Winson Chungb5c41b72016-12-07 15:00:47 -08003207 }
3208 transit = TRANSIT_TASK_OPEN;
3209 }
3210 }
Wale Ogunwale3a256e62018-12-06 14:41:18 -08003211 dc.prepareAppTransition(transit, keepCurTransition);
Jorim Jaggifa9ed962018-01-25 00:16:49 +01003212 mStackSupervisor.mNoAnimActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003213 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003214 boolean doShow = true;
3215 if (newTask) {
3216 // Even though this activity is starting fresh, we still need
3217 // to reset it to make sure we apply affinities to move any
3218 // existing activities from other tasks in to it.
3219 // If the caller has requested that the target task be
3220 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07003221 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003222 resetTaskIfNeededLocked(r, r);
3223 doShow = topRunningNonDelayedActivityLocked(null) == r;
3224 }
Chong Zhang280d3322015-11-03 17:27:26 -08003225 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07003226 == ActivityOptions.ANIM_SCENE_TRANSITION) {
3227 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003228 }
Craig Mautnerbb742462014-07-07 15:28:55 -07003229 if (r.mLaunchTaskBehind) {
3230 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
3231 // tell WindowManager that r is visible even though it is at the back of the stack.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08003232 r.setVisibility(true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003233 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07003234 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003235 // Figure out if we are transitioning from another activity that is
3236 // "has the same starting icon" as the next one. This allows the
3237 // window manager to keep the previous window it had previously
3238 // created, if it still had one.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003239 TaskRecord prevTask = r.getTaskRecord();
Bryce Leeaf691c02017-03-20 14:20:22 -07003240 ActivityRecord prev = prevTask.topRunningActivityWithStartingWindowLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003241 if (prev != null) {
3242 // We don't want to reuse the previous starting preview if:
3243 // (1) The current activity is in a different task.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003244 if (prev.getTaskRecord() != prevTask) {
Craig Mautner29219d92013-04-16 20:19:12 -07003245 prev = null;
3246 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003247 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07003248 else if (prev.nowVisible) {
3249 prev = null;
3250 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003251 }
Jorim Jaggi02886a82016-12-06 09:10:06 -08003252 r.showStartingWindow(prev, newTask, isTaskSwitch(r, focusedTopActivity));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003253 }
3254 } else {
3255 // If this is the first activity, don't do any fancy animations,
3256 // because there is nothing for it to animate on top of.
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003257 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003258 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08003259 }
3260
Winson Chungf7e03e12017-08-22 11:32:16 -07003261 /**
3262 * @return Whether the switch to another task can trigger the currently running activity to
3263 * enter PiP while it is pausing (if supported). Only one of {@param toFrontTask} or
3264 * {@param toFrontActivity} should be set.
3265 */
3266 private boolean canEnterPipOnTaskSwitch(ActivityRecord pipCandidate,
3267 TaskRecord toFrontTask, ActivityRecord toFrontActivity, ActivityOptions opts) {
3268 if (opts != null && opts.disallowEnterPictureInPictureWhileLaunching()) {
3269 // Ensure the caller has requested not to trigger auto-enter PiP
3270 return false;
3271 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003272 if (pipCandidate == null || pipCandidate.inPinnedWindowingMode()) {
Winson Chungf7e03e12017-08-22 11:32:16 -07003273 // Ensure that we do not trigger entering PiP an activity on the pinned stack
3274 return false;
3275 }
Wale Ogunwale68278562017-09-23 17:13:55 -07003276 final ActivityStack targetStack = toFrontTask != null
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003277 ? toFrontTask.getStack() : toFrontActivity.getActivityStack();
Wale Ogunwale68278562017-09-23 17:13:55 -07003278 if (targetStack != null && targetStack.isActivityTypeAssistant()) {
Winson Chungf7e03e12017-08-22 11:32:16 -07003279 // Ensure the task/activity being brought forward is not the assistant
3280 return false;
3281 }
Winson Chungf7e03e12017-08-22 11:32:16 -07003282 return true;
3283 }
3284
Jorim Jaggi02886a82016-12-06 09:10:06 -08003285 private boolean isTaskSwitch(ActivityRecord r,
3286 ActivityRecord topFocusedActivity) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003287 return topFocusedActivity != null && r.getTaskRecord() != topFocusedActivity.getTaskRecord();
Jorim Jaggi02886a82016-12-06 09:10:06 -08003288 }
3289
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003290 /**
3291 * Perform a reset of the given task, if needed as part of launching it.
3292 * Returns the new HistoryRecord at the top of the task.
3293 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08003294 /**
3295 * Helper method for #resetTaskIfNeededLocked.
3296 * We are inside of the task being reset... we'll either finish this activity, push it out
3297 * for another task, or leave it as-is.
3298 * @param task The task containing the Activity (taskTop) that might be reset.
3299 * @param forceReset
3300 * @return An ActivityOptions that needs to be processed.
3301 */
Andrii Kulian21713ac2016-10-12 22:05:05 -07003302 private ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003303 ActivityOptions topOptions = null;
3304
3305 int replyChainEnd = -1;
3306 boolean canMoveOptions = true;
3307
3308 // We only do this for activities that are not the root of the task (since if we finish
3309 // the root, we may no longer have the task!).
3310 final ArrayList<ActivityRecord> activities = task.mActivities;
3311 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07003312 final int rootActivityNdx = task.findEffectiveRootIndex();
3313 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003314 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00003315 if (target.frontOfTask)
3316 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003317
3318 final int flags = target.info.flags;
3319 final boolean finishOnTaskLaunch =
3320 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
3321 final boolean allowTaskReparenting =
3322 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
3323 final boolean clearWhenTaskReset =
3324 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
3325
3326 if (!finishOnTaskLaunch
3327 && !clearWhenTaskReset
3328 && target.resultTo != null) {
3329 // If this activity is sending a reply to a previous
3330 // activity, we can't do anything with it now until
3331 // we reach the start of the reply chain.
3332 // XXX note that we are assuming the result is always
3333 // to the previous activity, which is almost always
3334 // the case but we really shouldn't count on.
3335 if (replyChainEnd < 0) {
3336 replyChainEnd = i;
3337 }
3338 } else if (!finishOnTaskLaunch
3339 && !clearWhenTaskReset
3340 && allowTaskReparenting
3341 && target.taskAffinity != null
3342 && !target.taskAffinity.equals(task.affinity)) {
3343 // If this activity has an affinity for another
3344 // task, then we need to move it out of here. We will
3345 // move it as far out of the way as possible, to the
3346 // bottom of the activity stack. This also keeps it
3347 // correctly ordered with any activities we previously
3348 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08003349 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07003350 final ActivityRecord bottom =
3351 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08003352 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07003353 if (bottom != null && target.taskAffinity != null
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003354 && target.taskAffinity.equals(bottom.getTaskRecord().affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003355 // If the activity currently at the bottom has the
3356 // same task affinity as the one we are moving,
3357 // then merge it into the same task.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003358 targetTask = bottom.getTaskRecord();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003359 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Bryce Leeaf691c02017-03-20 14:20:22 -07003360 + " out to bottom task " + targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003361 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08003362 targetTask = createTaskRecord(
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003363 mStackSupervisor.getNextTaskIdForUserLocked(target.mUserId),
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07003364 target.info, null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08003365 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003366 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Bryce Leeaf691c02017-03-20 14:20:22 -07003367 + " out to new task " + targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003368 }
3369
Craig Mautner525f3d92013-05-07 14:01:50 -07003370 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003371 final int start = replyChainEnd < 0 ? i : replyChainEnd;
3372 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07003373 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003374 if (p.finishing) {
3375 continue;
3376 }
3377
Craig Mautnere3a74d52013-02-22 14:14:58 -08003378 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07003379 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003380 topOptions = p.takeOptionsLocked();
3381 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07003382 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003383 }
3384 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003385 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3386 "Removing activity " + p + " from task=" + task + " adding to task="
3387 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003388 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
Bryce Leeaf691c02017-03-20 14:20:22 -07003389 "Pushing next activity " + p + " out to target's task " + target);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08003390 p.reparent(targetTask, 0 /* position - bottom */, "resetTargetTaskIfNeeded");
Craig Mautnere3a74d52013-02-22 14:14:58 -08003391 }
3392
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003393 positionChildWindowContainerAtBottom(targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003394 replyChainEnd = -1;
3395 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
3396 // If the activity should just be removed -- either
3397 // because it asks for it, or the task should be
3398 // cleared -- then finish it and anything that is
3399 // part of its reply chain.
3400 int end;
3401 if (clearWhenTaskReset) {
3402 // In this case, we want to finish this activity
3403 // and everything above it, so be sneaky and pretend
3404 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08003405 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003406 } else if (replyChainEnd < 0) {
3407 end = i;
3408 } else {
3409 end = replyChainEnd;
3410 }
Craig Mautner525f3d92013-05-07 14:01:50 -07003411 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003412 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07003413 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003414 if (p.finishing) {
3415 continue;
3416 }
3417 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07003418 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003419 topOptions = p.takeOptionsLocked();
3420 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07003421 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003422 }
3423 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003424 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08003425 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08003426 if (finishActivityLocked(
3427 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003428 end--;
3429 srcPos--;
3430 }
3431 }
3432 replyChainEnd = -1;
3433 } else {
3434 // If we were in the middle of a chain, well the
3435 // activity that started it all doesn't want anything
3436 // special, so leave it all as-is.
3437 replyChainEnd = -1;
3438 }
3439 }
3440
3441 return topOptions;
3442 }
3443
3444 /**
3445 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
3446 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
3447 * @param affinityTask The task we are looking for an affinity to.
3448 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
3449 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
3450 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
3451 */
Craig Mautner525f3d92013-05-07 14:01:50 -07003452 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08003453 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003454 int replyChainEnd = -1;
3455 final int taskId = task.taskId;
3456 final String taskAffinity = task.affinity;
3457
3458 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
3459 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07003460 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
3461
3462 // Do not operate on or below the effective root Activity.
3463 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003464 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00003465 if (target.frontOfTask)
3466 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003467
3468 final int flags = target.info.flags;
3469 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
3470 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
3471
3472 if (target.resultTo != null) {
3473 // If this activity is sending a reply to a previous
3474 // activity, we can't do anything with it now until
3475 // we reach the start of the reply chain.
3476 // XXX note that we are assuming the result is always
3477 // to the previous activity, which is almost always
3478 // the case but we really shouldn't count on.
3479 if (replyChainEnd < 0) {
3480 replyChainEnd = i;
3481 }
3482 } else if (topTaskIsHigher
3483 && allowTaskReparenting
3484 && taskAffinity != null
3485 && taskAffinity.equals(target.taskAffinity)) {
3486 // This activity has an affinity for our task. Either remove it if we are
3487 // clearing or move it over to our task. Note that
3488 // we currently punt on the case where we are resetting a
3489 // task that is not at the top but who has activities above
3490 // with an affinity to it... this is really not a normal
3491 // case, and we will need to later pull that task to the front
3492 // and usually at that point we will do the reset and pick
3493 // up those remaining activities. (This only happens if
3494 // someone starts an activity in a new task from an activity
3495 // in a task that is not currently on top.)
3496 if (forceReset || finishOnTaskLaunch) {
3497 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003498 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
3499 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003500 for (int srcPos = start; srcPos >= i; --srcPos) {
3501 final ActivityRecord p = activities.get(srcPos);
3502 if (p.finishing) {
3503 continue;
3504 }
Todd Kennedy539db512014-12-15 09:57:55 -08003505 finishActivityLocked(
3506 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003507 }
3508 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003509 if (taskInsertionPoint < 0) {
3510 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08003511
Craig Mautner77878772013-03-04 19:46:24 -08003512 }
Craig Mautner77878772013-03-04 19:46:24 -08003513
3514 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003515 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
3516 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
3517 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08003518 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003519 final ActivityRecord p = activities.get(srcPos);
Winson Chung30480042017-01-26 10:55:34 -08003520 p.reparent(task, taskInsertionPoint, "resetAffinityTaskIfNeededLocked");
Craig Mautnere3a74d52013-02-22 14:14:58 -08003521
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003522 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3523 "Removing and adding activity " + p + " to stack at " + task
3524 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003525 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
3526 + " from " + srcPos + " in to resetting task " + task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003527 }
Riddle Hsu86cb7de2018-08-13 23:29:58 +08003528 positionChildWindowContainerAtTop(task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003529
3530 // Now we've moved it in to place... but what if this is
3531 // a singleTop activity and we have put it on top of another
3532 // instance of the same activity? Then we drop the instance
3533 // below so it remains singleTop.
3534 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
3535 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003536 int targetNdx = taskActivities.indexOf(target);
3537 if (targetNdx > 0) {
3538 ActivityRecord p = taskActivities.get(targetNdx - 1);
3539 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08003540 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
3541 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003542 }
3543 }
3544 }
3545 }
3546
3547 replyChainEnd = -1;
3548 }
3549 }
Craig Mautner77878772013-03-04 19:46:24 -08003550 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08003551 }
3552
Craig Mautner8849a5e2013-04-02 16:41:03 -07003553 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08003554 ActivityRecord newActivity) {
Winson Chung5fa39752017-10-04 14:50:15 -07003555 final boolean forceReset =
Craig Mautnere3a74d52013-02-22 14:14:58 -08003556 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003557 final TaskRecord task = taskTop.getTaskRecord();
Craig Mautnere3a74d52013-02-22 14:14:58 -08003558
3559 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
3560 * for remaining tasks. Used for later tasks to reparent to task. */
3561 boolean taskFound = false;
3562
3563 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
3564 ActivityOptions topOptions = null;
3565
Craig Mautner77878772013-03-04 19:46:24 -08003566 // Preserve the location for reparenting in the new task.
3567 int reparentInsertionPoint = -1;
3568
Craig Mautnere3a74d52013-02-22 14:14:58 -08003569 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
3570 final TaskRecord targetTask = mTaskHistory.get(i);
3571
3572 if (targetTask == task) {
3573 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
3574 taskFound = true;
3575 } else {
Craig Mautner77878772013-03-04 19:46:24 -08003576 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
3577 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08003578 }
3579 }
3580
Craig Mautner70a86932013-02-28 22:37:44 -08003581 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08003582 if (taskNdx >= 0) {
3583 do {
3584 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
3585 } while (taskTop == null && taskNdx >= 0);
3586 }
Craig Mautner70a86932013-02-28 22:37:44 -08003587
Craig Mautnere3a74d52013-02-22 14:14:58 -08003588 if (topOptions != null) {
3589 // If we got some ActivityOptions from an activity on top that
3590 // was removed from the task, propagate them to the new real top.
3591 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003592 taskTop.updateOptionsLocked(topOptions);
3593 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003594 topOptions.abort();
3595 }
3596 }
3597
3598 return taskTop;
3599 }
3600
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003601 void sendActivityResultLocked(int callingUid, ActivityRecord r,
3602 String resultWho, int requestCode, int resultCode, Intent data) {
3603
3604 if (callingUid > 0) {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07003605 mService.mUgmInternal.grantUriPermissionFromIntent(callingUid, r.packageName,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003606 data, r.getUriPermissionsLocked(), r.mUserId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003607 }
3608
3609 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
3610 + " : who=" + resultWho + " req=" + requestCode
3611 + " res=" + resultCode + " data=" + data);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003612 if (mResumedActivity == r && r.attachedToProcess()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003613 try {
3614 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
3615 list.add(new ResultInfo(resultWho, requestCode,
3616 resultCode, data));
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003617 mService.getLifecycleManager().scheduleTransaction(r.app.getThread(), r.appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08003618 ActivityResultItem.obtain(list));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003619 return;
3620 } catch (Exception e) {
3621 Slog.w(TAG, "Exception thrown sending result to " + r, e);
3622 }
3623 }
3624
3625 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
3626 }
3627
Shunta Sato63b8ee32016-07-11 13:32:52 +09003628 /** Returns true if the task is one of the task finishing on-top of the top running task. */
Wale Ogunwale66e16852017-10-19 13:35:52 -07003629 private boolean isATopFinishingTask(TaskRecord task) {
Shunta Sato63b8ee32016-07-11 13:32:52 +09003630 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
3631 final TaskRecord current = mTaskHistory.get(i);
3632 final ActivityRecord r = current.topRunningActivityLocked();
3633 if (r != null) {
3634 // We got a top running activity, so there isn't a top finishing task...
3635 return false;
3636 }
3637 if (current == task) {
3638 return true;
3639 }
3640 }
3641 return false;
3642 }
3643
Wale Ogunwale66e16852017-10-19 13:35:52 -07003644 private void adjustFocusedActivityStack(ActivityRecord r, String reason) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08003645 if (!mRootActivityContainer.isTopDisplayFocusedStack(this) ||
Shunta Sato63b8ee32016-07-11 13:32:52 +09003646 ((mResumedActivity != r) && (mResumedActivity != null))) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003647 return;
3648 }
3649
3650 final ActivityRecord next = topRunningActivityLocked();
3651 final String myReason = reason + " adjustFocus";
Bryce Lee3345c4e2017-04-25 07:40:41 -07003652
Wale Ogunwale66e16852017-10-19 13:35:52 -07003653 if (next == r) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08003654 final ActivityRecord top = mRootActivityContainer.topRunningActivity();
Louis Chang19443452018-10-09 12:10:21 +08003655 if (top != null) {
3656 top.moveFocusableActivityToTop(myReason);
3657 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07003658 return;
Wale Ogunwaled045c822015-12-02 09:14:28 -08003659 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003660
Wale Ogunwale66e16852017-10-19 13:35:52 -07003661 if (next != null && isFocusable()) {
3662 // Keep focus in stack if we have a top running activity and are focusable.
3663 return;
3664 }
3665
3666 // Task is not guaranteed to be non-null. For example, destroying the
3667 // {@link ActivityRecord} will disassociate the task from the activity.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003668 final TaskRecord task = r.getTaskRecord();
Wale Ogunwale66e16852017-10-19 13:35:52 -07003669
3670 if (task == null) {
3671 throw new IllegalStateException("activity no longer associated with task:" + r);
3672 }
3673
3674 // Move focus to next focusable stack if possible.
Riddle Hsu3026e8d2018-08-03 15:50:53 +08003675 final ActivityStack nextFocusableStack = adjustFocusToNextFocusableStack(myReason);
3676 if (nextFocusableStack != null) {
3677 final ActivityRecord top = nextFocusableStack.topRunningActivityLocked();
Wale Ogunwaled32da472018-11-16 07:19:28 -08003678 if (top != null && top == mRootActivityContainer.getTopResumedActivity()) {
Riddle Hsu3026e8d2018-08-03 15:50:53 +08003679 // TODO(b/111361570): Remove this and update focused app per-display in
3680 // WindowManager every time an activity becomes resumed in
3681 // ActivityTaskManagerService#setResumedActivityUncheckLocked().
3682 mService.setResumedActivityUncheckLocked(top, reason);
3683 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07003684 return;
3685 }
3686
3687 // Whatever...go home.
Louis Changbd48dca2018-08-29 17:44:34 +08003688 getDisplay().moveHomeActivityToTop(myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07003689 }
3690
Andrii Kulianab132ee2018-07-24 22:10:21 +08003691 /**
3692 * Find next proper focusable stack and make it focused.
3693 * @return The stack that now got the focus, {@code null} if none found.
3694 */
3695 ActivityStack adjustFocusToNextFocusableStack(String reason) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07003696 return adjustFocusToNextFocusableStack(reason, false /* allowFocusSelf */);
Andrii Kulian250d6532017-02-08 23:30:45 -08003697 }
3698
3699 /**
3700 * Find next proper focusable stack and make it focused.
3701 * @param allowFocusSelf Is the focus allowed to remain on the same stack.
Andrii Kulianab132ee2018-07-24 22:10:21 +08003702 * @return The stack that now got the focus, {@code null} if none found.
Andrii Kulian250d6532017-02-08 23:30:45 -08003703 */
Andrii Kulianab132ee2018-07-24 22:10:21 +08003704 private ActivityStack adjustFocusToNextFocusableStack(String reason, boolean allowFocusSelf) {
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -08003705 final ActivityStack stack =
Wale Ogunwaled32da472018-11-16 07:19:28 -08003706 mRootActivityContainer.getNextFocusableStack(this, !allowFocusSelf);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003707 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003708 if (stack == null) {
Andrii Kulianab132ee2018-07-24 22:10:21 +08003709 return null;
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003710 }
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003711
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003712 final ActivityRecord top = stack.topRunningActivityLocked();
3713
Wale Ogunwale66e16852017-10-19 13:35:52 -07003714 if (stack.isActivityTypeHome() && (top == null || !top.visible)) {
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003715 // If we will be focusing on the home stack next and its current top activity isn't
Wale Ogunwale66e16852017-10-19 13:35:52 -07003716 // visible, then use the move the home stack task to top to make the activity visible.
Louis Changbd48dca2018-08-29 17:44:34 +08003717 stack.getDisplay().moveHomeActivityToTop(reason);
Andrii Kulianab132ee2018-07-24 22:10:21 +08003718 return stack;
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003719 }
Chong Zhang6cda19c2016-06-14 19:07:56 -07003720
3721 stack.moveToFront(myReason);
Andrii Kulianab132ee2018-07-24 22:10:21 +08003722 return stack;
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003723 }
3724
Craig Mautnerf3333272013-04-22 10:55:53 -07003725 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003726 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003727 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3728 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3729 if (!r.finishing) {
David Stevens9440dc82017-03-16 19:00:20 -07003730 if (!shouldSleepActivities()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003731 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003732 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
3733 "stop-no-history", false)) {
Bryce Lee3345c4e2017-04-25 07:40:41 -07003734 // If {@link requestFinishActivityLocked} returns {@code true},
Wale Ogunwale66e16852017-10-19 13:35:52 -07003735 // {@link adjustFocusedActivityStack} would have been already called.
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003736 r.resumeKeyDispatchingLocked();
3737 return;
3738 }
Christopher Tated3f175c2012-06-14 14:16:54 -07003739 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003740 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07003741 + " on stop because we're just sleeping");
3742 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003743 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003744 }
3745
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003746 if (r.attachedToProcess()) {
Wale Ogunwale66e16852017-10-19 13:35:52 -07003747 adjustFocusedActivityStack(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003748 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003749 try {
3750 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003751 if (DEBUG_STATES) Slog.v(TAG_STATES,
3752 "Moving to STOPPING: " + r + " (stop requested)");
Bryce Lee7ace3952018-02-16 14:34:32 -08003753 r.setState(STOPPING, "stopActivityLocked");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003754 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3755 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003756 if (!r.visible) {
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07003757 r.setVisible(false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003758 }
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003759 EventLogTags.writeAmStopActivity(
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003760 r.mUserId, System.identityHashCode(r), r.shortComponentName);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003761 mService.getLifecycleManager().scheduleTransaction(r.app.getThread(), r.appToken,
Bryce Lee1d0d5142018-04-12 10:35:07 -07003762 StopActivityItem.obtain(r.visible, r.configChangeFlags));
David Stevens9440dc82017-03-16 19:00:20 -07003763 if (shouldSleepOrShutDownActivities()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003764 r.setSleeping(true);
3765 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003766 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003767 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003768 } catch (Exception e) {
3769 // Maybe just ignore exceptions here... if the process
3770 // has crashed, our death notification will clean things
3771 // up.
3772 Slog.w(TAG, "Exception thrown during pause", e);
3773 // Just in case, assume it to be stopped.
3774 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003775 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Bryce Lee7ace3952018-02-16 14:34:32 -08003776 r.setState(STOPPED, "stopActivityLocked");
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003777 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003778 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003779 }
3780 }
3781 }
3782 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003783
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003784 /**
3785 * @return Returns true if the activity is being finished, false if for
3786 * some reason it is being left as-is.
3787 */
3788 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003789 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003790 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003791 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3792 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003793 + ", result=" + resultCode + ", data=" + resultData
3794 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003795 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003796 return false;
3797 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003798
Craig Mautnerd44711d2013-02-23 11:24:36 -08003799 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003800 return true;
3801 }
3802
Craig Mautnerd2328952013-03-05 12:46:26 -08003803 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003804 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3805 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3806 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3807 ActivityRecord r = activities.get(activityNdx);
3808 if (r.resultTo == self && r.requestCode == requestCode) {
3809 if ((r.resultWho == null && resultWho == null) ||
3810 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3811 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3812 false);
3813 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003814 }
3815 }
3816 }
Wale Ogunwalef6733932018-06-27 05:14:34 -07003817 mService.updateOomAdj();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003818 }
3819
Andrii Kulian8cc92ac62018-04-02 23:14:18 -07003820 /**
3821 * Finish the topmost activity that belongs to the crashed app. We may also finish the activity
3822 * that requested launch of the crashed one to prevent launch-crash loop.
3823 * @param app The app that crashed.
3824 * @param reason Reason to perform this action.
3825 * @return The task that was finished in this stack, {@code null} if top running activity does
3826 * not belong to the crashed app.
3827 */
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003828 final TaskRecord finishTopCrashedActivityLocked(WindowProcessController app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003829 ActivityRecord r = topRunningActivityLocked();
Adrian Roos20d7df32016-01-12 18:59:43 +01003830 TaskRecord finishedTask = null;
3831 if (r == null || r.app != app) {
3832 return null;
3833 }
3834 Slog.w(TAG, " Force finishing activity "
3835 + r.intent.getComponent().flattenToShortString());
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003836 finishedTask = r.getTaskRecord();
Bryce Leeaf691c02017-03-20 14:20:22 -07003837 int taskNdx = mTaskHistory.indexOf(finishedTask);
3838 final TaskRecord task = finishedTask;
3839 int activityNdx = task.mActivities.indexOf(r);
Wale Ogunwale3a256e62018-12-06 14:41:18 -08003840 getDisplay().mDisplayContent.prepareAppTransition(
lumark588a3e82018-07-20 18:53:54 +08003841 TRANSIT_CRASHING_ACTIVITY_CLOSE, false /* alwaysKeepCurrent */);
Adrian Roos20d7df32016-01-12 18:59:43 +01003842 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Bryce Leeaf691c02017-03-20 14:20:22 -07003843 finishedTask = task;
Adrian Roos20d7df32016-01-12 18:59:43 +01003844 // Also terminate any activities below it that aren't yet
3845 // stopped, to avoid a situation where one will get
3846 // re-start our crashing activity once it gets resumed again.
3847 --activityNdx;
3848 if (activityNdx < 0) {
3849 do {
3850 --taskNdx;
3851 if (taskNdx < 0) {
3852 break;
3853 }
3854 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3855 } while (activityNdx < 0);
3856 }
3857 if (activityNdx >= 0) {
3858 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Bryce Lee7ace3952018-02-16 14:34:32 -08003859 if (r.isState(RESUMED, PAUSING, PAUSED)) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003860 if (!r.isActivityTypeHome() || mService.mHomeProcess != r.app) {
Adrian Roos20d7df32016-01-12 18:59:43 +01003861 Slog.w(TAG, " Force finishing activity "
3862 + r.intent.getComponent().flattenToShortString());
3863 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003864 }
3865 }
3866 }
Adrian Roos20d7df32016-01-12 18:59:43 +01003867 return finishedTask;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003868 }
3869
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003870 final void finishVoiceTask(IVoiceInteractionSession session) {
3871 IBinder sessionBinder = session.asBinder();
3872 boolean didOne = false;
3873 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3874 TaskRecord tr = mTaskHistory.get(taskNdx);
3875 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3876 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3877 ActivityRecord r = tr.mActivities.get(activityNdx);
3878 if (!r.finishing) {
3879 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3880 false);
3881 didOne = true;
3882 }
3883 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003884 } else {
3885 // Check if any of the activities are using voice
3886 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3887 ActivityRecord r = tr.mActivities.get(activityNdx);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003888 if (r.voiceSession != null && r.voiceSession.asBinder() == sessionBinder) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003889 // Inform of cancellation
3890 r.clearVoiceSessionLocked();
3891 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07003892 r.app.getThread().scheduleLocalVoiceInteractionStarted(
3893 r.appToken, null);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003894 } catch (RemoteException re) {
3895 // Ok
3896 }
Wale Ogunwalef6733932018-06-27 05:14:34 -07003897 mService.finishRunningVoiceLocked();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003898 break;
3899 }
3900 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003901 }
3902 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003903
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003904 if (didOne) {
Wale Ogunwalef6733932018-06-27 05:14:34 -07003905 mService.updateOomAdj();
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003906 }
3907 }
3908
Craig Mautnerd2328952013-03-05 12:46:26 -08003909 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003910 ArrayList<ActivityRecord> activities = r.getTaskRecord().mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003911 for (int index = activities.indexOf(r); index >= 0; --index) {
3912 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003913 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003914 break;
3915 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003916 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003917 }
3918 return true;
3919 }
3920
Andrii Kulian21713ac2016-10-12 22:05:05 -07003921 private void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
Dianne Hackborn5c607432012-02-28 14:44:19 -08003922 // send the result
3923 ActivityRecord resultTo = r.resultTo;
3924 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003925 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003926 + " who=" + r.resultWho + " req=" + r.requestCode
3927 + " res=" + resultCode + " data=" + resultData);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003928 if (resultTo.mUserId != r.mUserId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003929 if (resultData != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003930 resultData.prepareToLeaveUser(r.mUserId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003931 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003932 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003933 if (r.info.applicationInfo.uid > 0) {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07003934 mService.mUgmInternal.grantUriPermissionFromIntent(r.info.applicationInfo.uid,
Dianne Hackborn5c607432012-02-28 14:44:19 -08003935 resultTo.packageName, resultData,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003936 resultTo.getUriPermissionsLocked(), resultTo.mUserId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003937 }
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -07003938 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode, resultData);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003939 r.resultTo = null;
3940 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003941 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003942
3943 // Make sure this HistoryRecord is not holding on to other resources,
3944 // because clients have remote IPC references to this object so we
3945 // can't assume that will go away and want to avoid circular IPC refs.
3946 r.results = null;
3947 r.pendingResults = null;
3948 r.newIntents = null;
3949 r.icicle = null;
3950 }
3951
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003952 /**
Winson Chung6954fc92017-03-24 16:22:12 -07003953 * See {@link #finishActivityLocked(ActivityRecord, int, Intent, String, boolean, boolean)}
3954 */
3955 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3956 String reason, boolean oomAdj) {
3957 return finishActivityLocked(r, resultCode, resultData, reason, oomAdj, !PAUSE_IMMEDIATELY);
3958 }
3959
3960 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003961 * @return Returns true if this activity has been removed from the history
3962 * list, or false if it is still in the list and will be removed later.
3963 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003964 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
Winson Chung6954fc92017-03-24 16:22:12 -07003965 String reason, boolean oomAdj, boolean pauseImmediately) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003966 if (r.finishing) {
3967 Slog.w(TAG, "Duplicate finish request for " + r);
3968 return false;
3969 }
3970
Jorim Jaggife762342016-10-13 14:33:27 +02003971 mWindowManager.deferSurfaceLayout();
3972 try {
3973 r.makeFinishingLocked();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003974 final TaskRecord task = r.getTaskRecord();
Jorim Jaggife762342016-10-13 14:33:27 +02003975 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003976 r.mUserId, System.identityHashCode(r),
Jorim Jaggife762342016-10-13 14:33:27 +02003977 task.taskId, r.shortComponentName, reason);
3978 final ArrayList<ActivityRecord> activities = task.mActivities;
3979 final int index = activities.indexOf(r);
3980 if (index < (activities.size() - 1)) {
3981 task.setFrontOfTask();
3982 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
3983 // If the caller asked that this activity (and all above it)
3984 // be cleared when the task is reset, don't lose that information,
3985 // but propagate it up to the next activity.
3986 ActivityRecord next = activities.get(index+1);
3987 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
3988 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003989 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003990
Jorim Jaggife762342016-10-13 14:33:27 +02003991 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003992
Wale Ogunwale66e16852017-10-19 13:35:52 -07003993 adjustFocusedActivityStack(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003994
Jorim Jaggife762342016-10-13 14:33:27 +02003995 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003996
Yorke Leebdef5372017-04-10 16:38:51 -07003997 final boolean endTask = index <= 0 && !task.isClearingToReuseTask();
Jorim Jaggife762342016-10-13 14:33:27 +02003998 final int transit = endTask ? TRANSIT_TASK_CLOSE : TRANSIT_ACTIVITY_CLOSE;
3999 if (mResumedActivity == r) {
4000 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
4001 "Prepare close transition: finishing " + r);
Winson Chung6954fc92017-03-24 16:22:12 -07004002 if (endTask) {
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -07004003 mService.getTaskChangeNotificationController().notifyTaskRemovalStarted(
Mark Renoufc808f062019-02-07 15:20:37 -05004004 task.getTaskInfo());
Winson Chung6954fc92017-03-24 16:22:12 -07004005 }
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004006 getDisplay().mDisplayContent.prepareAppTransition(transit, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004007
Jorim Jaggife762342016-10-13 14:33:27 +02004008 // Tell window manager to prepare for this one to be removed.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08004009 r.setVisibility(false);
Jorim Jaggife762342016-10-13 14:33:27 +02004010
4011 if (mPausingActivity == null) {
4012 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
4013 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
4014 "finish() => pause with userLeaving=false");
Winson Chung6954fc92017-03-24 16:22:12 -07004015 startPausingLocked(false, false, null, pauseImmediately);
Jorim Jaggife762342016-10-13 14:33:27 +02004016 }
4017
4018 if (endTask) {
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -07004019 mService.getLockTaskController().clearLockedTask(task);
Jorim Jaggife762342016-10-13 14:33:27 +02004020 }
Bryce Lee7ace3952018-02-16 14:34:32 -08004021 } else if (!r.isState(PAUSING)) {
Jorim Jaggife762342016-10-13 14:33:27 +02004022 // If the activity is PAUSING, we will complete the finish once
4023 // it is done pausing; else we can just directly finish it here.
4024 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
4025 if (r.visible) {
Winson Chung6954fc92017-03-24 16:22:12 -07004026 prepareActivityHideTransitionAnimation(r, transit);
4027 }
4028
4029 final int finishMode = (r.visible || r.nowVisible) ? FINISH_AFTER_VISIBLE
4030 : FINISH_AFTER_PAUSE;
Bryce Leef52974c2018-02-14 15:12:01 -08004031 final boolean removedActivity = finishCurrentActivityLocked(r, finishMode, oomAdj,
4032 "finishActivityLocked") == null;
Winson Chung6954fc92017-03-24 16:22:12 -07004033
4034 // The following code is an optimization. When the last non-task overlay activity
4035 // is removed from the task, we remove the entire task from the stack. However,
4036 // since that is done after the scheduled destroy callback from the activity, that
4037 // call to change the visibility of the task overlay activities would be out of
4038 // sync with the activitiy visibility being set for this finishing activity above.
4039 // In this case, we can set the visibility of all the task overlay activities when
4040 // we detect the last one is finishing to keep them in sync.
4041 if (task.onlyHasTaskOverlayActivities(true /* excludeFinishing */)) {
4042 for (ActivityRecord taskOverlay : task.mActivities) {
4043 if (!taskOverlay.mTaskOverlay) {
4044 continue;
4045 }
4046 prepareActivityHideTransitionAnimation(taskOverlay, transit);
Jorim Jaggife762342016-10-13 14:33:27 +02004047 }
4048 }
Winson Chung6954fc92017-03-24 16:22:12 -07004049 return removedActivity;
Jorim Jaggife762342016-10-13 14:33:27 +02004050 } else {
4051 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
4052 }
4053
4054 return false;
4055 } finally {
4056 mWindowManager.continueSurfaceLayout();
4057 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004058 }
4059
Winson Chung6954fc92017-03-24 16:22:12 -07004060 private void prepareActivityHideTransitionAnimation(ActivityRecord r, int transit) {
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004061 final DisplayContent dc = getDisplay().mDisplayContent;
4062 dc.prepareAppTransition(transit, false);
Winson Chung6954fc92017-03-24 16:22:12 -07004063 r.setVisibility(false);
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004064 dc.executeAppTransition();
Winson Chung6954fc92017-03-24 16:22:12 -07004065 }
4066
Craig Mautnerf3333272013-04-22 10:55:53 -07004067 static final int FINISH_IMMEDIATELY = 0;
4068 static final int FINISH_AFTER_PAUSE = 1;
4069 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004070
Bryce Leef52974c2018-02-14 15:12:01 -08004071 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj,
4072 String reason) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004073 // First things first: if this activity is currently visible,
4074 // and the resumed activity is not yet visible, then hold off on
4075 // finishing until the resumed one becomes visible.
Chong Zhang824b6dc2016-04-27 14:11:12 -07004076
Bryce Leec961e0a2018-04-13 17:58:02 -07004077 // The activity that we are finishing may be over the lock screen. In this case, we do not
4078 // want to consider activities that cannot be shown on the lock screen as running and should
4079 // proceed with finishing the activity if there is no valid next top running activity.
Riddle Hsue10cea52018-10-16 23:33:23 +08004080 final ActivityDisplay display = getDisplay();
4081 final ActivityRecord next = display.topRunningActivity(true /* considerKeyguardState */);
Chong Zhang824b6dc2016-04-27 14:11:12 -07004082
Chong Zhangefd9a5b2016-04-26 16:21:07 -07004083 if (mode == FINISH_AFTER_VISIBLE && (r.visible || r.nowVisible)
Chong Zhang824b6dc2016-04-27 14:11:12 -07004084 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07004085 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Winson Chung4dabf232017-01-25 13:25:22 -08004086 addToStopping(r, false /* scheduleIdle */, false /* idleDelayed */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004087 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004088 if (DEBUG_STATES) Slog.v(TAG_STATES,
4089 "Moving to STOPPING: "+ r + " (finish requested)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004090 r.setState(STOPPING, "finishCurrentActivityLocked");
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07004091 if (oomAdj) {
Wale Ogunwalef6733932018-06-27 05:14:34 -07004092 mService.updateOomAdj();
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07004093 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004094 return r;
4095 }
4096
4097 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07004098 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07004099 mStackSupervisor.mGoingToSleepActivities.remove(r);
Bryce Lee7ace3952018-02-16 14:34:32 -08004100 final ActivityState prevState = r.getState();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004101 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Bryce Leeb0f993f2018-03-02 15:38:01 -08004102
Bryce Lee7ace3952018-02-16 14:34:32 -08004103 r.setState(FINISHING, "finishCurrentActivityLocked");
Riddle Hsue10cea52018-10-16 23:33:23 +08004104 final boolean finishingInNonFocusedStackOrNoRunning = mode == FINISH_AFTER_VISIBLE
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004105 && prevState == PAUSED && (r.getActivityStack() != display.getFocusedStack()
Riddle Hsue10cea52018-10-16 23:33:23 +08004106 || (next == null && display.topRunningActivity() == null));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004107
4108 if (mode == FINISH_IMMEDIATELY
Bryce Lee7ace3952018-02-16 14:34:32 -08004109 || (prevState == PAUSED
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004110 && (mode == FINISH_AFTER_PAUSE || inPinnedWindowingMode()))
Riddle Hsue10cea52018-10-16 23:33:23 +08004111 || finishingInNonFocusedStackOrNoRunning
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07004112 || prevState == STOPPING
4113 || prevState == STOPPED
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004114 || prevState == ActivityState.INITIALIZING) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004115 r.makeFinishingLocked();
Bryce Leef52974c2018-02-14 15:12:01 -08004116 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm:" + reason);
Andrii Kulian7318d632016-07-20 18:59:28 -07004117
Riddle Hsue10cea52018-10-16 23:33:23 +08004118 if (finishingInNonFocusedStackOrNoRunning) {
Andrii Kulian995fa2b2016-07-29 12:55:41 -07004119 // Finishing activity that was in paused state and it was in not currently focused
Riddle Hsue10cea52018-10-16 23:33:23 +08004120 // stack, need to make something visible in its place. Also if the display does not
4121 // have running activity, the configuration may need to be updated for restoring
4122 // original orientation of the display.
Wale Ogunwaled32da472018-11-16 07:19:28 -08004123 mRootActivityContainer.ensureVisibilityAndConfig(next, mDisplayId,
Andrii Kulianf4479ee2018-05-23 17:52:48 -07004124 false /* markFrozenIfConfigChanged */, true /* deferResume */);
Andrii Kulian7318d632016-07-20 18:59:28 -07004125 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004126 if (activityRemoved) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08004127 mRootActivityContainer.resumeFocusedStacksTopActivities();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004128 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004129 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07004130 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
4131 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004132 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004133 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004134
4135 // Need to go through the full pause cycle to get this
4136 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004137 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07004138 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02004139 r.resumeKeyDispatchingLocked();
Wale Ogunwaled32da472018-11-16 07:19:28 -08004140 mRootActivityContainer.resumeFocusedStacksTopActivities();
Andrii Kulian6b321512019-01-23 06:37:00 +00004141 // If activity was not paused at this point - explicitly pause it to start finishing
4142 // process. Finishing will be completed once it reports pause back.
4143 if (r.isState(RESUMED) && mPausingActivity != null) {
4144 startPausingLocked(false /* userLeaving */, false /* uiSleeping */, next /* resuming */,
4145 false /* dontWait */);
4146 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004147 return r;
4148 }
4149
Craig Mautneree36c772014-07-16 14:56:05 -07004150 void finishAllActivitiesLocked(boolean immediately) {
4151 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004152 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4153 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4154 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4155 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07004156 noActivitiesInStack = false;
4157 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004158 continue;
4159 }
Craig Mautneree36c772014-07-16 14:56:05 -07004160 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Bryce Leef52974c2018-02-14 15:12:01 -08004161 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false,
4162 "finishAllActivitiesLocked");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004163 }
4164 }
Craig Mautneree36c772014-07-16 14:56:05 -07004165 if (noActivitiesInStack) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07004166 remove();
Craig Mautneree36c772014-07-16 14:56:05 -07004167 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004168 }
4169
Wale Ogunwale66e16852017-10-19 13:35:52 -07004170 /** @return true if the stack behind this one is a standard activity type. */
4171 boolean inFrontOfStandardStack() {
4172 final ActivityDisplay display = getDisplay();
4173 if (display == null) {
4174 return false;
4175 }
4176 final int index = display.getIndexOf(this);
4177 if (index == 0) {
4178 return false;
4179 }
4180 final ActivityStack stackBehind = display.getChildAt(index - 1);
4181 return stackBehind.isActivityTypeStandard();
4182 }
4183
4184 boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07004185 // Basic case: for simple app-centric recents, we need to recreate
4186 // the task if the affinity has changed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004187 if (srec == null || srec.getTaskRecord().affinity == null ||
4188 !srec.getTaskRecord().affinity.equals(destAffinity)) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07004189 return true;
4190 }
4191 // Document-centric case: an app may be split in to multiple documents;
4192 // they need to re-create their task if this current activity is the root
4193 // of a document, unless simply finishing it will return them to the the
4194 // correct app behind.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004195 final TaskRecord task = srec.getTaskRecord();
Wale Ogunwale66e16852017-10-19 13:35:52 -07004196 if (srec.frontOfTask && task.getBaseIntent() != null && task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07004197 // Okay, this activity is at the root of its task. What to do, what to do...
Wale Ogunwale66e16852017-10-19 13:35:52 -07004198 if (!inFrontOfStandardStack()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07004199 // Finishing won't return to an application, so we need to recreate.
4200 return true;
4201 }
4202 // We now need to get the task below it to determine what to do.
Bryce Leeaf691c02017-03-20 14:20:22 -07004203 int taskIdx = mTaskHistory.indexOf(task);
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07004204 if (taskIdx <= 0) {
4205 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
4206 return false;
4207 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07004208 final TaskRecord prevTask = mTaskHistory.get(taskIdx);
Bryce Leeaf691c02017-03-20 14:20:22 -07004209 if (!task.affinity.equals(prevTask.affinity)) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07004210 // These are different apps, so need to recreate.
4211 return true;
4212 }
4213 }
4214 return false;
4215 }
4216
Wale Ogunwale7d701172015-03-11 15:36:30 -07004217 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004218 Intent resultData) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004219 final TaskRecord task = srec.getTaskRecord();
Craig Mautner0247fc82013-02-28 14:32:06 -08004220 final ArrayList<ActivityRecord> activities = task.mActivities;
4221 final int start = activities.indexOf(srec);
4222 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004223 return false;
4224 }
4225 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08004226 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004227 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08004228 final ComponentName dest = destIntent.getComponent();
4229 if (start > 0 && dest != null) {
4230 for (int i = finishTo; i >= 0; i--) {
4231 ActivityRecord r = activities.get(i);
4232 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004233 r.info.name.equals(dest.getClassName())) {
4234 finishTo = i;
4235 parent = r;
4236 foundParentInTask = true;
4237 break;
4238 }
4239 }
4240 }
4241
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004242 // TODO: There is a dup. of this block of code in ActivityTaskManagerService.finishActivity
4243 // We should consolidate.
4244 IActivityController controller = mService.mController;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004245 if (controller != null) {
4246 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
4247 if (next != null) {
4248 // ask watcher if this is allowed
4249 boolean resumeOK = true;
4250 try {
4251 resumeOK = controller.activityResuming(next.packageName);
4252 } catch (RemoteException e) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004253 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004254 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004255 }
4256
4257 if (!resumeOK) {
4258 return false;
4259 }
4260 }
4261 }
4262 final long origId = Binder.clearCallingIdentity();
4263 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08004264 ActivityRecord r = activities.get(i);
4265 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004266 // Only return the supplied result for the first activity finished
4267 resultCode = Activity.RESULT_CANCELED;
4268 resultData = null;
4269 }
4270
4271 if (parent != null && foundParentInTask) {
4272 final int parentLaunchMode = parent.info.launchMode;
4273 final int destIntentFlags = destIntent.getFlags();
4274 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
4275 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
4276 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
4277 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004278 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
4279 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004280 } else {
4281 try {
4282 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
Philip P. Moltmanncff8f0f2018-03-27 12:51:51 -07004283 destIntent.getComponent(), ActivityManagerService.STOCK_PM_FLAGS,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004284 srec.mUserId);
Bryce Lee4c9a5972017-12-01 22:14:24 -08004285 // TODO(b/64750076): Check if calling pid should really be -1.
4286 final int res = mService.getActivityStartController()
4287 .obtainStarter(destIntent, "navigateUpTo")
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07004288 .setCaller(srec.app.getThread())
Bryce Lee4c9a5972017-12-01 22:14:24 -08004289 .setActivityInfo(aInfo)
4290 .setResultTo(parent.appToken)
4291 .setCallingPid(-1)
4292 .setCallingUid(parent.launchedFromUid)
4293 .setCallingPackage(parent.launchedFromPackage)
4294 .setRealCallingPid(-1)
4295 .setRealCallingUid(parent.launchedFromUid)
4296 .setComponentSpecified(true)
4297 .execute();
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004298 foundParentInTask = res == ActivityManager.START_SUCCESS;
4299 } catch (RemoteException e) {
4300 foundParentInTask = false;
4301 }
4302 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08004303 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004304 }
4305 }
4306 Binder.restoreCallingIdentity(origId);
4307 return foundParentInTask;
4308 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004309
4310 /**
4311 * Remove any state associated with the {@link ActivityRecord}. This should be called whenever
4312 * an activity moves away from the stack.
4313 */
4314 void onActivityRemovedFromStack(ActivityRecord r) {
Bryce Lee84730a02018-04-03 14:10:04 -07004315 removeTimeoutsForActivityLocked(r);
4316
4317 if (mResumedActivity != null && mResumedActivity == r) {
4318 setResumedActivity(null, "onActivityRemovedFromStack");
Bryce Leeaf691c02017-03-20 14:20:22 -07004319 }
Bryce Lee84730a02018-04-03 14:10:04 -07004320 if (mPausingActivity != null && mPausingActivity == r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07004321 mPausingActivity = null;
4322 }
Bryce Lee84730a02018-04-03 14:10:04 -07004323 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004324
Bryce Lee84730a02018-04-03 14:10:04 -07004325 void onActivityAddedToStack(ActivityRecord r) {
4326 if(r.getState() == RESUMED) {
4327 setResumedActivity(r, "onActivityAddedToStack");
4328 }
Bryce Leeaf691c02017-03-20 14:20:22 -07004329 }
4330
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004331 /**
4332 * Perform the common clean-up of an activity record. This is called both
4333 * as part of destroyActivityLocked() (when destroying the client-side
4334 * representation) and cleaning things up as a result of its hosting
4335 * processing going away, in which case there is no remaining client-side
4336 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08004337 *
4338 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004339 */
Bryce Leec9406602018-03-09 11:02:10 -08004340 private void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices, boolean setState) {
Bryce Leeaf691c02017-03-20 14:20:22 -07004341 onActivityRemovedFromStack(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004342
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004343 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004344 r.frozenBeforeDestroy = false;
4345
Bryce Leec9406602018-03-09 11:02:10 -08004346 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004347 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004348 r.setState(DESTROYED, "cleanupActivityLocked");
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004349 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004350 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004351 }
4352
Bryce Lee4a194382017-04-04 14:32:48 -07004353 // Inform supervisor the activity has been removed.
4354 mStackSupervisor.cleanupActivity(r);
4355
Craig Mautner2420ead2013-04-01 17:13:20 -07004356
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004357 // Remove any pending results.
4358 if (r.finishing && r.pendingResults != null) {
4359 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
4360 PendingIntentRecord rec = apr.get();
4361 if (rec != null) {
Wale Ogunwaleee6eca12018-09-19 20:37:53 -07004362 mService.mPendingIntentController.cancelIntentSender(rec, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004363 }
4364 }
4365 r.pendingResults = null;
4366 }
4367
4368 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07004369 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004370 }
4371
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004372 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004373 removeTimeoutsForActivityLocked(r);
Wale Ogunwale8fd75422016-06-24 14:20:37 -07004374 // Clean-up activities are no longer relaunching (e.g. app process died). Notify window
4375 // manager so it can update its bookkeeping.
4376 mWindowManager.notifyAppRelaunchesCleared(r.appToken);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004377 }
4378
Wale Ogunwale53783742018-09-16 10:21:51 -07004379 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004380 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004381 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004382 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004383 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07004384 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004385 }
4386
Chong Zhangbffd8892016-08-08 11:16:06 -07004387 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004388 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07004389 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004390 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
4391 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
4392
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004393 r.takeFromHistory();
4394 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004395 if (DEBUG_STATES) Slog.v(TAG_STATES,
4396 "Moving to DESTROYED: " + r + " (removed from history)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004397 r.setState(DESTROYED, "removeActivityFromHistoryLocked");
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004398 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004399 r.app = null;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08004400 r.removeWindowContainer();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004401 final TaskRecord task = r.getTaskRecord();
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004402 final boolean lastActivity = task != null ? task.removeActivity(r) : false;
Winson Chung6954fc92017-03-24 16:22:12 -07004403 // If we are removing the last activity in the task, not including task overlay activities,
4404 // then fall through into the block below to remove the entire task itself
4405 final boolean onlyHasTaskOverlays = task != null
4406 ? task.onlyHasTaskOverlayActivities(false /* excludingFinishing */) : false;
Bryce Leeaa5e8c32017-03-01 16:01:06 -08004407
Winson Chung6954fc92017-03-24 16:22:12 -07004408 if (lastActivity || onlyHasTaskOverlays) {
4409 if (DEBUG_STACK) {
4410 Slog.i(TAG_STACK,
4411 "removeActivityFromHistoryLocked: last activity removed from " + this
4412 + " onlyHasTaskOverlays=" + onlyHasTaskOverlays);
4413 }
4414
Bryce Leed6d26752017-05-25 13:57:46 -07004415 // The following block can be executed multiple times if there is more than one overlay.
4416 // {@link ActivityStackSupervisor#removeTaskByIdLocked} handles this by reverse lookup
4417 // of the task by id and exiting early if not found.
Winson Chunge3c21e02017-04-11 18:02:17 -07004418 if (onlyHasTaskOverlays) {
4419 // When destroying a task, tell the supervisor to remove it so that any activity it
4420 // has can be cleaned up correctly. This is currently the only place where we remove
4421 // a task with the DESTROYING mode, so instead of passing the onlyHasTaskOverlays
4422 // state into removeTask(), we just clear the task here before the other residual
4423 // work.
4424 // TODO: If the callers to removeTask() changes such that we have multiple places
4425 // where we are destroying the task, move this back into removeTask()
4426 mStackSupervisor.removeTaskByIdLocked(task.taskId, false /* killProcess */,
Winson Chung0ec2a352017-10-26 11:38:30 -07004427 !REMOVE_FROM_RECENTS, PAUSE_IMMEDIATELY, reason);
Winson Chunge3c21e02017-04-11 18:02:17 -07004428 }
Bryce Lee1d930582017-05-01 08:35:24 -07004429
Bryce Leed6d26752017-05-25 13:57:46 -07004430 // We must keep the task around until all activities are destroyed. The following
4431 // statement will only execute once since overlays are also considered activities.
Bryce Lee1d930582017-05-01 08:35:24 -07004432 if (lastActivity) {
4433 removeTask(task, reason, REMOVE_TASK_MODE_DESTROYING);
4434 }
Craig Mautner312ba862014-02-10 17:55:01 -08004435 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004436 cleanUpActivityServicesLocked(r);
4437 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004438 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004439
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004440 /**
4441 * Perform clean-up of service connections in an activity record.
4442 */
Andrii Kulian21713ac2016-10-12 22:05:05 -07004443 private void cleanUpActivityServicesLocked(ActivityRecord r) {
Wale Ogunwalec4e63a42018-10-02 13:19:54 -07004444 if (r.mServiceConnectionsHolder == null) {
4445 return;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004446 }
Wale Ogunwalec4e63a42018-10-02 13:19:54 -07004447 // Throw away any services that have been bound by this activity.
4448 r.mServiceConnectionsHolder.disconnectActivityFromServices();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004449 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004450
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07004451 final void scheduleDestroyActivities(WindowProcessController owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004452 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004453 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004454 mHandler.sendMessage(msg);
4455 }
4456
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07004457 private void destroyActivitiesLocked(WindowProcessController owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07004458 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004459 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004460 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4461 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4462 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4463 final ActivityRecord r = activities.get(activityNdx);
4464 if (r.finishing) {
4465 continue;
4466 }
4467 if (r.fullscreen) {
4468 lastIsOpaque = true;
4469 }
4470 if (owner != null && r.app != owner) {
4471 continue;
4472 }
4473 if (!lastIsOpaque) {
4474 continue;
4475 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004476 if (r.isDestroyable()) {
Bryce Lee7ace3952018-02-16 14:34:32 -08004477 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r
4478 + " in state " + r.getState()
Craig Mautnerd44711d2013-02-23 11:24:36 -08004479 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004480 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004481 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004482 activityRemoved = true;
4483 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004484 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004485 }
4486 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004487 if (activityRemoved) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08004488 mRootActivityContainer.resumeFocusedStacksTopActivities();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004489 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004490 }
4491
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004492 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
4493 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004494 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Bryce Lee7ace3952018-02-16 14:34:32 -08004495 "Destroying " + r + " in state " + r.getState() + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004496 + " pausing=" + mPausingActivity + " for reason " + reason);
4497 return destroyActivityLocked(r, true, reason);
4498 }
4499 return false;
4500 }
4501
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07004502 final int releaseSomeActivitiesLocked(WindowProcessController app, ArraySet<TaskRecord> tasks,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004503 String reason) {
4504 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004505 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004506 int maxTasks = tasks.size() / 4;
4507 if (maxTasks < 1) {
4508 maxTasks = 1;
4509 }
4510 int numReleased = 0;
4511 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
4512 final TaskRecord task = mTaskHistory.get(taskNdx);
4513 if (!tasks.contains(task)) {
4514 continue;
4515 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004516 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004517 int curNum = 0;
4518 final ArrayList<ActivityRecord> activities = task.mActivities;
4519 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
4520 final ActivityRecord activity = activities.get(actNdx);
4521 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004522 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Bryce Lee7ace3952018-02-16 14:34:32 -08004523 + " in state " + activity.getState() + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004524 + " pausing=" + mPausingActivity + " for reason " + reason);
4525 destroyActivityLocked(activity, true, reason);
4526 if (activities.get(actNdx) != activity) {
4527 // Was removed from list, back up so we don't miss the next one.
4528 actNdx--;
4529 }
4530 curNum++;
4531 }
4532 }
4533 if (curNum > 0) {
4534 numReleased += curNum;
4535 maxTasks--;
4536 if (mTaskHistory.get(taskNdx) != task) {
4537 // The entire task got removed, back up so we don't miss the next one.
4538 taskNdx--;
4539 }
4540 }
4541 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004542 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
4543 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004544 return numReleased;
4545 }
4546
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004547 /**
4548 * Destroy the current CLIENT SIDE instance of an activity. This may be
4549 * called both when actually finishing an activity, or when performing
4550 * a configuration switch where we destroy the current client-side object
4551 * but then create a new client-side object for this same HistoryRecord.
4552 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07004553 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004554 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
4555 "Removing activity from " + reason + ": token=" + r
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07004556 + ", app=" + (r.hasProcess() ? r.app.mName : "(null)"));
Bryce Leeb0f993f2018-03-02 15:38:01 -08004557
4558 if (r.isState(DESTROYING, DESTROYED)) {
Bryce Leec9406602018-03-09 11:02:10 -08004559 if (DEBUG_STATES) Slog.v(TAG_STATES, "activity " + r + " already destroying."
Bryce Leeb0f993f2018-03-02 15:38:01 -08004560 + "skipping request with reason:" + reason);
4561 return false;
4562 }
4563
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004564 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004565 r.mUserId, System.identityHashCode(r),
4566 r.getTaskRecord().taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004567
4568 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004569
Bryce Leec9406602018-03-09 11:02:10 -08004570 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004571
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07004572 final boolean hadApp = r.hasProcess();
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004573
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004574 if (hadApp) {
4575 if (removeFromApp) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07004576 r.app.removeActivity(r);
Wale Ogunwale53783742018-09-16 10:21:51 -07004577 if (!r.app.hasActivities()) {
4578 mService.clearHeavyWeightProcessIfEquals(r.app);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004579 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07004580 if (!r.app.hasActivities()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07004581 // Update any services we are bound to that might care about whether
4582 // their client may have activities.
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07004583 // No longer have activities, so update LRU list and oom adj.
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07004584 r.app.updateProcessInfo(true, true, false, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004585 }
4586 }
4587
4588 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004589
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004590 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004591 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07004592 mService.getLifecycleManager().scheduleTransaction(r.app.getThread(), r.appToken,
Bryce Lee1d0d5142018-04-12 10:35:07 -07004593 DestroyActivityItem.obtain(r.finishing, r.configChangeFlags));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004594 } catch (Exception e) {
4595 // We can just ignore exceptions here... if the process
4596 // has crashed, our death notification will clean things
4597 // up.
4598 //Slog.w(TAG, "Exception thrown during finish", e);
4599 if (r.finishing) {
Chong Zhangbffd8892016-08-08 11:16:06 -07004600 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004601 removedFromHistory = true;
4602 skipDestroy = true;
4603 }
4604 }
4605
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004606 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004607
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004608 // If the activity is finishing, we need to wait on removing it
4609 // from the list to give it a chance to do its cleanup. During
4610 // that time it may make calls back with its token so we need to
4611 // be able to find it on the list and so we don't want to remove
4612 // it from the list yet. Otherwise, we can just immediately put
4613 // it in the destroyed state since we are not removing it from the
4614 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004615 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004616 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004617 + " (destroy requested)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004618 r.setState(DESTROYING,
4619 "destroyActivityLocked. finishing and not skipping destroy");
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07004620 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004621 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
4622 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004623 if (DEBUG_STATES) Slog.v(TAG_STATES,
4624 "Moving to DESTROYED: " + r + " (destroy skipped)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004625 r.setState(DESTROYED,
4626 "destroyActivityLocked. not finishing or skipping destroy");
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004627 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004628 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004629 }
4630 } else {
4631 // remove this record from the history.
4632 if (r.finishing) {
Chong Zhangbffd8892016-08-08 11:16:06 -07004633 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004634 removedFromHistory = true;
4635 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004636 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Bryce Lee7ace3952018-02-16 14:34:32 -08004637 r.setState(DESTROYED, "destroyActivityLocked. not finishing and had no app");
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004638 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004639 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004640 }
4641 }
4642
4643 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004644
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004645 if (!mLRUActivities.remove(r) && hadApp) {
4646 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
4647 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07004648
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004649 return removedFromHistory;
4650 }
4651
Craig Mautner299f9602015-01-26 09:47:33 -08004652 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004653 final long origId = Binder.clearCallingIdentity();
4654 try {
Bryce Lee7ace3952018-02-16 14:34:32 -08004655 activityDestroyedLocked(ActivityRecord.forTokenLocked(token), reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08004656 } finally {
4657 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004658 }
4659 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004660
Bryce Leec9406602018-03-09 11:02:10 -08004661 /**
4662 * This method is to only be called from the client via binder when the activity is destroyed
4663 * AND finished.
4664 */
Bryce Lee7ace3952018-02-16 14:34:32 -08004665 final void activityDestroyedLocked(ActivityRecord record, String reason) {
4666 if (record != null) {
4667 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, record);
4668 }
4669
4670 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + record);
4671
4672 if (isInStackLocked(record) != null) {
4673 if (record.isState(DESTROYING, DESTROYED)) {
Bryce Leec9406602018-03-09 11:02:10 -08004674 cleanUpActivityLocked(record, true, false);
Bryce Lee7ace3952018-02-16 14:34:32 -08004675 removeActivityFromHistoryLocked(record, reason);
4676 }
4677 }
4678
Wale Ogunwaled32da472018-11-16 07:19:28 -08004679 mRootActivityContainer.resumeFocusedStacksTopActivities();
Bryce Lee7ace3952018-02-16 14:34:32 -08004680 }
4681
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004682 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07004683 WindowProcessController app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004684 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004685 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4686 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004687 while (i > 0) {
4688 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08004689 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004690 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004691 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004692 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004693 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07004694 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004695 }
4696 }
4697 }
4698
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07004699 private boolean removeHistoryRecordsForAppLocked(WindowProcessController app) {
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004700 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004701 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
4702 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07004703 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
4704 "mGoingToSleepActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07004705 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
4706 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004707
Riddle Hsuaaef7312019-01-24 19:00:58 +08004708 final boolean isProcessRemoved = app.isRemoved();
4709 if (isProcessRemoved) {
4710 // The package of the died process should be force-stopped, so make its activities as
4711 // finishing to prevent the process from being started again if the next top (or being
4712 // visible) activity also resides in the same process.
4713 app.makeFinishingForProcessRemoved();
4714 }
4715
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004716 boolean hasVisibleActivities = false;
4717
4718 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08004719 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004720 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4721 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08004722 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4723 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Bryce Leefbd263b42018-03-07 10:33:55 -08004724 mTmpActivities.clear();
4725 mTmpActivities.addAll(activities);
4726
4727 while (!mTmpActivities.isEmpty()) {
4728 final int targetIndex = mTmpActivities.size() - 1;
4729 final ActivityRecord r = mTmpActivities.remove(targetIndex);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004730 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
Bryce Leefbd263b42018-03-07 10:33:55 -08004731 "Record #" + targetIndex + " " + r + ": app=" + r.app);
4732
Craig Mautner0247fc82013-02-28 14:32:06 -08004733 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08004734 if (r.visible) {
4735 hasVisibleActivities = true;
4736 }
Craig Mautneracebdc82015-02-24 10:53:03 -08004737 final boolean remove;
Garfield Tan2746ab52018-07-25 12:33:01 -07004738 if ((r.mRelaunchReason == RELAUNCH_REASON_WINDOWING_MODE_RESIZE
4739 || r.mRelaunchReason == RELAUNCH_REASON_FREE_RESIZE)
4740 && r.launchCount < 3 && !r.finishing) {
4741 // If the process crashed during a resize, always try to relaunch it, unless
4742 // it has failed more than twice. Skip activities that's already finishing
4743 // cleanly by itself.
4744 remove = false;
Riddle Hsu7b766fd2019-01-28 21:14:59 +08004745 } else if ((!r.haveState && !r.stateNotNeeded
4746 && !r.isState(ActivityState.RESTARTING_PROCESS)) || r.finishing) {
Craig Mautner0247fc82013-02-28 14:32:06 -08004747 // Don't currently have state for the activity, or
4748 // it is finishing -- always remove it.
4749 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004750 } else if (!r.visible && r.launchCount > 2 &&
4751 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08004752 // We have launched this activity too many times since it was
4753 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08004754 // (Note if the activity is visible, we don't remove the record.
4755 // We leave the dead window on the screen but the process will
4756 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08004757 remove = true;
4758 } else {
4759 // The process may be gone, but the activity lives on!
4760 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004761 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004762 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004763 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
4764 "Removing activity " + r + " from stack at " + i
4765 + ": haveState=" + r.haveState
4766 + " stateNotNeeded=" + r.stateNotNeeded
4767 + " finishing=" + r.finishing
Bryce Lee7ace3952018-02-16 14:34:32 -08004768 + " state=" + r.getState() + " callers=" + Debug.getCallers(5));
Riddle Hsuaaef7312019-01-24 19:00:58 +08004769 if (!r.finishing || isProcessRemoved) {
Craig Mautner0247fc82013-02-28 14:32:06 -08004770 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
4771 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004772 r.mUserId, System.identityHashCode(r),
4773 r.getTaskRecord().taskId, r.shortComponentName,
Craig Mautner0247fc82013-02-28 14:32:06 -08004774 "proc died without state saved");
4775 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004776 } else {
4777 // We have the current state for this activity, so
4778 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004779 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
4780 if (DEBUG_APP) Slog.v(TAG_APP,
4781 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08004782 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004783 // Set nowVisible to previous visible state. If the app was visible while
4784 // it died, we leave the dead window on screen so it's basically visible.
4785 // This is needed when user later tap on the dead window, we need to stop
4786 // other apps when user transfers focus to the restarted activity.
4787 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08004788 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004789 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08004790 "App died, clearing saved state of " + r);
4791 r.icicle = null;
4792 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004793 }
Bryce Leec9406602018-03-09 11:02:10 -08004794 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08004795 if (remove) {
Chong Zhangbffd8892016-08-08 11:16:06 -07004796 removeActivityFromHistoryLocked(r, "appDied");
Craig Mautneracebdc82015-02-24 10:53:03 -08004797 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004798 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004799 }
4800 }
4801
4802 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004803 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004804
chaviw0d562bf2018-03-15 14:24:14 -07004805 private void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004806 if (options != null) {
chaviw0d562bf2018-03-15 14:24:14 -07004807 ActivityRecord r = topRunningActivityLocked();
4808 if (r != null && !r.isState(RESUMED)) {
4809 r.updateOptionsLocked(options);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004810 } else {
4811 ActivityOptions.abort(options);
4812 }
4813 }
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004814 getDisplay().mDisplayContent.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004815 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004816
Andrii Kulian21713ac2016-10-12 22:05:05 -07004817 private void updateTaskMovement(TaskRecord task, boolean toFront) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004818 if (task.isPersistable) {
4819 task.mLastTimeMoved = System.currentTimeMillis();
4820 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
4821 // recently will be most negative, tasks sent to the bottom before that will be less
4822 // negative. Similarly for recent tasks moved to the top which will be most positive.
4823 if (!toFront) {
4824 task.mLastTimeMoved *= -1;
4825 }
4826 }
Wale Ogunwaled32da472018-11-16 07:19:28 -08004827 mRootActivityContainer.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07004828 }
4829
chaviw0d562bf2018-03-15 14:24:14 -07004830 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
4831 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004832 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004833
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004834 final ActivityStack topStack = getDisplay().getTopStack();
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004835 final ActivityRecord topActivity = topStack != null ? topStack.getTopActivity() : null;
Craig Mautner11bf9a52013-02-19 14:08:51 -08004836 final int numTasks = mTaskHistory.size();
4837 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07004838 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004839 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004840 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004841 ActivityOptions.abort(options);
4842 } else {
chaviw0d562bf2018-03-15 14:24:14 -07004843 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004844 }
4845 return;
4846 }
4847
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004848 if (timeTracker != null) {
4849 // The caller wants a time tracker associated with this task.
4850 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4851 tr.mActivities.get(i).appTimeTracker = timeTracker;
4852 }
4853 }
4854
Chavi Weingarten3a748552018-05-14 17:32:42 +00004855 try {
4856 // Defer updating the IME target since the new IME target will try to get computed
4857 // before updating all closing and opening apps, which can cause the ime target to
4858 // get calculated incorrectly.
4859 getDisplay().deferUpdateImeTarget();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004860
Chavi Weingarten3a748552018-05-14 17:32:42 +00004861 // Shift all activities with this task up to the top
4862 // of the stack, keeping them in the same internal order.
4863 insertTaskAtTop(tr, null);
4864
4865 // Don't refocus if invisible to current user
4866 final ActivityRecord top = tr.getTopActivity();
4867 if (top == null || !top.okToShowLocked()) {
4868 if (top != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004869 mStackSupervisor.mRecentTasks.add(top.getTaskRecord());
Chavi Weingarten3a748552018-05-14 17:32:42 +00004870 }
4871 ActivityOptions.abort(options);
4872 return;
Winson Chung1dbc8112017-09-28 18:05:31 -07004873 }
Chavi Weingarten9e875b62018-05-14 16:10:59 +00004874
Chavi Weingarten3a748552018-05-14 17:32:42 +00004875 // Set focus to the top running activity of this stack.
4876 final ActivityRecord r = topRunningActivityLocked();
Louis Chang19443452018-10-09 12:10:21 +08004877 if (r != null) {
4878 r.moveFocusableActivityToTop(reason);
4879 }
Chavi Weingarten9e875b62018-05-14 16:10:59 +00004880
Chavi Weingarten3a748552018-05-14 17:32:42 +00004881 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
4882 if (noAnimation) {
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004883 getDisplay().mDisplayContent.prepareAppTransition(TRANSIT_NONE, false);
Chavi Weingarten3a748552018-05-14 17:32:42 +00004884 if (r != null) {
4885 mStackSupervisor.mNoAnimActivities.add(r);
4886 }
4887 ActivityOptions.abort(options);
4888 } else {
4889 updateTransitLocked(TRANSIT_TASK_TO_FRONT, options);
Chavi Weingarten9e875b62018-05-14 16:10:59 +00004890 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004891 // If a new task is moved to the front, then mark the existing top activity as
4892 // supporting
Chavi Weingarten9e875b62018-05-14 16:10:59 +00004893
Chavi Weingarten3a748552018-05-14 17:32:42 +00004894 // picture-in-picture while paused only if the task would not be considered an oerlay
4895 // on top
4896 // of the current activity (eg. not fullscreen, or the assistant)
4897 if (canEnterPipOnTaskSwitch(topActivity, tr, null /* toFrontActivity */,
4898 options)) {
4899 topActivity.supportsEnterPipOnTaskSwitch = true;
4900 }
Chavi Weingarten9e875b62018-05-14 16:10:59 +00004901
Wale Ogunwaled32da472018-11-16 07:19:28 -08004902 mRootActivityContainer.resumeFocusedStacksTopActivities();
Chavi Weingarten3a748552018-05-14 17:32:42 +00004903 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Mark Renoufc808f062019-02-07 15:20:37 -05004904 mService.getTaskChangeNotificationController().notifyTaskMovedToFront(tr.getTaskInfo());
Chavi Weingarten3a748552018-05-14 17:32:42 +00004905 } finally {
4906 getDisplay().continueUpdateImeTarget();
4907 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004908 }
4909
4910 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004911 * Worker method for rearranging history stack. Implements the function of moving all
4912 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004913 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004914 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004915 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4916 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004917 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004918 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004919 * @return Returns true if the move completed, false if not.
4920 */
Craig Mautner299f9602015-01-26 09:47:33 -08004921 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004922 final TaskRecord tr = taskForIdLocked(taskId);
4923 if (tr == null) {
4924 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4925 return false;
4926 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004927 Slog.i(TAG, "moveTaskToBack: " + tr);
Winson Chung261c0f32017-03-07 16:54:31 -08004928
Charles Heff9b4dff2017-09-22 10:18:37 +01004929 // In LockTask mode, moving a locked task to the back of the stack may expose unlocked
4930 // ones. Therefore we need to check if this operation is allowed.
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -07004931 if (!mService.getLockTaskController().canMoveTaskToBack(tr)) {
Winson Chung261c0f32017-03-07 16:54:31 -08004932 return false;
4933 }
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004934
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004935 // If we have a watcher, preflight the move before committing to it. First check
4936 // for *other* available tasks, but if none are available, then try again allowing the
4937 // current task to be selected.
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004938 if (isTopStackOnDisplay() && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004939 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004940 if (next == null) {
4941 next = topRunningActivityLocked(null, 0);
4942 }
4943 if (next != null) {
4944 // ask watcher if this is allowed
4945 boolean moveOK = true;
4946 try {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004947 moveOK = mService.mController.activityResuming(next.packageName);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004948 } catch (RemoteException e) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07004949 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004950 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004951 }
4952 if (!moveOK) {
4953 return false;
4954 }
4955 }
4956 }
4957
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004958 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004959
Wale Ogunwale66e16852017-10-19 13:35:52 -07004960 mTaskHistory.remove(tr);
4961 mTaskHistory.add(0, tr);
4962 updateTaskMovement(tr, false);
Wale Ogunwale42709242015-08-11 13:54:42 -07004963
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004964 getDisplay().mDisplayContent.prepareAppTransition(TRANSIT_TASK_TO_BACK, false);
Evan Rosky9c448172017-11-02 14:19:27 -07004965 moveToBack("moveTaskToBackLocked", tr);
Winson Chung261c0f32017-03-07 16:54:31 -08004966
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004967 if (inPinnedWindowingMode()) {
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07004968 mStackSupervisor.removeStack(this);
Winson Chung261c0f32017-03-07 16:54:31 -08004969 return true;
4970 }
4971
Wale Ogunwaled32da472018-11-16 07:19:28 -08004972 mRootActivityContainer.resumeFocusedStacksTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004973 return true;
4974 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004975
Andrii Kulian21713ac2016-10-12 22:05:05 -07004976 static void logStartActivity(int tag, ActivityRecord r, TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004977 final Uri data = r.intent.getData();
4978 final String strData = data != null ? data.toSafeString() : null;
4979
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004980 EventLog.writeEvent(tag,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004981 r.mUserId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004982 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004983 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004984 }
4985
4986 /**
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004987 * Ensures all visible activities at or below the input activity have the right configuration.
4988 */
4989 void ensureVisibleActivitiesConfigurationLocked(ActivityRecord start, boolean preserveWindow) {
4990 if (start == null || !start.visible) {
4991 return;
4992 }
4993
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004994 final TaskRecord startTask = start.getTaskRecord();
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07004995 boolean behindFullscreen = false;
4996 boolean updatedConfig = false;
4997
4998 for (int taskIndex = mTaskHistory.indexOf(startTask); taskIndex >= 0; --taskIndex) {
4999 final TaskRecord task = mTaskHistory.get(taskIndex);
5000 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08005001 int activityIndex = (start.getTaskRecord() == task)
5002 ? activities.indexOf(start) : activities.size() - 1;
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07005003 for (; activityIndex >= 0; --activityIndex) {
5004 final ActivityRecord r = activities.get(activityIndex);
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08005005 updatedConfig |= r.ensureActivityConfiguration(0 /* globalChanges */,
Andrii Kulian21713ac2016-10-12 22:05:05 -07005006 preserveWindow);
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07005007 if (r.fullscreen) {
5008 behindFullscreen = true;
5009 break;
5010 }
5011 }
5012 if (behindFullscreen) {
5013 break;
5014 }
5015 }
5016 if (updatedConfig) {
Wale Ogunwale089586f2016-06-20 14:16:22 -07005017 // Ensure the resumed state of the focus activity if we updated the configuration of
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07005018 // any activity.
Wale Ogunwaled32da472018-11-16 07:19:28 -08005019 mRootActivityContainer.resumeFocusedStacksTopActivities();
Wale Ogunwaledb0fa122016-05-16 15:12:03 -07005020 }
5021 }
5022
Wale Ogunwale1666e312016-12-16 11:27:18 -08005023 // TODO: Figure-out a way to consolidate with resize() method below.
Yunfan Chen279f5582018-12-12 15:24:50 -08005024 void requestResize(Rect bounds) {
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -07005025 mService.resizeStack(mStackId, bounds,
Wale Ogunwale65ebd952018-04-25 15:41:44 -07005026 true /* allowResizeInDockedMode */, false /* preserveWindows */,
5027 false /* animate */, -1 /* animationDuration */);
Wale Ogunwale1666e312016-12-16 11:27:18 -08005028 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07005029
Wale Ogunwale1666e312016-12-16 11:27:18 -08005030 // TODO: Can only be called from special methods in ActivityStackSupervisor.
5031 // Need to consolidate those calls points into this resize method so anyone can call directly.
5032 void resize(Rect bounds, Rect tempTaskBounds, Rect tempTaskInsetBounds) {
Evan Rosky1ac84462018-11-13 11:25:30 -08005033 if (!updateBoundsAllowed(bounds)) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08005034 return;
5035 }
5036
5037 // Update override configurations of all tasks in the stack.
5038 final Rect taskBounds = tempTaskBounds != null ? tempTaskBounds : bounds;
Andrii Kulian1e32e022016-09-16 15:29:34 -07005039
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08005040 for (int i = mTaskHistory.size() - 1; i >= 0; i--) {
5041 final TaskRecord task = mTaskHistory.get(i);
5042 if (task.isResizeable()) {
Garfield Tan020607d2018-12-17 17:01:58 -08005043 task.updateOverrideConfiguration(taskBounds, tempTaskInsetBounds);
Andrii Kulian1e32e022016-09-16 15:29:34 -07005044 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07005045 }
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08005046
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08005047 setBounds(bounds);
Andrii Kulian1e32e022016-09-16 15:29:34 -07005048 }
5049
Winson Chung15036ca2018-05-31 15:51:47 -07005050 void onPipAnimationEndResize() {
Yunfan Chen279f5582018-12-12 15:24:50 -08005051 if (mTaskStack == null) return;
5052 mTaskStack.onPipAnimationEndResize();
Winson Chung15036ca2018-05-31 15:51:47 -07005053 }
5054
Andrii Kulian1e32e022016-09-16 15:29:34 -07005055
5056 /**
Evan Rosky1ac84462018-11-13 11:25:30 -08005057 * Until we can break this "set task bounds to same as stack bounds" behavior, this
5058 * basically resizes both stack and task bounds to the same bounds.
Andrii Kulian1e32e022016-09-16 15:29:34 -07005059 */
Evan Rosky1ac84462018-11-13 11:25:30 -08005060 void setTaskBounds(Rect bounds) {
5061 if (!updateBoundsAllowed(bounds)) {
Andrii Kulian1e32e022016-09-16 15:29:34 -07005062 return;
5063 }
5064
Evan Rosky1ac84462018-11-13 11:25:30 -08005065 for (int i = mTaskHistory.size() - 1; i >= 0; i--) {
5066 final TaskRecord task = mTaskHistory.get(i);
5067 if (task.isResizeable()) {
5068 task.setBounds(bounds);
5069 } else {
5070 task.setBounds(null);
Andrii Kulian1e32e022016-09-16 15:29:34 -07005071 }
Evan Rosky1ac84462018-11-13 11:25:30 -08005072 }
5073 }
5074
5075 /** Helper to setDisplayedBounds on all child tasks */
5076 void setTaskDisplayedBounds(Rect bounds) {
5077 if (!updateDisplayedBoundsAllowed(bounds)) {
5078 return;
Andrii Kulian1e32e022016-09-16 15:29:34 -07005079 }
5080
Evan Rosky1ac84462018-11-13 11:25:30 -08005081 for (int i = mTaskHistory.size() - 1; i >= 0; i--) {
5082 final TaskRecord task = mTaskHistory.get(i);
5083 if (bounds == null || bounds.isEmpty()) {
5084 task.setDisplayedBounds(null);
5085 } else if (task.isResizeable()) {
5086 task.setDisplayedBounds(bounds);
Andrii Kulian1e32e022016-09-16 15:29:34 -07005087 }
Andrii Kulian1e32e022016-09-16 15:29:34 -07005088 }
5089 }
5090
Craig Mautnercae015f2013-02-08 14:31:27 -08005091 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08005092 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5093 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
5094 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
5095 final ActivityRecord r = activities.get(activityNdx);
5096 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08005097 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08005098 }
5099 if (r.fullscreen && !r.finishing) {
5100 return false;
5101 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005102 }
5103 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07005104 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08005105 if (r == null) {
5106 return false;
5107 }
5108 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
5109 + " would have returned true for r=" + r);
5110 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08005111 }
5112
5113 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08005114 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5115 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
5116 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
5117 final ActivityRecord r = activities.get(activityNdx);
5118 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08005119 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08005120 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005121 }
5122 }
5123 }
5124
Wale Ogunwale540e1232015-05-01 15:35:39 -07005125 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
5126 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08005127 boolean didSomething = false;
5128 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00005129 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08005130 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5131 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Bryce Leefbd263b42018-03-07 10:33:55 -08005132 mTmpActivities.clear();
5133 mTmpActivities.addAll(activities);
5134
5135 while (!mTmpActivities.isEmpty()) {
5136 ActivityRecord r = mTmpActivities.remove(0);
Wale Ogunwale540e1232015-05-01 15:35:39 -07005137 final boolean sameComponent =
5138 (r.packageName.equals(packageName) && (filterByClasses == null
Wale Ogunwale8b19de92018-11-29 19:58:26 -08005139 || filterByClasses.contains(r.mActivityComponent.getClassName())))
5140 || (packageName == null && r.mUserId == userId);
5141 if ((userId == UserHandle.USER_ALL || r.mUserId == userId)
5142 && (sameComponent || r.getTaskRecord() == lastTask)
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07005143 && (r.app == null || evenPersistent || !r.app.isPersistent())) {
Craig Mautner56f52db2013-02-25 10:03:01 -08005144 if (!doit) {
5145 if (r.finishing) {
5146 // If this activity is just finishing, then it is not
5147 // interesting as far as something to stop.
5148 continue;
5149 }
5150 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08005151 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07005152 if (r.isActivityTypeHome()) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08005153 if (homeActivity != null && homeActivity.equals(r.mActivityComponent)) {
Craig Mautner9d8a30d2014-07-07 17:26:20 +00005154 Slog.i(TAG, "Skip force-stop again " + r);
5155 continue;
5156 } else {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08005157 homeActivity = r.mActivityComponent;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00005158 }
5159 }
Craig Mautner56f52db2013-02-25 10:03:01 -08005160 didSomething = true;
5161 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08005162 lastTask = r.getTaskRecord();
Bryce Leefbd263b42018-03-07 10:33:55 -08005163 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
5164 true);
Craig Mautnercae015f2013-02-08 14:31:27 -08005165 }
5166 }
5167 }
5168 return didSomething;
5169 }
5170
Winson Chung61c9e5a2017-10-11 10:39:32 -07005171 /**
5172 * @return The set of running tasks through {@param tasksOut} that are available to the caller.
5173 * If {@param ignoreActivityType} or {@param ignoreWindowingMode} are not undefined,
5174 * then skip running tasks that match those types.
5175 */
5176 void getRunningTasks(List<TaskRecord> tasksOut, @ActivityType int ignoreActivityType,
5177 @WindowingMode int ignoreWindowingMode, int callingUid, boolean allowed) {
Wale Ogunwaled32da472018-11-16 07:19:28 -08005178 boolean focusedStack = mRootActivityContainer.getTopDisplayFocusedStack() == this;
riddle_hsuddc74152015-04-07 11:30:09 +08005179 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07005180 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08005181 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08005182 if (task.getTopActivity() == null) {
Winson Chung61c9e5a2017-10-11 10:39:32 -07005183 // Skip if there are no activities in the task
riddle_hsuddc74152015-04-07 11:30:09 +08005184 continue;
5185 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07005186 if (!allowed && !task.isActivityTypeHome() && task.effectiveUid != callingUid) {
Winson Chung61c9e5a2017-10-11 10:39:32 -07005187 // Skip if the caller can't fetch this task
Dianne Hackborn09233282014-04-30 11:33:59 -07005188 continue;
5189 }
Winson Chung61c9e5a2017-10-11 10:39:32 -07005190 if (ignoreActivityType != ACTIVITY_TYPE_UNDEFINED
5191 && task.getActivityType() == ignoreActivityType) {
5192 // Skip ignored activity type
5193 continue;
Craig Mautneraab647e2013-02-28 16:31:36 -08005194 }
Winson Chung61c9e5a2017-10-11 10:39:32 -07005195 if (ignoreWindowingMode != WINDOWING_MODE_UNDEFINED
5196 && task.getWindowingMode() == ignoreWindowingMode) {
5197 // Skip ignored windowing mode
5198 continue;
5199 }
riddle_hsuddc74152015-04-07 11:30:09 +08005200 if (focusedStack && topTask) {
Winson Chung61c9e5a2017-10-11 10:39:32 -07005201 // For the focused stack top task, update the last stack active time so that it can
5202 // be used to determine the order of the tasks (it may not be set for newly created
5203 // tasks)
5204 task.lastActiveTime = SystemClock.elapsedRealtime();
riddle_hsuddc74152015-04-07 11:30:09 +08005205 topTask = false;
5206 }
Winson Chung61c9e5a2017-10-11 10:39:32 -07005207 tasksOut.add(task);
Craig Mautnercae015f2013-02-08 14:31:27 -08005208 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005209 }
5210
Andrii Kulian21713ac2016-10-12 22:05:05 -07005211 void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08005212 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005213 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08005214 if (top >= 0) {
5215 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
5216 int activityTop = activities.size() - 1;
Dianne Hackborn354736e2016-08-22 17:00:05 -07005217 if (activityTop >= 0) {
Craig Mautneraab647e2013-02-28 16:31:36 -08005218 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
5219 "unhandled-back", true);
5220 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005221 }
5222 }
5223
Craig Mautner6b74cb52013-09-27 17:02:21 -07005224 /**
5225 * Reset local parameters because an app's activity died.
5226 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07005227 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07005228 */
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07005229 boolean handleAppDiedLocked(WindowProcessController app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07005230 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005231 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07005232 "App died while pausing: " + mPausingActivity);
5233 mPausingActivity = null;
5234 }
5235 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
5236 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07005237 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07005238 }
5239
Craig Mautner19091252013-10-05 00:03:53 -07005240 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07005241 }
5242
Wale Ogunwaled32da472018-11-16 07:19:28 -08005243 void handleAppCrash(WindowProcessController app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08005244 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5245 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
5246 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
5247 final ActivityRecord r = activities.get(activityNdx);
5248 if (r.app == app) {
5249 Slog.w(TAG, " Force finishing activity "
5250 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07005251 // Force the destroy to skip right to removal.
5252 r.app = null;
Wale Ogunwale3a256e62018-12-06 14:41:18 -08005253 getDisplay().mDisplayContent.prepareAppTransition(
lumark588a3e82018-07-20 18:53:54 +08005254 TRANSIT_CRASHING_ACTIVITY_CLOSE, false /* alwaysKeepCurrent */);
Bryce Leef52974c2018-02-14 15:12:01 -08005255 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false,
5256 "handleAppCrashedLocked");
Craig Mautnerd44711d2013-02-23 11:24:36 -08005257 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005258 }
5259 }
5260 }
5261
Garfield Tan347bd602018-12-21 15:11:12 -08005262 boolean dump(FileDescriptor fd, PrintWriter pw, boolean dumpAll, boolean dumpClient,
5263 String dumpPackage, boolean needSep) {
5264 pw.println(" Stack #" + mStackId
5265 + ": type=" + activityTypeToString(getActivityType())
5266 + " mode=" + windowingModeToString(getWindowingMode()));
5267 pw.println(" isSleeping=" + shouldSleepActivities());
5268 pw.println(" mBounds=" + getRequestedOverrideBounds());
5269
5270 boolean printed = dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
5271 needSep);
5272
5273 printed |= dumpHistoryList(fd, pw, mLRUActivities, " ", "Run", false,
5274 !dumpAll, false, dumpPackage, true,
5275 " Running activities (most recent first):", null);
5276
5277 needSep = printed;
5278 boolean pr = printThisActivity(pw, mPausingActivity, dumpPackage, needSep,
5279 " mPausingActivity: ");
5280 if (pr) {
5281 printed = true;
5282 needSep = false;
5283 }
5284 pr = printThisActivity(pw, getResumedActivity(), dumpPackage, needSep,
5285 " mResumedActivity: ");
5286 if (pr) {
5287 printed = true;
5288 needSep = false;
5289 }
5290 if (dumpAll) {
5291 pr = printThisActivity(pw, mLastPausedActivity, dumpPackage, needSep,
5292 " mLastPausedActivity: ");
5293 if (pr) {
5294 printed = true;
5295 needSep = true;
5296 }
5297 printed |= printThisActivity(pw, mLastNoHistoryActivity, dumpPackage,
5298 needSep, " mLastNoHistoryActivity: ");
5299 }
5300 return printed;
5301 }
5302
Dianne Hackborn390517b2013-05-30 15:03:32 -07005303 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005304 boolean dumpClient, String dumpPackage, boolean needSep) {
5305
5306 if (mTaskHistory.isEmpty()) {
5307 return false;
5308 }
5309 final String prefix = " ";
Craig Mautneraab647e2013-02-28 16:31:36 -08005310 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5311 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005312 if (needSep) {
5313 pw.println("");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07005314 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005315 pw.println(prefix + "Task id #" + task.taskId);
Evan Roskydfe3da72018-10-26 17:21:06 -07005316 pw.println(prefix + "mBounds=" + task.getRequestedOverrideBounds());
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005317 pw.println(prefix + "mMinWidth=" + task.mMinWidth);
5318 pw.println(prefix + "mMinHeight=" + task.mMinHeight);
5319 pw.println(prefix + "mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
5320 pw.println(prefix + "* " + task);
5321 task.dump(pw, prefix + " ");
Garfield Tan347bd602018-12-21 15:11:12 -08005322 dumpHistoryList(fd, pw, mTaskHistory.get(taskNdx).mActivities,
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005323 prefix, "Hist", true, !dumpAll, dumpClient, dumpPackage, false, null, task);
Craig Mautneraab647e2013-02-28 16:31:36 -08005324 }
Wale Ogunwale34a5b572017-08-31 08:29:41 -07005325 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08005326 }
5327
5328 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Winson Chung6998bc42017-02-28 17:07:05 -08005329 ArrayList<ActivityRecord> activities = new ArrayList<>();
Craig Mautnercae015f2013-02-08 14:31:27 -08005330
5331 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08005332 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5333 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08005334 }
5335 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08005336 final int top = mTaskHistory.size() - 1;
5337 if (top >= 0) {
5338 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
5339 int listTop = list.size() - 1;
5340 if (listTop >= 0) {
5341 activities.add(list.get(listTop));
5342 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005343 }
5344 } else {
5345 ItemMatcher matcher = new ItemMatcher();
5346 matcher.build(name);
5347
Craig Mautneraab647e2013-02-28 16:31:36 -08005348 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5349 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
5350 if (matcher.match(r1, r1.intent.getComponent())) {
5351 activities.add(r1);
5352 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005353 }
5354 }
5355 }
5356
5357 return activities;
5358 }
5359
5360 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07005361 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08005362
5363 // All activities that came from the package must be
5364 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08005365 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5366 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
5367 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
5368 final ActivityRecord a = activities.get(activityNdx);
5369 if (a.info.packageName.equals(packageName)) {
5370 a.forceNewConfig = true;
5371 if (starting != null && a == starting && a.visible) {
5372 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08005373 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08005374 }
Craig Mautnercae015f2013-02-08 14:31:27 -08005375 }
5376 }
5377 }
5378
5379 return starting;
5380 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08005381
Wale Ogunwale000957c2015-04-03 08:19:12 -07005382 /**
5383 * Removes the input task from this stack.
Garfield Tan891146c2018-10-09 12:14:00 -07005384 *
Wale Ogunwale000957c2015-04-03 08:19:12 -07005385 * @param task to remove.
5386 * @param reason for removal.
Wale Ogunwale06579d62016-04-30 15:29:06 -07005387 * @param mode task removal mode. Either {@link #REMOVE_TASK_MODE_DESTROYING},
5388 * {@link #REMOVE_TASK_MODE_MOVING}, {@link #REMOVE_TASK_MODE_MOVING_TO_TOP}.
Wale Ogunwale000957c2015-04-03 08:19:12 -07005389 */
Wale Ogunwale06579d62016-04-30 15:29:06 -07005390 void removeTask(TaskRecord task, String reason, int mode) {
Bryce Lee2b8e0372018-04-05 17:01:37 -07005391 final boolean removed = mTaskHistory.remove(task);
5392
5393 if (removed) {
5394 EventLog.writeEvent(EventLogTags.AM_REMOVE_TASK, task.taskId, getStackId());
5395 }
5396
Winson Chungabb433b2017-03-24 09:35:42 -07005397 removeActivitiesFromLRUListLocked(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07005398 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07005399
Garfield Tan347bd602018-12-21 15:11:12 -08005400 if (mode == REMOVE_TASK_MODE_DESTROYING) {
5401 task.cleanUpResourcesForDestroy();
Dianne Hackborn91097de2014-04-04 18:02:06 -07005402 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005403
5404 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005405 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Wale Ogunwale06579d62016-04-30 15:29:06 -07005406 // We only need to adjust focused stack if this stack is in focus and we are not in the
5407 // process of moving the task to the top of the stack that will be focused.
Louis Changbd48dca2018-08-29 17:44:34 +08005408 if (mode != REMOVE_TASK_MODE_MOVING_TO_TOP
Wale Ogunwaled32da472018-11-16 07:19:28 -08005409 && mRootActivityContainer.isTopDisplayFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08005410 String myReason = reason + " leftTaskHistoryEmpty";
Andrii Kulianab132ee2018-07-24 22:10:21 +08005411 if (!inMultiWindowMode() || adjustFocusToNextFocusableStack(myReason) == null) {
Louis Changbd48dca2018-08-29 17:44:34 +08005412 getDisplay().moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08005413 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005414 }
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07005415 if (isAttached()) {
5416 getDisplay().positionChildAtBottom(this);
Craig Mautner593a4e62014-01-15 17:55:51 -08005417 }
Louis Chang7d0037c2018-08-13 12:42:06 +08005418 if (!isActivityTypeHome() || getDisplay().isRemoved()) {
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07005419 remove();
Wale Ogunwale49853bf2015-02-23 09:24:42 -08005420 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08005421 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07005422
Andrii Kulian02b7a832016-10-06 23:11:56 -07005423 task.setStack(null);
Winson Chungc81c0ce2017-03-17 12:27:30 -07005424
5425 // Notify if a task from the pinned stack is being removed (or moved depending on the mode)
Wale Ogunwale44f036f2017-09-29 05:09:09 -07005426 if (inPinnedWindowingMode()) {
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -07005427 mService.getTaskChangeNotificationController().notifyActivityUnpinned();
Winson Chungc81c0ce2017-03-17 12:27:30 -07005428 }
Craig Mautner0247fc82013-02-28 14:32:06 -08005429 }
5430
Dianne Hackborn91097de2014-04-04 18:02:06 -07005431 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
5432 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07005433 boolean toTop) {
Bryce Leeb802ea12017-11-15 21:25:03 -08005434 return createTaskRecord(taskId, info, intent, voiceSession, voiceInteractor, toTop,
5435 null /*activity*/, null /*source*/, null /*options*/);
5436 }
5437
5438 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
5439 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
5440 boolean toTop, ActivityRecord activity, ActivityRecord source,
5441 ActivityOptions options) {
Garfield Tan9b1efea2017-12-05 16:43:46 -08005442 final TaskRecord task = TaskRecord.create(
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -07005443 mService, taskId, info, intent, voiceSession, voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07005444 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08005445 addTask(task, toTop, "createTaskRecord");
Lucas Dupin47a65c72018-02-15 14:16:18 -08005446 final int displayId = mDisplayId != INVALID_DISPLAY ? mDisplayId : DEFAULT_DISPLAY;
Bryce Lee2a3cc462017-10-27 10:57:35 -07005447 final boolean isLockscreenShown = mService.mStackSupervisor.getKeyguardController()
Lucas Dupin47a65c72018-02-15 14:16:18 -08005448 .isKeyguardOrAodShowing(displayId);
Bryce Leeec55eb02017-12-05 20:51:27 -08005449 if (!mStackSupervisor.getLaunchParamsController()
Bryce Leeb802ea12017-11-15 21:25:03 -08005450 .layoutTask(task, info.windowLayout, activity, source, options)
Bryce Leef3c6a472017-11-14 14:53:06 -08005451 && !matchParentBounds() && task.isResizeable() && !isLockscreenShown) {
Evan Roskydfe3da72018-10-26 17:21:06 -07005452 task.updateOverrideConfiguration(getRequestedOverrideBounds());
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07005453 }
Yunfan Chen0e7aff92018-12-05 16:35:32 -08005454 task.createTask(toTop, (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08005455 return task;
5456 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08005457
5458 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08005459 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005460 }
5461
Wale Ogunwale5f986092015-12-04 15:35:38 -08005462 void addTask(final TaskRecord task, final boolean toTop, String reason) {
Winson Chung5af42fc2017-03-24 17:11:33 -07005463 addTask(task, toTop ? MAX_VALUE : 0, true /* schedulePictureInPictureModeChange */, reason);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005464 if (toTop) {
5465 // TODO: figure-out a way to remove this call.
Riddle Hsu86cb7de2018-08-13 23:29:58 +08005466 positionChildWindowContainerAtTop(task);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005467 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005468 }
5469
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005470 // TODO: This shouldn't allow automatic reparenting. Remove the call to preAddTask and deal
5471 // with the fall-out...
Winson Chung5af42fc2017-03-24 17:11:33 -07005472 void addTask(final TaskRecord task, int position, boolean schedulePictureInPictureModeChange,
5473 String reason) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005474 // TODO: Is this remove really needed? Need to look into the call path for the other addTask
5475 mTaskHistory.remove(task);
Wale Ogunwale9e737db2018-12-17 15:42:37 -08005476 if (isSingleTaskInstance() && !mTaskHistory.isEmpty()) {
5477 throw new IllegalStateException("Can only have one child on stack=" + this);
5478 }
5479
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005480 position = getAdjustedPositionForTask(task, position, null /* starting */);
5481 final boolean toTop = position >= mTaskHistory.size();
Wale Ogunwale06579d62016-04-30 15:29:06 -07005482 final ActivityStack prevStack = preAddTask(task, reason, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005483
Andrii Kulianfb1bf692017-01-17 11:17:34 -08005484 mTaskHistory.add(position, task);
Andrii Kulian02b7a832016-10-06 23:11:56 -07005485 task.setStack(this);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005486
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005487 updateTaskMovement(task, toTop);
5488
Winson Chung5af42fc2017-03-24 17:11:33 -07005489 postAddTask(task, prevStack, schedulePictureInPictureModeChange);
Craig Mautnerc00204b2013-03-05 15:02:14 -08005490 }
5491
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005492 void positionChildAt(TaskRecord task, int index) {
5493
5494 if (task.getStack() != this) {
5495 throw new IllegalArgumentException("AS.positionChildAt: task=" + task
5496 + " is not a child of stack=" + this + " current parent=" + task.getStack());
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005497 }
5498
5499 task.updateOverrideConfigurationForStack(this);
5500
Jorim Jaggi023da532016-04-20 22:42:32 -07005501 final ActivityRecord topRunningActivity = task.topRunningActivityLocked();
Andrii Kulian02b7a832016-10-06 23:11:56 -07005502 final boolean wasResumed = topRunningActivity == task.getStack().mResumedActivity;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005503 insertTaskAtPosition(task, index);
Andrii Kulianfb1bf692017-01-17 11:17:34 -08005504 task.setStack(this);
Winson Chung5af42fc2017-03-24 17:11:33 -07005505 postAddTask(task, null /* prevStack */, true /* schedulePictureInPictureModeChange */);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -08005506
Jorim Jaggi023da532016-04-20 22:42:32 -07005507 if (wasResumed) {
5508 if (mResumedActivity != null) {
5509 Log.wtf(TAG, "mResumedActivity was already set when moving mResumedActivity from"
5510 + " other stack to this stack mResumedActivity=" + mResumedActivity
5511 + " other mResumedActivity=" + topRunningActivity);
5512 }
Bryce Leec4ab62a2018-03-05 14:19:26 -08005513 topRunningActivity.setState(RESUMED, "positionChildAt");
Jorim Jaggi023da532016-04-20 22:42:32 -07005514 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08005515
5516 // The task might have already been running and its visibility needs to be synchronized with
5517 // the visibility of the stack / windows.
5518 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled32da472018-11-16 07:19:28 -08005519 mRootActivityContainer.resumeFocusedStacksTopActivities();
Wale Ogunwale5f986092015-12-04 15:35:38 -08005520 }
5521
Wale Ogunwale06579d62016-04-30 15:29:06 -07005522 private ActivityStack preAddTask(TaskRecord task, String reason, boolean toTop) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07005523 final ActivityStack prevStack = task.getStack();
Wale Ogunwale5f986092015-12-04 15:35:38 -08005524 if (prevStack != null && prevStack != this) {
Wale Ogunwale06579d62016-04-30 15:29:06 -07005525 prevStack.removeTask(task, reason,
5526 toTop ? REMOVE_TASK_MODE_MOVING_TO_TOP : REMOVE_TASK_MODE_MOVING);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005527 }
5528 return prevStack;
5529 }
5530
Winson Chung5af42fc2017-03-24 17:11:33 -07005531 /**
5532 * @param schedulePictureInPictureModeChange specifies whether or not to schedule the PiP mode
5533 * change. Callers may set this to false if they are explicitly scheduling PiP mode
5534 * changes themselves, like during the PiP animation
5535 */
5536 private void postAddTask(TaskRecord task, ActivityStack prevStack,
5537 boolean schedulePictureInPictureModeChange) {
5538 if (schedulePictureInPictureModeChange && prevStack != null) {
5539 mStackSupervisor.scheduleUpdatePictureInPictureModeIfNeeded(task, prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08005540 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005541 try {
5542 task.voiceSession.taskStarted(task.intent, task.taskId);
5543 } catch (RemoteException e) {
5544 }
5545 }
5546 }
5547
Kazuki Takisef85197b2018-06-18 18:18:36 +09005548 public void setAlwaysOnTop(boolean alwaysOnTop) {
5549 if (isAlwaysOnTop() == alwaysOnTop) {
5550 return;
5551 }
5552 super.setAlwaysOnTop(alwaysOnTop);
5553 final ActivityDisplay display = getDisplay();
5554 // positionChildAtTop() must be called even when always on top gets turned off because we
5555 // need to make sure that the stack is moved from among always on top windows to below other
5556 // always on top windows. Since the position the stack should be inserted into is calculated
5557 // properly in {@link ActivityDisplay#getTopInsertPosition()} in both cases, we can just
5558 // request that the stack is put at top here.
Riddle Hsu57831b52018-07-27 00:31:48 +08005559 display.positionChildAtTop(this, false /* includingParents */);
Kazuki Takisef85197b2018-06-18 18:18:36 +09005560 }
5561
Riddle Hsu16567132018-08-16 21:37:47 +08005562 /** NOTE: Should only be called from {@link TaskRecord#reparent}. */
Winson Chungc2baac02017-01-11 13:34:47 -08005563 void moveToFrontAndResumeStateIfNeeded(ActivityRecord r, boolean moveToFront, boolean setResume,
5564 boolean setPause, String reason) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08005565 if (!moveToFront) {
5566 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07005567 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08005568
Riddle Hsu16567132018-08-16 21:37:47 +08005569 final ActivityState origState = r.getState();
Wale Ogunwaled046a012015-12-24 13:05:59 -08005570 // If the activity owns the last resumed activity, transfer that together,
5571 // so that we don't resume the same activity again in the new stack.
5572 // Apps may depend on onResume()/onPause() being called in pairs.
5573 if (setResume) {
Bryce Leec4ab62a2018-03-05 14:19:26 -08005574 r.setState(RESUMED, "moveToFrontAndResumeStateIfNeeded");
Winson Chungabb433b2017-03-24 09:35:42 -07005575 updateLRUListLocked(r);
Wale Ogunwaled046a012015-12-24 13:05:59 -08005576 }
Winson Chungc2baac02017-01-11 13:34:47 -08005577 // If the activity was previously pausing, then ensure we transfer that as well
5578 if (setPause) {
5579 mPausingActivity = r;
Winson Chung4dabf232017-01-25 13:25:22 -08005580 schedulePauseTimeout(r);
Winson Chungc2baac02017-01-11 13:34:47 -08005581 }
Riddle Hsu16567132018-08-16 21:37:47 +08005582 // Move the stack in which we are placing the activity to the front.
Wale Ogunwaled046a012015-12-24 13:05:59 -08005583 moveToFront(reason);
Riddle Hsu16567132018-08-16 21:37:47 +08005584 // If the original state is resumed, there is no state change to update focused app.
5585 // So here makes sure the activity focus is set if it is the top.
Wale Ogunwaled32da472018-11-16 07:19:28 -08005586 if (origState == RESUMED && r == mRootActivityContainer.getTopResumedActivity()) {
Riddle Hsu16567132018-08-16 21:37:47 +08005587 // TODO(b/111361570): Support multiple focused apps in WM
5588 mService.setResumedActivityUncheckLocked(r, reason);
5589 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07005590 }
5591
Yunfan Chen279f5582018-12-12 15:24:50 -08005592
5593 Rect getDefaultPictureInPictureBounds(float aspectRatio) {
5594 if (getTaskStack() == null) return null;
5595 return getTaskStack().getPictureInPictureBounds(aspectRatio, null /* currentStackBounds */);
5596 }
5597
5598 void animateResizePinnedStack(Rect sourceHintBounds, Rect toBounds, int animationDuration,
5599 boolean fromFullscreen) {
5600 if (!inPinnedWindowingMode()) return;
5601 if (skipResizeAnimation(toBounds == null /* toFullscreen */)) {
5602 mService.moveTasksToFullscreenStack(mStackId, true /* onTop */);
5603 } else {
5604 if (getTaskStack() == null) return;
5605 getTaskStack().animateResizePinnedStack(toBounds, sourceHintBounds,
5606 animationDuration, fromFullscreen);
5607 }
5608 }
5609
wilsonshih5c4cf522019-01-25 09:03:47 +08005610 /**
5611 * Get current bounds of this stack, return empty when it is unavailable.
5612 * @see TaskStack#getAnimationOrCurrentBounds(Rect)
5613 */
5614 void getAnimationOrCurrentBounds(Rect outBounds) {
5615 final TaskStack stack = getTaskStack();
5616 if (stack == null) {
5617 outBounds.setEmpty();
5618 return;
5619 }
5620 stack.getAnimationOrCurrentBounds(outBounds);
5621 }
5622
Yunfan Chen279f5582018-12-12 15:24:50 -08005623 private boolean skipResizeAnimation(boolean toFullscreen) {
5624 if (!toFullscreen) {
5625 return false;
5626 }
5627 final Configuration parentConfig = getParent().getConfiguration();
5628 final ActivityRecord top = topRunningNonOverlayTaskActivity();
5629 return top != null && !top.isConfigurationCompatible(parentConfig);
5630 }
5631
5632 void setPictureInPictureAspectRatio(float aspectRatio) {
5633 if (getTaskStack() == null) return;
5634 getTaskStack().setPictureInPictureAspectRatio(aspectRatio);
5635 }
5636
5637 void setPictureInPictureActions(List<RemoteAction> actions) {
5638 if (getTaskStack() == null) return;
5639 getTaskStack().setPictureInPictureActions(actions);
5640 }
5641
5642 boolean isAnimatingBoundsToFullscreen() {
5643 if (getTaskStack() == null) return false;
5644 return getTaskStack().isAnimatingBoundsToFullscreen();
5645 }
5646
5647 public void updatePictureInPictureModeForPinnedStackAnimation(Rect targetStackBounds,
5648 boolean forceUpdate) {
5649 // It is guaranteed that the activities requiring the update will be in the pinned stack at
5650 // this point (either reparented before the animation into PiP, or before reparenting after
5651 // the animation out of PiP)
5652 synchronized (mService.mGlobalLock) {
5653 if (!isAttached()) {
5654 return;
5655 }
5656 ArrayList<TaskRecord> tasks = getAllTasks();
5657 for (int i = 0; i < tasks.size(); i++) {
5658 mStackSupervisor.updatePictureInPictureMode(tasks.get(i), targetStackBounds,
5659 forceUpdate);
5660 }
5661 }
5662 }
5663
Craig Mautnerc00204b2013-03-05 15:02:14 -08005664 public int getStackId() {
5665 return mStackId;
5666 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07005667
5668 @Override
5669 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07005670 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
Wale Ogunwale44f036f2017-09-29 05:09:09 -07005671 + " stackId=" + mStackId + " type=" + activityTypeToString(getActivityType())
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07005672 + " mode=" + windowingModeToString(getWindowingMode())
Wale Ogunwale8e923af2018-03-23 08:48:11 -07005673 + " visible=" + shouldBeVisible(null /* starting */)
5674 + " translucent=" + isStackTranslucent(null /* starting */)
5675 + ", "
Wale Ogunwale44f036f2017-09-29 05:09:09 -07005676 + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07005677 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08005678
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -07005679 void onLockTaskPackagesUpdated() {
Craig Mautner15df08a2015-04-01 12:17:18 -07005680 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5681 mTaskHistory.get(taskNdx).setLockTaskAuth();
5682 }
5683 }
skuhne@google.com1b974dc2016-12-09 13:41:29 -08005684
5685 void executeAppTransition(ActivityOptions options) {
Wale Ogunwale3a256e62018-12-06 14:41:18 -08005686 getDisplay().mDisplayContent.executeAppTransition();
skuhne@google.com1b974dc2016-12-09 13:41:29 -08005687 ActivityOptions.abort(options);
5688 }
David Stevens9440dc82017-03-16 19:00:20 -07005689
5690 boolean shouldSleepActivities() {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07005691 final ActivityDisplay display = getDisplay();
Bryce Lee271617a2018-03-15 10:39:12 -07005692
5693 // Do not sleep activities in this stack if we're marked as focused and the keyguard
5694 // is in the process of going away.
Tiger Huang1e5b10a2018-07-30 20:19:51 +08005695 if (isFocusedStackOnDisplay()
Bryce Lee271617a2018-03-15 10:39:12 -07005696 && mStackSupervisor.getKeyguardController().isKeyguardGoingAway()) {
5697 return false;
5698 }
5699
Wale Ogunwalef6733932018-06-27 05:14:34 -07005700 return display != null ? display.isSleeping() : mService.isSleepingLocked();
David Stevens9440dc82017-03-16 19:00:20 -07005701 }
5702
5703 boolean shouldSleepOrShutDownActivities() {
Wale Ogunwalef6733932018-06-27 05:14:34 -07005704 return shouldSleepActivities() || mService.mShuttingDown;
David Stevens9440dc82017-03-16 19:00:20 -07005705 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07005706
Nataniel Borges023ecb52019-01-16 14:15:43 -08005707 public void writeToProto(ProtoOutputStream proto, long fieldId,
5708 @WindowTraceLogLevel int logLevel) {
Steven Timotius4346f0a2017-09-12 11:07:21 -07005709 final long token = proto.start(fieldId);
Nataniel Borges023ecb52019-01-16 14:15:43 -08005710 super.writeToProto(proto, CONFIGURATION_CONTAINER, logLevel);
Steven Timotius4346f0a2017-09-12 11:07:21 -07005711 proto.write(ID, mStackId);
5712 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5713 final TaskRecord task = mTaskHistory.get(taskNdx);
Nataniel Borges023ecb52019-01-16 14:15:43 -08005714 task.writeToProto(proto, TASKS, logLevel);
Steven Timotius4346f0a2017-09-12 11:07:21 -07005715 }
5716 if (mResumedActivity != null) {
5717 mResumedActivity.writeIdentifierToProto(proto, RESUMED_ACTIVITY);
5718 }
5719 proto.write(DISPLAY_ID, mDisplayId);
Bryce Leef3c6a472017-11-14 14:53:06 -08005720 if (!matchParentBounds()) {
Evan Roskydfe3da72018-10-26 17:21:06 -07005721 final Rect bounds = getRequestedOverrideBounds();
Bryce Leef3c6a472017-11-14 14:53:06 -08005722 bounds.writeToProto(proto, BOUNDS);
Steven Timotius4346f0a2017-09-12 11:07:21 -07005723 }
Bryce Leef3c6a472017-11-14 14:53:06 -08005724
5725 // TODO: Remove, no longer needed with windowingMode.
5726 proto.write(FULLSCREEN, matchParentBounds());
Steven Timotius4346f0a2017-09-12 11:07:21 -07005727 proto.end(token);
5728 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005729}