blob: 75333c728e0b2554de1908fd6176e7b5d0e2d99b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
Andrii Kulianab132ee2018-07-24 22:10:21 +080019import static android.app.ActivityTaskManager.INVALID_STACK_ID;
Wale Ogunwale98875612018-10-12 07:53:02 -070020import static android.app.ActivityTaskManager.INVALID_TASK_ID;
Wale Ogunwale65ebd952018-04-25 15:41:44 -070021import static android.app.ActivityTaskManager.RESIZE_MODE_FORCED;
22import static android.app.ActivityTaskManager.RESIZE_MODE_SYSTEM;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070023import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
24import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Riddle Hsu61987bc2019-04-03 13:08:47 +080025import static android.app.WindowConfiguration.ROTATION_UNDEFINED;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070026import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
27import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
28import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
29import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070030import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Evan Rosky1ac84462018-11-13 11:25:30 -080031import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Jorim Jaggi0a932142016-02-01 17:42:25 -080032import static android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
Wale Ogunwale66e16852017-10-19 13:35:52 -070033import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
Jorim Jaggi0a932142016-02-01 17:42:25 -080034import static android.content.Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
Wale Ogunwale66e16852017-10-19 13:35:52 -070035import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale3eadad72016-10-13 09:16:59 -070036import static android.content.pm.ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY;
Jorim Jaggi0a932142016-02-01 17:42:25 -080037import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_ALWAYS;
38import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_DEFAULT;
39import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
40import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_NEVER;
skuhne@google.com322347b2016-12-02 12:54:03 -080041import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZABLE_LANDSCAPE_ONLY;
42import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZABLE_PORTRAIT_ONLY;
43import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZABLE_PRESERVE_ORIENTATION;
Wale Ogunwaled829d362016-02-10 19:24:49 -080044import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Wale Ogunwale625ed0c2016-10-18 08:50:31 -070045import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE;
Winson Chungd3395382016-12-13 11:49:09 -080046import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_AND_PIPABLE_DEPRECATED;
Wale Ogunwale625ed0c2016-10-18 08:50:31 -070047import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Evan Rosky730f6e82018-12-03 17:40:11 -080048import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
49import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
50import static android.content.res.Configuration.ORIENTATION_UNDEFINED;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080051import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
Suprabh Shukla7745c142016-03-07 18:21:10 -080052import static android.provider.Settings.Secure.USER_SETUP_COMPLETE;
Andrii Kulian036e3ad2017-04-19 10:55:10 -070053import static android.view.Display.DEFAULT_DISPLAY;
Garfield Tan891146c2018-10-09 12:14:00 -070054
Yunfan Chen0e7aff92018-12-05 16:35:32 -080055import static com.android.server.EventLogTags.WM_TASK_CREATED;
Yi Jin6c6e9ca2018-03-20 16:53:35 -070056import static com.android.server.am.TaskRecordProto.ACTIVITIES;
Andrii Kulianab132ee2018-07-24 22:10:21 +080057import static com.android.server.am.TaskRecordProto.ACTIVITY_TYPE;
Yi Jin6c6e9ca2018-03-20 16:53:35 -070058import static com.android.server.am.TaskRecordProto.BOUNDS;
59import static com.android.server.am.TaskRecordProto.CONFIGURATION_CONTAINER;
60import static com.android.server.am.TaskRecordProto.FULLSCREEN;
61import static com.android.server.am.TaskRecordProto.ID;
62import static com.android.server.am.TaskRecordProto.LAST_NON_FULLSCREEN_BOUNDS;
63import static com.android.server.am.TaskRecordProto.MIN_HEIGHT;
64import static com.android.server.am.TaskRecordProto.MIN_WIDTH;
65import static com.android.server.am.TaskRecordProto.ORIG_ACTIVITY;
66import static com.android.server.am.TaskRecordProto.REAL_ACTIVITY;
67import static com.android.server.am.TaskRecordProto.RESIZE_MODE;
68import static com.android.server.am.TaskRecordProto.STACK_ID;
Andrii Kulian057a6512019-07-15 16:15:51 -070069import static com.android.server.wm.ActivityRecord.FINISH_RESULT_REMOVED;
Garfield Tan891146c2018-10-09 12:14:00 -070070import static com.android.server.wm.ActivityRecord.STARTING_WINDOW_SHOWN;
71import static com.android.server.wm.ActivityStack.REMOVE_TASK_MODE_MOVING;
72import static com.android.server.wm.ActivityStack.REMOVE_TASK_MODE_MOVING_TO_TOP;
73import static com.android.server.wm.ActivityStackSupervisor.ON_TOP;
Garfield Tan891146c2018-10-09 12:14:00 -070074import static com.android.server.wm.ActivityStackSupervisor.PRESERVE_WINDOWS;
75import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_ADD_REMOVE;
76import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_LOCKTASK;
77import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_RECENTS;
78import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_TASKS;
79import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_ADD_REMOVE;
80import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_LOCKTASK;
81import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_RECENTS;
82import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_TASKS;
83import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
84import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
Yunfan Chen0e7aff92018-12-05 16:35:32 -080085import static com.android.server.wm.WindowContainer.POSITION_BOTTOM;
86import static com.android.server.wm.WindowContainer.POSITION_TOP;
87import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
88import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Garfield Tan891146c2018-10-09 12:14:00 -070089
Winson Chung74666102017-02-22 17:49:24 -080090import static java.lang.Integer.MAX_VALUE;
91
Jorim Jaggie7d2b852017-08-28 17:55:15 +020092import android.annotation.IntDef;
Evan Rosky1ac84462018-11-13 11:25:30 -080093import android.annotation.NonNull;
Jorim Jaggie7d2b852017-08-28 17:55:15 +020094import android.annotation.Nullable;
95import android.app.Activity;
96import android.app.ActivityManager;
Jorim Jaggie7d2b852017-08-28 17:55:15 +020097import android.app.ActivityManager.TaskDescription;
98import android.app.ActivityManager.TaskSnapshot;
99import android.app.ActivityOptions;
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700100import android.app.ActivityTaskManager;
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200101import android.app.AppGlobals;
Winson Chungabfdcce2018-07-02 17:23:33 -0700102import android.app.TaskInfo;
Garfield Tan891146c2018-10-09 12:14:00 -0700103import android.app.WindowConfiguration;
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200104import android.content.ComponentName;
105import android.content.Intent;
106import android.content.pm.ActivityInfo;
107import android.content.pm.ApplicationInfo;
108import android.content.pm.IPackageManager;
109import android.content.pm.PackageManager;
110import android.content.res.Configuration;
111import android.graphics.Rect;
112import android.os.Debug;
113import android.os.RemoteException;
Winson Chungfb44d212017-10-04 11:39:10 -0700114import android.os.SystemClock;
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200115import android.os.Trace;
116import android.os.UserHandle;
117import android.provider.Settings;
118import android.service.voice.IVoiceInteractionSession;
119import android.util.DisplayMetrics;
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800120import android.util.EventLog;
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200121import android.util.Slog;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700122import android.util.proto.ProtoOutputStream;
Mark Renoufb1abb552019-02-08 13:51:41 -0500123import android.view.Display;
Evan Rosky1ac84462018-11-13 11:25:30 -0800124import android.view.DisplayInfo;
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200125
126import com.android.internal.annotations.VisibleForTesting;
127import com.android.internal.app.IVoiceInteractor;
128import com.android.internal.util.XmlUtils;
Wale Ogunwale59507092018-10-29 09:00:30 -0700129import com.android.server.wm.ActivityStack.ActivityState;
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200130
131import org.xmlpull.v1.XmlPullParser;
132import org.xmlpull.v1.XmlPullParserException;
133import org.xmlpull.v1.XmlSerializer;
134
135import java.io.IOException;
136import java.io.PrintWriter;
137import java.lang.annotation.Retention;
138import java.lang.annotation.RetentionPolicy;
139import java.util.ArrayList;
140import java.util.Objects;
141
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800142class TaskRecord extends ConfigurationContainer {
Wale Ogunwale98875612018-10-12 07:53:02 -0700143 private static final String TAG = TAG_WITH_CLASS_NAME ? "TaskRecord" : TAG_ATM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700144 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700145 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Craig Mautnere0570202015-05-13 13:06:11 -0700146 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700147 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800148
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700149 private static final String ATTR_TASKID = "task_id";
Craig Mautner21d24a22014-04-23 11:45:37 -0700150 private static final String TAG_INTENT = "intent";
151 private static final String TAG_AFFINITYINTENT = "affinity_intent";
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700152 private static final String ATTR_REALACTIVITY = "real_activity";
153 private static final String ATTR_REALACTIVITY_SUSPENDED = "real_activity_suspended";
Craig Mautner21d24a22014-04-23 11:45:37 -0700154 private static final String ATTR_ORIGACTIVITY = "orig_activity";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700155 private static final String TAG_ACTIVITY = "activity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700156 private static final String ATTR_AFFINITY = "affinity";
Dianne Hackborn79228822014-09-16 11:11:23 -0700157 private static final String ATTR_ROOT_AFFINITY = "root_affinity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700158 private static final String ATTR_ROOTHASRESET = "root_has_reset";
Dianne Hackborn13420f22014-07-18 15:43:56 -0700159 private static final String ATTR_AUTOREMOVERECENTS = "auto_remove_recents";
Craig Mautner21d24a22014-04-23 11:45:37 -0700160 private static final String ATTR_ASKEDCOMPATMODE = "asked_compat_mode";
161 private static final String ATTR_USERID = "user_id";
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800162 private static final String ATTR_USER_SETUP_COMPLETE = "user_setup_complete";
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700163 private static final String ATTR_EFFECTIVE_UID = "effective_uid";
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700164 @Deprecated
Craig Mautner21d24a22014-04-23 11:45:37 -0700165 private static final String ATTR_TASKTYPE = "task_type";
Craig Mautner21d24a22014-04-23 11:45:37 -0700166 private static final String ATTR_LASTDESCRIPTION = "last_description";
167 private static final String ATTR_LASTTIMEMOVED = "last_time_moved";
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700168 private static final String ATTR_NEVERRELINQUISH = "never_relinquish_identity";
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700169 private static final String ATTR_TASK_AFFILIATION = "task_affiliation";
Craig Mautnera228ae92014-07-09 05:44:55 -0700170 private static final String ATTR_PREV_AFFILIATION = "prev_affiliation";
171 private static final String ATTR_NEXT_AFFILIATION = "next_affiliation";
Winson Chungec396d62014-08-06 17:08:00 -0700172 private static final String ATTR_TASK_AFFILIATION_COLOR = "task_affiliation_color";
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700173 private static final String ATTR_CALLING_UID = "calling_uid";
174 private static final String ATTR_CALLING_PACKAGE = "calling_package";
Winson Chungd3395382016-12-13 11:49:09 -0800175 private static final String ATTR_SUPPORTS_PICTURE_IN_PICTURE = "supports_picture_in_picture";
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800176 private static final String ATTR_RESIZE_MODE = "resize_mode";
Wale Ogunwale706ed792015-08-02 10:29:44 -0700177 private static final String ATTR_NON_FULLSCREEN_BOUNDS = "non_fullscreen_bounds";
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700178 private static final String ATTR_MIN_WIDTH = "min_width";
179 private static final String ATTR_MIN_HEIGHT = "min_height";
Wale Ogunwale625ed0c2016-10-18 08:50:31 -0700180 private static final String ATTR_PERSIST_TASK_VERSION = "persist_task_version";
Andrii Kulian18d75122016-03-27 20:20:28 -0700181
Wale Ogunwale625ed0c2016-10-18 08:50:31 -0700182 // Current version of the task record we persist. Used to check if we need to run any upgrade
183 // code.
184 private static final int PERSIST_TASK_VERSION = 1;
Craig Mautner21d24a22014-04-23 11:45:37 -0700185
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700186 private static final int INVALID_MIN_SIZE = -1;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800187
Winson Chung74666102017-02-22 17:49:24 -0800188 /**
189 * The modes to control how the stack is moved to the front when calling
190 * {@link TaskRecord#reparent}.
191 */
192 @Retention(RetentionPolicy.SOURCE)
193 @IntDef({
194 REPARENT_MOVE_STACK_TO_FRONT,
195 REPARENT_KEEP_STACK_AT_FRONT,
196 REPARENT_LEAVE_STACK_IN_PLACE
197 })
Wale Ogunwale66e16852017-10-19 13:35:52 -0700198 @interface ReparentMoveStackMode {}
Winson Chung74666102017-02-22 17:49:24 -0800199 // Moves the stack to the front if it was not at the front
Wale Ogunwale66e16852017-10-19 13:35:52 -0700200 static final int REPARENT_MOVE_STACK_TO_FRONT = 0;
Winson Chung74666102017-02-22 17:49:24 -0800201 // Only moves the stack to the front if it was focused or front most already
Wale Ogunwale66e16852017-10-19 13:35:52 -0700202 static final int REPARENT_KEEP_STACK_AT_FRONT = 1;
Winson Chung74666102017-02-22 17:49:24 -0800203 // Do not move the stack as a part of reparenting
Wale Ogunwale66e16852017-10-19 13:35:52 -0700204 static final int REPARENT_LEAVE_STACK_IN_PLACE = 2;
Winson Chung74666102017-02-22 17:49:24 -0800205
Garfield Tan9b1efea2017-12-05 16:43:46 -0800206 /**
207 * The factory used to create {@link TaskRecord}. This allows OEM subclass {@link TaskRecord}.
208 */
209 private static TaskRecordFactory sTaskRecordFactory;
210
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700211 final int mTaskId; // Unique identifier for this task.
Dianne Hackborn79228822014-09-16 11:11:23 -0700212 String affinity; // The affinity name for this task, or null; may change identity.
213 String rootAffinity; // Initial base affinity, or null; does not change from initial root.
Dianne Hackborn91097de2014-04-04 18:02:06 -0700214 final IVoiceInteractionSession voiceSession; // Voice interaction session driving task
215 final IVoiceInteractor voiceInteractor; // Associated interactor to provide to app
Bryce Lee1a990e52018-04-23 10:54:11 -0700216 Intent intent; // The original intent that started the task. Note that this value can
217 // be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 Intent affinityIntent; // Intent of affinity-moved activity that started this task.
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700219 int effectiveUid; // The current effective uid of the identity of this task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 ComponentName origActivity; // The non-alias activity component of the intent.
221 ComponentName realActivity; // The actual activity component that started the task.
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +0000222 boolean realActivitySuspended; // True if the actual activity component that started the
223 // task is suspended.
Dianne Hackborn852975d2014-08-22 17:42:43 -0700224 boolean inRecents; // Actually in the recents list?
Winson Chungfb44d212017-10-04 11:39:10 -0700225 long lastActiveTime; // Last time this task was active in the current device session,
226 // including sleep. This time is initialized to the elapsed time when
227 // restored from disk.
Dianne Hackborn852975d2014-08-22 17:42:43 -0700228 boolean isAvailable; // Is the activity available to be launched?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 boolean rootWasReset; // True if the intent at the root of the task had
230 // the FLAG_ACTIVITY_RESET_TASK_IF_NEEDED flag.
Dianne Hackborn13420f22014-07-18 15:43:56 -0700231 boolean autoRemoveRecents; // If true, we should automatically remove the task from
232 // recents when activity finishes
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700233 boolean askedCompatMode;// Have asked the user about compat mode for this task.
Dianne Hackbornd38aed82014-06-10 21:36:35 -0700234 boolean hasBeenVisible; // Set if any activities in the task have been visible to the user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700236 String stringName; // caching of toString() result.
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700237 int mUserId; // user for which this task was created
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800238 boolean mUserSetupComplete; // The user set-up is complete as of the last time the task activity
239 // was changed.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800240
241 int numFullscreen; // Number of fullscreen activities.
242
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800243 int mResizeMode; // The resize mode of this task and its activities.
244 // Based on the {@link ActivityInfo#resizeMode} of the root activity.
Wale Ogunwale069bbd32017-02-03 07:58:14 -0800245 private boolean mSupportsPictureInPicture; // Whether or not this task and its activities
246 // support PiP. Based on the {@link ActivityInfo#FLAG_SUPPORTS_PICTURE_IN_PICTURE} flag
247 // of the root activity.
Craig Mautner15df08a2015-04-01 12:17:18 -0700248 /** Can't be put in lockTask mode. */
249 final static int LOCK_TASK_AUTH_DONT_LOCK = 0;
Benjamin Franz469dd582015-06-09 14:24:36 +0100250 /** Can enter app pinning with user approval. Can never start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700251 final static int LOCK_TASK_AUTH_PINNABLE = 1;
252 /** Starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing lockTask task. */
253 final static int LOCK_TASK_AUTH_LAUNCHABLE = 2;
Benjamin Franz469dd582015-06-09 14:24:36 +0100254 /** Can enter lockTask without user approval. Can start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700255 final static int LOCK_TASK_AUTH_WHITELISTED = 3;
Benjamin Franz469dd582015-06-09 14:24:36 +0100256 /** Priv-app that starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing
257 * lockTask task. */
258 final static int LOCK_TASK_AUTH_LAUNCHABLE_PRIV = 4;
Craig Mautner15df08a2015-04-01 12:17:18 -0700259 int mLockTaskAuth = LOCK_TASK_AUTH_PINNABLE;
260
261 int mLockTaskUid = -1; // The uid of the application that called startLockTask().
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800262
Winson Chung03a9bae2014-05-02 09:56:12 -0700263 // This represents the last resolved activity values for this task
264 // NOTE: This value needs to be persisted with each task
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700265 TaskDescription mTaskDescription;
Winson Chung03a9bae2014-05-02 09:56:12 -0700266
Craig Mautnerd2328952013-03-05 12:46:26 -0800267 /** List of all activities in the task arranged in history order */
Craig Mautner21d24a22014-04-23 11:45:37 -0700268 final ArrayList<ActivityRecord> mActivities;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800269
Andrii Kulian02b7a832016-10-06 23:11:56 -0700270 /** Current stack. Setter must always be used to update the value. */
271 private ActivityStack mStack;
Craig Mautnerd2328952013-03-05 12:46:26 -0800272
Dianne Hackborn68a06332017-11-15 17:54:18 -0800273 /** The process that had previously hosted the root activity of this task.
274 * Used to know that we should try harder to keep this process around, in case the
275 * user wants to return to it. */
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700276 private WindowProcessController mRootProcess;
Dianne Hackborn68a06332017-11-15 17:54:18 -0800277
Craig Mautner21d24a22014-04-23 11:45:37 -0700278 /** Takes on same value as first root activity */
279 boolean isPersistable = false;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700280 int maxRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700281
282 /** Only used for persistable tasks, otherwise 0. The last time this task was moved. Used for
283 * determining the order when restoring. Sign indicates whether last task movement was to front
284 * (positive) or back (negative). Absolute value indicates time. */
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700285 long mLastTimeMoved;
Craig Mautner21d24a22014-04-23 11:45:37 -0700286
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700287 /** If original intent did not allow relinquishing task identity, save that information */
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700288 private boolean mNeverRelinquishIdentity = true;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700289
Craig Mautner362449a2014-06-20 14:04:39 -0700290 // Used in the unique case where we are clearing the task in order to reuse it. In that case we
291 // do not want to delete the stack when the task goes empty.
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -0800292 private boolean mReuseTask = false;
Craig Mautner362449a2014-06-20 14:04:39 -0700293
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700294 CharSequence lastDescription; // Last description captured for this item.
295
Craig Mautnera228ae92014-07-09 05:44:55 -0700296 int mAffiliatedTaskId; // taskId of parent affiliation or self if no parent.
Winson Chungec396d62014-08-06 17:08:00 -0700297 int mAffiliatedTaskColor; // color of the parent task affiliation.
Craig Mautnera228ae92014-07-09 05:44:55 -0700298 TaskRecord mPrevAffiliate; // previous task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800299 int mPrevAffiliateTaskId = INVALID_TASK_ID; // previous id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700300 TaskRecord mNextAffiliate; // next task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800301 int mNextAffiliateTaskId = INVALID_TASK_ID; // next id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700302
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700303 // For relaunching the task from recents as though it was launched by the original launcher.
304 int mCallingUid;
305 String mCallingPackage;
306
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700307 final ActivityTaskManagerService mAtmService;
Craig Mautner21d24a22014-04-23 11:45:37 -0700308
Jorim Jaggi82c9dc92016-02-05 15:10:33 -0800309 private final Rect mTmpStableBounds = new Rect();
310 private final Rect mTmpNonDecorBounds = new Rect();
Evan Rosky1ac84462018-11-13 11:25:30 -0800311 private final Rect mTmpBounds = new Rect();
312 private final Rect mTmpInsets = new Rect();
Jorim Jaggi0a932142016-02-01 17:42:25 -0800313
Wale Ogunwale706ed792015-08-02 10:29:44 -0700314 // Last non-fullscreen bounds the task was launched in or resized to.
315 // The information is persisted and used to determine the appropriate stack to launch the
316 // task into on restore.
317 Rect mLastNonFullscreenBounds = null;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700318 // Minimal width and height of this task when it's resizeable. -1 means it should use the
319 // default minimal width/height.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700320 int mMinWidth;
321 int mMinHeight;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700322
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700323 // Ranking (from top) of this task among all visible tasks. (-1 means it's not visible)
324 // This number will be assigned when we evaluate OOM scores for all visible tasks.
325 int mLayerRank = -1;
326
Evan Roskyed6767f2018-10-26 17:21:06 -0700327 // When non-empty, this represents the bounds this task will be drawn at. This gets set during
328 // transient operations such as split-divider dragging and animations.
329 // TODO(b/119687367): This member is temporary.
330 final Rect mDisplayedBounds = new Rect();
331
Andrii Kulian1779e612016-10-12 21:58:25 -0700332 /** Helper object used for updating override configuration. */
333 private Configuration mTmpConfig = new Configuration();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700334
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800335 // TODO: remove after unification
336 Task mTask;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800337
Mark Renoufc808f062019-02-07 15:20:37 -0500338 /** Used by fillTaskInfo */
339 final TaskActivitiesReport mReuseActivitiesReport = new TaskActivitiesReport();
340
Garfield Tan9b1efea2017-12-05 16:43:46 -0800341 /**
Wale Ogunwaled95c06b2018-05-08 10:35:38 -0700342 * Don't use constructor directly. Use {@link #create(ActivityTaskManagerService, int,
343 * ActivityInfo, Intent, TaskDescription)} instead.
Garfield Tan9b1efea2017-12-05 16:43:46 -0800344 */
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700345 TaskRecord(ActivityTaskManagerService atmService, int _taskId, ActivityInfo info,
346 Intent _intent, IVoiceInteractionSession _voiceSession,
347 IVoiceInteractor _voiceInteractor, TaskDescription _taskDescription) {
348 this(atmService, _taskId, _intent, null /*_affinityIntent*/, null /*_affinity*/,
349 null /*_rootAffinity*/, null /*_realActivity*/, null /*_origActivity*/,
350 false /*_rootWasReset*/, false /*_autoRemoveRecents*/, false /*_askedCompatMode*/,
351 UserHandle.getUserId(info.applicationInfo.uid), 0 /*_effectiveUid*/,
352 null /*_lastDescription*/, new ArrayList<>(), System.currentTimeMillis(),
353 true /*neverRelinquishIdentity*/,
354 _taskDescription != null ? _taskDescription : new TaskDescription(),
355 _taskId, INVALID_TASK_ID, INVALID_TASK_ID, 0 /*taskAffiliationColor*/,
356 info.applicationInfo.uid, info.packageName, info.resizeMode,
357 info.supportsPictureInPicture(), false /*_realActivitySuspended*/,
358 false /*userSetupComplete*/, INVALID_MIN_SIZE, INVALID_MIN_SIZE, info,
359 _voiceSession, _voiceInteractor);
Craig Mautner21d24a22014-04-23 11:45:37 -0700360 }
361
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700362 /** Don't use constructor directly. This is only used by XML parser. */
363 TaskRecord(ActivityTaskManagerService atmService, int _taskId, Intent _intent,
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800364 Intent _affinityIntent, String _affinity, String _rootAffinity,
365 ComponentName _realActivity, ComponentName _origActivity, boolean _rootWasReset,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700366 boolean _autoRemoveRecents, boolean _askedCompatMode, int _userId,
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800367 int _effectiveUid, String _lastDescription, ArrayList<ActivityRecord> activities,
Winson Chungfb44d212017-10-04 11:39:10 -0700368 long lastTimeMoved, boolean neverRelinquishIdentity,
369 TaskDescription _lastTaskDescription, int taskAffiliation, int prevTaskId,
370 int nextTaskId, int taskAffiliationColor, int callingUid, String callingPackage,
Charles He2bf28322017-10-12 22:24:49 +0100371 int resizeMode, boolean supportsPictureInPicture, boolean _realActivitySuspended,
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700372 boolean userSetupComplete, int minWidth, int minHeight, ActivityInfo info,
373 IVoiceInteractionSession _voiceSession, IVoiceInteractor _voiceInteractor) {
374 mAtmService = atmService;
375 mTaskId = _taskId;
Craig Mautner21d24a22014-04-23 11:45:37 -0700376 affinityIntent = _affinityIntent;
377 affinity = _affinity;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800378 rootAffinity = _rootAffinity;
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700379 voiceSession = _voiceSession;
380 voiceInteractor = _voiceInteractor;
Craig Mautner21d24a22014-04-23 11:45:37 -0700381 realActivity = _realActivity;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800382 realActivitySuspended = _realActivitySuspended;
Craig Mautner21d24a22014-04-23 11:45:37 -0700383 origActivity = _origActivity;
384 rootWasReset = _rootWasReset;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700385 isAvailable = true;
Dianne Hackborn13420f22014-07-18 15:43:56 -0700386 autoRemoveRecents = _autoRemoveRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700387 askedCompatMode = _askedCompatMode;
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700388 mUserId = _userId;
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800389 mUserSetupComplete = userSetupComplete;
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700390 effectiveUid = _effectiveUid;
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700391 touchActiveTime();
Craig Mautner21d24a22014-04-23 11:45:37 -0700392 lastDescription = _lastDescription;
393 mActivities = activities;
394 mLastTimeMoved = lastTimeMoved;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700395 mNeverRelinquishIdentity = neverRelinquishIdentity;
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700396 mTaskDescription = _lastTaskDescription;
Craig Mautnera228ae92014-07-09 05:44:55 -0700397 mAffiliatedTaskId = taskAffiliation;
Winson Chungec396d62014-08-06 17:08:00 -0700398 mAffiliatedTaskColor = taskAffiliationColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700399 mPrevAffiliateTaskId = prevTaskId;
400 mNextAffiliateTaskId = nextTaskId;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700401 mCallingUid = callingUid;
402 mCallingPackage = callingPackage;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800403 mResizeMode = resizeMode;
Winson Chungd3395382016-12-13 11:49:09 -0800404 mSupportsPictureInPicture = supportsPictureInPicture;
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700405 if (info != null) {
406 setIntent(_intent, info);
407 setMinDimensions(info);
408 } else {
409 intent = _intent;
410 mMinWidth = minWidth;
411 mMinHeight = minHeight;
412 }
413 mAtmService.getTaskChangeNotificationController().notifyTaskCreated(_taskId, realActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 }
415
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800416 Task getTask() {
417 return mTask;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800418 }
419
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800420 void createTask(boolean onTop, boolean showForAllUsers) {
421 if (mTask != null) {
422 throw new IllegalArgumentException("mTask=" + mTask
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800423 + " already created for task=" + this);
424 }
425
426 final Rect bounds = updateOverrideConfigurationFromLaunchBounds();
Yunfan Chen279f5582018-12-12 15:24:50 -0800427 final TaskStack stack = getStack().getTaskStack();
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800428
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800429 if (stack == null) {
Yunfan Chen279f5582018-12-12 15:24:50 -0800430 throw new IllegalArgumentException("TaskRecord: invalid stack=" + mStack);
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800431 }
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700432 EventLog.writeEvent(WM_TASK_CREATED, mTaskId, stack.mStackId);
433 mTask = new Task(mTaskId, stack, mUserId, mAtmService.mWindowManager, mResizeMode,
434 mSupportsPictureInPicture, mTaskDescription, this);
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800435 final int position = onTop ? POSITION_TOP : POSITION_BOTTOM;
436
437 if (!mDisplayedBounds.isEmpty()) {
438 mTask.setOverrideDisplayedBounds(mDisplayedBounds);
439 }
440 // We only want to move the parents to the parents if we are creating this task at the
441 // top of its stack.
442 stack.addTask(mTask, position, showForAllUsers, onTop /* moveParents */);
Bryce Lee04ab3462017-04-10 15:06:33 -0700443 }
444
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800445 void setTask(Task task) {
446 mTask = task;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800447 }
448
Garfield Tan347bd602018-12-21 15:11:12 -0800449 void cleanUpResourcesForDestroy() {
450 if (!mActivities.isEmpty()) {
451 return;
452 }
453
454 // This task is going away, so save the last state if necessary.
455 saveLaunchingStateIfNeeded();
456
457 // TODO: VI what about activity?
458 final boolean isVoiceSession = voiceSession != null;
459 if (isVoiceSession) {
460 try {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700461 voiceSession.taskFinished(intent, mTaskId);
Garfield Tan347bd602018-12-21 15:11:12 -0800462 } catch (RemoteException e) {
463 }
464 }
465 if (autoRemoveFromRecents() || isVoiceSession) {
466 // Task creator asked to remove this when done, or this task was a voice
467 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700468 mAtmService.mStackSupervisor.mRecentTasks.remove(this);
Garfield Tan347bd602018-12-21 15:11:12 -0800469 }
470
471 removeWindowContainer();
472 }
473
474 @VisibleForTesting
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800475 void removeWindowContainer() {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700476 mAtmService.getLockTaskController().clearLockedTask(this);
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800477 if (mTask == null) {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700478 if (DEBUG_STACK) Slog.i(TAG_WM, "removeTask: could not find taskId=" + mTaskId);
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800479 return;
480 }
481 mTask.removeIfPossible();
482 mTask = null;
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700483 if (!getWindowConfiguration().persistTaskBounds()) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800484 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
485 // default configuration the next time it launches.
Evan Roskya4cc3a92019-06-28 13:25:01 -0700486 setBounds(null);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800487 }
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700488 mAtmService.getTaskChangeNotificationController().notifyTaskRemoved(mTaskId);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800489 }
490
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700491 void onSnapshotChanged(TaskSnapshot snapshot) {
492 mAtmService.getTaskChangeNotificationController().notifyTaskSnapshotChanged(mTaskId, snapshot);
Jorim Jaggifb9d78a2017-01-05 18:57:12 +0100493 }
494
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800495 void setResizeMode(int resizeMode) {
496 if (mResizeMode == resizeMode) {
497 return;
498 }
499 mResizeMode = resizeMode;
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800500 mTask.setResizeable(resizeMode);
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700501 mAtmService.mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
502 mAtmService.mRootActivityContainer.resumeFocusedStacksTopActivities();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800503 }
504
505 void setTaskDockedResizing(boolean resizing) {
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800506 if (mTask == null) {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700507 Slog.w(TAG_WM, "setTaskDockedResizing: taskId " + mTaskId + " not found.");
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800508 return;
509 }
510 mTask.setTaskDockedResizing(resizing);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800511 }
512
Wale Ogunwale1666e312016-12-16 11:27:18 -0800513 // TODO: Consolidate this with the resize() method below.
Wale Ogunwale1666e312016-12-16 11:27:18 -0800514 public void requestResize(Rect bounds, int resizeMode) {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700515 mAtmService.resizeTask(mTaskId, bounds, resizeMode);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800516 }
517
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800518 boolean resize(Rect bounds, int resizeMode, boolean preserveWindow, boolean deferResume) {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700519 mAtmService.deferWindowLayout();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800520
Bryce Leef3c6a472017-11-14 14:53:06 -0800521 try {
522 if (!isResizeable()) {
523 Slog.w(TAG, "resizeTask: task " + this + " not resizeable.");
524 return true;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800525 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800526
Bryce Leef3c6a472017-11-14 14:53:06 -0800527 // If this is a forced resize, let it go through even if the bounds is not changing,
528 // as we might need a relayout due to surface size change (to/from fullscreen).
529 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Evan Roskydfe3da72018-10-26 17:21:06 -0700530 if (equivalentRequestedOverrideBounds(bounds) && !forced) {
Bryce Leef3c6a472017-11-14 14:53:06 -0800531 // Nothing to do here...
532 return true;
533 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800534
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800535 if (mTask == null) {
Bryce Leef3c6a472017-11-14 14:53:06 -0800536 // Task doesn't exist in window manager yet (e.g. was restored from recents).
537 // All we can do for now is update the bounds so it can be used when the task is
538 // added to window manager.
Evan Roskya4cc3a92019-06-28 13:25:01 -0700539 setBounds(bounds);
Bryce Leef3c6a472017-11-14 14:53:06 -0800540 if (!inFreeformWindowingMode()) {
541 // re-restore the task so it can have the proper stack association.
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700542 mAtmService.mStackSupervisor.restoreRecentTaskLocked(this, null, !ON_TOP);
Bryce Leef3c6a472017-11-14 14:53:06 -0800543 }
544 return true;
545 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800546
Bryce Leef3c6a472017-11-14 14:53:06 -0800547 if (!canResizeToBounds(bounds)) {
548 throw new IllegalArgumentException("resizeTask: Can not resize task=" + this
549 + " to bounds=" + bounds + " resizeMode=" + mResizeMode);
550 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800551
Bryce Leef3c6a472017-11-14 14:53:06 -0800552 // Do not move the task to another stack here.
553 // This method assumes that the task is already placed in the right stack.
554 // we do not mess with that decision and we only do the resize!
555
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700556 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + mTaskId);
Bryce Leef3c6a472017-11-14 14:53:06 -0800557
Evan Roskya4cc3a92019-06-28 13:25:01 -0700558 boolean updatedConfig = false;
559 mTmpConfig.setTo(getResolvedOverrideConfiguration());
560 if (setBounds(bounds) != BOUNDS_CHANGE_NONE) {
561 updatedConfig = !mTmpConfig.equals(getResolvedOverrideConfiguration());
562 }
Bryce Leef3c6a472017-11-14 14:53:06 -0800563 // This variable holds information whether the configuration didn't change in a significant
564
565 // way and the activity was kept the way it was. If it's false, it means the activity
566 // had
567 // to be relaunched due to configuration change.
568 boolean kept = true;
569 if (updatedConfig) {
570 final ActivityRecord r = topRunningActivityLocked();
571 if (r != null && !deferResume) {
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -0800572 kept = r.ensureActivityConfiguration(0 /* globalChanges */,
Bryce Leef3c6a472017-11-14 14:53:06 -0800573 preserveWindow);
Garfield Tanb9151182018-06-25 16:29:21 -0700574 // Preserve other windows for resizing because if resizing happens when there
575 // is a dialog activity in the front, the activity that still shows some
576 // content to the user will become black and cause flickers. Note in most cases
577 // this won't cause tons of irrelevant windows being preserved because only
578 // activities in this task may experience a bounds change. Configs for other
579 // activities stay the same.
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700580 mAtmService.mRootActivityContainer.ensureActivitiesVisible(r, 0, preserveWindow);
Bryce Leef3c6a472017-11-14 14:53:06 -0800581 if (!kept) {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700582 mAtmService.mRootActivityContainer.resumeFocusedStacksTopActivities();
Bryce Leef3c6a472017-11-14 14:53:06 -0800583 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800584 }
585 }
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800586 mTask.resize(kept, forced);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800587
Garfield Tan891146c2018-10-09 12:14:00 -0700588 saveLaunchingStateIfNeeded();
589
Bryce Leef3c6a472017-11-14 14:53:06 -0800590 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
591 return kept;
592 } finally {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700593 mAtmService.continueWindowLayout();
Bryce Leef3c6a472017-11-14 14:53:06 -0800594 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800595 }
596
597 // TODO: Investigate combining with the resize() method above.
598 void resizeWindowContainer() {
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800599 mTask.resize(false /* relayout */, false /* forced */);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800600 }
601
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800602 void getWindowContainerBounds(Rect bounds) {
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800603 if (mTask != null) {
604 mTask.getBounds(bounds);
605 } else {
606 bounds.setEmpty();
607 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800608 }
609
Winson Chung74666102017-02-22 17:49:24 -0800610 /**
611 * Convenience method to reparent a task to the top or bottom position of the stack.
612 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700613 boolean reparent(ActivityStack preferredStack, boolean toTop,
614 @ReparentMoveStackMode int moveStackMode, boolean animate, boolean deferResume,
615 String reason) {
616 return reparent(preferredStack, toTop ? MAX_VALUE : 0, moveStackMode, animate, deferResume,
617 true /* schedulePictureInPictureModeChange */, reason);
Winson Chung5af42fc2017-03-24 17:11:33 -0700618 }
619
620 /**
621 * Convenience method to reparent a task to the top or bottom position of the stack, with
622 * an option to skip scheduling the picture-in-picture mode change.
623 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700624 boolean reparent(ActivityStack preferredStack, boolean toTop,
625 @ReparentMoveStackMode int moveStackMode, boolean animate, boolean deferResume,
626 boolean schedulePictureInPictureModeChange, String reason) {
627 return reparent(preferredStack, toTop ? MAX_VALUE : 0, moveStackMode, animate,
Winson Chung5af42fc2017-03-24 17:11:33 -0700628 deferResume, schedulePictureInPictureModeChange, reason);
629 }
630
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700631 /** Convenience method to reparent a task to a specific position of the stack. */
632 boolean reparent(ActivityStack preferredStack, int position,
633 @ReparentMoveStackMode int moveStackMode, boolean animate, boolean deferResume,
634 String reason) {
635 return reparent(preferredStack, position, moveStackMode, animate, deferResume,
Winson Chung5af42fc2017-03-24 17:11:33 -0700636 true /* schedulePictureInPictureModeChange */, reason);
Winson Chung74666102017-02-22 17:49:24 -0800637 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800638
Winson Chung74666102017-02-22 17:49:24 -0800639 /**
640 * Reparents the task into a preferred stack, creating it if necessary.
641 *
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700642 * @param preferredStack the target stack to move this task
Winson Chung74666102017-02-22 17:49:24 -0800643 * @param position the position to place this task in the new stack
644 * @param animate whether or not we should wait for the new window created as a part of the
Winson Chung5af42fc2017-03-24 17:11:33 -0700645 * reparenting to be drawn and animated in
Winson Chung74666102017-02-22 17:49:24 -0800646 * @param moveStackMode whether or not to move the stack to the front always, only if it was
Winson Chung5af42fc2017-03-24 17:11:33 -0700647 * previously focused & in front, or never
Winson Chung74666102017-02-22 17:49:24 -0800648 * @param deferResume whether or not to update the visibility of other tasks and stacks that may
Winson Chung5af42fc2017-03-24 17:11:33 -0700649 * have changed as a result of this reparenting
650 * @param schedulePictureInPictureModeChange specifies whether or not to schedule the PiP mode
651 * change. Callers may set this to false if they are explicitly scheduling PiP mode
652 * changes themselves, like during the PiP animation
Winson Chung74666102017-02-22 17:49:24 -0800653 * @param reason the caller of this reparenting
Winson Chung5af42fc2017-03-24 17:11:33 -0700654 * @return whether the task was reparented
Winson Chung74666102017-02-22 17:49:24 -0800655 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700656 // TODO: Inspect all call sites and change to just changing windowing mode of the stack vs.
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700657 // re-parenting the task. Can only be done when we are no longer using static stack Ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700658 boolean reparent(ActivityStack preferredStack, int position,
659 @ReparentMoveStackMode int moveStackMode, boolean animate, boolean deferResume,
660 boolean schedulePictureInPictureModeChange, String reason) {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700661 final ActivityStackSupervisor supervisor = mAtmService.mStackSupervisor;
662 final RootActivityContainer root = mAtmService.mRootActivityContainer;
663 final WindowManagerService windowManager = mAtmService.mWindowManager;
Winson Chung74666102017-02-22 17:49:24 -0800664 final ActivityStack sourceStack = getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700665 final ActivityStack toStack = supervisor.getReparentTargetStack(this, preferredStack,
Winson Chung74666102017-02-22 17:49:24 -0800666 position == MAX_VALUE);
667 if (toStack == sourceStack) {
668 return false;
669 }
Andrii Kulianb850ea52017-12-12 23:49:10 -0800670 if (!canBeLaunchedOnDisplay(toStack.mDisplayId)) {
671 return false;
672 }
Winson Chung74666102017-02-22 17:49:24 -0800673
Andrii Kulian6b321512019-01-23 06:37:00 +0000674 final boolean toTopOfStack = position == MAX_VALUE;
675 if (toTopOfStack && toStack.getResumedActivity() != null
676 && toStack.topRunningActivityLocked() != null) {
677 // Pause the resumed activity on the target stack while re-parenting task on top of it.
678 toStack.startPausingLocked(false /* userLeaving */, false /* uiSleeping */,
Louis Chang7b03ad92019-08-21 12:32:33 +0800679 null /* resuming */);
Andrii Kulian6b321512019-01-23 06:37:00 +0000680 }
681
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700682 final int toStackWindowingMode = toStack.getWindowingMode();
Winson Chung74666102017-02-22 17:49:24 -0800683 final ActivityRecord topActivity = getTopActivity();
684
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800685 final boolean mightReplaceWindow = topActivity != null
686 && replaceWindowsOnTaskMove(getWindowingMode(), toStackWindowingMode);
Winson Chung74666102017-02-22 17:49:24 -0800687 if (mightReplaceWindow) {
688 // We are about to relaunch the activity because its configuration changed due to
689 // being maximized, i.e. size change. The activity will first remove the old window
690 // and then add a new one. This call will tell window manager about this, so it can
691 // preserve the old window until the new one is drawn. This prevents having a gap
692 // between the removal and addition, in which no window is visible. We also want the
693 // entrance of the new window to be properly animated.
694 // Note here we always set the replacing window first, as the flags might be needed
695 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
696 windowManager.setWillReplaceWindow(topActivity.appToken, animate);
697 }
698
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700699 mAtmService.deferWindowLayout();
Winson Chung74666102017-02-22 17:49:24 -0800700 boolean kept = true;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800701 try {
Winson Chung74666102017-02-22 17:49:24 -0800702 final ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled32da472018-11-16 07:19:28 -0800703 final boolean wasFocused = r != null && root.isTopDisplayFocusedStack(sourceStack)
Winson Chung74666102017-02-22 17:49:24 -0800704 && (topRunningActivityLocked() == r);
Bryce Leec4ab62a2018-03-05 14:19:26 -0800705 final boolean wasResumed = r != null && sourceStack.getResumedActivity() == r;
Winson Chung95f8f0e2017-03-24 09:20:17 -0700706 final boolean wasPaused = r != null && sourceStack.mPausingActivity == r;
Winson Chung74666102017-02-22 17:49:24 -0800707
708 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
709 // Whenever we are moving the top activity from the front stack we want to make sure to
710 // move the stack to the front.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700711 final boolean wasFront = r != null && sourceStack.isTopStackOnDisplay()
Winson Chung74666102017-02-22 17:49:24 -0800712 && (sourceStack.topRunningActivityLocked() == r);
713
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800714 // Adjust the position for the new parent stack as needed.
Winson Chung74666102017-02-22 17:49:24 -0800715 position = toStack.getAdjustedPositionForTask(this, position, null /* starting */);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800716
717 // Must reparent first in window manager to avoid a situation where AM can delete the
718 // we are coming from in WM before we reparent because it became empty.
Yunfan Chen279f5582018-12-12 15:24:50 -0800719 mTask.reparent(toStack.getTaskStack(), position,
Wale Ogunwale2719cc12017-04-14 09:45:27 -0700720 moveStackMode == REPARENT_MOVE_STACK_TO_FRONT);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800721
Wale Ogunwale56d8d162017-05-30 11:12:20 -0700722 final boolean moveStackToFront = moveStackMode == REPARENT_MOVE_STACK_TO_FRONT
723 || (moveStackMode == REPARENT_KEEP_STACK_AT_FRONT && (wasFocused || wasFront));
Winson Chung74666102017-02-22 17:49:24 -0800724 // Move the task
Wale Ogunwale56d8d162017-05-30 11:12:20 -0700725 sourceStack.removeTask(this, reason, moveStackToFront
726 ? REMOVE_TASK_MODE_MOVING_TO_TOP : REMOVE_TASK_MODE_MOVING);
Winson Chung5af42fc2017-03-24 17:11:33 -0700727 toStack.addTask(this, position, false /* schedulePictureInPictureModeChange */, reason);
Winson Chung74666102017-02-22 17:49:24 -0800728
Winson Chung5af42fc2017-03-24 17:11:33 -0700729 if (schedulePictureInPictureModeChange) {
730 // Notify of picture-in-picture mode changes
731 supervisor.scheduleUpdatePictureInPictureModeIfNeeded(this, sourceStack);
732 }
Winson Chung74666102017-02-22 17:49:24 -0800733
734 // TODO: Ensure that this is actually necessary here
735 // Notify the voice session if required
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800736 if (voiceSession != null) {
737 try {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700738 voiceSession.taskStarted(intent, mTaskId);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800739 } catch (RemoteException e) {
740 }
741 }
Winson Chung74666102017-02-22 17:49:24 -0800742
743 // If the task had focus before (or we're requested to move focus), move focus to the
744 // new stack by moving the stack to the front.
Winson Chung95f8f0e2017-03-24 09:20:17 -0700745 if (r != null) {
746 toStack.moveToFrontAndResumeStateIfNeeded(r, moveStackToFront, wasResumed,
747 wasPaused, reason);
748 }
Winson Chung74666102017-02-22 17:49:24 -0800749 if (!animate) {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700750 mAtmService.mStackSupervisor.mNoAnimActivities.add(topActivity);
Winson Chung74666102017-02-22 17:49:24 -0800751 }
752
753 // We might trigger a configuration change. Save the current task bounds for freezing.
754 // TODO: Should this call be moved inside the resize method in WM?
755 toStack.prepareFreezingTaskBounds();
756
757 // Make sure the task has the appropriate bounds/size for the stack it is in.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700758 final boolean toStackSplitScreenPrimary =
759 toStackWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Evan Roskydfe3da72018-10-26 17:21:06 -0700760 final Rect configBounds = getRequestedOverrideBounds();
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700761 if ((toStackWindowingMode == WINDOWING_MODE_FULLSCREEN
762 || toStackWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY)
Evan Roskydfe3da72018-10-26 17:21:06 -0700763 && !Objects.equals(configBounds, toStack.getRequestedOverrideBounds())) {
764 kept = resize(toStack.getRequestedOverrideBounds(), RESIZE_MODE_SYSTEM,
765 !mightReplaceWindow, deferResume);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700766 } else if (toStackWindowingMode == WINDOWING_MODE_FREEFORM) {
Winson Chung74666102017-02-22 17:49:24 -0800767 Rect bounds = getLaunchBounds();
768 if (bounds == null) {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700769 mAtmService.mStackSupervisor.getLaunchParamsController().layoutTask(this, null);
Bryce Leef3c6a472017-11-14 14:53:06 -0800770 bounds = configBounds;
Winson Chung74666102017-02-22 17:49:24 -0800771 }
772 kept = resize(bounds, RESIZE_MODE_FORCED, !mightReplaceWindow, deferResume);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700773 } else if (toStackSplitScreenPrimary || toStackWindowingMode == WINDOWING_MODE_PINNED) {
774 if (toStackSplitScreenPrimary && moveStackMode == REPARENT_KEEP_STACK_AT_FRONT) {
Matthew Ng330757d2017-02-28 14:19:17 -0800775 // Move recents to front so it is not behind home stack when going into docked
776 // mode
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700777 mAtmService.mStackSupervisor.moveRecentsStackToFront(reason);
Matthew Ng330757d2017-02-28 14:19:17 -0800778 }
Evan Roskydfe3da72018-10-26 17:21:06 -0700779 kept = resize(toStack.getRequestedOverrideBounds(), RESIZE_MODE_SYSTEM,
780 !mightReplaceWindow, deferResume);
Winson Chung74666102017-02-22 17:49:24 -0800781 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800782 } finally {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700783 mAtmService.continueWindowLayout();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800784 }
Winson Chung74666102017-02-22 17:49:24 -0800785
786 if (mightReplaceWindow) {
787 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
788 // window), we need to clear the replace window settings. Otherwise, we schedule a
789 // timeout to remove the old window if the replacing window is not coming in time.
790 windowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
791 }
792
793 if (!deferResume) {
794 // The task might have already been running and its visibility needs to be synchronized
795 // with the visibility of the stack / windows.
Wale Ogunwaled32da472018-11-16 07:19:28 -0800796 root.ensureActivitiesVisible(null, 0, !mightReplaceWindow);
797 root.resumeFocusedStacksTopActivities();
Winson Chung74666102017-02-22 17:49:24 -0800798 }
799
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700800 // TODO: Handle incorrect request to move before the actual move, not after.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700801 supervisor.handleNonResizableTaskIfNeeded(this, preferredStack.getWindowingMode(),
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700802 DEFAULT_DISPLAY, toStack);
Winson Chung74666102017-02-22 17:49:24 -0800803
Winson Chungdff7a732017-12-11 12:17:06 -0800804 return (preferredStack == toStack);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800805 }
806
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700807 /**
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800808 * @return True if the windows of tasks being moved to the target stack from the source stack
809 * should be replaced, meaning that window manager will keep the old window around until the new
810 * is ready.
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700811 */
812 private static boolean replaceWindowsOnTaskMove(
813 int sourceWindowingMode, int targetWindowingMode) {
814 return sourceWindowingMode == WINDOWING_MODE_FREEFORM
815 || targetWindowingMode == WINDOWING_MODE_FREEFORM;
816 }
817
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800818 void cancelWindowTransition() {
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800819 if (mTask == null) {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700820 Slog.w(TAG_WM, "cancelWindowTransition: taskId " + mTaskId + " not found.");
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800821 return;
822 }
823 mTask.cancelTaskWindowTransition();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800824 }
825
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100826 /**
827 * DO NOT HOLD THE ACTIVITY MANAGER LOCK WHEN CALLING THIS METHOD!
828 */
Jorim Jaggi925bb3c2019-06-04 19:51:45 +0200829 TaskSnapshot getSnapshot(boolean reducedResolution, boolean restoreFromDisk) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100830
831 // TODO: Move this to {@link TaskWindowContainerController} once recent tasks are more
832 // synchronized between AM and WM.
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700833 return mAtmService.mWindowManager.getTaskSnapshot(mTaskId, mUserId, reducedResolution,
Jorim Jaggi925bb3c2019-06-04 19:51:45 +0200834 restoreFromDisk);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800835 }
836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 void touchActiveTime() {
Winson Chungfb44d212017-10-04 11:39:10 -0700838 lastActiveTime = SystemClock.elapsedRealtime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 long getInactiveDuration() {
Winson Chungfb44d212017-10-04 11:39:10 -0700842 return SystemClock.elapsedRealtime() - lastActiveTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700844
Winson Chungfee26772014-08-05 12:21:52 -0700845 /** Sets the original intent, and the calling uid and package. */
846 void setIntent(ActivityRecord r) {
Winson Chungfee26772014-08-05 12:21:52 -0700847 mCallingUid = r.launchedFromUid;
848 mCallingPackage = r.launchedFromPackage;
Craig Mautner15df08a2015-04-01 12:17:18 -0700849 setIntent(r.intent, r.info);
Charles He2bf28322017-10-12 22:24:49 +0100850 setLockTaskAuth(r);
Winson Chungfee26772014-08-05 12:21:52 -0700851 }
852
853 /** Sets the original intent, _without_ updating the calling uid or package. */
854 private void setIntent(Intent _intent, ActivityInfo info) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700855 if (intent == null) {
856 mNeverRelinquishIdentity =
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700857 (info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700858 } else if (mNeverRelinquishIdentity) {
859 return;
860 }
861
862 affinity = info.taskAffinity;
Dianne Hackborn79228822014-09-16 11:11:23 -0700863 if (intent == null) {
864 // If this task already has an intent associated with it, don't set the root
865 // affinity -- we don't want it changing after initially set, but the initially
866 // set value may be null.
867 rootAffinity = affinity;
868 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700869 effectiveUid = info.applicationInfo.uid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700870 stringName = null;
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 if (info.targetActivity == null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800873 if (_intent != null) {
874 // If this Intent has a selector, we want to clear it for the
875 // recent task since it is not relevant if the user later wants
876 // to re-launch the app.
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700877 if (_intent.getSelector() != null || _intent.getSourceBounds() != null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800878 _intent = new Intent(_intent);
879 _intent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700880 _intent.setSourceBounds(null);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800881 }
882 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700883 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Setting Intent of " + this + " to " + _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 intent = _intent;
885 realActivity = _intent != null ? _intent.getComponent() : null;
886 origActivity = null;
887 } else {
888 ComponentName targetComponent = new ComponentName(
889 info.packageName, info.targetActivity);
890 if (_intent != null) {
891 Intent targetIntent = new Intent(_intent);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800892 targetIntent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700893 targetIntent.setSourceBounds(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700894 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700895 "Setting Intent of " + this + " to target " + targetIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 intent = targetIntent;
897 realActivity = targetComponent;
898 origActivity = _intent.getComponent();
899 } else {
900 intent = null;
901 realActivity = targetComponent;
902 origActivity = new ComponentName(info.packageName, info.name);
903 }
904 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700905
Craig Mautner47b20ba2014-09-17 17:23:44 -0700906 final int intentFlags = intent == null ? 0 : intent.getFlags();
907 if ((intentFlags & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 // Once we are set to an Intent with this flag, we count this
909 // task as having a true root activity.
910 rootWasReset = true;
911 }
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700912 mUserId = UserHandle.getUserId(info.applicationInfo.uid);
913 mUserSetupComplete = Settings.Secure.getIntForUser(
914 mAtmService.mContext.getContentResolver(), USER_SETUP_COMPLETE, 0, mUserId) != 0;
Craig Mautner41db4a72014-05-07 17:20:56 -0700915 if ((info.flags & ActivityInfo.FLAG_AUTO_REMOVE_FROM_RECENTS) != 0) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700916 // If the activity itself has requested auto-remove, then just always do it.
917 autoRemoveRecents = true;
Wale Ogunwale843bfb92015-03-27 11:06:48 -0700918 } else if ((intentFlags & (FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_RETAIN_IN_RECENTS))
919 == FLAG_ACTIVITY_NEW_DOCUMENT) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700920 // If the caller has not asked for the document to be retained, then we may
921 // want to turn on auto-remove, depending on whether the target has set its
922 // own document launch mode.
923 if (info.documentLaunchMode != ActivityInfo.DOCUMENT_LAUNCH_NONE) {
924 autoRemoveRecents = false;
925 } else {
926 autoRemoveRecents = true;
927 }
928 } else {
929 autoRemoveRecents = false;
Craig Mautner41db4a72014-05-07 17:20:56 -0700930 }
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800931 mResizeMode = info.resizeMode;
Winson Chungd3395382016-12-13 11:49:09 -0800932 mSupportsPictureInPicture = info.supportsPictureInPicture();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800934
Andrii Kulian2e751b82016-03-16 16:59:32 -0700935 /** Sets the original minimal width and height. */
936 private void setMinDimensions(ActivityInfo info) {
937 if (info != null && info.windowLayout != null) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700938 mMinWidth = info.windowLayout.minWidth;
939 mMinHeight = info.windowLayout.minHeight;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700940 } else {
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700941 mMinWidth = INVALID_MIN_SIZE;
942 mMinHeight = INVALID_MIN_SIZE;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700943 }
944 }
945
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800946 /**
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700947 * Return true if the input activity has the same intent filter as the intent this task
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800948 * record is based on (normally the root activity intent).
949 */
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700950 boolean isSameIntentFilter(ActivityRecord r) {
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800951 final Intent intent = new Intent(r.intent);
Louis Chang23df1a62019-01-09 15:10:49 +0800952 // Make sure the component are the same if the input activity has the same real activity
953 // as the one in the task because either one of them could be the alias activity.
954 if (Objects.equals(realActivity, r.mActivityComponent) && this.intent != null) {
955 intent.setComponent(this.intent.getComponent());
956 }
Bryce Lee1a990e52018-04-23 10:54:11 -0700957 return intent.filterEquals(this.intent);
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800958 }
959
Wale Ogunwale66e16852017-10-19 13:35:52 -0700960 boolean returnsToHomeStack() {
961 final int returnHomeFlags = FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME;
Bryce Lee1a990e52018-04-23 10:54:11 -0700962 return intent != null && (intent.getFlags() & returnHomeFlags) == returnHomeFlags;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700963 }
964
Craig Mautnera228ae92014-07-09 05:44:55 -0700965 void setPrevAffiliate(TaskRecord prevAffiliate) {
966 mPrevAffiliate = prevAffiliate;
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700967 mPrevAffiliateTaskId = prevAffiliate == null ? INVALID_TASK_ID : prevAffiliate.mTaskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700968 }
969
970 void setNextAffiliate(TaskRecord nextAffiliate) {
971 mNextAffiliate = nextAffiliate;
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700972 mNextAffiliateTaskId = nextAffiliate == null ? INVALID_TASK_ID : nextAffiliate.mTaskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700973 }
974
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700975 <T extends ActivityStack> T getStack() {
976 return (T) mStack;
Andrii Kulian02b7a832016-10-06 23:11:56 -0700977 }
978
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800979 /**
980 * Must be used for setting parent stack because it performs configuration updates.
981 * Must be called after adding task as a child to the stack.
982 */
Andrii Kulian02b7a832016-10-06 23:11:56 -0700983 void setStack(ActivityStack stack) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800984 if (stack != null && !stack.isInStackLocked(this)) {
985 throw new IllegalStateException("Task must be added as a Stack child first.");
986 }
Bryce Lee84730a02018-04-03 14:10:04 -0700987 final ActivityStack oldStack = mStack;
Andrii Kulian02b7a832016-10-06 23:11:56 -0700988 mStack = stack;
Bryce Lee84730a02018-04-03 14:10:04 -0700989
990 // If the new {@link TaskRecord} is from a different {@link ActivityStack}, remove this
991 // {@link ActivityRecord} from its current {@link ActivityStack}.
992
993 if (oldStack != mStack) {
994 for (int i = getChildCount() - 1; i >= 0; --i) {
995 final ActivityRecord activity = getChildAt(i);
996
997 if (oldStack != null) {
998 oldStack.onActivityRemovedFromStack(activity);
999 }
1000
1001 if (mStack != null) {
1002 stack.onActivityAddedToStack(activity);
1003 }
1004 }
1005 }
1006
Andrii Kulian1779e612016-10-12 21:58:25 -07001007 onParentChanged();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001008 }
1009
1010 /**
1011 * @return Id of current stack, {@link INVALID_STACK_ID} if no stack is set.
1012 */
1013 int getStackId() {
1014 return mStack != null ? mStack.mStackId : INVALID_STACK_ID;
1015 }
1016
Andrii Kulian1779e612016-10-12 21:58:25 -07001017 @Override
1018 protected int getChildCount() {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001019 return mActivities.size();
Andrii Kulian1779e612016-10-12 21:58:25 -07001020 }
1021
1022 @Override
chaviw82a0ba82018-03-15 14:26:29 -07001023 protected ActivityRecord getChildAt(int index) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001024 return mActivities.get(index);
Andrii Kulian1779e612016-10-12 21:58:25 -07001025 }
1026
1027 @Override
1028 protected ConfigurationContainer getParent() {
1029 return mStack;
1030 }
1031
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001032 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001033 protected void onParentChanged() {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001034 super.onParentChanged();
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001035 mAtmService.mRootActivityContainer.updateUIDsPresentOnDisplay();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001036 }
1037
Craig Mautnera228ae92014-07-09 05:44:55 -07001038 // Close up recents linked list.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001039 private void closeRecentsChain() {
Craig Mautnera228ae92014-07-09 05:44:55 -07001040 if (mPrevAffiliate != null) {
1041 mPrevAffiliate.setNextAffiliate(mNextAffiliate);
1042 }
1043 if (mNextAffiliate != null) {
1044 mNextAffiliate.setPrevAffiliate(mPrevAffiliate);
1045 }
1046 setPrevAffiliate(null);
1047 setNextAffiliate(null);
1048 }
1049
Winson Chung740c3ac2014-11-12 16:14:38 -08001050 void removedFromRecents() {
Dianne Hackborn852975d2014-08-22 17:42:43 -07001051 closeRecentsChain();
1052 if (inRecents) {
1053 inRecents = false;
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001054 mAtmService.notifyTaskPersisterLocked(this, false);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001055 }
Jorim Jaggif9084ec2017-01-16 13:16:59 +01001056
Dianne Hackborn68a06332017-11-15 17:54:18 -08001057 clearRootProcess();
1058
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001059 mAtmService.mWindowManager.mTaskSnapshotController.notifyTaskRemovedFromRecents(
1060 mTaskId, mUserId);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001061 }
1062
Craig Mautnera228ae92014-07-09 05:44:55 -07001063 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
1064 closeRecentsChain();
1065 mAffiliatedTaskId = taskToAffiliateWith.mAffiliatedTaskId;
Winson Chungec396d62014-08-06 17:08:00 -07001066 mAffiliatedTaskColor = taskToAffiliateWith.mAffiliatedTaskColor;
Craig Mautnera228ae92014-07-09 05:44:55 -07001067 // Find the end
1068 while (taskToAffiliateWith.mNextAffiliate != null) {
1069 final TaskRecord nextRecents = taskToAffiliateWith.mNextAffiliate;
1070 if (nextRecents.mAffiliatedTaskId != mAffiliatedTaskId) {
1071 Slog.e(TAG, "setTaskToAffiliateWith: nextRecents=" + nextRecents + " affilTaskId="
1072 + nextRecents.mAffiliatedTaskId + " should be " + mAffiliatedTaskId);
1073 if (nextRecents.mPrevAffiliate == taskToAffiliateWith) {
1074 nextRecents.setPrevAffiliate(null);
1075 }
1076 taskToAffiliateWith.setNextAffiliate(null);
1077 break;
1078 }
1079 taskToAffiliateWith = nextRecents;
1080 }
1081 taskToAffiliateWith.setNextAffiliate(this);
1082 setPrevAffiliate(taskToAffiliateWith);
1083 setNextAffiliate(null);
1084 }
1085
Winson Chung1147c402014-05-14 11:05:00 -07001086 /** Returns the intent for the root activity for this task */
1087 Intent getBaseIntent() {
1088 return intent != null ? intent : affinityIntent;
1089 }
1090
Andrii Kulian39f27442019-06-26 19:09:19 -07001091 /** Returns the first non-finishing activity from the bottom. */
Winson Chung3b3f4642014-04-22 10:08:18 -07001092 ActivityRecord getRootActivity() {
Andrii Kulian39f27442019-06-26 19:09:19 -07001093 final int rootActivityIndex = findRootIndex(false /* effectiveRoot */);
1094 if (rootActivityIndex == -1) {
1095 // There are no non-finishing activities in the task.
1096 return null;
Winson Chung3b3f4642014-04-22 10:08:18 -07001097 }
Andrii Kulian39f27442019-06-26 19:09:19 -07001098 return mActivities.get(rootActivityIndex);
Winson Chung3b3f4642014-04-22 10:08:18 -07001099 }
1100
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001101 ActivityRecord getTopActivity() {
Bryce Lee9f6affd2017-09-01 09:18:35 -07001102 return getTopActivity(true /* includeOverlays */);
1103 }
1104
1105 ActivityRecord getTopActivity(boolean includeOverlays) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001106 for (int i = mActivities.size() - 1; i >= 0; --i) {
1107 final ActivityRecord r = mActivities.get(i);
Bryce Lee9f6affd2017-09-01 09:18:35 -07001108 if (r.finishing || (!includeOverlays && r.mTaskOverlay)) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001109 continue;
1110 }
1111 return r;
1112 }
1113 return null;
1114 }
1115
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001116 ActivityRecord topRunningActivityLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001117 if (mStack != null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001118 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1119 ActivityRecord r = mActivities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -07001120 if (!r.finishing && r.okToShowLocked()) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001121 return r;
1122 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001123 }
1124 }
1125 return null;
1126 }
1127
Jorim Jaggi172e99f2017-10-20 14:33:18 +02001128 boolean isVisible() {
1129 for (int i = mActivities.size() - 1; i >= 0; --i) {
1130 final ActivityRecord r = mActivities.get(i);
1131 if (r.visible) {
1132 return true;
1133 }
1134 }
1135 return false;
1136 }
1137
Ricky Waib147fa12019-04-25 16:08:30 +01001138 /**
1139 * Return true if any activities in this task belongs to input uid.
1140 */
1141 boolean containsAppUid(int uid) {
1142 for (int i = mActivities.size() - 1; i >= 0; --i) {
1143 final ActivityRecord r = mActivities.get(i);
1144 if (r.getUid() == uid) {
1145 return true;
1146 }
1147 }
1148 return false;
1149 }
1150
Jorim Jaggiea039a82017-08-02 14:37:49 +02001151 void getAllRunningVisibleActivitiesLocked(ArrayList<ActivityRecord> outActivities) {
1152 if (mStack != null) {
1153 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1154 ActivityRecord r = mActivities.get(activityNdx);
Jorim Jaggi02f1d2f2017-08-04 14:29:16 +02001155 if (!r.finishing && r.okToShowLocked() && r.visibleIgnoringKeyguard) {
Jorim Jaggiea039a82017-08-02 14:37:49 +02001156 outActivities.add(r);
1157 }
1158 }
1159 }
1160 }
1161
Wale Ogunwale3b232392016-05-13 15:37:13 -07001162 ActivityRecord topRunningActivityWithStartingWindowLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001163 if (mStack != null) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07001164 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1165 ActivityRecord r = mActivities.get(activityNdx);
1166 if (r.mStartingWindowState != STARTING_WINDOW_SHOWN
Chong Zhang87761972016-08-22 13:53:24 -07001167 || r.finishing || !r.okToShowLocked()) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07001168 continue;
1169 }
1170 return r;
1171 }
1172 }
1173 return null;
1174 }
1175
Winson Chung61c9e5a2017-10-11 10:39:32 -07001176 /**
1177 * Return the number of running activities, and the number of non-finishing/initializing
1178 * activities in the provided {@param reportOut} respectively.
1179 */
1180 void getNumRunningActivities(TaskActivitiesReport reportOut) {
1181 reportOut.reset();
1182 for (int i = mActivities.size() - 1; i >= 0; --i) {
1183 final ActivityRecord r = mActivities.get(i);
1184 if (r.finishing) {
1185 continue;
1186 }
1187
1188 reportOut.base = r;
1189
1190 // Increment the total number of non-finishing activities
1191 reportOut.numActivities++;
1192
Bryce Lee7ace3952018-02-16 14:34:32 -08001193 if (reportOut.top == null || (reportOut.top.isState(ActivityState.INITIALIZING))) {
Winson Chung61c9e5a2017-10-11 10:39:32 -07001194 reportOut.top = r;
1195 // Reset the number of running activities until we hit the first non-initializing
1196 // activity
1197 reportOut.numRunning = 0;
1198 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001199 if (r.attachedToProcess()) {
Winson Chung61c9e5a2017-10-11 10:39:32 -07001200 // Increment the number of actually running activities
1201 reportOut.numRunning++;
1202 }
1203 }
1204 }
1205
Chong Zhang87761972016-08-22 13:53:24 -07001206 boolean okToShowLocked() {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001207 // NOTE: If {@link TaskRecord#topRunningActivity} return is not null then it is
Chong Zhang87761972016-08-22 13:53:24 -07001208 // okay to show the activity when locked.
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001209 return mAtmService.mStackSupervisor.isCurrentProfileLocked(mUserId)
Chong Zhang87761972016-08-22 13:53:24 -07001210 || topRunningActivityLocked() != null;
1211 }
1212
Craig Mautnerde4ef022013-04-07 19:01:33 -07001213 /**
Craig Mautner3b475fe2013-12-16 15:58:31 -08001214 * Reorder the history stack so that the passed activity is brought to the front.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001215 */
1216 final void moveActivityToFrontLocked(ActivityRecord newTop) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001217 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
1218 "Removing and adding activity " + newTop
1219 + " to stack at top callers=" + Debug.getCallers(4));
Craig Mautnerde4ef022013-04-07 19:01:33 -07001220
Craig Mautnerde4ef022013-04-07 19:01:33 -07001221 mActivities.remove(newTop);
1222 mActivities.add(newTop);
Bryce Leed58d7b32017-09-08 15:55:22 -07001223
1224 // Make sure window manager is aware of the position change.
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08001225 mTask.positionChildAtTop(newTop);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001226 updateEffectiveIntent();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001227 }
1228
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001229 void addActivityToTop(ActivityRecord r) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001230 addActivityAtIndex(mActivities.size(), r);
1231 }
1232
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001233 @Override
Wale Ogunwaleeea34ee92017-08-31 20:07:45 -07001234 /*@WindowConfiguration.ActivityType*/
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001235 public int getActivityType() {
1236 final int applicationType = super.getActivityType();
1237 if (applicationType != ACTIVITY_TYPE_UNDEFINED || mActivities.isEmpty()) {
1238 return applicationType;
1239 }
1240 return mActivities.get(0).getActivityType();
1241 }
1242
Winson Chung30480042017-01-26 10:55:34 -08001243 /**
1244 * Adds an activity {@param r} at the given {@param index}. The activity {@param r} must either
1245 * be in the current task or unparented to any task.
1246 */
Craig Mautner1602ec22013-05-12 10:24:27 -07001247 void addActivityAtIndex(int index, ActivityRecord r) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001248 TaskRecord task = r.getTaskRecord();
Bryce Leeaf691c02017-03-20 14:20:22 -07001249 if (task != null && task != this) {
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001250 throw new IllegalArgumentException("Can not add r=" + " to task=" + this
Bryce Leeaf691c02017-03-20 14:20:22 -07001251 + " current parent=" + task);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001252 }
Bryce Leeaf691c02017-03-20 14:20:22 -07001253
1254 r.setTask(this);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001255
Craig Mautner6170f732013-04-02 13:05:23 -07001256 // Remove r first, and if it wasn't already in the list and it's fullscreen, count it.
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08001257 if (!mActivities.remove(r) && r.occludesParent()) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001258 // Was not previously in list.
1259 numFullscreen++;
1260 }
Craig Mautner2c1faed2013-07-23 12:56:02 -07001261 // Only set this based on the first activity
1262 if (mActivities.isEmpty()) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001263 if (r.getActivityType() == ACTIVITY_TYPE_UNDEFINED) {
1264 // Normally non-standard activity type for the activity record will be set when the
1265 // object is created, however we delay setting the standard application type until
1266 // this point so that the task can set the type for additional activities added in
1267 // the else condition below.
1268 r.setActivityType(ACTIVITY_TYPE_STANDARD);
1269 }
1270 setActivityType(r.getActivityType());
Craig Mautner21d24a22014-04-23 11:45:37 -07001271 isPersistable = r.isPersistable();
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001272 mCallingUid = r.launchedFromUid;
1273 mCallingPackage = r.launchedFromPackage;
Dianne Hackborn852975d2014-08-22 17:42:43 -07001274 // Clamp to [1, max].
1275 maxRecents = Math.min(Math.max(r.info.maxRecents, 1),
Wale Ogunwale65ebd952018-04-25 15:41:44 -07001276 ActivityTaskManager.getMaxAppRecentsLimitStatic());
Craig Mautner2c1faed2013-07-23 12:56:02 -07001277 } else {
1278 // Otherwise make all added activities match this one.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001279 r.setActivityType(getActivityType());
Craig Mautner78733002013-06-10 13:54:49 -07001280 }
Wale Ogunwale3b232392016-05-13 15:37:13 -07001281
1282 final int size = mActivities.size();
1283
1284 if (index == size && size > 0) {
1285 final ActivityRecord top = mActivities.get(size - 1);
1286 if (top.mTaskOverlay) {
1287 // Place below the task overlay activity since the overlay activity should always
1288 // be on top.
1289 index--;
1290 }
1291 }
1292
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001293 index = Math.min(size, index);
Craig Mautner77878772013-03-04 19:46:24 -08001294 mActivities.add(index, r);
Bryce Lee84730a02018-04-03 14:10:04 -07001295
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001296 updateEffectiveIntent();
Craig Mautner21d24a22014-04-23 11:45:37 -07001297 if (r.isPersistable()) {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001298 mAtmService.notifyTaskPersisterLocked(this, false);
Craig Mautner21d24a22014-04-23 11:45:37 -07001299 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001300
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08001301 if (r.getParent() != null) {
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001302 // Only attempt to move in WM if the child has a controller. It is possible we haven't
1303 // created controller for the activity we are starting yet.
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08001304 mTask.positionChildAt(r, index);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001305 }
David Stevens82ea6cb2017-03-03 16:18:50 -08001306
1307 // Make sure the list of display UID whitelists is updated
1308 // now that this record is in a new task.
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001309 mAtmService.mRootActivityContainer.updateUIDsPresentOnDisplay();
Craig Mautner77878772013-03-04 19:46:24 -08001310 }
1311
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001312 /**
Bryce Leeaf691c02017-03-20 14:20:22 -07001313 * Removes the specified activity from this task.
1314 * @param r The {@link ActivityRecord} to remove.
1315 * @return true if this was the last activity in the task.
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001316 */
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001317 boolean removeActivity(ActivityRecord r) {
Bryce Lee84730a02018-04-03 14:10:04 -07001318 return removeActivity(r, false /* reparenting */);
Bryce Leeaf691c02017-03-20 14:20:22 -07001319 }
1320
1321 boolean removeActivity(ActivityRecord r, boolean reparenting) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001322 if (r.getTaskRecord() != this) {
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001323 throw new IllegalArgumentException(
1324 "Activity=" + r + " does not belong to task=" + this);
1325 }
1326
Bryce Lee84730a02018-04-03 14:10:04 -07001327 r.setTask(null /* task */, reparenting /* reparenting */);
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001328
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08001329 if (mActivities.remove(r) && r.occludesParent()) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001330 // Was previously in list.
1331 numFullscreen--;
1332 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001333 if (r.isPersistable()) {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001334 mAtmService.notifyTaskPersisterLocked(this, false);
Craig Mautner21d24a22014-04-23 11:45:37 -07001335 }
Wale Ogunwale89182d52016-03-11 10:38:36 -08001336
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001337 if (inPinnedWindowingMode()) {
Wale Ogunwale89182d52016-03-11 10:38:36 -08001338 // We normally notify listeners of task stack changes on pause, however pinned stack
1339 // activities are normally in the paused state so no notification will be sent there
1340 // before the activity is removed. We send it here so instead.
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001341 mAtmService.getTaskChangeNotificationController().notifyTaskStackChanged();
Wale Ogunwale89182d52016-03-11 10:38:36 -08001342 }
1343
Craig Mautner41326202014-06-20 14:38:21 -07001344 if (mActivities.isEmpty()) {
Craig Mautner5afcd4d2014-06-21 18:11:33 -07001345 return !mReuseTask;
Craig Mautner41326202014-06-20 14:38:21 -07001346 }
1347 updateEffectiveIntent();
1348 return false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001349 }
1350
Winson Chung6954fc92017-03-24 16:22:12 -07001351 /**
1352 * @return whether or not there are ONLY task overlay activities in the stack.
1353 * If {@param excludeFinishing} is set, then ignore finishing activities in the check.
1354 * If there are no task overlay activities, this call returns false.
1355 */
1356 boolean onlyHasTaskOverlayActivities(boolean excludeFinishing) {
1357 int count = 0;
1358 for (int i = mActivities.size() - 1; i >= 0; i--) {
1359 final ActivityRecord r = mActivities.get(i);
1360 if (excludeFinishing && r.finishing) {
1361 continue;
1362 }
1363 if (!r.mTaskOverlay) {
1364 return false;
1365 }
1366 count++;
1367 }
1368 return count > 0;
1369 }
1370
Craig Mautner41db4a72014-05-07 17:20:56 -07001371 boolean autoRemoveFromRecents() {
Dianne Hackbornd38aed82014-06-10 21:36:35 -07001372 // We will automatically remove the task either if it has explicitly asked for
1373 // this, or it is empty and has never contained an activity that got shown to
1374 // the user.
Dianne Hackborn13420f22014-07-18 15:43:56 -07001375 return autoRemoveRecents || (mActivities.isEmpty() && !hasBeenVisible);
Craig Mautner41db4a72014-05-07 17:20:56 -07001376 }
1377
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001378 /**
1379 * Completely remove all activities associated with an existing
1380 * task starting at a specified index.
1381 */
Louis Chang7b03ad92019-08-21 12:32:33 +08001382 final void performClearTaskAtIndexLocked(int activityNdx, String reason) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001383 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001384 for ( ; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001385 final ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001386 if (r.finishing) {
1387 continue;
1388 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001389 if (mStack == null) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001390 // Task was restored from persistent storage.
1391 r.takeFromHistory();
1392 mActivities.remove(activityNdx);
1393 --activityNdx;
1394 --numActivities;
Louis Chang7b03ad92019-08-21 12:32:33 +08001395 } else if (r.finishIfPossible(Activity.RESULT_CANCELED, null /* resultData */, reason,
1396 false /* oomAdj */)
Andrii Kulian7dd39bb2019-07-22 13:11:10 -07001397 == FINISH_RESULT_REMOVED) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001398 --activityNdx;
1399 --numActivities;
1400 }
1401 }
1402 }
1403
1404 /**
1405 * Completely remove all activities associated with an existing task.
1406 */
Benjamin Franza83859f2017-07-03 16:34:14 +01001407 void performClearTaskLocked() {
Craig Mautner362449a2014-06-20 14:04:39 -07001408 mReuseTask = true;
Louis Chang7b03ad92019-08-21 12:32:33 +08001409 performClearTaskAtIndexLocked(0, "clear-task-all");
Craig Mautner362449a2014-06-20 14:04:39 -07001410 mReuseTask = false;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001411 }
1412
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -08001413 ActivityRecord performClearTaskForReuseLocked(ActivityRecord newR, int launchFlags) {
1414 mReuseTask = true;
1415 final ActivityRecord result = performClearTaskLocked(newR, launchFlags);
1416 mReuseTask = false;
1417 return result;
1418 }
1419
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001420 /**
1421 * Perform clear operation as requested by
1422 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
1423 * stack to the given task, then look for
1424 * an instance of that activity in the stack and, if found, finish all
1425 * activities on top of it and return the instance.
1426 *
1427 * @param newR Description of the new activity being started.
1428 * @return Returns the old activity that should be continued to be used,
1429 * or null if none was found.
1430 */
1431 final ActivityRecord performClearTaskLocked(ActivityRecord newR, int launchFlags) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001432 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001433 for (int activityNdx = numActivities - 1; activityNdx >= 0; --activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001434 ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001435 if (r.finishing) {
1436 continue;
1437 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001438 if (r.mActivityComponent.equals(newR.mActivityComponent)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001439 // Here it is! Now finish everything in front...
Craig Mautner1602ec22013-05-12 10:24:27 -07001440 final ActivityRecord ret = r;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001441
1442 for (++activityNdx; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001443 r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001444 if (r.finishing) {
1445 continue;
1446 }
Jorim Jaggi346702a2019-05-08 17:49:33 +02001447 ActivityOptions opts = r.takeOptionsLocked(false /* fromClient */);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001448 if (opts != null) {
1449 ret.updateOptionsLocked(opts);
1450 }
Andrii Kulian7dd39bb2019-07-22 13:11:10 -07001451 if (r.finishIfPossible("clear-task-stack", false /* oomAdj */)
1452 == FINISH_RESULT_REMOVED) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001453 --activityNdx;
1454 --numActivities;
1455 }
1456 }
1457
1458 // Finally, if this is a normal launch mode (that is, not
1459 // expecting onNewIntent()), then we will finish the current
1460 // instance of the activity so a new fresh one can be started.
1461 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
Daichi Hirono15a02992016-04-27 18:47:01 +09001462 && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0
1463 && !ActivityStarter.isDocumentLaunchesIntoExisting(launchFlags)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001464 if (!ret.finishing) {
Andrii Kulian7dd39bb2019-07-22 13:11:10 -07001465 ret.finishIfPossible("clear-task-top", false /* oomAdj */);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001466 return null;
1467 }
1468 }
1469
1470 return ret;
1471 }
1472 }
1473
1474 return null;
1475 }
1476
Louis Chang7b03ad92019-08-21 12:32:33 +08001477 void removeTaskActivitiesLocked(String reason) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001478 // Just remove the entire task.
Louis Chang7b03ad92019-08-21 12:32:33 +08001479 performClearTaskAtIndexLocked(0, reason);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07001480 }
1481
Craig Mautner432f64e2015-05-20 14:59:57 -07001482 String lockTaskAuthToString() {
1483 switch (mLockTaskAuth) {
1484 case LOCK_TASK_AUTH_DONT_LOCK: return "LOCK_TASK_AUTH_DONT_LOCK";
1485 case LOCK_TASK_AUTH_PINNABLE: return "LOCK_TASK_AUTH_PINNABLE";
1486 case LOCK_TASK_AUTH_LAUNCHABLE: return "LOCK_TASK_AUTH_LAUNCHABLE";
1487 case LOCK_TASK_AUTH_WHITELISTED: return "LOCK_TASK_AUTH_WHITELISTED";
Benjamin Franz469dd582015-06-09 14:24:36 +01001488 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV: return "LOCK_TASK_AUTH_LAUNCHABLE_PRIV";
Craig Mautner432f64e2015-05-20 14:59:57 -07001489 default: return "unknown=" + mLockTaskAuth;
1490 }
1491 }
1492
Craig Mautner15df08a2015-04-01 12:17:18 -07001493 void setLockTaskAuth() {
Charles He2bf28322017-10-12 22:24:49 +01001494 setLockTaskAuth(getRootActivity());
1495 }
1496
1497 private void setLockTaskAuth(@Nullable ActivityRecord r) {
1498 if (r == null) {
1499 mLockTaskAuth = LOCK_TASK_AUTH_PINNABLE;
1500 return;
1501 }
1502
Charles He520b2832017-09-02 15:27:16 +01001503 final String pkg = (realActivity != null) ? realActivity.getPackageName() : null;
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001504 final LockTaskController lockTaskController = mAtmService.getLockTaskController();
Charles He2bf28322017-10-12 22:24:49 +01001505 switch (r.lockTaskLaunchMode) {
Craig Mautner15df08a2015-04-01 12:17:18 -07001506 case LOCK_TASK_LAUNCH_MODE_DEFAULT:
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001507 mLockTaskAuth = lockTaskController.isPackageWhitelisted(mUserId, pkg)
Charles He520b2832017-09-02 15:27:16 +01001508 ? LOCK_TASK_AUTH_WHITELISTED : LOCK_TASK_AUTH_PINNABLE;
Craig Mautner15df08a2015-04-01 12:17:18 -07001509 break;
1510
1511 case LOCK_TASK_LAUNCH_MODE_NEVER:
Benjamin Franz469dd582015-06-09 14:24:36 +01001512 mLockTaskAuth = LOCK_TASK_AUTH_DONT_LOCK;
Craig Mautner15df08a2015-04-01 12:17:18 -07001513 break;
1514
1515 case LOCK_TASK_LAUNCH_MODE_ALWAYS:
Benjamin Franz469dd582015-06-09 14:24:36 +01001516 mLockTaskAuth = LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -07001517 break;
1518
1519 case LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED:
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001520 mLockTaskAuth = lockTaskController.isPackageWhitelisted(mUserId, pkg)
Charles He520b2832017-09-02 15:27:16 +01001521 ? LOCK_TASK_AUTH_LAUNCHABLE : LOCK_TASK_AUTH_PINNABLE;
Craig Mautner15df08a2015-04-01 12:17:18 -07001522 break;
1523 }
Craig Mautner432f64e2015-05-20 14:59:57 -07001524 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "setLockTaskAuth: task=" + this +
1525 " mLockTaskAuth=" + lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07001526 }
1527
Winson Chungd3395382016-12-13 11:49:09 -08001528 private boolean isResizeable(boolean checkSupportsPip) {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001529 return (mAtmService.mForceResizableActivities || ActivityInfo.isResizeableMode(mResizeMode)
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001530 || (checkSupportsPip && mSupportsPictureInPicture));
Winson Chungd3395382016-12-13 11:49:09 -08001531 }
1532
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001533 boolean isResizeable() {
Winson Chungd3395382016-12-13 11:49:09 -08001534 return isResizeable(true /* checkSupportsPip */);
1535 }
1536
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001537 @Override
1538 public boolean supportsSplitScreenWindowingMode() {
Winson Chungd3395382016-12-13 11:49:09 -08001539 // A task can not be docked even if it is considered resizeable because it only supports
1540 // picture-in-picture mode but has a non-resizeable resizeMode
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001541 return super.supportsSplitScreenWindowingMode()
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001542 && mAtmService.mSupportsSplitScreenMultiWindow
1543 && (mAtmService.mForceResizableActivities
Bryce Leec857a5b2017-08-16 10:04:52 -07001544 || (isResizeable(false /* checkSupportsPip */)
1545 && !ActivityInfo.isPreserveOrientationMode(mResizeMode)));
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001546 }
1547
skuhne@google.com322347b2016-12-02 12:54:03 -08001548 /**
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001549 * Check whether this task can be launched on the specified display.
Riddle Hsu16567132018-08-16 21:37:47 +08001550 *
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001551 * @param displayId Target display id.
Riddle Hsu16567132018-08-16 21:37:47 +08001552 * @return {@code true} if either it is the default display or this activity can be put on a
1553 * secondary display.
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001554 */
1555 boolean canBeLaunchedOnDisplay(int displayId) {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001556 return mAtmService.mStackSupervisor.canPlaceEntityOnDisplay(displayId,
Riddle Hsu16567132018-08-16 21:37:47 +08001557 -1 /* don't check PID */, -1 /* don't check UID */, null /* activityInfo */);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001558 }
1559
1560 /**
skuhne@google.com322347b2016-12-02 12:54:03 -08001561 * Check that a given bounds matches the application requested orientation.
1562 *
1563 * @param bounds The bounds to be tested.
1564 * @return True if the requested bounds are okay for a resizing request.
1565 */
Wale Ogunwale069bbd32017-02-03 07:58:14 -08001566 private boolean canResizeToBounds(Rect bounds) {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001567 if (bounds == null || !inFreeformWindowingMode()) {
skuhne@google.com322347b2016-12-02 12:54:03 -08001568 // Note: If not on the freeform workspace, we ignore the bounds.
1569 return true;
1570 }
1571 final boolean landscape = bounds.width() > bounds.height();
Evan Roskydfe3da72018-10-26 17:21:06 -07001572 final Rect configBounds = getRequestedOverrideBounds();
skuhne@google.com322347b2016-12-02 12:54:03 -08001573 if (mResizeMode == RESIZE_MODE_FORCE_RESIZABLE_PRESERVE_ORIENTATION) {
Bryce Leef3c6a472017-11-14 14:53:06 -08001574 return configBounds.isEmpty()
1575 || landscape == (configBounds.width() > configBounds.height());
skuhne@google.com322347b2016-12-02 12:54:03 -08001576 }
1577 return (mResizeMode != RESIZE_MODE_FORCE_RESIZABLE_PORTRAIT_ONLY || !landscape)
1578 && (mResizeMode != RESIZE_MODE_FORCE_RESIZABLE_LANDSCAPE_ONLY || landscape);
1579 }
1580
Craig Mautner525f3d92013-05-07 14:01:50 -07001581 /**
Yorke Leebdef5372017-04-10 16:38:51 -07001582 * @return {@code true} if the task is being cleared for the purposes of being reused.
1583 */
1584 boolean isClearingToReuseTask() {
1585 return mReuseTask;
1586 }
1587
1588 /**
Craig Mautner525f3d92013-05-07 14:01:50 -07001589 * Find the activity in the history stack within the given task. Returns
1590 * the index within the history at which it's found, or < 0 if not found.
1591 */
1592 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001593 final ComponentName realActivity = r.mActivityComponent;
Craig Mautner525f3d92013-05-07 14:01:50 -07001594 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1595 ActivityRecord candidate = mActivities.get(activityNdx);
1596 if (candidate.finishing) {
1597 continue;
1598 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001599 if (candidate.mActivityComponent.equals(realActivity)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001600 return candidate;
1601 }
1602 }
1603 return null;
1604 }
1605
Winson Chunga449dc02014-05-16 11:15:04 -07001606 /** Updates the last task description values. */
1607 void updateTaskDescription() {
Andrii Kulian39f27442019-06-26 19:09:19 -07001608 // TODO(AM refactor): Cleanup to use findRootIndex()
Winson Chunga449dc02014-05-16 11:15:04 -07001609 // Traverse upwards looking for any break between main task activities and
1610 // utility activities.
1611 int activityNdx;
1612 final int numActivities = mActivities.size();
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001613 final boolean relinquish = numActivities != 0 &&
1614 (mActivities.get(0).info.flags & FLAG_RELINQUISH_TASK_IDENTITY) != 0;
Winson Chunga449dc02014-05-16 11:15:04 -07001615 for (activityNdx = Math.min(numActivities, 1); activityNdx < numActivities;
Craig Mautner21d24a22014-04-23 11:45:37 -07001616 ++activityNdx) {
Winson Chunga449dc02014-05-16 11:15:04 -07001617 final ActivityRecord r = mActivities.get(activityNdx);
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001618 if (relinquish && (r.info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001619 // This will be the top activity for determining taskDescription. Pre-inc to
1620 // overcome initial decrement below.
1621 ++activityNdx;
1622 break;
1623 }
Winson Chunga449dc02014-05-16 11:15:04 -07001624 if (r.intent != null &&
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001625 (r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Winson Chunga449dc02014-05-16 11:15:04 -07001626 break;
1627 }
1628 }
1629 if (activityNdx > 0) {
1630 // Traverse downwards starting below break looking for set label, icon.
1631 // Note that if there are activities in the task but none of them set the
1632 // recent activity values, then we do not fall back to the last set
1633 // values in the TaskRecord.
1634 String label = null;
Craig Mautner648f69b2014-09-18 14:16:26 -07001635 String iconFilename = null;
Matthew Ng54bc9422017-10-02 17:16:28 -07001636 int iconResource = -1;
Winson Chunga449dc02014-05-16 11:15:04 -07001637 int colorPrimary = 0;
Winson Chung1af8eda2016-02-05 17:55:56 +00001638 int colorBackground = 0;
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001639 int statusBarColor = 0;
1640 int navigationBarColor = 0;
Adrian Roos4c864592019-04-10 14:47:57 +02001641 boolean statusBarContrastWhenTransparent = false;
1642 boolean navigationBarContrastWhenTransparent = false;
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001643 boolean topActivity = true;
Winson Chunga449dc02014-05-16 11:15:04 -07001644 for (--activityNdx; activityNdx >= 0; --activityNdx) {
1645 final ActivityRecord r = mActivities.get(activityNdx);
Winson Chung80f80db2018-05-30 21:13:25 -07001646 if (r.mTaskOverlay) {
1647 continue;
1648 }
Winson Chunga449dc02014-05-16 11:15:04 -07001649 if (r.taskDescription != null) {
1650 if (label == null) {
1651 label = r.taskDescription.getLabel();
1652 }
Matthew Ng54bc9422017-10-02 17:16:28 -07001653 if (iconResource == -1) {
1654 iconResource = r.taskDescription.getIconResource();
1655 }
Craig Mautner648f69b2014-09-18 14:16:26 -07001656 if (iconFilename == null) {
1657 iconFilename = r.taskDescription.getIconFilename();
Winson Chunga449dc02014-05-16 11:15:04 -07001658 }
1659 if (colorPrimary == 0) {
1660 colorPrimary = r.taskDescription.getPrimaryColor();
Winson Chunga449dc02014-05-16 11:15:04 -07001661 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001662 if (topActivity) {
Winson Chung1af8eda2016-02-05 17:55:56 +00001663 colorBackground = r.taskDescription.getBackgroundColor();
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001664 statusBarColor = r.taskDescription.getStatusBarColor();
1665 navigationBarColor = r.taskDescription.getNavigationBarColor();
Adrian Roos4c864592019-04-10 14:47:57 +02001666 statusBarContrastWhenTransparent =
1667 r.taskDescription.getEnsureStatusBarContrastWhenTransparent();
1668 navigationBarContrastWhenTransparent =
1669 r.taskDescription.getEnsureNavigationBarContrastWhenTransparent();
Winson Chung1af8eda2016-02-05 17:55:56 +00001670 }
Winson Chunga449dc02014-05-16 11:15:04 -07001671 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001672 topActivity = false;
Winson Chunga449dc02014-05-16 11:15:04 -07001673 }
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001674 mTaskDescription = new TaskDescription(label, null, iconResource, iconFilename,
Adrian Roos4c864592019-04-10 14:47:57 +02001675 colorPrimary, colorBackground, statusBarColor, navigationBarColor,
1676 statusBarContrastWhenTransparent, navigationBarContrastWhenTransparent);
Yunfan Chen0e7aff92018-12-05 16:35:32 -08001677 if (mTask != null) {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001678 mTask.setTaskDescription(mTaskDescription);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +01001679 }
Winson Chungec396d62014-08-06 17:08:00 -07001680 // Update the task affiliation color if we are the parent of the group
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001681 if (mTaskId == mAffiliatedTaskId) {
1682 mAffiliatedTaskColor = mTaskDescription.getPrimaryColor();
Winson Chungec396d62014-08-06 17:08:00 -07001683 }
Winson Chunga449dc02014-05-16 11:15:04 -07001684 }
1685 }
1686
Andrii Kulian39f27442019-06-26 19:09:19 -07001687 /**
1688 * Find the index of the root activity in the task. It will be the first activity from the
1689 * bottom that is not finishing.
1690 *
1691 * @param effectiveRoot Flag indicating whether 'effective root' should be returned - an
1692 * activity that defines the task identity (its base intent). It's the
1693 * first one that does not have
1694 * {@link ActivityInfo#FLAG_RELINQUISH_TASK_IDENTITY}.
1695 * @return index of the 'root' or 'effective' root in the list of activities, -1 if no eligible
1696 * activity was found.
1697 */
1698 int findRootIndex(boolean effectiveRoot) {
1699 int effectiveNdx = -1;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001700 final int topActivityNdx = mActivities.size() - 1;
Dianne Hackborn39569af2014-09-23 10:56:58 -07001701 for (int activityNdx = 0; activityNdx <= topActivityNdx; ++activityNdx) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001702 final ActivityRecord r = mActivities.get(activityNdx);
1703 if (r.finishing) {
1704 continue;
1705 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001706 effectiveNdx = activityNdx;
Andrii Kulian39f27442019-06-26 19:09:19 -07001707 if (!effectiveRoot || (r.info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001708 break;
1709 }
1710 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001711 return effectiveNdx;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001712 }
1713
Andrii Kulian39f27442019-06-26 19:09:19 -07001714 // TODO (AM refactor): Invoke automatically when there is a change in children
1715 @VisibleForTesting
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001716 void updateEffectiveIntent() {
Andrii Kulian39f27442019-06-26 19:09:19 -07001717 int effectiveRootIndex = findRootIndex(true /* effectiveRoot */);
1718 if (effectiveRootIndex == -1) {
1719 // All activities in the task are either finishing or relinquish task identity.
1720 // But we still want to update the intent, so let's use the bottom activity.
1721 effectiveRootIndex = 0;
1722 }
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001723 final ActivityRecord r = mActivities.get(effectiveRootIndex);
Winson Chungfee26772014-08-05 12:21:52 -07001724 setIntent(r);
Winson Chung8d9009e2017-11-16 15:43:05 -08001725
1726 // Update the task description when the activities change
1727 updateTaskDescription();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001728 }
1729
Evan Rosky730f6e82018-12-03 17:40:11 -08001730 void adjustForMinimalTaskDimensions(Rect bounds, Rect previousBounds) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001731 if (bounds == null) {
1732 return;
1733 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001734 int minWidth = mMinWidth;
1735 int minHeight = mMinHeight;
Robert Carr9c5867d2016-03-10 15:52:46 -08001736 // If the task has no requested minimal size, we'd like to enforce a minimal size
1737 // so that the user can not render the task too small to manipulate. We don't need
1738 // to do this for the pinned stack as the bounds are controlled by the system.
Evan Rosky1ac84462018-11-13 11:25:30 -08001739 if (!inPinnedWindowingMode() && mStack != null) {
Garfield Tan4a48a7f2018-10-02 14:23:55 -07001740 final int defaultMinSizeDp =
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001741 mAtmService.mRootActivityContainer.mDefaultMinSizeOfResizeableTaskDp;
Garfield Tan4a48a7f2018-10-02 14:23:55 -07001742 final ActivityDisplay display =
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001743 mAtmService.mRootActivityContainer.getActivityDisplay(mStack.mDisplayId);
Garfield Tan4a48a7f2018-10-02 14:23:55 -07001744 final float density =
1745 (float) display.getConfiguration().densityDpi / DisplayMetrics.DENSITY_DEFAULT;
1746 final int defaultMinSize = (int) (defaultMinSizeDp * density);
1747
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001748 if (minWidth == INVALID_MIN_SIZE) {
Garfield Tan4a48a7f2018-10-02 14:23:55 -07001749 minWidth = defaultMinSize;
Andrii Kulian2e751b82016-03-16 16:59:32 -07001750 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001751 if (minHeight == INVALID_MIN_SIZE) {
Garfield Tan4a48a7f2018-10-02 14:23:55 -07001752 minHeight = defaultMinSize;
Andrii Kulian2e751b82016-03-16 16:59:32 -07001753 }
Robert Carr9c5867d2016-03-10 15:52:46 -08001754 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001755 final boolean adjustWidth = minWidth > bounds.width();
1756 final boolean adjustHeight = minHeight > bounds.height();
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001757 if (!(adjustWidth || adjustHeight)) {
1758 return;
1759 }
1760
1761 if (adjustWidth) {
Garfield Tan020607d2018-12-17 17:01:58 -08001762 if (!previousBounds.isEmpty() && bounds.right == previousBounds.right) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001763 bounds.left = bounds.right - minWidth;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001764 } else {
1765 // Either left bounds match, or neither match, or the previous bounds were
1766 // fullscreen and we default to keeping left.
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001767 bounds.right = bounds.left + minWidth;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001768 }
1769 }
1770 if (adjustHeight) {
Garfield Tan020607d2018-12-17 17:01:58 -08001771 if (!previousBounds.isEmpty() && bounds.bottom == previousBounds.bottom) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001772 bounds.top = bounds.bottom - minHeight;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001773 } else {
1774 // Either top bounds match, or neither match, or the previous bounds were
1775 // fullscreen and we default to keeping top.
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001776 bounds.bottom = bounds.top + minHeight;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001777 }
1778 }
1779 }
1780
Evan Rosky9ba524e2018-01-03 16:27:56 -08001781 void setLastNonFullscreenBounds(Rect bounds) {
1782 if (mLastNonFullscreenBounds == null) {
1783 mLastNonFullscreenBounds = new Rect(bounds);
1784 } else {
1785 mLastNonFullscreenBounds.set(bounds);
1786 }
1787 }
1788
Jorim Jaggi0a932142016-02-01 17:42:25 -08001789 /**
Bryce Leec4ab62a2018-03-05 14:19:26 -08001790 * This should be called when an child activity changes state. This should only
1791 * be called from
1792 * {@link ActivityRecord#setState(ActivityState, String)} .
1793 * @param record The {@link ActivityRecord} whose state has changed.
1794 * @param state The new state.
1795 * @param reason The reason for the change.
1796 */
1797 void onActivityStateChanged(ActivityRecord record, ActivityState state, String reason) {
1798 final ActivityStack parent = getStack();
1799
1800 if (parent != null) {
1801 parent.onActivityStateChanged(record, state, reason);
1802 }
1803 }
1804
Wale Ogunwaleeb76b762017-11-17 10:08:04 -08001805 @Override
1806 public void onConfigurationChanged(Configuration newParentConfig) {
Evan Rosky730f6e82018-12-03 17:40:11 -08001807 // Check if the new configuration supports persistent bounds (eg. is Freeform) and if so
1808 // restore the last recorded non-fullscreen bounds.
1809 final boolean prevPersistTaskBounds = getWindowConfiguration().persistTaskBounds();
1810 final boolean nextPersistTaskBounds =
1811 getRequestedOverrideConfiguration().windowConfiguration.persistTaskBounds()
1812 || newParentConfig.windowConfiguration.persistTaskBounds();
1813 if (!prevPersistTaskBounds && nextPersistTaskBounds
1814 && mLastNonFullscreenBounds != null && !mLastNonFullscreenBounds.isEmpty()) {
1815 // Bypass onRequestedOverrideConfigurationChanged here to avoid infinite loop.
1816 getRequestedOverrideConfiguration().windowConfiguration
1817 .setBounds(mLastNonFullscreenBounds);
1818 }
1819
Wale Ogunwaleeb76b762017-11-17 10:08:04 -08001820 final boolean wasInMultiWindowMode = inMultiWindowMode();
1821 super.onConfigurationChanged(newParentConfig);
1822 if (wasInMultiWindowMode != inMultiWindowMode()) {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001823 mAtmService.mStackSupervisor.scheduleUpdateMultiWindowMode(this);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001824 }
Evan Rosky730f6e82018-12-03 17:40:11 -08001825
1826 // If the configuration supports persistent bounds (eg. Freeform), keep track of the
1827 // current (non-fullscreen) bounds for persistence.
Evan Rosky1ac84462018-11-13 11:25:30 -08001828 if (getWindowConfiguration().persistTaskBounds()) {
1829 final Rect currentBounds = getRequestedOverrideBounds();
1830 if (!currentBounds.isEmpty()) {
1831 setLastNonFullscreenBounds(currentBounds);
1832 }
1833 }
Wale Ogunwaleeb76b762017-11-17 10:08:04 -08001834 // TODO: Should also take care of Pip mode changes here.
Garfield Tan891146c2018-10-09 12:14:00 -07001835
1836 saveLaunchingStateIfNeeded();
1837 }
1838
1839 /**
1840 * Saves launching state if necessary so that we can launch the activity to its latest state.
1841 * It only saves state if this task has been shown to user and it's in fullscreen or freeform
Garfield Tan3129b852019-06-24 16:51:20 -07001842 * mode on freeform displays.
Garfield Tan891146c2018-10-09 12:14:00 -07001843 */
1844 void saveLaunchingStateIfNeeded() {
1845 if (!hasBeenVisible) {
1846 // Not ever visible to user.
1847 return;
1848 }
1849
1850 final int windowingMode = getWindowingMode();
Garfield Tan3129b852019-06-24 16:51:20 -07001851 if (windowingMode != WINDOWING_MODE_FULLSCREEN
1852 && windowingMode != WINDOWING_MODE_FREEFORM) {
1853 return;
1854 }
1855
1856 // Don't persist state if display isn't in freeform mode. Then the task will be launched
1857 // back to its last state in a freeform display when it's launched in a freeform display
1858 // next time.
1859 if (getWindowConfiguration().getDisplayWindowingMode() != WINDOWING_MODE_FREEFORM) {
Garfield Tan891146c2018-10-09 12:14:00 -07001860 return;
1861 }
1862
1863 // Saves the new state so that we can launch the activity at the same location.
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07001864 mAtmService.mStackSupervisor.mLaunchParamsPersister.saveTask(this);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001865 }
1866
Evan Roskyed6767f2018-10-26 17:21:06 -07001867 /**
Evan Rosky1ac84462018-11-13 11:25:30 -08001868 * Adjust bounds to stay within stack bounds.
1869 *
1870 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
1871 * that keep them unchanged, but be contained within the stack bounds.
1872 *
1873 * @param bounds Bounds to be adjusted.
1874 * @param stackBounds Bounds within which the other bounds should remain.
Evan Rosky60dba2f2019-02-01 10:58:38 -08001875 * @param overlapPxX The amount of px required to be visible in the X dimension.
1876 * @param overlapPxY The amount of px required to be visible in the Y dimension.
Evan Rosky1ac84462018-11-13 11:25:30 -08001877 */
Evan Rosky60dba2f2019-02-01 10:58:38 -08001878 private static void fitWithinBounds(Rect bounds, Rect stackBounds, int overlapPxX,
1879 int overlapPxY) {
Evan Rosky1ac84462018-11-13 11:25:30 -08001880 if (stackBounds == null || stackBounds.isEmpty() || stackBounds.contains(bounds)) {
1881 return;
1882 }
1883
Evan Rosky60dba2f2019-02-01 10:58:38 -08001884 // For each side of the parent (eg. left), check if the opposing side of the window (eg.
1885 // right) is at least overlap pixels away. If less, offset the window by that difference.
1886 int horizontalDiff = 0;
1887 // If window is smaller than overlap, use it's smallest dimension instead
1888 int overlapLR = Math.min(overlapPxX, bounds.width());
1889 if (bounds.right < (stackBounds.left + overlapLR)) {
1890 horizontalDiff = overlapLR - (bounds.right - stackBounds.left);
1891 } else if (bounds.left > (stackBounds.right - overlapLR)) {
1892 horizontalDiff = -(overlapLR - (stackBounds.right - bounds.left));
Evan Rosky1ac84462018-11-13 11:25:30 -08001893 }
Evan Rosky60dba2f2019-02-01 10:58:38 -08001894 int verticalDiff = 0;
1895 int overlapTB = Math.min(overlapPxY, bounds.width());
1896 if (bounds.bottom < (stackBounds.top + overlapTB)) {
1897 verticalDiff = overlapTB - (bounds.bottom - stackBounds.top);
1898 } else if (bounds.top > (stackBounds.bottom - overlapTB)) {
1899 verticalDiff = -(overlapTB - (stackBounds.bottom - bounds.top));
Evan Rosky1ac84462018-11-13 11:25:30 -08001900 }
Evan Rosky60dba2f2019-02-01 10:58:38 -08001901 bounds.offset(horizontalDiff, verticalDiff);
Evan Rosky1ac84462018-11-13 11:25:30 -08001902 }
1903
1904 /**
Evan Roskyed6767f2018-10-26 17:21:06 -07001905 * Displayed bounds are used to set where the task is drawn at any given time. This is
1906 * separate from its actual bounds so that the app doesn't see any meaningful configuration
1907 * changes during transitionary periods.
1908 */
1909 void setDisplayedBounds(Rect bounds) {
1910 if (bounds == null) {
1911 mDisplayedBounds.setEmpty();
1912 } else {
1913 mDisplayedBounds.set(bounds);
1914 }
Yunfan Chen0e7aff92018-12-05 16:35:32 -08001915 if (mTask != null) {
1916 mTask.setOverrideDisplayedBounds(
Evan Roskyed6767f2018-10-26 17:21:06 -07001917 mDisplayedBounds.isEmpty() ? null : mDisplayedBounds);
1918 }
1919 }
1920
1921 /**
1922 * Gets the current overridden displayed bounds. These will be empty if the task is not
1923 * currently overriding where it is displayed.
1924 */
1925 Rect getDisplayedBounds() {
1926 return mDisplayedBounds;
1927 }
1928
1929 /**
1930 * @return {@code true} if this has overridden displayed bounds.
1931 */
1932 boolean hasDisplayedBounds() {
1933 return !mDisplayedBounds.isEmpty();
1934 }
1935
Evan Rosky1ac84462018-11-13 11:25:30 -08001936 /**
1937 * Intersects inOutBounds with intersectBounds-intersectInsets. If inOutBounds is larger than
1938 * intersectBounds on a side, then the respective side will not be intersected.
1939 *
1940 * The assumption is that if inOutBounds is initially larger than intersectBounds, then the
1941 * inset on that side is no-longer applicable. This scenario happens when a task's minimal
1942 * bounds are larger than the provided parent/display bounds.
1943 *
1944 * @param inOutBounds the bounds to intersect.
1945 * @param intersectBounds the bounds to intersect with.
1946 * @param intersectInsets insets to apply to intersectBounds before intersecting.
1947 */
Riddle Hsu74826262019-04-17 14:57:42 +08001948 static void intersectWithInsetsIfFits(
Evan Rosky1ac84462018-11-13 11:25:30 -08001949 Rect inOutBounds, Rect intersectBounds, Rect intersectInsets) {
1950 if (inOutBounds.right <= intersectBounds.right) {
1951 inOutBounds.right =
1952 Math.min(intersectBounds.right - intersectInsets.right, inOutBounds.right);
1953 }
1954 if (inOutBounds.bottom <= intersectBounds.bottom) {
1955 inOutBounds.bottom =
1956 Math.min(intersectBounds.bottom - intersectInsets.bottom, inOutBounds.bottom);
1957 }
1958 if (inOutBounds.left >= intersectBounds.left) {
1959 inOutBounds.left =
1960 Math.max(intersectBounds.left + intersectInsets.left, inOutBounds.left);
1961 }
1962 if (inOutBounds.top >= intersectBounds.top) {
1963 inOutBounds.top =
1964 Math.max(intersectBounds.top + intersectInsets.top, inOutBounds.top);
1965 }
1966 }
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001967
Evan Rosky1ac84462018-11-13 11:25:30 -08001968 /**
1969 * Gets bounds with non-decor and stable insets applied respectively.
1970 *
1971 * If bounds overhangs the display, those edges will not get insets. See
1972 * {@link #intersectWithInsetsIfFits}
1973 *
1974 * @param outNonDecorBounds where to place bounds with non-decor insets applied.
1975 * @param outStableBounds where to place bounds with stable insets applied.
1976 * @param bounds the bounds to inset.
1977 */
1978 private void calculateInsetFrames(Rect outNonDecorBounds, Rect outStableBounds, Rect bounds,
1979 DisplayInfo displayInfo) {
1980 outNonDecorBounds.set(bounds);
1981 outStableBounds.set(bounds);
1982 if (getStack() == null || getStack().getDisplay() == null) {
1983 return;
1984 }
1985 DisplayPolicy policy = getStack().getDisplay().mDisplayContent.getDisplayPolicy();
1986 if (policy == null) {
1987 return;
1988 }
1989 mTmpBounds.set(0, 0, displayInfo.logicalWidth, displayInfo.logicalHeight);
Bryce Lee7566d762017-03-30 09:34:15 -07001990
Riddle Hsu61987bc2019-04-03 13:08:47 +08001991 policy.getNonDecorInsetsLw(displayInfo.rotation, displayInfo.logicalWidth,
1992 displayInfo.logicalHeight, displayInfo.displayCutout, mTmpInsets);
Evan Rosky1ac84462018-11-13 11:25:30 -08001993 intersectWithInsetsIfFits(outNonDecorBounds, mTmpBounds, mTmpInsets);
Riddle Hsu61987bc2019-04-03 13:08:47 +08001994
1995 policy.convertNonDecorInsetsToStableInsets(mTmpInsets, displayInfo.rotation);
1996 intersectWithInsetsIfFits(outStableBounds, mTmpBounds, mTmpInsets);
Evan Rosky1ac84462018-11-13 11:25:30 -08001997 }
1998
1999 /**
2000 * Asks docked-divider controller for the smallestwidthdp given bounds.
2001 * @param bounds bounds to calculate smallestwidthdp for.
2002 */
2003 private int getSmallestScreenWidthDpForDockedBounds(Rect bounds) {
2004 DisplayContent dc = mStack.getDisplay().mDisplayContent;
2005 if (dc != null) {
2006 return dc.getDockedDividerController().getSmallestWidthDpForBounds(bounds);
2007 }
2008 return Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
2009 }
2010
Riddle Hsu0a343c32018-12-21 00:40:48 +08002011 void computeConfigResourceOverrides(@NonNull Configuration inOutConfig,
2012 @NonNull Configuration parentConfig) {
Riddle Hsu61987bc2019-04-03 13:08:47 +08002013 computeConfigResourceOverrides(inOutConfig, parentConfig, null /* compatInsets */);
Riddle Hsu0a343c32018-12-21 00:40:48 +08002014 }
2015
Evan Rosky1ac84462018-11-13 11:25:30 -08002016 /**
2017 * Calculates configuration values used by the client to get resources. This should be run
2018 * using app-facing bounds (bounds unmodified by animations or transient interactions).
2019 *
2020 * This assumes bounds are non-empty/null. For the null-bounds case, the caller is likely
2021 * configuring an "inherit-bounds" window which means that all configuration settings would
2022 * just be inherited from the parent configuration.
2023 **/
Evan Rosky730f6e82018-12-03 17:40:11 -08002024 void computeConfigResourceOverrides(@NonNull Configuration inOutConfig,
Riddle Hsu61987bc2019-04-03 13:08:47 +08002025 @NonNull Configuration parentConfig,
2026 @Nullable ActivityRecord.CompatDisplayInsets compatInsets) {
Evan Rosky1ac84462018-11-13 11:25:30 -08002027 int windowingMode = inOutConfig.windowConfiguration.getWindowingMode();
2028 if (windowingMode == WINDOWING_MODE_UNDEFINED) {
2029 windowingMode = parentConfig.windowConfiguration.getWindowingMode();
Winson Chungbdc646f2017-02-13 12:12:22 -08002030 }
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002031
Evan Rosky1ac84462018-11-13 11:25:30 -08002032 float density = inOutConfig.densityDpi;
2033 if (density == Configuration.DENSITY_DPI_UNDEFINED) {
2034 density = parentConfig.densityDpi;
2035 }
2036 density *= DisplayMetrics.DENSITY_DEFAULT_SCALE;
Winson Chung60c1aba2017-03-14 17:47:42 -07002037
Evan Rosky730f6e82018-12-03 17:40:11 -08002038 final Rect bounds = inOutConfig.windowConfiguration.getBounds();
Evan Rosky1ac84462018-11-13 11:25:30 -08002039 Rect outAppBounds = inOutConfig.windowConfiguration.getAppBounds();
2040 if (outAppBounds == null || outAppBounds.isEmpty()) {
2041 inOutConfig.windowConfiguration.setAppBounds(bounds);
2042 outAppBounds = inOutConfig.windowConfiguration.getAppBounds();
2043 }
Riddle Hsu61987bc2019-04-03 13:08:47 +08002044 // Non-null compatibility insets means the activity prefers to keep its original size, so
2045 // the out bounds doesn't need to be restricted by the parent.
2046 final boolean insideParentBounds = compatInsets == null;
Riddle Hsu0a343c32018-12-21 00:40:48 +08002047 if (insideParentBounds && windowingMode != WINDOWING_MODE_FREEFORM) {
Evan Rosky1ac84462018-11-13 11:25:30 -08002048 final Rect parentAppBounds = parentConfig.windowConfiguration.getAppBounds();
2049 if (parentAppBounds != null && !parentAppBounds.isEmpty()) {
2050 outAppBounds.intersect(parentAppBounds);
2051 }
2052 }
2053
2054 if (inOutConfig.screenWidthDp == Configuration.SCREEN_WIDTH_DP_UNDEFINED
2055 || inOutConfig.screenHeightDp == Configuration.SCREEN_HEIGHT_DP_UNDEFINED) {
Riddle Hsu0a343c32018-12-21 00:40:48 +08002056 if (insideParentBounds && mStack != null) {
Evan Rosky1ac84462018-11-13 11:25:30 -08002057 final DisplayInfo di = new DisplayInfo();
2058 mStack.getDisplay().mDisplay.getDisplayInfo(di);
2059
2060 // For calculating screenWidthDp, screenWidthDp, we use the stable inset screen
2061 // area, i.e. the screen area without the system bars.
2062 // The non decor inset are areas that could never be removed in Honeycomb. See
2063 // {@link WindowManagerPolicy#getNonDecorInsetsLw}.
2064 calculateInsetFrames(mTmpNonDecorBounds, mTmpStableBounds, bounds, di);
2065 } else {
Riddle Hsu61987bc2019-04-03 13:08:47 +08002066 // Apply the given non-decor and stable insets to calculate the corresponding bounds
2067 // for screen size of configuration.
2068 final int rotation = parentConfig.windowConfiguration.getRotation();
2069 if (rotation != ROTATION_UNDEFINED && compatInsets != null) {
Riddle Hsu74826262019-04-17 14:57:42 +08002070 mTmpNonDecorBounds.set(bounds);
2071 mTmpStableBounds.set(bounds);
2072 compatInsets.getDisplayBoundsByRotation(mTmpBounds, rotation);
Riddle Hsu61987bc2019-04-03 13:08:47 +08002073 intersectWithInsetsIfFits(mTmpNonDecorBounds, mTmpBounds,
2074 compatInsets.mNonDecorInsets[rotation]);
2075 intersectWithInsetsIfFits(mTmpStableBounds, mTmpBounds,
2076 compatInsets.mStableInsets[rotation]);
Riddle Hsu74826262019-04-17 14:57:42 +08002077 outAppBounds.set(mTmpNonDecorBounds);
2078 } else {
2079 // Set to app bounds because it excludes decor insets.
2080 mTmpNonDecorBounds.set(outAppBounds);
2081 mTmpStableBounds.set(outAppBounds);
Riddle Hsu61987bc2019-04-03 13:08:47 +08002082 }
Evan Rosky1ac84462018-11-13 11:25:30 -08002083 }
2084
2085 if (inOutConfig.screenWidthDp == Configuration.SCREEN_WIDTH_DP_UNDEFINED) {
Riddle Hsu0a343c32018-12-21 00:40:48 +08002086 final int overrideScreenWidthDp = (int) (mTmpStableBounds.width() / density);
2087 inOutConfig.screenWidthDp = insideParentBounds
2088 ? Math.min(overrideScreenWidthDp, parentConfig.screenWidthDp)
2089 : overrideScreenWidthDp;
Evan Rosky1ac84462018-11-13 11:25:30 -08002090 }
2091 if (inOutConfig.screenHeightDp == Configuration.SCREEN_HEIGHT_DP_UNDEFINED) {
Riddle Hsu0a343c32018-12-21 00:40:48 +08002092 final int overrideScreenHeightDp = (int) (mTmpStableBounds.height() / density);
2093 inOutConfig.screenHeightDp = insideParentBounds
Riddle Hsu88e3c8732019-02-18 19:15:12 +08002094 ? Math.min(overrideScreenHeightDp, parentConfig.screenHeightDp)
Riddle Hsu0a343c32018-12-21 00:40:48 +08002095 : overrideScreenHeightDp;
Evan Rosky1ac84462018-11-13 11:25:30 -08002096 }
2097
2098 if (inOutConfig.smallestScreenWidthDp
2099 == Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
2100 if (WindowConfiguration.isFloating(windowingMode)) {
2101 // For floating tasks, calculate the smallest width from the bounds of the task
2102 inOutConfig.smallestScreenWidthDp = (int) (
2103 Math.min(bounds.width(), bounds.height()) / density);
2104 } else if (WindowConfiguration.isSplitScreenWindowingMode(windowingMode)) {
2105 // Iterating across all screen orientations, and return the minimum of the task
2106 // width taking into account that the bounds might change because the snap
2107 // algorithm snaps to a different value
Evan Rosky730f6e82018-12-03 17:40:11 -08002108 inOutConfig.smallestScreenWidthDp =
2109 getSmallestScreenWidthDpForDockedBounds(bounds);
Evan Rosky1ac84462018-11-13 11:25:30 -08002110 }
2111 // otherwise, it will just inherit
2112 }
2113 }
2114
Evan Rosky730f6e82018-12-03 17:40:11 -08002115 if (inOutConfig.orientation == ORIENTATION_UNDEFINED) {
Evan Rosky1ac84462018-11-13 11:25:30 -08002116 inOutConfig.orientation = (inOutConfig.screenWidthDp <= inOutConfig.screenHeightDp)
Riddle Hsu0a343c32018-12-21 00:40:48 +08002117 ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Evan Rosky1ac84462018-11-13 11:25:30 -08002118 }
2119 if (inOutConfig.screenLayout == Configuration.SCREENLAYOUT_UNDEFINED) {
2120 // For calculating screen layout, we need to use the non-decor inset screen area for the
2121 // calculation for compatibility reasons, i.e. screen area without system bars that
2122 // could never go away in Honeycomb.
2123 final int compatScreenWidthDp = (int) (mTmpNonDecorBounds.width() / density);
2124 final int compatScreenHeightDp = (int) (mTmpNonDecorBounds.height() / density);
2125 // We're only overriding LONG, SIZE and COMPAT parts of screenLayout, so we start
2126 // override calculation with partial default.
2127 // Reducing the screen layout starting from its parent config.
2128 final int sl = parentConfig.screenLayout
2129 & (Configuration.SCREENLAYOUT_LONG_MASK | Configuration.SCREENLAYOUT_SIZE_MASK);
2130 final int longSize = Math.max(compatScreenHeightDp, compatScreenWidthDp);
2131 final int shortSize = Math.min(compatScreenHeightDp, compatScreenWidthDp);
2132 inOutConfig.screenLayout = Configuration.reduceScreenLayout(sl, longSize, shortSize);
2133 }
2134 }
2135
Evan Rosky1ac84462018-11-13 11:25:30 -08002136 @Override
2137 void resolveOverrideConfiguration(Configuration newParentConfig) {
Evan Rosky730f6e82018-12-03 17:40:11 -08002138 mTmpBounds.set(getResolvedOverrideConfiguration().windowConfiguration.getBounds());
2139 super.resolveOverrideConfiguration(newParentConfig);
2140 int windowingMode =
2141 getRequestedOverrideConfiguration().windowConfiguration.getWindowingMode();
2142 if (windowingMode == WINDOWING_MODE_UNDEFINED) {
2143 windowingMode = newParentConfig.windowConfiguration.getWindowingMode();
2144 }
2145 Rect outOverrideBounds =
2146 getResolvedOverrideConfiguration().windowConfiguration.getBounds();
2147
2148 if (windowingMode == WINDOWING_MODE_FULLSCREEN) {
Riddle Hsu74826262019-04-17 14:57:42 +08002149 computeFullscreenBounds(outOverrideBounds, null /* refActivity */,
2150 newParentConfig.windowConfiguration.getBounds(),
2151 newParentConfig.orientation);
Evan Rosky730f6e82018-12-03 17:40:11 -08002152 }
2153
2154 if (outOverrideBounds.isEmpty()) {
2155 // If the task fills the parent, just inherit all the other configs from parent.
2156 return;
2157 }
2158
2159 adjustForMinimalTaskDimensions(outOverrideBounds, mTmpBounds);
2160 if (windowingMode == WINDOWING_MODE_FREEFORM) {
2161 // by policy, make sure the window remains within parent somewhere
Evan Rosky60dba2f2019-02-01 10:58:38 -08002162 final float density =
2163 ((float) newParentConfig.densityDpi) / DisplayMetrics.DENSITY_DEFAULT;
GyeHun Jeon84b30d22019-04-24 14:20:15 -07002164 final Rect parentBounds =
2165 new Rect(newParentConfig.windowConfiguration.getBounds());
2166 final ActivityDisplay display = mStack.getDisplay();
2167 if (display != null && display.mDisplayContent != null) {
2168 // If a freeform window moves below system bar, there is no way to move it again
2169 // by touch. Because its caption is covered by system bar. So we exclude them
2170 // from stack bounds. and then caption will be shown inside stable area.
2171 final Rect stableBounds = new Rect();
2172 display.mDisplayContent.getStableRect(stableBounds);
2173 parentBounds.intersect(stableBounds);
2174 }
2175
2176 fitWithinBounds(outOverrideBounds, parentBounds,
Evan Rosky60dba2f2019-02-01 10:58:38 -08002177 (int) (density * WindowState.MINIMUM_VISIBLE_WIDTH_IN_DP),
2178 (int) (density * WindowState.MINIMUM_VISIBLE_HEIGHT_IN_DP));
GyeHun Jeon84b30d22019-04-24 14:20:15 -07002179
2180 // Prevent to overlap caption with stable insets.
2181 final int offsetTop = parentBounds.top - outOverrideBounds.top;
2182 if (offsetTop > 0) {
2183 outOverrideBounds.offset(0, offsetTop);
2184 }
Evan Rosky730f6e82018-12-03 17:40:11 -08002185 }
2186 computeConfigResourceOverrides(getResolvedOverrideConfiguration(), newParentConfig);
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08002187 }
2188
Riddle Hsu74826262019-04-17 14:57:42 +08002189 /** @see WindowContainer#handlesOrientationChangeFromDescendant */
2190 boolean handlesOrientationChangeFromDescendant() {
2191 return mTask != null && mTask.getParent() != null
2192 && mTask.getParent().handlesOrientationChangeFromDescendant();
2193 }
2194
2195 /**
2196 * Compute bounds (letterbox or pillarbox) for {@link #WINDOWING_MODE_FULLSCREEN} when the
2197 * parent doesn't handle the orientation change and the requested orientation is different from
2198 * the parent.
2199 */
2200 void computeFullscreenBounds(@NonNull Rect outBounds, @Nullable ActivityRecord refActivity,
2201 @NonNull Rect parentBounds, int parentOrientation) {
2202 // In FULLSCREEN mode, always start with empty bounds to indicate "fill parent".
2203 outBounds.setEmpty();
2204 if (handlesOrientationChangeFromDescendant()) {
2205 return;
2206 }
2207 if (refActivity == null) {
2208 // Use the top activity as the reference of orientation. Don't include overlays because
2209 // it is usually not the actual content or just temporarily shown.
2210 // E.g. ForcedResizableInfoActivity.
2211 refActivity = getTopActivity(false /* includeOverlays */);
2212 }
2213
2214 // If the task or the reference activity requires a different orientation (either by
2215 // override or activityInfo), make it fit the available bounds by scaling down its bounds.
2216 final int overrideOrientation = getRequestedOverrideConfiguration().orientation;
2217 final int forcedOrientation =
2218 (overrideOrientation != ORIENTATION_UNDEFINED || refActivity == null)
2219 ? overrideOrientation : refActivity.getRequestedConfigurationOrientation();
2220 if (forcedOrientation == ORIENTATION_UNDEFINED || forcedOrientation == parentOrientation) {
2221 return;
2222 }
2223
2224 final int parentWidth = parentBounds.width();
2225 final int parentHeight = parentBounds.height();
2226 final float aspect = ((float) parentHeight) / parentWidth;
2227 if (forcedOrientation == ORIENTATION_LANDSCAPE) {
2228 final int height = (int) (parentWidth / aspect);
2229 final int top = parentBounds.centerY() - height / 2;
2230 outBounds.set(parentBounds.left, top, parentBounds.right, top + height);
2231 } else {
2232 final int width = (int) (parentHeight * aspect);
2233 final int left = parentBounds.centerX() - width / 2;
2234 outBounds.set(left, parentBounds.top, left + width, parentBounds.bottom);
2235 }
2236 }
2237
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002238 Rect updateOverrideConfigurationFromLaunchBounds() {
Bryce Leef3c6a472017-11-14 14:53:06 -08002239 final Rect bounds = getLaunchBounds();
Evan Roskya4cc3a92019-06-28 13:25:01 -07002240 setBounds(bounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08002241 if (bounds != null && !bounds.isEmpty()) {
2242 // TODO: Review if we actually want to do this - we are setting the launch bounds
2243 // directly here.
Evan Roskydfe3da72018-10-26 17:21:06 -07002244 bounds.set(getRequestedOverrideBounds());
Andrii Kulian73336d812016-03-24 12:56:08 -07002245 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002246 return bounds;
2247 }
2248
Wale Ogunwale935e5022015-11-10 12:36:10 -08002249 /** Updates the task's bounds and override configuration to match what is expected for the
2250 * input stack. */
2251 void updateOverrideConfigurationForStack(ActivityStack inStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002252 if (mStack != null && mStack == inStack) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08002253 return;
2254 }
2255
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002256 if (inStack.inFreeformWindowingMode()) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002257 if (!isResizeable()) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08002258 throw new IllegalArgumentException("Can not position non-resizeable task="
2259 + this + " in stack=" + inStack);
2260 }
Bryce Leef3c6a472017-11-14 14:53:06 -08002261 if (!matchParentBounds()) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08002262 return;
2263 }
2264 if (mLastNonFullscreenBounds != null) {
Evan Roskya4cc3a92019-06-28 13:25:01 -07002265 setBounds(mLastNonFullscreenBounds);
Wale Ogunwale935e5022015-11-10 12:36:10 -08002266 } else {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002267 mAtmService.mStackSupervisor.getLaunchParamsController().layoutTask(this, null);
Wale Ogunwale935e5022015-11-10 12:36:10 -08002268 }
2269 } else {
Evan Roskya4cc3a92019-06-28 13:25:01 -07002270 setBounds(inStack.getRequestedOverrideBounds());
Wale Ogunwale935e5022015-11-10 12:36:10 -08002271 }
2272 }
2273
Wale Ogunwale706ed792015-08-02 10:29:44 -07002274 /** Returns the bounds that should be used to launch this task. */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002275 Rect getLaunchBounds() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002276 if (mStack == null) {
Chong Zhang7d5f5102016-01-13 10:29:24 -08002277 return null;
2278 }
2279
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002280 final int windowingMode = getWindowingMode();
2281 if (!isActivityTypeStandardOrUndefined()
2282 || windowingMode == WINDOWING_MODE_FULLSCREEN
2283 || (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY && !isResizeable())) {
Evan Roskydfe3da72018-10-26 17:21:06 -07002284 return isResizeable() ? mStack.getRequestedOverrideBounds() : null;
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002285 } else if (!getWindowConfiguration().persistTaskBounds()) {
Evan Roskydfe3da72018-10-26 17:21:06 -07002286 return mStack.getRequestedOverrideBounds();
Wale Ogunwale706ed792015-08-02 10:29:44 -07002287 }
2288 return mLastNonFullscreenBounds;
2289 }
2290
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01002291 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
2292 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2293 final ActivityRecord r = mActivities.get(activityNdx);
2294 if (r.visible) {
2295 r.showStartingWindow(null /* prev */, false /* newTask */, taskSwitch);
2296 }
2297 }
2298 }
2299
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002300 void setRootProcess(WindowProcessController proc) {
Dianne Hackborn68a06332017-11-15 17:54:18 -08002301 clearRootProcess();
2302 if (intent != null &&
2303 (intent.getFlags() & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0) {
2304 mRootProcess = proc;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002305 mRootProcess.addRecentTask(this);
Dianne Hackborn68a06332017-11-15 17:54:18 -08002306 }
2307 }
2308
2309 void clearRootProcess() {
2310 if (mRootProcess != null) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002311 mRootProcess.removeRecentTask(this);
Dianne Hackborn68a06332017-11-15 17:54:18 -08002312 mRootProcess = null;
2313 }
2314 }
2315
chaviw82a0ba82018-03-15 14:26:29 -07002316 void clearAllPendingOptions() {
2317 for (int i = getChildCount() - 1; i >= 0; i--) {
2318 getChildAt(i).clearOptionsLocked(false /* withAbort */);
2319 }
2320 }
2321
Winson Chungabfdcce2018-07-02 17:23:33 -07002322 /**
2323 * Fills in a {@link TaskInfo} with information from this task.
2324 * @param info the {@link TaskInfo} to fill in
Winson Chungabfdcce2018-07-02 17:23:33 -07002325 */
Mark Renoufc808f062019-02-07 15:20:37 -05002326 void fillTaskInfo(TaskInfo info) {
2327 getNumRunningActivities(mReuseActivitiesReport);
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002328 info.userId = mUserId;
Winson Chungabfdcce2018-07-02 17:23:33 -07002329 info.stackId = getStackId();
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002330 info.taskId = mTaskId;
Mark Renoufb1abb552019-02-08 13:51:41 -05002331 info.displayId = mStack == null ? Display.INVALID_DISPLAY : mStack.mDisplayId;
Winson Chungabfdcce2018-07-02 17:23:33 -07002332 info.isRunning = getTopActivity() != null;
Riddle Hsu2f9acd22018-11-06 23:44:43 +08002333 info.baseIntent = new Intent(getBaseIntent());
Mark Renoufc808f062019-02-07 15:20:37 -05002334 info.baseActivity = mReuseActivitiesReport.base != null
2335 ? mReuseActivitiesReport.base.intent.getComponent()
Winson Chungabfdcce2018-07-02 17:23:33 -07002336 : null;
Mark Renoufc808f062019-02-07 15:20:37 -05002337 info.topActivity = mReuseActivitiesReport.top != null
2338 ? mReuseActivitiesReport.top.mActivityComponent
Winson Chungabfdcce2018-07-02 17:23:33 -07002339 : null;
2340 info.origActivity = origActivity;
2341 info.realActivity = realActivity;
Mark Renoufc808f062019-02-07 15:20:37 -05002342 info.numActivities = mReuseActivitiesReport.numActivities;
Winson Chungabfdcce2018-07-02 17:23:33 -07002343 info.lastActiveTime = lastActiveTime;
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002344 info.taskDescription = new ActivityManager.TaskDescription(mTaskDescription);
Winson Chungabfdcce2018-07-02 17:23:33 -07002345 info.supportsSplitScreenMultiWindow = supportsSplitScreenWindowingMode();
2346 info.resizeMode = mResizeMode;
2347 info.configuration.setTo(getConfiguration());
2348 }
2349
Mark Renoufc808f062019-02-07 15:20:37 -05002350 /**
2351 * Returns a {@link TaskInfo} with information from this task.
2352 */
2353 ActivityManager.RunningTaskInfo getTaskInfo() {
2354 ActivityManager.RunningTaskInfo info = new ActivityManager.RunningTaskInfo();
2355 fillTaskInfo(info);
2356 return info;
2357 }
2358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 void dump(PrintWriter pw, String prefix) {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002360 pw.print(prefix); pw.print("userId="); pw.print(mUserId);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07002361 pw.print(" effectiveUid="); UserHandle.formatUid(pw, effectiveUid);
2362 pw.print(" mCallingUid="); UserHandle.formatUid(pw, mCallingUid);
Suprabh Shukla7745c142016-03-07 18:21:10 -08002363 pw.print(" mUserSetupComplete="); pw.print(mUserSetupComplete);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002364 pw.print(" mCallingPackage="); pw.println(mCallingPackage);
Dianne Hackborn79228822014-09-16 11:11:23 -07002365 if (affinity != null || rootAffinity != null) {
2366 pw.print(prefix); pw.print("affinity="); pw.print(affinity);
2367 if (affinity == null || !affinity.equals(rootAffinity)) {
2368 pw.print(" root="); pw.println(rootAffinity);
2369 } else {
2370 pw.println();
2371 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002372 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002373 if (voiceSession != null || voiceInteractor != null) {
2374 pw.print(prefix); pw.print("VOICE: session=0x");
2375 pw.print(Integer.toHexString(System.identityHashCode(voiceSession)));
2376 pw.print(" interactor=0x");
2377 pw.println(Integer.toHexString(System.identityHashCode(voiceInteractor)));
2378 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002379 if (intent != null) {
2380 StringBuilder sb = new StringBuilder(128);
2381 sb.append(prefix); sb.append("intent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08002382 intent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002383 sb.append('}');
2384 pw.println(sb.toString());
2385 }
2386 if (affinityIntent != null) {
2387 StringBuilder sb = new StringBuilder(128);
2388 sb.append(prefix); sb.append("affinityIntent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08002389 affinityIntent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002390 sb.append('}');
2391 pw.println(sb.toString());
2392 }
2393 if (origActivity != null) {
2394 pw.print(prefix); pw.print("origActivity=");
2395 pw.println(origActivity.flattenToShortString());
2396 }
2397 if (realActivity != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002398 pw.print(prefix); pw.print("mActivityComponent=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002399 pw.println(realActivity.flattenToShortString());
2400 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07002401 if (autoRemoveRecents || isPersistable || !isActivityTypeStandard() || numFullscreen != 0) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002402 pw.print(prefix); pw.print("autoRemoveRecents="); pw.print(autoRemoveRecents);
Dianne Hackborn852975d2014-08-22 17:42:43 -07002403 pw.print(" isPersistable="); pw.print(isPersistable);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002404 pw.print(" numFullscreen="); pw.print(numFullscreen);
Wale Ogunwale66e16852017-10-19 13:35:52 -07002405 pw.print(" activityType="); pw.println(getActivityType());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002406 }
Craig Mautner432f64e2015-05-20 14:59:57 -07002407 if (rootWasReset || mNeverRelinquishIdentity || mReuseTask
2408 || mLockTaskAuth != LOCK_TASK_AUTH_PINNABLE) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002409 pw.print(prefix); pw.print("rootWasReset="); pw.print(rootWasReset);
2410 pw.print(" mNeverRelinquishIdentity="); pw.print(mNeverRelinquishIdentity);
Craig Mautner432f64e2015-05-20 14:59:57 -07002411 pw.print(" mReuseTask="); pw.print(mReuseTask);
2412 pw.print(" mLockTaskAuth="); pw.println(lockTaskAuthToString());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002413 }
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002414 if (mAffiliatedTaskId != mTaskId || mPrevAffiliateTaskId != INVALID_TASK_ID
Wale Ogunwale18795a22014-12-03 11:38:33 -08002415 || mPrevAffiliate != null || mNextAffiliateTaskId != INVALID_TASK_ID
2416 || mNextAffiliate != null) {
Dianne Hackborn852975d2014-08-22 17:42:43 -07002417 pw.print(prefix); pw.print("affiliation="); pw.print(mAffiliatedTaskId);
2418 pw.print(" prevAffiliation="); pw.print(mPrevAffiliateTaskId);
2419 pw.print(" (");
2420 if (mPrevAffiliate == null) {
2421 pw.print("null");
2422 } else {
2423 pw.print(Integer.toHexString(System.identityHashCode(mPrevAffiliate)));
2424 }
2425 pw.print(") nextAffiliation="); pw.print(mNextAffiliateTaskId);
2426 pw.print(" (");
2427 if (mNextAffiliate == null) {
2428 pw.print("null");
2429 } else {
2430 pw.print(Integer.toHexString(System.identityHashCode(mNextAffiliate)));
2431 }
2432 pw.println(")");
2433 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002434 pw.print(prefix); pw.print("Activities="); pw.println(mActivities);
Dianne Hackborn852975d2014-08-22 17:42:43 -07002435 if (!askedCompatMode || !inRecents || !isAvailable) {
2436 pw.print(prefix); pw.print("askedCompatMode="); pw.print(askedCompatMode);
2437 pw.print(" inRecents="); pw.print(inRecents);
2438 pw.print(" isAvailable="); pw.println(isAvailable);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07002439 }
Dianne Hackborn852975d2014-08-22 17:42:43 -07002440 if (lastDescription != null) {
2441 pw.print(prefix); pw.print("lastDescription="); pw.println(lastDescription);
2442 }
Dianne Hackborn68a06332017-11-15 17:54:18 -08002443 if (mRootProcess != null) {
2444 pw.print(prefix); pw.print("mRootProcess="); pw.println(mRootProcess);
2445 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002446 pw.print(prefix); pw.print("stackId="); pw.println(getStackId());
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002447 pw.print(prefix + "hasBeenVisible=" + hasBeenVisible);
2448 pw.print(" mResizeMode=" + ActivityInfo.resizeModeToString(mResizeMode));
Winson Chungd3395382016-12-13 11:49:09 -08002449 pw.print(" mSupportsPictureInPicture=" + mSupportsPictureInPicture);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002450 pw.print(" isResizeable=" + isResizeable());
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002451 pw.print(" lastActiveTime=" + lastActiveTime);
2452 pw.println(" (inactive for " + (getInactiveDuration() / 1000) + "s)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 }
2454
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002455 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002457 StringBuilder sb = new StringBuilder(128);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002458 if (stringName != null) {
2459 sb.append(stringName);
2460 sb.append(" U=");
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002461 sb.append(mUserId);
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002462 sb.append(" StackId=");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002463 sb.append(getStackId());
Craig Mautnerde4ef022013-04-07 19:01:33 -07002464 sb.append(" sz=");
2465 sb.append(mActivities.size());
2466 sb.append('}');
2467 return sb.toString();
2468 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002469 sb.append("TaskRecord{");
2470 sb.append(Integer.toHexString(System.identityHashCode(this)));
2471 sb.append(" #");
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002472 sb.append(mTaskId);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002473 if (affinity != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002474 sb.append(" A=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002475 sb.append(affinity);
2476 } else if (intent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002477 sb.append(" I=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002478 sb.append(intent.getComponent().flattenToShortString());
Bryce Leefbd263b42018-03-07 10:33:55 -08002479 } else if (affinityIntent != null && affinityIntent.getComponent() != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002480 sb.append(" aI=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002481 sb.append(affinityIntent.getComponent().flattenToShortString());
2482 } else {
2483 sb.append(" ??");
2484 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002485 stringName = sb.toString();
2486 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07002488
Nataniel Borges023ecb52019-01-16 14:15:43 -08002489 public void writeToProto(ProtoOutputStream proto, long fieldId,
2490 @WindowTraceLogLevel int logLevel) {
2491 if (logLevel == WindowTraceLogLevel.CRITICAL && !isVisible()) {
2492 return;
2493 }
2494
Steven Timotius4346f0a2017-09-12 11:07:21 -07002495 final long token = proto.start(fieldId);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002496 super.writeToProto(proto, CONFIGURATION_CONTAINER, logLevel);
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002497 proto.write(ID, mTaskId);
Steven Timotius4346f0a2017-09-12 11:07:21 -07002498 for (int i = mActivities.size() - 1; i >= 0; i--) {
2499 ActivityRecord activity = mActivities.get(i);
2500 activity.writeToProto(proto, ACTIVITIES);
2501 }
2502 proto.write(STACK_ID, mStack.mStackId);
2503 if (mLastNonFullscreenBounds != null) {
2504 mLastNonFullscreenBounds.writeToProto(proto, LAST_NON_FULLSCREEN_BOUNDS);
2505 }
2506 if (realActivity != null) {
2507 proto.write(REAL_ACTIVITY, realActivity.flattenToShortString());
2508 }
2509 if (origActivity != null) {
2510 proto.write(ORIG_ACTIVITY, origActivity.flattenToShortString());
2511 }
2512 proto.write(ACTIVITY_TYPE, getActivityType());
Steven Timotius4346f0a2017-09-12 11:07:21 -07002513 proto.write(RESIZE_MODE, mResizeMode);
Bryce Leef3c6a472017-11-14 14:53:06 -08002514 // TODO: Remove, no longer needed with windowingMode.
2515 proto.write(FULLSCREEN, matchParentBounds());
2516
2517 if (!matchParentBounds()) {
Evan Roskydfe3da72018-10-26 17:21:06 -07002518 final Rect bounds = getRequestedOverrideBounds();
Bryce Leef3c6a472017-11-14 14:53:06 -08002519 bounds.writeToProto(proto, BOUNDS);
Steven Timotius4346f0a2017-09-12 11:07:21 -07002520 }
2521 proto.write(MIN_WIDTH, mMinWidth);
2522 proto.write(MIN_HEIGHT, mMinHeight);
2523 proto.end(token);
2524 }
Winson Chung61c9e5a2017-10-11 10:39:32 -07002525
2526 /**
2527 * See {@link #getNumRunningActivities(TaskActivitiesReport)}.
2528 */
2529 static class TaskActivitiesReport {
2530 int numRunning;
2531 int numActivities;
2532 ActivityRecord top;
2533 ActivityRecord base;
2534
2535 void reset() {
2536 numRunning = numActivities = 0;
2537 top = base = null;
2538 }
2539 }
Garfield Tan9b1efea2017-12-05 16:43:46 -08002540
2541 /**
2542 * Saves this {@link TaskRecord} to XML using given serializer.
2543 */
2544 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
2545 if (DEBUG_RECENTS) Slog.i(TAG_RECENTS, "Saving task=" + this);
2546
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002547 out.attribute(null, ATTR_TASKID, String.valueOf(mTaskId));
Garfield Tan9b1efea2017-12-05 16:43:46 -08002548 if (realActivity != null) {
2549 out.attribute(null, ATTR_REALACTIVITY, realActivity.flattenToShortString());
2550 }
2551 out.attribute(null, ATTR_REALACTIVITY_SUSPENDED, String.valueOf(realActivitySuspended));
2552 if (origActivity != null) {
2553 out.attribute(null, ATTR_ORIGACTIVITY, origActivity.flattenToShortString());
2554 }
2555 // Write affinity, and root affinity if it is different from affinity.
2556 // We use the special string "@" for a null root affinity, so we can identify
2557 // later whether we were given a root affinity or should just make it the
2558 // same as the affinity.
2559 if (affinity != null) {
2560 out.attribute(null, ATTR_AFFINITY, affinity);
2561 if (!affinity.equals(rootAffinity)) {
2562 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
2563 }
2564 } else if (rootAffinity != null) {
2565 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
2566 }
2567 out.attribute(null, ATTR_ROOTHASRESET, String.valueOf(rootWasReset));
2568 out.attribute(null, ATTR_AUTOREMOVERECENTS, String.valueOf(autoRemoveRecents));
2569 out.attribute(null, ATTR_ASKEDCOMPATMODE, String.valueOf(askedCompatMode));
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002570 out.attribute(null, ATTR_USERID, String.valueOf(mUserId));
Garfield Tan9b1efea2017-12-05 16:43:46 -08002571 out.attribute(null, ATTR_USER_SETUP_COMPLETE, String.valueOf(mUserSetupComplete));
2572 out.attribute(null, ATTR_EFFECTIVE_UID, String.valueOf(effectiveUid));
2573 out.attribute(null, ATTR_LASTTIMEMOVED, String.valueOf(mLastTimeMoved));
2574 out.attribute(null, ATTR_NEVERRELINQUISH, String.valueOf(mNeverRelinquishIdentity));
2575 if (lastDescription != null) {
2576 out.attribute(null, ATTR_LASTDESCRIPTION, lastDescription.toString());
2577 }
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002578 if (mTaskDescription != null) {
2579 mTaskDescription.saveToXml(out);
Garfield Tan9b1efea2017-12-05 16:43:46 -08002580 }
2581 out.attribute(null, ATTR_TASK_AFFILIATION_COLOR, String.valueOf(mAffiliatedTaskColor));
2582 out.attribute(null, ATTR_TASK_AFFILIATION, String.valueOf(mAffiliatedTaskId));
2583 out.attribute(null, ATTR_PREV_AFFILIATION, String.valueOf(mPrevAffiliateTaskId));
2584 out.attribute(null, ATTR_NEXT_AFFILIATION, String.valueOf(mNextAffiliateTaskId));
2585 out.attribute(null, ATTR_CALLING_UID, String.valueOf(mCallingUid));
2586 out.attribute(null, ATTR_CALLING_PACKAGE, mCallingPackage == null ? "" : mCallingPackage);
2587 out.attribute(null, ATTR_RESIZE_MODE, String.valueOf(mResizeMode));
2588 out.attribute(null, ATTR_SUPPORTS_PICTURE_IN_PICTURE,
2589 String.valueOf(mSupportsPictureInPicture));
2590 if (mLastNonFullscreenBounds != null) {
2591 out.attribute(
2592 null, ATTR_NON_FULLSCREEN_BOUNDS, mLastNonFullscreenBounds.flattenToString());
2593 }
2594 out.attribute(null, ATTR_MIN_WIDTH, String.valueOf(mMinWidth));
2595 out.attribute(null, ATTR_MIN_HEIGHT, String.valueOf(mMinHeight));
2596 out.attribute(null, ATTR_PERSIST_TASK_VERSION, String.valueOf(PERSIST_TASK_VERSION));
2597
2598 if (affinityIntent != null) {
2599 out.startTag(null, TAG_AFFINITYINTENT);
2600 affinityIntent.saveToXml(out);
2601 out.endTag(null, TAG_AFFINITYINTENT);
2602 }
2603
Bryce Lee1a990e52018-04-23 10:54:11 -07002604 if (intent != null) {
2605 out.startTag(null, TAG_INTENT);
2606 intent.saveToXml(out);
2607 out.endTag(null, TAG_INTENT);
2608 }
Garfield Tan9b1efea2017-12-05 16:43:46 -08002609
2610 final ArrayList<ActivityRecord> activities = mActivities;
2611 final int numActivities = activities.size();
2612 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2613 final ActivityRecord r = activities.get(activityNdx);
2614 if (r.info.persistableMode == ActivityInfo.PERSIST_ROOT_ONLY || !r.isPersistable() ||
2615 ((r.intent.getFlags() & FLAG_ACTIVITY_NEW_DOCUMENT
2616 | FLAG_ACTIVITY_RETAIN_IN_RECENTS) == FLAG_ACTIVITY_NEW_DOCUMENT) &&
2617 activityNdx > 0) {
2618 // Stop at first non-persistable or first break in task (CLEAR_WHEN_TASK_RESET).
2619 break;
2620 }
2621 out.startTag(null, TAG_ACTIVITY);
2622 r.saveToXml(out);
2623 out.endTag(null, TAG_ACTIVITY);
2624 }
2625 }
2626
2627 @VisibleForTesting
2628 static TaskRecordFactory getTaskRecordFactory() {
2629 if (sTaskRecordFactory == null) {
2630 setTaskRecordFactory(new TaskRecordFactory());
2631 }
2632 return sTaskRecordFactory;
2633 }
2634
2635 static void setTaskRecordFactory(TaskRecordFactory factory) {
2636 sTaskRecordFactory = factory;
2637 }
2638
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07002639 static TaskRecord create(ActivityTaskManagerService service, int taskId, ActivityInfo info,
Garfield Tan9b1efea2017-12-05 16:43:46 -08002640 Intent intent, IVoiceInteractionSession voiceSession,
2641 IVoiceInteractor voiceInteractor) {
2642 return getTaskRecordFactory().create(
2643 service, taskId, info, intent, voiceSession, voiceInteractor);
2644 }
2645
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07002646 static TaskRecord create(ActivityTaskManagerService service, int taskId, ActivityInfo info,
Garfield Tan9b1efea2017-12-05 16:43:46 -08002647 Intent intent, TaskDescription taskDescription) {
2648 return getTaskRecordFactory().create(service, taskId, info, intent, taskDescription);
2649 }
2650
2651 static TaskRecord restoreFromXml(XmlPullParser in, ActivityStackSupervisor stackSupervisor)
2652 throws IOException, XmlPullParserException {
2653 return getTaskRecordFactory().restoreFromXml(in, stackSupervisor);
2654 }
2655
2656 /**
2657 * A factory class used to create {@link TaskRecord} or its subclass if any. This can be
2658 * specified when system boots by setting it with
2659 * {@link #setTaskRecordFactory(TaskRecordFactory)}.
2660 */
2661 static class TaskRecordFactory {
2662
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07002663 TaskRecord create(ActivityTaskManagerService service, int taskId, ActivityInfo info,
Garfield Tan9b1efea2017-12-05 16:43:46 -08002664 Intent intent, IVoiceInteractionSession voiceSession,
2665 IVoiceInteractor voiceInteractor) {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002666 return new TaskRecord(service, taskId, info, intent, voiceSession, voiceInteractor,
2667 null /*taskDescription*/);
Garfield Tan9b1efea2017-12-05 16:43:46 -08002668 }
2669
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07002670 TaskRecord create(ActivityTaskManagerService service, int taskId, ActivityInfo info,
Garfield Tan9b1efea2017-12-05 16:43:46 -08002671 Intent intent, TaskDescription taskDescription) {
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002672 return new TaskRecord(service, taskId, info, intent, null /*voiceSession*/,
2673 null /*voiceInteractor*/, taskDescription);
Garfield Tan9b1efea2017-12-05 16:43:46 -08002674 }
2675
2676 /**
2677 * Should only be used when we're restoring {@link TaskRecord} from storage.
2678 */
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07002679 TaskRecord create(ActivityTaskManagerService service, int taskId, Intent intent,
Garfield Tan9b1efea2017-12-05 16:43:46 -08002680 Intent affinityIntent, String affinity, String rootAffinity,
2681 ComponentName realActivity, ComponentName origActivity, boolean rootWasReset,
2682 boolean autoRemoveRecents, boolean askedCompatMode, int userId,
2683 int effectiveUid, String lastDescription, ArrayList<ActivityRecord> activities,
2684 long lastTimeMoved, boolean neverRelinquishIdentity,
2685 TaskDescription lastTaskDescription, int taskAffiliation, int prevTaskId,
2686 int nextTaskId, int taskAffiliationColor, int callingUid, String callingPackage,
2687 int resizeMode, boolean supportsPictureInPicture, boolean realActivitySuspended,
2688 boolean userSetupComplete, int minWidth, int minHeight) {
2689 return new TaskRecord(service, taskId, intent, affinityIntent, affinity,
2690 rootAffinity, realActivity, origActivity, rootWasReset, autoRemoveRecents,
2691 askedCompatMode, userId, effectiveUid, lastDescription, activities,
2692 lastTimeMoved, neverRelinquishIdentity, lastTaskDescription, taskAffiliation,
2693 prevTaskId, nextTaskId, taskAffiliationColor, callingUid, callingPackage,
2694 resizeMode, supportsPictureInPicture, realActivitySuspended, userSetupComplete,
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -07002695 minWidth, minHeight, null /*ActivityInfo*/, null /*_voiceSession*/,
2696 null /*_voiceInteractor*/);
Garfield Tan9b1efea2017-12-05 16:43:46 -08002697 }
2698
2699 TaskRecord restoreFromXml(XmlPullParser in, ActivityStackSupervisor stackSupervisor)
2700 throws IOException, XmlPullParserException {
2701 Intent intent = null;
2702 Intent affinityIntent = null;
2703 ArrayList<ActivityRecord> activities = new ArrayList<>();
2704 ComponentName realActivity = null;
2705 boolean realActivitySuspended = false;
2706 ComponentName origActivity = null;
2707 String affinity = null;
2708 String rootAffinity = null;
2709 boolean hasRootAffinity = false;
2710 boolean rootHasReset = false;
2711 boolean autoRemoveRecents = false;
2712 boolean askedCompatMode = false;
2713 int taskType = 0;
2714 int userId = 0;
2715 boolean userSetupComplete = true;
2716 int effectiveUid = -1;
2717 String lastDescription = null;
2718 long lastTimeOnTop = 0;
2719 boolean neverRelinquishIdentity = true;
2720 int taskId = INVALID_TASK_ID;
2721 final int outerDepth = in.getDepth();
2722 TaskDescription taskDescription = new TaskDescription();
2723 int taskAffiliation = INVALID_TASK_ID;
2724 int taskAffiliationColor = 0;
2725 int prevTaskId = INVALID_TASK_ID;
2726 int nextTaskId = INVALID_TASK_ID;
2727 int callingUid = -1;
2728 String callingPackage = "";
2729 int resizeMode = RESIZE_MODE_FORCE_RESIZEABLE;
2730 boolean supportsPictureInPicture = false;
Garfield Tan367b35a2017-12-13 12:16:21 -08002731 Rect lastNonFullscreenBounds = null;
Garfield Tan9b1efea2017-12-05 16:43:46 -08002732 int minWidth = INVALID_MIN_SIZE;
2733 int minHeight = INVALID_MIN_SIZE;
2734 int persistTaskVersion = 0;
2735
2736 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
2737 final String attrName = in.getAttributeName(attrNdx);
2738 final String attrValue = in.getAttributeValue(attrNdx);
2739 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: attribute name=" +
2740 attrName + " value=" + attrValue);
2741 switch (attrName) {
2742 case ATTR_TASKID:
2743 if (taskId == INVALID_TASK_ID) taskId = Integer.parseInt(attrValue);
2744 break;
2745 case ATTR_REALACTIVITY:
2746 realActivity = ComponentName.unflattenFromString(attrValue);
2747 break;
2748 case ATTR_REALACTIVITY_SUSPENDED:
2749 realActivitySuspended = Boolean.valueOf(attrValue);
2750 break;
2751 case ATTR_ORIGACTIVITY:
2752 origActivity = ComponentName.unflattenFromString(attrValue);
2753 break;
2754 case ATTR_AFFINITY:
2755 affinity = attrValue;
2756 break;
2757 case ATTR_ROOT_AFFINITY:
2758 rootAffinity = attrValue;
2759 hasRootAffinity = true;
2760 break;
2761 case ATTR_ROOTHASRESET:
2762 rootHasReset = Boolean.parseBoolean(attrValue);
2763 break;
2764 case ATTR_AUTOREMOVERECENTS:
2765 autoRemoveRecents = Boolean.parseBoolean(attrValue);
2766 break;
2767 case ATTR_ASKEDCOMPATMODE:
2768 askedCompatMode = Boolean.parseBoolean(attrValue);
2769 break;
2770 case ATTR_USERID:
2771 userId = Integer.parseInt(attrValue);
2772 break;
2773 case ATTR_USER_SETUP_COMPLETE:
2774 userSetupComplete = Boolean.parseBoolean(attrValue);
2775 break;
2776 case ATTR_EFFECTIVE_UID:
2777 effectiveUid = Integer.parseInt(attrValue);
2778 break;
2779 case ATTR_TASKTYPE:
2780 taskType = Integer.parseInt(attrValue);
2781 break;
2782 case ATTR_LASTDESCRIPTION:
2783 lastDescription = attrValue;
2784 break;
2785 case ATTR_LASTTIMEMOVED:
2786 lastTimeOnTop = Long.parseLong(attrValue);
2787 break;
2788 case ATTR_NEVERRELINQUISH:
2789 neverRelinquishIdentity = Boolean.parseBoolean(attrValue);
2790 break;
2791 case ATTR_TASK_AFFILIATION:
2792 taskAffiliation = Integer.parseInt(attrValue);
2793 break;
2794 case ATTR_PREV_AFFILIATION:
2795 prevTaskId = Integer.parseInt(attrValue);
2796 break;
2797 case ATTR_NEXT_AFFILIATION:
2798 nextTaskId = Integer.parseInt(attrValue);
2799 break;
2800 case ATTR_TASK_AFFILIATION_COLOR:
2801 taskAffiliationColor = Integer.parseInt(attrValue);
2802 break;
2803 case ATTR_CALLING_UID:
2804 callingUid = Integer.parseInt(attrValue);
2805 break;
2806 case ATTR_CALLING_PACKAGE:
2807 callingPackage = attrValue;
2808 break;
2809 case ATTR_RESIZE_MODE:
2810 resizeMode = Integer.parseInt(attrValue);
2811 break;
2812 case ATTR_SUPPORTS_PICTURE_IN_PICTURE:
2813 supportsPictureInPicture = Boolean.parseBoolean(attrValue);
2814 break;
2815 case ATTR_NON_FULLSCREEN_BOUNDS:
Garfield Tan367b35a2017-12-13 12:16:21 -08002816 lastNonFullscreenBounds = Rect.unflattenFromString(attrValue);
Garfield Tan9b1efea2017-12-05 16:43:46 -08002817 break;
2818 case ATTR_MIN_WIDTH:
2819 minWidth = Integer.parseInt(attrValue);
2820 break;
2821 case ATTR_MIN_HEIGHT:
2822 minHeight = Integer.parseInt(attrValue);
2823 break;
2824 case ATTR_PERSIST_TASK_VERSION:
2825 persistTaskVersion = Integer.parseInt(attrValue);
2826 break;
2827 default:
2828 if (attrName.startsWith(TaskDescription.ATTR_TASKDESCRIPTION_PREFIX)) {
2829 taskDescription.restoreFromXml(attrName, attrValue);
2830 } else {
2831 Slog.w(TAG, "TaskRecord: Unknown attribute=" + attrName);
2832 }
2833 }
2834 }
2835
2836 int event;
2837 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
2838 (event != XmlPullParser.END_TAG || in.getDepth() >= outerDepth)) {
2839 if (event == XmlPullParser.START_TAG) {
2840 final String name = in.getName();
2841 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG,
2842 "TaskRecord: START_TAG name=" + name);
2843 if (TAG_AFFINITYINTENT.equals(name)) {
2844 affinityIntent = Intent.restoreFromXml(in);
2845 } else if (TAG_INTENT.equals(name)) {
2846 intent = Intent.restoreFromXml(in);
2847 } else if (TAG_ACTIVITY.equals(name)) {
2848 ActivityRecord activity =
2849 ActivityRecord.restoreFromXml(in, stackSupervisor);
2850 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: activity=" +
2851 activity);
2852 if (activity != null) {
2853 activities.add(activity);
2854 }
2855 } else {
Garfield Tan1e740192017-12-12 14:37:42 -08002856 handleUnknownTag(name, in);
Garfield Tan9b1efea2017-12-05 16:43:46 -08002857 }
2858 }
2859 }
2860 if (!hasRootAffinity) {
2861 rootAffinity = affinity;
2862 } else if ("@".equals(rootAffinity)) {
2863 rootAffinity = null;
2864 }
2865 if (effectiveUid <= 0) {
2866 Intent checkIntent = intent != null ? intent : affinityIntent;
2867 effectiveUid = 0;
2868 if (checkIntent != null) {
2869 IPackageManager pm = AppGlobals.getPackageManager();
2870 try {
2871 ApplicationInfo ai = pm.getApplicationInfo(
2872 checkIntent.getComponent().getPackageName(),
2873 PackageManager.MATCH_UNINSTALLED_PACKAGES
2874 | PackageManager.MATCH_DISABLED_COMPONENTS, userId);
2875 if (ai != null) {
2876 effectiveUid = ai.uid;
2877 }
2878 } catch (RemoteException e) {
2879 }
2880 }
2881 Slog.w(TAG, "Updating task #" + taskId + " for " + checkIntent
2882 + ": effectiveUid=" + effectiveUid);
2883 }
2884
2885 if (persistTaskVersion < 1) {
2886 // We need to convert the resize mode of home activities saved before version one if
2887 // they are marked as RESIZE_MODE_RESIZEABLE to
2888 // RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION since we didn't have that differentiation
2889 // before version 1 and the system didn't resize home activities before then.
2890 if (taskType == 1 /* old home type */ && resizeMode == RESIZE_MODE_RESIZEABLE) {
2891 resizeMode = RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
2892 }
2893 } else {
2894 // This activity has previously marked itself explicitly as both resizeable and
2895 // supporting picture-in-picture. Since there is no longer a requirement for
2896 // picture-in-picture activities to be resizeable, we can mark this simply as
2897 // resizeable and supporting picture-in-picture separately.
2898 if (resizeMode == RESIZE_MODE_RESIZEABLE_AND_PIPABLE_DEPRECATED) {
2899 resizeMode = RESIZE_MODE_RESIZEABLE;
2900 supportsPictureInPicture = true;
2901 }
2902 }
2903
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002904 final TaskRecord task = create(stackSupervisor.mService,
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07002905 taskId, intent, affinityIntent,
Garfield Tan9b1efea2017-12-05 16:43:46 -08002906 affinity, rootAffinity, realActivity, origActivity, rootHasReset,
2907 autoRemoveRecents, askedCompatMode, userId, effectiveUid, lastDescription,
2908 activities, lastTimeOnTop, neverRelinquishIdentity, taskDescription,
2909 taskAffiliation, prevTaskId, nextTaskId, taskAffiliationColor, callingUid,
2910 callingPackage, resizeMode, supportsPictureInPicture, realActivitySuspended,
2911 userSetupComplete, minWidth, minHeight);
Garfield Tan367b35a2017-12-13 12:16:21 -08002912 task.mLastNonFullscreenBounds = lastNonFullscreenBounds;
2913 task.setBounds(lastNonFullscreenBounds);
Garfield Tan9b1efea2017-12-05 16:43:46 -08002914
2915 for (int activityNdx = activities.size() - 1; activityNdx >=0; --activityNdx) {
2916 activities.get(activityNdx).setTask(task);
2917 }
2918
2919 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Restored task=" + task);
2920 return task;
2921 }
Garfield Tan1e740192017-12-12 14:37:42 -08002922
2923 void handleUnknownTag(String name, XmlPullParser in)
2924 throws IOException, XmlPullParserException {
2925 Slog.e(TAG, "restoreTask: Unexpected name=" + name);
2926 XmlUtils.skipCurrentTag(in);
2927 }
Garfield Tan9b1efea2017-12-05 16:43:46 -08002928 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929}