blob: 10fb559de2e66ceef9c1517002fef46899d42eeb [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;
Garfield Tan891146c2018-10-09 12:14:00 -070069import static com.android.server.wm.ActivityRecord.STARTING_WINDOW_SHOWN;
70import static com.android.server.wm.ActivityStack.REMOVE_TASK_MODE_MOVING;
71import static com.android.server.wm.ActivityStack.REMOVE_TASK_MODE_MOVING_TO_TOP;
72import static com.android.server.wm.ActivityStackSupervisor.ON_TOP;
73import static com.android.server.wm.ActivityStackSupervisor.PAUSE_IMMEDIATELY;
74import 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 final int taskId; // 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.
Dianne Hackborn9da2d402012-03-15 13:43:08 -0700237 int userId; // 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
Craig Mautner648f69b2014-09-18 14:16:26 -0700265 TaskDescription lastTaskDescription = new TaskDescription();
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. */
285 long mLastTimeMoved = System.currentTimeMillis();
286
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 Ogunwaled95c06b2018-05-08 10:35:38 -0700307 final ActivityTaskManagerService mService;
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 Ogunwaled95c06b2018-05-08 10:35:38 -0700345 TaskRecord(ActivityTaskManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700346 IVoiceInteractionSession _voiceSession, IVoiceInteractor _voiceInteractor) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700347 mService = service;
Suprabh Shukla23593142015-11-03 17:31:15 -0800348 userId = UserHandle.getUserId(info.applicationInfo.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 taskId = _taskId;
Winson Chungfb44d212017-10-04 11:39:10 -0700350 lastActiveTime = SystemClock.elapsedRealtime();
Craig Mautnera228ae92014-07-09 05:44:55 -0700351 mAffiliatedTaskId = _taskId;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700352 voiceSession = _voiceSession;
353 voiceInteractor = _voiceInteractor;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700354 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800355 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700356 mCallingUid = info.applicationInfo.uid;
357 mCallingPackage = info.packageName;
Martijn Coenend4a69702014-06-30 11:12:17 -0700358 setIntent(_intent, info);
Andrii Kulian2e751b82016-03-16 16:59:32 -0700359 setMinDimensions(info);
Winson730bf062016-03-31 18:04:56 -0700360 touchActiveTime();
Wale Ogunwaled95c06b2018-05-08 10:35:38 -0700361 mService.getTaskChangeNotificationController().notifyTaskCreated(_taskId, realActivity);
Craig Mautner21d24a22014-04-23 11:45:37 -0700362 }
363
Garfield Tan9b1efea2017-12-05 16:43:46 -0800364 /**
Wale Ogunwaled95c06b2018-05-08 10:35:38 -0700365 * Don't use constructor directly.
366 * Use {@link #create(ActivityTaskManagerService, int, ActivityInfo,
Garfield Tan9b1efea2017-12-05 16:43:46 -0800367 * Intent, IVoiceInteractionSession, IVoiceInteractor)} instead.
368 */
Wale Ogunwaled95c06b2018-05-08 10:35:38 -0700369 TaskRecord(ActivityTaskManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200370 TaskDescription _taskDescription) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700371 mService = service;
Suprabh Shukla23593142015-11-03 17:31:15 -0800372 userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700373 taskId = _taskId;
Winson Chungfb44d212017-10-04 11:39:10 -0700374 lastActiveTime = SystemClock.elapsedRealtime();
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700375 mAffiliatedTaskId = _taskId;
376 voiceSession = null;
377 voiceInteractor = null;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700378 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800379 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700380 mCallingUid = info.applicationInfo.uid;
381 mCallingPackage = info.packageName;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700382 setIntent(_intent, info);
Andrii Kulian2e751b82016-03-16 16:59:32 -0700383 setMinDimensions(info);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700384
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700385 isPersistable = true;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700386 // Clamp to [1, max].
387 maxRecents = Math.min(Math.max(info.maxRecents, 1),
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700388 ActivityTaskManager.getMaxAppRecentsLimitStatic());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700389
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700390 lastTaskDescription = _taskDescription;
Winson730bf062016-03-31 18:04:56 -0700391 touchActiveTime();
Wale Ogunwaled95c06b2018-05-08 10:35:38 -0700392 mService.getTaskChangeNotificationController().notifyTaskCreated(_taskId, realActivity);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700393 }
394
Garfield Tan9b1efea2017-12-05 16:43:46 -0800395 /**
396 * Don't use constructor directly. This is only used by XML parser.
397 */
Wale Ogunwaled95c06b2018-05-08 10:35:38 -0700398 TaskRecord(ActivityTaskManagerService service, int _taskId, Intent _intent,
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800399 Intent _affinityIntent, String _affinity, String _rootAffinity,
400 ComponentName _realActivity, ComponentName _origActivity, boolean _rootWasReset,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700401 boolean _autoRemoveRecents, boolean _askedCompatMode, int _userId,
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800402 int _effectiveUid, String _lastDescription, ArrayList<ActivityRecord> activities,
Winson Chungfb44d212017-10-04 11:39:10 -0700403 long lastTimeMoved, boolean neverRelinquishIdentity,
404 TaskDescription _lastTaskDescription, int taskAffiliation, int prevTaskId,
405 int nextTaskId, int taskAffiliationColor, int callingUid, String callingPackage,
Charles He2bf28322017-10-12 22:24:49 +0100406 int resizeMode, boolean supportsPictureInPicture, boolean _realActivitySuspended,
407 boolean userSetupComplete, int minWidth, int minHeight) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700408 mService = service;
409 taskId = _taskId;
410 intent = _intent;
411 affinityIntent = _affinityIntent;
412 affinity = _affinity;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800413 rootAffinity = _rootAffinity;
Craig Mautner21d24a22014-04-23 11:45:37 -0700414 voiceSession = null;
415 voiceInteractor = null;
416 realActivity = _realActivity;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800417 realActivitySuspended = _realActivitySuspended;
Craig Mautner21d24a22014-04-23 11:45:37 -0700418 origActivity = _origActivity;
419 rootWasReset = _rootWasReset;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700420 isAvailable = true;
Dianne Hackborn13420f22014-07-18 15:43:56 -0700421 autoRemoveRecents = _autoRemoveRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700422 askedCompatMode = _askedCompatMode;
Craig Mautner21d24a22014-04-23 11:45:37 -0700423 userId = _userId;
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800424 mUserSetupComplete = userSetupComplete;
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700425 effectiveUid = _effectiveUid;
Winson Chungfb44d212017-10-04 11:39:10 -0700426 lastActiveTime = SystemClock.elapsedRealtime();
Craig Mautner21d24a22014-04-23 11:45:37 -0700427 lastDescription = _lastDescription;
428 mActivities = activities;
429 mLastTimeMoved = lastTimeMoved;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700430 mNeverRelinquishIdentity = neverRelinquishIdentity;
Winson Chung2cb86c72014-06-25 12:03:30 -0700431 lastTaskDescription = _lastTaskDescription;
Craig Mautnera228ae92014-07-09 05:44:55 -0700432 mAffiliatedTaskId = taskAffiliation;
Winson Chungec396d62014-08-06 17:08:00 -0700433 mAffiliatedTaskColor = taskAffiliationColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700434 mPrevAffiliateTaskId = prevTaskId;
435 mNextAffiliateTaskId = nextTaskId;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700436 mCallingUid = callingUid;
437 mCallingPackage = callingPackage;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800438 mResizeMode = resizeMode;
Winson Chungd3395382016-12-13 11:49:09 -0800439 mSupportsPictureInPicture = supportsPictureInPicture;
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700440 mMinWidth = minWidth;
441 mMinHeight = minHeight;
Wale Ogunwaled95c06b2018-05-08 10:35:38 -0700442 mService.getTaskChangeNotificationController().notifyTaskCreated(_taskId, realActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 }
444
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800445 Task getTask() {
446 return mTask;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800447 }
448
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800449 void createTask(boolean onTop, boolean showForAllUsers) {
450 if (mTask != null) {
451 throw new IllegalArgumentException("mTask=" + mTask
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800452 + " already created for task=" + this);
453 }
454
455 final Rect bounds = updateOverrideConfigurationFromLaunchBounds();
Yunfan Chen279f5582018-12-12 15:24:50 -0800456 final TaskStack stack = getStack().getTaskStack();
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800457
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800458 if (stack == null) {
Yunfan Chen279f5582018-12-12 15:24:50 -0800459 throw new IllegalArgumentException("TaskRecord: invalid stack=" + mStack);
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800460 }
461 EventLog.writeEvent(WM_TASK_CREATED, taskId, stack.mStackId);
462 mTask = new Task(taskId, stack, userId, mService.mWindowManager, mResizeMode,
463 mSupportsPictureInPicture, lastTaskDescription, this);
464 final int position = onTop ? POSITION_TOP : POSITION_BOTTOM;
465
466 if (!mDisplayedBounds.isEmpty()) {
467 mTask.setOverrideDisplayedBounds(mDisplayedBounds);
468 }
469 // We only want to move the parents to the parents if we are creating this task at the
470 // top of its stack.
471 stack.addTask(mTask, position, showForAllUsers, onTop /* moveParents */);
Bryce Lee04ab3462017-04-10 15:06:33 -0700472 }
473
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800474 void setTask(Task task) {
475 mTask = task;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800476 }
477
Garfield Tan347bd602018-12-21 15:11:12 -0800478 void cleanUpResourcesForDestroy() {
479 if (!mActivities.isEmpty()) {
480 return;
481 }
482
483 // This task is going away, so save the last state if necessary.
484 saveLaunchingStateIfNeeded();
485
486 // TODO: VI what about activity?
487 final boolean isVoiceSession = voiceSession != null;
488 if (isVoiceSession) {
489 try {
490 voiceSession.taskFinished(intent, taskId);
491 } catch (RemoteException e) {
492 }
493 }
494 if (autoRemoveFromRecents() || isVoiceSession) {
495 // Task creator asked to remove this when done, or this task was a voice
496 // interaction, so it should not remain on the recent tasks list.
497 mService.mStackSupervisor.mRecentTasks.remove(this);
498 }
499
500 removeWindowContainer();
501 }
502
503 @VisibleForTesting
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800504 void removeWindowContainer() {
Bryce Lee2b8e0372018-04-05 17:01:37 -0700505 mService.getLockTaskController().clearLockedTask(this);
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800506 if (mTask == null) {
507 if (DEBUG_STACK) Slog.i(TAG_WM, "removeTask: could not find taskId=" + taskId);
508 return;
509 }
510 mTask.removeIfPossible();
511 mTask = null;
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700512 if (!getWindowConfiguration().persistTaskBounds()) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800513 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
514 // default configuration the next time it launches.
515 updateOverrideConfiguration(null);
516 }
Wale Ogunwaled95c06b2018-05-08 10:35:38 -0700517 mService.getTaskChangeNotificationController().notifyTaskRemoved(taskId);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800518 }
519
Jorim Jaggifb9d78a2017-01-05 18:57:12 +0100520 public void onSnapshotChanged(TaskSnapshot snapshot) {
Wale Ogunwaled95c06b2018-05-08 10:35:38 -0700521 mService.getTaskChangeNotificationController().notifyTaskSnapshotChanged(taskId, snapshot);
Jorim Jaggifb9d78a2017-01-05 18:57:12 +0100522 }
523
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800524 void setResizeMode(int resizeMode) {
525 if (mResizeMode == resizeMode) {
526 return;
527 }
528 mResizeMode = resizeMode;
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800529 mTask.setResizeable(resizeMode);
Wale Ogunwaled32da472018-11-16 07:19:28 -0800530 mService.mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
531 mService.mRootActivityContainer.resumeFocusedStacksTopActivities();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800532 }
533
534 void setTaskDockedResizing(boolean resizing) {
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800535 if (mTask == null) {
536 Slog.w(TAG_WM, "setTaskDockedResizing: taskId " + taskId + " not found.");
537 return;
538 }
539 mTask.setTaskDockedResizing(resizing);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800540 }
541
Wale Ogunwale1666e312016-12-16 11:27:18 -0800542 // TODO: Consolidate this with the resize() method below.
Wale Ogunwale1666e312016-12-16 11:27:18 -0800543 public void requestResize(Rect bounds, int resizeMode) {
Wale Ogunwaled95c06b2018-05-08 10:35:38 -0700544 mService.resizeTask(taskId, bounds, resizeMode);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800545 }
546
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800547 boolean resize(Rect bounds, int resizeMode, boolean preserveWindow, boolean deferResume) {
Bryce Leef3c6a472017-11-14 14:53:06 -0800548 mService.mWindowManager.deferSurfaceLayout();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800549
Bryce Leef3c6a472017-11-14 14:53:06 -0800550 try {
551 if (!isResizeable()) {
552 Slog.w(TAG, "resizeTask: task " + this + " not resizeable.");
553 return true;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800554 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800555
Bryce Leef3c6a472017-11-14 14:53:06 -0800556 // If this is a forced resize, let it go through even if the bounds is not changing,
557 // as we might need a relayout due to surface size change (to/from fullscreen).
558 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Evan Roskydfe3da72018-10-26 17:21:06 -0700559 if (equivalentRequestedOverrideBounds(bounds) && !forced) {
Bryce Leef3c6a472017-11-14 14:53:06 -0800560 // Nothing to do here...
561 return true;
562 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800563
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800564 if (mTask == null) {
Bryce Leef3c6a472017-11-14 14:53:06 -0800565 // Task doesn't exist in window manager yet (e.g. was restored from recents).
566 // All we can do for now is update the bounds so it can be used when the task is
567 // added to window manager.
568 updateOverrideConfiguration(bounds);
569 if (!inFreeformWindowingMode()) {
570 // re-restore the task so it can have the proper stack association.
571 mService.mStackSupervisor.restoreRecentTaskLocked(this, null, !ON_TOP);
572 }
573 return true;
574 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800575
Bryce Leef3c6a472017-11-14 14:53:06 -0800576 if (!canResizeToBounds(bounds)) {
577 throw new IllegalArgumentException("resizeTask: Can not resize task=" + this
578 + " to bounds=" + bounds + " resizeMode=" + mResizeMode);
579 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800580
Bryce Leef3c6a472017-11-14 14:53:06 -0800581 // Do not move the task to another stack here.
582 // This method assumes that the task is already placed in the right stack.
583 // we do not mess with that decision and we only do the resize!
584
585 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + taskId);
586
587 final boolean updatedConfig = updateOverrideConfiguration(bounds);
588 // This variable holds information whether the configuration didn't change in a significant
589
590 // way and the activity was kept the way it was. If it's false, it means the activity
591 // had
592 // to be relaunched due to configuration change.
593 boolean kept = true;
594 if (updatedConfig) {
595 final ActivityRecord r = topRunningActivityLocked();
596 if (r != null && !deferResume) {
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -0800597 kept = r.ensureActivityConfiguration(0 /* globalChanges */,
Bryce Leef3c6a472017-11-14 14:53:06 -0800598 preserveWindow);
Garfield Tanb9151182018-06-25 16:29:21 -0700599 // Preserve other windows for resizing because if resizing happens when there
600 // is a dialog activity in the front, the activity that still shows some
601 // content to the user will become black and cause flickers. Note in most cases
602 // this won't cause tons of irrelevant windows being preserved because only
603 // activities in this task may experience a bounds change. Configs for other
604 // activities stay the same.
Wale Ogunwaled32da472018-11-16 07:19:28 -0800605 mService.mRootActivityContainer.ensureActivitiesVisible(r, 0, preserveWindow);
Bryce Leef3c6a472017-11-14 14:53:06 -0800606 if (!kept) {
Wale Ogunwaled32da472018-11-16 07:19:28 -0800607 mService.mRootActivityContainer.resumeFocusedStacksTopActivities();
Bryce Leef3c6a472017-11-14 14:53:06 -0800608 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800609 }
610 }
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800611 mTask.resize(kept, forced);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800612
Garfield Tan891146c2018-10-09 12:14:00 -0700613 saveLaunchingStateIfNeeded();
614
Bryce Leef3c6a472017-11-14 14:53:06 -0800615 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
616 return kept;
617 } finally {
618 mService.mWindowManager.continueSurfaceLayout();
619 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800620 }
621
622 // TODO: Investigate combining with the resize() method above.
623 void resizeWindowContainer() {
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800624 mTask.resize(false /* relayout */, false /* forced */);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800625 }
626
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800627 void getWindowContainerBounds(Rect bounds) {
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800628 if (mTask != null) {
629 mTask.getBounds(bounds);
630 } else {
631 bounds.setEmpty();
632 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800633 }
634
Winson Chung74666102017-02-22 17:49:24 -0800635 /**
636 * Convenience method to reparent a task to the top or bottom position of the stack.
637 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700638 boolean reparent(ActivityStack preferredStack, boolean toTop,
639 @ReparentMoveStackMode int moveStackMode, boolean animate, boolean deferResume,
640 String reason) {
641 return reparent(preferredStack, toTop ? MAX_VALUE : 0, moveStackMode, animate, deferResume,
642 true /* schedulePictureInPictureModeChange */, reason);
Winson Chung5af42fc2017-03-24 17:11:33 -0700643 }
644
645 /**
646 * Convenience method to reparent a task to the top or bottom position of the stack, with
647 * an option to skip scheduling the picture-in-picture mode change.
648 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700649 boolean reparent(ActivityStack preferredStack, boolean toTop,
650 @ReparentMoveStackMode int moveStackMode, boolean animate, boolean deferResume,
651 boolean schedulePictureInPictureModeChange, String reason) {
652 return reparent(preferredStack, toTop ? MAX_VALUE : 0, moveStackMode, animate,
Winson Chung5af42fc2017-03-24 17:11:33 -0700653 deferResume, schedulePictureInPictureModeChange, reason);
654 }
655
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700656 /** Convenience method to reparent a task to a specific position of the stack. */
657 boolean reparent(ActivityStack preferredStack, int position,
658 @ReparentMoveStackMode int moveStackMode, boolean animate, boolean deferResume,
659 String reason) {
660 return reparent(preferredStack, position, moveStackMode, animate, deferResume,
Winson Chung5af42fc2017-03-24 17:11:33 -0700661 true /* schedulePictureInPictureModeChange */, reason);
Winson Chung74666102017-02-22 17:49:24 -0800662 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800663
Winson Chung74666102017-02-22 17:49:24 -0800664 /**
665 * Reparents the task into a preferred stack, creating it if necessary.
666 *
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700667 * @param preferredStack the target stack to move this task
Winson Chung74666102017-02-22 17:49:24 -0800668 * @param position the position to place this task in the new stack
669 * @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 -0700670 * reparenting to be drawn and animated in
Winson Chung74666102017-02-22 17:49:24 -0800671 * @param moveStackMode whether or not to move the stack to the front always, only if it was
Winson Chung5af42fc2017-03-24 17:11:33 -0700672 * previously focused & in front, or never
Winson Chung74666102017-02-22 17:49:24 -0800673 * @param deferResume whether or not to update the visibility of other tasks and stacks that may
Winson Chung5af42fc2017-03-24 17:11:33 -0700674 * have changed as a result of this reparenting
675 * @param schedulePictureInPictureModeChange specifies whether or not to schedule the PiP mode
676 * change. Callers may set this to false if they are explicitly scheduling PiP mode
677 * changes themselves, like during the PiP animation
Winson Chung74666102017-02-22 17:49:24 -0800678 * @param reason the caller of this reparenting
Winson Chung5af42fc2017-03-24 17:11:33 -0700679 * @return whether the task was reparented
Winson Chung74666102017-02-22 17:49:24 -0800680 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700681 // TODO: Inspect all call sites and change to just changing windowing mode of the stack vs.
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700682 // re-parenting the task. Can only be done when we are no longer using static stack Ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700683 boolean reparent(ActivityStack preferredStack, int position,
684 @ReparentMoveStackMode int moveStackMode, boolean animate, boolean deferResume,
685 boolean schedulePictureInPictureModeChange, String reason) {
Winson Chung74666102017-02-22 17:49:24 -0800686 final ActivityStackSupervisor supervisor = mService.mStackSupervisor;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800687 final RootActivityContainer root = mService.mRootActivityContainer;
Winson Chung74666102017-02-22 17:49:24 -0800688 final WindowManagerService windowManager = mService.mWindowManager;
689 final ActivityStack sourceStack = getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700690 final ActivityStack toStack = supervisor.getReparentTargetStack(this, preferredStack,
Winson Chung74666102017-02-22 17:49:24 -0800691 position == MAX_VALUE);
692 if (toStack == sourceStack) {
693 return false;
694 }
Andrii Kulianb850ea52017-12-12 23:49:10 -0800695 if (!canBeLaunchedOnDisplay(toStack.mDisplayId)) {
696 return false;
697 }
Winson Chung74666102017-02-22 17:49:24 -0800698
Andrii Kulian6b321512019-01-23 06:37:00 +0000699 final boolean toTopOfStack = position == MAX_VALUE;
700 if (toTopOfStack && toStack.getResumedActivity() != null
701 && toStack.topRunningActivityLocked() != null) {
702 // Pause the resumed activity on the target stack while re-parenting task on top of it.
703 toStack.startPausingLocked(false /* userLeaving */, false /* uiSleeping */,
704 null /* resuming */, false /* pauseImmediately */);
705 }
706
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700707 final int toStackWindowingMode = toStack.getWindowingMode();
Winson Chung74666102017-02-22 17:49:24 -0800708 final ActivityRecord topActivity = getTopActivity();
709
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800710 final boolean mightReplaceWindow = topActivity != null
711 && replaceWindowsOnTaskMove(getWindowingMode(), toStackWindowingMode);
Winson Chung74666102017-02-22 17:49:24 -0800712 if (mightReplaceWindow) {
713 // We are about to relaunch the activity because its configuration changed due to
714 // being maximized, i.e. size change. The activity will first remove the old window
715 // and then add a new one. This call will tell window manager about this, so it can
716 // preserve the old window until the new one is drawn. This prevents having a gap
717 // between the removal and addition, in which no window is visible. We also want the
718 // entrance of the new window to be properly animated.
719 // Note here we always set the replacing window first, as the flags might be needed
720 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
721 windowManager.setWillReplaceWindow(topActivity.appToken, animate);
722 }
723
724 windowManager.deferSurfaceLayout();
725 boolean kept = true;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800726 try {
Winson Chung74666102017-02-22 17:49:24 -0800727 final ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled32da472018-11-16 07:19:28 -0800728 final boolean wasFocused = r != null && root.isTopDisplayFocusedStack(sourceStack)
Winson Chung74666102017-02-22 17:49:24 -0800729 && (topRunningActivityLocked() == r);
Bryce Leec4ab62a2018-03-05 14:19:26 -0800730 final boolean wasResumed = r != null && sourceStack.getResumedActivity() == r;
Winson Chung95f8f0e2017-03-24 09:20:17 -0700731 final boolean wasPaused = r != null && sourceStack.mPausingActivity == r;
Winson Chung74666102017-02-22 17:49:24 -0800732
733 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
734 // Whenever we are moving the top activity from the front stack we want to make sure to
735 // move the stack to the front.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700736 final boolean wasFront = r != null && sourceStack.isTopStackOnDisplay()
Winson Chung74666102017-02-22 17:49:24 -0800737 && (sourceStack.topRunningActivityLocked() == r);
738
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800739 // Adjust the position for the new parent stack as needed.
Winson Chung74666102017-02-22 17:49:24 -0800740 position = toStack.getAdjustedPositionForTask(this, position, null /* starting */);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800741
742 // Must reparent first in window manager to avoid a situation where AM can delete the
743 // we are coming from in WM before we reparent because it became empty.
Yunfan Chen279f5582018-12-12 15:24:50 -0800744 mTask.reparent(toStack.getTaskStack(), position,
Wale Ogunwale2719cc12017-04-14 09:45:27 -0700745 moveStackMode == REPARENT_MOVE_STACK_TO_FRONT);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800746
Wale Ogunwale56d8d162017-05-30 11:12:20 -0700747 final boolean moveStackToFront = moveStackMode == REPARENT_MOVE_STACK_TO_FRONT
748 || (moveStackMode == REPARENT_KEEP_STACK_AT_FRONT && (wasFocused || wasFront));
Winson Chung74666102017-02-22 17:49:24 -0800749 // Move the task
Wale Ogunwale56d8d162017-05-30 11:12:20 -0700750 sourceStack.removeTask(this, reason, moveStackToFront
751 ? REMOVE_TASK_MODE_MOVING_TO_TOP : REMOVE_TASK_MODE_MOVING);
Winson Chung5af42fc2017-03-24 17:11:33 -0700752 toStack.addTask(this, position, false /* schedulePictureInPictureModeChange */, reason);
Winson Chung74666102017-02-22 17:49:24 -0800753
Winson Chung5af42fc2017-03-24 17:11:33 -0700754 if (schedulePictureInPictureModeChange) {
755 // Notify of picture-in-picture mode changes
756 supervisor.scheduleUpdatePictureInPictureModeIfNeeded(this, sourceStack);
757 }
Winson Chung74666102017-02-22 17:49:24 -0800758
759 // TODO: Ensure that this is actually necessary here
760 // Notify the voice session if required
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800761 if (voiceSession != null) {
762 try {
763 voiceSession.taskStarted(intent, taskId);
764 } catch (RemoteException e) {
765 }
766 }
Winson Chung74666102017-02-22 17:49:24 -0800767
768 // If the task had focus before (or we're requested to move focus), move focus to the
769 // new stack by moving the stack to the front.
Winson Chung95f8f0e2017-03-24 09:20:17 -0700770 if (r != null) {
771 toStack.moveToFrontAndResumeStateIfNeeded(r, moveStackToFront, wasResumed,
772 wasPaused, reason);
773 }
Winson Chung74666102017-02-22 17:49:24 -0800774 if (!animate) {
Jorim Jaggifa9ed962018-01-25 00:16:49 +0100775 mService.mStackSupervisor.mNoAnimActivities.add(topActivity);
Winson Chung74666102017-02-22 17:49:24 -0800776 }
777
778 // We might trigger a configuration change. Save the current task bounds for freezing.
779 // TODO: Should this call be moved inside the resize method in WM?
780 toStack.prepareFreezingTaskBounds();
781
782 // Make sure the task has the appropriate bounds/size for the stack it is in.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700783 final boolean toStackSplitScreenPrimary =
784 toStackWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Evan Roskydfe3da72018-10-26 17:21:06 -0700785 final Rect configBounds = getRequestedOverrideBounds();
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700786 if ((toStackWindowingMode == WINDOWING_MODE_FULLSCREEN
787 || toStackWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY)
Evan Roskydfe3da72018-10-26 17:21:06 -0700788 && !Objects.equals(configBounds, toStack.getRequestedOverrideBounds())) {
789 kept = resize(toStack.getRequestedOverrideBounds(), RESIZE_MODE_SYSTEM,
790 !mightReplaceWindow, deferResume);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700791 } else if (toStackWindowingMode == WINDOWING_MODE_FREEFORM) {
Winson Chung74666102017-02-22 17:49:24 -0800792 Rect bounds = getLaunchBounds();
793 if (bounds == null) {
Bryce Leeec55eb02017-12-05 20:51:27 -0800794 mService.mStackSupervisor.getLaunchParamsController().layoutTask(this, null);
Bryce Leef3c6a472017-11-14 14:53:06 -0800795 bounds = configBounds;
Winson Chung74666102017-02-22 17:49:24 -0800796 }
797 kept = resize(bounds, RESIZE_MODE_FORCED, !mightReplaceWindow, deferResume);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700798 } else if (toStackSplitScreenPrimary || toStackWindowingMode == WINDOWING_MODE_PINNED) {
799 if (toStackSplitScreenPrimary && moveStackMode == REPARENT_KEEP_STACK_AT_FRONT) {
Matthew Ng330757d2017-02-28 14:19:17 -0800800 // Move recents to front so it is not behind home stack when going into docked
801 // mode
802 mService.mStackSupervisor.moveRecentsStackToFront(reason);
803 }
Evan Roskydfe3da72018-10-26 17:21:06 -0700804 kept = resize(toStack.getRequestedOverrideBounds(), RESIZE_MODE_SYSTEM,
805 !mightReplaceWindow, deferResume);
Winson Chung74666102017-02-22 17:49:24 -0800806 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800807 } finally {
Winson Chung74666102017-02-22 17:49:24 -0800808 windowManager.continueSurfaceLayout();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800809 }
Winson Chung74666102017-02-22 17:49:24 -0800810
811 if (mightReplaceWindow) {
812 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
813 // window), we need to clear the replace window settings. Otherwise, we schedule a
814 // timeout to remove the old window if the replacing window is not coming in time.
815 windowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
816 }
817
818 if (!deferResume) {
819 // The task might have already been running and its visibility needs to be synchronized
820 // with the visibility of the stack / windows.
Wale Ogunwaled32da472018-11-16 07:19:28 -0800821 root.ensureActivitiesVisible(null, 0, !mightReplaceWindow);
822 root.resumeFocusedStacksTopActivities();
Winson Chung74666102017-02-22 17:49:24 -0800823 }
824
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700825 // TODO: Handle incorrect request to move before the actual move, not after.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700826 supervisor.handleNonResizableTaskIfNeeded(this, preferredStack.getWindowingMode(),
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700827 DEFAULT_DISPLAY, toStack);
Winson Chung74666102017-02-22 17:49:24 -0800828
Winson Chungdff7a732017-12-11 12:17:06 -0800829 return (preferredStack == toStack);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800830 }
831
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700832 /**
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800833 * @return True if the windows of tasks being moved to the target stack from the source stack
834 * should be replaced, meaning that window manager will keep the old window around until the new
835 * is ready.
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700836 */
837 private static boolean replaceWindowsOnTaskMove(
838 int sourceWindowingMode, int targetWindowingMode) {
839 return sourceWindowingMode == WINDOWING_MODE_FREEFORM
840 || targetWindowingMode == WINDOWING_MODE_FREEFORM;
841 }
842
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800843 void cancelWindowTransition() {
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800844 if (mTask == null) {
845 Slog.w(TAG_WM, "cancelWindowTransition: taskId " + taskId + " not found.");
846 return;
847 }
848 mTask.cancelTaskWindowTransition();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800849 }
850
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100851 /**
852 * DO NOT HOLD THE ACTIVITY MANAGER LOCK WHEN CALLING THIS METHOD!
853 */
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100854 TaskSnapshot getSnapshot(boolean reducedResolution) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100855
856 // TODO: Move this to {@link TaskWindowContainerController} once recent tasks are more
857 // synchronized between AM and WM.
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100858 return mService.mWindowManager.getTaskSnapshot(taskId, userId, reducedResolution);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800859 }
860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 void touchActiveTime() {
Winson Chungfb44d212017-10-04 11:39:10 -0700862 lastActiveTime = SystemClock.elapsedRealtime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 long getInactiveDuration() {
Winson Chungfb44d212017-10-04 11:39:10 -0700866 return SystemClock.elapsedRealtime() - lastActiveTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700868
Winson Chungfee26772014-08-05 12:21:52 -0700869 /** Sets the original intent, and the calling uid and package. */
870 void setIntent(ActivityRecord r) {
Winson Chungfee26772014-08-05 12:21:52 -0700871 mCallingUid = r.launchedFromUid;
872 mCallingPackage = r.launchedFromPackage;
Craig Mautner15df08a2015-04-01 12:17:18 -0700873 setIntent(r.intent, r.info);
Charles He2bf28322017-10-12 22:24:49 +0100874 setLockTaskAuth(r);
Winson Chungfee26772014-08-05 12:21:52 -0700875 }
876
877 /** Sets the original intent, _without_ updating the calling uid or package. */
878 private void setIntent(Intent _intent, ActivityInfo info) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700879 if (intent == null) {
880 mNeverRelinquishIdentity =
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700881 (info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700882 } else if (mNeverRelinquishIdentity) {
883 return;
884 }
885
886 affinity = info.taskAffinity;
Dianne Hackborn79228822014-09-16 11:11:23 -0700887 if (intent == null) {
888 // If this task already has an intent associated with it, don't set the root
889 // affinity -- we don't want it changing after initially set, but the initially
890 // set value may be null.
891 rootAffinity = affinity;
892 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700893 effectiveUid = info.applicationInfo.uid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700894 stringName = null;
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 if (info.targetActivity == null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800897 if (_intent != null) {
898 // If this Intent has a selector, we want to clear it for the
899 // recent task since it is not relevant if the user later wants
900 // to re-launch the app.
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700901 if (_intent.getSelector() != null || _intent.getSourceBounds() != null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800902 _intent = new Intent(_intent);
903 _intent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700904 _intent.setSourceBounds(null);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800905 }
906 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700907 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Setting Intent of " + this + " to " + _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 intent = _intent;
909 realActivity = _intent != null ? _intent.getComponent() : null;
910 origActivity = null;
911 } else {
912 ComponentName targetComponent = new ComponentName(
913 info.packageName, info.targetActivity);
914 if (_intent != null) {
915 Intent targetIntent = new Intent(_intent);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800916 targetIntent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700917 targetIntent.setSourceBounds(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700918 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700919 "Setting Intent of " + this + " to target " + targetIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 intent = targetIntent;
921 realActivity = targetComponent;
922 origActivity = _intent.getComponent();
923 } else {
924 intent = null;
925 realActivity = targetComponent;
926 origActivity = new ComponentName(info.packageName, info.name);
927 }
928 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700929
Craig Mautner47b20ba2014-09-17 17:23:44 -0700930 final int intentFlags = intent == null ? 0 : intent.getFlags();
931 if ((intentFlags & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 // Once we are set to an Intent with this flag, we count this
933 // task as having a true root activity.
934 rootWasReset = true;
935 }
Dianne Hackborn09233282014-04-30 11:33:59 -0700936 userId = UserHandle.getUserId(info.applicationInfo.uid);
Winson Chung36f3f032016-09-08 23:29:43 +0000937 mUserSetupComplete = Settings.Secure.getIntForUser(mService.mContext.getContentResolver(),
938 USER_SETUP_COMPLETE, 0, userId) != 0;
Craig Mautner41db4a72014-05-07 17:20:56 -0700939 if ((info.flags & ActivityInfo.FLAG_AUTO_REMOVE_FROM_RECENTS) != 0) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700940 // If the activity itself has requested auto-remove, then just always do it.
941 autoRemoveRecents = true;
Wale Ogunwale843bfb92015-03-27 11:06:48 -0700942 } else if ((intentFlags & (FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_RETAIN_IN_RECENTS))
943 == FLAG_ACTIVITY_NEW_DOCUMENT) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700944 // If the caller has not asked for the document to be retained, then we may
945 // want to turn on auto-remove, depending on whether the target has set its
946 // own document launch mode.
947 if (info.documentLaunchMode != ActivityInfo.DOCUMENT_LAUNCH_NONE) {
948 autoRemoveRecents = false;
949 } else {
950 autoRemoveRecents = true;
951 }
952 } else {
953 autoRemoveRecents = false;
Craig Mautner41db4a72014-05-07 17:20:56 -0700954 }
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800955 mResizeMode = info.resizeMode;
Winson Chungd3395382016-12-13 11:49:09 -0800956 mSupportsPictureInPicture = info.supportsPictureInPicture();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800958
Andrii Kulian2e751b82016-03-16 16:59:32 -0700959 /** Sets the original minimal width and height. */
960 private void setMinDimensions(ActivityInfo info) {
961 if (info != null && info.windowLayout != null) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700962 mMinWidth = info.windowLayout.minWidth;
963 mMinHeight = info.windowLayout.minHeight;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700964 } else {
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700965 mMinWidth = INVALID_MIN_SIZE;
966 mMinHeight = INVALID_MIN_SIZE;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700967 }
968 }
969
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800970 /**
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700971 * Return true if the input activity has the same intent filter as the intent this task
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800972 * record is based on (normally the root activity intent).
973 */
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700974 boolean isSameIntentFilter(ActivityRecord r) {
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800975 final Intent intent = new Intent(r.intent);
Louis Chang23df1a62019-01-09 15:10:49 +0800976 // Make sure the component are the same if the input activity has the same real activity
977 // as the one in the task because either one of them could be the alias activity.
978 if (Objects.equals(realActivity, r.mActivityComponent) && this.intent != null) {
979 intent.setComponent(this.intent.getComponent());
980 }
Bryce Lee1a990e52018-04-23 10:54:11 -0700981 return intent.filterEquals(this.intent);
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800982 }
983
Wale Ogunwale66e16852017-10-19 13:35:52 -0700984 boolean returnsToHomeStack() {
985 final int returnHomeFlags = FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME;
Bryce Lee1a990e52018-04-23 10:54:11 -0700986 return intent != null && (intent.getFlags() & returnHomeFlags) == returnHomeFlags;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700987 }
988
Craig Mautnera228ae92014-07-09 05:44:55 -0700989 void setPrevAffiliate(TaskRecord prevAffiliate) {
990 mPrevAffiliate = prevAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800991 mPrevAffiliateTaskId = prevAffiliate == null ? INVALID_TASK_ID : prevAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700992 }
993
994 void setNextAffiliate(TaskRecord nextAffiliate) {
995 mNextAffiliate = nextAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800996 mNextAffiliateTaskId = nextAffiliate == null ? INVALID_TASK_ID : nextAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700997 }
998
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700999 <T extends ActivityStack> T getStack() {
1000 return (T) mStack;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001001 }
1002
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001003 /**
1004 * Must be used for setting parent stack because it performs configuration updates.
1005 * Must be called after adding task as a child to the stack.
1006 */
Andrii Kulian02b7a832016-10-06 23:11:56 -07001007 void setStack(ActivityStack stack) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001008 if (stack != null && !stack.isInStackLocked(this)) {
1009 throw new IllegalStateException("Task must be added as a Stack child first.");
1010 }
Bryce Lee84730a02018-04-03 14:10:04 -07001011 final ActivityStack oldStack = mStack;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001012 mStack = stack;
Bryce Lee84730a02018-04-03 14:10:04 -07001013
1014 // If the new {@link TaskRecord} is from a different {@link ActivityStack}, remove this
1015 // {@link ActivityRecord} from its current {@link ActivityStack}.
1016
1017 if (oldStack != mStack) {
1018 for (int i = getChildCount() - 1; i >= 0; --i) {
1019 final ActivityRecord activity = getChildAt(i);
1020
1021 if (oldStack != null) {
1022 oldStack.onActivityRemovedFromStack(activity);
1023 }
1024
1025 if (mStack != null) {
1026 stack.onActivityAddedToStack(activity);
1027 }
1028 }
1029 }
1030
Andrii Kulian1779e612016-10-12 21:58:25 -07001031 onParentChanged();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001032 }
1033
1034 /**
1035 * @return Id of current stack, {@link INVALID_STACK_ID} if no stack is set.
1036 */
1037 int getStackId() {
1038 return mStack != null ? mStack.mStackId : INVALID_STACK_ID;
1039 }
1040
Andrii Kulian1779e612016-10-12 21:58:25 -07001041 @Override
1042 protected int getChildCount() {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001043 return mActivities.size();
Andrii Kulian1779e612016-10-12 21:58:25 -07001044 }
1045
1046 @Override
chaviw82a0ba82018-03-15 14:26:29 -07001047 protected ActivityRecord getChildAt(int index) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001048 return mActivities.get(index);
Andrii Kulian1779e612016-10-12 21:58:25 -07001049 }
1050
1051 @Override
1052 protected ConfigurationContainer getParent() {
1053 return mStack;
1054 }
1055
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001056 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001057 protected void onParentChanged() {
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001058 super.onParentChanged();
Wale Ogunwaled32da472018-11-16 07:19:28 -08001059 mService.mRootActivityContainer.updateUIDsPresentOnDisplay();
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001060 }
1061
Craig Mautnera228ae92014-07-09 05:44:55 -07001062 // Close up recents linked list.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001063 private void closeRecentsChain() {
Craig Mautnera228ae92014-07-09 05:44:55 -07001064 if (mPrevAffiliate != null) {
1065 mPrevAffiliate.setNextAffiliate(mNextAffiliate);
1066 }
1067 if (mNextAffiliate != null) {
1068 mNextAffiliate.setPrevAffiliate(mPrevAffiliate);
1069 }
1070 setPrevAffiliate(null);
1071 setNextAffiliate(null);
1072 }
1073
Winson Chung740c3ac2014-11-12 16:14:38 -08001074 void removedFromRecents() {
Dianne Hackborn852975d2014-08-22 17:42:43 -07001075 closeRecentsChain();
1076 if (inRecents) {
1077 inRecents = false;
Winson Chung740c3ac2014-11-12 16:14:38 -08001078 mService.notifyTaskPersisterLocked(this, false);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001079 }
Jorim Jaggif9084ec2017-01-16 13:16:59 +01001080
Dianne Hackborn68a06332017-11-15 17:54:18 -08001081 clearRootProcess();
1082
Jorim Jaggif9084ec2017-01-16 13:16:59 +01001083 // TODO: Use window container controller once tasks are better synced between AM and WM
1084 mService.mWindowManager.notifyTaskRemovedFromRecents(taskId, userId);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001085 }
1086
Craig Mautnera228ae92014-07-09 05:44:55 -07001087 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
1088 closeRecentsChain();
1089 mAffiliatedTaskId = taskToAffiliateWith.mAffiliatedTaskId;
Winson Chungec396d62014-08-06 17:08:00 -07001090 mAffiliatedTaskColor = taskToAffiliateWith.mAffiliatedTaskColor;
Craig Mautnera228ae92014-07-09 05:44:55 -07001091 // Find the end
1092 while (taskToAffiliateWith.mNextAffiliate != null) {
1093 final TaskRecord nextRecents = taskToAffiliateWith.mNextAffiliate;
1094 if (nextRecents.mAffiliatedTaskId != mAffiliatedTaskId) {
1095 Slog.e(TAG, "setTaskToAffiliateWith: nextRecents=" + nextRecents + " affilTaskId="
1096 + nextRecents.mAffiliatedTaskId + " should be " + mAffiliatedTaskId);
1097 if (nextRecents.mPrevAffiliate == taskToAffiliateWith) {
1098 nextRecents.setPrevAffiliate(null);
1099 }
1100 taskToAffiliateWith.setNextAffiliate(null);
1101 break;
1102 }
1103 taskToAffiliateWith = nextRecents;
1104 }
1105 taskToAffiliateWith.setNextAffiliate(this);
1106 setPrevAffiliate(taskToAffiliateWith);
1107 setNextAffiliate(null);
1108 }
1109
Winson Chung1147c402014-05-14 11:05:00 -07001110 /** Returns the intent for the root activity for this task */
1111 Intent getBaseIntent() {
1112 return intent != null ? intent : affinityIntent;
1113 }
1114
Winson Chung3b3f4642014-04-22 10:08:18 -07001115 /** Returns the first non-finishing activity from the root. */
1116 ActivityRecord getRootActivity() {
1117 for (int i = 0; i < mActivities.size(); i++) {
1118 final ActivityRecord r = mActivities.get(i);
1119 if (r.finishing) {
1120 continue;
1121 }
1122 return r;
1123 }
1124 return null;
1125 }
1126
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001127 ActivityRecord getTopActivity() {
Bryce Lee9f6affd2017-09-01 09:18:35 -07001128 return getTopActivity(true /* includeOverlays */);
1129 }
1130
1131 ActivityRecord getTopActivity(boolean includeOverlays) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001132 for (int i = mActivities.size() - 1; i >= 0; --i) {
1133 final ActivityRecord r = mActivities.get(i);
Bryce Lee9f6affd2017-09-01 09:18:35 -07001134 if (r.finishing || (!includeOverlays && r.mTaskOverlay)) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001135 continue;
1136 }
1137 return r;
1138 }
1139 return null;
1140 }
1141
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001142 ActivityRecord topRunningActivityLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001143 if (mStack != null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001144 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1145 ActivityRecord r = mActivities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -07001146 if (!r.finishing && r.okToShowLocked()) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001147 return r;
1148 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001149 }
1150 }
1151 return null;
1152 }
1153
Jorim Jaggi172e99f2017-10-20 14:33:18 +02001154 boolean isVisible() {
1155 for (int i = mActivities.size() - 1; i >= 0; --i) {
1156 final ActivityRecord r = mActivities.get(i);
1157 if (r.visible) {
1158 return true;
1159 }
1160 }
1161 return false;
1162 }
1163
Jorim Jaggiea039a82017-08-02 14:37:49 +02001164 void getAllRunningVisibleActivitiesLocked(ArrayList<ActivityRecord> outActivities) {
1165 if (mStack != null) {
1166 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1167 ActivityRecord r = mActivities.get(activityNdx);
Jorim Jaggi02f1d2f2017-08-04 14:29:16 +02001168 if (!r.finishing && r.okToShowLocked() && r.visibleIgnoringKeyguard) {
Jorim Jaggiea039a82017-08-02 14:37:49 +02001169 outActivities.add(r);
1170 }
1171 }
1172 }
1173 }
1174
Wale Ogunwale3b232392016-05-13 15:37:13 -07001175 ActivityRecord topRunningActivityWithStartingWindowLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001176 if (mStack != null) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07001177 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1178 ActivityRecord r = mActivities.get(activityNdx);
1179 if (r.mStartingWindowState != STARTING_WINDOW_SHOWN
Chong Zhang87761972016-08-22 13:53:24 -07001180 || r.finishing || !r.okToShowLocked()) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07001181 continue;
1182 }
1183 return r;
1184 }
1185 }
1186 return null;
1187 }
1188
Winson Chung61c9e5a2017-10-11 10:39:32 -07001189 /**
1190 * Return the number of running activities, and the number of non-finishing/initializing
1191 * activities in the provided {@param reportOut} respectively.
1192 */
1193 void getNumRunningActivities(TaskActivitiesReport reportOut) {
1194 reportOut.reset();
1195 for (int i = mActivities.size() - 1; i >= 0; --i) {
1196 final ActivityRecord r = mActivities.get(i);
1197 if (r.finishing) {
1198 continue;
1199 }
1200
1201 reportOut.base = r;
1202
1203 // Increment the total number of non-finishing activities
1204 reportOut.numActivities++;
1205
Bryce Lee7ace3952018-02-16 14:34:32 -08001206 if (reportOut.top == null || (reportOut.top.isState(ActivityState.INITIALIZING))) {
Winson Chung61c9e5a2017-10-11 10:39:32 -07001207 reportOut.top = r;
1208 // Reset the number of running activities until we hit the first non-initializing
1209 // activity
1210 reportOut.numRunning = 0;
1211 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001212 if (r.attachedToProcess()) {
Winson Chung61c9e5a2017-10-11 10:39:32 -07001213 // Increment the number of actually running activities
1214 reportOut.numRunning++;
1215 }
1216 }
1217 }
1218
Chong Zhang87761972016-08-22 13:53:24 -07001219 boolean okToShowLocked() {
Wale Ogunwaled32da472018-11-16 07:19:28 -08001220 // NOTE: If {@link TaskRecord#topRunningActivity} return is not null then it is
Chong Zhang87761972016-08-22 13:53:24 -07001221 // okay to show the activity when locked.
1222 return mService.mStackSupervisor.isCurrentProfileLocked(userId)
1223 || topRunningActivityLocked() != null;
1224 }
1225
Craig Mautner3b475fe2013-12-16 15:58:31 -08001226 /** Call after activity movement or finish to make sure that frontOfTask is set correctly */
Bryce Leed71317c2017-02-07 14:27:22 -08001227 final void setFrontOfTask() {
1228 boolean foundFront = false;
Craig Mautner3b475fe2013-12-16 15:58:31 -08001229 final int numActivities = mActivities.size();
Craig Mautner704e40b2013-12-18 16:43:51 -08001230 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
Craig Mautner3b475fe2013-12-16 15:58:31 -08001231 final ActivityRecord r = mActivities.get(activityNdx);
1232 if (foundFront || r.finishing) {
1233 r.frontOfTask = false;
1234 } else {
1235 r.frontOfTask = true;
1236 // Set frontOfTask false for every following activity.
1237 foundFront = true;
1238 }
1239 }
Craig Mautner9587ee02014-06-23 15:00:10 +00001240 if (!foundFront && numActivities > 0) {
1241 // All activities of this task are finishing. As we ought to have a frontOfTask
1242 // activity, make the bottom activity front.
1243 mActivities.get(0).frontOfTask = true;
1244 }
Craig Mautner3b475fe2013-12-16 15:58:31 -08001245 }
1246
Craig Mautnerde4ef022013-04-07 19:01:33 -07001247 /**
Craig Mautner3b475fe2013-12-16 15:58:31 -08001248 * Reorder the history stack so that the passed activity is brought to the front.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001249 */
1250 final void moveActivityToFrontLocked(ActivityRecord newTop) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001251 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
1252 "Removing and adding activity " + newTop
1253 + " to stack at top callers=" + Debug.getCallers(4));
Craig Mautnerde4ef022013-04-07 19:01:33 -07001254
Craig Mautnerde4ef022013-04-07 19:01:33 -07001255 mActivities.remove(newTop);
1256 mActivities.add(newTop);
Bryce Leed58d7b32017-09-08 15:55:22 -07001257
1258 // Make sure window manager is aware of the position change.
Yunfan Chen0e7aff92018-12-05 16:35:32 -08001259 mTask.positionChildAtTop(newTop.mAppWindowToken);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001260 updateEffectiveIntent();
Craig Mautner3b475fe2013-12-16 15:58:31 -08001261
Bryce Leed71317c2017-02-07 14:27:22 -08001262 setFrontOfTask();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001263 }
1264
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001265 void addActivityToTop(ActivityRecord r) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001266 addActivityAtIndex(mActivities.size(), r);
1267 }
1268
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001269 @Override
Wale Ogunwaleeea34ee92017-08-31 20:07:45 -07001270 /*@WindowConfiguration.ActivityType*/
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001271 public int getActivityType() {
1272 final int applicationType = super.getActivityType();
1273 if (applicationType != ACTIVITY_TYPE_UNDEFINED || mActivities.isEmpty()) {
1274 return applicationType;
1275 }
1276 return mActivities.get(0).getActivityType();
1277 }
1278
Winson Chung30480042017-01-26 10:55:34 -08001279 /**
1280 * Adds an activity {@param r} at the given {@param index}. The activity {@param r} must either
1281 * be in the current task or unparented to any task.
1282 */
Craig Mautner1602ec22013-05-12 10:24:27 -07001283 void addActivityAtIndex(int index, ActivityRecord r) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001284 TaskRecord task = r.getTaskRecord();
Bryce Leeaf691c02017-03-20 14:20:22 -07001285 if (task != null && task != this) {
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001286 throw new IllegalArgumentException("Can not add r=" + " to task=" + this
Bryce Leeaf691c02017-03-20 14:20:22 -07001287 + " current parent=" + task);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001288 }
Bryce Leeaf691c02017-03-20 14:20:22 -07001289
1290 r.setTask(this);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001291
Craig Mautner6170f732013-04-02 13:05:23 -07001292 // Remove r first, and if it wasn't already in the list and it's fullscreen, count it.
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001293 if (!mActivities.remove(r) && r.fullscreen) {
1294 // Was not previously in list.
1295 numFullscreen++;
1296 }
Craig Mautner2c1faed2013-07-23 12:56:02 -07001297 // Only set this based on the first activity
1298 if (mActivities.isEmpty()) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001299 if (r.getActivityType() == ACTIVITY_TYPE_UNDEFINED) {
1300 // Normally non-standard activity type for the activity record will be set when the
1301 // object is created, however we delay setting the standard application type until
1302 // this point so that the task can set the type for additional activities added in
1303 // the else condition below.
1304 r.setActivityType(ACTIVITY_TYPE_STANDARD);
1305 }
1306 setActivityType(r.getActivityType());
Craig Mautner21d24a22014-04-23 11:45:37 -07001307 isPersistable = r.isPersistable();
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001308 mCallingUid = r.launchedFromUid;
1309 mCallingPackage = r.launchedFromPackage;
Dianne Hackborn852975d2014-08-22 17:42:43 -07001310 // Clamp to [1, max].
1311 maxRecents = Math.min(Math.max(r.info.maxRecents, 1),
Wale Ogunwale65ebd952018-04-25 15:41:44 -07001312 ActivityTaskManager.getMaxAppRecentsLimitStatic());
Craig Mautner2c1faed2013-07-23 12:56:02 -07001313 } else {
1314 // Otherwise make all added activities match this one.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001315 r.setActivityType(getActivityType());
Craig Mautner78733002013-06-10 13:54:49 -07001316 }
Wale Ogunwale3b232392016-05-13 15:37:13 -07001317
1318 final int size = mActivities.size();
1319
1320 if (index == size && size > 0) {
1321 final ActivityRecord top = mActivities.get(size - 1);
1322 if (top.mTaskOverlay) {
1323 // Place below the task overlay activity since the overlay activity should always
1324 // be on top.
1325 index--;
1326 }
1327 }
1328
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001329 index = Math.min(size, index);
Craig Mautner77878772013-03-04 19:46:24 -08001330 mActivities.add(index, r);
Bryce Lee84730a02018-04-03 14:10:04 -07001331
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001332 updateEffectiveIntent();
Craig Mautner21d24a22014-04-23 11:45:37 -07001333 if (r.isPersistable()) {
1334 mService.notifyTaskPersisterLocked(this, false);
1335 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001336
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08001337 if (r.mAppWindowToken != null) {
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001338 // Only attempt to move in WM if the child has a controller. It is possible we haven't
1339 // created controller for the activity we are starting yet.
Yunfan Chen0e7aff92018-12-05 16:35:32 -08001340 mTask.positionChildAt(r.mAppWindowToken, index);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001341 }
David Stevens82ea6cb2017-03-03 16:18:50 -08001342
1343 // Make sure the list of display UID whitelists is updated
1344 // now that this record is in a new task.
Wale Ogunwaled32da472018-11-16 07:19:28 -08001345 mService.mRootActivityContainer.updateUIDsPresentOnDisplay();
Craig Mautner77878772013-03-04 19:46:24 -08001346 }
1347
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001348 /**
Bryce Leeaf691c02017-03-20 14:20:22 -07001349 * Removes the specified activity from this task.
1350 * @param r The {@link ActivityRecord} to remove.
1351 * @return true if this was the last activity in the task.
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001352 */
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001353 boolean removeActivity(ActivityRecord r) {
Bryce Lee84730a02018-04-03 14:10:04 -07001354 return removeActivity(r, false /* reparenting */);
Bryce Leeaf691c02017-03-20 14:20:22 -07001355 }
1356
1357 boolean removeActivity(ActivityRecord r, boolean reparenting) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001358 if (r.getTaskRecord() != this) {
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001359 throw new IllegalArgumentException(
1360 "Activity=" + r + " does not belong to task=" + this);
1361 }
1362
Bryce Lee84730a02018-04-03 14:10:04 -07001363 r.setTask(null /* task */, reparenting /* reparenting */);
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001364
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001365 if (mActivities.remove(r) && r.fullscreen) {
1366 // Was previously in list.
1367 numFullscreen--;
1368 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001369 if (r.isPersistable()) {
1370 mService.notifyTaskPersisterLocked(this, false);
1371 }
Wale Ogunwale89182d52016-03-11 10:38:36 -08001372
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001373 if (inPinnedWindowingMode()) {
Wale Ogunwale89182d52016-03-11 10:38:36 -08001374 // We normally notify listeners of task stack changes on pause, however pinned stack
1375 // activities are normally in the paused state so no notification will be sent there
1376 // before the activity is removed. We send it here so instead.
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07001377 mService.getTaskChangeNotificationController().notifyTaskStackChanged();
Wale Ogunwale89182d52016-03-11 10:38:36 -08001378 }
1379
Craig Mautner41326202014-06-20 14:38:21 -07001380 if (mActivities.isEmpty()) {
Craig Mautner5afcd4d2014-06-21 18:11:33 -07001381 return !mReuseTask;
Craig Mautner41326202014-06-20 14:38:21 -07001382 }
1383 updateEffectiveIntent();
1384 return false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001385 }
1386
Winson Chung6954fc92017-03-24 16:22:12 -07001387 /**
1388 * @return whether or not there are ONLY task overlay activities in the stack.
1389 * If {@param excludeFinishing} is set, then ignore finishing activities in the check.
1390 * If there are no task overlay activities, this call returns false.
1391 */
1392 boolean onlyHasTaskOverlayActivities(boolean excludeFinishing) {
1393 int count = 0;
1394 for (int i = mActivities.size() - 1; i >= 0; i--) {
1395 final ActivityRecord r = mActivities.get(i);
1396 if (excludeFinishing && r.finishing) {
1397 continue;
1398 }
1399 if (!r.mTaskOverlay) {
1400 return false;
1401 }
1402 count++;
1403 }
1404 return count > 0;
1405 }
1406
Craig Mautner41db4a72014-05-07 17:20:56 -07001407 boolean autoRemoveFromRecents() {
Dianne Hackbornd38aed82014-06-10 21:36:35 -07001408 // We will automatically remove the task either if it has explicitly asked for
1409 // this, or it is empty and has never contained an activity that got shown to
1410 // the user.
Dianne Hackborn13420f22014-07-18 15:43:56 -07001411 return autoRemoveRecents || (mActivities.isEmpty() && !hasBeenVisible);
Craig Mautner41db4a72014-05-07 17:20:56 -07001412 }
1413
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001414 /**
1415 * Completely remove all activities associated with an existing
1416 * task starting at a specified index.
1417 */
Winson Chung0ec2a352017-10-26 11:38:30 -07001418 final void performClearTaskAtIndexLocked(int activityNdx, boolean pauseImmediately,
1419 String reason) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001420 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001421 for ( ; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001422 final ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001423 if (r.finishing) {
1424 continue;
1425 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001426 if (mStack == null) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001427 // Task was restored from persistent storage.
1428 r.takeFromHistory();
1429 mActivities.remove(activityNdx);
1430 --activityNdx;
1431 --numActivities;
Winson Chung6954fc92017-03-24 16:22:12 -07001432 } else if (mStack.finishActivityLocked(r, Activity.RESULT_CANCELED, null,
Winson Chung0ec2a352017-10-26 11:38:30 -07001433 reason, false, pauseImmediately)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001434 --activityNdx;
1435 --numActivities;
1436 }
1437 }
1438 }
1439
1440 /**
1441 * Completely remove all activities associated with an existing task.
1442 */
Benjamin Franza83859f2017-07-03 16:34:14 +01001443 void performClearTaskLocked() {
Craig Mautner362449a2014-06-20 14:04:39 -07001444 mReuseTask = true;
Winson Chung0ec2a352017-10-26 11:38:30 -07001445 performClearTaskAtIndexLocked(0, !PAUSE_IMMEDIATELY, "clear-task-all");
Craig Mautner362449a2014-06-20 14:04:39 -07001446 mReuseTask = false;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001447 }
1448
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -08001449 ActivityRecord performClearTaskForReuseLocked(ActivityRecord newR, int launchFlags) {
1450 mReuseTask = true;
1451 final ActivityRecord result = performClearTaskLocked(newR, launchFlags);
1452 mReuseTask = false;
1453 return result;
1454 }
1455
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001456 /**
1457 * Perform clear operation as requested by
1458 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
1459 * stack to the given task, then look for
1460 * an instance of that activity in the stack and, if found, finish all
1461 * activities on top of it and return the instance.
1462 *
1463 * @param newR Description of the new activity being started.
1464 * @return Returns the old activity that should be continued to be used,
1465 * or null if none was found.
1466 */
1467 final ActivityRecord performClearTaskLocked(ActivityRecord newR, int launchFlags) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001468 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001469 for (int activityNdx = numActivities - 1; activityNdx >= 0; --activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001470 ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001471 if (r.finishing) {
1472 continue;
1473 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001474 if (r.mActivityComponent.equals(newR.mActivityComponent)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001475 // Here it is! Now finish everything in front...
Craig Mautner1602ec22013-05-12 10:24:27 -07001476 final ActivityRecord ret = r;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001477
1478 for (++activityNdx; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001479 r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001480 if (r.finishing) {
1481 continue;
1482 }
1483 ActivityOptions opts = r.takeOptionsLocked();
1484 if (opts != null) {
1485 ret.updateOptionsLocked(opts);
1486 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001487 if (mStack != null && mStack.finishActivityLocked(
Todd Kennedy539db512014-12-15 09:57:55 -08001488 r, Activity.RESULT_CANCELED, null, "clear-task-stack", false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001489 --activityNdx;
1490 --numActivities;
1491 }
1492 }
1493
1494 // Finally, if this is a normal launch mode (that is, not
1495 // expecting onNewIntent()), then we will finish the current
1496 // instance of the activity so a new fresh one can be started.
1497 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
Daichi Hirono15a02992016-04-27 18:47:01 +09001498 && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0
1499 && !ActivityStarter.isDocumentLaunchesIntoExisting(launchFlags)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001500 if (!ret.finishing) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001501 if (mStack != null) {
1502 mStack.finishActivityLocked(
Wale Ogunwale7d701172015-03-11 15:36:30 -07001503 ret, Activity.RESULT_CANCELED, null, "clear-task-top", false);
1504 }
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001505 return null;
1506 }
1507 }
1508
1509 return ret;
1510 }
1511 }
1512
1513 return null;
1514 }
1515
Winson Chung0ec2a352017-10-26 11:38:30 -07001516 void removeTaskActivitiesLocked(boolean pauseImmediately, String reason) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001517 // Just remove the entire task.
Winson Chung0ec2a352017-10-26 11:38:30 -07001518 performClearTaskAtIndexLocked(0, pauseImmediately, reason);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07001519 }
1520
Craig Mautner432f64e2015-05-20 14:59:57 -07001521 String lockTaskAuthToString() {
1522 switch (mLockTaskAuth) {
1523 case LOCK_TASK_AUTH_DONT_LOCK: return "LOCK_TASK_AUTH_DONT_LOCK";
1524 case LOCK_TASK_AUTH_PINNABLE: return "LOCK_TASK_AUTH_PINNABLE";
1525 case LOCK_TASK_AUTH_LAUNCHABLE: return "LOCK_TASK_AUTH_LAUNCHABLE";
1526 case LOCK_TASK_AUTH_WHITELISTED: return "LOCK_TASK_AUTH_WHITELISTED";
Benjamin Franz469dd582015-06-09 14:24:36 +01001527 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV: return "LOCK_TASK_AUTH_LAUNCHABLE_PRIV";
Craig Mautner432f64e2015-05-20 14:59:57 -07001528 default: return "unknown=" + mLockTaskAuth;
1529 }
1530 }
1531
Craig Mautner15df08a2015-04-01 12:17:18 -07001532 void setLockTaskAuth() {
Charles He2bf28322017-10-12 22:24:49 +01001533 setLockTaskAuth(getRootActivity());
1534 }
1535
1536 private void setLockTaskAuth(@Nullable ActivityRecord r) {
1537 if (r == null) {
1538 mLockTaskAuth = LOCK_TASK_AUTH_PINNABLE;
1539 return;
1540 }
1541
Charles He520b2832017-09-02 15:27:16 +01001542 final String pkg = (realActivity != null) ? realActivity.getPackageName() : null;
Bryce Lee2b8e0372018-04-05 17:01:37 -07001543 final LockTaskController lockTaskController = mService.getLockTaskController();
Charles He2bf28322017-10-12 22:24:49 +01001544 switch (r.lockTaskLaunchMode) {
Craig Mautner15df08a2015-04-01 12:17:18 -07001545 case LOCK_TASK_LAUNCH_MODE_DEFAULT:
Bryce Lee2b8e0372018-04-05 17:01:37 -07001546 mLockTaskAuth = lockTaskController.isPackageWhitelisted(userId, pkg)
Charles He520b2832017-09-02 15:27:16 +01001547 ? LOCK_TASK_AUTH_WHITELISTED : LOCK_TASK_AUTH_PINNABLE;
Craig Mautner15df08a2015-04-01 12:17:18 -07001548 break;
1549
1550 case LOCK_TASK_LAUNCH_MODE_NEVER:
Benjamin Franz469dd582015-06-09 14:24:36 +01001551 mLockTaskAuth = LOCK_TASK_AUTH_DONT_LOCK;
Craig Mautner15df08a2015-04-01 12:17:18 -07001552 break;
1553
1554 case LOCK_TASK_LAUNCH_MODE_ALWAYS:
Benjamin Franz469dd582015-06-09 14:24:36 +01001555 mLockTaskAuth = LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -07001556 break;
1557
1558 case LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED:
Bryce Lee2b8e0372018-04-05 17:01:37 -07001559 mLockTaskAuth = lockTaskController.isPackageWhitelisted(userId, pkg)
Charles He520b2832017-09-02 15:27:16 +01001560 ? LOCK_TASK_AUTH_LAUNCHABLE : LOCK_TASK_AUTH_PINNABLE;
Craig Mautner15df08a2015-04-01 12:17:18 -07001561 break;
1562 }
Craig Mautner432f64e2015-05-20 14:59:57 -07001563 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "setLockTaskAuth: task=" + this +
1564 " mLockTaskAuth=" + lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07001565 }
1566
Winson Chungd3395382016-12-13 11:49:09 -08001567 private boolean isResizeable(boolean checkSupportsPip) {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001568 return (mService.mForceResizableActivities || ActivityInfo.isResizeableMode(mResizeMode)
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001569 || (checkSupportsPip && mSupportsPictureInPicture));
Winson Chungd3395382016-12-13 11:49:09 -08001570 }
1571
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001572 boolean isResizeable() {
Winson Chungd3395382016-12-13 11:49:09 -08001573 return isResizeable(true /* checkSupportsPip */);
1574 }
1575
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001576 @Override
1577 public boolean supportsSplitScreenWindowingMode() {
Winson Chungd3395382016-12-13 11:49:09 -08001578 // A task can not be docked even if it is considered resizeable because it only supports
1579 // picture-in-picture mode but has a non-resizeable resizeMode
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001580 return super.supportsSplitScreenWindowingMode()
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001581 && mService.mSupportsSplitScreenMultiWindow
1582 && (mService.mForceResizableActivities
Bryce Leec857a5b2017-08-16 10:04:52 -07001583 || (isResizeable(false /* checkSupportsPip */)
1584 && !ActivityInfo.isPreserveOrientationMode(mResizeMode)));
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001585 }
1586
skuhne@google.com322347b2016-12-02 12:54:03 -08001587 /**
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001588 * Check whether this task can be launched on the specified display.
Riddle Hsu16567132018-08-16 21:37:47 +08001589 *
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001590 * @param displayId Target display id.
Riddle Hsu16567132018-08-16 21:37:47 +08001591 * @return {@code true} if either it is the default display or this activity can be put on a
1592 * secondary display.
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001593 */
1594 boolean canBeLaunchedOnDisplay(int displayId) {
1595 return mService.mStackSupervisor.canPlaceEntityOnDisplay(displayId,
Riddle Hsu16567132018-08-16 21:37:47 +08001596 -1 /* don't check PID */, -1 /* don't check UID */, null /* activityInfo */);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001597 }
1598
1599 /**
skuhne@google.com322347b2016-12-02 12:54:03 -08001600 * Check that a given bounds matches the application requested orientation.
1601 *
1602 * @param bounds The bounds to be tested.
1603 * @return True if the requested bounds are okay for a resizing request.
1604 */
Wale Ogunwale069bbd32017-02-03 07:58:14 -08001605 private boolean canResizeToBounds(Rect bounds) {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001606 if (bounds == null || !inFreeformWindowingMode()) {
skuhne@google.com322347b2016-12-02 12:54:03 -08001607 // Note: If not on the freeform workspace, we ignore the bounds.
1608 return true;
1609 }
1610 final boolean landscape = bounds.width() > bounds.height();
Evan Roskydfe3da72018-10-26 17:21:06 -07001611 final Rect configBounds = getRequestedOverrideBounds();
skuhne@google.com322347b2016-12-02 12:54:03 -08001612 if (mResizeMode == RESIZE_MODE_FORCE_RESIZABLE_PRESERVE_ORIENTATION) {
Bryce Leef3c6a472017-11-14 14:53:06 -08001613 return configBounds.isEmpty()
1614 || landscape == (configBounds.width() > configBounds.height());
skuhne@google.com322347b2016-12-02 12:54:03 -08001615 }
1616 return (mResizeMode != RESIZE_MODE_FORCE_RESIZABLE_PORTRAIT_ONLY || !landscape)
1617 && (mResizeMode != RESIZE_MODE_FORCE_RESIZABLE_LANDSCAPE_ONLY || landscape);
1618 }
1619
Craig Mautner525f3d92013-05-07 14:01:50 -07001620 /**
Yorke Leebdef5372017-04-10 16:38:51 -07001621 * @return {@code true} if the task is being cleared for the purposes of being reused.
1622 */
1623 boolean isClearingToReuseTask() {
1624 return mReuseTask;
1625 }
1626
1627 /**
Craig Mautner525f3d92013-05-07 14:01:50 -07001628 * Find the activity in the history stack within the given task. Returns
1629 * the index within the history at which it's found, or < 0 if not found.
1630 */
1631 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001632 final ComponentName realActivity = r.mActivityComponent;
Craig Mautner525f3d92013-05-07 14:01:50 -07001633 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1634 ActivityRecord candidate = mActivities.get(activityNdx);
1635 if (candidate.finishing) {
1636 continue;
1637 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001638 if (candidate.mActivityComponent.equals(realActivity)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001639 return candidate;
1640 }
1641 }
1642 return null;
1643 }
1644
Winson Chunga449dc02014-05-16 11:15:04 -07001645 /** Updates the last task description values. */
1646 void updateTaskDescription() {
1647 // Traverse upwards looking for any break between main task activities and
1648 // utility activities.
1649 int activityNdx;
1650 final int numActivities = mActivities.size();
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001651 final boolean relinquish = numActivities != 0 &&
1652 (mActivities.get(0).info.flags & FLAG_RELINQUISH_TASK_IDENTITY) != 0;
Winson Chunga449dc02014-05-16 11:15:04 -07001653 for (activityNdx = Math.min(numActivities, 1); activityNdx < numActivities;
Craig Mautner21d24a22014-04-23 11:45:37 -07001654 ++activityNdx) {
Winson Chunga449dc02014-05-16 11:15:04 -07001655 final ActivityRecord r = mActivities.get(activityNdx);
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001656 if (relinquish && (r.info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001657 // This will be the top activity for determining taskDescription. Pre-inc to
1658 // overcome initial decrement below.
1659 ++activityNdx;
1660 break;
1661 }
Winson Chunga449dc02014-05-16 11:15:04 -07001662 if (r.intent != null &&
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001663 (r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Winson Chunga449dc02014-05-16 11:15:04 -07001664 break;
1665 }
1666 }
1667 if (activityNdx > 0) {
1668 // Traverse downwards starting below break looking for set label, icon.
1669 // Note that if there are activities in the task but none of them set the
1670 // recent activity values, then we do not fall back to the last set
1671 // values in the TaskRecord.
1672 String label = null;
Craig Mautner648f69b2014-09-18 14:16:26 -07001673 String iconFilename = null;
Matthew Ng54bc9422017-10-02 17:16:28 -07001674 int iconResource = -1;
Winson Chunga449dc02014-05-16 11:15:04 -07001675 int colorPrimary = 0;
Winson Chung1af8eda2016-02-05 17:55:56 +00001676 int colorBackground = 0;
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001677 int statusBarColor = 0;
1678 int navigationBarColor = 0;
Adrian Roos4c864592019-04-10 14:47:57 +02001679 boolean statusBarContrastWhenTransparent = false;
1680 boolean navigationBarContrastWhenTransparent = false;
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001681 boolean topActivity = true;
Winson Chunga449dc02014-05-16 11:15:04 -07001682 for (--activityNdx; activityNdx >= 0; --activityNdx) {
1683 final ActivityRecord r = mActivities.get(activityNdx);
Winson Chung80f80db2018-05-30 21:13:25 -07001684 if (r.mTaskOverlay) {
1685 continue;
1686 }
Winson Chunga449dc02014-05-16 11:15:04 -07001687 if (r.taskDescription != null) {
1688 if (label == null) {
1689 label = r.taskDescription.getLabel();
1690 }
Matthew Ng54bc9422017-10-02 17:16:28 -07001691 if (iconResource == -1) {
1692 iconResource = r.taskDescription.getIconResource();
1693 }
Craig Mautner648f69b2014-09-18 14:16:26 -07001694 if (iconFilename == null) {
1695 iconFilename = r.taskDescription.getIconFilename();
Winson Chunga449dc02014-05-16 11:15:04 -07001696 }
1697 if (colorPrimary == 0) {
1698 colorPrimary = r.taskDescription.getPrimaryColor();
Winson Chunga449dc02014-05-16 11:15:04 -07001699 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001700 if (topActivity) {
Winson Chung1af8eda2016-02-05 17:55:56 +00001701 colorBackground = r.taskDescription.getBackgroundColor();
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001702 statusBarColor = r.taskDescription.getStatusBarColor();
1703 navigationBarColor = r.taskDescription.getNavigationBarColor();
Adrian Roos4c864592019-04-10 14:47:57 +02001704 statusBarContrastWhenTransparent =
1705 r.taskDescription.getEnsureStatusBarContrastWhenTransparent();
1706 navigationBarContrastWhenTransparent =
1707 r.taskDescription.getEnsureNavigationBarContrastWhenTransparent();
Winson Chung1af8eda2016-02-05 17:55:56 +00001708 }
Winson Chunga449dc02014-05-16 11:15:04 -07001709 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001710 topActivity = false;
Winson Chunga449dc02014-05-16 11:15:04 -07001711 }
Matthew Ng54bc9422017-10-02 17:16:28 -07001712 lastTaskDescription = new TaskDescription(label, null, iconResource, iconFilename,
Adrian Roos4c864592019-04-10 14:47:57 +02001713 colorPrimary, colorBackground, statusBarColor, navigationBarColor,
1714 statusBarContrastWhenTransparent, navigationBarContrastWhenTransparent);
Yunfan Chen0e7aff92018-12-05 16:35:32 -08001715 if (mTask != null) {
1716 mTask.setTaskDescription(lastTaskDescription);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +01001717 }
Winson Chungec396d62014-08-06 17:08:00 -07001718 // Update the task affiliation color if we are the parent of the group
1719 if (taskId == mAffiliatedTaskId) {
1720 mAffiliatedTaskColor = lastTaskDescription.getPrimaryColor();
1721 }
Winson Chunga449dc02014-05-16 11:15:04 -07001722 }
1723 }
1724
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001725 int findEffectiveRootIndex() {
Craig Mautner4767f4b2014-09-18 15:38:33 -07001726 int effectiveNdx = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001727 final int topActivityNdx = mActivities.size() - 1;
Dianne Hackborn39569af2014-09-23 10:56:58 -07001728 for (int activityNdx = 0; activityNdx <= topActivityNdx; ++activityNdx) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001729 final ActivityRecord r = mActivities.get(activityNdx);
1730 if (r.finishing) {
1731 continue;
1732 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001733 effectiveNdx = activityNdx;
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001734 if ((r.info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001735 break;
1736 }
1737 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001738 return effectiveNdx;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001739 }
1740
1741 void updateEffectiveIntent() {
1742 final int effectiveRootIndex = findEffectiveRootIndex();
1743 final ActivityRecord r = mActivities.get(effectiveRootIndex);
Winson Chungfee26772014-08-05 12:21:52 -07001744 setIntent(r);
Winson Chung8d9009e2017-11-16 15:43:05 -08001745
1746 // Update the task description when the activities change
1747 updateTaskDescription();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001748 }
1749
Evan Rosky730f6e82018-12-03 17:40:11 -08001750 void adjustForMinimalTaskDimensions(Rect bounds, Rect previousBounds) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001751 if (bounds == null) {
1752 return;
1753 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001754 int minWidth = mMinWidth;
1755 int minHeight = mMinHeight;
Robert Carr9c5867d2016-03-10 15:52:46 -08001756 // If the task has no requested minimal size, we'd like to enforce a minimal size
1757 // so that the user can not render the task too small to manipulate. We don't need
1758 // to do this for the pinned stack as the bounds are controlled by the system.
Evan Rosky1ac84462018-11-13 11:25:30 -08001759 if (!inPinnedWindowingMode() && mStack != null) {
Garfield Tan4a48a7f2018-10-02 14:23:55 -07001760 final int defaultMinSizeDp =
Wale Ogunwaled32da472018-11-16 07:19:28 -08001761 mService.mRootActivityContainer.mDefaultMinSizeOfResizeableTaskDp;
Garfield Tan4a48a7f2018-10-02 14:23:55 -07001762 final ActivityDisplay display =
Wale Ogunwaled32da472018-11-16 07:19:28 -08001763 mService.mRootActivityContainer.getActivityDisplay(mStack.mDisplayId);
Garfield Tan4a48a7f2018-10-02 14:23:55 -07001764 final float density =
1765 (float) display.getConfiguration().densityDpi / DisplayMetrics.DENSITY_DEFAULT;
1766 final int defaultMinSize = (int) (defaultMinSizeDp * density);
1767
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001768 if (minWidth == INVALID_MIN_SIZE) {
Garfield Tan4a48a7f2018-10-02 14:23:55 -07001769 minWidth = defaultMinSize;
Andrii Kulian2e751b82016-03-16 16:59:32 -07001770 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001771 if (minHeight == INVALID_MIN_SIZE) {
Garfield Tan4a48a7f2018-10-02 14:23:55 -07001772 minHeight = defaultMinSize;
Andrii Kulian2e751b82016-03-16 16:59:32 -07001773 }
Robert Carr9c5867d2016-03-10 15:52:46 -08001774 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001775 final boolean adjustWidth = minWidth > bounds.width();
1776 final boolean adjustHeight = minHeight > bounds.height();
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001777 if (!(adjustWidth || adjustHeight)) {
1778 return;
1779 }
1780
1781 if (adjustWidth) {
Garfield Tan020607d2018-12-17 17:01:58 -08001782 if (!previousBounds.isEmpty() && bounds.right == previousBounds.right) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001783 bounds.left = bounds.right - minWidth;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001784 } else {
1785 // Either left bounds match, or neither match, or the previous bounds were
1786 // fullscreen and we default to keeping left.
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001787 bounds.right = bounds.left + minWidth;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001788 }
1789 }
1790 if (adjustHeight) {
Garfield Tan020607d2018-12-17 17:01:58 -08001791 if (!previousBounds.isEmpty() && bounds.bottom == previousBounds.bottom) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001792 bounds.top = bounds.bottom - minHeight;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001793 } else {
1794 // Either top bounds match, or neither match, or the previous bounds were
1795 // fullscreen and we default to keeping top.
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001796 bounds.bottom = bounds.top + minHeight;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001797 }
1798 }
1799 }
1800
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001801 /**
1802 * Update task's override configuration based on the bounds.
Jorim Jaggi0a932142016-02-01 17:42:25 -08001803 * @param bounds The bounds of the task.
Andrii Kulian8072d112016-09-16 11:11:01 -07001804 * @return True if the override configuration was updated.
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001805 */
Andrii Kulian8072d112016-09-16 11:11:01 -07001806 boolean updateOverrideConfiguration(Rect bounds) {
Jorim Jaggi0a932142016-02-01 17:42:25 -08001807 return updateOverrideConfiguration(bounds, null /* insetBounds */);
1808 }
1809
Evan Rosky9ba524e2018-01-03 16:27:56 -08001810 void setLastNonFullscreenBounds(Rect bounds) {
1811 if (mLastNonFullscreenBounds == null) {
1812 mLastNonFullscreenBounds = new Rect(bounds);
1813 } else {
1814 mLastNonFullscreenBounds.set(bounds);
1815 }
1816 }
1817
Jorim Jaggi0a932142016-02-01 17:42:25 -08001818 /**
1819 * Update task's override configuration based on the bounds.
1820 * @param bounds The bounds of the task.
1821 * @param insetBounds The bounds used to calculate the system insets, which is used here to
1822 * subtract the navigation bar/status bar size from the screen size reported
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001823 * to the application. See {@link IActivityTaskManager#resizeDockedStack}.
Andrii Kulian8072d112016-09-16 11:11:01 -07001824 * @return True if the override configuration was updated.
Jorim Jaggi0a932142016-02-01 17:42:25 -08001825 */
Andrii Kulian8072d112016-09-16 11:11:01 -07001826 boolean updateOverrideConfiguration(Rect bounds, @Nullable Rect insetBounds) {
Evan Rosky1ac84462018-11-13 11:25:30 -08001827 final boolean hasSetDisplayedBounds = (insetBounds != null && !insetBounds.isEmpty());
1828 if (hasSetDisplayedBounds) {
1829 setDisplayedBounds(bounds);
1830 } else {
1831 setDisplayedBounds(null);
1832 }
1833 // "steady" bounds do not include any temporary offsets from animation or interaction.
1834 Rect steadyBounds = hasSetDisplayedBounds ? insetBounds : bounds;
1835 if (equivalentRequestedOverrideBounds(steadyBounds)) {
Andrii Kulian8072d112016-09-16 11:11:01 -07001836 return false;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001837 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001838
Evan Rosky1ac84462018-11-13 11:25:30 -08001839 mTmpConfig.setTo(getResolvedOverrideConfiguration());
1840 setBounds(steadyBounds);
1841 return !mTmpConfig.equals(getResolvedOverrideConfiguration());
Wale Ogunwaleeb76b762017-11-17 10:08:04 -08001842 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001843
Bryce Leec4ab62a2018-03-05 14:19:26 -08001844 /**
1845 * This should be called when an child activity changes state. This should only
1846 * be called from
1847 * {@link ActivityRecord#setState(ActivityState, String)} .
1848 * @param record The {@link ActivityRecord} whose state has changed.
1849 * @param state The new state.
1850 * @param reason The reason for the change.
1851 */
1852 void onActivityStateChanged(ActivityRecord record, ActivityState state, String reason) {
1853 final ActivityStack parent = getStack();
1854
1855 if (parent != null) {
1856 parent.onActivityStateChanged(record, state, reason);
1857 }
1858 }
1859
Wale Ogunwaleeb76b762017-11-17 10:08:04 -08001860 @Override
1861 public void onConfigurationChanged(Configuration newParentConfig) {
Evan Rosky730f6e82018-12-03 17:40:11 -08001862 // Check if the new configuration supports persistent bounds (eg. is Freeform) and if so
1863 // restore the last recorded non-fullscreen bounds.
1864 final boolean prevPersistTaskBounds = getWindowConfiguration().persistTaskBounds();
1865 final boolean nextPersistTaskBounds =
1866 getRequestedOverrideConfiguration().windowConfiguration.persistTaskBounds()
1867 || newParentConfig.windowConfiguration.persistTaskBounds();
1868 if (!prevPersistTaskBounds && nextPersistTaskBounds
1869 && mLastNonFullscreenBounds != null && !mLastNonFullscreenBounds.isEmpty()) {
1870 // Bypass onRequestedOverrideConfigurationChanged here to avoid infinite loop.
1871 getRequestedOverrideConfiguration().windowConfiguration
1872 .setBounds(mLastNonFullscreenBounds);
1873 }
1874
Wale Ogunwaleeb76b762017-11-17 10:08:04 -08001875 final boolean wasInMultiWindowMode = inMultiWindowMode();
1876 super.onConfigurationChanged(newParentConfig);
1877 if (wasInMultiWindowMode != inMultiWindowMode()) {
Winson Chung5af42fc2017-03-24 17:11:33 -07001878 mService.mStackSupervisor.scheduleUpdateMultiWindowMode(this);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001879 }
Evan Rosky730f6e82018-12-03 17:40:11 -08001880
1881 // If the configuration supports persistent bounds (eg. Freeform), keep track of the
1882 // current (non-fullscreen) bounds for persistence.
Evan Rosky1ac84462018-11-13 11:25:30 -08001883 if (getWindowConfiguration().persistTaskBounds()) {
1884 final Rect currentBounds = getRequestedOverrideBounds();
1885 if (!currentBounds.isEmpty()) {
1886 setLastNonFullscreenBounds(currentBounds);
1887 }
1888 }
Wale Ogunwaleeb76b762017-11-17 10:08:04 -08001889 // TODO: Should also take care of Pip mode changes here.
Garfield Tan891146c2018-10-09 12:14:00 -07001890
1891 saveLaunchingStateIfNeeded();
1892 }
1893
1894 /**
1895 * Saves launching state if necessary so that we can launch the activity to its latest state.
1896 * It only saves state if this task has been shown to user and it's in fullscreen or freeform
1897 * mode.
1898 */
1899 void saveLaunchingStateIfNeeded() {
1900 if (!hasBeenVisible) {
1901 // Not ever visible to user.
1902 return;
1903 }
1904
1905 final int windowingMode = getWindowingMode();
1906 if (windowingMode != WindowConfiguration.WINDOWING_MODE_FULLSCREEN
1907 && windowingMode != WindowConfiguration.WINDOWING_MODE_FREEFORM) {
1908 return;
1909 }
1910
1911 // Saves the new state so that we can launch the activity at the same location.
1912 mService.mStackSupervisor.mLaunchParamsPersister.saveTask(this);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001913 }
1914
Evan Roskyed6767f2018-10-26 17:21:06 -07001915 /**
Evan Rosky1ac84462018-11-13 11:25:30 -08001916 * Adjust bounds to stay within stack bounds.
1917 *
1918 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
1919 * that keep them unchanged, but be contained within the stack bounds.
1920 *
1921 * @param bounds Bounds to be adjusted.
1922 * @param stackBounds Bounds within which the other bounds should remain.
Evan Rosky60dba2f2019-02-01 10:58:38 -08001923 * @param overlapPxX The amount of px required to be visible in the X dimension.
1924 * @param overlapPxY The amount of px required to be visible in the Y dimension.
Evan Rosky1ac84462018-11-13 11:25:30 -08001925 */
Evan Rosky60dba2f2019-02-01 10:58:38 -08001926 private static void fitWithinBounds(Rect bounds, Rect stackBounds, int overlapPxX,
1927 int overlapPxY) {
Evan Rosky1ac84462018-11-13 11:25:30 -08001928 if (stackBounds == null || stackBounds.isEmpty() || stackBounds.contains(bounds)) {
1929 return;
1930 }
1931
Evan Rosky60dba2f2019-02-01 10:58:38 -08001932 // For each side of the parent (eg. left), check if the opposing side of the window (eg.
1933 // right) is at least overlap pixels away. If less, offset the window by that difference.
1934 int horizontalDiff = 0;
1935 // If window is smaller than overlap, use it's smallest dimension instead
1936 int overlapLR = Math.min(overlapPxX, bounds.width());
1937 if (bounds.right < (stackBounds.left + overlapLR)) {
1938 horizontalDiff = overlapLR - (bounds.right - stackBounds.left);
1939 } else if (bounds.left > (stackBounds.right - overlapLR)) {
1940 horizontalDiff = -(overlapLR - (stackBounds.right - bounds.left));
Evan Rosky1ac84462018-11-13 11:25:30 -08001941 }
Evan Rosky60dba2f2019-02-01 10:58:38 -08001942 int verticalDiff = 0;
1943 int overlapTB = Math.min(overlapPxY, bounds.width());
1944 if (bounds.bottom < (stackBounds.top + overlapTB)) {
1945 verticalDiff = overlapTB - (bounds.bottom - stackBounds.top);
1946 } else if (bounds.top > (stackBounds.bottom - overlapTB)) {
1947 verticalDiff = -(overlapTB - (stackBounds.bottom - bounds.top));
Evan Rosky1ac84462018-11-13 11:25:30 -08001948 }
Evan Rosky60dba2f2019-02-01 10:58:38 -08001949 bounds.offset(horizontalDiff, verticalDiff);
Evan Rosky1ac84462018-11-13 11:25:30 -08001950 }
1951
1952 /**
Evan Roskyed6767f2018-10-26 17:21:06 -07001953 * Displayed bounds are used to set where the task is drawn at any given time. This is
1954 * separate from its actual bounds so that the app doesn't see any meaningful configuration
1955 * changes during transitionary periods.
1956 */
1957 void setDisplayedBounds(Rect bounds) {
1958 if (bounds == null) {
1959 mDisplayedBounds.setEmpty();
1960 } else {
1961 mDisplayedBounds.set(bounds);
1962 }
Yunfan Chen0e7aff92018-12-05 16:35:32 -08001963 if (mTask != null) {
1964 mTask.setOverrideDisplayedBounds(
Evan Roskyed6767f2018-10-26 17:21:06 -07001965 mDisplayedBounds.isEmpty() ? null : mDisplayedBounds);
1966 }
1967 }
1968
1969 /**
1970 * Gets the current overridden displayed bounds. These will be empty if the task is not
1971 * currently overriding where it is displayed.
1972 */
1973 Rect getDisplayedBounds() {
1974 return mDisplayedBounds;
1975 }
1976
1977 /**
1978 * @return {@code true} if this has overridden displayed bounds.
1979 */
1980 boolean hasDisplayedBounds() {
1981 return !mDisplayedBounds.isEmpty();
1982 }
1983
Evan Rosky1ac84462018-11-13 11:25:30 -08001984 /**
1985 * Intersects inOutBounds with intersectBounds-intersectInsets. If inOutBounds is larger than
1986 * intersectBounds on a side, then the respective side will not be intersected.
1987 *
1988 * The assumption is that if inOutBounds is initially larger than intersectBounds, then the
1989 * inset on that side is no-longer applicable. This scenario happens when a task's minimal
1990 * bounds are larger than the provided parent/display bounds.
1991 *
1992 * @param inOutBounds the bounds to intersect.
1993 * @param intersectBounds the bounds to intersect with.
1994 * @param intersectInsets insets to apply to intersectBounds before intersecting.
1995 */
Riddle Hsu74826262019-04-17 14:57:42 +08001996 static void intersectWithInsetsIfFits(
Evan Rosky1ac84462018-11-13 11:25:30 -08001997 Rect inOutBounds, Rect intersectBounds, Rect intersectInsets) {
1998 if (inOutBounds.right <= intersectBounds.right) {
1999 inOutBounds.right =
2000 Math.min(intersectBounds.right - intersectInsets.right, inOutBounds.right);
2001 }
2002 if (inOutBounds.bottom <= intersectBounds.bottom) {
2003 inOutBounds.bottom =
2004 Math.min(intersectBounds.bottom - intersectInsets.bottom, inOutBounds.bottom);
2005 }
2006 if (inOutBounds.left >= intersectBounds.left) {
2007 inOutBounds.left =
2008 Math.max(intersectBounds.left + intersectInsets.left, inOutBounds.left);
2009 }
2010 if (inOutBounds.top >= intersectBounds.top) {
2011 inOutBounds.top =
2012 Math.max(intersectBounds.top + intersectInsets.top, inOutBounds.top);
2013 }
2014 }
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002015
Evan Rosky1ac84462018-11-13 11:25:30 -08002016 /**
2017 * Gets bounds with non-decor and stable insets applied respectively.
2018 *
2019 * If bounds overhangs the display, those edges will not get insets. See
2020 * {@link #intersectWithInsetsIfFits}
2021 *
2022 * @param outNonDecorBounds where to place bounds with non-decor insets applied.
2023 * @param outStableBounds where to place bounds with stable insets applied.
2024 * @param bounds the bounds to inset.
2025 */
2026 private void calculateInsetFrames(Rect outNonDecorBounds, Rect outStableBounds, Rect bounds,
2027 DisplayInfo displayInfo) {
2028 outNonDecorBounds.set(bounds);
2029 outStableBounds.set(bounds);
2030 if (getStack() == null || getStack().getDisplay() == null) {
2031 return;
2032 }
2033 DisplayPolicy policy = getStack().getDisplay().mDisplayContent.getDisplayPolicy();
2034 if (policy == null) {
2035 return;
2036 }
2037 mTmpBounds.set(0, 0, displayInfo.logicalWidth, displayInfo.logicalHeight);
Bryce Lee7566d762017-03-30 09:34:15 -07002038
Riddle Hsu61987bc2019-04-03 13:08:47 +08002039 policy.getNonDecorInsetsLw(displayInfo.rotation, displayInfo.logicalWidth,
2040 displayInfo.logicalHeight, displayInfo.displayCutout, mTmpInsets);
Evan Rosky1ac84462018-11-13 11:25:30 -08002041 intersectWithInsetsIfFits(outNonDecorBounds, mTmpBounds, mTmpInsets);
Riddle Hsu61987bc2019-04-03 13:08:47 +08002042
2043 policy.convertNonDecorInsetsToStableInsets(mTmpInsets, displayInfo.rotation);
2044 intersectWithInsetsIfFits(outStableBounds, mTmpBounds, mTmpInsets);
Evan Rosky1ac84462018-11-13 11:25:30 -08002045 }
2046
2047 /**
2048 * Asks docked-divider controller for the smallestwidthdp given bounds.
2049 * @param bounds bounds to calculate smallestwidthdp for.
2050 */
2051 private int getSmallestScreenWidthDpForDockedBounds(Rect bounds) {
2052 DisplayContent dc = mStack.getDisplay().mDisplayContent;
2053 if (dc != null) {
2054 return dc.getDockedDividerController().getSmallestWidthDpForBounds(bounds);
2055 }
2056 return Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
2057 }
2058
Riddle Hsu0a343c32018-12-21 00:40:48 +08002059 void computeConfigResourceOverrides(@NonNull Configuration inOutConfig,
2060 @NonNull Configuration parentConfig) {
Riddle Hsu61987bc2019-04-03 13:08:47 +08002061 computeConfigResourceOverrides(inOutConfig, parentConfig, null /* compatInsets */);
Riddle Hsu0a343c32018-12-21 00:40:48 +08002062 }
2063
Evan Rosky1ac84462018-11-13 11:25:30 -08002064 /**
2065 * Calculates configuration values used by the client to get resources. This should be run
2066 * using app-facing bounds (bounds unmodified by animations or transient interactions).
2067 *
2068 * This assumes bounds are non-empty/null. For the null-bounds case, the caller is likely
2069 * configuring an "inherit-bounds" window which means that all configuration settings would
2070 * just be inherited from the parent configuration.
2071 **/
Evan Rosky730f6e82018-12-03 17:40:11 -08002072 void computeConfigResourceOverrides(@NonNull Configuration inOutConfig,
Riddle Hsu61987bc2019-04-03 13:08:47 +08002073 @NonNull Configuration parentConfig,
2074 @Nullable ActivityRecord.CompatDisplayInsets compatInsets) {
Evan Rosky1ac84462018-11-13 11:25:30 -08002075 int windowingMode = inOutConfig.windowConfiguration.getWindowingMode();
2076 if (windowingMode == WINDOWING_MODE_UNDEFINED) {
2077 windowingMode = parentConfig.windowConfiguration.getWindowingMode();
Winson Chungbdc646f2017-02-13 12:12:22 -08002078 }
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002079
Evan Rosky1ac84462018-11-13 11:25:30 -08002080 float density = inOutConfig.densityDpi;
2081 if (density == Configuration.DENSITY_DPI_UNDEFINED) {
2082 density = parentConfig.densityDpi;
2083 }
2084 density *= DisplayMetrics.DENSITY_DEFAULT_SCALE;
Winson Chung60c1aba2017-03-14 17:47:42 -07002085
Evan Rosky730f6e82018-12-03 17:40:11 -08002086 final Rect bounds = inOutConfig.windowConfiguration.getBounds();
Evan Rosky1ac84462018-11-13 11:25:30 -08002087 Rect outAppBounds = inOutConfig.windowConfiguration.getAppBounds();
2088 if (outAppBounds == null || outAppBounds.isEmpty()) {
2089 inOutConfig.windowConfiguration.setAppBounds(bounds);
2090 outAppBounds = inOutConfig.windowConfiguration.getAppBounds();
2091 }
Riddle Hsu61987bc2019-04-03 13:08:47 +08002092 // Non-null compatibility insets means the activity prefers to keep its original size, so
2093 // the out bounds doesn't need to be restricted by the parent.
2094 final boolean insideParentBounds = compatInsets == null;
Riddle Hsu0a343c32018-12-21 00:40:48 +08002095 if (insideParentBounds && windowingMode != WINDOWING_MODE_FREEFORM) {
Evan Rosky1ac84462018-11-13 11:25:30 -08002096 final Rect parentAppBounds = parentConfig.windowConfiguration.getAppBounds();
2097 if (parentAppBounds != null && !parentAppBounds.isEmpty()) {
2098 outAppBounds.intersect(parentAppBounds);
2099 }
2100 }
2101
2102 if (inOutConfig.screenWidthDp == Configuration.SCREEN_WIDTH_DP_UNDEFINED
2103 || inOutConfig.screenHeightDp == Configuration.SCREEN_HEIGHT_DP_UNDEFINED) {
Riddle Hsu0a343c32018-12-21 00:40:48 +08002104 if (insideParentBounds && mStack != null) {
Evan Rosky1ac84462018-11-13 11:25:30 -08002105 final DisplayInfo di = new DisplayInfo();
2106 mStack.getDisplay().mDisplay.getDisplayInfo(di);
2107
2108 // For calculating screenWidthDp, screenWidthDp, we use the stable inset screen
2109 // area, i.e. the screen area without the system bars.
2110 // The non decor inset are areas that could never be removed in Honeycomb. See
2111 // {@link WindowManagerPolicy#getNonDecorInsetsLw}.
2112 calculateInsetFrames(mTmpNonDecorBounds, mTmpStableBounds, bounds, di);
2113 } else {
Riddle Hsu61987bc2019-04-03 13:08:47 +08002114 // Apply the given non-decor and stable insets to calculate the corresponding bounds
2115 // for screen size of configuration.
2116 final int rotation = parentConfig.windowConfiguration.getRotation();
2117 if (rotation != ROTATION_UNDEFINED && compatInsets != null) {
Riddle Hsu74826262019-04-17 14:57:42 +08002118 mTmpNonDecorBounds.set(bounds);
2119 mTmpStableBounds.set(bounds);
2120 compatInsets.getDisplayBoundsByRotation(mTmpBounds, rotation);
Riddle Hsu61987bc2019-04-03 13:08:47 +08002121 intersectWithInsetsIfFits(mTmpNonDecorBounds, mTmpBounds,
2122 compatInsets.mNonDecorInsets[rotation]);
2123 intersectWithInsetsIfFits(mTmpStableBounds, mTmpBounds,
2124 compatInsets.mStableInsets[rotation]);
Riddle Hsu74826262019-04-17 14:57:42 +08002125 outAppBounds.set(mTmpNonDecorBounds);
2126 } else {
2127 // Set to app bounds because it excludes decor insets.
2128 mTmpNonDecorBounds.set(outAppBounds);
2129 mTmpStableBounds.set(outAppBounds);
Riddle Hsu61987bc2019-04-03 13:08:47 +08002130 }
Evan Rosky1ac84462018-11-13 11:25:30 -08002131 }
2132
2133 if (inOutConfig.screenWidthDp == Configuration.SCREEN_WIDTH_DP_UNDEFINED) {
Riddle Hsu0a343c32018-12-21 00:40:48 +08002134 final int overrideScreenWidthDp = (int) (mTmpStableBounds.width() / density);
2135 inOutConfig.screenWidthDp = insideParentBounds
2136 ? Math.min(overrideScreenWidthDp, parentConfig.screenWidthDp)
2137 : overrideScreenWidthDp;
Evan Rosky1ac84462018-11-13 11:25:30 -08002138 }
2139 if (inOutConfig.screenHeightDp == Configuration.SCREEN_HEIGHT_DP_UNDEFINED) {
Riddle Hsu0a343c32018-12-21 00:40:48 +08002140 final int overrideScreenHeightDp = (int) (mTmpStableBounds.height() / density);
2141 inOutConfig.screenHeightDp = insideParentBounds
Riddle Hsu88e3c8732019-02-18 19:15:12 +08002142 ? Math.min(overrideScreenHeightDp, parentConfig.screenHeightDp)
Riddle Hsu0a343c32018-12-21 00:40:48 +08002143 : overrideScreenHeightDp;
Evan Rosky1ac84462018-11-13 11:25:30 -08002144 }
2145
2146 if (inOutConfig.smallestScreenWidthDp
2147 == Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
2148 if (WindowConfiguration.isFloating(windowingMode)) {
2149 // For floating tasks, calculate the smallest width from the bounds of the task
2150 inOutConfig.smallestScreenWidthDp = (int) (
2151 Math.min(bounds.width(), bounds.height()) / density);
2152 } else if (WindowConfiguration.isSplitScreenWindowingMode(windowingMode)) {
2153 // Iterating across all screen orientations, and return the minimum of the task
2154 // width taking into account that the bounds might change because the snap
2155 // algorithm snaps to a different value
Evan Rosky730f6e82018-12-03 17:40:11 -08002156 inOutConfig.smallestScreenWidthDp =
2157 getSmallestScreenWidthDpForDockedBounds(bounds);
Evan Rosky1ac84462018-11-13 11:25:30 -08002158 }
2159 // otherwise, it will just inherit
2160 }
2161 }
2162
Evan Rosky730f6e82018-12-03 17:40:11 -08002163 if (inOutConfig.orientation == ORIENTATION_UNDEFINED) {
Evan Rosky1ac84462018-11-13 11:25:30 -08002164 inOutConfig.orientation = (inOutConfig.screenWidthDp <= inOutConfig.screenHeightDp)
Riddle Hsu0a343c32018-12-21 00:40:48 +08002165 ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Evan Rosky1ac84462018-11-13 11:25:30 -08002166 }
2167 if (inOutConfig.screenLayout == Configuration.SCREENLAYOUT_UNDEFINED) {
2168 // For calculating screen layout, we need to use the non-decor inset screen area for the
2169 // calculation for compatibility reasons, i.e. screen area without system bars that
2170 // could never go away in Honeycomb.
2171 final int compatScreenWidthDp = (int) (mTmpNonDecorBounds.width() / density);
2172 final int compatScreenHeightDp = (int) (mTmpNonDecorBounds.height() / density);
2173 // We're only overriding LONG, SIZE and COMPAT parts of screenLayout, so we start
2174 // override calculation with partial default.
2175 // Reducing the screen layout starting from its parent config.
2176 final int sl = parentConfig.screenLayout
2177 & (Configuration.SCREENLAYOUT_LONG_MASK | Configuration.SCREENLAYOUT_SIZE_MASK);
2178 final int longSize = Math.max(compatScreenHeightDp, compatScreenWidthDp);
2179 final int shortSize = Math.min(compatScreenHeightDp, compatScreenWidthDp);
2180 inOutConfig.screenLayout = Configuration.reduceScreenLayout(sl, longSize, shortSize);
2181 }
2182 }
2183
Evan Rosky1ac84462018-11-13 11:25:30 -08002184 @Override
2185 void resolveOverrideConfiguration(Configuration newParentConfig) {
Evan Rosky730f6e82018-12-03 17:40:11 -08002186 mTmpBounds.set(getResolvedOverrideConfiguration().windowConfiguration.getBounds());
2187 super.resolveOverrideConfiguration(newParentConfig);
2188 int windowingMode =
2189 getRequestedOverrideConfiguration().windowConfiguration.getWindowingMode();
2190 if (windowingMode == WINDOWING_MODE_UNDEFINED) {
2191 windowingMode = newParentConfig.windowConfiguration.getWindowingMode();
2192 }
2193 Rect outOverrideBounds =
2194 getResolvedOverrideConfiguration().windowConfiguration.getBounds();
2195
2196 if (windowingMode == WINDOWING_MODE_FULLSCREEN) {
Riddle Hsu74826262019-04-17 14:57:42 +08002197 computeFullscreenBounds(outOverrideBounds, null /* refActivity */,
2198 newParentConfig.windowConfiguration.getBounds(),
2199 newParentConfig.orientation);
Evan Rosky730f6e82018-12-03 17:40:11 -08002200 }
2201
2202 if (outOverrideBounds.isEmpty()) {
2203 // If the task fills the parent, just inherit all the other configs from parent.
2204 return;
2205 }
2206
2207 adjustForMinimalTaskDimensions(outOverrideBounds, mTmpBounds);
2208 if (windowingMode == WINDOWING_MODE_FREEFORM) {
2209 // by policy, make sure the window remains within parent somewhere
Evan Rosky60dba2f2019-02-01 10:58:38 -08002210 final float density =
2211 ((float) newParentConfig.densityDpi) / DisplayMetrics.DENSITY_DEFAULT;
2212 fitWithinBounds(outOverrideBounds, newParentConfig.windowConfiguration.getBounds(),
2213 (int) (density * WindowState.MINIMUM_VISIBLE_WIDTH_IN_DP),
2214 (int) (density * WindowState.MINIMUM_VISIBLE_HEIGHT_IN_DP));
Evan Rosky730f6e82018-12-03 17:40:11 -08002215 }
2216 computeConfigResourceOverrides(getResolvedOverrideConfiguration(), newParentConfig);
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08002217 }
2218
Riddle Hsu74826262019-04-17 14:57:42 +08002219 /** @see WindowContainer#handlesOrientationChangeFromDescendant */
2220 boolean handlesOrientationChangeFromDescendant() {
2221 return mTask != null && mTask.getParent() != null
2222 && mTask.getParent().handlesOrientationChangeFromDescendant();
2223 }
2224
2225 /**
2226 * Compute bounds (letterbox or pillarbox) for {@link #WINDOWING_MODE_FULLSCREEN} when the
2227 * parent doesn't handle the orientation change and the requested orientation is different from
2228 * the parent.
2229 */
2230 void computeFullscreenBounds(@NonNull Rect outBounds, @Nullable ActivityRecord refActivity,
2231 @NonNull Rect parentBounds, int parentOrientation) {
2232 // In FULLSCREEN mode, always start with empty bounds to indicate "fill parent".
2233 outBounds.setEmpty();
2234 if (handlesOrientationChangeFromDescendant()) {
2235 return;
2236 }
2237 if (refActivity == null) {
2238 // Use the top activity as the reference of orientation. Don't include overlays because
2239 // it is usually not the actual content or just temporarily shown.
2240 // E.g. ForcedResizableInfoActivity.
2241 refActivity = getTopActivity(false /* includeOverlays */);
2242 }
2243
2244 // If the task or the reference activity requires a different orientation (either by
2245 // override or activityInfo), make it fit the available bounds by scaling down its bounds.
2246 final int overrideOrientation = getRequestedOverrideConfiguration().orientation;
2247 final int forcedOrientation =
2248 (overrideOrientation != ORIENTATION_UNDEFINED || refActivity == null)
2249 ? overrideOrientation : refActivity.getRequestedConfigurationOrientation();
2250 if (forcedOrientation == ORIENTATION_UNDEFINED || forcedOrientation == parentOrientation) {
2251 return;
2252 }
2253
2254 final int parentWidth = parentBounds.width();
2255 final int parentHeight = parentBounds.height();
2256 final float aspect = ((float) parentHeight) / parentWidth;
2257 if (forcedOrientation == ORIENTATION_LANDSCAPE) {
2258 final int height = (int) (parentWidth / aspect);
2259 final int top = parentBounds.centerY() - height / 2;
2260 outBounds.set(parentBounds.left, top, parentBounds.right, top + height);
2261 } else {
2262 final int width = (int) (parentHeight * aspect);
2263 final int left = parentBounds.centerX() - width / 2;
2264 outBounds.set(left, parentBounds.top, left + width, parentBounds.bottom);
2265 }
2266 }
2267
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002268 Rect updateOverrideConfigurationFromLaunchBounds() {
Bryce Leef3c6a472017-11-14 14:53:06 -08002269 final Rect bounds = getLaunchBounds();
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002270 updateOverrideConfiguration(bounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08002271 if (bounds != null && !bounds.isEmpty()) {
2272 // TODO: Review if we actually want to do this - we are setting the launch bounds
2273 // directly here.
Evan Roskydfe3da72018-10-26 17:21:06 -07002274 bounds.set(getRequestedOverrideBounds());
Andrii Kulian73336d812016-03-24 12:56:08 -07002275 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002276 return bounds;
2277 }
2278
Wale Ogunwale935e5022015-11-10 12:36:10 -08002279 /** Updates the task's bounds and override configuration to match what is expected for the
2280 * input stack. */
2281 void updateOverrideConfigurationForStack(ActivityStack inStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002282 if (mStack != null && mStack == inStack) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08002283 return;
2284 }
2285
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002286 if (inStack.inFreeformWindowingMode()) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002287 if (!isResizeable()) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08002288 throw new IllegalArgumentException("Can not position non-resizeable task="
2289 + this + " in stack=" + inStack);
2290 }
Bryce Leef3c6a472017-11-14 14:53:06 -08002291 if (!matchParentBounds()) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08002292 return;
2293 }
2294 if (mLastNonFullscreenBounds != null) {
2295 updateOverrideConfiguration(mLastNonFullscreenBounds);
2296 } else {
Bryce Leeec55eb02017-12-05 20:51:27 -08002297 mService.mStackSupervisor.getLaunchParamsController().layoutTask(this, null);
Wale Ogunwale935e5022015-11-10 12:36:10 -08002298 }
2299 } else {
Evan Roskydfe3da72018-10-26 17:21:06 -07002300 updateOverrideConfiguration(inStack.getRequestedOverrideBounds());
Wale Ogunwale935e5022015-11-10 12:36:10 -08002301 }
2302 }
2303
Wale Ogunwale706ed792015-08-02 10:29:44 -07002304 /** Returns the bounds that should be used to launch this task. */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002305 Rect getLaunchBounds() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002306 if (mStack == null) {
Chong Zhang7d5f5102016-01-13 10:29:24 -08002307 return null;
2308 }
2309
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002310 final int windowingMode = getWindowingMode();
2311 if (!isActivityTypeStandardOrUndefined()
2312 || windowingMode == WINDOWING_MODE_FULLSCREEN
2313 || (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY && !isResizeable())) {
Evan Roskydfe3da72018-10-26 17:21:06 -07002314 return isResizeable() ? mStack.getRequestedOverrideBounds() : null;
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002315 } else if (!getWindowConfiguration().persistTaskBounds()) {
Evan Roskydfe3da72018-10-26 17:21:06 -07002316 return mStack.getRequestedOverrideBounds();
Wale Ogunwale706ed792015-08-02 10:29:44 -07002317 }
2318 return mLastNonFullscreenBounds;
2319 }
2320
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01002321 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
2322 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2323 final ActivityRecord r = mActivities.get(activityNdx);
2324 if (r.visible) {
2325 r.showStartingWindow(null /* prev */, false /* newTask */, taskSwitch);
2326 }
2327 }
2328 }
2329
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002330 void setRootProcess(WindowProcessController proc) {
Dianne Hackborn68a06332017-11-15 17:54:18 -08002331 clearRootProcess();
2332 if (intent != null &&
2333 (intent.getFlags() & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0) {
2334 mRootProcess = proc;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002335 mRootProcess.addRecentTask(this);
Dianne Hackborn68a06332017-11-15 17:54:18 -08002336 }
2337 }
2338
2339 void clearRootProcess() {
2340 if (mRootProcess != null) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002341 mRootProcess.removeRecentTask(this);
Dianne Hackborn68a06332017-11-15 17:54:18 -08002342 mRootProcess = null;
2343 }
2344 }
2345
chaviw82a0ba82018-03-15 14:26:29 -07002346 void clearAllPendingOptions() {
2347 for (int i = getChildCount() - 1; i >= 0; i--) {
2348 getChildAt(i).clearOptionsLocked(false /* withAbort */);
2349 }
2350 }
2351
Winson Chungabfdcce2018-07-02 17:23:33 -07002352 /**
2353 * Fills in a {@link TaskInfo} with information from this task.
2354 * @param info the {@link TaskInfo} to fill in
Winson Chungabfdcce2018-07-02 17:23:33 -07002355 */
Mark Renoufc808f062019-02-07 15:20:37 -05002356 void fillTaskInfo(TaskInfo info) {
2357 getNumRunningActivities(mReuseActivitiesReport);
Winson Chungabfdcce2018-07-02 17:23:33 -07002358 info.userId = userId;
2359 info.stackId = getStackId();
2360 info.taskId = taskId;
Mark Renoufb1abb552019-02-08 13:51:41 -05002361 info.displayId = mStack == null ? Display.INVALID_DISPLAY : mStack.mDisplayId;
Winson Chungabfdcce2018-07-02 17:23:33 -07002362 info.isRunning = getTopActivity() != null;
Riddle Hsu2f9acd22018-11-06 23:44:43 +08002363 info.baseIntent = new Intent(getBaseIntent());
Mark Renoufc808f062019-02-07 15:20:37 -05002364 info.baseActivity = mReuseActivitiesReport.base != null
2365 ? mReuseActivitiesReport.base.intent.getComponent()
Winson Chungabfdcce2018-07-02 17:23:33 -07002366 : null;
Mark Renoufc808f062019-02-07 15:20:37 -05002367 info.topActivity = mReuseActivitiesReport.top != null
2368 ? mReuseActivitiesReport.top.mActivityComponent
Winson Chungabfdcce2018-07-02 17:23:33 -07002369 : null;
2370 info.origActivity = origActivity;
2371 info.realActivity = realActivity;
Mark Renoufc808f062019-02-07 15:20:37 -05002372 info.numActivities = mReuseActivitiesReport.numActivities;
Winson Chungabfdcce2018-07-02 17:23:33 -07002373 info.lastActiveTime = lastActiveTime;
2374 info.taskDescription = new ActivityManager.TaskDescription(lastTaskDescription);
2375 info.supportsSplitScreenMultiWindow = supportsSplitScreenWindowingMode();
2376 info.resizeMode = mResizeMode;
2377 info.configuration.setTo(getConfiguration());
2378 }
2379
Mark Renoufc808f062019-02-07 15:20:37 -05002380 /**
2381 * Returns a {@link TaskInfo} with information from this task.
2382 */
2383 ActivityManager.RunningTaskInfo getTaskInfo() {
2384 ActivityManager.RunningTaskInfo info = new ActivityManager.RunningTaskInfo();
2385 fillTaskInfo(info);
2386 return info;
2387 }
2388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002390 pw.print(prefix); pw.print("userId="); pw.print(userId);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07002391 pw.print(" effectiveUid="); UserHandle.formatUid(pw, effectiveUid);
2392 pw.print(" mCallingUid="); UserHandle.formatUid(pw, mCallingUid);
Suprabh Shukla7745c142016-03-07 18:21:10 -08002393 pw.print(" mUserSetupComplete="); pw.print(mUserSetupComplete);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002394 pw.print(" mCallingPackage="); pw.println(mCallingPackage);
Dianne Hackborn79228822014-09-16 11:11:23 -07002395 if (affinity != null || rootAffinity != null) {
2396 pw.print(prefix); pw.print("affinity="); pw.print(affinity);
2397 if (affinity == null || !affinity.equals(rootAffinity)) {
2398 pw.print(" root="); pw.println(rootAffinity);
2399 } else {
2400 pw.println();
2401 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002402 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002403 if (voiceSession != null || voiceInteractor != null) {
2404 pw.print(prefix); pw.print("VOICE: session=0x");
2405 pw.print(Integer.toHexString(System.identityHashCode(voiceSession)));
2406 pw.print(" interactor=0x");
2407 pw.println(Integer.toHexString(System.identityHashCode(voiceInteractor)));
2408 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002409 if (intent != null) {
2410 StringBuilder sb = new StringBuilder(128);
2411 sb.append(prefix); sb.append("intent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08002412 intent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002413 sb.append('}');
2414 pw.println(sb.toString());
2415 }
2416 if (affinityIntent != null) {
2417 StringBuilder sb = new StringBuilder(128);
2418 sb.append(prefix); sb.append("affinityIntent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08002419 affinityIntent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002420 sb.append('}');
2421 pw.println(sb.toString());
2422 }
2423 if (origActivity != null) {
2424 pw.print(prefix); pw.print("origActivity=");
2425 pw.println(origActivity.flattenToShortString());
2426 }
2427 if (realActivity != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002428 pw.print(prefix); pw.print("mActivityComponent=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002429 pw.println(realActivity.flattenToShortString());
2430 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07002431 if (autoRemoveRecents || isPersistable || !isActivityTypeStandard() || numFullscreen != 0) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002432 pw.print(prefix); pw.print("autoRemoveRecents="); pw.print(autoRemoveRecents);
Dianne Hackborn852975d2014-08-22 17:42:43 -07002433 pw.print(" isPersistable="); pw.print(isPersistable);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002434 pw.print(" numFullscreen="); pw.print(numFullscreen);
Wale Ogunwale66e16852017-10-19 13:35:52 -07002435 pw.print(" activityType="); pw.println(getActivityType());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002436 }
Craig Mautner432f64e2015-05-20 14:59:57 -07002437 if (rootWasReset || mNeverRelinquishIdentity || mReuseTask
2438 || mLockTaskAuth != LOCK_TASK_AUTH_PINNABLE) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002439 pw.print(prefix); pw.print("rootWasReset="); pw.print(rootWasReset);
2440 pw.print(" mNeverRelinquishIdentity="); pw.print(mNeverRelinquishIdentity);
Craig Mautner432f64e2015-05-20 14:59:57 -07002441 pw.print(" mReuseTask="); pw.print(mReuseTask);
2442 pw.print(" mLockTaskAuth="); pw.println(lockTaskAuthToString());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002443 }
Wale Ogunwale18795a22014-12-03 11:38:33 -08002444 if (mAffiliatedTaskId != taskId || mPrevAffiliateTaskId != INVALID_TASK_ID
2445 || mPrevAffiliate != null || mNextAffiliateTaskId != INVALID_TASK_ID
2446 || mNextAffiliate != null) {
Dianne Hackborn852975d2014-08-22 17:42:43 -07002447 pw.print(prefix); pw.print("affiliation="); pw.print(mAffiliatedTaskId);
2448 pw.print(" prevAffiliation="); pw.print(mPrevAffiliateTaskId);
2449 pw.print(" (");
2450 if (mPrevAffiliate == null) {
2451 pw.print("null");
2452 } else {
2453 pw.print(Integer.toHexString(System.identityHashCode(mPrevAffiliate)));
2454 }
2455 pw.print(") nextAffiliation="); pw.print(mNextAffiliateTaskId);
2456 pw.print(" (");
2457 if (mNextAffiliate == null) {
2458 pw.print("null");
2459 } else {
2460 pw.print(Integer.toHexString(System.identityHashCode(mNextAffiliate)));
2461 }
2462 pw.println(")");
2463 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002464 pw.print(prefix); pw.print("Activities="); pw.println(mActivities);
Dianne Hackborn852975d2014-08-22 17:42:43 -07002465 if (!askedCompatMode || !inRecents || !isAvailable) {
2466 pw.print(prefix); pw.print("askedCompatMode="); pw.print(askedCompatMode);
2467 pw.print(" inRecents="); pw.print(inRecents);
2468 pw.print(" isAvailable="); pw.println(isAvailable);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07002469 }
Dianne Hackborn852975d2014-08-22 17:42:43 -07002470 if (lastDescription != null) {
2471 pw.print(prefix); pw.print("lastDescription="); pw.println(lastDescription);
2472 }
Dianne Hackborn68a06332017-11-15 17:54:18 -08002473 if (mRootProcess != null) {
2474 pw.print(prefix); pw.print("mRootProcess="); pw.println(mRootProcess);
2475 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002476 pw.print(prefix); pw.print("stackId="); pw.println(getStackId());
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002477 pw.print(prefix + "hasBeenVisible=" + hasBeenVisible);
2478 pw.print(" mResizeMode=" + ActivityInfo.resizeModeToString(mResizeMode));
Winson Chungd3395382016-12-13 11:49:09 -08002479 pw.print(" mSupportsPictureInPicture=" + mSupportsPictureInPicture);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002480 pw.print(" isResizeable=" + isResizeable());
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002481 pw.print(" lastActiveTime=" + lastActiveTime);
2482 pw.println(" (inactive for " + (getInactiveDuration() / 1000) + "s)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 }
2484
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002485 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002487 StringBuilder sb = new StringBuilder(128);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002488 if (stringName != null) {
2489 sb.append(stringName);
2490 sb.append(" U=");
2491 sb.append(userId);
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002492 sb.append(" StackId=");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002493 sb.append(getStackId());
Craig Mautnerde4ef022013-04-07 19:01:33 -07002494 sb.append(" sz=");
2495 sb.append(mActivities.size());
2496 sb.append('}');
2497 return sb.toString();
2498 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002499 sb.append("TaskRecord{");
2500 sb.append(Integer.toHexString(System.identityHashCode(this)));
2501 sb.append(" #");
2502 sb.append(taskId);
2503 if (affinity != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002504 sb.append(" A=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002505 sb.append(affinity);
2506 } else if (intent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002507 sb.append(" I=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002508 sb.append(intent.getComponent().flattenToShortString());
Bryce Leefbd263b42018-03-07 10:33:55 -08002509 } else if (affinityIntent != null && affinityIntent.getComponent() != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002510 sb.append(" aI=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002511 sb.append(affinityIntent.getComponent().flattenToShortString());
2512 } else {
2513 sb.append(" ??");
2514 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002515 stringName = sb.toString();
2516 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07002518
Nataniel Borges023ecb52019-01-16 14:15:43 -08002519 public void writeToProto(ProtoOutputStream proto, long fieldId,
2520 @WindowTraceLogLevel int logLevel) {
2521 if (logLevel == WindowTraceLogLevel.CRITICAL && !isVisible()) {
2522 return;
2523 }
2524
Steven Timotius4346f0a2017-09-12 11:07:21 -07002525 final long token = proto.start(fieldId);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002526 super.writeToProto(proto, CONFIGURATION_CONTAINER, logLevel);
Steven Timotius4346f0a2017-09-12 11:07:21 -07002527 proto.write(ID, taskId);
2528 for (int i = mActivities.size() - 1; i >= 0; i--) {
2529 ActivityRecord activity = mActivities.get(i);
2530 activity.writeToProto(proto, ACTIVITIES);
2531 }
2532 proto.write(STACK_ID, mStack.mStackId);
2533 if (mLastNonFullscreenBounds != null) {
2534 mLastNonFullscreenBounds.writeToProto(proto, LAST_NON_FULLSCREEN_BOUNDS);
2535 }
2536 if (realActivity != null) {
2537 proto.write(REAL_ACTIVITY, realActivity.flattenToShortString());
2538 }
2539 if (origActivity != null) {
2540 proto.write(ORIG_ACTIVITY, origActivity.flattenToShortString());
2541 }
2542 proto.write(ACTIVITY_TYPE, getActivityType());
Steven Timotius4346f0a2017-09-12 11:07:21 -07002543 proto.write(RESIZE_MODE, mResizeMode);
Bryce Leef3c6a472017-11-14 14:53:06 -08002544 // TODO: Remove, no longer needed with windowingMode.
2545 proto.write(FULLSCREEN, matchParentBounds());
2546
2547 if (!matchParentBounds()) {
Evan Roskydfe3da72018-10-26 17:21:06 -07002548 final Rect bounds = getRequestedOverrideBounds();
Bryce Leef3c6a472017-11-14 14:53:06 -08002549 bounds.writeToProto(proto, BOUNDS);
Steven Timotius4346f0a2017-09-12 11:07:21 -07002550 }
2551 proto.write(MIN_WIDTH, mMinWidth);
2552 proto.write(MIN_HEIGHT, mMinHeight);
2553 proto.end(token);
2554 }
Winson Chung61c9e5a2017-10-11 10:39:32 -07002555
2556 /**
2557 * See {@link #getNumRunningActivities(TaskActivitiesReport)}.
2558 */
2559 static class TaskActivitiesReport {
2560 int numRunning;
2561 int numActivities;
2562 ActivityRecord top;
2563 ActivityRecord base;
2564
2565 void reset() {
2566 numRunning = numActivities = 0;
2567 top = base = null;
2568 }
2569 }
Garfield Tan9b1efea2017-12-05 16:43:46 -08002570
2571 /**
2572 * Saves this {@link TaskRecord} to XML using given serializer.
2573 */
2574 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
2575 if (DEBUG_RECENTS) Slog.i(TAG_RECENTS, "Saving task=" + this);
2576
2577 out.attribute(null, ATTR_TASKID, String.valueOf(taskId));
2578 if (realActivity != null) {
2579 out.attribute(null, ATTR_REALACTIVITY, realActivity.flattenToShortString());
2580 }
2581 out.attribute(null, ATTR_REALACTIVITY_SUSPENDED, String.valueOf(realActivitySuspended));
2582 if (origActivity != null) {
2583 out.attribute(null, ATTR_ORIGACTIVITY, origActivity.flattenToShortString());
2584 }
2585 // Write affinity, and root affinity if it is different from affinity.
2586 // We use the special string "@" for a null root affinity, so we can identify
2587 // later whether we were given a root affinity or should just make it the
2588 // same as the affinity.
2589 if (affinity != null) {
2590 out.attribute(null, ATTR_AFFINITY, affinity);
2591 if (!affinity.equals(rootAffinity)) {
2592 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
2593 }
2594 } else if (rootAffinity != null) {
2595 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
2596 }
2597 out.attribute(null, ATTR_ROOTHASRESET, String.valueOf(rootWasReset));
2598 out.attribute(null, ATTR_AUTOREMOVERECENTS, String.valueOf(autoRemoveRecents));
2599 out.attribute(null, ATTR_ASKEDCOMPATMODE, String.valueOf(askedCompatMode));
2600 out.attribute(null, ATTR_USERID, String.valueOf(userId));
2601 out.attribute(null, ATTR_USER_SETUP_COMPLETE, String.valueOf(mUserSetupComplete));
2602 out.attribute(null, ATTR_EFFECTIVE_UID, String.valueOf(effectiveUid));
2603 out.attribute(null, ATTR_LASTTIMEMOVED, String.valueOf(mLastTimeMoved));
2604 out.attribute(null, ATTR_NEVERRELINQUISH, String.valueOf(mNeverRelinquishIdentity));
2605 if (lastDescription != null) {
2606 out.attribute(null, ATTR_LASTDESCRIPTION, lastDescription.toString());
2607 }
2608 if (lastTaskDescription != null) {
2609 lastTaskDescription.saveToXml(out);
2610 }
2611 out.attribute(null, ATTR_TASK_AFFILIATION_COLOR, String.valueOf(mAffiliatedTaskColor));
2612 out.attribute(null, ATTR_TASK_AFFILIATION, String.valueOf(mAffiliatedTaskId));
2613 out.attribute(null, ATTR_PREV_AFFILIATION, String.valueOf(mPrevAffiliateTaskId));
2614 out.attribute(null, ATTR_NEXT_AFFILIATION, String.valueOf(mNextAffiliateTaskId));
2615 out.attribute(null, ATTR_CALLING_UID, String.valueOf(mCallingUid));
2616 out.attribute(null, ATTR_CALLING_PACKAGE, mCallingPackage == null ? "" : mCallingPackage);
2617 out.attribute(null, ATTR_RESIZE_MODE, String.valueOf(mResizeMode));
2618 out.attribute(null, ATTR_SUPPORTS_PICTURE_IN_PICTURE,
2619 String.valueOf(mSupportsPictureInPicture));
2620 if (mLastNonFullscreenBounds != null) {
2621 out.attribute(
2622 null, ATTR_NON_FULLSCREEN_BOUNDS, mLastNonFullscreenBounds.flattenToString());
2623 }
2624 out.attribute(null, ATTR_MIN_WIDTH, String.valueOf(mMinWidth));
2625 out.attribute(null, ATTR_MIN_HEIGHT, String.valueOf(mMinHeight));
2626 out.attribute(null, ATTR_PERSIST_TASK_VERSION, String.valueOf(PERSIST_TASK_VERSION));
2627
2628 if (affinityIntent != null) {
2629 out.startTag(null, TAG_AFFINITYINTENT);
2630 affinityIntent.saveToXml(out);
2631 out.endTag(null, TAG_AFFINITYINTENT);
2632 }
2633
Bryce Lee1a990e52018-04-23 10:54:11 -07002634 if (intent != null) {
2635 out.startTag(null, TAG_INTENT);
2636 intent.saveToXml(out);
2637 out.endTag(null, TAG_INTENT);
2638 }
Garfield Tan9b1efea2017-12-05 16:43:46 -08002639
2640 final ArrayList<ActivityRecord> activities = mActivities;
2641 final int numActivities = activities.size();
2642 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2643 final ActivityRecord r = activities.get(activityNdx);
2644 if (r.info.persistableMode == ActivityInfo.PERSIST_ROOT_ONLY || !r.isPersistable() ||
2645 ((r.intent.getFlags() & FLAG_ACTIVITY_NEW_DOCUMENT
2646 | FLAG_ACTIVITY_RETAIN_IN_RECENTS) == FLAG_ACTIVITY_NEW_DOCUMENT) &&
2647 activityNdx > 0) {
2648 // Stop at first non-persistable or first break in task (CLEAR_WHEN_TASK_RESET).
2649 break;
2650 }
2651 out.startTag(null, TAG_ACTIVITY);
2652 r.saveToXml(out);
2653 out.endTag(null, TAG_ACTIVITY);
2654 }
2655 }
2656
2657 @VisibleForTesting
2658 static TaskRecordFactory getTaskRecordFactory() {
2659 if (sTaskRecordFactory == null) {
2660 setTaskRecordFactory(new TaskRecordFactory());
2661 }
2662 return sTaskRecordFactory;
2663 }
2664
2665 static void setTaskRecordFactory(TaskRecordFactory factory) {
2666 sTaskRecordFactory = factory;
2667 }
2668
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07002669 static TaskRecord create(ActivityTaskManagerService service, int taskId, ActivityInfo info,
Garfield Tan9b1efea2017-12-05 16:43:46 -08002670 Intent intent, IVoiceInteractionSession voiceSession,
2671 IVoiceInteractor voiceInteractor) {
2672 return getTaskRecordFactory().create(
2673 service, taskId, info, intent, voiceSession, voiceInteractor);
2674 }
2675
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07002676 static TaskRecord create(ActivityTaskManagerService service, int taskId, ActivityInfo info,
Garfield Tan9b1efea2017-12-05 16:43:46 -08002677 Intent intent, TaskDescription taskDescription) {
2678 return getTaskRecordFactory().create(service, taskId, info, intent, taskDescription);
2679 }
2680
2681 static TaskRecord restoreFromXml(XmlPullParser in, ActivityStackSupervisor stackSupervisor)
2682 throws IOException, XmlPullParserException {
2683 return getTaskRecordFactory().restoreFromXml(in, stackSupervisor);
2684 }
2685
2686 /**
2687 * A factory class used to create {@link TaskRecord} or its subclass if any. This can be
2688 * specified when system boots by setting it with
2689 * {@link #setTaskRecordFactory(TaskRecordFactory)}.
2690 */
2691 static class TaskRecordFactory {
2692
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07002693 TaskRecord create(ActivityTaskManagerService service, int taskId, ActivityInfo info,
Garfield Tan9b1efea2017-12-05 16:43:46 -08002694 Intent intent, IVoiceInteractionSession voiceSession,
2695 IVoiceInteractor voiceInteractor) {
2696 return new TaskRecord(
2697 service, taskId, info, intent, voiceSession, voiceInteractor);
2698 }
2699
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07002700 TaskRecord create(ActivityTaskManagerService service, int taskId, ActivityInfo info,
Garfield Tan9b1efea2017-12-05 16:43:46 -08002701 Intent intent, TaskDescription taskDescription) {
2702 return new TaskRecord(service, taskId, info, intent, taskDescription);
2703 }
2704
2705 /**
2706 * Should only be used when we're restoring {@link TaskRecord} from storage.
2707 */
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07002708 TaskRecord create(ActivityTaskManagerService service, int taskId, Intent intent,
Garfield Tan9b1efea2017-12-05 16:43:46 -08002709 Intent affinityIntent, String affinity, String rootAffinity,
2710 ComponentName realActivity, ComponentName origActivity, boolean rootWasReset,
2711 boolean autoRemoveRecents, boolean askedCompatMode, int userId,
2712 int effectiveUid, String lastDescription, ArrayList<ActivityRecord> activities,
2713 long lastTimeMoved, boolean neverRelinquishIdentity,
2714 TaskDescription lastTaskDescription, int taskAffiliation, int prevTaskId,
2715 int nextTaskId, int taskAffiliationColor, int callingUid, String callingPackage,
2716 int resizeMode, boolean supportsPictureInPicture, boolean realActivitySuspended,
2717 boolean userSetupComplete, int minWidth, int minHeight) {
2718 return new TaskRecord(service, taskId, intent, affinityIntent, affinity,
2719 rootAffinity, realActivity, origActivity, rootWasReset, autoRemoveRecents,
2720 askedCompatMode, userId, effectiveUid, lastDescription, activities,
2721 lastTimeMoved, neverRelinquishIdentity, lastTaskDescription, taskAffiliation,
2722 prevTaskId, nextTaskId, taskAffiliationColor, callingUid, callingPackage,
2723 resizeMode, supportsPictureInPicture, realActivitySuspended, userSetupComplete,
2724 minWidth, minHeight);
2725 }
2726
2727 TaskRecord restoreFromXml(XmlPullParser in, ActivityStackSupervisor stackSupervisor)
2728 throws IOException, XmlPullParserException {
2729 Intent intent = null;
2730 Intent affinityIntent = null;
2731 ArrayList<ActivityRecord> activities = new ArrayList<>();
2732 ComponentName realActivity = null;
2733 boolean realActivitySuspended = false;
2734 ComponentName origActivity = null;
2735 String affinity = null;
2736 String rootAffinity = null;
2737 boolean hasRootAffinity = false;
2738 boolean rootHasReset = false;
2739 boolean autoRemoveRecents = false;
2740 boolean askedCompatMode = false;
2741 int taskType = 0;
2742 int userId = 0;
2743 boolean userSetupComplete = true;
2744 int effectiveUid = -1;
2745 String lastDescription = null;
2746 long lastTimeOnTop = 0;
2747 boolean neverRelinquishIdentity = true;
2748 int taskId = INVALID_TASK_ID;
2749 final int outerDepth = in.getDepth();
2750 TaskDescription taskDescription = new TaskDescription();
2751 int taskAffiliation = INVALID_TASK_ID;
2752 int taskAffiliationColor = 0;
2753 int prevTaskId = INVALID_TASK_ID;
2754 int nextTaskId = INVALID_TASK_ID;
2755 int callingUid = -1;
2756 String callingPackage = "";
2757 int resizeMode = RESIZE_MODE_FORCE_RESIZEABLE;
2758 boolean supportsPictureInPicture = false;
Garfield Tan367b35a2017-12-13 12:16:21 -08002759 Rect lastNonFullscreenBounds = null;
Garfield Tan9b1efea2017-12-05 16:43:46 -08002760 int minWidth = INVALID_MIN_SIZE;
2761 int minHeight = INVALID_MIN_SIZE;
2762 int persistTaskVersion = 0;
2763
2764 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
2765 final String attrName = in.getAttributeName(attrNdx);
2766 final String attrValue = in.getAttributeValue(attrNdx);
2767 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: attribute name=" +
2768 attrName + " value=" + attrValue);
2769 switch (attrName) {
2770 case ATTR_TASKID:
2771 if (taskId == INVALID_TASK_ID) taskId = Integer.parseInt(attrValue);
2772 break;
2773 case ATTR_REALACTIVITY:
2774 realActivity = ComponentName.unflattenFromString(attrValue);
2775 break;
2776 case ATTR_REALACTIVITY_SUSPENDED:
2777 realActivitySuspended = Boolean.valueOf(attrValue);
2778 break;
2779 case ATTR_ORIGACTIVITY:
2780 origActivity = ComponentName.unflattenFromString(attrValue);
2781 break;
2782 case ATTR_AFFINITY:
2783 affinity = attrValue;
2784 break;
2785 case ATTR_ROOT_AFFINITY:
2786 rootAffinity = attrValue;
2787 hasRootAffinity = true;
2788 break;
2789 case ATTR_ROOTHASRESET:
2790 rootHasReset = Boolean.parseBoolean(attrValue);
2791 break;
2792 case ATTR_AUTOREMOVERECENTS:
2793 autoRemoveRecents = Boolean.parseBoolean(attrValue);
2794 break;
2795 case ATTR_ASKEDCOMPATMODE:
2796 askedCompatMode = Boolean.parseBoolean(attrValue);
2797 break;
2798 case ATTR_USERID:
2799 userId = Integer.parseInt(attrValue);
2800 break;
2801 case ATTR_USER_SETUP_COMPLETE:
2802 userSetupComplete = Boolean.parseBoolean(attrValue);
2803 break;
2804 case ATTR_EFFECTIVE_UID:
2805 effectiveUid = Integer.parseInt(attrValue);
2806 break;
2807 case ATTR_TASKTYPE:
2808 taskType = Integer.parseInt(attrValue);
2809 break;
2810 case ATTR_LASTDESCRIPTION:
2811 lastDescription = attrValue;
2812 break;
2813 case ATTR_LASTTIMEMOVED:
2814 lastTimeOnTop = Long.parseLong(attrValue);
2815 break;
2816 case ATTR_NEVERRELINQUISH:
2817 neverRelinquishIdentity = Boolean.parseBoolean(attrValue);
2818 break;
2819 case ATTR_TASK_AFFILIATION:
2820 taskAffiliation = Integer.parseInt(attrValue);
2821 break;
2822 case ATTR_PREV_AFFILIATION:
2823 prevTaskId = Integer.parseInt(attrValue);
2824 break;
2825 case ATTR_NEXT_AFFILIATION:
2826 nextTaskId = Integer.parseInt(attrValue);
2827 break;
2828 case ATTR_TASK_AFFILIATION_COLOR:
2829 taskAffiliationColor = Integer.parseInt(attrValue);
2830 break;
2831 case ATTR_CALLING_UID:
2832 callingUid = Integer.parseInt(attrValue);
2833 break;
2834 case ATTR_CALLING_PACKAGE:
2835 callingPackage = attrValue;
2836 break;
2837 case ATTR_RESIZE_MODE:
2838 resizeMode = Integer.parseInt(attrValue);
2839 break;
2840 case ATTR_SUPPORTS_PICTURE_IN_PICTURE:
2841 supportsPictureInPicture = Boolean.parseBoolean(attrValue);
2842 break;
2843 case ATTR_NON_FULLSCREEN_BOUNDS:
Garfield Tan367b35a2017-12-13 12:16:21 -08002844 lastNonFullscreenBounds = Rect.unflattenFromString(attrValue);
Garfield Tan9b1efea2017-12-05 16:43:46 -08002845 break;
2846 case ATTR_MIN_WIDTH:
2847 minWidth = Integer.parseInt(attrValue);
2848 break;
2849 case ATTR_MIN_HEIGHT:
2850 minHeight = Integer.parseInt(attrValue);
2851 break;
2852 case ATTR_PERSIST_TASK_VERSION:
2853 persistTaskVersion = Integer.parseInt(attrValue);
2854 break;
2855 default:
2856 if (attrName.startsWith(TaskDescription.ATTR_TASKDESCRIPTION_PREFIX)) {
2857 taskDescription.restoreFromXml(attrName, attrValue);
2858 } else {
2859 Slog.w(TAG, "TaskRecord: Unknown attribute=" + attrName);
2860 }
2861 }
2862 }
2863
2864 int event;
2865 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
2866 (event != XmlPullParser.END_TAG || in.getDepth() >= outerDepth)) {
2867 if (event == XmlPullParser.START_TAG) {
2868 final String name = in.getName();
2869 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG,
2870 "TaskRecord: START_TAG name=" + name);
2871 if (TAG_AFFINITYINTENT.equals(name)) {
2872 affinityIntent = Intent.restoreFromXml(in);
2873 } else if (TAG_INTENT.equals(name)) {
2874 intent = Intent.restoreFromXml(in);
2875 } else if (TAG_ACTIVITY.equals(name)) {
2876 ActivityRecord activity =
2877 ActivityRecord.restoreFromXml(in, stackSupervisor);
2878 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: activity=" +
2879 activity);
2880 if (activity != null) {
2881 activities.add(activity);
2882 }
2883 } else {
Garfield Tan1e740192017-12-12 14:37:42 -08002884 handleUnknownTag(name, in);
Garfield Tan9b1efea2017-12-05 16:43:46 -08002885 }
2886 }
2887 }
2888 if (!hasRootAffinity) {
2889 rootAffinity = affinity;
2890 } else if ("@".equals(rootAffinity)) {
2891 rootAffinity = null;
2892 }
2893 if (effectiveUid <= 0) {
2894 Intent checkIntent = intent != null ? intent : affinityIntent;
2895 effectiveUid = 0;
2896 if (checkIntent != null) {
2897 IPackageManager pm = AppGlobals.getPackageManager();
2898 try {
2899 ApplicationInfo ai = pm.getApplicationInfo(
2900 checkIntent.getComponent().getPackageName(),
2901 PackageManager.MATCH_UNINSTALLED_PACKAGES
2902 | PackageManager.MATCH_DISABLED_COMPONENTS, userId);
2903 if (ai != null) {
2904 effectiveUid = ai.uid;
2905 }
2906 } catch (RemoteException e) {
2907 }
2908 }
2909 Slog.w(TAG, "Updating task #" + taskId + " for " + checkIntent
2910 + ": effectiveUid=" + effectiveUid);
2911 }
2912
2913 if (persistTaskVersion < 1) {
2914 // We need to convert the resize mode of home activities saved before version one if
2915 // they are marked as RESIZE_MODE_RESIZEABLE to
2916 // RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION since we didn't have that differentiation
2917 // before version 1 and the system didn't resize home activities before then.
2918 if (taskType == 1 /* old home type */ && resizeMode == RESIZE_MODE_RESIZEABLE) {
2919 resizeMode = RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
2920 }
2921 } else {
2922 // This activity has previously marked itself explicitly as both resizeable and
2923 // supporting picture-in-picture. Since there is no longer a requirement for
2924 // picture-in-picture activities to be resizeable, we can mark this simply as
2925 // resizeable and supporting picture-in-picture separately.
2926 if (resizeMode == RESIZE_MODE_RESIZEABLE_AND_PIPABLE_DEPRECATED) {
2927 resizeMode = RESIZE_MODE_RESIZEABLE;
2928 supportsPictureInPicture = true;
2929 }
2930 }
2931
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002932 final TaskRecord task = create(stackSupervisor.mService,
Wale Ogunwaled95c06b2018-05-08 10:35:38 -07002933 taskId, intent, affinityIntent,
Garfield Tan9b1efea2017-12-05 16:43:46 -08002934 affinity, rootAffinity, realActivity, origActivity, rootHasReset,
2935 autoRemoveRecents, askedCompatMode, userId, effectiveUid, lastDescription,
2936 activities, lastTimeOnTop, neverRelinquishIdentity, taskDescription,
2937 taskAffiliation, prevTaskId, nextTaskId, taskAffiliationColor, callingUid,
2938 callingPackage, resizeMode, supportsPictureInPicture, realActivitySuspended,
2939 userSetupComplete, minWidth, minHeight);
Garfield Tan367b35a2017-12-13 12:16:21 -08002940 task.mLastNonFullscreenBounds = lastNonFullscreenBounds;
2941 task.setBounds(lastNonFullscreenBounds);
Garfield Tan9b1efea2017-12-05 16:43:46 -08002942
2943 for (int activityNdx = activities.size() - 1; activityNdx >=0; --activityNdx) {
2944 activities.get(activityNdx).setTask(task);
2945 }
2946
2947 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Restored task=" + task);
2948 return task;
2949 }
Garfield Tan1e740192017-12-12 14:37:42 -08002950
2951 void handleUnknownTag(String name, XmlPullParser in)
2952 throws IOException, XmlPullParserException {
2953 Slog.e(TAG, "restoreTask: Unexpected name=" + name);
2954 XmlUtils.skipCurrentTag(in);
2955 }
Garfield Tan9b1efea2017-12-05 16:43:46 -08002956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957}