blob: 9b178e2c4c5447cfcb017aad0e93b2d4a98ebb38 [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
17package com.android.server.am;
18
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080019import static android.app.ActivityManager.RESIZE_MODE_FORCED;
Winson Chung74666102017-02-22 17:49:24 -080020import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
Jorim Jaggi0a932142016-02-01 17:42:25 -080021import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070022import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
23import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
24import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
25import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
26import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070027import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
28import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
29import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
30import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070031import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
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;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080048import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
Suprabh Shukla7745c142016-03-07 18:21:10 -080049import static android.provider.Settings.Secure.USER_SETUP_COMPLETE;
Andrii Kulian036e3ad2017-04-19 10:55:10 -070050import static android.view.Display.DEFAULT_DISPLAY;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070051
Jorim Jaggi0a932142016-02-01 17:42:25 -080052import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ADD_REMOVE;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
54import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
56import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_ADD_REMOVE;
57import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
58import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
59import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
60import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
61import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale3b232392016-05-13 15:37:13 -070062import static com.android.server.am.ActivityRecord.STARTING_WINDOW_SHOWN;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080063import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
Wale Ogunwale56d8d162017-05-30 11:12:20 -070064import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING_TO_TOP;
Wale Ogunwaleab5de372017-10-18 06:46:31 -070065import static com.android.server.am.ActivityStackSupervisor.ON_TOP;
Winson Chung6954fc92017-03-24 16:22:12 -070066import static com.android.server.am.ActivityStackSupervisor.PAUSE_IMMEDIATELY;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080067import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Steven Timotius4346f0a2017-09-12 11:07:21 -070068import static com.android.server.am.proto.TaskRecordProto.ACTIVITIES;
69import static com.android.server.am.proto.TaskRecordProto.BOUNDS;
70import static com.android.server.am.proto.TaskRecordProto.CONFIGURATION_CONTAINER;
71import static com.android.server.am.proto.TaskRecordProto.FULLSCREEN;
72import static com.android.server.am.proto.TaskRecordProto.ID;
73import static com.android.server.am.proto.TaskRecordProto.LAST_NON_FULLSCREEN_BOUNDS;
74import static com.android.server.am.proto.TaskRecordProto.MIN_HEIGHT;
75import static com.android.server.am.proto.TaskRecordProto.MIN_WIDTH;
76import static com.android.server.am.proto.TaskRecordProto.ORIG_ACTIVITY;
77import static com.android.server.am.proto.TaskRecordProto.REAL_ACTIVITY;
78import static com.android.server.am.proto.TaskRecordProto.RESIZE_MODE;
Steven Timotius4346f0a2017-09-12 11:07:21 -070079import static com.android.server.am.proto.TaskRecordProto.STACK_ID;
80import static com.android.server.am.proto.TaskRecordProto.ACTIVITY_TYPE;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070081
Winson Chung74666102017-02-22 17:49:24 -080082import static java.lang.Integer.MAX_VALUE;
83
Jorim Jaggie7d2b852017-08-28 17:55:15 +020084import android.annotation.IntDef;
85import android.annotation.Nullable;
86import android.app.Activity;
87import android.app.ActivityManager;
Jorim Jaggie7d2b852017-08-28 17:55:15 +020088import android.app.ActivityManager.TaskDescription;
89import android.app.ActivityManager.TaskSnapshot;
90import android.app.ActivityOptions;
91import android.app.AppGlobals;
92import android.app.IActivityManager;
93import android.content.ComponentName;
94import android.content.Intent;
95import android.content.pm.ActivityInfo;
96import android.content.pm.ApplicationInfo;
97import android.content.pm.IPackageManager;
98import android.content.pm.PackageManager;
99import android.content.res.Configuration;
100import android.graphics.Rect;
101import android.os.Debug;
102import android.os.RemoteException;
Winson Chungfb44d212017-10-04 11:39:10 -0700103import android.os.SystemClock;
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200104import android.os.Trace;
105import android.os.UserHandle;
106import android.provider.Settings;
107import android.service.voice.IVoiceInteractionSession;
108import android.util.DisplayMetrics;
109import android.util.Slog;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700110import android.util.proto.ProtoOutputStream;
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200111
112import com.android.internal.annotations.VisibleForTesting;
113import com.android.internal.app.IVoiceInteractor;
114import com.android.internal.util.XmlUtils;
115import com.android.server.wm.AppWindowContainerController;
116import com.android.server.wm.ConfigurationContainer;
117import com.android.server.wm.StackWindowController;
118import com.android.server.wm.TaskWindowContainerController;
119import com.android.server.wm.TaskWindowContainerListener;
120import com.android.server.wm.WindowManagerService;
121
122import org.xmlpull.v1.XmlPullParser;
123import org.xmlpull.v1.XmlPullParserException;
124import org.xmlpull.v1.XmlSerializer;
125
126import java.io.IOException;
127import java.io.PrintWriter;
128import java.lang.annotation.Retention;
129import java.lang.annotation.RetentionPolicy;
130import java.util.ArrayList;
131import java.util.Objects;
132
Benjamin Franza83859f2017-07-03 16:34:14 +0100133class TaskRecord extends ConfigurationContainer implements TaskWindowContainerListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800134 private static final String TAG = TAG_WITH_CLASS_NAME ? "TaskRecord" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700135 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700136 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Craig Mautnere0570202015-05-13 13:06:11 -0700137 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700138 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800139
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700140 private static final String ATTR_TASKID = "task_id";
Craig Mautner21d24a22014-04-23 11:45:37 -0700141 private static final String TAG_INTENT = "intent";
142 private static final String TAG_AFFINITYINTENT = "affinity_intent";
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700143 private static final String ATTR_REALACTIVITY = "real_activity";
144 private static final String ATTR_REALACTIVITY_SUSPENDED = "real_activity_suspended";
Craig Mautner21d24a22014-04-23 11:45:37 -0700145 private static final String ATTR_ORIGACTIVITY = "orig_activity";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700146 private static final String TAG_ACTIVITY = "activity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700147 private static final String ATTR_AFFINITY = "affinity";
Dianne Hackborn79228822014-09-16 11:11:23 -0700148 private static final String ATTR_ROOT_AFFINITY = "root_affinity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700149 private static final String ATTR_ROOTHASRESET = "root_has_reset";
Dianne Hackborn13420f22014-07-18 15:43:56 -0700150 private static final String ATTR_AUTOREMOVERECENTS = "auto_remove_recents";
Craig Mautner21d24a22014-04-23 11:45:37 -0700151 private static final String ATTR_ASKEDCOMPATMODE = "asked_compat_mode";
152 private static final String ATTR_USERID = "user_id";
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800153 private static final String ATTR_USER_SETUP_COMPLETE = "user_setup_complete";
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700154 private static final String ATTR_EFFECTIVE_UID = "effective_uid";
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700155 @Deprecated
Craig Mautner21d24a22014-04-23 11:45:37 -0700156 private static final String ATTR_TASKTYPE = "task_type";
Craig Mautner21d24a22014-04-23 11:45:37 -0700157 private static final String ATTR_LASTDESCRIPTION = "last_description";
158 private static final String ATTR_LASTTIMEMOVED = "last_time_moved";
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700159 private static final String ATTR_NEVERRELINQUISH = "never_relinquish_identity";
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700160 private static final String ATTR_TASK_AFFILIATION = "task_affiliation";
Craig Mautnera228ae92014-07-09 05:44:55 -0700161 private static final String ATTR_PREV_AFFILIATION = "prev_affiliation";
162 private static final String ATTR_NEXT_AFFILIATION = "next_affiliation";
Winson Chungec396d62014-08-06 17:08:00 -0700163 private static final String ATTR_TASK_AFFILIATION_COLOR = "task_affiliation_color";
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700164 private static final String ATTR_CALLING_UID = "calling_uid";
165 private static final String ATTR_CALLING_PACKAGE = "calling_package";
Winson Chungd3395382016-12-13 11:49:09 -0800166 private static final String ATTR_SUPPORTS_PICTURE_IN_PICTURE = "supports_picture_in_picture";
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800167 private static final String ATTR_RESIZE_MODE = "resize_mode";
Wale Ogunwale706ed792015-08-02 10:29:44 -0700168 private static final String ATTR_NON_FULLSCREEN_BOUNDS = "non_fullscreen_bounds";
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700169 private static final String ATTR_MIN_WIDTH = "min_width";
170 private static final String ATTR_MIN_HEIGHT = "min_height";
Wale Ogunwale625ed0c2016-10-18 08:50:31 -0700171 private static final String ATTR_PERSIST_TASK_VERSION = "persist_task_version";
Andrii Kulian18d75122016-03-27 20:20:28 -0700172
Wale Ogunwale625ed0c2016-10-18 08:50:31 -0700173 // Current version of the task record we persist. Used to check if we need to run any upgrade
174 // code.
175 private static final int PERSIST_TASK_VERSION = 1;
Craig Mautner21d24a22014-04-23 11:45:37 -0700176
Wale Ogunwale18795a22014-12-03 11:38:33 -0800177 static final int INVALID_TASK_ID = -1;
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700178 private static final int INVALID_MIN_SIZE = -1;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800179
Winson Chung74666102017-02-22 17:49:24 -0800180 /**
181 * The modes to control how the stack is moved to the front when calling
182 * {@link TaskRecord#reparent}.
183 */
184 @Retention(RetentionPolicy.SOURCE)
185 @IntDef({
186 REPARENT_MOVE_STACK_TO_FRONT,
187 REPARENT_KEEP_STACK_AT_FRONT,
188 REPARENT_LEAVE_STACK_IN_PLACE
189 })
Wale Ogunwale66e16852017-10-19 13:35:52 -0700190 @interface ReparentMoveStackMode {}
Winson Chung74666102017-02-22 17:49:24 -0800191 // Moves the stack to the front if it was not at the front
Wale Ogunwale66e16852017-10-19 13:35:52 -0700192 static final int REPARENT_MOVE_STACK_TO_FRONT = 0;
Winson Chung74666102017-02-22 17:49:24 -0800193 // Only moves the stack to the front if it was focused or front most already
Wale Ogunwale66e16852017-10-19 13:35:52 -0700194 static final int REPARENT_KEEP_STACK_AT_FRONT = 1;
Winson Chung74666102017-02-22 17:49:24 -0800195 // Do not move the stack as a part of reparenting
Wale Ogunwale66e16852017-10-19 13:35:52 -0700196 static final int REPARENT_LEAVE_STACK_IN_PLACE = 2;
Winson Chung74666102017-02-22 17:49:24 -0800197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 final int taskId; // Unique identifier for this task.
Dianne Hackborn79228822014-09-16 11:11:23 -0700199 String affinity; // The affinity name for this task, or null; may change identity.
200 String rootAffinity; // Initial base affinity, or null; does not change from initial root.
Dianne Hackborn91097de2014-04-04 18:02:06 -0700201 final IVoiceInteractionSession voiceSession; // Voice interaction session driving task
202 final IVoiceInteractor voiceInteractor; // Associated interactor to provide to app
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 Intent intent; // The original intent that started the task.
204 Intent affinityIntent; // Intent of affinity-moved activity that started this task.
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700205 int effectiveUid; // The current effective uid of the identity of this task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 ComponentName origActivity; // The non-alias activity component of the intent.
207 ComponentName realActivity; // The actual activity component that started the task.
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +0000208 boolean realActivitySuspended; // True if the actual activity component that started the
209 // task is suspended.
Dianne Hackborn852975d2014-08-22 17:42:43 -0700210 boolean inRecents; // Actually in the recents list?
Winson Chungfb44d212017-10-04 11:39:10 -0700211 long lastActiveTime; // Last time this task was active in the current device session,
212 // including sleep. This time is initialized to the elapsed time when
213 // restored from disk.
Dianne Hackborn852975d2014-08-22 17:42:43 -0700214 boolean isAvailable; // Is the activity available to be launched?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 boolean rootWasReset; // True if the intent at the root of the task had
216 // the FLAG_ACTIVITY_RESET_TASK_IF_NEEDED flag.
Dianne Hackborn13420f22014-07-18 15:43:56 -0700217 boolean autoRemoveRecents; // If true, we should automatically remove the task from
218 // recents when activity finishes
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700219 boolean askedCompatMode;// Have asked the user about compat mode for this task.
Dianne Hackbornd38aed82014-06-10 21:36:35 -0700220 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 -0800221
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700222 String stringName; // caching of toString() result.
Dianne Hackborn9da2d402012-03-15 13:43:08 -0700223 int userId; // user for which this task was created
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800224 boolean mUserSetupComplete; // The user set-up is complete as of the last time the task activity
225 // was changed.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800226
227 int numFullscreen; // Number of fullscreen activities.
228
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800229 int mResizeMode; // The resize mode of this task and its activities.
230 // Based on the {@link ActivityInfo#resizeMode} of the root activity.
Wale Ogunwale069bbd32017-02-03 07:58:14 -0800231 private boolean mSupportsPictureInPicture; // Whether or not this task and its activities
232 // support PiP. Based on the {@link ActivityInfo#FLAG_SUPPORTS_PICTURE_IN_PICTURE} flag
233 // of the root activity.
Craig Mautner15df08a2015-04-01 12:17:18 -0700234 /** Can't be put in lockTask mode. */
235 final static int LOCK_TASK_AUTH_DONT_LOCK = 0;
Benjamin Franz469dd582015-06-09 14:24:36 +0100236 /** Can enter app pinning with user approval. Can never start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700237 final static int LOCK_TASK_AUTH_PINNABLE = 1;
238 /** Starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing lockTask task. */
239 final static int LOCK_TASK_AUTH_LAUNCHABLE = 2;
Benjamin Franz469dd582015-06-09 14:24:36 +0100240 /** Can enter lockTask without user approval. Can start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700241 final static int LOCK_TASK_AUTH_WHITELISTED = 3;
Benjamin Franz469dd582015-06-09 14:24:36 +0100242 /** Priv-app that starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing
243 * lockTask task. */
244 final static int LOCK_TASK_AUTH_LAUNCHABLE_PRIV = 4;
Craig Mautner15df08a2015-04-01 12:17:18 -0700245 int mLockTaskAuth = LOCK_TASK_AUTH_PINNABLE;
246
247 int mLockTaskUid = -1; // The uid of the application that called startLockTask().
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800248
Winson Chung03a9bae2014-05-02 09:56:12 -0700249 // This represents the last resolved activity values for this task
250 // NOTE: This value needs to be persisted with each task
Craig Mautner648f69b2014-09-18 14:16:26 -0700251 TaskDescription lastTaskDescription = new TaskDescription();
Winson Chung03a9bae2014-05-02 09:56:12 -0700252
Craig Mautnerd2328952013-03-05 12:46:26 -0800253 /** List of all activities in the task arranged in history order */
Craig Mautner21d24a22014-04-23 11:45:37 -0700254 final ArrayList<ActivityRecord> mActivities;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800255
Andrii Kulian02b7a832016-10-06 23:11:56 -0700256 /** Current stack. Setter must always be used to update the value. */
257 private ActivityStack mStack;
Craig Mautnerd2328952013-03-05 12:46:26 -0800258
Craig Mautner21d24a22014-04-23 11:45:37 -0700259 /** Takes on same value as first root activity */
260 boolean isPersistable = false;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700261 int maxRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700262
263 /** Only used for persistable tasks, otherwise 0. The last time this task was moved. Used for
264 * determining the order when restoring. Sign indicates whether last task movement was to front
265 * (positive) or back (negative). Absolute value indicates time. */
266 long mLastTimeMoved = System.currentTimeMillis();
267
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700268 /** If original intent did not allow relinquishing task identity, save that information */
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700269 private boolean mNeverRelinquishIdentity = true;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700270
Craig Mautner362449a2014-06-20 14:04:39 -0700271 // Used in the unique case where we are clearing the task in order to reuse it. In that case we
272 // do not want to delete the stack when the task goes empty.
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -0800273 private boolean mReuseTask = false;
Craig Mautner362449a2014-06-20 14:04:39 -0700274
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700275 CharSequence lastDescription; // Last description captured for this item.
276
Craig Mautnera228ae92014-07-09 05:44:55 -0700277 int mAffiliatedTaskId; // taskId of parent affiliation or self if no parent.
Winson Chungec396d62014-08-06 17:08:00 -0700278 int mAffiliatedTaskColor; // color of the parent task affiliation.
Craig Mautnera228ae92014-07-09 05:44:55 -0700279 TaskRecord mPrevAffiliate; // previous task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800280 int mPrevAffiliateTaskId = INVALID_TASK_ID; // previous id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700281 TaskRecord mNextAffiliate; // next task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800282 int mNextAffiliateTaskId = INVALID_TASK_ID; // next id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700283
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700284 // For relaunching the task from recents as though it was launched by the original launcher.
285 int mCallingUid;
286 String mCallingPackage;
287
Craig Mautner21d24a22014-04-23 11:45:37 -0700288 final ActivityManagerService mService;
289
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700290 // Whether or not this task covers the entire screen; by default tasks are fullscreen.
291 boolean mFullscreen = true;
292
293 // Bounds of the Task. null for fullscreen tasks.
294 Rect mBounds = null;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -0800295 private final Rect mTmpStableBounds = new Rect();
296 private final Rect mTmpNonDecorBounds = new Rect();
Wale Ogunwale9a08f822016-02-17 19:03:58 -0800297 private final Rect mTmpRect = new Rect();
Jorim Jaggi0a932142016-02-01 17:42:25 -0800298
Wale Ogunwale706ed792015-08-02 10:29:44 -0700299 // Last non-fullscreen bounds the task was launched in or resized to.
300 // The information is persisted and used to determine the appropriate stack to launch the
301 // task into on restore.
302 Rect mLastNonFullscreenBounds = null;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700303 // Minimal width and height of this task when it's resizeable. -1 means it should use the
304 // default minimal width/height.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700305 int mMinWidth;
306 int mMinHeight;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700307
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700308 // Ranking (from top) of this task among all visible tasks. (-1 means it's not visible)
309 // This number will be assigned when we evaluate OOM scores for all visible tasks.
310 int mLayerRank = -1;
311
Andrii Kulian1779e612016-10-12 21:58:25 -0700312 /** Helper object used for updating override configuration. */
313 private Configuration mTmpConfig = new Configuration();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700314
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800315 private TaskWindowContainerController mWindowContainerController;
316
Craig Mautner21d24a22014-04-23 11:45:37 -0700317 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700318 IVoiceInteractionSession _voiceSession, IVoiceInteractor _voiceInteractor) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700319 mService = service;
Suprabh Shukla23593142015-11-03 17:31:15 -0800320 userId = UserHandle.getUserId(info.applicationInfo.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 taskId = _taskId;
Winson Chungfb44d212017-10-04 11:39:10 -0700322 lastActiveTime = SystemClock.elapsedRealtime();
Craig Mautnera228ae92014-07-09 05:44:55 -0700323 mAffiliatedTaskId = _taskId;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700324 voiceSession = _voiceSession;
325 voiceInteractor = _voiceInteractor;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700326 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800327 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700328 mCallingUid = info.applicationInfo.uid;
329 mCallingPackage = info.packageName;
Martijn Coenend4a69702014-06-30 11:12:17 -0700330 setIntent(_intent, info);
Andrii Kulian2e751b82016-03-16 16:59:32 -0700331 setMinDimensions(info);
Winson730bf062016-03-31 18:04:56 -0700332 touchActiveTime();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700333 mService.mTaskChangeNotificationController.notifyTaskCreated(_taskId, realActivity);
Craig Mautner21d24a22014-04-23 11:45:37 -0700334 }
335
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700336 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200337 TaskDescription _taskDescription) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700338 mService = service;
Suprabh Shukla23593142015-11-03 17:31:15 -0800339 userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700340 taskId = _taskId;
Winson Chungfb44d212017-10-04 11:39:10 -0700341 lastActiveTime = SystemClock.elapsedRealtime();
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700342 mAffiliatedTaskId = _taskId;
343 voiceSession = null;
344 voiceInteractor = null;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700345 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800346 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700347 mCallingUid = info.applicationInfo.uid;
348 mCallingPackage = info.packageName;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700349 setIntent(_intent, info);
Andrii Kulian2e751b82016-03-16 16:59:32 -0700350 setMinDimensions(info);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700351
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700352 isPersistable = true;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700353 // Clamp to [1, max].
354 maxRecents = Math.min(Math.max(info.maxRecents, 1),
355 ActivityManager.getMaxAppRecentsLimitStatic());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700356
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700357 lastTaskDescription = _taskDescription;
Winson730bf062016-03-31 18:04:56 -0700358 touchActiveTime();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700359 mService.mTaskChangeNotificationController.notifyTaskCreated(_taskId, realActivity);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700360 }
361
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800362 private TaskRecord(ActivityManagerService service, int _taskId, Intent _intent,
363 Intent _affinityIntent, String _affinity, String _rootAffinity,
364 ComponentName _realActivity, ComponentName _origActivity, boolean _rootWasReset,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700365 boolean _autoRemoveRecents, boolean _askedCompatMode, int _userId,
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800366 int _effectiveUid, String _lastDescription, ArrayList<ActivityRecord> activities,
Winson Chungfb44d212017-10-04 11:39:10 -0700367 long lastTimeMoved, boolean neverRelinquishIdentity,
368 TaskDescription _lastTaskDescription, int taskAffiliation, int prevTaskId,
369 int nextTaskId, int taskAffiliationColor, int callingUid, String callingPackage,
Charles He2bf28322017-10-12 22:24:49 +0100370 int resizeMode, boolean supportsPictureInPicture, boolean _realActivitySuspended,
371 boolean userSetupComplete, int minWidth, int minHeight) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700372 mService = service;
373 taskId = _taskId;
374 intent = _intent;
375 affinityIntent = _affinityIntent;
376 affinity = _affinity;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800377 rootAffinity = _rootAffinity;
Craig Mautner21d24a22014-04-23 11:45:37 -0700378 voiceSession = null;
379 voiceInteractor = null;
380 realActivity = _realActivity;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800381 realActivitySuspended = _realActivitySuspended;
Craig Mautner21d24a22014-04-23 11:45:37 -0700382 origActivity = _origActivity;
383 rootWasReset = _rootWasReset;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700384 isAvailable = true;
Dianne Hackborn13420f22014-07-18 15:43:56 -0700385 autoRemoveRecents = _autoRemoveRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700386 askedCompatMode = _askedCompatMode;
Craig Mautner21d24a22014-04-23 11:45:37 -0700387 userId = _userId;
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800388 mUserSetupComplete = userSetupComplete;
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700389 effectiveUid = _effectiveUid;
Winson Chungfb44d212017-10-04 11:39:10 -0700390 lastActiveTime = SystemClock.elapsedRealtime();
Craig Mautner21d24a22014-04-23 11:45:37 -0700391 lastDescription = _lastDescription;
392 mActivities = activities;
393 mLastTimeMoved = lastTimeMoved;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700394 mNeverRelinquishIdentity = neverRelinquishIdentity;
Winson Chung2cb86c72014-06-25 12:03:30 -0700395 lastTaskDescription = _lastTaskDescription;
Craig Mautnera228ae92014-07-09 05:44:55 -0700396 mAffiliatedTaskId = taskAffiliation;
Winson Chungec396d62014-08-06 17:08:00 -0700397 mAffiliatedTaskColor = taskAffiliationColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700398 mPrevAffiliateTaskId = prevTaskId;
399 mNextAffiliateTaskId = nextTaskId;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700400 mCallingUid = callingUid;
401 mCallingPackage = callingPackage;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800402 mResizeMode = resizeMode;
Winson Chungd3395382016-12-13 11:49:09 -0800403 mSupportsPictureInPicture = supportsPictureInPicture;
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700404 mMinWidth = minWidth;
405 mMinHeight = minHeight;
Yorke Leebd54c2a2016-10-25 13:49:23 -0700406 mService.mTaskChangeNotificationController.notifyTaskCreated(_taskId, realActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 }
408
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800409 TaskWindowContainerController getWindowContainerController() {
410 return mWindowContainerController;
411 }
412
413 void createWindowContainer(boolean onTop, boolean showForAllUsers) {
414 if (mWindowContainerController != null) {
415 throw new IllegalArgumentException("Window container=" + mWindowContainerController
416 + " already created for task=" + this);
417 }
418
419 final Rect bounds = updateOverrideConfigurationFromLaunchBounds();
Bryce Lee04ab3462017-04-10 15:06:33 -0700420 setWindowContainerController(new TaskWindowContainerController(taskId, this,
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700421 getStack().getWindowContainerController(), userId, bounds,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700422 mResizeMode, mSupportsPictureInPicture, onTop,
423 showForAllUsers, lastTaskDescription));
Bryce Lee04ab3462017-04-10 15:06:33 -0700424 }
425
426 /**
427 * Should only be invoked from {@link #createWindowContainer(boolean, boolean)}.
428 */
429 @VisibleForTesting
430 protected void setWindowContainerController(TaskWindowContainerController controller) {
431 if (mWindowContainerController != null) {
432 throw new IllegalArgumentException("Window container=" + mWindowContainerController
433 + " already created for task=" + this);
434 }
435
436 mWindowContainerController = controller;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800437 }
438
439 void removeWindowContainer() {
Benjamin Franza83859f2017-07-03 16:34:14 +0100440 mService.mLockTaskController.removeLockedTask(this);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800441 mWindowContainerController.removeContainer();
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700442 if (!getWindowConfiguration().persistTaskBounds()) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800443 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
444 // default configuration the next time it launches.
445 updateOverrideConfiguration(null);
446 }
447 mService.mTaskChangeNotificationController.notifyTaskRemoved(taskId);
448 mWindowContainerController = null;
449 }
450
Jorim Jaggifb9d78a2017-01-05 18:57:12 +0100451 @Override
452 public void onSnapshotChanged(TaskSnapshot snapshot) {
453 mService.mTaskChangeNotificationController.notifyTaskSnapshotChanged(taskId, snapshot);
454 }
455
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800456 void setResizeMode(int resizeMode) {
457 if (mResizeMode == resizeMode) {
458 return;
459 }
460 mResizeMode = resizeMode;
461 mWindowContainerController.setResizeable(resizeMode);
462 mService.mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
463 mService.mStackSupervisor.resumeFocusedStackTopActivityLocked();
464 }
465
466 void setTaskDockedResizing(boolean resizing) {
467 mWindowContainerController.setTaskDockedResizing(resizing);
468 }
469
Wale Ogunwale1666e312016-12-16 11:27:18 -0800470 // TODO: Consolidate this with the resize() method below.
471 @Override
472 public void requestResize(Rect bounds, int resizeMode) {
473 mService.resizeTask(taskId, bounds, resizeMode);
474 }
475
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800476 boolean resize(Rect bounds, int resizeMode, boolean preserveWindow, boolean deferResume) {
477 if (!isResizeable()) {
478 Slog.w(TAG, "resizeTask: task " + this + " not resizeable.");
479 return true;
480 }
481
482 // If this is a forced resize, let it go through even if the bounds is not changing,
483 // as we might need a relayout due to surface size change (to/from fullscreen).
484 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
485 if (Objects.equals(mBounds, bounds) && !forced) {
486 // Nothing to do here...
487 return true;
488 }
489 bounds = validateBounds(bounds);
490
491 if (mWindowContainerController == null) {
492 // Task doesn't exist in window manager yet (e.g. was restored from recents).
493 // All we can do for now is update the bounds so it can be used when the task is
494 // added to window manager.
495 updateOverrideConfiguration(bounds);
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700496 if (!inFreeformWindowingMode()) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800497 // re-restore the task so it can have the proper stack association.
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700498 mService.mStackSupervisor.restoreRecentTaskLocked(this, null, !ON_TOP);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800499 }
500 return true;
501 }
502
503 if (!canResizeToBounds(bounds)) {
504 throw new IllegalArgumentException("resizeTask: Can not resize task=" + this
505 + " to bounds=" + bounds + " resizeMode=" + mResizeMode);
506 }
507
508 // Do not move the task to another stack here.
509 // This method assumes that the task is already placed in the right stack.
510 // we do not mess with that decision and we only do the resize!
511
512 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + taskId);
513
514 final boolean updatedConfig = updateOverrideConfiguration(bounds);
515 // This variable holds information whether the configuration didn't change in a significant
516 // way and the activity was kept the way it was. If it's false, it means the activity had
517 // to be relaunched due to configuration change.
518 boolean kept = true;
519 if (updatedConfig) {
520 final ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaleea3d3fd2017-05-01 07:41:08 -0700521 if (r != null && !deferResume) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800522 kept = r.ensureActivityConfigurationLocked(0 /* globalChanges */, preserveWindow);
Wale Ogunwaleea3d3fd2017-05-01 07:41:08 -0700523 mService.mStackSupervisor.ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
524 if (!kept) {
525 mService.mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800526 }
527 }
528 }
529 mWindowContainerController.resize(mBounds, getOverrideConfiguration(), kept, forced);
530
531 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
532 return kept;
533 }
534
535 // TODO: Investigate combining with the resize() method above.
536 void resizeWindowContainer() {
537 mWindowContainerController.resize(mBounds, getOverrideConfiguration(), false /* relayout */,
538 false /* forced */);
539 }
540
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800541 void getWindowContainerBounds(Rect bounds) {
542 mWindowContainerController.getBounds(bounds);
543 }
544
Winson Chung74666102017-02-22 17:49:24 -0800545 /**
546 * Convenience method to reparent a task to the top or bottom position of the stack.
547 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700548 boolean reparent(ActivityStack preferredStack, boolean toTop,
549 @ReparentMoveStackMode int moveStackMode, boolean animate, boolean deferResume,
550 String reason) {
551 return reparent(preferredStack, toTop ? MAX_VALUE : 0, moveStackMode, animate, deferResume,
552 true /* schedulePictureInPictureModeChange */, reason);
Winson Chung5af42fc2017-03-24 17:11:33 -0700553 }
554
555 /**
556 * Convenience method to reparent a task to the top or bottom position of the stack, with
557 * an option to skip scheduling the picture-in-picture mode change.
558 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700559 boolean reparent(ActivityStack preferredStack, boolean toTop,
560 @ReparentMoveStackMode int moveStackMode, boolean animate, boolean deferResume,
561 boolean schedulePictureInPictureModeChange, String reason) {
562 return reparent(preferredStack, toTop ? MAX_VALUE : 0, moveStackMode, animate,
Winson Chung5af42fc2017-03-24 17:11:33 -0700563 deferResume, schedulePictureInPictureModeChange, reason);
564 }
565
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700566 /** Convenience method to reparent a task to a specific position of the stack. */
567 boolean reparent(ActivityStack preferredStack, int position,
568 @ReparentMoveStackMode int moveStackMode, boolean animate, boolean deferResume,
569 String reason) {
570 return reparent(preferredStack, position, moveStackMode, animate, deferResume,
Winson Chung5af42fc2017-03-24 17:11:33 -0700571 true /* schedulePictureInPictureModeChange */, reason);
Winson Chung74666102017-02-22 17:49:24 -0800572 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800573
Winson Chung74666102017-02-22 17:49:24 -0800574 /**
575 * Reparents the task into a preferred stack, creating it if necessary.
576 *
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700577 * @param preferredStack the target stack to move this task
Winson Chung74666102017-02-22 17:49:24 -0800578 * @param position the position to place this task in the new stack
579 * @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 -0700580 * reparenting to be drawn and animated in
Winson Chung74666102017-02-22 17:49:24 -0800581 * @param moveStackMode whether or not to move the stack to the front always, only if it was
Winson Chung5af42fc2017-03-24 17:11:33 -0700582 * previously focused & in front, or never
Winson Chung74666102017-02-22 17:49:24 -0800583 * @param deferResume whether or not to update the visibility of other tasks and stacks that may
Winson Chung5af42fc2017-03-24 17:11:33 -0700584 * have changed as a result of this reparenting
585 * @param schedulePictureInPictureModeChange specifies whether or not to schedule the PiP mode
586 * change. Callers may set this to false if they are explicitly scheduling PiP mode
587 * changes themselves, like during the PiP animation
Winson Chung74666102017-02-22 17:49:24 -0800588 * @param reason the caller of this reparenting
Winson Chung5af42fc2017-03-24 17:11:33 -0700589 * @return whether the task was reparented
Winson Chung74666102017-02-22 17:49:24 -0800590 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700591 // TODO: Inspect all call sites and change to just changing windowing mode of the stack vs.
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700592 // re-parenting the task. Can only be done when we are no longer using static stack Ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700593 boolean reparent(ActivityStack preferredStack, int position,
594 @ReparentMoveStackMode int moveStackMode, boolean animate, boolean deferResume,
595 boolean schedulePictureInPictureModeChange, String reason) {
Winson Chung74666102017-02-22 17:49:24 -0800596 final ActivityStackSupervisor supervisor = mService.mStackSupervisor;
597 final WindowManagerService windowManager = mService.mWindowManager;
598 final ActivityStack sourceStack = getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700599 final ActivityStack toStack = supervisor.getReparentTargetStack(this, preferredStack,
Winson Chung74666102017-02-22 17:49:24 -0800600 position == MAX_VALUE);
601 if (toStack == sourceStack) {
602 return false;
603 }
604
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700605 final int toStackWindowingMode = toStack.getWindowingMode();
Winson Chung74666102017-02-22 17:49:24 -0800606 final ActivityRecord topActivity = getTopActivity();
607
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700608 final boolean mightReplaceWindow =
609 replaceWindowsOnTaskMove(getWindowingMode(), toStackWindowingMode)
610 && topActivity != null;
Winson Chung74666102017-02-22 17:49:24 -0800611 if (mightReplaceWindow) {
612 // We are about to relaunch the activity because its configuration changed due to
613 // being maximized, i.e. size change. The activity will first remove the old window
614 // and then add a new one. This call will tell window manager about this, so it can
615 // preserve the old window until the new one is drawn. This prevents having a gap
616 // between the removal and addition, in which no window is visible. We also want the
617 // entrance of the new window to be properly animated.
618 // Note here we always set the replacing window first, as the flags might be needed
619 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
620 windowManager.setWillReplaceWindow(topActivity.appToken, animate);
621 }
622
623 windowManager.deferSurfaceLayout();
624 boolean kept = true;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800625 try {
Winson Chung74666102017-02-22 17:49:24 -0800626 final ActivityRecord r = topRunningActivityLocked();
Winson Chung95f8f0e2017-03-24 09:20:17 -0700627 final boolean wasFocused = r != null && supervisor.isFocusedStack(sourceStack)
Winson Chung74666102017-02-22 17:49:24 -0800628 && (topRunningActivityLocked() == r);
Winson Chung95f8f0e2017-03-24 09:20:17 -0700629 final boolean wasResumed = r != null && sourceStack.mResumedActivity == r;
630 final boolean wasPaused = r != null && sourceStack.mPausingActivity == r;
Winson Chung74666102017-02-22 17:49:24 -0800631
632 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
633 // Whenever we are moving the top activity from the front stack we want to make sure to
634 // move the stack to the front.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700635 final boolean wasFront = r != null && sourceStack.isTopStackOnDisplay()
Winson Chung74666102017-02-22 17:49:24 -0800636 && (sourceStack.topRunningActivityLocked() == r);
637
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800638 // Adjust the position for the new parent stack as needed.
Winson Chung74666102017-02-22 17:49:24 -0800639 position = toStack.getAdjustedPositionForTask(this, position, null /* starting */);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800640
641 // Must reparent first in window manager to avoid a situation where AM can delete the
642 // we are coming from in WM before we reparent because it became empty.
Wale Ogunwale2719cc12017-04-14 09:45:27 -0700643 mWindowContainerController.reparent(toStack.getWindowContainerController(), position,
644 moveStackMode == REPARENT_MOVE_STACK_TO_FRONT);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800645
Wale Ogunwale56d8d162017-05-30 11:12:20 -0700646 final boolean moveStackToFront = moveStackMode == REPARENT_MOVE_STACK_TO_FRONT
647 || (moveStackMode == REPARENT_KEEP_STACK_AT_FRONT && (wasFocused || wasFront));
Winson Chung74666102017-02-22 17:49:24 -0800648 // Move the task
Wale Ogunwale56d8d162017-05-30 11:12:20 -0700649 sourceStack.removeTask(this, reason, moveStackToFront
650 ? REMOVE_TASK_MODE_MOVING_TO_TOP : REMOVE_TASK_MODE_MOVING);
Winson Chung5af42fc2017-03-24 17:11:33 -0700651 toStack.addTask(this, position, false /* schedulePictureInPictureModeChange */, reason);
Winson Chung74666102017-02-22 17:49:24 -0800652
Winson Chung5af42fc2017-03-24 17:11:33 -0700653 if (schedulePictureInPictureModeChange) {
654 // Notify of picture-in-picture mode changes
655 supervisor.scheduleUpdatePictureInPictureModeIfNeeded(this, sourceStack);
656 }
Winson Chung74666102017-02-22 17:49:24 -0800657
658 // TODO: Ensure that this is actually necessary here
659 // Notify the voice session if required
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800660 if (voiceSession != null) {
661 try {
662 voiceSession.taskStarted(intent, taskId);
663 } catch (RemoteException e) {
664 }
665 }
Winson Chung74666102017-02-22 17:49:24 -0800666
667 // If the task had focus before (or we're requested to move focus), move focus to the
668 // new stack by moving the stack to the front.
Winson Chung95f8f0e2017-03-24 09:20:17 -0700669 if (r != null) {
670 toStack.moveToFrontAndResumeStateIfNeeded(r, moveStackToFront, wasResumed,
671 wasPaused, reason);
672 }
Winson Chung74666102017-02-22 17:49:24 -0800673 if (!animate) {
674 toStack.mNoAnimActivities.add(topActivity);
675 }
676
677 // We might trigger a configuration change. Save the current task bounds for freezing.
678 // TODO: Should this call be moved inside the resize method in WM?
679 toStack.prepareFreezingTaskBounds();
680
681 // Make sure the task has the appropriate bounds/size for the stack it is in.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700682 final boolean toStackSplitScreenPrimary =
683 toStackWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700684 if ((toStackWindowingMode == WINDOWING_MODE_FULLSCREEN
685 || toStackWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY)
Winson Chung74666102017-02-22 17:49:24 -0800686 && !Objects.equals(mBounds, toStack.mBounds)) {
687 kept = resize(toStack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow,
688 deferResume);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700689 } else if (toStackWindowingMode == WINDOWING_MODE_FREEFORM) {
Winson Chung74666102017-02-22 17:49:24 -0800690 Rect bounds = getLaunchBounds();
691 if (bounds == null) {
692 toStack.layoutTaskInStack(this, null);
693 bounds = mBounds;
694 }
695 kept = resize(bounds, RESIZE_MODE_FORCED, !mightReplaceWindow, deferResume);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700696 } else if (toStackSplitScreenPrimary || toStackWindowingMode == WINDOWING_MODE_PINNED) {
697 if (toStackSplitScreenPrimary && moveStackMode == REPARENT_KEEP_STACK_AT_FRONT) {
Matthew Ng330757d2017-02-28 14:19:17 -0800698 // Move recents to front so it is not behind home stack when going into docked
699 // mode
700 mService.mStackSupervisor.moveRecentsStackToFront(reason);
701 }
Winson Chung74666102017-02-22 17:49:24 -0800702 kept = resize(toStack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow,
703 deferResume);
704 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800705 } finally {
Winson Chung74666102017-02-22 17:49:24 -0800706 windowManager.continueSurfaceLayout();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800707 }
Winson Chung74666102017-02-22 17:49:24 -0800708
709 if (mightReplaceWindow) {
710 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
711 // window), we need to clear the replace window settings. Otherwise, we schedule a
712 // timeout to remove the old window if the replacing window is not coming in time.
713 windowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
714 }
715
716 if (!deferResume) {
717 // The task might have already been running and its visibility needs to be synchronized
718 // with the visibility of the stack / windows.
719 supervisor.ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
720 supervisor.resumeFocusedStackTopActivityLocked();
721 }
722
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700723 // TODO: Handle incorrect request to move before the actual move, not after.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700724 final boolean inSplitScreenMode = supervisor.getDefaultDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700725 supervisor.handleNonResizableTaskIfNeeded(this, preferredStack.getWindowingMode(),
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700726 DEFAULT_DISPLAY, toStack);
Winson Chung74666102017-02-22 17:49:24 -0800727
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700728 boolean successful = (preferredStack == toStack);
729 if (successful && toStack.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng330757d2017-02-28 14:19:17 -0800730 // If task moved to docked stack - show recents if needed.
731 mService.mWindowManager.showRecentApps(false /* fromHome */);
732 }
733 return successful;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800734 }
735
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700736 /**
737 * Returns true if the windows of tasks being moved to the target stack from the source
738 * stack should be replaced, meaning that window manager will keep the old window around
739 * until the new is ready.
740 * @hide
741 */
742 private static boolean replaceWindowsOnTaskMove(
743 int sourceWindowingMode, int targetWindowingMode) {
744 return sourceWindowingMode == WINDOWING_MODE_FREEFORM
745 || targetWindowingMode == WINDOWING_MODE_FREEFORM;
746 }
747
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800748 void cancelWindowTransition() {
749 mWindowContainerController.cancelWindowTransition();
750 }
751
752 void cancelThumbnailTransition() {
753 mWindowContainerController.cancelThumbnailTransition();
754 }
755
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100756 /**
757 * DO NOT HOLD THE ACTIVITY MANAGER LOCK WHEN CALLING THIS METHOD!
758 */
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100759 TaskSnapshot getSnapshot(boolean reducedResolution) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100760
761 // TODO: Move this to {@link TaskWindowContainerController} once recent tasks are more
762 // synchronized between AM and WM.
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100763 return mService.mWindowManager.getTaskSnapshot(taskId, userId, reducedResolution);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800764 }
765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 void touchActiveTime() {
Winson Chungfb44d212017-10-04 11:39:10 -0700767 lastActiveTime = SystemClock.elapsedRealtime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 long getInactiveDuration() {
Winson Chungfb44d212017-10-04 11:39:10 -0700771 return SystemClock.elapsedRealtime() - lastActiveTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700773
Winson Chungfee26772014-08-05 12:21:52 -0700774 /** Sets the original intent, and the calling uid and package. */
775 void setIntent(ActivityRecord r) {
Winson Chungfee26772014-08-05 12:21:52 -0700776 mCallingUid = r.launchedFromUid;
777 mCallingPackage = r.launchedFromPackage;
Craig Mautner15df08a2015-04-01 12:17:18 -0700778 setIntent(r.intent, r.info);
Charles He2bf28322017-10-12 22:24:49 +0100779 setLockTaskAuth(r);
Winson Chungfee26772014-08-05 12:21:52 -0700780 }
781
782 /** Sets the original intent, _without_ updating the calling uid or package. */
783 private void setIntent(Intent _intent, ActivityInfo info) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700784 if (intent == null) {
785 mNeverRelinquishIdentity =
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700786 (info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700787 } else if (mNeverRelinquishIdentity) {
788 return;
789 }
790
791 affinity = info.taskAffinity;
Dianne Hackborn79228822014-09-16 11:11:23 -0700792 if (intent == null) {
793 // If this task already has an intent associated with it, don't set the root
794 // affinity -- we don't want it changing after initially set, but the initially
795 // set value may be null.
796 rootAffinity = affinity;
797 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700798 effectiveUid = info.applicationInfo.uid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700799 stringName = null;
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 if (info.targetActivity == null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800802 if (_intent != null) {
803 // If this Intent has a selector, we want to clear it for the
804 // recent task since it is not relevant if the user later wants
805 // to re-launch the app.
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700806 if (_intent.getSelector() != null || _intent.getSourceBounds() != null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800807 _intent = new Intent(_intent);
808 _intent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700809 _intent.setSourceBounds(null);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800810 }
811 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700812 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Setting Intent of " + this + " to " + _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 intent = _intent;
814 realActivity = _intent != null ? _intent.getComponent() : null;
815 origActivity = null;
816 } else {
817 ComponentName targetComponent = new ComponentName(
818 info.packageName, info.targetActivity);
819 if (_intent != null) {
820 Intent targetIntent = new Intent(_intent);
821 targetIntent.setComponent(targetComponent);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800822 targetIntent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700823 targetIntent.setSourceBounds(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700824 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700825 "Setting Intent of " + this + " to target " + targetIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 intent = targetIntent;
827 realActivity = targetComponent;
828 origActivity = _intent.getComponent();
829 } else {
830 intent = null;
831 realActivity = targetComponent;
832 origActivity = new ComponentName(info.packageName, info.name);
833 }
834 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700835
Craig Mautner47b20ba2014-09-17 17:23:44 -0700836 final int intentFlags = intent == null ? 0 : intent.getFlags();
837 if ((intentFlags & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 // Once we are set to an Intent with this flag, we count this
839 // task as having a true root activity.
840 rootWasReset = true;
841 }
Dianne Hackborn09233282014-04-30 11:33:59 -0700842 userId = UserHandle.getUserId(info.applicationInfo.uid);
Winson Chung36f3f032016-09-08 23:29:43 +0000843 mUserSetupComplete = Settings.Secure.getIntForUser(mService.mContext.getContentResolver(),
844 USER_SETUP_COMPLETE, 0, userId) != 0;
Craig Mautner41db4a72014-05-07 17:20:56 -0700845 if ((info.flags & ActivityInfo.FLAG_AUTO_REMOVE_FROM_RECENTS) != 0) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700846 // If the activity itself has requested auto-remove, then just always do it.
847 autoRemoveRecents = true;
Wale Ogunwale843bfb92015-03-27 11:06:48 -0700848 } else if ((intentFlags & (FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_RETAIN_IN_RECENTS))
849 == FLAG_ACTIVITY_NEW_DOCUMENT) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700850 // If the caller has not asked for the document to be retained, then we may
851 // want to turn on auto-remove, depending on whether the target has set its
852 // own document launch mode.
853 if (info.documentLaunchMode != ActivityInfo.DOCUMENT_LAUNCH_NONE) {
854 autoRemoveRecents = false;
855 } else {
856 autoRemoveRecents = true;
857 }
858 } else {
859 autoRemoveRecents = false;
Craig Mautner41db4a72014-05-07 17:20:56 -0700860 }
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800861 mResizeMode = info.resizeMode;
Winson Chungd3395382016-12-13 11:49:09 -0800862 mSupportsPictureInPicture = info.supportsPictureInPicture();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800864
Andrii Kulian2e751b82016-03-16 16:59:32 -0700865 /** Sets the original minimal width and height. */
866 private void setMinDimensions(ActivityInfo info) {
867 if (info != null && info.windowLayout != null) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700868 mMinWidth = info.windowLayout.minWidth;
869 mMinHeight = info.windowLayout.minHeight;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700870 } else {
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700871 mMinWidth = INVALID_MIN_SIZE;
872 mMinHeight = INVALID_MIN_SIZE;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700873 }
874 }
875
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800876 /**
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700877 * Return true if the input activity has the same intent filter as the intent this task
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800878 * record is based on (normally the root activity intent).
879 */
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700880 boolean isSameIntentFilter(ActivityRecord r) {
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800881 final Intent intent = new Intent(r.intent);
882 // Correct the activity intent for aliasing. The task record intent will always be based on
883 // the real activity that will be launched not the alias, so we need to use an intent with
884 // the component name pointing to the real activity not the alias in the activity record.
885 intent.setComponent(r.realActivity);
886 return this.intent.filterEquals(intent);
887 }
888
Wale Ogunwale66e16852017-10-19 13:35:52 -0700889 boolean returnsToHomeStack() {
890 final int returnHomeFlags = FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME;
891 return (intent.getFlags() & returnHomeFlags) == returnHomeFlags;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700892 }
893
Craig Mautnera228ae92014-07-09 05:44:55 -0700894 void setPrevAffiliate(TaskRecord prevAffiliate) {
895 mPrevAffiliate = prevAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800896 mPrevAffiliateTaskId = prevAffiliate == null ? INVALID_TASK_ID : prevAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700897 }
898
899 void setNextAffiliate(TaskRecord nextAffiliate) {
900 mNextAffiliate = nextAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800901 mNextAffiliateTaskId = nextAffiliate == null ? INVALID_TASK_ID : nextAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700902 }
903
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700904 <T extends ActivityStack> T getStack() {
905 return (T) mStack;
Andrii Kulian02b7a832016-10-06 23:11:56 -0700906 }
907
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800908 /**
909 * Must be used for setting parent stack because it performs configuration updates.
910 * Must be called after adding task as a child to the stack.
911 */
Andrii Kulian02b7a832016-10-06 23:11:56 -0700912 void setStack(ActivityStack stack) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800913 if (stack != null && !stack.isInStackLocked(this)) {
914 throw new IllegalStateException("Task must be added as a Stack child first.");
915 }
Andrii Kulian02b7a832016-10-06 23:11:56 -0700916 mStack = stack;
Andrii Kulian1779e612016-10-12 21:58:25 -0700917 onParentChanged();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700918 }
919
920 /**
921 * @return Id of current stack, {@link INVALID_STACK_ID} if no stack is set.
922 */
923 int getStackId() {
924 return mStack != null ? mStack.mStackId : INVALID_STACK_ID;
925 }
926
Andrii Kulian1779e612016-10-12 21:58:25 -0700927 @Override
928 protected int getChildCount() {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700929 return mActivities.size();
Andrii Kulian1779e612016-10-12 21:58:25 -0700930 }
931
932 @Override
933 protected ConfigurationContainer getChildAt(int index) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700934 return mActivities.get(index);
Andrii Kulian1779e612016-10-12 21:58:25 -0700935 }
936
937 @Override
938 protected ConfigurationContainer getParent() {
939 return mStack;
940 }
941
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800942 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -0700943 protected void onParentChanged() {
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800944 super.onParentChanged();
945 mService.mStackSupervisor.updateUIDsPresentOnDisplay();
946 }
947
Craig Mautnera228ae92014-07-09 05:44:55 -0700948 // Close up recents linked list.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700949 private void closeRecentsChain() {
Craig Mautnera228ae92014-07-09 05:44:55 -0700950 if (mPrevAffiliate != null) {
951 mPrevAffiliate.setNextAffiliate(mNextAffiliate);
952 }
953 if (mNextAffiliate != null) {
954 mNextAffiliate.setPrevAffiliate(mPrevAffiliate);
955 }
956 setPrevAffiliate(null);
957 setNextAffiliate(null);
958 }
959
Winson Chung740c3ac2014-11-12 16:14:38 -0800960 void removedFromRecents() {
Dianne Hackborn852975d2014-08-22 17:42:43 -0700961 closeRecentsChain();
962 if (inRecents) {
963 inRecents = false;
Winson Chung740c3ac2014-11-12 16:14:38 -0800964 mService.notifyTaskPersisterLocked(this, false);
Dianne Hackborn852975d2014-08-22 17:42:43 -0700965 }
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100966
967 // TODO: Use window container controller once tasks are better synced between AM and WM
968 mService.mWindowManager.notifyTaskRemovedFromRecents(taskId, userId);
Dianne Hackborn852975d2014-08-22 17:42:43 -0700969 }
970
Craig Mautnera228ae92014-07-09 05:44:55 -0700971 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
972 closeRecentsChain();
973 mAffiliatedTaskId = taskToAffiliateWith.mAffiliatedTaskId;
Winson Chungec396d62014-08-06 17:08:00 -0700974 mAffiliatedTaskColor = taskToAffiliateWith.mAffiliatedTaskColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700975 // Find the end
976 while (taskToAffiliateWith.mNextAffiliate != null) {
977 final TaskRecord nextRecents = taskToAffiliateWith.mNextAffiliate;
978 if (nextRecents.mAffiliatedTaskId != mAffiliatedTaskId) {
979 Slog.e(TAG, "setTaskToAffiliateWith: nextRecents=" + nextRecents + " affilTaskId="
980 + nextRecents.mAffiliatedTaskId + " should be " + mAffiliatedTaskId);
981 if (nextRecents.mPrevAffiliate == taskToAffiliateWith) {
982 nextRecents.setPrevAffiliate(null);
983 }
984 taskToAffiliateWith.setNextAffiliate(null);
985 break;
986 }
987 taskToAffiliateWith = nextRecents;
988 }
989 taskToAffiliateWith.setNextAffiliate(this);
990 setPrevAffiliate(taskToAffiliateWith);
991 setNextAffiliate(null);
992 }
993
Winson Chung1147c402014-05-14 11:05:00 -0700994 /** Returns the intent for the root activity for this task */
995 Intent getBaseIntent() {
996 return intent != null ? intent : affinityIntent;
997 }
998
Winson Chung3b3f4642014-04-22 10:08:18 -0700999 /** Returns the first non-finishing activity from the root. */
1000 ActivityRecord getRootActivity() {
1001 for (int i = 0; i < mActivities.size(); i++) {
1002 final ActivityRecord r = mActivities.get(i);
1003 if (r.finishing) {
1004 continue;
1005 }
1006 return r;
1007 }
1008 return null;
1009 }
1010
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001011 ActivityRecord getTopActivity() {
Bryce Lee9f6affd2017-09-01 09:18:35 -07001012 return getTopActivity(true /* includeOverlays */);
1013 }
1014
1015 ActivityRecord getTopActivity(boolean includeOverlays) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001016 for (int i = mActivities.size() - 1; i >= 0; --i) {
1017 final ActivityRecord r = mActivities.get(i);
Bryce Lee9f6affd2017-09-01 09:18:35 -07001018 if (r.finishing || (!includeOverlays && r.mTaskOverlay)) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001019 continue;
1020 }
1021 return r;
1022 }
1023 return null;
1024 }
1025
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001026 ActivityRecord topRunningActivityLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001027 if (mStack != null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001028 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1029 ActivityRecord r = mActivities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -07001030 if (!r.finishing && r.okToShowLocked()) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001031 return r;
1032 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001033 }
1034 }
1035 return null;
1036 }
1037
Jorim Jaggiea039a82017-08-02 14:37:49 +02001038 void getAllRunningVisibleActivitiesLocked(ArrayList<ActivityRecord> outActivities) {
1039 if (mStack != null) {
1040 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1041 ActivityRecord r = mActivities.get(activityNdx);
Jorim Jaggi02f1d2f2017-08-04 14:29:16 +02001042 if (!r.finishing && r.okToShowLocked() && r.visibleIgnoringKeyguard) {
Jorim Jaggiea039a82017-08-02 14:37:49 +02001043 outActivities.add(r);
1044 }
1045 }
1046 }
1047 }
1048
Wale Ogunwale3b232392016-05-13 15:37:13 -07001049 ActivityRecord topRunningActivityWithStartingWindowLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001050 if (mStack != null) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07001051 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1052 ActivityRecord r = mActivities.get(activityNdx);
1053 if (r.mStartingWindowState != STARTING_WINDOW_SHOWN
Chong Zhang87761972016-08-22 13:53:24 -07001054 || r.finishing || !r.okToShowLocked()) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07001055 continue;
1056 }
1057 return r;
1058 }
1059 }
1060 return null;
1061 }
1062
Chong Zhang87761972016-08-22 13:53:24 -07001063 boolean okToShowLocked() {
1064 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
1065 // okay to show the activity when locked.
1066 return mService.mStackSupervisor.isCurrentProfileLocked(userId)
1067 || topRunningActivityLocked() != null;
1068 }
1069
Craig Mautner3b475fe2013-12-16 15:58:31 -08001070 /** Call after activity movement or finish to make sure that frontOfTask is set correctly */
Bryce Leed71317c2017-02-07 14:27:22 -08001071 final void setFrontOfTask() {
1072 boolean foundFront = false;
Craig Mautner3b475fe2013-12-16 15:58:31 -08001073 final int numActivities = mActivities.size();
Craig Mautner704e40b2013-12-18 16:43:51 -08001074 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
Craig Mautner3b475fe2013-12-16 15:58:31 -08001075 final ActivityRecord r = mActivities.get(activityNdx);
1076 if (foundFront || r.finishing) {
1077 r.frontOfTask = false;
1078 } else {
1079 r.frontOfTask = true;
1080 // Set frontOfTask false for every following activity.
1081 foundFront = true;
1082 }
1083 }
Craig Mautner9587ee02014-06-23 15:00:10 +00001084 if (!foundFront && numActivities > 0) {
1085 // All activities of this task are finishing. As we ought to have a frontOfTask
1086 // activity, make the bottom activity front.
1087 mActivities.get(0).frontOfTask = true;
1088 }
Craig Mautner3b475fe2013-12-16 15:58:31 -08001089 }
1090
Craig Mautnerde4ef022013-04-07 19:01:33 -07001091 /**
Craig Mautner3b475fe2013-12-16 15:58:31 -08001092 * Reorder the history stack so that the passed activity is brought to the front.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001093 */
1094 final void moveActivityToFrontLocked(ActivityRecord newTop) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001095 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
1096 "Removing and adding activity " + newTop
1097 + " to stack at top callers=" + Debug.getCallers(4));
Craig Mautnerde4ef022013-04-07 19:01:33 -07001098
Craig Mautnerde4ef022013-04-07 19:01:33 -07001099 mActivities.remove(newTop);
1100 mActivities.add(newTop);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001101 updateEffectiveIntent();
Craig Mautner3b475fe2013-12-16 15:58:31 -08001102
Bryce Leed71317c2017-02-07 14:27:22 -08001103 setFrontOfTask();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001104 }
1105
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001106 void addActivityAtBottom(ActivityRecord r) {
Craig Mautner77878772013-03-04 19:46:24 -08001107 addActivityAtIndex(0, r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001108 }
1109
1110 void addActivityToTop(ActivityRecord r) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001111 addActivityAtIndex(mActivities.size(), r);
1112 }
1113
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001114 @Override
Wale Ogunwaleeea34ee92017-08-31 20:07:45 -07001115 /*@WindowConfiguration.ActivityType*/
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001116 public int getActivityType() {
1117 final int applicationType = super.getActivityType();
1118 if (applicationType != ACTIVITY_TYPE_UNDEFINED || mActivities.isEmpty()) {
1119 return applicationType;
1120 }
1121 return mActivities.get(0).getActivityType();
1122 }
1123
Winson Chung30480042017-01-26 10:55:34 -08001124 /**
1125 * Adds an activity {@param r} at the given {@param index}. The activity {@param r} must either
1126 * be in the current task or unparented to any task.
1127 */
Craig Mautner1602ec22013-05-12 10:24:27 -07001128 void addActivityAtIndex(int index, ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07001129 TaskRecord task = r.getTask();
1130 if (task != null && task != this) {
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001131 throw new IllegalArgumentException("Can not add r=" + " to task=" + this
Bryce Leeaf691c02017-03-20 14:20:22 -07001132 + " current parent=" + task);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001133 }
Bryce Leeaf691c02017-03-20 14:20:22 -07001134
1135 r.setTask(this);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001136
Craig Mautner6170f732013-04-02 13:05:23 -07001137 // 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 -08001138 if (!mActivities.remove(r) && r.fullscreen) {
1139 // Was not previously in list.
1140 numFullscreen++;
1141 }
Craig Mautner2c1faed2013-07-23 12:56:02 -07001142 // Only set this based on the first activity
1143 if (mActivities.isEmpty()) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001144 if (r.getActivityType() == ACTIVITY_TYPE_UNDEFINED) {
1145 // Normally non-standard activity type for the activity record will be set when the
1146 // object is created, however we delay setting the standard application type until
1147 // this point so that the task can set the type for additional activities added in
1148 // the else condition below.
1149 r.setActivityType(ACTIVITY_TYPE_STANDARD);
1150 }
1151 setActivityType(r.getActivityType());
Craig Mautner21d24a22014-04-23 11:45:37 -07001152 isPersistable = r.isPersistable();
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001153 mCallingUid = r.launchedFromUid;
1154 mCallingPackage = r.launchedFromPackage;
Dianne Hackborn852975d2014-08-22 17:42:43 -07001155 // Clamp to [1, max].
1156 maxRecents = Math.min(Math.max(r.info.maxRecents, 1),
1157 ActivityManager.getMaxAppRecentsLimitStatic());
Craig Mautner2c1faed2013-07-23 12:56:02 -07001158 } else {
1159 // Otherwise make all added activities match this one.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001160 r.setActivityType(getActivityType());
Craig Mautner78733002013-06-10 13:54:49 -07001161 }
Wale Ogunwale3b232392016-05-13 15:37:13 -07001162
1163 final int size = mActivities.size();
1164
1165 if (index == size && size > 0) {
1166 final ActivityRecord top = mActivities.get(size - 1);
1167 if (top.mTaskOverlay) {
1168 // Place below the task overlay activity since the overlay activity should always
1169 // be on top.
1170 index--;
1171 }
1172 }
1173
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001174 index = Math.min(size, index);
Craig Mautner77878772013-03-04 19:46:24 -08001175 mActivities.add(index, r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001176 updateEffectiveIntent();
Craig Mautner21d24a22014-04-23 11:45:37 -07001177 if (r.isPersistable()) {
1178 mService.notifyTaskPersisterLocked(this, false);
1179 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001180
1181 // Sync. with window manager
1182 updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001183 final AppWindowContainerController appController = r.getWindowContainerController();
1184 if (appController != null) {
1185 // Only attempt to move in WM if the child has a controller. It is possible we haven't
1186 // created controller for the activity we are starting yet.
1187 mWindowContainerController.positionChildAt(appController, index);
1188 }
David Stevens82ea6cb2017-03-03 16:18:50 -08001189
1190 // Make sure the list of display UID whitelists is updated
1191 // now that this record is in a new task.
1192 mService.mStackSupervisor.updateUIDsPresentOnDisplay();
Craig Mautner77878772013-03-04 19:46:24 -08001193 }
1194
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001195 /**
Bryce Leeaf691c02017-03-20 14:20:22 -07001196 * Removes the specified activity from this task.
1197 * @param r The {@link ActivityRecord} to remove.
1198 * @return true if this was the last activity in the task.
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001199 */
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001200 boolean removeActivity(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07001201 return removeActivity(r, false /*reparenting*/);
1202 }
1203
1204 boolean removeActivity(ActivityRecord r, boolean reparenting) {
1205 if (r.getTask() != this) {
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001206 throw new IllegalArgumentException(
1207 "Activity=" + r + " does not belong to task=" + this);
1208 }
1209
Bryce Leeaf691c02017-03-20 14:20:22 -07001210 r.setTask(null /*task*/, reparenting);
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001211
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001212 if (mActivities.remove(r) && r.fullscreen) {
1213 // Was previously in list.
1214 numFullscreen--;
1215 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001216 if (r.isPersistable()) {
1217 mService.notifyTaskPersisterLocked(this, false);
1218 }
Wale Ogunwale89182d52016-03-11 10:38:36 -08001219
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001220 if (inPinnedWindowingMode()) {
Wale Ogunwale89182d52016-03-11 10:38:36 -08001221 // We normally notify listeners of task stack changes on pause, however pinned stack
1222 // activities are normally in the paused state so no notification will be sent there
1223 // before the activity is removed. We send it here so instead.
Yorke Leebd54c2a2016-10-25 13:49:23 -07001224 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale89182d52016-03-11 10:38:36 -08001225 }
1226
Craig Mautner41326202014-06-20 14:38:21 -07001227 if (mActivities.isEmpty()) {
Craig Mautner5afcd4d2014-06-21 18:11:33 -07001228 return !mReuseTask;
Craig Mautner41326202014-06-20 14:38:21 -07001229 }
1230 updateEffectiveIntent();
1231 return false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001232 }
1233
Winson Chung6954fc92017-03-24 16:22:12 -07001234 /**
1235 * @return whether or not there are ONLY task overlay activities in the stack.
1236 * If {@param excludeFinishing} is set, then ignore finishing activities in the check.
1237 * If there are no task overlay activities, this call returns false.
1238 */
1239 boolean onlyHasTaskOverlayActivities(boolean excludeFinishing) {
1240 int count = 0;
1241 for (int i = mActivities.size() - 1; i >= 0; i--) {
1242 final ActivityRecord r = mActivities.get(i);
1243 if (excludeFinishing && r.finishing) {
1244 continue;
1245 }
1246 if (!r.mTaskOverlay) {
1247 return false;
1248 }
1249 count++;
1250 }
1251 return count > 0;
1252 }
1253
Craig Mautner41db4a72014-05-07 17:20:56 -07001254 boolean autoRemoveFromRecents() {
Dianne Hackbornd38aed82014-06-10 21:36:35 -07001255 // We will automatically remove the task either if it has explicitly asked for
1256 // this, or it is empty and has never contained an activity that got shown to
1257 // the user.
Dianne Hackborn13420f22014-07-18 15:43:56 -07001258 return autoRemoveRecents || (mActivities.isEmpty() && !hasBeenVisible);
Craig Mautner41db4a72014-05-07 17:20:56 -07001259 }
1260
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001261 /**
1262 * Completely remove all activities associated with an existing
1263 * task starting at a specified index.
1264 */
Winson Chung6954fc92017-03-24 16:22:12 -07001265 final void performClearTaskAtIndexLocked(int activityNdx, boolean pauseImmediately) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001266 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001267 for ( ; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001268 final ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001269 if (r.finishing) {
1270 continue;
1271 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001272 if (mStack == null) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001273 // Task was restored from persistent storage.
1274 r.takeFromHistory();
1275 mActivities.remove(activityNdx);
1276 --activityNdx;
1277 --numActivities;
Winson Chung6954fc92017-03-24 16:22:12 -07001278 } else if (mStack.finishActivityLocked(r, Activity.RESULT_CANCELED, null,
1279 "clear-task-index", false, pauseImmediately)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001280 --activityNdx;
1281 --numActivities;
1282 }
1283 }
1284 }
1285
1286 /**
1287 * Completely remove all activities associated with an existing task.
1288 */
Benjamin Franza83859f2017-07-03 16:34:14 +01001289 void performClearTaskLocked() {
Craig Mautner362449a2014-06-20 14:04:39 -07001290 mReuseTask = true;
Winson Chung6954fc92017-03-24 16:22:12 -07001291 performClearTaskAtIndexLocked(0, !PAUSE_IMMEDIATELY);
Craig Mautner362449a2014-06-20 14:04:39 -07001292 mReuseTask = false;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001293 }
1294
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -08001295 ActivityRecord performClearTaskForReuseLocked(ActivityRecord newR, int launchFlags) {
1296 mReuseTask = true;
1297 final ActivityRecord result = performClearTaskLocked(newR, launchFlags);
1298 mReuseTask = false;
1299 return result;
1300 }
1301
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001302 /**
1303 * Perform clear operation as requested by
1304 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
1305 * stack to the given task, then look for
1306 * an instance of that activity in the stack and, if found, finish all
1307 * activities on top of it and return the instance.
1308 *
1309 * @param newR Description of the new activity being started.
1310 * @return Returns the old activity that should be continued to be used,
1311 * or null if none was found.
1312 */
1313 final ActivityRecord performClearTaskLocked(ActivityRecord newR, int launchFlags) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001314 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001315 for (int activityNdx = numActivities - 1; activityNdx >= 0; --activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001316 ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001317 if (r.finishing) {
1318 continue;
1319 }
1320 if (r.realActivity.equals(newR.realActivity)) {
1321 // Here it is! Now finish everything in front...
Craig Mautner1602ec22013-05-12 10:24:27 -07001322 final ActivityRecord ret = r;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001323
1324 for (++activityNdx; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001325 r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001326 if (r.finishing) {
1327 continue;
1328 }
1329 ActivityOptions opts = r.takeOptionsLocked();
1330 if (opts != null) {
1331 ret.updateOptionsLocked(opts);
1332 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001333 if (mStack != null && mStack.finishActivityLocked(
Todd Kennedy539db512014-12-15 09:57:55 -08001334 r, Activity.RESULT_CANCELED, null, "clear-task-stack", false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001335 --activityNdx;
1336 --numActivities;
1337 }
1338 }
1339
1340 // Finally, if this is a normal launch mode (that is, not
1341 // expecting onNewIntent()), then we will finish the current
1342 // instance of the activity so a new fresh one can be started.
1343 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
Daichi Hirono15a02992016-04-27 18:47:01 +09001344 && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0
1345 && !ActivityStarter.isDocumentLaunchesIntoExisting(launchFlags)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001346 if (!ret.finishing) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001347 if (mStack != null) {
1348 mStack.finishActivityLocked(
Wale Ogunwale7d701172015-03-11 15:36:30 -07001349 ret, Activity.RESULT_CANCELED, null, "clear-task-top", false);
1350 }
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001351 return null;
1352 }
1353 }
1354
1355 return ret;
1356 }
1357 }
1358
1359 return null;
1360 }
1361
Winson Chung6954fc92017-03-24 16:22:12 -07001362 void removeTaskActivitiesLocked(boolean pauseImmediately) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001363 // Just remove the entire task.
Winson Chung6954fc92017-03-24 16:22:12 -07001364 performClearTaskAtIndexLocked(0, pauseImmediately);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07001365 }
1366
Craig Mautner432f64e2015-05-20 14:59:57 -07001367 String lockTaskAuthToString() {
1368 switch (mLockTaskAuth) {
1369 case LOCK_TASK_AUTH_DONT_LOCK: return "LOCK_TASK_AUTH_DONT_LOCK";
1370 case LOCK_TASK_AUTH_PINNABLE: return "LOCK_TASK_AUTH_PINNABLE";
1371 case LOCK_TASK_AUTH_LAUNCHABLE: return "LOCK_TASK_AUTH_LAUNCHABLE";
1372 case LOCK_TASK_AUTH_WHITELISTED: return "LOCK_TASK_AUTH_WHITELISTED";
Benjamin Franz469dd582015-06-09 14:24:36 +01001373 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV: return "LOCK_TASK_AUTH_LAUNCHABLE_PRIV";
Craig Mautner432f64e2015-05-20 14:59:57 -07001374 default: return "unknown=" + mLockTaskAuth;
1375 }
1376 }
1377
Craig Mautner15df08a2015-04-01 12:17:18 -07001378 void setLockTaskAuth() {
Charles He2bf28322017-10-12 22:24:49 +01001379 setLockTaskAuth(getRootActivity());
1380 }
1381
1382 private void setLockTaskAuth(@Nullable ActivityRecord r) {
1383 if (r == null) {
1384 mLockTaskAuth = LOCK_TASK_AUTH_PINNABLE;
1385 return;
1386 }
1387
Charles He520b2832017-09-02 15:27:16 +01001388 final String pkg = (realActivity != null) ? realActivity.getPackageName() : null;
Charles He2bf28322017-10-12 22:24:49 +01001389 switch (r.lockTaskLaunchMode) {
Craig Mautner15df08a2015-04-01 12:17:18 -07001390 case LOCK_TASK_LAUNCH_MODE_DEFAULT:
Charles He520b2832017-09-02 15:27:16 +01001391 mLockTaskAuth = mService.mLockTaskController.isPackageWhitelisted(userId, pkg)
1392 ? LOCK_TASK_AUTH_WHITELISTED : LOCK_TASK_AUTH_PINNABLE;
Craig Mautner15df08a2015-04-01 12:17:18 -07001393 break;
1394
1395 case LOCK_TASK_LAUNCH_MODE_NEVER:
Benjamin Franz469dd582015-06-09 14:24:36 +01001396 mLockTaskAuth = LOCK_TASK_AUTH_DONT_LOCK;
Craig Mautner15df08a2015-04-01 12:17:18 -07001397 break;
1398
1399 case LOCK_TASK_LAUNCH_MODE_ALWAYS:
Benjamin Franz469dd582015-06-09 14:24:36 +01001400 mLockTaskAuth = LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -07001401 break;
1402
1403 case LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED:
Charles He520b2832017-09-02 15:27:16 +01001404 mLockTaskAuth = mService.mLockTaskController.isPackageWhitelisted(userId, pkg)
1405 ? LOCK_TASK_AUTH_LAUNCHABLE : LOCK_TASK_AUTH_PINNABLE;
Craig Mautner15df08a2015-04-01 12:17:18 -07001406 break;
1407 }
Craig Mautner432f64e2015-05-20 14:59:57 -07001408 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "setLockTaskAuth: task=" + this +
1409 " mLockTaskAuth=" + lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07001410 }
1411
Winson Chungd3395382016-12-13 11:49:09 -08001412 private boolean isResizeable(boolean checkSupportsPip) {
1413 return (mService.mForceResizableActivities || ActivityInfo.isResizeableMode(mResizeMode)
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001414 || (checkSupportsPip && mSupportsPictureInPicture));
Winson Chungd3395382016-12-13 11:49:09 -08001415 }
1416
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001417 boolean isResizeable() {
Winson Chungd3395382016-12-13 11:49:09 -08001418 return isResizeable(true /* checkSupportsPip */);
1419 }
1420
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001421 @Override
1422 public boolean supportsSplitScreenWindowingMode() {
Winson Chungd3395382016-12-13 11:49:09 -08001423 // A task can not be docked even if it is considered resizeable because it only supports
1424 // picture-in-picture mode but has a non-resizeable resizeMode
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001425 return super.supportsSplitScreenWindowingMode()
1426 && mService.mSupportsSplitScreenMultiWindow
Bryce Leec857a5b2017-08-16 10:04:52 -07001427 && (mService.mForceResizableActivities
1428 || (isResizeable(false /* checkSupportsPip */)
1429 && !ActivityInfo.isPreserveOrientationMode(mResizeMode)));
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001430 }
1431
skuhne@google.com322347b2016-12-02 12:54:03 -08001432 /**
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001433 * Check whether this task can be launched on the specified display.
1434 * @param displayId Target display id.
1435 * @return {@code true} if either it is the default display or this activity is resizeable and
1436 * can be put a secondary screen.
1437 */
1438 boolean canBeLaunchedOnDisplay(int displayId) {
1439 return mService.mStackSupervisor.canPlaceEntityOnDisplay(displayId,
Andrii Kulian02689a72017-07-06 14:28:59 -07001440 isResizeable(false /* checkSupportsPip */), -1 /* don't check PID */,
1441 -1 /* don't check UID */, null /* activityInfo */);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001442 }
1443
1444 /**
skuhne@google.com322347b2016-12-02 12:54:03 -08001445 * Check that a given bounds matches the application requested orientation.
1446 *
1447 * @param bounds The bounds to be tested.
1448 * @return True if the requested bounds are okay for a resizing request.
1449 */
Wale Ogunwale069bbd32017-02-03 07:58:14 -08001450 private boolean canResizeToBounds(Rect bounds) {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001451 if (bounds == null || !inFreeformWindowingMode()) {
skuhne@google.com322347b2016-12-02 12:54:03 -08001452 // Note: If not on the freeform workspace, we ignore the bounds.
1453 return true;
1454 }
1455 final boolean landscape = bounds.width() > bounds.height();
1456 if (mResizeMode == RESIZE_MODE_FORCE_RESIZABLE_PRESERVE_ORIENTATION) {
1457 return mBounds == null || landscape == (mBounds.width() > mBounds.height());
1458 }
1459 return (mResizeMode != RESIZE_MODE_FORCE_RESIZABLE_PORTRAIT_ONLY || !landscape)
1460 && (mResizeMode != RESIZE_MODE_FORCE_RESIZABLE_LANDSCAPE_ONLY || landscape);
1461 }
1462
Craig Mautner525f3d92013-05-07 14:01:50 -07001463 /**
Yorke Leebdef5372017-04-10 16:38:51 -07001464 * @return {@code true} if the task is being cleared for the purposes of being reused.
1465 */
1466 boolean isClearingToReuseTask() {
1467 return mReuseTask;
1468 }
1469
1470 /**
Craig Mautner525f3d92013-05-07 14:01:50 -07001471 * Find the activity in the history stack within the given task. Returns
1472 * the index within the history at which it's found, or < 0 if not found.
1473 */
1474 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r) {
1475 final ComponentName realActivity = r.realActivity;
1476 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1477 ActivityRecord candidate = mActivities.get(activityNdx);
1478 if (candidate.finishing) {
1479 continue;
1480 }
1481 if (candidate.realActivity.equals(realActivity)) {
1482 return candidate;
1483 }
1484 }
1485 return null;
1486 }
1487
Winson Chunga449dc02014-05-16 11:15:04 -07001488 /** Updates the last task description values. */
1489 void updateTaskDescription() {
1490 // Traverse upwards looking for any break between main task activities and
1491 // utility activities.
1492 int activityNdx;
1493 final int numActivities = mActivities.size();
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001494 final boolean relinquish = numActivities != 0 &&
1495 (mActivities.get(0).info.flags & FLAG_RELINQUISH_TASK_IDENTITY) != 0;
Winson Chunga449dc02014-05-16 11:15:04 -07001496 for (activityNdx = Math.min(numActivities, 1); activityNdx < numActivities;
Craig Mautner21d24a22014-04-23 11:45:37 -07001497 ++activityNdx) {
Winson Chunga449dc02014-05-16 11:15:04 -07001498 final ActivityRecord r = mActivities.get(activityNdx);
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001499 if (relinquish && (r.info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001500 // This will be the top activity for determining taskDescription. Pre-inc to
1501 // overcome initial decrement below.
1502 ++activityNdx;
1503 break;
1504 }
Winson Chunga449dc02014-05-16 11:15:04 -07001505 if (r.intent != null &&
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001506 (r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Winson Chunga449dc02014-05-16 11:15:04 -07001507 break;
1508 }
1509 }
1510 if (activityNdx > 0) {
1511 // Traverse downwards starting below break looking for set label, icon.
1512 // Note that if there are activities in the task but none of them set the
1513 // recent activity values, then we do not fall back to the last set
1514 // values in the TaskRecord.
1515 String label = null;
Craig Mautner648f69b2014-09-18 14:16:26 -07001516 String iconFilename = null;
Matthew Ng54bc9422017-10-02 17:16:28 -07001517 int iconResource = -1;
Winson Chunga449dc02014-05-16 11:15:04 -07001518 int colorPrimary = 0;
Winson Chung1af8eda2016-02-05 17:55:56 +00001519 int colorBackground = 0;
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001520 int statusBarColor = 0;
1521 int navigationBarColor = 0;
1522 boolean topActivity = true;
Winson Chunga449dc02014-05-16 11:15:04 -07001523 for (--activityNdx; activityNdx >= 0; --activityNdx) {
1524 final ActivityRecord r = mActivities.get(activityNdx);
1525 if (r.taskDescription != null) {
1526 if (label == null) {
1527 label = r.taskDescription.getLabel();
1528 }
Matthew Ng54bc9422017-10-02 17:16:28 -07001529 if (iconResource == -1) {
1530 iconResource = r.taskDescription.getIconResource();
1531 }
Craig Mautner648f69b2014-09-18 14:16:26 -07001532 if (iconFilename == null) {
1533 iconFilename = r.taskDescription.getIconFilename();
Winson Chunga449dc02014-05-16 11:15:04 -07001534 }
1535 if (colorPrimary == 0) {
1536 colorPrimary = r.taskDescription.getPrimaryColor();
Winson Chunga449dc02014-05-16 11:15:04 -07001537 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001538 if (topActivity) {
Winson Chung1af8eda2016-02-05 17:55:56 +00001539 colorBackground = r.taskDescription.getBackgroundColor();
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001540 statusBarColor = r.taskDescription.getStatusBarColor();
1541 navigationBarColor = r.taskDescription.getNavigationBarColor();
Winson Chung1af8eda2016-02-05 17:55:56 +00001542 }
Winson Chunga449dc02014-05-16 11:15:04 -07001543 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001544 topActivity = false;
Winson Chunga449dc02014-05-16 11:15:04 -07001545 }
Matthew Ng54bc9422017-10-02 17:16:28 -07001546 lastTaskDescription = new TaskDescription(label, null, iconResource, iconFilename,
1547 colorPrimary, colorBackground, statusBarColor, navigationBarColor);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +01001548 if (mWindowContainerController != null) {
1549 mWindowContainerController.setTaskDescription(lastTaskDescription);
1550 }
Winson Chungec396d62014-08-06 17:08:00 -07001551 // Update the task affiliation color if we are the parent of the group
1552 if (taskId == mAffiliatedTaskId) {
1553 mAffiliatedTaskColor = lastTaskDescription.getPrimaryColor();
1554 }
Winson Chunga449dc02014-05-16 11:15:04 -07001555 }
1556 }
1557
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001558 int findEffectiveRootIndex() {
Craig Mautner4767f4b2014-09-18 15:38:33 -07001559 int effectiveNdx = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001560 final int topActivityNdx = mActivities.size() - 1;
Dianne Hackborn39569af2014-09-23 10:56:58 -07001561 for (int activityNdx = 0; activityNdx <= topActivityNdx; ++activityNdx) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001562 final ActivityRecord r = mActivities.get(activityNdx);
1563 if (r.finishing) {
1564 continue;
1565 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001566 effectiveNdx = activityNdx;
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001567 if ((r.info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001568 break;
1569 }
1570 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001571 return effectiveNdx;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001572 }
1573
1574 void updateEffectiveIntent() {
1575 final int effectiveRootIndex = findEffectiveRootIndex();
1576 final ActivityRecord r = mActivities.get(effectiveRootIndex);
Winson Chungfee26772014-08-05 12:21:52 -07001577 setIntent(r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001578 }
1579
Craig Mautner21d24a22014-04-23 11:45:37 -07001580 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001581 if (DEBUG_RECENTS) Slog.i(TAG_RECENTS, "Saving task=" + this);
Craig Mautner21d24a22014-04-23 11:45:37 -07001582
1583 out.attribute(null, ATTR_TASKID, String.valueOf(taskId));
1584 if (realActivity != null) {
1585 out.attribute(null, ATTR_REALACTIVITY, realActivity.flattenToShortString());
1586 }
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001587 out.attribute(null, ATTR_REALACTIVITY_SUSPENDED, String.valueOf(realActivitySuspended));
Craig Mautner21d24a22014-04-23 11:45:37 -07001588 if (origActivity != null) {
1589 out.attribute(null, ATTR_ORIGACTIVITY, origActivity.flattenToShortString());
1590 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001591 // Write affinity, and root affinity if it is different from affinity.
1592 // We use the special string "@" for a null root affinity, so we can identify
1593 // later whether we were given a root affinity or should just make it the
1594 // same as the affinity.
Craig Mautner21d24a22014-04-23 11:45:37 -07001595 if (affinity != null) {
1596 out.attribute(null, ATTR_AFFINITY, affinity);
Dianne Hackborn79228822014-09-16 11:11:23 -07001597 if (!affinity.equals(rootAffinity)) {
1598 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
1599 }
1600 } else if (rootAffinity != null) {
1601 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
Craig Mautner21d24a22014-04-23 11:45:37 -07001602 }
1603 out.attribute(null, ATTR_ROOTHASRESET, String.valueOf(rootWasReset));
Dianne Hackborn13420f22014-07-18 15:43:56 -07001604 out.attribute(null, ATTR_AUTOREMOVERECENTS, String.valueOf(autoRemoveRecents));
Craig Mautner21d24a22014-04-23 11:45:37 -07001605 out.attribute(null, ATTR_ASKEDCOMPATMODE, String.valueOf(askedCompatMode));
1606 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001607 out.attribute(null, ATTR_USER_SETUP_COMPLETE, String.valueOf(mUserSetupComplete));
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001608 out.attribute(null, ATTR_EFFECTIVE_UID, String.valueOf(effectiveUid));
Craig Mautner21d24a22014-04-23 11:45:37 -07001609 out.attribute(null, ATTR_LASTTIMEMOVED, String.valueOf(mLastTimeMoved));
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001610 out.attribute(null, ATTR_NEVERRELINQUISH, String.valueOf(mNeverRelinquishIdentity));
Craig Mautner21d24a22014-04-23 11:45:37 -07001611 if (lastDescription != null) {
1612 out.attribute(null, ATTR_LASTDESCRIPTION, lastDescription.toString());
1613 }
Winson Chung2cb86c72014-06-25 12:03:30 -07001614 if (lastTaskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07001615 lastTaskDescription.saveToXml(out);
Winson Chung2cb86c72014-06-25 12:03:30 -07001616 }
Winson Chungec396d62014-08-06 17:08:00 -07001617 out.attribute(null, ATTR_TASK_AFFILIATION_COLOR, String.valueOf(mAffiliatedTaskColor));
Craig Mautnera228ae92014-07-09 05:44:55 -07001618 out.attribute(null, ATTR_TASK_AFFILIATION, String.valueOf(mAffiliatedTaskId));
1619 out.attribute(null, ATTR_PREV_AFFILIATION, String.valueOf(mPrevAffiliateTaskId));
1620 out.attribute(null, ATTR_NEXT_AFFILIATION, String.valueOf(mNextAffiliateTaskId));
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001621 out.attribute(null, ATTR_CALLING_UID, String.valueOf(mCallingUid));
1622 out.attribute(null, ATTR_CALLING_PACKAGE, mCallingPackage == null ? "" : mCallingPackage);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001623 out.attribute(null, ATTR_RESIZE_MODE, String.valueOf(mResizeMode));
Winson Chungd3395382016-12-13 11:49:09 -08001624 out.attribute(null, ATTR_SUPPORTS_PICTURE_IN_PICTURE,
1625 String.valueOf(mSupportsPictureInPicture));
Wale Ogunwale706ed792015-08-02 10:29:44 -07001626 if (mLastNonFullscreenBounds != null) {
1627 out.attribute(
1628 null, ATTR_NON_FULLSCREEN_BOUNDS, mLastNonFullscreenBounds.flattenToString());
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001629 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001630 out.attribute(null, ATTR_MIN_WIDTH, String.valueOf(mMinWidth));
1631 out.attribute(null, ATTR_MIN_HEIGHT, String.valueOf(mMinHeight));
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001632 out.attribute(null, ATTR_PERSIST_TASK_VERSION, String.valueOf(PERSIST_TASK_VERSION));
Winson Chung2cb86c72014-06-25 12:03:30 -07001633
Craig Mautner21d24a22014-04-23 11:45:37 -07001634 if (affinityIntent != null) {
1635 out.startTag(null, TAG_AFFINITYINTENT);
1636 affinityIntent.saveToXml(out);
1637 out.endTag(null, TAG_AFFINITYINTENT);
1638 }
1639
Winson Chung36f3f032016-09-08 23:29:43 +00001640 out.startTag(null, TAG_INTENT);
1641 intent.saveToXml(out);
1642 out.endTag(null, TAG_INTENT);
Craig Mautner21d24a22014-04-23 11:45:37 -07001643
1644 final ArrayList<ActivityRecord> activities = mActivities;
1645 final int numActivities = activities.size();
1646 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1647 final ActivityRecord r = activities.get(activityNdx);
Craig Mautner43e52ed2014-06-16 17:18:52 -07001648 if (r.info.persistableMode == ActivityInfo.PERSIST_ROOT_ONLY || !r.isPersistable() ||
Wale Ogunwale843bfb92015-03-27 11:06:48 -07001649 ((r.intent.getFlags() & FLAG_ACTIVITY_NEW_DOCUMENT
1650 | FLAG_ACTIVITY_RETAIN_IN_RECENTS) == FLAG_ACTIVITY_NEW_DOCUMENT) &&
Craig Mautner43e52ed2014-06-16 17:18:52 -07001651 activityNdx > 0) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001652 // Stop at first non-persistable or first break in task (CLEAR_WHEN_TASK_RESET).
Craig Mautner21d24a22014-04-23 11:45:37 -07001653 break;
1654 }
1655 out.startTag(null, TAG_ACTIVITY);
1656 r.saveToXml(out);
1657 out.endTag(null, TAG_ACTIVITY);
1658 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001659 }
1660
Winson Chung36f3f032016-09-08 23:29:43 +00001661 static TaskRecord restoreFromXml(XmlPullParser in, ActivityStackSupervisor stackSupervisor)
1662 throws IOException, XmlPullParserException {
Craig Mautner21d24a22014-04-23 11:45:37 -07001663 Intent intent = null;
1664 Intent affinityIntent = null;
Winsonc809cbb2015-11-02 12:06:15 -08001665 ArrayList<ActivityRecord> activities = new ArrayList<>();
Craig Mautner21d24a22014-04-23 11:45:37 -07001666 ComponentName realActivity = null;
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001667 boolean realActivitySuspended = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001668 ComponentName origActivity = null;
1669 String affinity = null;
Dianne Hackborn79228822014-09-16 11:11:23 -07001670 String rootAffinity = null;
1671 boolean hasRootAffinity = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001672 boolean rootHasReset = false;
Dianne Hackborn13420f22014-07-18 15:43:56 -07001673 boolean autoRemoveRecents = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001674 boolean askedCompatMode = false;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001675 int taskType = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -07001676 int userId = 0;
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001677 boolean userSetupComplete = true;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001678 int effectiveUid = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -07001679 String lastDescription = null;
1680 long lastTimeOnTop = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001681 boolean neverRelinquishIdentity = true;
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001682 int taskId = INVALID_TASK_ID;
Craig Mautner21d24a22014-04-23 11:45:37 -07001683 final int outerDepth = in.getDepth();
Craig Mautner648f69b2014-09-18 14:16:26 -07001684 TaskDescription taskDescription = new TaskDescription();
Wale Ogunwale18795a22014-12-03 11:38:33 -08001685 int taskAffiliation = INVALID_TASK_ID;
Winson Chungec396d62014-08-06 17:08:00 -07001686 int taskAffiliationColor = 0;
Wale Ogunwale18795a22014-12-03 11:38:33 -08001687 int prevTaskId = INVALID_TASK_ID;
1688 int nextTaskId = INVALID_TASK_ID;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001689 int callingUid = -1;
1690 String callingPackage = "";
Wale Ogunwaled829d362016-02-10 19:24:49 -08001691 int resizeMode = RESIZE_MODE_FORCE_RESIZEABLE;
Winson Chungd3395382016-12-13 11:49:09 -08001692 boolean supportsPictureInPicture = false;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001693 Rect bounds = null;
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001694 int minWidth = INVALID_MIN_SIZE;
1695 int minHeight = INVALID_MIN_SIZE;
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001696 int persistTaskVersion = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -07001697
1698 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
1699 final String attrName = in.getAttributeName(attrNdx);
1700 final String attrValue = in.getAttributeValue(attrNdx);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001701 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: attribute name=" +
1702 attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001703 if (ATTR_TASKID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001704 if (taskId == INVALID_TASK_ID) taskId = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001705 } else if (ATTR_REALACTIVITY.equals(attrName)) {
1706 realActivity = ComponentName.unflattenFromString(attrValue);
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001707 } else if (ATTR_REALACTIVITY_SUSPENDED.equals(attrName)) {
1708 realActivitySuspended = Boolean.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001709 } else if (ATTR_ORIGACTIVITY.equals(attrName)) {
1710 origActivity = ComponentName.unflattenFromString(attrValue);
1711 } else if (ATTR_AFFINITY.equals(attrName)) {
1712 affinity = attrValue;
Dianne Hackborn79228822014-09-16 11:11:23 -07001713 } else if (ATTR_ROOT_AFFINITY.equals(attrName)) {
1714 rootAffinity = attrValue;
1715 hasRootAffinity = true;
Craig Mautner21d24a22014-04-23 11:45:37 -07001716 } else if (ATTR_ROOTHASRESET.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001717 rootHasReset = Boolean.parseBoolean(attrValue);
Dianne Hackborn13420f22014-07-18 15:43:56 -07001718 } else if (ATTR_AUTOREMOVERECENTS.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001719 autoRemoveRecents = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001720 } else if (ATTR_ASKEDCOMPATMODE.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001721 askedCompatMode = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001722 } else if (ATTR_USERID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001723 userId = Integer.parseInt(attrValue);
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001724 } else if (ATTR_USER_SETUP_COMPLETE.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001725 userSetupComplete = Boolean.parseBoolean(attrValue);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001726 } else if (ATTR_EFFECTIVE_UID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001727 effectiveUid = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001728 } else if (ATTR_TASKTYPE.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001729 taskType = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001730 } else if (ATTR_LASTDESCRIPTION.equals(attrName)) {
1731 lastDescription = attrValue;
1732 } else if (ATTR_LASTTIMEMOVED.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001733 lastTimeOnTop = Long.parseLong(attrValue);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001734 } else if (ATTR_NEVERRELINQUISH.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001735 neverRelinquishIdentity = Boolean.parseBoolean(attrValue);
Craig Mautner648f69b2014-09-18 14:16:26 -07001736 } else if (attrName.startsWith(TaskDescription.ATTR_TASKDESCRIPTION_PREFIX)) {
1737 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001738 } else if (ATTR_TASK_AFFILIATION.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001739 taskAffiliation = Integer.parseInt(attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001740 } else if (ATTR_PREV_AFFILIATION.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001741 prevTaskId = Integer.parseInt(attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001742 } else if (ATTR_NEXT_AFFILIATION.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001743 nextTaskId = Integer.parseInt(attrValue);
Winson Chungec396d62014-08-06 17:08:00 -07001744 } else if (ATTR_TASK_AFFILIATION_COLOR.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001745 taskAffiliationColor = Integer.parseInt(attrValue);
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001746 } else if (ATTR_CALLING_UID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001747 callingUid = Integer.parseInt(attrValue);
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001748 } else if (ATTR_CALLING_PACKAGE.equals(attrName)) {
1749 callingPackage = attrValue;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001750 } else if (ATTR_RESIZE_MODE.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001751 resizeMode = Integer.parseInt(attrValue);
Winson Chungd3395382016-12-13 11:49:09 -08001752 } else if (ATTR_SUPPORTS_PICTURE_IN_PICTURE.equals(attrName)) {
1753 supportsPictureInPicture = Boolean.parseBoolean(attrValue);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001754 } else if (ATTR_NON_FULLSCREEN_BOUNDS.equals(attrName)) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001755 bounds = Rect.unflattenFromString(attrValue);
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001756 } else if (ATTR_MIN_WIDTH.equals(attrName)) {
1757 minWidth = Integer.parseInt(attrValue);
1758 } else if (ATTR_MIN_HEIGHT.equals(attrName)) {
1759 minHeight = Integer.parseInt(attrValue);
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001760 } else if (ATTR_PERSIST_TASK_VERSION.equals(attrName)) {
1761 persistTaskVersion = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001762 } else {
1763 Slog.w(TAG, "TaskRecord: Unknown attribute=" + attrName);
1764 }
1765 }
1766
1767 int event;
1768 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
Ben Kwa8814cf42015-07-08 10:54:56 -07001769 (event != XmlPullParser.END_TAG || in.getDepth() >= outerDepth)) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001770 if (event == XmlPullParser.START_TAG) {
1771 final String name = in.getName();
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001772 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: START_TAG name=" +
1773 name);
Craig Mautner21d24a22014-04-23 11:45:37 -07001774 if (TAG_AFFINITYINTENT.equals(name)) {
1775 affinityIntent = Intent.restoreFromXml(in);
1776 } else if (TAG_INTENT.equals(name)) {
1777 intent = Intent.restoreFromXml(in);
1778 } else if (TAG_ACTIVITY.equals(name)) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001779 ActivityRecord activity = ActivityRecord.restoreFromXml(in, stackSupervisor);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001780 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: activity=" +
1781 activity);
Craig Mautner21d24a22014-04-23 11:45:37 -07001782 if (activity != null) {
1783 activities.add(activity);
1784 }
1785 } else {
1786 Slog.e(TAG, "restoreTask: Unexpected name=" + name);
1787 XmlUtils.skipCurrentTag(in);
1788 }
1789 }
1790 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001791 if (!hasRootAffinity) {
1792 rootAffinity = affinity;
1793 } else if ("@".equals(rootAffinity)) {
1794 rootAffinity = null;
1795 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001796 if (effectiveUid <= 0) {
1797 Intent checkIntent = intent != null ? intent : affinityIntent;
1798 effectiveUid = 0;
1799 if (checkIntent != null) {
1800 IPackageManager pm = AppGlobals.getPackageManager();
1801 try {
1802 ApplicationInfo ai = pm.getApplicationInfo(
1803 checkIntent.getComponent().getPackageName(),
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -07001804 PackageManager.MATCH_UNINSTALLED_PACKAGES
1805 | PackageManager.MATCH_DISABLED_COMPONENTS, userId);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001806 if (ai != null) {
1807 effectiveUid = ai.uid;
1808 }
1809 } catch (RemoteException e) {
1810 }
1811 }
1812 Slog.w(TAG, "Updating task #" + taskId + " for " + checkIntent
1813 + ": effectiveUid=" + effectiveUid);
1814 }
1815
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001816 if (persistTaskVersion < 1) {
1817 // We need to convert the resize mode of home activities saved before version one if
1818 // they are marked as RESIZE_MODE_RESIZEABLE to RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION
1819 // since we didn't have that differentiation before version 1 and the system didn't
1820 // resize home activities before then.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001821 if (taskType == 1 /* old home type */ && resizeMode == RESIZE_MODE_RESIZEABLE) {
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001822 resizeMode = RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
1823 }
Winson Chungd3395382016-12-13 11:49:09 -08001824 } else {
1825 // This activity has previously marked itself explicitly as both resizeable and
1826 // supporting picture-in-picture. Since there is no longer a requirement for
1827 // picture-in-picture activities to be resizeable, we can mark this simply as
1828 // resizeable and supporting picture-in-picture separately.
1829 if (resizeMode == RESIZE_MODE_RESIZEABLE_AND_PIPABLE_DEPRECATED) {
1830 resizeMode = RESIZE_MODE_RESIZEABLE;
1831 supportsPictureInPicture = true;
1832 }
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001833 }
1834
Winson Chung36f3f032016-09-08 23:29:43 +00001835 final TaskRecord task = new TaskRecord(stackSupervisor.mService, taskId, intent,
Dianne Hackborn79228822014-09-16 11:11:23 -07001836 affinityIntent, affinity, rootAffinity, realActivity, origActivity, rootHasReset,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001837 autoRemoveRecents, askedCompatMode, userId, effectiveUid, lastDescription,
Winson Chungfb44d212017-10-04 11:39:10 -07001838 activities, lastTimeOnTop, neverRelinquishIdentity, taskDescription,
1839 taskAffiliation, prevTaskId, nextTaskId, taskAffiliationColor, callingUid,
Charles He2bf28322017-10-12 22:24:49 +01001840 callingPackage, resizeMode, supportsPictureInPicture, realActivitySuspended,
1841 userSetupComplete, minWidth, minHeight);
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07001842 task.updateOverrideConfiguration(bounds);
Craig Mautner21d24a22014-04-23 11:45:37 -07001843
1844 for (int activityNdx = activities.size() - 1; activityNdx >=0; --activityNdx) {
Bryce Leeaf691c02017-03-20 14:20:22 -07001845 activities.get(activityNdx).setTask(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001846 }
1847
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001848 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Restored task=" + task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001849 return task;
1850 }
1851
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001852 private void adjustForMinimalTaskDimensions(Rect bounds) {
1853 if (bounds == null) {
1854 return;
1855 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001856 int minWidth = mMinWidth;
1857 int minHeight = mMinHeight;
Robert Carr9c5867d2016-03-10 15:52:46 -08001858 // If the task has no requested minimal size, we'd like to enforce a minimal size
1859 // so that the user can not render the task too small to manipulate. We don't need
1860 // to do this for the pinned stack as the bounds are controlled by the system.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001861 if (!inPinnedWindowingMode()) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001862 if (minWidth == INVALID_MIN_SIZE) {
1863 minWidth = mService.mStackSupervisor.mDefaultMinSizeOfResizeableTask;
Andrii Kulian2e751b82016-03-16 16:59:32 -07001864 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001865 if (minHeight == INVALID_MIN_SIZE) {
1866 minHeight = mService.mStackSupervisor.mDefaultMinSizeOfResizeableTask;
Andrii Kulian2e751b82016-03-16 16:59:32 -07001867 }
Robert Carr9c5867d2016-03-10 15:52:46 -08001868 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001869 final boolean adjustWidth = minWidth > bounds.width();
1870 final boolean adjustHeight = minHeight > bounds.height();
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001871 if (!(adjustWidth || adjustHeight)) {
1872 return;
1873 }
1874
1875 if (adjustWidth) {
1876 if (mBounds != null && bounds.right == mBounds.right) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001877 bounds.left = bounds.right - minWidth;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001878 } else {
1879 // Either left bounds match, or neither match, or the previous bounds were
1880 // fullscreen and we default to keeping left.
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001881 bounds.right = bounds.left + minWidth;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001882 }
1883 }
1884 if (adjustHeight) {
1885 if (mBounds != null && bounds.bottom == mBounds.bottom) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001886 bounds.top = bounds.bottom - minHeight;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001887 } else {
1888 // Either top bounds match, or neither match, or the previous bounds were
1889 // fullscreen and we default to keeping top.
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001890 bounds.bottom = bounds.top + minHeight;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001891 }
1892 }
1893 }
1894
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001895 /**
Winson Chung5af42fc2017-03-24 17:11:33 -07001896 * @return a new Configuration for this Task, given the provided {@param bounds} and
1897 * {@param insetBounds}.
1898 */
1899 Configuration computeNewOverrideConfigurationForBounds(Rect bounds, Rect insetBounds) {
1900 // Compute a new override configuration for the given bounds, if fullscreen bounds
1901 // (bounds == null), then leave the override config unset
1902 final Configuration newOverrideConfig = new Configuration();
1903 if (bounds != null) {
1904 newOverrideConfig.setTo(getOverrideConfiguration());
1905 mTmpRect.set(bounds);
1906 adjustForMinimalTaskDimensions(mTmpRect);
1907 computeOverrideConfiguration(newOverrideConfig, mTmpRect, insetBounds,
1908 mTmpRect.right != bounds.right, mTmpRect.bottom != bounds.bottom);
1909 }
1910
1911 return newOverrideConfig;
1912 }
1913
1914 /**
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001915 * Update task's override configuration based on the bounds.
Jorim Jaggi0a932142016-02-01 17:42:25 -08001916 * @param bounds The bounds of the task.
Andrii Kulian8072d112016-09-16 11:11:01 -07001917 * @return True if the override configuration was updated.
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001918 */
Andrii Kulian8072d112016-09-16 11:11:01 -07001919 boolean updateOverrideConfiguration(Rect bounds) {
Jorim Jaggi0a932142016-02-01 17:42:25 -08001920 return updateOverrideConfiguration(bounds, null /* insetBounds */);
1921 }
1922
1923 /**
1924 * Update task's override configuration based on the bounds.
1925 * @param bounds The bounds of the task.
1926 * @param insetBounds The bounds used to calculate the system insets, which is used here to
1927 * subtract the navigation bar/status bar size from the screen size reported
1928 * to the application. See {@link IActivityManager#resizeDockedStack}.
Andrii Kulian8072d112016-09-16 11:11:01 -07001929 * @return True if the override configuration was updated.
Jorim Jaggi0a932142016-02-01 17:42:25 -08001930 */
Andrii Kulian8072d112016-09-16 11:11:01 -07001931 boolean updateOverrideConfiguration(Rect bounds, @Nullable Rect insetBounds) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001932 if (Objects.equals(mBounds, bounds)) {
Andrii Kulian8072d112016-09-16 11:11:01 -07001933 return false;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001934 }
Andrii Kulian1779e612016-10-12 21:58:25 -07001935 mTmpConfig.setTo(getOverrideConfiguration());
Wale Ogunwale5f986092015-12-04 15:35:38 -08001936 final boolean oldFullscreen = mFullscreen;
Andrii Kulian1779e612016-10-12 21:58:25 -07001937 final Configuration newConfig = getOverrideConfiguration();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001938
1939 mFullscreen = bounds == null;
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001940 final boolean persistBounds = getWindowConfiguration().persistTaskBounds();
Wale Ogunwale706ed792015-08-02 10:29:44 -07001941 if (mFullscreen) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001942 if (mBounds != null && persistBounds) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001943 mLastNonFullscreenBounds = mBounds;
1944 }
1945 mBounds = null;
Andrii Kulian1779e612016-10-12 21:58:25 -07001946 newConfig.unset();
Wale Ogunwale706ed792015-08-02 10:29:44 -07001947 } else {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001948 mTmpRect.set(bounds);
1949 adjustForMinimalTaskDimensions(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -08001950 if (mBounds == null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001951 mBounds = new Rect(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -08001952 } else {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001953 mBounds.set(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -08001954 }
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001955 if (mStack == null || persistBounds) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001956 mLastNonFullscreenBounds = mBounds;
1957 }
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001958 computeOverrideConfiguration(newConfig, mTmpRect, insetBounds,
Andrii Kuliandaea3572016-04-08 13:20:51 -07001959 mTmpRect.right != bounds.right, mTmpRect.bottom != bounds.bottom);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001960 }
Andrii Kulian1779e612016-10-12 21:58:25 -07001961 onOverrideConfigurationChanged(newConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001962
1963 if (mFullscreen != oldFullscreen) {
Winson Chung5af42fc2017-03-24 17:11:33 -07001964 mService.mStackSupervisor.scheduleUpdateMultiWindowMode(this);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001965 }
1966
Andrii Kulian1779e612016-10-12 21:58:25 -07001967 return !mTmpConfig.equals(newConfig);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001968 }
1969
Andrii Kulian1779e612016-10-12 21:58:25 -07001970 /** Clears passed config and fills it with new override values. */
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001971 // TODO(b/36505427): TaskRecord.computeOverrideConfiguration() is a utility method that doesn't
1972 // depend on task or stacks, but uses those object to get the display to base the calculation
1973 // on. Probably best to centralize calculations like this in ConfigurationContainer.
1974 void computeOverrideConfiguration(Configuration config, Rect bounds, Rect insetBounds,
Andrii Kuliana8a9bc52016-10-14 11:00:13 -07001975 boolean overrideWidth, boolean overrideHeight) {
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001976 mTmpNonDecorBounds.set(bounds);
1977 mTmpStableBounds.set(bounds);
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001978
Andrii Kulian1779e612016-10-12 21:58:25 -07001979 config.unset();
Winson Chungbdc646f2017-02-13 12:12:22 -08001980 final Configuration parentConfig = getParent().getConfiguration();
Bryce Lee7566d762017-03-30 09:34:15 -07001981
Andrii Kulian1779e612016-10-12 21:58:25 -07001982 final float density = parentConfig.densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001983
Winson Chungbdc646f2017-02-13 12:12:22 -08001984 if (mStack != null) {
1985 final StackWindowController stackController = mStack.getWindowContainerController();
1986 stackController.adjustConfigurationForBounds(bounds, insetBounds,
1987 mTmpNonDecorBounds, mTmpStableBounds, overrideWidth, overrideHeight, density,
1988 config, parentConfig);
1989 } else {
Bryce Lee7566d762017-03-30 09:34:15 -07001990 throw new IllegalArgumentException("Expected stack when calculating override config");
Winson Chungbdc646f2017-02-13 12:12:22 -08001991 }
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001992
Winson Chung60c1aba2017-03-14 17:47:42 -07001993 config.orientation = (config.screenWidthDp <= config.screenHeightDp)
1994 ? Configuration.ORIENTATION_PORTRAIT
1995 : Configuration.ORIENTATION_LANDSCAPE;
1996
Jorim Jaggi85639432016-05-06 17:27:55 -07001997 // For calculating screen layout, we need to use the non-decor inset screen area for the
1998 // calculation for compatibility reasons, i.e. screen area without system bars that could
1999 // never go away in Honeycomb.
Winson Chungbdc646f2017-02-13 12:12:22 -08002000 final int compatScreenWidthDp = (int) (mTmpNonDecorBounds.width() / density);
2001 final int compatScreenHeightDp = (int) (mTmpNonDecorBounds.height() / density);
Andrii Kulian1779e612016-10-12 21:58:25 -07002002 // We're only overriding LONG, SIZE and COMPAT parts of screenLayout, so we start override
2003 // calculation with partial default.
2004 final int sl = Configuration.SCREENLAYOUT_LONG_YES | Configuration.SCREENLAYOUT_SIZE_XLARGE;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002005 final int longSize = Math.max(compatScreenHeightDp, compatScreenWidthDp);
Andrii Kulian1779e612016-10-12 21:58:25 -07002006 final int shortSize = Math.min(compatScreenHeightDp, compatScreenWidthDp);
Jorim Jaggi85639432016-05-06 17:27:55 -07002007 config.screenLayout = Configuration.reduceScreenLayout(sl, longSize, shortSize);
2008
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08002009 }
2010
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002011 Rect updateOverrideConfigurationFromLaunchBounds() {
2012 final Rect bounds = validateBounds(getLaunchBounds());
2013 updateOverrideConfiguration(bounds);
Andrii Kulian73336d812016-03-24 12:56:08 -07002014 if (bounds != null) {
2015 bounds.set(mBounds);
2016 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002017 return bounds;
2018 }
2019
2020 static Rect validateBounds(Rect bounds) {
2021 if (bounds != null && bounds.isEmpty()) {
2022 Slog.wtf(TAG, "Received strange task bounds: " + bounds, new Throwable());
2023 return null;
2024 }
2025 return bounds;
2026 }
2027
Wale Ogunwale935e5022015-11-10 12:36:10 -08002028 /** Updates the task's bounds and override configuration to match what is expected for the
2029 * input stack. */
2030 void updateOverrideConfigurationForStack(ActivityStack inStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002031 if (mStack != null && mStack == inStack) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08002032 return;
2033 }
2034
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002035 if (inStack.inFreeformWindowingMode()) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002036 if (!isResizeable()) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08002037 throw new IllegalArgumentException("Can not position non-resizeable task="
2038 + this + " in stack=" + inStack);
2039 }
2040 if (mBounds != null) {
2041 return;
2042 }
2043 if (mLastNonFullscreenBounds != null) {
2044 updateOverrideConfiguration(mLastNonFullscreenBounds);
2045 } else {
2046 inStack.layoutTaskInStack(this, null);
2047 }
2048 } else {
2049 updateOverrideConfiguration(inStack.mBounds);
2050 }
2051 }
2052
Wale Ogunwale706ed792015-08-02 10:29:44 -07002053 /** Returns the bounds that should be used to launch this task. */
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002054 private Rect getLaunchBounds() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002055 if (mStack == null) {
Chong Zhang7d5f5102016-01-13 10:29:24 -08002056 return null;
2057 }
2058
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002059 final int windowingMode = getWindowingMode();
2060 if (!isActivityTypeStandardOrUndefined()
2061 || windowingMode == WINDOWING_MODE_FULLSCREEN
2062 || (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY && !isResizeable())) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002063 return isResizeable() ? mStack.mBounds : null;
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002064 } else if (!getWindowConfiguration().persistTaskBounds()) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002065 return mStack.mBounds;
Wale Ogunwale706ed792015-08-02 10:29:44 -07002066 }
2067 return mLastNonFullscreenBounds;
2068 }
2069
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01002070 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
2071 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2072 final ActivityRecord r = mActivities.get(activityNdx);
2073 if (r.visible) {
2074 r.showStartingWindow(null /* prev */, false /* newTask */, taskSwitch);
2075 }
2076 }
2077 }
2078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002080 pw.print(prefix); pw.print("userId="); pw.print(userId);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07002081 pw.print(" effectiveUid="); UserHandle.formatUid(pw, effectiveUid);
2082 pw.print(" mCallingUid="); UserHandle.formatUid(pw, mCallingUid);
Suprabh Shukla7745c142016-03-07 18:21:10 -08002083 pw.print(" mUserSetupComplete="); pw.print(mUserSetupComplete);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002084 pw.print(" mCallingPackage="); pw.println(mCallingPackage);
Dianne Hackborn79228822014-09-16 11:11:23 -07002085 if (affinity != null || rootAffinity != null) {
2086 pw.print(prefix); pw.print("affinity="); pw.print(affinity);
2087 if (affinity == null || !affinity.equals(rootAffinity)) {
2088 pw.print(" root="); pw.println(rootAffinity);
2089 } else {
2090 pw.println();
2091 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002092 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002093 if (voiceSession != null || voiceInteractor != null) {
2094 pw.print(prefix); pw.print("VOICE: session=0x");
2095 pw.print(Integer.toHexString(System.identityHashCode(voiceSession)));
2096 pw.print(" interactor=0x");
2097 pw.println(Integer.toHexString(System.identityHashCode(voiceInteractor)));
2098 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002099 if (intent != null) {
2100 StringBuilder sb = new StringBuilder(128);
2101 sb.append(prefix); sb.append("intent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08002102 intent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002103 sb.append('}');
2104 pw.println(sb.toString());
2105 }
2106 if (affinityIntent != null) {
2107 StringBuilder sb = new StringBuilder(128);
2108 sb.append(prefix); sb.append("affinityIntent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08002109 affinityIntent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002110 sb.append('}');
2111 pw.println(sb.toString());
2112 }
2113 if (origActivity != null) {
2114 pw.print(prefix); pw.print("origActivity=");
2115 pw.println(origActivity.flattenToShortString());
2116 }
2117 if (realActivity != null) {
2118 pw.print(prefix); pw.print("realActivity=");
2119 pw.println(realActivity.flattenToShortString());
2120 }
Wale Ogunwale66e16852017-10-19 13:35:52 -07002121 if (autoRemoveRecents || isPersistable || !isActivityTypeStandard() || numFullscreen != 0) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002122 pw.print(prefix); pw.print("autoRemoveRecents="); pw.print(autoRemoveRecents);
Dianne Hackborn852975d2014-08-22 17:42:43 -07002123 pw.print(" isPersistable="); pw.print(isPersistable);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002124 pw.print(" numFullscreen="); pw.print(numFullscreen);
Wale Ogunwale66e16852017-10-19 13:35:52 -07002125 pw.print(" activityType="); pw.println(getActivityType());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002126 }
Craig Mautner432f64e2015-05-20 14:59:57 -07002127 if (rootWasReset || mNeverRelinquishIdentity || mReuseTask
2128 || mLockTaskAuth != LOCK_TASK_AUTH_PINNABLE) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002129 pw.print(prefix); pw.print("rootWasReset="); pw.print(rootWasReset);
2130 pw.print(" mNeverRelinquishIdentity="); pw.print(mNeverRelinquishIdentity);
Craig Mautner432f64e2015-05-20 14:59:57 -07002131 pw.print(" mReuseTask="); pw.print(mReuseTask);
2132 pw.print(" mLockTaskAuth="); pw.println(lockTaskAuthToString());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002133 }
Wale Ogunwale18795a22014-12-03 11:38:33 -08002134 if (mAffiliatedTaskId != taskId || mPrevAffiliateTaskId != INVALID_TASK_ID
2135 || mPrevAffiliate != null || mNextAffiliateTaskId != INVALID_TASK_ID
2136 || mNextAffiliate != null) {
Dianne Hackborn852975d2014-08-22 17:42:43 -07002137 pw.print(prefix); pw.print("affiliation="); pw.print(mAffiliatedTaskId);
2138 pw.print(" prevAffiliation="); pw.print(mPrevAffiliateTaskId);
2139 pw.print(" (");
2140 if (mPrevAffiliate == null) {
2141 pw.print("null");
2142 } else {
2143 pw.print(Integer.toHexString(System.identityHashCode(mPrevAffiliate)));
2144 }
2145 pw.print(") nextAffiliation="); pw.print(mNextAffiliateTaskId);
2146 pw.print(" (");
2147 if (mNextAffiliate == null) {
2148 pw.print("null");
2149 } else {
2150 pw.print(Integer.toHexString(System.identityHashCode(mNextAffiliate)));
2151 }
2152 pw.println(")");
2153 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002154 pw.print(prefix); pw.print("Activities="); pw.println(mActivities);
Dianne Hackborn852975d2014-08-22 17:42:43 -07002155 if (!askedCompatMode || !inRecents || !isAvailable) {
2156 pw.print(prefix); pw.print("askedCompatMode="); pw.print(askedCompatMode);
2157 pw.print(" inRecents="); pw.print(inRecents);
2158 pw.print(" isAvailable="); pw.println(isAvailable);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07002159 }
Dianne Hackborn852975d2014-08-22 17:42:43 -07002160 if (lastDescription != null) {
2161 pw.print(prefix); pw.print("lastDescription="); pw.println(lastDescription);
2162 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002163 pw.print(prefix); pw.print("stackId="); pw.println(getStackId());
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002164 pw.print(prefix + "hasBeenVisible=" + hasBeenVisible);
2165 pw.print(" mResizeMode=" + ActivityInfo.resizeModeToString(mResizeMode));
Winson Chungd3395382016-12-13 11:49:09 -08002166 pw.print(" mSupportsPictureInPicture=" + mSupportsPictureInPicture);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002167 pw.print(" isResizeable=" + isResizeable());
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002168 pw.print(" lastActiveTime=" + lastActiveTime);
2169 pw.println(" (inactive for " + (getInactiveDuration() / 1000) + "s)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 }
2171
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002172 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002174 StringBuilder sb = new StringBuilder(128);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002175 if (stringName != null) {
2176 sb.append(stringName);
2177 sb.append(" U=");
2178 sb.append(userId);
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002179 sb.append(" StackId=");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002180 sb.append(getStackId());
Craig Mautnerde4ef022013-04-07 19:01:33 -07002181 sb.append(" sz=");
2182 sb.append(mActivities.size());
2183 sb.append('}');
2184 return sb.toString();
2185 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002186 sb.append("TaskRecord{");
2187 sb.append(Integer.toHexString(System.identityHashCode(this)));
2188 sb.append(" #");
2189 sb.append(taskId);
2190 if (affinity != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002191 sb.append(" A=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002192 sb.append(affinity);
2193 } else if (intent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002194 sb.append(" I=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002195 sb.append(intent.getComponent().flattenToShortString());
2196 } else if (affinityIntent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002197 sb.append(" aI=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002198 sb.append(affinityIntent.getComponent().flattenToShortString());
2199 } else {
2200 sb.append(" ??");
2201 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002202 stringName = sb.toString();
2203 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07002205
2206 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2207 final long token = proto.start(fieldId);
2208 super.writeToProto(proto, CONFIGURATION_CONTAINER);
2209 proto.write(ID, taskId);
2210 for (int i = mActivities.size() - 1; i >= 0; i--) {
2211 ActivityRecord activity = mActivities.get(i);
2212 activity.writeToProto(proto, ACTIVITIES);
2213 }
2214 proto.write(STACK_ID, mStack.mStackId);
2215 if (mLastNonFullscreenBounds != null) {
2216 mLastNonFullscreenBounds.writeToProto(proto, LAST_NON_FULLSCREEN_BOUNDS);
2217 }
2218 if (realActivity != null) {
2219 proto.write(REAL_ACTIVITY, realActivity.flattenToShortString());
2220 }
2221 if (origActivity != null) {
2222 proto.write(ORIG_ACTIVITY, origActivity.flattenToShortString());
2223 }
2224 proto.write(ACTIVITY_TYPE, getActivityType());
Steven Timotius4346f0a2017-09-12 11:07:21 -07002225 proto.write(RESIZE_MODE, mResizeMode);
2226 proto.write(FULLSCREEN, mFullscreen);
2227 if (mBounds != null) {
2228 mBounds.writeToProto(proto, BOUNDS);
2229 }
2230 proto.write(MIN_WIDTH, mMinWidth);
2231 proto.write(MIN_HEIGHT, mMinHeight);
2232 proto.end(token);
2233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234}