blob: 4080c27b4eccec3b5ac898d7bcee20820554f543 [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;
33import static android.content.Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
Wale Ogunwale3eadad72016-10-13 09:16:59 -070034import static android.content.pm.ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY;
Jorim Jaggi0a932142016-02-01 17:42:25 -080035import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_ALWAYS;
36import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_DEFAULT;
37import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
38import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_NEVER;
skuhne@google.com322347b2016-12-02 12:54:03 -080039import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZABLE_LANDSCAPE_ONLY;
40import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZABLE_PORTRAIT_ONLY;
41import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZABLE_PRESERVE_ORIENTATION;
Wale Ogunwaled829d362016-02-10 19:24:49 -080042import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Wale Ogunwale625ed0c2016-10-18 08:50:31 -070043import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE;
Winson Chungd3395382016-12-13 11:49:09 -080044import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_AND_PIPABLE_DEPRECATED;
Wale Ogunwale625ed0c2016-10-18 08:50:31 -070045import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080046import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
Suprabh Shukla7745c142016-03-07 18:21:10 -080047import static android.provider.Settings.Secure.USER_SETUP_COMPLETE;
Andrii Kulian036e3ad2017-04-19 10:55:10 -070048import static android.view.Display.DEFAULT_DISPLAY;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070049
Jorim Jaggi0a932142016-02-01 17:42:25 -080050import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ADD_REMOVE;
51import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
52import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
53import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
54import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_ADD_REMOVE;
55import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
56import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
57import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
58import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
59import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale3b232392016-05-13 15:37:13 -070060import static com.android.server.am.ActivityRecord.STARTING_WINDOW_SHOWN;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080061import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
Wale Ogunwale56d8d162017-05-30 11:12:20 -070062import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING_TO_TOP;
Wale Ogunwaleab5de372017-10-18 06:46:31 -070063import static com.android.server.am.ActivityStackSupervisor.ON_TOP;
Winson Chung6954fc92017-03-24 16:22:12 -070064import static com.android.server.am.ActivityStackSupervisor.PAUSE_IMMEDIATELY;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080065import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Steven Timotius4346f0a2017-09-12 11:07:21 -070066import static com.android.server.am.proto.TaskRecordProto.ACTIVITIES;
67import static com.android.server.am.proto.TaskRecordProto.BOUNDS;
68import static com.android.server.am.proto.TaskRecordProto.CONFIGURATION_CONTAINER;
69import static com.android.server.am.proto.TaskRecordProto.FULLSCREEN;
70import static com.android.server.am.proto.TaskRecordProto.ID;
71import static com.android.server.am.proto.TaskRecordProto.LAST_NON_FULLSCREEN_BOUNDS;
72import static com.android.server.am.proto.TaskRecordProto.MIN_HEIGHT;
73import static com.android.server.am.proto.TaskRecordProto.MIN_WIDTH;
74import static com.android.server.am.proto.TaskRecordProto.ORIG_ACTIVITY;
75import static com.android.server.am.proto.TaskRecordProto.REAL_ACTIVITY;
76import static com.android.server.am.proto.TaskRecordProto.RESIZE_MODE;
77import static com.android.server.am.proto.TaskRecordProto.RETURN_TO_TYPE;
78import static com.android.server.am.proto.TaskRecordProto.STACK_ID;
79import static com.android.server.am.proto.TaskRecordProto.ACTIVITY_TYPE;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070080
Winson Chung74666102017-02-22 17:49:24 -080081import static java.lang.Integer.MAX_VALUE;
82
Jorim Jaggie7d2b852017-08-28 17:55:15 +020083import android.annotation.IntDef;
84import android.annotation.Nullable;
85import android.app.Activity;
86import android.app.ActivityManager;
Jorim Jaggie7d2b852017-08-28 17:55:15 +020087import android.app.ActivityManager.TaskDescription;
88import android.app.ActivityManager.TaskSnapshot;
89import android.app.ActivityOptions;
90import android.app.AppGlobals;
91import android.app.IActivityManager;
92import android.content.ComponentName;
93import android.content.Intent;
94import android.content.pm.ActivityInfo;
95import android.content.pm.ApplicationInfo;
96import android.content.pm.IPackageManager;
97import android.content.pm.PackageManager;
98import android.content.res.Configuration;
99import android.graphics.Rect;
100import android.os.Debug;
101import android.os.RemoteException;
Winson Chungfb44d212017-10-04 11:39:10 -0700102import android.os.SystemClock;
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200103import android.os.Trace;
104import android.os.UserHandle;
105import android.provider.Settings;
106import android.service.voice.IVoiceInteractionSession;
107import android.util.DisplayMetrics;
108import android.util.Slog;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700109import android.util.proto.ProtoOutputStream;
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200110
111import com.android.internal.annotations.VisibleForTesting;
112import com.android.internal.app.IVoiceInteractor;
113import com.android.internal.util.XmlUtils;
114import com.android.server.wm.AppWindowContainerController;
115import com.android.server.wm.ConfigurationContainer;
116import com.android.server.wm.StackWindowController;
117import com.android.server.wm.TaskWindowContainerController;
118import com.android.server.wm.TaskWindowContainerListener;
119import com.android.server.wm.WindowManagerService;
120
121import org.xmlpull.v1.XmlPullParser;
122import org.xmlpull.v1.XmlPullParserException;
123import org.xmlpull.v1.XmlSerializer;
124
125import java.io.IOException;
126import java.io.PrintWriter;
127import java.lang.annotation.Retention;
128import java.lang.annotation.RetentionPolicy;
129import java.util.ArrayList;
130import java.util.Objects;
131
Benjamin Franza83859f2017-07-03 16:34:14 +0100132class TaskRecord extends ConfigurationContainer implements TaskWindowContainerListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800133 private static final String TAG = TAG_WITH_CLASS_NAME ? "TaskRecord" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700134 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700135 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Craig Mautnere0570202015-05-13 13:06:11 -0700136 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700137 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800138
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700139 private static final String ATTR_TASKID = "task_id";
Craig Mautner21d24a22014-04-23 11:45:37 -0700140 private static final String TAG_INTENT = "intent";
141 private static final String TAG_AFFINITYINTENT = "affinity_intent";
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700142 private static final String ATTR_REALACTIVITY = "real_activity";
143 private static final String ATTR_REALACTIVITY_SUSPENDED = "real_activity_suspended";
Craig Mautner21d24a22014-04-23 11:45:37 -0700144 private static final String ATTR_ORIGACTIVITY = "orig_activity";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700145 private static final String TAG_ACTIVITY = "activity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700146 private static final String ATTR_AFFINITY = "affinity";
Dianne Hackborn79228822014-09-16 11:11:23 -0700147 private static final String ATTR_ROOT_AFFINITY = "root_affinity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700148 private static final String ATTR_ROOTHASRESET = "root_has_reset";
Dianne Hackborn13420f22014-07-18 15:43:56 -0700149 private static final String ATTR_AUTOREMOVERECENTS = "auto_remove_recents";
Craig Mautner21d24a22014-04-23 11:45:37 -0700150 private static final String ATTR_ASKEDCOMPATMODE = "asked_compat_mode";
151 private static final String ATTR_USERID = "user_id";
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800152 private static final String ATTR_USER_SETUP_COMPLETE = "user_setup_complete";
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700153 private static final String ATTR_EFFECTIVE_UID = "effective_uid";
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700154 @Deprecated
Craig Mautner21d24a22014-04-23 11:45:37 -0700155 private static final String ATTR_TASKTYPE = "task_type";
Craig Mautner21d24a22014-04-23 11:45:37 -0700156 private static final String ATTR_LASTDESCRIPTION = "last_description";
157 private static final String ATTR_LASTTIMEMOVED = "last_time_moved";
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700158 private static final String ATTR_NEVERRELINQUISH = "never_relinquish_identity";
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700159 private static final String ATTR_TASK_AFFILIATION = "task_affiliation";
Craig Mautnera228ae92014-07-09 05:44:55 -0700160 private static final String ATTR_PREV_AFFILIATION = "prev_affiliation";
161 private static final String ATTR_NEXT_AFFILIATION = "next_affiliation";
Winson Chungec396d62014-08-06 17:08:00 -0700162 private static final String ATTR_TASK_AFFILIATION_COLOR = "task_affiliation_color";
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700163 private static final String ATTR_CALLING_UID = "calling_uid";
164 private static final String ATTR_CALLING_PACKAGE = "calling_package";
Winson Chungd3395382016-12-13 11:49:09 -0800165 private static final String ATTR_SUPPORTS_PICTURE_IN_PICTURE = "supports_picture_in_picture";
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800166 private static final String ATTR_RESIZE_MODE = "resize_mode";
Wale Ogunwale706ed792015-08-02 10:29:44 -0700167 private static final String ATTR_NON_FULLSCREEN_BOUNDS = "non_fullscreen_bounds";
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700168 private static final String ATTR_MIN_WIDTH = "min_width";
169 private static final String ATTR_MIN_HEIGHT = "min_height";
Wale Ogunwale625ed0c2016-10-18 08:50:31 -0700170 private static final String ATTR_PERSIST_TASK_VERSION = "persist_task_version";
Andrii Kulian18d75122016-03-27 20:20:28 -0700171
Wale Ogunwale625ed0c2016-10-18 08:50:31 -0700172 // Current version of the task record we persist. Used to check if we need to run any upgrade
173 // code.
174 private static final int PERSIST_TASK_VERSION = 1;
Craig Mautner21d24a22014-04-23 11:45:37 -0700175 private static final String TASK_THUMBNAIL_SUFFIX = "_task_thumbnail";
176
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 })
190 public @interface ReparentMoveStackMode {}
191 // Moves the stack to the front if it was not at the front
192 public static final int REPARENT_MOVE_STACK_TO_FRONT = 0;
193 // Only moves the stack to the front if it was focused or front most already
194 public static final int REPARENT_KEEP_STACK_AT_FRONT = 1;
195 // Do not move the stack as a part of reparenting
196 public static final int REPARENT_LEAVE_STACK_IN_PLACE = 2;
197
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
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700268 /** Indication of what to run next when task exits. */
269 // TODO: Shouldn't be needed if we have things in visual order. I.e. we stop using stacks or
270 // have a stack per standard application type...
Wale Ogunwaleeea34ee92017-08-31 20:07:45 -0700271 /*@WindowConfiguration.ActivityType*/
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700272 private int mTaskToReturnTo = ACTIVITY_TYPE_STANDARD;
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700273
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700274 /** If original intent did not allow relinquishing task identity, save that information */
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700275 private boolean mNeverRelinquishIdentity = true;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700276
Craig Mautner362449a2014-06-20 14:04:39 -0700277 // Used in the unique case where we are clearing the task in order to reuse it. In that case we
278 // do not want to delete the stack when the task goes empty.
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -0800279 private boolean mReuseTask = false;
Craig Mautner362449a2014-06-20 14:04:39 -0700280
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700281 private final String mFilename;
282 CharSequence lastDescription; // Last description captured for this item.
283
Craig Mautnera228ae92014-07-09 05:44:55 -0700284 int mAffiliatedTaskId; // taskId of parent affiliation or self if no parent.
Winson Chungec396d62014-08-06 17:08:00 -0700285 int mAffiliatedTaskColor; // color of the parent task affiliation.
Craig Mautnera228ae92014-07-09 05:44:55 -0700286 TaskRecord mPrevAffiliate; // previous task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800287 int mPrevAffiliateTaskId = INVALID_TASK_ID; // previous id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700288 TaskRecord mNextAffiliate; // next task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800289 int mNextAffiliateTaskId = INVALID_TASK_ID; // next id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700290
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700291 // For relaunching the task from recents as though it was launched by the original launcher.
292 int mCallingUid;
293 String mCallingPackage;
294
Craig Mautner21d24a22014-04-23 11:45:37 -0700295 final ActivityManagerService mService;
296
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700297 // Whether or not this task covers the entire screen; by default tasks are fullscreen.
298 boolean mFullscreen = true;
299
300 // Bounds of the Task. null for fullscreen tasks.
301 Rect mBounds = null;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -0800302 private final Rect mTmpStableBounds = new Rect();
303 private final Rect mTmpNonDecorBounds = new Rect();
Wale Ogunwale9a08f822016-02-17 19:03:58 -0800304 private final Rect mTmpRect = new Rect();
Jorim Jaggi0a932142016-02-01 17:42:25 -0800305
Wale Ogunwale706ed792015-08-02 10:29:44 -0700306 // Last non-fullscreen bounds the task was launched in or resized to.
307 // The information is persisted and used to determine the appropriate stack to launch the
308 // task into on restore.
309 Rect mLastNonFullscreenBounds = null;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700310 // Minimal width and height of this task when it's resizeable. -1 means it should use the
311 // default minimal width/height.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700312 int mMinWidth;
313 int mMinHeight;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700314
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700315 // Ranking (from top) of this task among all visible tasks. (-1 means it's not visible)
316 // This number will be assigned when we evaluate OOM scores for all visible tasks.
317 int mLayerRank = -1;
318
Andrii Kulian1779e612016-10-12 21:58:25 -0700319 /** Helper object used for updating override configuration. */
320 private Configuration mTmpConfig = new Configuration();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700321
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800322 private TaskWindowContainerController mWindowContainerController;
323
Craig Mautner21d24a22014-04-23 11:45:37 -0700324 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700325 IVoiceInteractionSession _voiceSession, IVoiceInteractor _voiceInteractor) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700326 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700327 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
328 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800329 userId = UserHandle.getUserId(info.applicationInfo.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 taskId = _taskId;
Winson Chungfb44d212017-10-04 11:39:10 -0700331 lastActiveTime = SystemClock.elapsedRealtime();
Craig Mautnera228ae92014-07-09 05:44:55 -0700332 mAffiliatedTaskId = _taskId;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700333 voiceSession = _voiceSession;
334 voiceInteractor = _voiceInteractor;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700335 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800336 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700337 mCallingUid = info.applicationInfo.uid;
338 mCallingPackage = info.packageName;
Martijn Coenend4a69702014-06-30 11:12:17 -0700339 setIntent(_intent, info);
Andrii Kulian2e751b82016-03-16 16:59:32 -0700340 setMinDimensions(info);
Winson730bf062016-03-31 18:04:56 -0700341 touchActiveTime();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700342 mService.mTaskChangeNotificationController.notifyTaskCreated(_taskId, realActivity);
Craig Mautner21d24a22014-04-23 11:45:37 -0700343 }
344
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700345 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200346 TaskDescription _taskDescription) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700347 mService = service;
348 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
349 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800350 userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700351 taskId = _taskId;
Winson Chungfb44d212017-10-04 11:39:10 -0700352 lastActiveTime = SystemClock.elapsedRealtime();
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700353 mAffiliatedTaskId = _taskId;
354 voiceSession = null;
355 voiceInteractor = null;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700356 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800357 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700358 mCallingUid = info.applicationInfo.uid;
359 mCallingPackage = info.packageName;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700360 setIntent(_intent, info);
Andrii Kulian2e751b82016-03-16 16:59:32 -0700361 setMinDimensions(info);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700362
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700363 isPersistable = true;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700364 // Clamp to [1, max].
365 maxRecents = Math.min(Math.max(info.maxRecents, 1),
366 ActivityManager.getMaxAppRecentsLimitStatic());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700367
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700368 mTaskToReturnTo = ACTIVITY_TYPE_HOME;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700369 lastTaskDescription = _taskDescription;
Winson730bf062016-03-31 18:04:56 -0700370 touchActiveTime();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700371 mService.mTaskChangeNotificationController.notifyTaskCreated(_taskId, realActivity);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700372 }
373
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800374 private TaskRecord(ActivityManagerService service, int _taskId, Intent _intent,
375 Intent _affinityIntent, String _affinity, String _rootAffinity,
376 ComponentName _realActivity, ComponentName _origActivity, boolean _rootWasReset,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700377 boolean _autoRemoveRecents, boolean _askedCompatMode, int _userId,
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800378 int _effectiveUid, String _lastDescription, ArrayList<ActivityRecord> activities,
Winson Chungfb44d212017-10-04 11:39:10 -0700379 long lastTimeMoved, boolean neverRelinquishIdentity,
380 TaskDescription _lastTaskDescription, int taskAffiliation, int prevTaskId,
381 int nextTaskId, int taskAffiliationColor, int callingUid, String callingPackage,
Charles He2bf28322017-10-12 22:24:49 +0100382 int resizeMode, boolean supportsPictureInPicture, boolean _realActivitySuspended,
383 boolean userSetupComplete, int minWidth, int minHeight) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700384 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700385 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
386 TaskPersister.IMAGE_EXTENSION;
Craig Mautner21d24a22014-04-23 11:45:37 -0700387 taskId = _taskId;
388 intent = _intent;
389 affinityIntent = _affinityIntent;
390 affinity = _affinity;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800391 rootAffinity = _rootAffinity;
Craig Mautner21d24a22014-04-23 11:45:37 -0700392 voiceSession = null;
393 voiceInteractor = null;
394 realActivity = _realActivity;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800395 realActivitySuspended = _realActivitySuspended;
Craig Mautner21d24a22014-04-23 11:45:37 -0700396 origActivity = _origActivity;
397 rootWasReset = _rootWasReset;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700398 isAvailable = true;
Dianne Hackborn13420f22014-07-18 15:43:56 -0700399 autoRemoveRecents = _autoRemoveRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700400 askedCompatMode = _askedCompatMode;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700401 mTaskToReturnTo = ACTIVITY_TYPE_HOME;
Craig Mautner21d24a22014-04-23 11:45:37 -0700402 userId = _userId;
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800403 mUserSetupComplete = userSetupComplete;
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700404 effectiveUid = _effectiveUid;
Winson Chungfb44d212017-10-04 11:39:10 -0700405 lastActiveTime = SystemClock.elapsedRealtime();
Craig Mautner21d24a22014-04-23 11:45:37 -0700406 lastDescription = _lastDescription;
407 mActivities = activities;
408 mLastTimeMoved = lastTimeMoved;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700409 mNeverRelinquishIdentity = neverRelinquishIdentity;
Winson Chung2cb86c72014-06-25 12:03:30 -0700410 lastTaskDescription = _lastTaskDescription;
Craig Mautnera228ae92014-07-09 05:44:55 -0700411 mAffiliatedTaskId = taskAffiliation;
Winson Chungec396d62014-08-06 17:08:00 -0700412 mAffiliatedTaskColor = taskAffiliationColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700413 mPrevAffiliateTaskId = prevTaskId;
414 mNextAffiliateTaskId = nextTaskId;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700415 mCallingUid = callingUid;
416 mCallingPackage = callingPackage;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800417 mResizeMode = resizeMode;
Winson Chungd3395382016-12-13 11:49:09 -0800418 mSupportsPictureInPicture = supportsPictureInPicture;
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700419 mMinWidth = minWidth;
420 mMinHeight = minHeight;
Yorke Leebd54c2a2016-10-25 13:49:23 -0700421 mService.mTaskChangeNotificationController.notifyTaskCreated(_taskId, realActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 }
423
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800424 TaskWindowContainerController getWindowContainerController() {
425 return mWindowContainerController;
426 }
427
428 void createWindowContainer(boolean onTop, boolean showForAllUsers) {
429 if (mWindowContainerController != null) {
430 throw new IllegalArgumentException("Window container=" + mWindowContainerController
431 + " already created for task=" + this);
432 }
433
434 final Rect bounds = updateOverrideConfigurationFromLaunchBounds();
Bryce Lee04ab3462017-04-10 15:06:33 -0700435 setWindowContainerController(new TaskWindowContainerController(taskId, this,
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700436 getStack().getWindowContainerController(), userId, bounds,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700437 mResizeMode, mSupportsPictureInPicture, onTop,
438 showForAllUsers, lastTaskDescription));
Bryce Lee04ab3462017-04-10 15:06:33 -0700439 }
440
441 /**
442 * Should only be invoked from {@link #createWindowContainer(boolean, boolean)}.
443 */
444 @VisibleForTesting
445 protected void setWindowContainerController(TaskWindowContainerController controller) {
446 if (mWindowContainerController != null) {
447 throw new IllegalArgumentException("Window container=" + mWindowContainerController
448 + " already created for task=" + this);
449 }
450
451 mWindowContainerController = controller;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800452 }
453
454 void removeWindowContainer() {
Benjamin Franza83859f2017-07-03 16:34:14 +0100455 mService.mLockTaskController.removeLockedTask(this);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800456 mWindowContainerController.removeContainer();
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700457 if (!getWindowConfiguration().persistTaskBounds()) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800458 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
459 // default configuration the next time it launches.
460 updateOverrideConfiguration(null);
461 }
462 mService.mTaskChangeNotificationController.notifyTaskRemoved(taskId);
463 mWindowContainerController = null;
464 }
465
Jorim Jaggifb9d78a2017-01-05 18:57:12 +0100466 @Override
467 public void onSnapshotChanged(TaskSnapshot snapshot) {
468 mService.mTaskChangeNotificationController.notifyTaskSnapshotChanged(taskId, snapshot);
469 }
470
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800471 void setResizeMode(int resizeMode) {
472 if (mResizeMode == resizeMode) {
473 return;
474 }
475 mResizeMode = resizeMode;
476 mWindowContainerController.setResizeable(resizeMode);
477 mService.mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
478 mService.mStackSupervisor.resumeFocusedStackTopActivityLocked();
479 }
480
481 void setTaskDockedResizing(boolean resizing) {
482 mWindowContainerController.setTaskDockedResizing(resizing);
483 }
484
Wale Ogunwale1666e312016-12-16 11:27:18 -0800485 // TODO: Consolidate this with the resize() method below.
486 @Override
487 public void requestResize(Rect bounds, int resizeMode) {
488 mService.resizeTask(taskId, bounds, resizeMode);
489 }
490
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800491 boolean resize(Rect bounds, int resizeMode, boolean preserveWindow, boolean deferResume) {
492 if (!isResizeable()) {
493 Slog.w(TAG, "resizeTask: task " + this + " not resizeable.");
494 return true;
495 }
496
497 // If this is a forced resize, let it go through even if the bounds is not changing,
498 // as we might need a relayout due to surface size change (to/from fullscreen).
499 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
500 if (Objects.equals(mBounds, bounds) && !forced) {
501 // Nothing to do here...
502 return true;
503 }
504 bounds = validateBounds(bounds);
505
506 if (mWindowContainerController == null) {
507 // Task doesn't exist in window manager yet (e.g. was restored from recents).
508 // All we can do for now is update the bounds so it can be used when the task is
509 // added to window manager.
510 updateOverrideConfiguration(bounds);
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700511 if (!inFreeformWindowingMode()) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800512 // re-restore the task so it can have the proper stack association.
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700513 mService.mStackSupervisor.restoreRecentTaskLocked(this, null, !ON_TOP);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800514 }
515 return true;
516 }
517
518 if (!canResizeToBounds(bounds)) {
519 throw new IllegalArgumentException("resizeTask: Can not resize task=" + this
520 + " to bounds=" + bounds + " resizeMode=" + mResizeMode);
521 }
522
523 // Do not move the task to another stack here.
524 // This method assumes that the task is already placed in the right stack.
525 // we do not mess with that decision and we only do the resize!
526
527 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + taskId);
528
529 final boolean updatedConfig = updateOverrideConfiguration(bounds);
530 // This variable holds information whether the configuration didn't change in a significant
531 // way and the activity was kept the way it was. If it's false, it means the activity had
532 // to be relaunched due to configuration change.
533 boolean kept = true;
534 if (updatedConfig) {
535 final ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaleea3d3fd2017-05-01 07:41:08 -0700536 if (r != null && !deferResume) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800537 kept = r.ensureActivityConfigurationLocked(0 /* globalChanges */, preserveWindow);
Wale Ogunwaleea3d3fd2017-05-01 07:41:08 -0700538 mService.mStackSupervisor.ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
539 if (!kept) {
540 mService.mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800541 }
542 }
543 }
544 mWindowContainerController.resize(mBounds, getOverrideConfiguration(), kept, forced);
545
546 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
547 return kept;
548 }
549
550 // TODO: Investigate combining with the resize() method above.
551 void resizeWindowContainer() {
552 mWindowContainerController.resize(mBounds, getOverrideConfiguration(), false /* relayout */,
553 false /* forced */);
554 }
555
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800556 void getWindowContainerBounds(Rect bounds) {
557 mWindowContainerController.getBounds(bounds);
558 }
559
Winson Chung74666102017-02-22 17:49:24 -0800560 /**
561 * Convenience method to reparent a task to the top or bottom position of the stack.
562 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700563 boolean reparent(ActivityStack preferredStack, boolean toTop,
564 @ReparentMoveStackMode int moveStackMode, boolean animate, boolean deferResume,
565 String reason) {
566 return reparent(preferredStack, toTop ? MAX_VALUE : 0, moveStackMode, animate, deferResume,
567 true /* schedulePictureInPictureModeChange */, reason);
Winson Chung5af42fc2017-03-24 17:11:33 -0700568 }
569
570 /**
571 * Convenience method to reparent a task to the top or bottom position of the stack, with
572 * an option to skip scheduling the picture-in-picture mode change.
573 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700574 boolean reparent(ActivityStack preferredStack, boolean toTop,
575 @ReparentMoveStackMode int moveStackMode, boolean animate, boolean deferResume,
576 boolean schedulePictureInPictureModeChange, String reason) {
577 return reparent(preferredStack, toTop ? MAX_VALUE : 0, moveStackMode, animate,
Winson Chung5af42fc2017-03-24 17:11:33 -0700578 deferResume, schedulePictureInPictureModeChange, reason);
579 }
580
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700581 /** Convenience method to reparent a task to a specific position of the stack. */
582 boolean reparent(ActivityStack preferredStack, int position,
583 @ReparentMoveStackMode int moveStackMode, boolean animate, boolean deferResume,
584 String reason) {
585 return reparent(preferredStack, position, moveStackMode, animate, deferResume,
Winson Chung5af42fc2017-03-24 17:11:33 -0700586 true /* schedulePictureInPictureModeChange */, reason);
Winson Chung74666102017-02-22 17:49:24 -0800587 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800588
Winson Chung74666102017-02-22 17:49:24 -0800589 /**
590 * Reparents the task into a preferred stack, creating it if necessary.
591 *
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700592 * @param preferredStack the target stack to move this task
Winson Chung74666102017-02-22 17:49:24 -0800593 * @param position the position to place this task in the new stack
594 * @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 -0700595 * reparenting to be drawn and animated in
Winson Chung74666102017-02-22 17:49:24 -0800596 * @param moveStackMode whether or not to move the stack to the front always, only if it was
Winson Chung5af42fc2017-03-24 17:11:33 -0700597 * previously focused & in front, or never
Winson Chung74666102017-02-22 17:49:24 -0800598 * @param deferResume whether or not to update the visibility of other tasks and stacks that may
Winson Chung5af42fc2017-03-24 17:11:33 -0700599 * have changed as a result of this reparenting
600 * @param schedulePictureInPictureModeChange specifies whether or not to schedule the PiP mode
601 * change. Callers may set this to false if they are explicitly scheduling PiP mode
602 * changes themselves, like during the PiP animation
Winson Chung74666102017-02-22 17:49:24 -0800603 * @param reason the caller of this reparenting
Winson Chung5af42fc2017-03-24 17:11:33 -0700604 * @return whether the task was reparented
Winson Chung74666102017-02-22 17:49:24 -0800605 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700606 // TODO: Inspect all call sites and change to just changing windowing mode of the stack vs.
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700607 // re-parenting the task. Can only be done when we are no longer using static stack Ids.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700608 boolean reparent(ActivityStack preferredStack, int position,
609 @ReparentMoveStackMode int moveStackMode, boolean animate, boolean deferResume,
610 boolean schedulePictureInPictureModeChange, String reason) {
Winson Chung74666102017-02-22 17:49:24 -0800611 final ActivityStackSupervisor supervisor = mService.mStackSupervisor;
612 final WindowManagerService windowManager = mService.mWindowManager;
613 final ActivityStack sourceStack = getStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700614 final ActivityStack toStack = supervisor.getReparentTargetStack(this, preferredStack,
Winson Chung74666102017-02-22 17:49:24 -0800615 position == MAX_VALUE);
616 if (toStack == sourceStack) {
617 return false;
618 }
619
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700620 final int toStackWindowingMode = toStack.getWindowingMode();
Winson Chung74666102017-02-22 17:49:24 -0800621 final ActivityRecord topActivity = getTopActivity();
622
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700623 final boolean mightReplaceWindow =
624 replaceWindowsOnTaskMove(getWindowingMode(), toStackWindowingMode)
625 && topActivity != null;
Winson Chung74666102017-02-22 17:49:24 -0800626 if (mightReplaceWindow) {
627 // We are about to relaunch the activity because its configuration changed due to
628 // being maximized, i.e. size change. The activity will first remove the old window
629 // and then add a new one. This call will tell window manager about this, so it can
630 // preserve the old window until the new one is drawn. This prevents having a gap
631 // between the removal and addition, in which no window is visible. We also want the
632 // entrance of the new window to be properly animated.
633 // Note here we always set the replacing window first, as the flags might be needed
634 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
635 windowManager.setWillReplaceWindow(topActivity.appToken, animate);
636 }
637
638 windowManager.deferSurfaceLayout();
639 boolean kept = true;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800640 try {
Winson Chung74666102017-02-22 17:49:24 -0800641 final ActivityRecord r = topRunningActivityLocked();
Winson Chung95f8f0e2017-03-24 09:20:17 -0700642 final boolean wasFocused = r != null && supervisor.isFocusedStack(sourceStack)
Winson Chung74666102017-02-22 17:49:24 -0800643 && (topRunningActivityLocked() == r);
Winson Chung95f8f0e2017-03-24 09:20:17 -0700644 final boolean wasResumed = r != null && sourceStack.mResumedActivity == r;
645 final boolean wasPaused = r != null && sourceStack.mPausingActivity == r;
Winson Chung74666102017-02-22 17:49:24 -0800646
647 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
648 // Whenever we are moving the top activity from the front stack we want to make sure to
649 // move the stack to the front.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700650 final boolean wasFront = r != null && sourceStack.isTopStackOnDisplay()
Winson Chung74666102017-02-22 17:49:24 -0800651 && (sourceStack.topRunningActivityLocked() == r);
652
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800653 // Adjust the position for the new parent stack as needed.
Winson Chung74666102017-02-22 17:49:24 -0800654 position = toStack.getAdjustedPositionForTask(this, position, null /* starting */);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800655
656 // Must reparent first in window manager to avoid a situation where AM can delete the
657 // we are coming from in WM before we reparent because it became empty.
Wale Ogunwale2719cc12017-04-14 09:45:27 -0700658 mWindowContainerController.reparent(toStack.getWindowContainerController(), position,
659 moveStackMode == REPARENT_MOVE_STACK_TO_FRONT);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800660
Wale Ogunwale56d8d162017-05-30 11:12:20 -0700661 final boolean moveStackToFront = moveStackMode == REPARENT_MOVE_STACK_TO_FRONT
662 || (moveStackMode == REPARENT_KEEP_STACK_AT_FRONT && (wasFocused || wasFront));
Winson Chung74666102017-02-22 17:49:24 -0800663 // Move the task
Wale Ogunwale56d8d162017-05-30 11:12:20 -0700664 sourceStack.removeTask(this, reason, moveStackToFront
665 ? REMOVE_TASK_MODE_MOVING_TO_TOP : REMOVE_TASK_MODE_MOVING);
Winson Chung5af42fc2017-03-24 17:11:33 -0700666 toStack.addTask(this, position, false /* schedulePictureInPictureModeChange */, reason);
Winson Chung74666102017-02-22 17:49:24 -0800667
Winson Chung5af42fc2017-03-24 17:11:33 -0700668 if (schedulePictureInPictureModeChange) {
669 // Notify of picture-in-picture mode changes
670 supervisor.scheduleUpdatePictureInPictureModeIfNeeded(this, sourceStack);
671 }
Winson Chung74666102017-02-22 17:49:24 -0800672
673 // TODO: Ensure that this is actually necessary here
674 // Notify the voice session if required
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800675 if (voiceSession != null) {
676 try {
677 voiceSession.taskStarted(intent, taskId);
678 } catch (RemoteException e) {
679 }
680 }
Winson Chung74666102017-02-22 17:49:24 -0800681
682 // If the task had focus before (or we're requested to move focus), move focus to the
683 // new stack by moving the stack to the front.
Winson Chung95f8f0e2017-03-24 09:20:17 -0700684 if (r != null) {
685 toStack.moveToFrontAndResumeStateIfNeeded(r, moveStackToFront, wasResumed,
686 wasPaused, reason);
687 }
Winson Chung74666102017-02-22 17:49:24 -0800688 if (!animate) {
689 toStack.mNoAnimActivities.add(topActivity);
690 }
691
692 // We might trigger a configuration change. Save the current task bounds for freezing.
693 // TODO: Should this call be moved inside the resize method in WM?
694 toStack.prepareFreezingTaskBounds();
695
696 // Make sure the task has the appropriate bounds/size for the stack it is in.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700697 final boolean toStackSplitScreenPrimary =
698 toStackWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700699 if ((toStackWindowingMode == WINDOWING_MODE_FULLSCREEN
700 || toStackWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY)
Winson Chung74666102017-02-22 17:49:24 -0800701 && !Objects.equals(mBounds, toStack.mBounds)) {
702 kept = resize(toStack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow,
703 deferResume);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700704 } else if (toStackWindowingMode == WINDOWING_MODE_FREEFORM) {
Winson Chung74666102017-02-22 17:49:24 -0800705 Rect bounds = getLaunchBounds();
706 if (bounds == null) {
707 toStack.layoutTaskInStack(this, null);
708 bounds = mBounds;
709 }
710 kept = resize(bounds, RESIZE_MODE_FORCED, !mightReplaceWindow, deferResume);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700711 } else if (toStackSplitScreenPrimary || toStackWindowingMode == WINDOWING_MODE_PINNED) {
712 if (toStackSplitScreenPrimary && moveStackMode == REPARENT_KEEP_STACK_AT_FRONT) {
Matthew Ng330757d2017-02-28 14:19:17 -0800713 // Move recents to front so it is not behind home stack when going into docked
714 // mode
715 mService.mStackSupervisor.moveRecentsStackToFront(reason);
716 }
Winson Chung74666102017-02-22 17:49:24 -0800717 kept = resize(toStack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow,
718 deferResume);
719 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800720 } finally {
Winson Chung74666102017-02-22 17:49:24 -0800721 windowManager.continueSurfaceLayout();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800722 }
Winson Chung74666102017-02-22 17:49:24 -0800723
724 if (mightReplaceWindow) {
725 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
726 // window), we need to clear the replace window settings. Otherwise, we schedule a
727 // timeout to remove the old window if the replacing window is not coming in time.
728 windowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
729 }
730
731 if (!deferResume) {
732 // The task might have already been running and its visibility needs to be synchronized
733 // with the visibility of the stack / windows.
734 supervisor.ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
735 supervisor.resumeFocusedStackTopActivityLocked();
736 }
737
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700738 // TODO: Handle incorrect request to move before the actual move, not after.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700739 final boolean inSplitScreenMode = supervisor.getDefaultDisplay().hasSplitScreenPrimaryStack();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700740 supervisor.handleNonResizableTaskIfNeeded(this, preferredStack.getWindowingMode(),
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700741 DEFAULT_DISPLAY, toStack);
Winson Chung74666102017-02-22 17:49:24 -0800742
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700743 boolean successful = (preferredStack == toStack);
744 if (successful && toStack.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
Matthew Ng330757d2017-02-28 14:19:17 -0800745 // If task moved to docked stack - show recents if needed.
746 mService.mWindowManager.showRecentApps(false /* fromHome */);
747 }
748 return successful;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800749 }
750
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700751 /**
752 * Returns true if the windows of tasks being moved to the target stack from the source
753 * stack should be replaced, meaning that window manager will keep the old window around
754 * until the new is ready.
755 * @hide
756 */
757 private static boolean replaceWindowsOnTaskMove(
758 int sourceWindowingMode, int targetWindowingMode) {
759 return sourceWindowingMode == WINDOWING_MODE_FREEFORM
760 || targetWindowingMode == WINDOWING_MODE_FREEFORM;
761 }
762
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800763 void cancelWindowTransition() {
764 mWindowContainerController.cancelWindowTransition();
765 }
766
767 void cancelThumbnailTransition() {
768 mWindowContainerController.cancelThumbnailTransition();
769 }
770
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100771 /**
772 * DO NOT HOLD THE ACTIVITY MANAGER LOCK WHEN CALLING THIS METHOD!
773 */
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100774 TaskSnapshot getSnapshot(boolean reducedResolution) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100775
776 // TODO: Move this to {@link TaskWindowContainerController} once recent tasks are more
777 // synchronized between AM and WM.
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100778 return mService.mWindowManager.getTaskSnapshot(taskId, userId, reducedResolution);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800779 }
780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 void touchActiveTime() {
Winson Chungfb44d212017-10-04 11:39:10 -0700782 lastActiveTime = SystemClock.elapsedRealtime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 long getInactiveDuration() {
Winson Chungfb44d212017-10-04 11:39:10 -0700786 return SystemClock.elapsedRealtime() - lastActiveTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700788
Winson Chungfee26772014-08-05 12:21:52 -0700789 /** Sets the original intent, and the calling uid and package. */
790 void setIntent(ActivityRecord r) {
Winson Chungfee26772014-08-05 12:21:52 -0700791 mCallingUid = r.launchedFromUid;
792 mCallingPackage = r.launchedFromPackage;
Craig Mautner15df08a2015-04-01 12:17:18 -0700793 setIntent(r.intent, r.info);
Charles He2bf28322017-10-12 22:24:49 +0100794 setLockTaskAuth(r);
Winson Chungfee26772014-08-05 12:21:52 -0700795 }
796
797 /** Sets the original intent, _without_ updating the calling uid or package. */
798 private void setIntent(Intent _intent, ActivityInfo info) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700799 if (intent == null) {
800 mNeverRelinquishIdentity =
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700801 (info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700802 } else if (mNeverRelinquishIdentity) {
803 return;
804 }
805
806 affinity = info.taskAffinity;
Dianne Hackborn79228822014-09-16 11:11:23 -0700807 if (intent == null) {
808 // If this task already has an intent associated with it, don't set the root
809 // affinity -- we don't want it changing after initially set, but the initially
810 // set value may be null.
811 rootAffinity = affinity;
812 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700813 effectiveUid = info.applicationInfo.uid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700814 stringName = null;
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 if (info.targetActivity == null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800817 if (_intent != null) {
818 // If this Intent has a selector, we want to clear it for the
819 // recent task since it is not relevant if the user later wants
820 // to re-launch the app.
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700821 if (_intent.getSelector() != null || _intent.getSourceBounds() != null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800822 _intent = new Intent(_intent);
823 _intent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700824 _intent.setSourceBounds(null);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800825 }
826 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700827 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Setting Intent of " + this + " to " + _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 intent = _intent;
829 realActivity = _intent != null ? _intent.getComponent() : null;
830 origActivity = null;
831 } else {
832 ComponentName targetComponent = new ComponentName(
833 info.packageName, info.targetActivity);
834 if (_intent != null) {
835 Intent targetIntent = new Intent(_intent);
836 targetIntent.setComponent(targetComponent);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800837 targetIntent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700838 targetIntent.setSourceBounds(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700839 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700840 "Setting Intent of " + this + " to target " + targetIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 intent = targetIntent;
842 realActivity = targetComponent;
843 origActivity = _intent.getComponent();
844 } else {
845 intent = null;
846 realActivity = targetComponent;
847 origActivity = new ComponentName(info.packageName, info.name);
848 }
849 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700850
Craig Mautner47b20ba2014-09-17 17:23:44 -0700851 final int intentFlags = intent == null ? 0 : intent.getFlags();
852 if ((intentFlags & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 // Once we are set to an Intent with this flag, we count this
854 // task as having a true root activity.
855 rootWasReset = true;
856 }
Dianne Hackborn09233282014-04-30 11:33:59 -0700857 userId = UserHandle.getUserId(info.applicationInfo.uid);
Winson Chung36f3f032016-09-08 23:29:43 +0000858 mUserSetupComplete = Settings.Secure.getIntForUser(mService.mContext.getContentResolver(),
859 USER_SETUP_COMPLETE, 0, userId) != 0;
Craig Mautner41db4a72014-05-07 17:20:56 -0700860 if ((info.flags & ActivityInfo.FLAG_AUTO_REMOVE_FROM_RECENTS) != 0) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700861 // If the activity itself has requested auto-remove, then just always do it.
862 autoRemoveRecents = true;
Wale Ogunwale843bfb92015-03-27 11:06:48 -0700863 } else if ((intentFlags & (FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_RETAIN_IN_RECENTS))
864 == FLAG_ACTIVITY_NEW_DOCUMENT) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700865 // If the caller has not asked for the document to be retained, then we may
866 // want to turn on auto-remove, depending on whether the target has set its
867 // own document launch mode.
868 if (info.documentLaunchMode != ActivityInfo.DOCUMENT_LAUNCH_NONE) {
869 autoRemoveRecents = false;
870 } else {
871 autoRemoveRecents = true;
872 }
873 } else {
874 autoRemoveRecents = false;
Craig Mautner41db4a72014-05-07 17:20:56 -0700875 }
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800876 mResizeMode = info.resizeMode;
Winson Chungd3395382016-12-13 11:49:09 -0800877 mSupportsPictureInPicture = info.supportsPictureInPicture();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800879
Andrii Kulian2e751b82016-03-16 16:59:32 -0700880 /** Sets the original minimal width and height. */
881 private void setMinDimensions(ActivityInfo info) {
882 if (info != null && info.windowLayout != null) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700883 mMinWidth = info.windowLayout.minWidth;
884 mMinHeight = info.windowLayout.minHeight;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700885 } else {
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700886 mMinWidth = INVALID_MIN_SIZE;
887 mMinHeight = INVALID_MIN_SIZE;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700888 }
889 }
890
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800891 /**
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700892 * Return true if the input activity has the same intent filter as the intent this task
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800893 * record is based on (normally the root activity intent).
894 */
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700895 boolean isSameIntentFilter(ActivityRecord r) {
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800896 final Intent intent = new Intent(r.intent);
897 // Correct the activity intent for aliasing. The task record intent will always be based on
898 // the real activity that will be launched not the alias, so we need to use an intent with
899 // the component name pointing to the real activity not the alias in the activity record.
900 intent.setComponent(r.realActivity);
901 return this.intent.filterEquals(intent);
902 }
903
Wale Ogunwaleeea34ee92017-08-31 20:07:45 -0700904 void setTaskToReturnTo(/*@WindowConfiguration.ActivityType*/ int taskToReturnTo) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700905 mTaskToReturnTo = taskToReturnTo == ACTIVITY_TYPE_RECENTS
906 ? ACTIVITY_TYPE_HOME : taskToReturnTo;
Craig Mautner84984fa2014-06-19 11:19:20 -0700907 }
908
Winson Chung83471632016-12-13 11:02:12 -0800909 void setTaskToReturnTo(ActivityRecord source) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700910 if (source.isActivityTypeRecents()) {
911 setTaskToReturnTo(ACTIVITY_TYPE_RECENTS);
912 } else if (source.isActivityTypeAssistant()) {
913 setTaskToReturnTo(ACTIVITY_TYPE_ASSISTANT);
Winson Chung83471632016-12-13 11:02:12 -0800914 }
915 }
916
Craig Mautner84984fa2014-06-19 11:19:20 -0700917 int getTaskToReturnTo() {
918 return mTaskToReturnTo;
919 }
920
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700921 boolean returnsToHomeTask() {
922 return mTaskToReturnTo == ACTIVITY_TYPE_HOME;
923 }
924
925 boolean returnsToStandardTask() {
926 return mTaskToReturnTo == ACTIVITY_TYPE_STANDARD;
927 }
928
Craig Mautnera228ae92014-07-09 05:44:55 -0700929 void setPrevAffiliate(TaskRecord prevAffiliate) {
930 mPrevAffiliate = prevAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800931 mPrevAffiliateTaskId = prevAffiliate == null ? INVALID_TASK_ID : prevAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700932 }
933
934 void setNextAffiliate(TaskRecord nextAffiliate) {
935 mNextAffiliate = nextAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800936 mNextAffiliateTaskId = nextAffiliate == null ? INVALID_TASK_ID : nextAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700937 }
938
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700939 <T extends ActivityStack> T getStack() {
940 return (T) mStack;
Andrii Kulian02b7a832016-10-06 23:11:56 -0700941 }
942
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800943 /**
944 * Must be used for setting parent stack because it performs configuration updates.
945 * Must be called after adding task as a child to the stack.
946 */
Andrii Kulian02b7a832016-10-06 23:11:56 -0700947 void setStack(ActivityStack stack) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800948 if (stack != null && !stack.isInStackLocked(this)) {
949 throw new IllegalStateException("Task must be added as a Stack child first.");
950 }
Andrii Kulian02b7a832016-10-06 23:11:56 -0700951 mStack = stack;
Andrii Kulian1779e612016-10-12 21:58:25 -0700952 onParentChanged();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700953 }
954
955 /**
956 * @return Id of current stack, {@link INVALID_STACK_ID} if no stack is set.
957 */
958 int getStackId() {
959 return mStack != null ? mStack.mStackId : INVALID_STACK_ID;
960 }
961
Andrii Kulian1779e612016-10-12 21:58:25 -0700962 @Override
963 protected int getChildCount() {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700964 return mActivities.size();
Andrii Kulian1779e612016-10-12 21:58:25 -0700965 }
966
967 @Override
968 protected ConfigurationContainer getChildAt(int index) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700969 return mActivities.get(index);
Andrii Kulian1779e612016-10-12 21:58:25 -0700970 }
971
972 @Override
973 protected ConfigurationContainer getParent() {
974 return mStack;
975 }
976
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800977 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -0700978 protected void onParentChanged() {
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800979 super.onParentChanged();
980 mService.mStackSupervisor.updateUIDsPresentOnDisplay();
981 }
982
Craig Mautnera228ae92014-07-09 05:44:55 -0700983 // Close up recents linked list.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700984 private void closeRecentsChain() {
Craig Mautnera228ae92014-07-09 05:44:55 -0700985 if (mPrevAffiliate != null) {
986 mPrevAffiliate.setNextAffiliate(mNextAffiliate);
987 }
988 if (mNextAffiliate != null) {
989 mNextAffiliate.setPrevAffiliate(mPrevAffiliate);
990 }
991 setPrevAffiliate(null);
992 setNextAffiliate(null);
993 }
994
Winson Chung740c3ac2014-11-12 16:14:38 -0800995 void removedFromRecents() {
Dianne Hackborn852975d2014-08-22 17:42:43 -0700996 closeRecentsChain();
997 if (inRecents) {
998 inRecents = false;
Winson Chung740c3ac2014-11-12 16:14:38 -0800999 mService.notifyTaskPersisterLocked(this, false);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001000 }
Jorim Jaggif9084ec2017-01-16 13:16:59 +01001001
1002 // TODO: Use window container controller once tasks are better synced between AM and WM
1003 mService.mWindowManager.notifyTaskRemovedFromRecents(taskId, userId);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001004 }
1005
Craig Mautnera228ae92014-07-09 05:44:55 -07001006 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
1007 closeRecentsChain();
1008 mAffiliatedTaskId = taskToAffiliateWith.mAffiliatedTaskId;
Winson Chungec396d62014-08-06 17:08:00 -07001009 mAffiliatedTaskColor = taskToAffiliateWith.mAffiliatedTaskColor;
Craig Mautnera228ae92014-07-09 05:44:55 -07001010 // Find the end
1011 while (taskToAffiliateWith.mNextAffiliate != null) {
1012 final TaskRecord nextRecents = taskToAffiliateWith.mNextAffiliate;
1013 if (nextRecents.mAffiliatedTaskId != mAffiliatedTaskId) {
1014 Slog.e(TAG, "setTaskToAffiliateWith: nextRecents=" + nextRecents + " affilTaskId="
1015 + nextRecents.mAffiliatedTaskId + " should be " + mAffiliatedTaskId);
1016 if (nextRecents.mPrevAffiliate == taskToAffiliateWith) {
1017 nextRecents.setPrevAffiliate(null);
1018 }
1019 taskToAffiliateWith.setNextAffiliate(null);
1020 break;
1021 }
1022 taskToAffiliateWith = nextRecents;
1023 }
1024 taskToAffiliateWith.setNextAffiliate(this);
1025 setPrevAffiliate(taskToAffiliateWith);
1026 setNextAffiliate(null);
1027 }
1028
Winson Chung1147c402014-05-14 11:05:00 -07001029 /** Returns the intent for the root activity for this task */
1030 Intent getBaseIntent() {
1031 return intent != null ? intent : affinityIntent;
1032 }
1033
Winson Chung3b3f4642014-04-22 10:08:18 -07001034 /** Returns the first non-finishing activity from the root. */
1035 ActivityRecord getRootActivity() {
1036 for (int i = 0; i < mActivities.size(); i++) {
1037 final ActivityRecord r = mActivities.get(i);
1038 if (r.finishing) {
1039 continue;
1040 }
1041 return r;
1042 }
1043 return null;
1044 }
1045
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001046 ActivityRecord getTopActivity() {
Bryce Lee9f6affd2017-09-01 09:18:35 -07001047 return getTopActivity(true /* includeOverlays */);
1048 }
1049
1050 ActivityRecord getTopActivity(boolean includeOverlays) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001051 for (int i = mActivities.size() - 1; i >= 0; --i) {
1052 final ActivityRecord r = mActivities.get(i);
Bryce Lee9f6affd2017-09-01 09:18:35 -07001053 if (r.finishing || (!includeOverlays && r.mTaskOverlay)) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001054 continue;
1055 }
1056 return r;
1057 }
1058 return null;
1059 }
1060
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001061 ActivityRecord topRunningActivityLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001062 if (mStack != null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001063 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1064 ActivityRecord r = mActivities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -07001065 if (!r.finishing && r.okToShowLocked()) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001066 return r;
1067 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001068 }
1069 }
1070 return null;
1071 }
1072
Jorim Jaggiea039a82017-08-02 14:37:49 +02001073 void getAllRunningVisibleActivitiesLocked(ArrayList<ActivityRecord> outActivities) {
1074 if (mStack != null) {
1075 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1076 ActivityRecord r = mActivities.get(activityNdx);
Jorim Jaggi02f1d2f2017-08-04 14:29:16 +02001077 if (!r.finishing && r.okToShowLocked() && r.visibleIgnoringKeyguard) {
Jorim Jaggiea039a82017-08-02 14:37:49 +02001078 outActivities.add(r);
1079 }
1080 }
1081 }
1082 }
1083
Wale Ogunwale3b232392016-05-13 15:37:13 -07001084 ActivityRecord topRunningActivityWithStartingWindowLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001085 if (mStack != null) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07001086 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1087 ActivityRecord r = mActivities.get(activityNdx);
1088 if (r.mStartingWindowState != STARTING_WINDOW_SHOWN
Chong Zhang87761972016-08-22 13:53:24 -07001089 || r.finishing || !r.okToShowLocked()) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07001090 continue;
1091 }
1092 return r;
1093 }
1094 }
1095 return null;
1096 }
1097
Chong Zhang87761972016-08-22 13:53:24 -07001098 boolean okToShowLocked() {
1099 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
1100 // okay to show the activity when locked.
1101 return mService.mStackSupervisor.isCurrentProfileLocked(userId)
1102 || topRunningActivityLocked() != null;
1103 }
1104
Craig Mautner3b475fe2013-12-16 15:58:31 -08001105 /** Call after activity movement or finish to make sure that frontOfTask is set correctly */
Bryce Leed71317c2017-02-07 14:27:22 -08001106 final void setFrontOfTask() {
1107 boolean foundFront = false;
Craig Mautner3b475fe2013-12-16 15:58:31 -08001108 final int numActivities = mActivities.size();
Craig Mautner704e40b2013-12-18 16:43:51 -08001109 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
Craig Mautner3b475fe2013-12-16 15:58:31 -08001110 final ActivityRecord r = mActivities.get(activityNdx);
1111 if (foundFront || r.finishing) {
1112 r.frontOfTask = false;
1113 } else {
1114 r.frontOfTask = true;
1115 // Set frontOfTask false for every following activity.
1116 foundFront = true;
1117 }
1118 }
Craig Mautner9587ee02014-06-23 15:00:10 +00001119 if (!foundFront && numActivities > 0) {
1120 // All activities of this task are finishing. As we ought to have a frontOfTask
1121 // activity, make the bottom activity front.
1122 mActivities.get(0).frontOfTask = true;
1123 }
Craig Mautner3b475fe2013-12-16 15:58:31 -08001124 }
1125
Craig Mautnerde4ef022013-04-07 19:01:33 -07001126 /**
Craig Mautner3b475fe2013-12-16 15:58:31 -08001127 * Reorder the history stack so that the passed activity is brought to the front.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001128 */
1129 final void moveActivityToFrontLocked(ActivityRecord newTop) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001130 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
1131 "Removing and adding activity " + newTop
1132 + " to stack at top callers=" + Debug.getCallers(4));
Craig Mautnerde4ef022013-04-07 19:01:33 -07001133
Craig Mautnerde4ef022013-04-07 19:01:33 -07001134 mActivities.remove(newTop);
1135 mActivities.add(newTop);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001136 updateEffectiveIntent();
Craig Mautner3b475fe2013-12-16 15:58:31 -08001137
Bryce Leed71317c2017-02-07 14:27:22 -08001138 setFrontOfTask();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001139 }
1140
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001141 void addActivityAtBottom(ActivityRecord r) {
Craig Mautner77878772013-03-04 19:46:24 -08001142 addActivityAtIndex(0, r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001143 }
1144
1145 void addActivityToTop(ActivityRecord r) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001146 addActivityAtIndex(mActivities.size(), r);
1147 }
1148
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001149 @Override
Wale Ogunwaleeea34ee92017-08-31 20:07:45 -07001150 /*@WindowConfiguration.ActivityType*/
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001151 public int getActivityType() {
1152 final int applicationType = super.getActivityType();
1153 if (applicationType != ACTIVITY_TYPE_UNDEFINED || mActivities.isEmpty()) {
1154 return applicationType;
1155 }
1156 return mActivities.get(0).getActivityType();
1157 }
1158
Winson Chung30480042017-01-26 10:55:34 -08001159 /**
1160 * Adds an activity {@param r} at the given {@param index}. The activity {@param r} must either
1161 * be in the current task or unparented to any task.
1162 */
Craig Mautner1602ec22013-05-12 10:24:27 -07001163 void addActivityAtIndex(int index, ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07001164 TaskRecord task = r.getTask();
1165 if (task != null && task != this) {
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001166 throw new IllegalArgumentException("Can not add r=" + " to task=" + this
Bryce Leeaf691c02017-03-20 14:20:22 -07001167 + " current parent=" + task);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001168 }
Bryce Leeaf691c02017-03-20 14:20:22 -07001169
1170 r.setTask(this);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001171
Craig Mautner6170f732013-04-02 13:05:23 -07001172 // 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 -08001173 if (!mActivities.remove(r) && r.fullscreen) {
1174 // Was not previously in list.
1175 numFullscreen++;
1176 }
Craig Mautner2c1faed2013-07-23 12:56:02 -07001177 // Only set this based on the first activity
1178 if (mActivities.isEmpty()) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001179 if (r.getActivityType() == ACTIVITY_TYPE_UNDEFINED) {
1180 // Normally non-standard activity type for the activity record will be set when the
1181 // object is created, however we delay setting the standard application type until
1182 // this point so that the task can set the type for additional activities added in
1183 // the else condition below.
1184 r.setActivityType(ACTIVITY_TYPE_STANDARD);
1185 }
1186 setActivityType(r.getActivityType());
Craig Mautner21d24a22014-04-23 11:45:37 -07001187 isPersistable = r.isPersistable();
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001188 mCallingUid = r.launchedFromUid;
1189 mCallingPackage = r.launchedFromPackage;
Dianne Hackborn852975d2014-08-22 17:42:43 -07001190 // Clamp to [1, max].
1191 maxRecents = Math.min(Math.max(r.info.maxRecents, 1),
1192 ActivityManager.getMaxAppRecentsLimitStatic());
Craig Mautner2c1faed2013-07-23 12:56:02 -07001193 } else {
1194 // Otherwise make all added activities match this one.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001195 r.setActivityType(getActivityType());
Craig Mautner78733002013-06-10 13:54:49 -07001196 }
Wale Ogunwale3b232392016-05-13 15:37:13 -07001197
1198 final int size = mActivities.size();
1199
1200 if (index == size && size > 0) {
1201 final ActivityRecord top = mActivities.get(size - 1);
1202 if (top.mTaskOverlay) {
1203 // Place below the task overlay activity since the overlay activity should always
1204 // be on top.
1205 index--;
1206 }
1207 }
1208
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001209 index = Math.min(size, index);
Craig Mautner77878772013-03-04 19:46:24 -08001210 mActivities.add(index, r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001211 updateEffectiveIntent();
Craig Mautner21d24a22014-04-23 11:45:37 -07001212 if (r.isPersistable()) {
1213 mService.notifyTaskPersisterLocked(this, false);
1214 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001215
1216 // Sync. with window manager
1217 updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001218 final AppWindowContainerController appController = r.getWindowContainerController();
1219 if (appController != null) {
1220 // Only attempt to move in WM if the child has a controller. It is possible we haven't
1221 // created controller for the activity we are starting yet.
1222 mWindowContainerController.positionChildAt(appController, index);
1223 }
David Stevens82ea6cb2017-03-03 16:18:50 -08001224
1225 // Make sure the list of display UID whitelists is updated
1226 // now that this record is in a new task.
1227 mService.mStackSupervisor.updateUIDsPresentOnDisplay();
Craig Mautner77878772013-03-04 19:46:24 -08001228 }
1229
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001230 /**
Bryce Leeaf691c02017-03-20 14:20:22 -07001231 * Removes the specified activity from this task.
1232 * @param r The {@link ActivityRecord} to remove.
1233 * @return true if this was the last activity in the task.
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001234 */
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001235 boolean removeActivity(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07001236 return removeActivity(r, false /*reparenting*/);
1237 }
1238
1239 boolean removeActivity(ActivityRecord r, boolean reparenting) {
1240 if (r.getTask() != this) {
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001241 throw new IllegalArgumentException(
1242 "Activity=" + r + " does not belong to task=" + this);
1243 }
1244
Bryce Leeaf691c02017-03-20 14:20:22 -07001245 r.setTask(null /*task*/, reparenting);
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001246
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001247 if (mActivities.remove(r) && r.fullscreen) {
1248 // Was previously in list.
1249 numFullscreen--;
1250 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001251 if (r.isPersistable()) {
1252 mService.notifyTaskPersisterLocked(this, false);
1253 }
Wale Ogunwale89182d52016-03-11 10:38:36 -08001254
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001255 if (inPinnedWindowingMode()) {
Wale Ogunwale89182d52016-03-11 10:38:36 -08001256 // We normally notify listeners of task stack changes on pause, however pinned stack
1257 // activities are normally in the paused state so no notification will be sent there
1258 // before the activity is removed. We send it here so instead.
Yorke Leebd54c2a2016-10-25 13:49:23 -07001259 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale89182d52016-03-11 10:38:36 -08001260 }
1261
Craig Mautner41326202014-06-20 14:38:21 -07001262 if (mActivities.isEmpty()) {
Craig Mautner5afcd4d2014-06-21 18:11:33 -07001263 return !mReuseTask;
Craig Mautner41326202014-06-20 14:38:21 -07001264 }
1265 updateEffectiveIntent();
1266 return false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001267 }
1268
Winson Chung6954fc92017-03-24 16:22:12 -07001269 /**
1270 * @return whether or not there are ONLY task overlay activities in the stack.
1271 * If {@param excludeFinishing} is set, then ignore finishing activities in the check.
1272 * If there are no task overlay activities, this call returns false.
1273 */
1274 boolean onlyHasTaskOverlayActivities(boolean excludeFinishing) {
1275 int count = 0;
1276 for (int i = mActivities.size() - 1; i >= 0; i--) {
1277 final ActivityRecord r = mActivities.get(i);
1278 if (excludeFinishing && r.finishing) {
1279 continue;
1280 }
1281 if (!r.mTaskOverlay) {
1282 return false;
1283 }
1284 count++;
1285 }
1286 return count > 0;
1287 }
1288
Craig Mautner41db4a72014-05-07 17:20:56 -07001289 boolean autoRemoveFromRecents() {
Dianne Hackbornd38aed82014-06-10 21:36:35 -07001290 // We will automatically remove the task either if it has explicitly asked for
1291 // this, or it is empty and has never contained an activity that got shown to
1292 // the user.
Dianne Hackborn13420f22014-07-18 15:43:56 -07001293 return autoRemoveRecents || (mActivities.isEmpty() && !hasBeenVisible);
Craig Mautner41db4a72014-05-07 17:20:56 -07001294 }
1295
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001296 /**
1297 * Completely remove all activities associated with an existing
1298 * task starting at a specified index.
1299 */
Winson Chung6954fc92017-03-24 16:22:12 -07001300 final void performClearTaskAtIndexLocked(int activityNdx, boolean pauseImmediately) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001301 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001302 for ( ; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001303 final ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001304 if (r.finishing) {
1305 continue;
1306 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001307 if (mStack == null) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001308 // Task was restored from persistent storage.
1309 r.takeFromHistory();
1310 mActivities.remove(activityNdx);
1311 --activityNdx;
1312 --numActivities;
Winson Chung6954fc92017-03-24 16:22:12 -07001313 } else if (mStack.finishActivityLocked(r, Activity.RESULT_CANCELED, null,
1314 "clear-task-index", false, pauseImmediately)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001315 --activityNdx;
1316 --numActivities;
1317 }
1318 }
1319 }
1320
1321 /**
1322 * Completely remove all activities associated with an existing task.
1323 */
Benjamin Franza83859f2017-07-03 16:34:14 +01001324 void performClearTaskLocked() {
Craig Mautner362449a2014-06-20 14:04:39 -07001325 mReuseTask = true;
Winson Chung6954fc92017-03-24 16:22:12 -07001326 performClearTaskAtIndexLocked(0, !PAUSE_IMMEDIATELY);
Craig Mautner362449a2014-06-20 14:04:39 -07001327 mReuseTask = false;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001328 }
1329
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -08001330 ActivityRecord performClearTaskForReuseLocked(ActivityRecord newR, int launchFlags) {
1331 mReuseTask = true;
1332 final ActivityRecord result = performClearTaskLocked(newR, launchFlags);
1333 mReuseTask = false;
1334 return result;
1335 }
1336
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001337 /**
1338 * Perform clear operation as requested by
1339 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
1340 * stack to the given task, then look for
1341 * an instance of that activity in the stack and, if found, finish all
1342 * activities on top of it and return the instance.
1343 *
1344 * @param newR Description of the new activity being started.
1345 * @return Returns the old activity that should be continued to be used,
1346 * or null if none was found.
1347 */
1348 final ActivityRecord performClearTaskLocked(ActivityRecord newR, int launchFlags) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001349 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001350 for (int activityNdx = numActivities - 1; activityNdx >= 0; --activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001351 ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001352 if (r.finishing) {
1353 continue;
1354 }
1355 if (r.realActivity.equals(newR.realActivity)) {
1356 // Here it is! Now finish everything in front...
Craig Mautner1602ec22013-05-12 10:24:27 -07001357 final ActivityRecord ret = r;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001358
1359 for (++activityNdx; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001360 r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001361 if (r.finishing) {
1362 continue;
1363 }
1364 ActivityOptions opts = r.takeOptionsLocked();
1365 if (opts != null) {
1366 ret.updateOptionsLocked(opts);
1367 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001368 if (mStack != null && mStack.finishActivityLocked(
Todd Kennedy539db512014-12-15 09:57:55 -08001369 r, Activity.RESULT_CANCELED, null, "clear-task-stack", false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001370 --activityNdx;
1371 --numActivities;
1372 }
1373 }
1374
1375 // Finally, if this is a normal launch mode (that is, not
1376 // expecting onNewIntent()), then we will finish the current
1377 // instance of the activity so a new fresh one can be started.
1378 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
Daichi Hirono15a02992016-04-27 18:47:01 +09001379 && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0
1380 && !ActivityStarter.isDocumentLaunchesIntoExisting(launchFlags)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001381 if (!ret.finishing) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001382 if (mStack != null) {
1383 mStack.finishActivityLocked(
Wale Ogunwale7d701172015-03-11 15:36:30 -07001384 ret, Activity.RESULT_CANCELED, null, "clear-task-top", false);
1385 }
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001386 return null;
1387 }
1388 }
1389
1390 return ret;
1391 }
1392 }
1393
1394 return null;
1395 }
1396
Winson Chung6954fc92017-03-24 16:22:12 -07001397 void removeTaskActivitiesLocked(boolean pauseImmediately) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001398 // Just remove the entire task.
Winson Chung6954fc92017-03-24 16:22:12 -07001399 performClearTaskAtIndexLocked(0, pauseImmediately);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07001400 }
1401
Craig Mautner432f64e2015-05-20 14:59:57 -07001402 String lockTaskAuthToString() {
1403 switch (mLockTaskAuth) {
1404 case LOCK_TASK_AUTH_DONT_LOCK: return "LOCK_TASK_AUTH_DONT_LOCK";
1405 case LOCK_TASK_AUTH_PINNABLE: return "LOCK_TASK_AUTH_PINNABLE";
1406 case LOCK_TASK_AUTH_LAUNCHABLE: return "LOCK_TASK_AUTH_LAUNCHABLE";
1407 case LOCK_TASK_AUTH_WHITELISTED: return "LOCK_TASK_AUTH_WHITELISTED";
Benjamin Franz469dd582015-06-09 14:24:36 +01001408 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV: return "LOCK_TASK_AUTH_LAUNCHABLE_PRIV";
Craig Mautner432f64e2015-05-20 14:59:57 -07001409 default: return "unknown=" + mLockTaskAuth;
1410 }
1411 }
1412
Craig Mautner15df08a2015-04-01 12:17:18 -07001413 void setLockTaskAuth() {
Charles He2bf28322017-10-12 22:24:49 +01001414 setLockTaskAuth(getRootActivity());
1415 }
1416
1417 private void setLockTaskAuth(@Nullable ActivityRecord r) {
1418 if (r == null) {
1419 mLockTaskAuth = LOCK_TASK_AUTH_PINNABLE;
1420 return;
1421 }
1422
Charles He520b2832017-09-02 15:27:16 +01001423 final String pkg = (realActivity != null) ? realActivity.getPackageName() : null;
Charles He2bf28322017-10-12 22:24:49 +01001424 switch (r.lockTaskLaunchMode) {
Craig Mautner15df08a2015-04-01 12:17:18 -07001425 case LOCK_TASK_LAUNCH_MODE_DEFAULT:
Charles He520b2832017-09-02 15:27:16 +01001426 mLockTaskAuth = mService.mLockTaskController.isPackageWhitelisted(userId, pkg)
1427 ? LOCK_TASK_AUTH_WHITELISTED : LOCK_TASK_AUTH_PINNABLE;
Craig Mautner15df08a2015-04-01 12:17:18 -07001428 break;
1429
1430 case LOCK_TASK_LAUNCH_MODE_NEVER:
Benjamin Franz469dd582015-06-09 14:24:36 +01001431 mLockTaskAuth = LOCK_TASK_AUTH_DONT_LOCK;
Craig Mautner15df08a2015-04-01 12:17:18 -07001432 break;
1433
1434 case LOCK_TASK_LAUNCH_MODE_ALWAYS:
Benjamin Franz469dd582015-06-09 14:24:36 +01001435 mLockTaskAuth = LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -07001436 break;
1437
1438 case LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED:
Charles He520b2832017-09-02 15:27:16 +01001439 mLockTaskAuth = mService.mLockTaskController.isPackageWhitelisted(userId, pkg)
1440 ? LOCK_TASK_AUTH_LAUNCHABLE : LOCK_TASK_AUTH_PINNABLE;
Craig Mautner15df08a2015-04-01 12:17:18 -07001441 break;
1442 }
Craig Mautner432f64e2015-05-20 14:59:57 -07001443 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "setLockTaskAuth: task=" + this +
1444 " mLockTaskAuth=" + lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07001445 }
1446
Craig Mautner84984fa2014-06-19 11:19:20 -07001447 boolean isOverHomeStack() {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001448 return mTaskToReturnTo == ACTIVITY_TYPE_HOME;
Craig Mautner1602ec22013-05-12 10:24:27 -07001449 }
1450
Winson Chung83471632016-12-13 11:02:12 -08001451 boolean isOverAssistantStack() {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001452 return mTaskToReturnTo == ACTIVITY_TYPE_ASSISTANT;
Winson Chung83471632016-12-13 11:02:12 -08001453 }
1454
Winson Chungd3395382016-12-13 11:49:09 -08001455 private boolean isResizeable(boolean checkSupportsPip) {
1456 return (mService.mForceResizableActivities || ActivityInfo.isResizeableMode(mResizeMode)
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -07001457 || (checkSupportsPip && mSupportsPictureInPicture));
Winson Chungd3395382016-12-13 11:49:09 -08001458 }
1459
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001460 boolean isResizeable() {
Winson Chungd3395382016-12-13 11:49:09 -08001461 return isResizeable(true /* checkSupportsPip */);
1462 }
1463
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001464 @Override
1465 public boolean supportsSplitScreenWindowingMode() {
Winson Chungd3395382016-12-13 11:49:09 -08001466 // A task can not be docked even if it is considered resizeable because it only supports
1467 // picture-in-picture mode but has a non-resizeable resizeMode
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001468 return super.supportsSplitScreenWindowingMode()
1469 && mService.mSupportsSplitScreenMultiWindow
Bryce Leec857a5b2017-08-16 10:04:52 -07001470 && (mService.mForceResizableActivities
1471 || (isResizeable(false /* checkSupportsPip */)
1472 && !ActivityInfo.isPreserveOrientationMode(mResizeMode)));
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001473 }
1474
skuhne@google.com322347b2016-12-02 12:54:03 -08001475 /**
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001476 * Check whether this task can be launched on the specified display.
1477 * @param displayId Target display id.
1478 * @return {@code true} if either it is the default display or this activity is resizeable and
1479 * can be put a secondary screen.
1480 */
1481 boolean canBeLaunchedOnDisplay(int displayId) {
1482 return mService.mStackSupervisor.canPlaceEntityOnDisplay(displayId,
Andrii Kulian02689a72017-07-06 14:28:59 -07001483 isResizeable(false /* checkSupportsPip */), -1 /* don't check PID */,
1484 -1 /* don't check UID */, null /* activityInfo */);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001485 }
1486
1487 /**
skuhne@google.com322347b2016-12-02 12:54:03 -08001488 * Check that a given bounds matches the application requested orientation.
1489 *
1490 * @param bounds The bounds to be tested.
1491 * @return True if the requested bounds are okay for a resizing request.
1492 */
Wale Ogunwale069bbd32017-02-03 07:58:14 -08001493 private boolean canResizeToBounds(Rect bounds) {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001494 if (bounds == null || !inFreeformWindowingMode()) {
skuhne@google.com322347b2016-12-02 12:54:03 -08001495 // Note: If not on the freeform workspace, we ignore the bounds.
1496 return true;
1497 }
1498 final boolean landscape = bounds.width() > bounds.height();
1499 if (mResizeMode == RESIZE_MODE_FORCE_RESIZABLE_PRESERVE_ORIENTATION) {
1500 return mBounds == null || landscape == (mBounds.width() > mBounds.height());
1501 }
1502 return (mResizeMode != RESIZE_MODE_FORCE_RESIZABLE_PORTRAIT_ONLY || !landscape)
1503 && (mResizeMode != RESIZE_MODE_FORCE_RESIZABLE_LANDSCAPE_ONLY || landscape);
1504 }
1505
Craig Mautner525f3d92013-05-07 14:01:50 -07001506 /**
Yorke Leebdef5372017-04-10 16:38:51 -07001507 * @return {@code true} if the task is being cleared for the purposes of being reused.
1508 */
1509 boolean isClearingToReuseTask() {
1510 return mReuseTask;
1511 }
1512
1513 /**
Craig Mautner525f3d92013-05-07 14:01:50 -07001514 * Find the activity in the history stack within the given task. Returns
1515 * the index within the history at which it's found, or < 0 if not found.
1516 */
1517 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r) {
1518 final ComponentName realActivity = r.realActivity;
1519 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1520 ActivityRecord candidate = mActivities.get(activityNdx);
1521 if (candidate.finishing) {
1522 continue;
1523 }
1524 if (candidate.realActivity.equals(realActivity)) {
1525 return candidate;
1526 }
1527 }
1528 return null;
1529 }
1530
Winson Chunga449dc02014-05-16 11:15:04 -07001531 /** Updates the last task description values. */
1532 void updateTaskDescription() {
1533 // Traverse upwards looking for any break between main task activities and
1534 // utility activities.
1535 int activityNdx;
1536 final int numActivities = mActivities.size();
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001537 final boolean relinquish = numActivities != 0 &&
1538 (mActivities.get(0).info.flags & FLAG_RELINQUISH_TASK_IDENTITY) != 0;
Winson Chunga449dc02014-05-16 11:15:04 -07001539 for (activityNdx = Math.min(numActivities, 1); activityNdx < numActivities;
Craig Mautner21d24a22014-04-23 11:45:37 -07001540 ++activityNdx) {
Winson Chunga449dc02014-05-16 11:15:04 -07001541 final ActivityRecord r = mActivities.get(activityNdx);
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001542 if (relinquish && (r.info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001543 // This will be the top activity for determining taskDescription. Pre-inc to
1544 // overcome initial decrement below.
1545 ++activityNdx;
1546 break;
1547 }
Winson Chunga449dc02014-05-16 11:15:04 -07001548 if (r.intent != null &&
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001549 (r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Winson Chunga449dc02014-05-16 11:15:04 -07001550 break;
1551 }
1552 }
1553 if (activityNdx > 0) {
1554 // Traverse downwards starting below break looking for set label, icon.
1555 // Note that if there are activities in the task but none of them set the
1556 // recent activity values, then we do not fall back to the last set
1557 // values in the TaskRecord.
1558 String label = null;
Craig Mautner648f69b2014-09-18 14:16:26 -07001559 String iconFilename = null;
Matthew Ng54bc9422017-10-02 17:16:28 -07001560 int iconResource = -1;
Winson Chunga449dc02014-05-16 11:15:04 -07001561 int colorPrimary = 0;
Winson Chung1af8eda2016-02-05 17:55:56 +00001562 int colorBackground = 0;
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001563 int statusBarColor = 0;
1564 int navigationBarColor = 0;
1565 boolean topActivity = true;
Winson Chunga449dc02014-05-16 11:15:04 -07001566 for (--activityNdx; activityNdx >= 0; --activityNdx) {
1567 final ActivityRecord r = mActivities.get(activityNdx);
1568 if (r.taskDescription != null) {
1569 if (label == null) {
1570 label = r.taskDescription.getLabel();
1571 }
Matthew Ng54bc9422017-10-02 17:16:28 -07001572 if (iconResource == -1) {
1573 iconResource = r.taskDescription.getIconResource();
1574 }
Craig Mautner648f69b2014-09-18 14:16:26 -07001575 if (iconFilename == null) {
1576 iconFilename = r.taskDescription.getIconFilename();
Winson Chunga449dc02014-05-16 11:15:04 -07001577 }
1578 if (colorPrimary == 0) {
1579 colorPrimary = r.taskDescription.getPrimaryColor();
Winson Chunga449dc02014-05-16 11:15:04 -07001580 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001581 if (topActivity) {
Winson Chung1af8eda2016-02-05 17:55:56 +00001582 colorBackground = r.taskDescription.getBackgroundColor();
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001583 statusBarColor = r.taskDescription.getStatusBarColor();
1584 navigationBarColor = r.taskDescription.getNavigationBarColor();
Winson Chung1af8eda2016-02-05 17:55:56 +00001585 }
Winson Chunga449dc02014-05-16 11:15:04 -07001586 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001587 topActivity = false;
Winson Chunga449dc02014-05-16 11:15:04 -07001588 }
Matthew Ng54bc9422017-10-02 17:16:28 -07001589 lastTaskDescription = new TaskDescription(label, null, iconResource, iconFilename,
1590 colorPrimary, colorBackground, statusBarColor, navigationBarColor);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +01001591 if (mWindowContainerController != null) {
1592 mWindowContainerController.setTaskDescription(lastTaskDescription);
1593 }
Winson Chungec396d62014-08-06 17:08:00 -07001594 // Update the task affiliation color if we are the parent of the group
1595 if (taskId == mAffiliatedTaskId) {
1596 mAffiliatedTaskColor = lastTaskDescription.getPrimaryColor();
1597 }
Winson Chunga449dc02014-05-16 11:15:04 -07001598 }
1599 }
1600
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001601 int findEffectiveRootIndex() {
Craig Mautner4767f4b2014-09-18 15:38:33 -07001602 int effectiveNdx = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001603 final int topActivityNdx = mActivities.size() - 1;
Dianne Hackborn39569af2014-09-23 10:56:58 -07001604 for (int activityNdx = 0; activityNdx <= topActivityNdx; ++activityNdx) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001605 final ActivityRecord r = mActivities.get(activityNdx);
1606 if (r.finishing) {
1607 continue;
1608 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001609 effectiveNdx = activityNdx;
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001610 if ((r.info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001611 break;
1612 }
1613 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001614 return effectiveNdx;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001615 }
1616
1617 void updateEffectiveIntent() {
1618 final int effectiveRootIndex = findEffectiveRootIndex();
1619 final ActivityRecord r = mActivities.get(effectiveRootIndex);
Winson Chungfee26772014-08-05 12:21:52 -07001620 setIntent(r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001621 }
1622
Craig Mautner21d24a22014-04-23 11:45:37 -07001623 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001624 if (DEBUG_RECENTS) Slog.i(TAG_RECENTS, "Saving task=" + this);
Craig Mautner21d24a22014-04-23 11:45:37 -07001625
1626 out.attribute(null, ATTR_TASKID, String.valueOf(taskId));
1627 if (realActivity != null) {
1628 out.attribute(null, ATTR_REALACTIVITY, realActivity.flattenToShortString());
1629 }
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001630 out.attribute(null, ATTR_REALACTIVITY_SUSPENDED, String.valueOf(realActivitySuspended));
Craig Mautner21d24a22014-04-23 11:45:37 -07001631 if (origActivity != null) {
1632 out.attribute(null, ATTR_ORIGACTIVITY, origActivity.flattenToShortString());
1633 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001634 // Write affinity, and root affinity if it is different from affinity.
1635 // We use the special string "@" for a null root affinity, so we can identify
1636 // later whether we were given a root affinity or should just make it the
1637 // same as the affinity.
Craig Mautner21d24a22014-04-23 11:45:37 -07001638 if (affinity != null) {
1639 out.attribute(null, ATTR_AFFINITY, affinity);
Dianne Hackborn79228822014-09-16 11:11:23 -07001640 if (!affinity.equals(rootAffinity)) {
1641 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
1642 }
1643 } else if (rootAffinity != null) {
1644 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
Craig Mautner21d24a22014-04-23 11:45:37 -07001645 }
1646 out.attribute(null, ATTR_ROOTHASRESET, String.valueOf(rootWasReset));
Dianne Hackborn13420f22014-07-18 15:43:56 -07001647 out.attribute(null, ATTR_AUTOREMOVERECENTS, String.valueOf(autoRemoveRecents));
Craig Mautner21d24a22014-04-23 11:45:37 -07001648 out.attribute(null, ATTR_ASKEDCOMPATMODE, String.valueOf(askedCompatMode));
1649 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001650 out.attribute(null, ATTR_USER_SETUP_COMPLETE, String.valueOf(mUserSetupComplete));
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001651 out.attribute(null, ATTR_EFFECTIVE_UID, String.valueOf(effectiveUid));
Craig Mautner21d24a22014-04-23 11:45:37 -07001652 out.attribute(null, ATTR_LASTTIMEMOVED, String.valueOf(mLastTimeMoved));
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001653 out.attribute(null, ATTR_NEVERRELINQUISH, String.valueOf(mNeverRelinquishIdentity));
Craig Mautner21d24a22014-04-23 11:45:37 -07001654 if (lastDescription != null) {
1655 out.attribute(null, ATTR_LASTDESCRIPTION, lastDescription.toString());
1656 }
Winson Chung2cb86c72014-06-25 12:03:30 -07001657 if (lastTaskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07001658 lastTaskDescription.saveToXml(out);
Winson Chung2cb86c72014-06-25 12:03:30 -07001659 }
Winson Chungec396d62014-08-06 17:08:00 -07001660 out.attribute(null, ATTR_TASK_AFFILIATION_COLOR, String.valueOf(mAffiliatedTaskColor));
Craig Mautnera228ae92014-07-09 05:44:55 -07001661 out.attribute(null, ATTR_TASK_AFFILIATION, String.valueOf(mAffiliatedTaskId));
1662 out.attribute(null, ATTR_PREV_AFFILIATION, String.valueOf(mPrevAffiliateTaskId));
1663 out.attribute(null, ATTR_NEXT_AFFILIATION, String.valueOf(mNextAffiliateTaskId));
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001664 out.attribute(null, ATTR_CALLING_UID, String.valueOf(mCallingUid));
1665 out.attribute(null, ATTR_CALLING_PACKAGE, mCallingPackage == null ? "" : mCallingPackage);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001666 out.attribute(null, ATTR_RESIZE_MODE, String.valueOf(mResizeMode));
Winson Chungd3395382016-12-13 11:49:09 -08001667 out.attribute(null, ATTR_SUPPORTS_PICTURE_IN_PICTURE,
1668 String.valueOf(mSupportsPictureInPicture));
Wale Ogunwale706ed792015-08-02 10:29:44 -07001669 if (mLastNonFullscreenBounds != null) {
1670 out.attribute(
1671 null, ATTR_NON_FULLSCREEN_BOUNDS, mLastNonFullscreenBounds.flattenToString());
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001672 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001673 out.attribute(null, ATTR_MIN_WIDTH, String.valueOf(mMinWidth));
1674 out.attribute(null, ATTR_MIN_HEIGHT, String.valueOf(mMinHeight));
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001675 out.attribute(null, ATTR_PERSIST_TASK_VERSION, String.valueOf(PERSIST_TASK_VERSION));
Winson Chung2cb86c72014-06-25 12:03:30 -07001676
Craig Mautner21d24a22014-04-23 11:45:37 -07001677 if (affinityIntent != null) {
1678 out.startTag(null, TAG_AFFINITYINTENT);
1679 affinityIntent.saveToXml(out);
1680 out.endTag(null, TAG_AFFINITYINTENT);
1681 }
1682
Winson Chung36f3f032016-09-08 23:29:43 +00001683 out.startTag(null, TAG_INTENT);
1684 intent.saveToXml(out);
1685 out.endTag(null, TAG_INTENT);
Craig Mautner21d24a22014-04-23 11:45:37 -07001686
1687 final ArrayList<ActivityRecord> activities = mActivities;
1688 final int numActivities = activities.size();
1689 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1690 final ActivityRecord r = activities.get(activityNdx);
Craig Mautner43e52ed2014-06-16 17:18:52 -07001691 if (r.info.persistableMode == ActivityInfo.PERSIST_ROOT_ONLY || !r.isPersistable() ||
Wale Ogunwale843bfb92015-03-27 11:06:48 -07001692 ((r.intent.getFlags() & FLAG_ACTIVITY_NEW_DOCUMENT
1693 | FLAG_ACTIVITY_RETAIN_IN_RECENTS) == FLAG_ACTIVITY_NEW_DOCUMENT) &&
Craig Mautner43e52ed2014-06-16 17:18:52 -07001694 activityNdx > 0) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001695 // Stop at first non-persistable or first break in task (CLEAR_WHEN_TASK_RESET).
Craig Mautner21d24a22014-04-23 11:45:37 -07001696 break;
1697 }
1698 out.startTag(null, TAG_ACTIVITY);
1699 r.saveToXml(out);
1700 out.endTag(null, TAG_ACTIVITY);
1701 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001702 }
1703
Winson Chung36f3f032016-09-08 23:29:43 +00001704 static TaskRecord restoreFromXml(XmlPullParser in, ActivityStackSupervisor stackSupervisor)
1705 throws IOException, XmlPullParserException {
Craig Mautner21d24a22014-04-23 11:45:37 -07001706 Intent intent = null;
1707 Intent affinityIntent = null;
Winsonc809cbb2015-11-02 12:06:15 -08001708 ArrayList<ActivityRecord> activities = new ArrayList<>();
Craig Mautner21d24a22014-04-23 11:45:37 -07001709 ComponentName realActivity = null;
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001710 boolean realActivitySuspended = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001711 ComponentName origActivity = null;
1712 String affinity = null;
Dianne Hackborn79228822014-09-16 11:11:23 -07001713 String rootAffinity = null;
1714 boolean hasRootAffinity = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001715 boolean rootHasReset = false;
Dianne Hackborn13420f22014-07-18 15:43:56 -07001716 boolean autoRemoveRecents = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001717 boolean askedCompatMode = false;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001718 int taskType = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -07001719 int userId = 0;
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001720 boolean userSetupComplete = true;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001721 int effectiveUid = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -07001722 String lastDescription = null;
1723 long lastTimeOnTop = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001724 boolean neverRelinquishIdentity = true;
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001725 int taskId = INVALID_TASK_ID;
Craig Mautner21d24a22014-04-23 11:45:37 -07001726 final int outerDepth = in.getDepth();
Craig Mautner648f69b2014-09-18 14:16:26 -07001727 TaskDescription taskDescription = new TaskDescription();
Wale Ogunwale18795a22014-12-03 11:38:33 -08001728 int taskAffiliation = INVALID_TASK_ID;
Winson Chungec396d62014-08-06 17:08:00 -07001729 int taskAffiliationColor = 0;
Wale Ogunwale18795a22014-12-03 11:38:33 -08001730 int prevTaskId = INVALID_TASK_ID;
1731 int nextTaskId = INVALID_TASK_ID;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001732 int callingUid = -1;
1733 String callingPackage = "";
Wale Ogunwaled829d362016-02-10 19:24:49 -08001734 int resizeMode = RESIZE_MODE_FORCE_RESIZEABLE;
Winson Chungd3395382016-12-13 11:49:09 -08001735 boolean supportsPictureInPicture = false;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001736 Rect bounds = null;
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001737 int minWidth = INVALID_MIN_SIZE;
1738 int minHeight = INVALID_MIN_SIZE;
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001739 int persistTaskVersion = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -07001740
1741 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
1742 final String attrName = in.getAttributeName(attrNdx);
1743 final String attrValue = in.getAttributeValue(attrNdx);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001744 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: attribute name=" +
1745 attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001746 if (ATTR_TASKID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001747 if (taskId == INVALID_TASK_ID) taskId = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001748 } else if (ATTR_REALACTIVITY.equals(attrName)) {
1749 realActivity = ComponentName.unflattenFromString(attrValue);
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001750 } else if (ATTR_REALACTIVITY_SUSPENDED.equals(attrName)) {
1751 realActivitySuspended = Boolean.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001752 } else if (ATTR_ORIGACTIVITY.equals(attrName)) {
1753 origActivity = ComponentName.unflattenFromString(attrValue);
1754 } else if (ATTR_AFFINITY.equals(attrName)) {
1755 affinity = attrValue;
Dianne Hackborn79228822014-09-16 11:11:23 -07001756 } else if (ATTR_ROOT_AFFINITY.equals(attrName)) {
1757 rootAffinity = attrValue;
1758 hasRootAffinity = true;
Craig Mautner21d24a22014-04-23 11:45:37 -07001759 } else if (ATTR_ROOTHASRESET.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001760 rootHasReset = Boolean.parseBoolean(attrValue);
Dianne Hackborn13420f22014-07-18 15:43:56 -07001761 } else if (ATTR_AUTOREMOVERECENTS.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001762 autoRemoveRecents = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001763 } else if (ATTR_ASKEDCOMPATMODE.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001764 askedCompatMode = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001765 } else if (ATTR_USERID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001766 userId = Integer.parseInt(attrValue);
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001767 } else if (ATTR_USER_SETUP_COMPLETE.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001768 userSetupComplete = Boolean.parseBoolean(attrValue);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001769 } else if (ATTR_EFFECTIVE_UID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001770 effectiveUid = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001771 } else if (ATTR_TASKTYPE.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001772 taskType = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001773 } else if (ATTR_LASTDESCRIPTION.equals(attrName)) {
1774 lastDescription = attrValue;
1775 } else if (ATTR_LASTTIMEMOVED.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001776 lastTimeOnTop = Long.parseLong(attrValue);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001777 } else if (ATTR_NEVERRELINQUISH.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001778 neverRelinquishIdentity = Boolean.parseBoolean(attrValue);
Craig Mautner648f69b2014-09-18 14:16:26 -07001779 } else if (attrName.startsWith(TaskDescription.ATTR_TASKDESCRIPTION_PREFIX)) {
1780 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001781 } else if (ATTR_TASK_AFFILIATION.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001782 taskAffiliation = Integer.parseInt(attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001783 } else if (ATTR_PREV_AFFILIATION.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001784 prevTaskId = Integer.parseInt(attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001785 } else if (ATTR_NEXT_AFFILIATION.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001786 nextTaskId = Integer.parseInt(attrValue);
Winson Chungec396d62014-08-06 17:08:00 -07001787 } else if (ATTR_TASK_AFFILIATION_COLOR.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001788 taskAffiliationColor = Integer.parseInt(attrValue);
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001789 } else if (ATTR_CALLING_UID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001790 callingUid = Integer.parseInt(attrValue);
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001791 } else if (ATTR_CALLING_PACKAGE.equals(attrName)) {
1792 callingPackage = attrValue;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001793 } else if (ATTR_RESIZE_MODE.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001794 resizeMode = Integer.parseInt(attrValue);
Winson Chungd3395382016-12-13 11:49:09 -08001795 } else if (ATTR_SUPPORTS_PICTURE_IN_PICTURE.equals(attrName)) {
1796 supportsPictureInPicture = Boolean.parseBoolean(attrValue);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001797 } else if (ATTR_NON_FULLSCREEN_BOUNDS.equals(attrName)) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001798 bounds = Rect.unflattenFromString(attrValue);
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001799 } else if (ATTR_MIN_WIDTH.equals(attrName)) {
1800 minWidth = Integer.parseInt(attrValue);
1801 } else if (ATTR_MIN_HEIGHT.equals(attrName)) {
1802 minHeight = Integer.parseInt(attrValue);
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001803 } else if (ATTR_PERSIST_TASK_VERSION.equals(attrName)) {
1804 persistTaskVersion = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001805 } else {
1806 Slog.w(TAG, "TaskRecord: Unknown attribute=" + attrName);
1807 }
1808 }
1809
1810 int event;
1811 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
Ben Kwa8814cf42015-07-08 10:54:56 -07001812 (event != XmlPullParser.END_TAG || in.getDepth() >= outerDepth)) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001813 if (event == XmlPullParser.START_TAG) {
1814 final String name = in.getName();
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001815 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: START_TAG name=" +
1816 name);
Craig Mautner21d24a22014-04-23 11:45:37 -07001817 if (TAG_AFFINITYINTENT.equals(name)) {
1818 affinityIntent = Intent.restoreFromXml(in);
1819 } else if (TAG_INTENT.equals(name)) {
1820 intent = Intent.restoreFromXml(in);
1821 } else if (TAG_ACTIVITY.equals(name)) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001822 ActivityRecord activity = ActivityRecord.restoreFromXml(in, stackSupervisor);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001823 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: activity=" +
1824 activity);
Craig Mautner21d24a22014-04-23 11:45:37 -07001825 if (activity != null) {
1826 activities.add(activity);
1827 }
1828 } else {
1829 Slog.e(TAG, "restoreTask: Unexpected name=" + name);
1830 XmlUtils.skipCurrentTag(in);
1831 }
1832 }
1833 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001834 if (!hasRootAffinity) {
1835 rootAffinity = affinity;
1836 } else if ("@".equals(rootAffinity)) {
1837 rootAffinity = null;
1838 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001839 if (effectiveUid <= 0) {
1840 Intent checkIntent = intent != null ? intent : affinityIntent;
1841 effectiveUid = 0;
1842 if (checkIntent != null) {
1843 IPackageManager pm = AppGlobals.getPackageManager();
1844 try {
1845 ApplicationInfo ai = pm.getApplicationInfo(
1846 checkIntent.getComponent().getPackageName(),
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -07001847 PackageManager.MATCH_UNINSTALLED_PACKAGES
1848 | PackageManager.MATCH_DISABLED_COMPONENTS, userId);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001849 if (ai != null) {
1850 effectiveUid = ai.uid;
1851 }
1852 } catch (RemoteException e) {
1853 }
1854 }
1855 Slog.w(TAG, "Updating task #" + taskId + " for " + checkIntent
1856 + ": effectiveUid=" + effectiveUid);
1857 }
1858
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001859 if (persistTaskVersion < 1) {
1860 // We need to convert the resize mode of home activities saved before version one if
1861 // they are marked as RESIZE_MODE_RESIZEABLE to RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION
1862 // since we didn't have that differentiation before version 1 and the system didn't
1863 // resize home activities before then.
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001864 if (taskType == 1 /* old home type */ && resizeMode == RESIZE_MODE_RESIZEABLE) {
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001865 resizeMode = RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
1866 }
Winson Chungd3395382016-12-13 11:49:09 -08001867 } else {
1868 // This activity has previously marked itself explicitly as both resizeable and
1869 // supporting picture-in-picture. Since there is no longer a requirement for
1870 // picture-in-picture activities to be resizeable, we can mark this simply as
1871 // resizeable and supporting picture-in-picture separately.
1872 if (resizeMode == RESIZE_MODE_RESIZEABLE_AND_PIPABLE_DEPRECATED) {
1873 resizeMode = RESIZE_MODE_RESIZEABLE;
1874 supportsPictureInPicture = true;
1875 }
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001876 }
1877
Winson Chung36f3f032016-09-08 23:29:43 +00001878 final TaskRecord task = new TaskRecord(stackSupervisor.mService, taskId, intent,
Dianne Hackborn79228822014-09-16 11:11:23 -07001879 affinityIntent, affinity, rootAffinity, realActivity, origActivity, rootHasReset,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001880 autoRemoveRecents, askedCompatMode, userId, effectiveUid, lastDescription,
Winson Chungfb44d212017-10-04 11:39:10 -07001881 activities, lastTimeOnTop, neverRelinquishIdentity, taskDescription,
1882 taskAffiliation, prevTaskId, nextTaskId, taskAffiliationColor, callingUid,
Charles He2bf28322017-10-12 22:24:49 +01001883 callingPackage, resizeMode, supportsPictureInPicture, realActivitySuspended,
1884 userSetupComplete, minWidth, minHeight);
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07001885 task.updateOverrideConfiguration(bounds);
Craig Mautner21d24a22014-04-23 11:45:37 -07001886
1887 for (int activityNdx = activities.size() - 1; activityNdx >=0; --activityNdx) {
Bryce Leeaf691c02017-03-20 14:20:22 -07001888 activities.get(activityNdx).setTask(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001889 }
1890
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001891 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Restored task=" + task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001892 return task;
1893 }
1894
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001895 private void adjustForMinimalTaskDimensions(Rect bounds) {
1896 if (bounds == null) {
1897 return;
1898 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001899 int minWidth = mMinWidth;
1900 int minHeight = mMinHeight;
Robert Carr9c5867d2016-03-10 15:52:46 -08001901 // If the task has no requested minimal size, we'd like to enforce a minimal size
1902 // so that the user can not render the task too small to manipulate. We don't need
1903 // to do this for the pinned stack as the bounds are controlled by the system.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001904 if (!inPinnedWindowingMode()) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001905 if (minWidth == INVALID_MIN_SIZE) {
1906 minWidth = mService.mStackSupervisor.mDefaultMinSizeOfResizeableTask;
Andrii Kulian2e751b82016-03-16 16:59:32 -07001907 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001908 if (minHeight == INVALID_MIN_SIZE) {
1909 minHeight = mService.mStackSupervisor.mDefaultMinSizeOfResizeableTask;
Andrii Kulian2e751b82016-03-16 16:59:32 -07001910 }
Robert Carr9c5867d2016-03-10 15:52:46 -08001911 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001912 final boolean adjustWidth = minWidth > bounds.width();
1913 final boolean adjustHeight = minHeight > bounds.height();
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001914 if (!(adjustWidth || adjustHeight)) {
1915 return;
1916 }
1917
1918 if (adjustWidth) {
1919 if (mBounds != null && bounds.right == mBounds.right) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001920 bounds.left = bounds.right - minWidth;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001921 } else {
1922 // Either left bounds match, or neither match, or the previous bounds were
1923 // fullscreen and we default to keeping left.
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001924 bounds.right = bounds.left + minWidth;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001925 }
1926 }
1927 if (adjustHeight) {
1928 if (mBounds != null && bounds.bottom == mBounds.bottom) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001929 bounds.top = bounds.bottom - minHeight;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001930 } else {
1931 // Either top bounds match, or neither match, or the previous bounds were
1932 // fullscreen and we default to keeping top.
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001933 bounds.bottom = bounds.top + minHeight;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001934 }
1935 }
1936 }
1937
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001938 /**
Winson Chung5af42fc2017-03-24 17:11:33 -07001939 * @return a new Configuration for this Task, given the provided {@param bounds} and
1940 * {@param insetBounds}.
1941 */
1942 Configuration computeNewOverrideConfigurationForBounds(Rect bounds, Rect insetBounds) {
1943 // Compute a new override configuration for the given bounds, if fullscreen bounds
1944 // (bounds == null), then leave the override config unset
1945 final Configuration newOverrideConfig = new Configuration();
1946 if (bounds != null) {
1947 newOverrideConfig.setTo(getOverrideConfiguration());
1948 mTmpRect.set(bounds);
1949 adjustForMinimalTaskDimensions(mTmpRect);
1950 computeOverrideConfiguration(newOverrideConfig, mTmpRect, insetBounds,
1951 mTmpRect.right != bounds.right, mTmpRect.bottom != bounds.bottom);
1952 }
1953
1954 return newOverrideConfig;
1955 }
1956
1957 /**
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001958 * Update task's override configuration based on the bounds.
Jorim Jaggi0a932142016-02-01 17:42:25 -08001959 * @param bounds The bounds of the task.
Andrii Kulian8072d112016-09-16 11:11:01 -07001960 * @return True if the override configuration was updated.
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001961 */
Andrii Kulian8072d112016-09-16 11:11:01 -07001962 boolean updateOverrideConfiguration(Rect bounds) {
Jorim Jaggi0a932142016-02-01 17:42:25 -08001963 return updateOverrideConfiguration(bounds, null /* insetBounds */);
1964 }
1965
1966 /**
1967 * Update task's override configuration based on the bounds.
1968 * @param bounds The bounds of the task.
1969 * @param insetBounds The bounds used to calculate the system insets, which is used here to
1970 * subtract the navigation bar/status bar size from the screen size reported
1971 * to the application. See {@link IActivityManager#resizeDockedStack}.
Andrii Kulian8072d112016-09-16 11:11:01 -07001972 * @return True if the override configuration was updated.
Jorim Jaggi0a932142016-02-01 17:42:25 -08001973 */
Andrii Kulian8072d112016-09-16 11:11:01 -07001974 boolean updateOverrideConfiguration(Rect bounds, @Nullable Rect insetBounds) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001975 if (Objects.equals(mBounds, bounds)) {
Andrii Kulian8072d112016-09-16 11:11:01 -07001976 return false;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001977 }
Andrii Kulian1779e612016-10-12 21:58:25 -07001978 mTmpConfig.setTo(getOverrideConfiguration());
Wale Ogunwale5f986092015-12-04 15:35:38 -08001979 final boolean oldFullscreen = mFullscreen;
Andrii Kulian1779e612016-10-12 21:58:25 -07001980 final Configuration newConfig = getOverrideConfiguration();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001981
1982 mFullscreen = bounds == null;
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001983 final boolean persistBounds = getWindowConfiguration().persistTaskBounds();
Wale Ogunwale706ed792015-08-02 10:29:44 -07001984 if (mFullscreen) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001985 if (mBounds != null && persistBounds) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001986 mLastNonFullscreenBounds = mBounds;
1987 }
1988 mBounds = null;
Andrii Kulian1779e612016-10-12 21:58:25 -07001989 newConfig.unset();
Wale Ogunwale706ed792015-08-02 10:29:44 -07001990 } else {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001991 mTmpRect.set(bounds);
1992 adjustForMinimalTaskDimensions(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -08001993 if (mBounds == null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001994 mBounds = new Rect(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -08001995 } else {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001996 mBounds.set(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -08001997 }
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001998 if (mStack == null || persistBounds) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001999 mLastNonFullscreenBounds = mBounds;
2000 }
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002001 computeOverrideConfiguration(newConfig, mTmpRect, insetBounds,
Andrii Kuliandaea3572016-04-08 13:20:51 -07002002 mTmpRect.right != bounds.right, mTmpRect.bottom != bounds.bottom);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002003 }
Andrii Kulian1779e612016-10-12 21:58:25 -07002004 onOverrideConfigurationChanged(newConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002005
2006 if (mFullscreen != oldFullscreen) {
Winson Chung5af42fc2017-03-24 17:11:33 -07002007 mService.mStackSupervisor.scheduleUpdateMultiWindowMode(this);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002008 }
2009
Andrii Kulian1779e612016-10-12 21:58:25 -07002010 return !mTmpConfig.equals(newConfig);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002011 }
2012
Andrii Kulian1779e612016-10-12 21:58:25 -07002013 /** Clears passed config and fills it with new override values. */
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002014 // TODO(b/36505427): TaskRecord.computeOverrideConfiguration() is a utility method that doesn't
2015 // depend on task or stacks, but uses those object to get the display to base the calculation
2016 // on. Probably best to centralize calculations like this in ConfigurationContainer.
2017 void computeOverrideConfiguration(Configuration config, Rect bounds, Rect insetBounds,
Andrii Kuliana8a9bc52016-10-14 11:00:13 -07002018 boolean overrideWidth, boolean overrideHeight) {
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002019 mTmpNonDecorBounds.set(bounds);
2020 mTmpStableBounds.set(bounds);
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002021
Andrii Kulian1779e612016-10-12 21:58:25 -07002022 config.unset();
Winson Chungbdc646f2017-02-13 12:12:22 -08002023 final Configuration parentConfig = getParent().getConfiguration();
Bryce Lee7566d762017-03-30 09:34:15 -07002024
Andrii Kulian1779e612016-10-12 21:58:25 -07002025 final float density = parentConfig.densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002026
Winson Chungbdc646f2017-02-13 12:12:22 -08002027 if (mStack != null) {
2028 final StackWindowController stackController = mStack.getWindowContainerController();
2029 stackController.adjustConfigurationForBounds(bounds, insetBounds,
2030 mTmpNonDecorBounds, mTmpStableBounds, overrideWidth, overrideHeight, density,
2031 config, parentConfig);
2032 } else {
Bryce Lee7566d762017-03-30 09:34:15 -07002033 throw new IllegalArgumentException("Expected stack when calculating override config");
Winson Chungbdc646f2017-02-13 12:12:22 -08002034 }
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002035
Winson Chung60c1aba2017-03-14 17:47:42 -07002036 config.orientation = (config.screenWidthDp <= config.screenHeightDp)
2037 ? Configuration.ORIENTATION_PORTRAIT
2038 : Configuration.ORIENTATION_LANDSCAPE;
2039
Jorim Jaggi85639432016-05-06 17:27:55 -07002040 // For calculating screen layout, we need to use the non-decor inset screen area for the
2041 // calculation for compatibility reasons, i.e. screen area without system bars that could
2042 // never go away in Honeycomb.
Winson Chungbdc646f2017-02-13 12:12:22 -08002043 final int compatScreenWidthDp = (int) (mTmpNonDecorBounds.width() / density);
2044 final int compatScreenHeightDp = (int) (mTmpNonDecorBounds.height() / density);
Andrii Kulian1779e612016-10-12 21:58:25 -07002045 // We're only overriding LONG, SIZE and COMPAT parts of screenLayout, so we start override
2046 // calculation with partial default.
2047 final int sl = Configuration.SCREENLAYOUT_LONG_YES | Configuration.SCREENLAYOUT_SIZE_XLARGE;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002048 final int longSize = Math.max(compatScreenHeightDp, compatScreenWidthDp);
Andrii Kulian1779e612016-10-12 21:58:25 -07002049 final int shortSize = Math.min(compatScreenHeightDp, compatScreenWidthDp);
Jorim Jaggi85639432016-05-06 17:27:55 -07002050 config.screenLayout = Configuration.reduceScreenLayout(sl, longSize, shortSize);
2051
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08002052 }
2053
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002054 Rect updateOverrideConfigurationFromLaunchBounds() {
2055 final Rect bounds = validateBounds(getLaunchBounds());
2056 updateOverrideConfiguration(bounds);
Andrii Kulian73336d812016-03-24 12:56:08 -07002057 if (bounds != null) {
2058 bounds.set(mBounds);
2059 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002060 return bounds;
2061 }
2062
2063 static Rect validateBounds(Rect bounds) {
2064 if (bounds != null && bounds.isEmpty()) {
2065 Slog.wtf(TAG, "Received strange task bounds: " + bounds, new Throwable());
2066 return null;
2067 }
2068 return bounds;
2069 }
2070
Wale Ogunwale935e5022015-11-10 12:36:10 -08002071 /** Updates the task's bounds and override configuration to match what is expected for the
2072 * input stack. */
2073 void updateOverrideConfigurationForStack(ActivityStack inStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002074 if (mStack != null && mStack == inStack) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08002075 return;
2076 }
2077
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002078 if (inStack.inFreeformWindowingMode()) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002079 if (!isResizeable()) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08002080 throw new IllegalArgumentException("Can not position non-resizeable task="
2081 + this + " in stack=" + inStack);
2082 }
2083 if (mBounds != null) {
2084 return;
2085 }
2086 if (mLastNonFullscreenBounds != null) {
2087 updateOverrideConfiguration(mLastNonFullscreenBounds);
2088 } else {
2089 inStack.layoutTaskInStack(this, null);
2090 }
2091 } else {
2092 updateOverrideConfiguration(inStack.mBounds);
2093 }
2094 }
2095
Wale Ogunwale706ed792015-08-02 10:29:44 -07002096 /** Returns the bounds that should be used to launch this task. */
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002097 private Rect getLaunchBounds() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002098 if (mStack == null) {
Chong Zhang7d5f5102016-01-13 10:29:24 -08002099 return null;
2100 }
2101
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07002102 final int windowingMode = getWindowingMode();
2103 if (!isActivityTypeStandardOrUndefined()
2104 || windowingMode == WINDOWING_MODE_FULLSCREEN
2105 || (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY && !isResizeable())) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002106 return isResizeable() ? mStack.mBounds : null;
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002107 } else if (!getWindowConfiguration().persistTaskBounds()) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002108 return mStack.mBounds;
Wale Ogunwale706ed792015-08-02 10:29:44 -07002109 }
2110 return mLastNonFullscreenBounds;
2111 }
2112
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01002113 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
2114 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2115 final ActivityRecord r = mActivities.get(activityNdx);
2116 if (r.visible) {
2117 r.showStartingWindow(null /* prev */, false /* newTask */, taskSwitch);
2118 }
2119 }
2120 }
2121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002123 pw.print(prefix); pw.print("userId="); pw.print(userId);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07002124 pw.print(" effectiveUid="); UserHandle.formatUid(pw, effectiveUid);
2125 pw.print(" mCallingUid="); UserHandle.formatUid(pw, mCallingUid);
Suprabh Shukla7745c142016-03-07 18:21:10 -08002126 pw.print(" mUserSetupComplete="); pw.print(mUserSetupComplete);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002127 pw.print(" mCallingPackage="); pw.println(mCallingPackage);
Dianne Hackborn79228822014-09-16 11:11:23 -07002128 if (affinity != null || rootAffinity != null) {
2129 pw.print(prefix); pw.print("affinity="); pw.print(affinity);
2130 if (affinity == null || !affinity.equals(rootAffinity)) {
2131 pw.print(" root="); pw.println(rootAffinity);
2132 } else {
2133 pw.println();
2134 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002135 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002136 if (voiceSession != null || voiceInteractor != null) {
2137 pw.print(prefix); pw.print("VOICE: session=0x");
2138 pw.print(Integer.toHexString(System.identityHashCode(voiceSession)));
2139 pw.print(" interactor=0x");
2140 pw.println(Integer.toHexString(System.identityHashCode(voiceInteractor)));
2141 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002142 if (intent != null) {
2143 StringBuilder sb = new StringBuilder(128);
2144 sb.append(prefix); sb.append("intent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08002145 intent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002146 sb.append('}');
2147 pw.println(sb.toString());
2148 }
2149 if (affinityIntent != null) {
2150 StringBuilder sb = new StringBuilder(128);
2151 sb.append(prefix); sb.append("affinityIntent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08002152 affinityIntent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002153 sb.append('}');
2154 pw.println(sb.toString());
2155 }
2156 if (origActivity != null) {
2157 pw.print(prefix); pw.print("origActivity=");
2158 pw.println(origActivity.flattenToShortString());
2159 }
2160 if (realActivity != null) {
2161 pw.print(prefix); pw.print("realActivity=");
2162 pw.println(realActivity.flattenToShortString());
2163 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002164 if (autoRemoveRecents || isPersistable || !isActivityTypeStandard()
2165 || mTaskToReturnTo != ACTIVITY_TYPE_STANDARD || numFullscreen != 0) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002166 pw.print(prefix); pw.print("autoRemoveRecents="); pw.print(autoRemoveRecents);
Dianne Hackborn852975d2014-08-22 17:42:43 -07002167 pw.print(" isPersistable="); pw.print(isPersistable);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002168 pw.print(" numFullscreen="); pw.print(numFullscreen);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002169 pw.print(" activityType="); pw.print(getActivityType());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002170 pw.print(" mTaskToReturnTo="); pw.println(mTaskToReturnTo);
2171 }
Craig Mautner432f64e2015-05-20 14:59:57 -07002172 if (rootWasReset || mNeverRelinquishIdentity || mReuseTask
2173 || mLockTaskAuth != LOCK_TASK_AUTH_PINNABLE) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002174 pw.print(prefix); pw.print("rootWasReset="); pw.print(rootWasReset);
2175 pw.print(" mNeverRelinquishIdentity="); pw.print(mNeverRelinquishIdentity);
Craig Mautner432f64e2015-05-20 14:59:57 -07002176 pw.print(" mReuseTask="); pw.print(mReuseTask);
2177 pw.print(" mLockTaskAuth="); pw.println(lockTaskAuthToString());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002178 }
Wale Ogunwale18795a22014-12-03 11:38:33 -08002179 if (mAffiliatedTaskId != taskId || mPrevAffiliateTaskId != INVALID_TASK_ID
2180 || mPrevAffiliate != null || mNextAffiliateTaskId != INVALID_TASK_ID
2181 || mNextAffiliate != null) {
Dianne Hackborn852975d2014-08-22 17:42:43 -07002182 pw.print(prefix); pw.print("affiliation="); pw.print(mAffiliatedTaskId);
2183 pw.print(" prevAffiliation="); pw.print(mPrevAffiliateTaskId);
2184 pw.print(" (");
2185 if (mPrevAffiliate == null) {
2186 pw.print("null");
2187 } else {
2188 pw.print(Integer.toHexString(System.identityHashCode(mPrevAffiliate)));
2189 }
2190 pw.print(") nextAffiliation="); pw.print(mNextAffiliateTaskId);
2191 pw.print(" (");
2192 if (mNextAffiliate == null) {
2193 pw.print("null");
2194 } else {
2195 pw.print(Integer.toHexString(System.identityHashCode(mNextAffiliate)));
2196 }
2197 pw.println(")");
2198 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002199 pw.print(prefix); pw.print("Activities="); pw.println(mActivities);
Dianne Hackborn852975d2014-08-22 17:42:43 -07002200 if (!askedCompatMode || !inRecents || !isAvailable) {
2201 pw.print(prefix); pw.print("askedCompatMode="); pw.print(askedCompatMode);
2202 pw.print(" inRecents="); pw.print(inRecents);
2203 pw.print(" isAvailable="); pw.println(isAvailable);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07002204 }
Dianne Hackborn852975d2014-08-22 17:42:43 -07002205 if (lastDescription != null) {
2206 pw.print(prefix); pw.print("lastDescription="); pw.println(lastDescription);
2207 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002208 pw.print(prefix); pw.print("stackId="); pw.println(getStackId());
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002209 pw.print(prefix + "hasBeenVisible=" + hasBeenVisible);
2210 pw.print(" mResizeMode=" + ActivityInfo.resizeModeToString(mResizeMode));
Winson Chungd3395382016-12-13 11:49:09 -08002211 pw.print(" mSupportsPictureInPicture=" + mSupportsPictureInPicture);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002212 pw.print(" isResizeable=" + isResizeable());
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002213 pw.print(" lastActiveTime=" + lastActiveTime);
2214 pw.println(" (inactive for " + (getInactiveDuration() / 1000) + "s)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 }
2216
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002217 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002219 StringBuilder sb = new StringBuilder(128);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002220 if (stringName != null) {
2221 sb.append(stringName);
2222 sb.append(" U=");
2223 sb.append(userId);
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002224 sb.append(" StackId=");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002225 sb.append(getStackId());
Craig Mautnerde4ef022013-04-07 19:01:33 -07002226 sb.append(" sz=");
2227 sb.append(mActivities.size());
2228 sb.append('}');
2229 return sb.toString();
2230 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002231 sb.append("TaskRecord{");
2232 sb.append(Integer.toHexString(System.identityHashCode(this)));
2233 sb.append(" #");
2234 sb.append(taskId);
2235 if (affinity != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002236 sb.append(" A=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002237 sb.append(affinity);
2238 } else if (intent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002239 sb.append(" I=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002240 sb.append(intent.getComponent().flattenToShortString());
2241 } else if (affinityIntent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002242 sb.append(" aI=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002243 sb.append(affinityIntent.getComponent().flattenToShortString());
2244 } else {
2245 sb.append(" ??");
2246 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002247 stringName = sb.toString();
2248 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07002250
2251 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2252 final long token = proto.start(fieldId);
2253 super.writeToProto(proto, CONFIGURATION_CONTAINER);
2254 proto.write(ID, taskId);
2255 for (int i = mActivities.size() - 1; i >= 0; i--) {
2256 ActivityRecord activity = mActivities.get(i);
2257 activity.writeToProto(proto, ACTIVITIES);
2258 }
2259 proto.write(STACK_ID, mStack.mStackId);
2260 if (mLastNonFullscreenBounds != null) {
2261 mLastNonFullscreenBounds.writeToProto(proto, LAST_NON_FULLSCREEN_BOUNDS);
2262 }
2263 if (realActivity != null) {
2264 proto.write(REAL_ACTIVITY, realActivity.flattenToShortString());
2265 }
2266 if (origActivity != null) {
2267 proto.write(ORIG_ACTIVITY, origActivity.flattenToShortString());
2268 }
2269 proto.write(ACTIVITY_TYPE, getActivityType());
2270 proto.write(RETURN_TO_TYPE, mTaskToReturnTo);
2271 proto.write(RESIZE_MODE, mResizeMode);
2272 proto.write(FULLSCREEN, mFullscreen);
2273 if (mBounds != null) {
2274 mBounds.writeToProto(proto, BOUNDS);
2275 }
2276 proto.write(MIN_WIDTH, mMinWidth);
2277 proto.write(MIN_HEIGHT, mMinHeight);
2278 proto.end(token);
2279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280}