blob: 15789309cf7a29df53c57809d8aeced00f8d73b7 [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;
Winson Chung83471632016-12-13 11:02:12 -080021import static android.app.ActivityManager.StackId.ASSISTANT_STACK_ID;
Jorim Jaggi0a932142016-02-01 17:42:25 -080022import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
23import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
24import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
25import static android.app.ActivityManager.StackId.HOME_STACK_ID;
26import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
27import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -080028import static android.app.ActivityManager.StackId.RECENTS_STACK_ID;
Jorim Jaggi0a932142016-02-01 17:42:25 -080029import static android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
30import static android.content.Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
Wale Ogunwale3eadad72016-10-13 09:16:59 -070031import static android.content.pm.ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY;
Jorim Jaggi0a932142016-02-01 17:42:25 -080032import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_ALWAYS;
33import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_DEFAULT;
34import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
35import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_NEVER;
skuhne@google.com322347b2016-12-02 12:54:03 -080036import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZABLE_LANDSCAPE_ONLY;
37import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZABLE_PORTRAIT_ONLY;
38import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZABLE_PRESERVE_ORIENTATION;
Wale Ogunwaled829d362016-02-10 19:24:49 -080039import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Wale Ogunwale625ed0c2016-10-18 08:50:31 -070040import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE;
Winson Chungd3395382016-12-13 11:49:09 -080041import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_AND_PIPABLE_DEPRECATED;
Wale Ogunwale625ed0c2016-10-18 08:50:31 -070042import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Jorim Jaggi0a932142016-02-01 17:42:25 -080043import static android.content.pm.ApplicationInfo.PRIVATE_FLAG_PRIVILEGED;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080044import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
Suprabh Shukla7745c142016-03-07 18:21:10 -080045import static android.provider.Settings.Secure.USER_SETUP_COMPLETE;
Andrii Kulian036e3ad2017-04-19 10:55:10 -070046import static android.view.Display.DEFAULT_DISPLAY;
Jorim Jaggi0a932142016-02-01 17:42:25 -080047import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ADD_REMOVE;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
51import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_ADD_REMOVE;
52import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
53import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
54import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
55import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
56import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Jorim Jaggi0a932142016-02-01 17:42:25 -080057import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Winson Chung83471632016-12-13 11:02:12 -080058import static com.android.server.am.ActivityRecord.ASSISTANT_ACTIVITY_TYPE;
Jorim Jaggi0a932142016-02-01 17:42:25 -080059import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
60import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
Wale Ogunwale3b232392016-05-13 15:37:13 -070061import static com.android.server.am.ActivityRecord.STARTING_WINDOW_SHOWN;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080062import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
Wale Ogunwale56d8d162017-05-30 11:12:20 -070063import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING_TO_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;
Winson Chung74666102017-02-22 17:49:24 -080066import static java.lang.Integer.MAX_VALUE;
67
Jorim Jaggie7d2b852017-08-28 17:55:15 +020068import android.annotation.IntDef;
69import android.annotation.Nullable;
70import android.app.Activity;
71import android.app.ActivityManager;
72import android.app.ActivityManager.StackId;
73import android.app.ActivityManager.TaskDescription;
74import android.app.ActivityManager.TaskSnapshot;
75import android.app.ActivityOptions;
76import android.app.AppGlobals;
77import android.app.IActivityManager;
78import android.content.ComponentName;
79import android.content.Intent;
80import android.content.pm.ActivityInfo;
81import android.content.pm.ApplicationInfo;
82import android.content.pm.IPackageManager;
83import android.content.pm.PackageManager;
84import android.content.res.Configuration;
85import android.graphics.Rect;
86import android.os.Debug;
87import android.os.RemoteException;
88import android.os.Trace;
89import android.os.UserHandle;
90import android.provider.Settings;
91import android.service.voice.IVoiceInteractionSession;
92import android.util.DisplayMetrics;
93import android.util.Slog;
94
95import com.android.internal.annotations.VisibleForTesting;
96import com.android.internal.app.IVoiceInteractor;
97import com.android.internal.util.XmlUtils;
98import com.android.server.wm.AppWindowContainerController;
99import com.android.server.wm.ConfigurationContainer;
100import com.android.server.wm.StackWindowController;
101import com.android.server.wm.TaskWindowContainerController;
102import com.android.server.wm.TaskWindowContainerListener;
103import com.android.server.wm.WindowManagerService;
104
105import org.xmlpull.v1.XmlPullParser;
106import org.xmlpull.v1.XmlPullParserException;
107import org.xmlpull.v1.XmlSerializer;
108
109import java.io.IOException;
110import java.io.PrintWriter;
111import java.lang.annotation.Retention;
112import java.lang.annotation.RetentionPolicy;
113import java.util.ArrayList;
114import java.util.Objects;
115
Benjamin Franza83859f2017-07-03 16:34:14 +0100116class TaskRecord extends ConfigurationContainer implements TaskWindowContainerListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800117 private static final String TAG = TAG_WITH_CLASS_NAME ? "TaskRecord" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700118 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700119 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Craig Mautnere0570202015-05-13 13:06:11 -0700120 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700121 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800122
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700123 private static final String ATTR_TASKID = "task_id";
Craig Mautner21d24a22014-04-23 11:45:37 -0700124 private static final String TAG_INTENT = "intent";
125 private static final String TAG_AFFINITYINTENT = "affinity_intent";
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700126 private static final String ATTR_REALACTIVITY = "real_activity";
127 private static final String ATTR_REALACTIVITY_SUSPENDED = "real_activity_suspended";
Craig Mautner21d24a22014-04-23 11:45:37 -0700128 private static final String ATTR_ORIGACTIVITY = "orig_activity";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700129 private static final String TAG_ACTIVITY = "activity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700130 private static final String ATTR_AFFINITY = "affinity";
Dianne Hackborn79228822014-09-16 11:11:23 -0700131 private static final String ATTR_ROOT_AFFINITY = "root_affinity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700132 private static final String ATTR_ROOTHASRESET = "root_has_reset";
Dianne Hackborn13420f22014-07-18 15:43:56 -0700133 private static final String ATTR_AUTOREMOVERECENTS = "auto_remove_recents";
Craig Mautner21d24a22014-04-23 11:45:37 -0700134 private static final String ATTR_ASKEDCOMPATMODE = "asked_compat_mode";
135 private static final String ATTR_USERID = "user_id";
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800136 private static final String ATTR_USER_SETUP_COMPLETE = "user_setup_complete";
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700137 private static final String ATTR_EFFECTIVE_UID = "effective_uid";
Craig Mautner21d24a22014-04-23 11:45:37 -0700138 private static final String ATTR_TASKTYPE = "task_type";
Winson Chungffa2ec62014-07-03 15:54:42 -0700139 private static final String ATTR_FIRSTACTIVETIME = "first_active_time";
Winson Chungf1fbd772014-06-24 18:06:58 -0700140 private static final String ATTR_LASTACTIVETIME = "last_active_time";
Craig Mautner21d24a22014-04-23 11:45:37 -0700141 private static final String ATTR_LASTDESCRIPTION = "last_description";
142 private static final String ATTR_LASTTIMEMOVED = "last_time_moved";
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700143 private static final String ATTR_NEVERRELINQUISH = "never_relinquish_identity";
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700144 private static final String ATTR_TASK_AFFILIATION = "task_affiliation";
Craig Mautnera228ae92014-07-09 05:44:55 -0700145 private static final String ATTR_PREV_AFFILIATION = "prev_affiliation";
146 private static final String ATTR_NEXT_AFFILIATION = "next_affiliation";
Winson Chungec396d62014-08-06 17:08:00 -0700147 private static final String ATTR_TASK_AFFILIATION_COLOR = "task_affiliation_color";
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700148 private static final String ATTR_CALLING_UID = "calling_uid";
149 private static final String ATTR_CALLING_PACKAGE = "calling_package";
Winson Chungd3395382016-12-13 11:49:09 -0800150 private static final String ATTR_SUPPORTS_PICTURE_IN_PICTURE = "supports_picture_in_picture";
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800151 private static final String ATTR_RESIZE_MODE = "resize_mode";
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700152 private static final String ATTR_PRIVILEGED = "privileged";
Wale Ogunwale706ed792015-08-02 10:29:44 -0700153 private static final String ATTR_NON_FULLSCREEN_BOUNDS = "non_fullscreen_bounds";
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700154 private static final String ATTR_MIN_WIDTH = "min_width";
155 private static final String ATTR_MIN_HEIGHT = "min_height";
Wale Ogunwale625ed0c2016-10-18 08:50:31 -0700156 private static final String ATTR_PERSIST_TASK_VERSION = "persist_task_version";
Andrii Kulian18d75122016-03-27 20:20:28 -0700157
Wale Ogunwale625ed0c2016-10-18 08:50:31 -0700158 // Current version of the task record we persist. Used to check if we need to run any upgrade
159 // code.
160 private static final int PERSIST_TASK_VERSION = 1;
Craig Mautner21d24a22014-04-23 11:45:37 -0700161 private static final String TASK_THUMBNAIL_SUFFIX = "_task_thumbnail";
162
Wale Ogunwale18795a22014-12-03 11:38:33 -0800163 static final int INVALID_TASK_ID = -1;
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700164 private static final int INVALID_MIN_SIZE = -1;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800165
Winson Chung74666102017-02-22 17:49:24 -0800166 /**
167 * The modes to control how the stack is moved to the front when calling
168 * {@link TaskRecord#reparent}.
169 */
170 @Retention(RetentionPolicy.SOURCE)
171 @IntDef({
172 REPARENT_MOVE_STACK_TO_FRONT,
173 REPARENT_KEEP_STACK_AT_FRONT,
174 REPARENT_LEAVE_STACK_IN_PLACE
175 })
176 public @interface ReparentMoveStackMode {}
177 // Moves the stack to the front if it was not at the front
178 public static final int REPARENT_MOVE_STACK_TO_FRONT = 0;
179 // Only moves the stack to the front if it was focused or front most already
180 public static final int REPARENT_KEEP_STACK_AT_FRONT = 1;
181 // Do not move the stack as a part of reparenting
182 public static final int REPARENT_LEAVE_STACK_IN_PLACE = 2;
183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 final int taskId; // Unique identifier for this task.
Dianne Hackborn79228822014-09-16 11:11:23 -0700185 String affinity; // The affinity name for this task, or null; may change identity.
186 String rootAffinity; // Initial base affinity, or null; does not change from initial root.
Dianne Hackborn91097de2014-04-04 18:02:06 -0700187 final IVoiceInteractionSession voiceSession; // Voice interaction session driving task
188 final IVoiceInteractor voiceInteractor; // Associated interactor to provide to app
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 Intent intent; // The original intent that started the task.
190 Intent affinityIntent; // Intent of affinity-moved activity that started this task.
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700191 int effectiveUid; // The current effective uid of the identity of this task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 ComponentName origActivity; // The non-alias activity component of the intent.
193 ComponentName realActivity; // The actual activity component that started the task.
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +0000194 boolean realActivitySuspended; // True if the actual activity component that started the
195 // task is suspended.
Winson Chung36f3f032016-09-08 23:29:43 +0000196 long firstActiveTime; // First time this task was active.
197 long lastActiveTime; // Last time this task was active, including sleep.
Dianne Hackborn852975d2014-08-22 17:42:43 -0700198 boolean inRecents; // Actually in the recents list?
199 boolean isAvailable; // Is the activity available to be launched?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 boolean rootWasReset; // True if the intent at the root of the task had
201 // the FLAG_ACTIVITY_RESET_TASK_IF_NEEDED flag.
Dianne Hackborn13420f22014-07-18 15:43:56 -0700202 boolean autoRemoveRecents; // If true, we should automatically remove the task from
203 // recents when activity finishes
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700204 boolean askedCompatMode;// Have asked the user about compat mode for this task.
Dianne Hackbornd38aed82014-06-10 21:36:35 -0700205 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 -0800206
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700207 String stringName; // caching of toString() result.
Dianne Hackborn9da2d402012-03-15 13:43:08 -0700208 int userId; // user for which this task was created
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800209 boolean mUserSetupComplete; // The user set-up is complete as of the last time the task activity
210 // was changed.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800211
212 int numFullscreen; // Number of fullscreen activities.
213
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800214 int mResizeMode; // The resize mode of this task and its activities.
215 // Based on the {@link ActivityInfo#resizeMode} of the root activity.
Wale Ogunwale069bbd32017-02-03 07:58:14 -0800216 private boolean mSupportsPictureInPicture; // Whether or not this task and its activities
217 // support PiP. Based on the {@link ActivityInfo#FLAG_SUPPORTS_PICTURE_IN_PICTURE} flag
218 // of the root activity.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -0800219 boolean mTemporarilyUnresizable; // Separate flag from mResizeMode used to suppress resize
220 // changes on a temporary basis.
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700221 private int mLockTaskMode; // Which tasklock mode to launch this task in. One of
222 // ActivityManager.LOCK_TASK_LAUNCH_MODE_*
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700223 private boolean mPrivileged; // The root activity application of this task holds
224 // privileged permissions.
225
Craig Mautner15df08a2015-04-01 12:17:18 -0700226 /** Can't be put in lockTask mode. */
227 final static int LOCK_TASK_AUTH_DONT_LOCK = 0;
Benjamin Franz469dd582015-06-09 14:24:36 +0100228 /** Can enter app pinning with user approval. Can never start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700229 final static int LOCK_TASK_AUTH_PINNABLE = 1;
230 /** Starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing lockTask task. */
231 final static int LOCK_TASK_AUTH_LAUNCHABLE = 2;
Benjamin Franz469dd582015-06-09 14:24:36 +0100232 /** Can enter lockTask without user approval. Can start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700233 final static int LOCK_TASK_AUTH_WHITELISTED = 3;
Benjamin Franz469dd582015-06-09 14:24:36 +0100234 /** Priv-app that starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing
235 * lockTask task. */
236 final static int LOCK_TASK_AUTH_LAUNCHABLE_PRIV = 4;
Craig Mautner15df08a2015-04-01 12:17:18 -0700237 int mLockTaskAuth = LOCK_TASK_AUTH_PINNABLE;
238
239 int mLockTaskUid = -1; // The uid of the application that called startLockTask().
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800240
Winson Chung03a9bae2014-05-02 09:56:12 -0700241 // This represents the last resolved activity values for this task
242 // NOTE: This value needs to be persisted with each task
Craig Mautner648f69b2014-09-18 14:16:26 -0700243 TaskDescription lastTaskDescription = new TaskDescription();
Winson Chung03a9bae2014-05-02 09:56:12 -0700244
Craig Mautnerd2328952013-03-05 12:46:26 -0800245 /** List of all activities in the task arranged in history order */
Craig Mautner21d24a22014-04-23 11:45:37 -0700246 final ArrayList<ActivityRecord> mActivities;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800247
Andrii Kulian02b7a832016-10-06 23:11:56 -0700248 /** Current stack. Setter must always be used to update the value. */
249 private ActivityStack mStack;
Craig Mautnerd2328952013-03-05 12:46:26 -0800250
Craig Mautner2c1faed2013-07-23 12:56:02 -0700251 /** Takes on same set of values as ActivityRecord.mActivityType */
Craig Mautner21d24a22014-04-23 11:45:37 -0700252 int taskType;
Craig Mautner1602ec22013-05-12 10:24:27 -0700253
Craig Mautner21d24a22014-04-23 11:45:37 -0700254 /** Takes on same value as first root activity */
255 boolean isPersistable = false;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700256 int maxRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700257
258 /** Only used for persistable tasks, otherwise 0. The last time this task was moved. Used for
259 * determining the order when restoring. Sign indicates whether last task movement was to front
260 * (positive) or back (negative). Absolute value indicates time. */
261 long mLastTimeMoved = System.currentTimeMillis();
262
Craig Mautner84984fa2014-06-19 11:19:20 -0700263 /** Indication of what to run next when task exits. Use ActivityRecord types.
264 * ActivityRecord.APPLICATION_ACTIVITY_TYPE indicates to resume the task below this one in the
265 * task stack. */
266 private int mTaskToReturnTo = APPLICATION_ACTIVITY_TYPE;
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700267
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700268 /** If original intent did not allow relinquishing task identity, save that information */
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700269 private boolean mNeverRelinquishIdentity = true;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700270
Craig Mautner362449a2014-06-20 14:04:39 -0700271 // Used in the unique case where we are clearing the task in order to reuse it. In that case we
272 // do not want to delete the stack when the task goes empty.
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -0800273 private boolean mReuseTask = false;
Craig Mautner362449a2014-06-20 14:04:39 -0700274
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700275 private final String mFilename;
276 CharSequence lastDescription; // Last description captured for this item.
277
Craig Mautnera228ae92014-07-09 05:44:55 -0700278 int mAffiliatedTaskId; // taskId of parent affiliation or self if no parent.
Winson Chungec396d62014-08-06 17:08:00 -0700279 int mAffiliatedTaskColor; // color of the parent task affiliation.
Craig Mautnera228ae92014-07-09 05:44:55 -0700280 TaskRecord mPrevAffiliate; // previous task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800281 int mPrevAffiliateTaskId = INVALID_TASK_ID; // previous id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700282 TaskRecord mNextAffiliate; // next task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800283 int mNextAffiliateTaskId = INVALID_TASK_ID; // next id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700284
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700285 // For relaunching the task from recents as though it was launched by the original launcher.
286 int mCallingUid;
287 String mCallingPackage;
288
Craig Mautner21d24a22014-04-23 11:45:37 -0700289 final ActivityManagerService mService;
290
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700291 // Whether or not this task covers the entire screen; by default tasks are fullscreen.
292 boolean mFullscreen = true;
293
294 // Bounds of the Task. null for fullscreen tasks.
295 Rect mBounds = null;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -0800296 private final Rect mTmpStableBounds = new Rect();
297 private final Rect mTmpNonDecorBounds = new Rect();
Wale Ogunwale9a08f822016-02-17 19:03:58 -0800298 private final Rect mTmpRect = new Rect();
Jorim Jaggi0a932142016-02-01 17:42:25 -0800299
Wale Ogunwale706ed792015-08-02 10:29:44 -0700300 // Last non-fullscreen bounds the task was launched in or resized to.
301 // The information is persisted and used to determine the appropriate stack to launch the
302 // task into on restore.
303 Rect mLastNonFullscreenBounds = null;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700304 // Minimal width and height of this task when it's resizeable. -1 means it should use the
305 // default minimal width/height.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700306 int mMinWidth;
307 int mMinHeight;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700308
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700309 // Ranking (from top) of this task among all visible tasks. (-1 means it's not visible)
310 // This number will be assigned when we evaluate OOM scores for all visible tasks.
311 int mLayerRank = -1;
312
Andrii Kulian1779e612016-10-12 21:58:25 -0700313 /** Helper object used for updating override configuration. */
314 private Configuration mTmpConfig = new Configuration();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700315
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800316 private TaskWindowContainerController mWindowContainerController;
317
Craig Mautner21d24a22014-04-23 11:45:37 -0700318 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Wale Ogunwale72919d22016-12-08 18:58:50 -0800319 IVoiceInteractionSession _voiceSession, IVoiceInteractor _voiceInteractor, int type) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700320 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700321 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
322 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800323 userId = UserHandle.getUserId(info.applicationInfo.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 taskId = _taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700325 mAffiliatedTaskId = _taskId;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700326 voiceSession = _voiceSession;
327 voiceInteractor = _voiceInteractor;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700328 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800329 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700330 mCallingUid = info.applicationInfo.uid;
331 mCallingPackage = info.packageName;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800332 taskType = type;
Martijn Coenend4a69702014-06-30 11:12:17 -0700333 setIntent(_intent, info);
Andrii Kulian2e751b82016-03-16 16:59:32 -0700334 setMinDimensions(info);
Winson730bf062016-03-31 18:04:56 -0700335 touchActiveTime();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700336 mService.mTaskChangeNotificationController.notifyTaskCreated(_taskId, realActivity);
Craig Mautner21d24a22014-04-23 11:45:37 -0700337 }
338
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700339 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200340 TaskDescription _taskDescription) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700341 mService = service;
342 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
343 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800344 userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700345 taskId = _taskId;
346 mAffiliatedTaskId = _taskId;
347 voiceSession = null;
348 voiceInteractor = null;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700349 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800350 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700351 mCallingUid = info.applicationInfo.uid;
352 mCallingPackage = info.packageName;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700353 setIntent(_intent, info);
Andrii Kulian2e751b82016-03-16 16:59:32 -0700354 setMinDimensions(info);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700355
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700356 isPersistable = true;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700357 // Clamp to [1, max].
358 maxRecents = Math.min(Math.max(info.maxRecents, 1),
359 ActivityManager.getMaxAppRecentsLimitStatic());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700360
361 taskType = APPLICATION_ACTIVITY_TYPE;
362 mTaskToReturnTo = HOME_ACTIVITY_TYPE;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700363 lastTaskDescription = _taskDescription;
Winson730bf062016-03-31 18:04:56 -0700364 touchActiveTime();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700365 mService.mTaskChangeNotificationController.notifyTaskCreated(_taskId, realActivity);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700366 }
367
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800368 private TaskRecord(ActivityManagerService service, int _taskId, Intent _intent,
369 Intent _affinityIntent, String _affinity, String _rootAffinity,
370 ComponentName _realActivity, ComponentName _origActivity, boolean _rootWasReset,
371 boolean _autoRemoveRecents, boolean _askedCompatMode, int _taskType, int _userId,
372 int _effectiveUid, String _lastDescription, ArrayList<ActivityRecord> activities,
373 long _firstActiveTime, long _lastActiveTime, long lastTimeMoved,
374 boolean neverRelinquishIdentity, TaskDescription _lastTaskDescription,
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200375 int taskAffiliation, int prevTaskId, int nextTaskId, int taskAffiliationColor,
376 int callingUid, String callingPackage, int resizeMode, boolean supportsPictureInPicture,
377 boolean privileged, boolean _realActivitySuspended, boolean userSetupComplete,
378 int minWidth, int minHeight) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700379 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700380 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
381 TaskPersister.IMAGE_EXTENSION;
Craig Mautner21d24a22014-04-23 11:45:37 -0700382 taskId = _taskId;
383 intent = _intent;
384 affinityIntent = _affinityIntent;
385 affinity = _affinity;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800386 rootAffinity = _rootAffinity;
Craig Mautner21d24a22014-04-23 11:45:37 -0700387 voiceSession = null;
388 voiceInteractor = null;
389 realActivity = _realActivity;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800390 realActivitySuspended = _realActivitySuspended;
Craig Mautner21d24a22014-04-23 11:45:37 -0700391 origActivity = _origActivity;
392 rootWasReset = _rootWasReset;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700393 isAvailable = true;
Dianne Hackborn13420f22014-07-18 15:43:56 -0700394 autoRemoveRecents = _autoRemoveRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700395 askedCompatMode = _askedCompatMode;
396 taskType = _taskType;
Craig Mautner84984fa2014-06-19 11:19:20 -0700397 mTaskToReturnTo = HOME_ACTIVITY_TYPE;
Craig Mautner21d24a22014-04-23 11:45:37 -0700398 userId = _userId;
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800399 mUserSetupComplete = userSetupComplete;
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700400 effectiveUid = _effectiveUid;
Winson Chungffa2ec62014-07-03 15:54:42 -0700401 firstActiveTime = _firstActiveTime;
Winson Chungf1fbd772014-06-24 18:06:58 -0700402 lastActiveTime = _lastActiveTime;
Craig Mautner21d24a22014-04-23 11:45:37 -0700403 lastDescription = _lastDescription;
404 mActivities = activities;
405 mLastTimeMoved = lastTimeMoved;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700406 mNeverRelinquishIdentity = neverRelinquishIdentity;
Winson Chung2cb86c72014-06-25 12:03:30 -0700407 lastTaskDescription = _lastTaskDescription;
Craig Mautnera228ae92014-07-09 05:44:55 -0700408 mAffiliatedTaskId = taskAffiliation;
Winson Chungec396d62014-08-06 17:08:00 -0700409 mAffiliatedTaskColor = taskAffiliationColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700410 mPrevAffiliateTaskId = prevTaskId;
411 mNextAffiliateTaskId = nextTaskId;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700412 mCallingUid = callingUid;
413 mCallingPackage = callingPackage;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800414 mResizeMode = resizeMode;
Winson Chungd3395382016-12-13 11:49:09 -0800415 mSupportsPictureInPicture = supportsPictureInPicture;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700416 mPrivileged = privileged;
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700417 mMinWidth = minWidth;
418 mMinHeight = minHeight;
Yorke Leebd54c2a2016-10-25 13:49:23 -0700419 mService.mTaskChangeNotificationController.notifyTaskCreated(_taskId, realActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 }
421
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800422 TaskWindowContainerController getWindowContainerController() {
423 return mWindowContainerController;
424 }
425
426 void createWindowContainer(boolean onTop, boolean showForAllUsers) {
427 if (mWindowContainerController != null) {
428 throw new IllegalArgumentException("Window container=" + mWindowContainerController
429 + " already created for task=" + this);
430 }
431
432 final Rect bounds = updateOverrideConfigurationFromLaunchBounds();
433 final Configuration overrideConfig = getOverrideConfiguration();
Bryce Lee04ab3462017-04-10 15:06:33 -0700434 setWindowContainerController(new TaskWindowContainerController(taskId, this,
Wale Ogunwale1666e312016-12-16 11:27:18 -0800435 getStack().getWindowContainerController(), userId, bounds, overrideConfig,
Wale Ogunwale069bbd32017-02-03 07:58:14 -0800436 mResizeMode, mSupportsPictureInPicture, isHomeTask(), onTop, showForAllUsers,
Bryce Lee04ab3462017-04-10 15:06:33 -0700437 lastTaskDescription));
438 }
439
440 /**
441 * Should only be invoked from {@link #createWindowContainer(boolean, boolean)}.
442 */
443 @VisibleForTesting
444 protected void setWindowContainerController(TaskWindowContainerController controller) {
445 if (mWindowContainerController != null) {
446 throw new IllegalArgumentException("Window container=" + mWindowContainerController
447 + " already created for task=" + this);
448 }
449
450 mWindowContainerController = controller;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800451 }
452
453 void removeWindowContainer() {
Benjamin Franza83859f2017-07-03 16:34:14 +0100454 mService.mLockTaskController.removeLockedTask(this);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800455 mWindowContainerController.removeContainer();
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700456 if (!getWindowConfiguration().persistTaskBounds()) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800457 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
458 // default configuration the next time it launches.
459 updateOverrideConfiguration(null);
460 }
461 mService.mTaskChangeNotificationController.notifyTaskRemoved(taskId);
462 mWindowContainerController = null;
463 }
464
Jorim Jaggifb9d78a2017-01-05 18:57:12 +0100465 @Override
466 public void onSnapshotChanged(TaskSnapshot snapshot) {
467 mService.mTaskChangeNotificationController.notifyTaskSnapshotChanged(taskId, snapshot);
468 }
469
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800470 void setResizeMode(int resizeMode) {
471 if (mResizeMode == resizeMode) {
472 return;
473 }
474 mResizeMode = resizeMode;
475 mWindowContainerController.setResizeable(resizeMode);
476 mService.mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
477 mService.mStackSupervisor.resumeFocusedStackTopActivityLocked();
478 }
479
480 void setTaskDockedResizing(boolean resizing) {
481 mWindowContainerController.setTaskDockedResizing(resizing);
482 }
483
Wale Ogunwale1666e312016-12-16 11:27:18 -0800484 // TODO: Consolidate this with the resize() method below.
485 @Override
486 public void requestResize(Rect bounds, int resizeMode) {
487 mService.resizeTask(taskId, bounds, resizeMode);
488 }
489
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800490 boolean resize(Rect bounds, int resizeMode, boolean preserveWindow, boolean deferResume) {
491 if (!isResizeable()) {
492 Slog.w(TAG, "resizeTask: task " + this + " not resizeable.");
493 return true;
494 }
495
496 // If this is a forced resize, let it go through even if the bounds is not changing,
497 // as we might need a relayout due to surface size change (to/from fullscreen).
498 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
499 if (Objects.equals(mBounds, bounds) && !forced) {
500 // Nothing to do here...
501 return true;
502 }
503 bounds = validateBounds(bounds);
504
505 if (mWindowContainerController == null) {
506 // Task doesn't exist in window manager yet (e.g. was restored from recents).
507 // All we can do for now is update the bounds so it can be used when the task is
508 // added to window manager.
509 updateOverrideConfiguration(bounds);
510 if (getStackId() != FREEFORM_WORKSPACE_STACK_ID) {
511 // re-restore the task so it can have the proper stack association.
512 mService.mStackSupervisor.restoreRecentTaskLocked(this,
513 FREEFORM_WORKSPACE_STACK_ID);
514 }
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 */
563 boolean reparent(int preferredStackId, boolean toTop, @ReparentMoveStackMode int moveStackMode,
564 boolean animate, boolean deferResume, String reason) {
565 return reparent(preferredStackId, toTop ? MAX_VALUE : 0, moveStackMode, animate,
Winson Chung5af42fc2017-03-24 17:11:33 -0700566 deferResume, true /* schedulePictureInPictureModeChange */, reason);
567 }
568
569 /**
570 * Convenience method to reparent a task to the top or bottom position of the stack, with
571 * an option to skip scheduling the picture-in-picture mode change.
572 */
573 boolean reparent(int preferredStackId, boolean toTop, @ReparentMoveStackMode int moveStackMode,
574 boolean animate, boolean deferResume, boolean schedulePictureInPictureModeChange,
575 String reason) {
576 return reparent(preferredStackId, toTop ? MAX_VALUE : 0, moveStackMode, animate,
577 deferResume, schedulePictureInPictureModeChange, reason);
578 }
579
580 /**
581 * Convenience method to reparent a task to a specific position of the stack.
582 */
583 boolean reparent(int preferredStackId, int position, @ReparentMoveStackMode int moveStackMode,
584 boolean animate, boolean deferResume, String reason) {
585 return reparent(preferredStackId, position, moveStackMode, animate, deferResume,
586 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 *
592 * @param preferredStackId the stack id of the target stack to move this task
593 * @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 */
606 boolean reparent(int preferredStackId, int position, @ReparentMoveStackMode int moveStackMode,
Winson Chung5af42fc2017-03-24 17:11:33 -0700607 boolean animate, boolean deferResume, boolean schedulePictureInPictureModeChange,
608 String reason) {
Winson Chung74666102017-02-22 17:49:24 -0800609 final ActivityStackSupervisor supervisor = mService.mStackSupervisor;
610 final WindowManagerService windowManager = mService.mWindowManager;
611 final ActivityStack sourceStack = getStack();
612 final ActivityStack toStack = supervisor.getReparentTargetStack(this, preferredStackId,
613 position == MAX_VALUE);
614 if (toStack == sourceStack) {
615 return false;
616 }
617
618 final int sourceStackId = getStackId();
619 final int stackId = toStack.getStackId();
620 final ActivityRecord topActivity = getTopActivity();
621
622 final boolean mightReplaceWindow = StackId.replaceWindowsOnTaskMove(sourceStackId, stackId)
623 && topActivity != null;
624 if (mightReplaceWindow) {
625 // We are about to relaunch the activity because its configuration changed due to
626 // being maximized, i.e. size change. The activity will first remove the old window
627 // and then add a new one. This call will tell window manager about this, so it can
628 // preserve the old window until the new one is drawn. This prevents having a gap
629 // between the removal and addition, in which no window is visible. We also want the
630 // entrance of the new window to be properly animated.
631 // Note here we always set the replacing window first, as the flags might be needed
632 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
633 windowManager.setWillReplaceWindow(topActivity.appToken, animate);
634 }
635
636 windowManager.deferSurfaceLayout();
637 boolean kept = true;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800638 try {
Winson Chung74666102017-02-22 17:49:24 -0800639 final ActivityRecord r = topRunningActivityLocked();
Winson Chung95f8f0e2017-03-24 09:20:17 -0700640 final boolean wasFocused = r != null && supervisor.isFocusedStack(sourceStack)
Winson Chung74666102017-02-22 17:49:24 -0800641 && (topRunningActivityLocked() == r);
Winson Chung95f8f0e2017-03-24 09:20:17 -0700642 final boolean wasResumed = r != null && sourceStack.mResumedActivity == r;
643 final boolean wasPaused = r != null && sourceStack.mPausingActivity == r;
Winson Chung74666102017-02-22 17:49:24 -0800644
645 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
646 // Whenever we are moving the top activity from the front stack we want to make sure to
647 // move the stack to the front.
Winson Chung95f8f0e2017-03-24 09:20:17 -0700648 final boolean wasFront = r != null && supervisor.isFrontStackOnDisplay(sourceStack)
Winson Chung74666102017-02-22 17:49:24 -0800649 && (sourceStack.topRunningActivityLocked() == r);
650
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800651 // Adjust the position for the new parent stack as needed.
Winson Chung74666102017-02-22 17:49:24 -0800652 position = toStack.getAdjustedPositionForTask(this, position, null /* starting */);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800653
654 // Must reparent first in window manager to avoid a situation where AM can delete the
655 // we are coming from in WM before we reparent because it became empty.
Wale Ogunwale2719cc12017-04-14 09:45:27 -0700656 mWindowContainerController.reparent(toStack.getWindowContainerController(), position,
657 moveStackMode == REPARENT_MOVE_STACK_TO_FRONT);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800658
Wale Ogunwale56d8d162017-05-30 11:12:20 -0700659 final boolean moveStackToFront = moveStackMode == REPARENT_MOVE_STACK_TO_FRONT
660 || (moveStackMode == REPARENT_KEEP_STACK_AT_FRONT && (wasFocused || wasFront));
Winson Chung74666102017-02-22 17:49:24 -0800661 // Move the task
Wale Ogunwale56d8d162017-05-30 11:12:20 -0700662 sourceStack.removeTask(this, reason, moveStackToFront
663 ? REMOVE_TASK_MODE_MOVING_TO_TOP : REMOVE_TASK_MODE_MOVING);
Winson Chung5af42fc2017-03-24 17:11:33 -0700664 toStack.addTask(this, position, false /* schedulePictureInPictureModeChange */, reason);
Winson Chung74666102017-02-22 17:49:24 -0800665
Winson Chung5af42fc2017-03-24 17:11:33 -0700666 if (schedulePictureInPictureModeChange) {
667 // Notify of picture-in-picture mode changes
668 supervisor.scheduleUpdatePictureInPictureModeIfNeeded(this, sourceStack);
669 }
Winson Chung74666102017-02-22 17:49:24 -0800670
671 // TODO: Ensure that this is actually necessary here
672 // Notify the voice session if required
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800673 if (voiceSession != null) {
674 try {
675 voiceSession.taskStarted(intent, taskId);
676 } catch (RemoteException e) {
677 }
678 }
Winson Chung74666102017-02-22 17:49:24 -0800679
680 // If the task had focus before (or we're requested to move focus), move focus to the
681 // new stack by moving the stack to the front.
Winson Chung95f8f0e2017-03-24 09:20:17 -0700682 if (r != null) {
683 toStack.moveToFrontAndResumeStateIfNeeded(r, moveStackToFront, wasResumed,
684 wasPaused, reason);
685 }
Winson Chung74666102017-02-22 17:49:24 -0800686 if (!animate) {
687 toStack.mNoAnimActivities.add(topActivity);
688 }
689
690 // We might trigger a configuration change. Save the current task bounds for freezing.
691 // TODO: Should this call be moved inside the resize method in WM?
692 toStack.prepareFreezingTaskBounds();
693
694 // Make sure the task has the appropriate bounds/size for the stack it is in.
695 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID
696 && !Objects.equals(mBounds, toStack.mBounds)) {
697 kept = resize(toStack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow,
698 deferResume);
699 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
700 Rect bounds = getLaunchBounds();
701 if (bounds == null) {
702 toStack.layoutTaskInStack(this, null);
703 bounds = mBounds;
704 }
705 kept = resize(bounds, RESIZE_MODE_FORCED, !mightReplaceWindow, deferResume);
706 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Matthew Ng330757d2017-02-28 14:19:17 -0800707 if (stackId == DOCKED_STACK_ID && moveStackMode == REPARENT_KEEP_STACK_AT_FRONT) {
708 // Move recents to front so it is not behind home stack when going into docked
709 // mode
710 mService.mStackSupervisor.moveRecentsStackToFront(reason);
711 }
Winson Chung74666102017-02-22 17:49:24 -0800712 kept = resize(toStack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow,
713 deferResume);
714 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800715 } finally {
Winson Chung74666102017-02-22 17:49:24 -0800716 windowManager.continueSurfaceLayout();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800717 }
Winson Chung74666102017-02-22 17:49:24 -0800718
719 if (mightReplaceWindow) {
720 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
721 // window), we need to clear the replace window settings. Otherwise, we schedule a
722 // timeout to remove the old window if the replacing window is not coming in time.
723 windowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
724 }
725
726 if (!deferResume) {
727 // The task might have already been running and its visibility needs to be synchronized
728 // with the visibility of the stack / windows.
729 supervisor.ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
730 supervisor.resumeFocusedStackTopActivityLocked();
731 }
732
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700733 // TODO: Handle incorrect request to move before the actual move, not after.
734 supervisor.handleNonResizableTaskIfNeeded(this, preferredStackId, DEFAULT_DISPLAY, stackId);
Winson Chung74666102017-02-22 17:49:24 -0800735
Matthew Ng330757d2017-02-28 14:19:17 -0800736 boolean successful = (preferredStackId == stackId);
737 if (successful && stackId == DOCKED_STACK_ID) {
738 // If task moved to docked stack - show recents if needed.
739 mService.mWindowManager.showRecentApps(false /* fromHome */);
740 }
741 return successful;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800742 }
743
744 void cancelWindowTransition() {
745 mWindowContainerController.cancelWindowTransition();
746 }
747
748 void cancelThumbnailTransition() {
749 mWindowContainerController.cancelThumbnailTransition();
750 }
751
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100752 /**
753 * DO NOT HOLD THE ACTIVITY MANAGER LOCK WHEN CALLING THIS METHOD!
754 */
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100755 TaskSnapshot getSnapshot(boolean reducedResolution) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100756
757 // TODO: Move this to {@link TaskWindowContainerController} once recent tasks are more
758 // synchronized between AM and WM.
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100759 return mService.mWindowManager.getTaskSnapshot(taskId, userId, reducedResolution);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800760 }
761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 void touchActiveTime() {
Winson Chung36f3f032016-09-08 23:29:43 +0000763 lastActiveTime = System.currentTimeMillis();
Winson Chungffa2ec62014-07-03 15:54:42 -0700764 if (firstActiveTime == 0) {
765 firstActiveTime = lastActiveTime;
766 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 long getInactiveDuration() {
Winson Chung36f3f032016-09-08 23:29:43 +0000770 return System.currentTimeMillis() - lastActiveTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700772
Winson Chungfee26772014-08-05 12:21:52 -0700773 /** Sets the original intent, and the calling uid and package. */
774 void setIntent(ActivityRecord r) {
Winson Chungfee26772014-08-05 12:21:52 -0700775 mCallingUid = r.launchedFromUid;
776 mCallingPackage = r.launchedFromPackage;
Craig Mautner15df08a2015-04-01 12:17:18 -0700777 setIntent(r.intent, r.info);
Winson Chungfee26772014-08-05 12:21:52 -0700778 }
779
780 /** Sets the original intent, _without_ updating the calling uid or package. */
781 private void setIntent(Intent _intent, ActivityInfo info) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700782 if (intent == null) {
783 mNeverRelinquishIdentity =
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700784 (info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700785 } else if (mNeverRelinquishIdentity) {
786 return;
787 }
788
789 affinity = info.taskAffinity;
Dianne Hackborn79228822014-09-16 11:11:23 -0700790 if (intent == null) {
791 // If this task already has an intent associated with it, don't set the root
792 // affinity -- we don't want it changing after initially set, but the initially
793 // set value may be null.
794 rootAffinity = affinity;
795 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700796 effectiveUid = info.applicationInfo.uid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700797 stringName = null;
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 if (info.targetActivity == null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800800 if (_intent != null) {
801 // If this Intent has a selector, we want to clear it for the
802 // recent task since it is not relevant if the user later wants
803 // to re-launch the app.
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700804 if (_intent.getSelector() != null || _intent.getSourceBounds() != null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800805 _intent = new Intent(_intent);
806 _intent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700807 _intent.setSourceBounds(null);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800808 }
809 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700810 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Setting Intent of " + this + " to " + _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 intent = _intent;
812 realActivity = _intent != null ? _intent.getComponent() : null;
813 origActivity = null;
814 } else {
815 ComponentName targetComponent = new ComponentName(
816 info.packageName, info.targetActivity);
817 if (_intent != null) {
818 Intent targetIntent = new Intent(_intent);
819 targetIntent.setComponent(targetComponent);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800820 targetIntent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700821 targetIntent.setSourceBounds(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700822 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700823 "Setting Intent of " + this + " to target " + targetIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 intent = targetIntent;
825 realActivity = targetComponent;
826 origActivity = _intent.getComponent();
827 } else {
828 intent = null;
829 realActivity = targetComponent;
830 origActivity = new ComponentName(info.packageName, info.name);
831 }
832 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700833
Craig Mautner47b20ba2014-09-17 17:23:44 -0700834 final int intentFlags = intent == null ? 0 : intent.getFlags();
835 if ((intentFlags & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 // Once we are set to an Intent with this flag, we count this
837 // task as having a true root activity.
838 rootWasReset = true;
839 }
Dianne Hackborn09233282014-04-30 11:33:59 -0700840 userId = UserHandle.getUserId(info.applicationInfo.uid);
Winson Chung36f3f032016-09-08 23:29:43 +0000841 mUserSetupComplete = Settings.Secure.getIntForUser(mService.mContext.getContentResolver(),
842 USER_SETUP_COMPLETE, 0, userId) != 0;
Craig Mautner41db4a72014-05-07 17:20:56 -0700843 if ((info.flags & ActivityInfo.FLAG_AUTO_REMOVE_FROM_RECENTS) != 0) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700844 // If the activity itself has requested auto-remove, then just always do it.
845 autoRemoveRecents = true;
Wale Ogunwale843bfb92015-03-27 11:06:48 -0700846 } else if ((intentFlags & (FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_RETAIN_IN_RECENTS))
847 == FLAG_ACTIVITY_NEW_DOCUMENT) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700848 // If the caller has not asked for the document to be retained, then we may
849 // want to turn on auto-remove, depending on whether the target has set its
850 // own document launch mode.
851 if (info.documentLaunchMode != ActivityInfo.DOCUMENT_LAUNCH_NONE) {
852 autoRemoveRecents = false;
853 } else {
854 autoRemoveRecents = true;
855 }
856 } else {
857 autoRemoveRecents = false;
Craig Mautner41db4a72014-05-07 17:20:56 -0700858 }
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800859 mResizeMode = info.resizeMode;
Winson Chungd3395382016-12-13 11:49:09 -0800860 mSupportsPictureInPicture = info.supportsPictureInPicture();
Craig Mautner15df08a2015-04-01 12:17:18 -0700861 mLockTaskMode = info.lockTaskLaunchMode;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700862 mPrivileged = (info.applicationInfo.privateFlags & PRIVATE_FLAG_PRIVILEGED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -0700863 setLockTaskAuth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800865
Andrii Kulian2e751b82016-03-16 16:59:32 -0700866 /** Sets the original minimal width and height. */
867 private void setMinDimensions(ActivityInfo info) {
868 if (info != null && info.windowLayout != null) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700869 mMinWidth = info.windowLayout.minWidth;
870 mMinHeight = info.windowLayout.minHeight;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700871 } else {
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700872 mMinWidth = INVALID_MIN_SIZE;
873 mMinHeight = INVALID_MIN_SIZE;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700874 }
875 }
876
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800877 /**
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700878 * Return true if the input activity has the same intent filter as the intent this task
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800879 * record is based on (normally the root activity intent).
880 */
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700881 boolean isSameIntentFilter(ActivityRecord r) {
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800882 final Intent intent = new Intent(r.intent);
883 // Correct the activity intent for aliasing. The task record intent will always be based on
884 // the real activity that will be launched not the alias, so we need to use an intent with
885 // the component name pointing to the real activity not the alias in the activity record.
886 intent.setComponent(r.realActivity);
887 return this.intent.filterEquals(intent);
888 }
889
Craig Mautner84984fa2014-06-19 11:19:20 -0700890 void setTaskToReturnTo(int taskToReturnTo) {
Wale Ogunwale673cbd22016-01-30 18:30:55 -0800891 mTaskToReturnTo = (taskToReturnTo == RECENTS_ACTIVITY_TYPE)
892 ? HOME_ACTIVITY_TYPE : taskToReturnTo;
Craig Mautner84984fa2014-06-19 11:19:20 -0700893 }
894
Winson Chung83471632016-12-13 11:02:12 -0800895 void setTaskToReturnTo(ActivityRecord source) {
896 if (source.isRecentsActivity()) {
897 setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
898 } else if (source.isAssistantActivity()) {
899 setTaskToReturnTo(ASSISTANT_ACTIVITY_TYPE);
900 }
901 }
902
Craig Mautner84984fa2014-06-19 11:19:20 -0700903 int getTaskToReturnTo() {
904 return mTaskToReturnTo;
905 }
906
Craig Mautnera228ae92014-07-09 05:44:55 -0700907 void setPrevAffiliate(TaskRecord prevAffiliate) {
908 mPrevAffiliate = prevAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800909 mPrevAffiliateTaskId = prevAffiliate == null ? INVALID_TASK_ID : prevAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700910 }
911
912 void setNextAffiliate(TaskRecord nextAffiliate) {
913 mNextAffiliate = nextAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800914 mNextAffiliateTaskId = nextAffiliate == null ? INVALID_TASK_ID : nextAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700915 }
916
Andrii Kulian02b7a832016-10-06 23:11:56 -0700917 ActivityStack getStack() {
918 return mStack;
919 }
920
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800921 /**
922 * Must be used for setting parent stack because it performs configuration updates.
923 * Must be called after adding task as a child to the stack.
924 */
Andrii Kulian02b7a832016-10-06 23:11:56 -0700925 void setStack(ActivityStack stack) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800926 if (stack != null && !stack.isInStackLocked(this)) {
927 throw new IllegalStateException("Task must be added as a Stack child first.");
928 }
Andrii Kulian02b7a832016-10-06 23:11:56 -0700929 mStack = stack;
Andrii Kulian1779e612016-10-12 21:58:25 -0700930 onParentChanged();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700931 }
932
933 /**
934 * @return Id of current stack, {@link INVALID_STACK_ID} if no stack is set.
935 */
936 int getStackId() {
937 return mStack != null ? mStack.mStackId : INVALID_STACK_ID;
938 }
939
Andrii Kulian1779e612016-10-12 21:58:25 -0700940 @Override
941 protected int getChildCount() {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700942 return mActivities.size();
Andrii Kulian1779e612016-10-12 21:58:25 -0700943 }
944
945 @Override
946 protected ConfigurationContainer getChildAt(int index) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700947 return mActivities.get(index);
Andrii Kulian1779e612016-10-12 21:58:25 -0700948 }
949
950 @Override
951 protected ConfigurationContainer getParent() {
952 return mStack;
953 }
954
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800955 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -0700956 protected void onParentChanged() {
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800957 super.onParentChanged();
958 mService.mStackSupervisor.updateUIDsPresentOnDisplay();
959 }
960
Craig Mautnera228ae92014-07-09 05:44:55 -0700961 // Close up recents linked list.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700962 private void closeRecentsChain() {
Craig Mautnera228ae92014-07-09 05:44:55 -0700963 if (mPrevAffiliate != null) {
964 mPrevAffiliate.setNextAffiliate(mNextAffiliate);
965 }
966 if (mNextAffiliate != null) {
967 mNextAffiliate.setPrevAffiliate(mPrevAffiliate);
968 }
969 setPrevAffiliate(null);
970 setNextAffiliate(null);
971 }
972
Winson Chung740c3ac2014-11-12 16:14:38 -0800973 void removedFromRecents() {
Dianne Hackborn852975d2014-08-22 17:42:43 -0700974 closeRecentsChain();
975 if (inRecents) {
976 inRecents = false;
Winson Chung740c3ac2014-11-12 16:14:38 -0800977 mService.notifyTaskPersisterLocked(this, false);
Dianne Hackborn852975d2014-08-22 17:42:43 -0700978 }
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100979
980 // TODO: Use window container controller once tasks are better synced between AM and WM
981 mService.mWindowManager.notifyTaskRemovedFromRecents(taskId, userId);
Dianne Hackborn852975d2014-08-22 17:42:43 -0700982 }
983
Craig Mautnera228ae92014-07-09 05:44:55 -0700984 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
985 closeRecentsChain();
986 mAffiliatedTaskId = taskToAffiliateWith.mAffiliatedTaskId;
Winson Chungec396d62014-08-06 17:08:00 -0700987 mAffiliatedTaskColor = taskToAffiliateWith.mAffiliatedTaskColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700988 // Find the end
989 while (taskToAffiliateWith.mNextAffiliate != null) {
990 final TaskRecord nextRecents = taskToAffiliateWith.mNextAffiliate;
991 if (nextRecents.mAffiliatedTaskId != mAffiliatedTaskId) {
992 Slog.e(TAG, "setTaskToAffiliateWith: nextRecents=" + nextRecents + " affilTaskId="
993 + nextRecents.mAffiliatedTaskId + " should be " + mAffiliatedTaskId);
994 if (nextRecents.mPrevAffiliate == taskToAffiliateWith) {
995 nextRecents.setPrevAffiliate(null);
996 }
997 taskToAffiliateWith.setNextAffiliate(null);
998 break;
999 }
1000 taskToAffiliateWith = nextRecents;
1001 }
1002 taskToAffiliateWith.setNextAffiliate(this);
1003 setPrevAffiliate(taskToAffiliateWith);
1004 setNextAffiliate(null);
1005 }
1006
Winson Chung1147c402014-05-14 11:05:00 -07001007 /** Returns the intent for the root activity for this task */
1008 Intent getBaseIntent() {
1009 return intent != null ? intent : affinityIntent;
1010 }
1011
Winson Chung942a85c2017-07-11 15:07:45 -07001012 /**
1013 * @return Whether there are only fullscreen activities in this task.
1014 */
1015 boolean containsOnlyFullscreenActivities() {
1016 for (int i = 0; i < mActivities.size(); i++) {
1017 final ActivityRecord r = mActivities.get(i);
1018 if (!r.finishing && !r.fullscreen) {
1019 return false;
1020 }
1021 }
1022 return true;
1023 }
1024
Winson Chung3b3f4642014-04-22 10:08:18 -07001025 /** Returns the first non-finishing activity from the root. */
1026 ActivityRecord getRootActivity() {
1027 for (int i = 0; i < mActivities.size(); i++) {
1028 final ActivityRecord r = mActivities.get(i);
1029 if (r.finishing) {
1030 continue;
1031 }
1032 return r;
1033 }
1034 return null;
1035 }
1036
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001037 ActivityRecord getTopActivity() {
1038 for (int i = mActivities.size() - 1; i >= 0; --i) {
1039 final ActivityRecord r = mActivities.get(i);
1040 if (r.finishing) {
1041 continue;
1042 }
1043 return r;
1044 }
1045 return null;
1046 }
1047
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001048 ActivityRecord topRunningActivityLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001049 if (mStack != null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001050 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1051 ActivityRecord r = mActivities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -07001052 if (!r.finishing && r.okToShowLocked()) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001053 return r;
1054 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001055 }
1056 }
1057 return null;
1058 }
1059
Jorim Jaggiea039a82017-08-02 14:37:49 +02001060 void getAllRunningVisibleActivitiesLocked(ArrayList<ActivityRecord> outActivities) {
1061 if (mStack != null) {
1062 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1063 ActivityRecord r = mActivities.get(activityNdx);
Jorim Jaggi02f1d2f2017-08-04 14:29:16 +02001064 if (!r.finishing && r.okToShowLocked() && r.visibleIgnoringKeyguard) {
Jorim Jaggiea039a82017-08-02 14:37:49 +02001065 outActivities.add(r);
1066 }
1067 }
1068 }
1069 }
1070
Wale Ogunwale3b232392016-05-13 15:37:13 -07001071 ActivityRecord topRunningActivityWithStartingWindowLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001072 if (mStack != null) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07001073 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1074 ActivityRecord r = mActivities.get(activityNdx);
1075 if (r.mStartingWindowState != STARTING_WINDOW_SHOWN
Chong Zhang87761972016-08-22 13:53:24 -07001076 || r.finishing || !r.okToShowLocked()) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07001077 continue;
1078 }
1079 return r;
1080 }
1081 }
1082 return null;
1083 }
1084
Chong Zhang87761972016-08-22 13:53:24 -07001085 boolean okToShowLocked() {
1086 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
1087 // okay to show the activity when locked.
1088 return mService.mStackSupervisor.isCurrentProfileLocked(userId)
1089 || topRunningActivityLocked() != null;
1090 }
1091
Craig Mautner3b475fe2013-12-16 15:58:31 -08001092 /** Call after activity movement or finish to make sure that frontOfTask is set correctly */
Bryce Leed71317c2017-02-07 14:27:22 -08001093 final void setFrontOfTask() {
1094 boolean foundFront = false;
Craig Mautner3b475fe2013-12-16 15:58:31 -08001095 final int numActivities = mActivities.size();
Craig Mautner704e40b2013-12-18 16:43:51 -08001096 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
Craig Mautner3b475fe2013-12-16 15:58:31 -08001097 final ActivityRecord r = mActivities.get(activityNdx);
1098 if (foundFront || r.finishing) {
1099 r.frontOfTask = false;
1100 } else {
1101 r.frontOfTask = true;
1102 // Set frontOfTask false for every following activity.
1103 foundFront = true;
1104 }
1105 }
Craig Mautner9587ee02014-06-23 15:00:10 +00001106 if (!foundFront && numActivities > 0) {
1107 // All activities of this task are finishing. As we ought to have a frontOfTask
1108 // activity, make the bottom activity front.
1109 mActivities.get(0).frontOfTask = true;
1110 }
Craig Mautner3b475fe2013-12-16 15:58:31 -08001111 }
1112
Craig Mautnerde4ef022013-04-07 19:01:33 -07001113 /**
Craig Mautner3b475fe2013-12-16 15:58:31 -08001114 * Reorder the history stack so that the passed activity is brought to the front.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001115 */
1116 final void moveActivityToFrontLocked(ActivityRecord newTop) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001117 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
1118 "Removing and adding activity " + newTop
1119 + " to stack at top callers=" + Debug.getCallers(4));
Craig Mautnerde4ef022013-04-07 19:01:33 -07001120
Craig Mautnerde4ef022013-04-07 19:01:33 -07001121 mActivities.remove(newTop);
1122 mActivities.add(newTop);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001123 updateEffectiveIntent();
Craig Mautner3b475fe2013-12-16 15:58:31 -08001124
Bryce Leed71317c2017-02-07 14:27:22 -08001125 setFrontOfTask();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001126 }
1127
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001128 void addActivityAtBottom(ActivityRecord r) {
Craig Mautner77878772013-03-04 19:46:24 -08001129 addActivityAtIndex(0, r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001130 }
1131
1132 void addActivityToTop(ActivityRecord r) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001133 addActivityAtIndex(mActivities.size(), r);
1134 }
1135
Winson Chung30480042017-01-26 10:55:34 -08001136 /**
1137 * Adds an activity {@param r} at the given {@param index}. The activity {@param r} must either
1138 * be in the current task or unparented to any task.
1139 */
Craig Mautner1602ec22013-05-12 10:24:27 -07001140 void addActivityAtIndex(int index, ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07001141 TaskRecord task = r.getTask();
1142 if (task != null && task != this) {
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001143 throw new IllegalArgumentException("Can not add r=" + " to task=" + this
Bryce Leeaf691c02017-03-20 14:20:22 -07001144 + " current parent=" + task);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001145 }
Bryce Leeaf691c02017-03-20 14:20:22 -07001146
1147 r.setTask(this);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001148
Craig Mautner6170f732013-04-02 13:05:23 -07001149 // 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 -08001150 if (!mActivities.remove(r) && r.fullscreen) {
1151 // Was not previously in list.
1152 numFullscreen++;
1153 }
Craig Mautner2c1faed2013-07-23 12:56:02 -07001154 // Only set this based on the first activity
1155 if (mActivities.isEmpty()) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001156 taskType = r.mActivityType;
1157 isPersistable = r.isPersistable();
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001158 mCallingUid = r.launchedFromUid;
1159 mCallingPackage = r.launchedFromPackage;
Dianne Hackborn852975d2014-08-22 17:42:43 -07001160 // Clamp to [1, max].
1161 maxRecents = Math.min(Math.max(r.info.maxRecents, 1),
1162 ActivityManager.getMaxAppRecentsLimitStatic());
Craig Mautner2c1faed2013-07-23 12:56:02 -07001163 } else {
1164 // Otherwise make all added activities match this one.
Craig Mautner21d24a22014-04-23 11:45:37 -07001165 r.mActivityType = taskType;
Craig Mautner78733002013-06-10 13:54:49 -07001166 }
Wale Ogunwale3b232392016-05-13 15:37:13 -07001167
1168 final int size = mActivities.size();
1169
1170 if (index == size && size > 0) {
1171 final ActivityRecord top = mActivities.get(size - 1);
1172 if (top.mTaskOverlay) {
1173 // Place below the task overlay activity since the overlay activity should always
1174 // be on top.
1175 index--;
1176 }
1177 }
1178
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001179 index = Math.min(size, index);
Craig Mautner77878772013-03-04 19:46:24 -08001180 mActivities.add(index, r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001181 updateEffectiveIntent();
Craig Mautner21d24a22014-04-23 11:45:37 -07001182 if (r.isPersistable()) {
1183 mService.notifyTaskPersisterLocked(this, false);
1184 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001185
1186 // Sync. with window manager
1187 updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001188 final AppWindowContainerController appController = r.getWindowContainerController();
1189 if (appController != null) {
1190 // Only attempt to move in WM if the child has a controller. It is possible we haven't
1191 // created controller for the activity we are starting yet.
1192 mWindowContainerController.positionChildAt(appController, index);
1193 }
David Stevens82ea6cb2017-03-03 16:18:50 -08001194
1195 // Make sure the list of display UID whitelists is updated
1196 // now that this record is in a new task.
1197 mService.mStackSupervisor.updateUIDsPresentOnDisplay();
Craig Mautner77878772013-03-04 19:46:24 -08001198 }
1199
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001200 /**
Bryce Leeaf691c02017-03-20 14:20:22 -07001201 * Removes the specified activity from this task.
1202 * @param r The {@link ActivityRecord} to remove.
1203 * @return true if this was the last activity in the task.
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001204 */
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001205 boolean removeActivity(ActivityRecord r) {
Bryce Leeaf691c02017-03-20 14:20:22 -07001206 return removeActivity(r, false /*reparenting*/);
1207 }
1208
1209 boolean removeActivity(ActivityRecord r, boolean reparenting) {
1210 if (r.getTask() != this) {
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001211 throw new IllegalArgumentException(
1212 "Activity=" + r + " does not belong to task=" + this);
1213 }
1214
Bryce Leeaf691c02017-03-20 14:20:22 -07001215 r.setTask(null /*task*/, reparenting);
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001216
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001217 if (mActivities.remove(r) && r.fullscreen) {
1218 // Was previously in list.
1219 numFullscreen--;
1220 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001221 if (r.isPersistable()) {
1222 mService.notifyTaskPersisterLocked(this, false);
1223 }
Wale Ogunwale89182d52016-03-11 10:38:36 -08001224
Andrii Kulian02b7a832016-10-06 23:11:56 -07001225 if (getStackId() == PINNED_STACK_ID) {
Wale Ogunwale89182d52016-03-11 10:38:36 -08001226 // We normally notify listeners of task stack changes on pause, however pinned stack
1227 // activities are normally in the paused state so no notification will be sent there
1228 // before the activity is removed. We send it here so instead.
Yorke Leebd54c2a2016-10-25 13:49:23 -07001229 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale89182d52016-03-11 10:38:36 -08001230 }
1231
Craig Mautner41326202014-06-20 14:38:21 -07001232 if (mActivities.isEmpty()) {
Craig Mautner5afcd4d2014-06-21 18:11:33 -07001233 return !mReuseTask;
Craig Mautner41326202014-06-20 14:38:21 -07001234 }
1235 updateEffectiveIntent();
1236 return false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001237 }
1238
Winson Chung6954fc92017-03-24 16:22:12 -07001239 /**
1240 * @return whether or not there are ONLY task overlay activities in the stack.
1241 * If {@param excludeFinishing} is set, then ignore finishing activities in the check.
1242 * If there are no task overlay activities, this call returns false.
1243 */
1244 boolean onlyHasTaskOverlayActivities(boolean excludeFinishing) {
1245 int count = 0;
1246 for (int i = mActivities.size() - 1; i >= 0; i--) {
1247 final ActivityRecord r = mActivities.get(i);
1248 if (excludeFinishing && r.finishing) {
1249 continue;
1250 }
1251 if (!r.mTaskOverlay) {
1252 return false;
1253 }
1254 count++;
1255 }
1256 return count > 0;
1257 }
1258
Craig Mautner41db4a72014-05-07 17:20:56 -07001259 boolean autoRemoveFromRecents() {
Dianne Hackbornd38aed82014-06-10 21:36:35 -07001260 // We will automatically remove the task either if it has explicitly asked for
1261 // this, or it is empty and has never contained an activity that got shown to
1262 // the user.
Dianne Hackborn13420f22014-07-18 15:43:56 -07001263 return autoRemoveRecents || (mActivities.isEmpty() && !hasBeenVisible);
Craig Mautner41db4a72014-05-07 17:20:56 -07001264 }
1265
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001266 /**
1267 * Completely remove all activities associated with an existing
1268 * task starting at a specified index.
1269 */
Winson Chung6954fc92017-03-24 16:22:12 -07001270 final void performClearTaskAtIndexLocked(int activityNdx, boolean pauseImmediately) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001271 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001272 for ( ; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001273 final ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001274 if (r.finishing) {
1275 continue;
1276 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001277 if (mStack == null) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001278 // Task was restored from persistent storage.
1279 r.takeFromHistory();
1280 mActivities.remove(activityNdx);
1281 --activityNdx;
1282 --numActivities;
Winson Chung6954fc92017-03-24 16:22:12 -07001283 } else if (mStack.finishActivityLocked(r, Activity.RESULT_CANCELED, null,
1284 "clear-task-index", false, pauseImmediately)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001285 --activityNdx;
1286 --numActivities;
1287 }
1288 }
1289 }
1290
1291 /**
1292 * Completely remove all activities associated with an existing task.
1293 */
Benjamin Franza83859f2017-07-03 16:34:14 +01001294 void performClearTaskLocked() {
Craig Mautner362449a2014-06-20 14:04:39 -07001295 mReuseTask = true;
Winson Chung6954fc92017-03-24 16:22:12 -07001296 performClearTaskAtIndexLocked(0, !PAUSE_IMMEDIATELY);
Craig Mautner362449a2014-06-20 14:04:39 -07001297 mReuseTask = false;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001298 }
1299
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -08001300 ActivityRecord performClearTaskForReuseLocked(ActivityRecord newR, int launchFlags) {
1301 mReuseTask = true;
1302 final ActivityRecord result = performClearTaskLocked(newR, launchFlags);
1303 mReuseTask = false;
1304 return result;
1305 }
1306
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001307 /**
1308 * Perform clear operation as requested by
1309 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
1310 * stack to the given task, then look for
1311 * an instance of that activity in the stack and, if found, finish all
1312 * activities on top of it and return the instance.
1313 *
1314 * @param newR Description of the new activity being started.
1315 * @return Returns the old activity that should be continued to be used,
1316 * or null if none was found.
1317 */
1318 final ActivityRecord performClearTaskLocked(ActivityRecord newR, int launchFlags) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001319 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001320 for (int activityNdx = numActivities - 1; activityNdx >= 0; --activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001321 ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001322 if (r.finishing) {
1323 continue;
1324 }
1325 if (r.realActivity.equals(newR.realActivity)) {
1326 // Here it is! Now finish everything in front...
Craig Mautner1602ec22013-05-12 10:24:27 -07001327 final ActivityRecord ret = r;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001328
1329 for (++activityNdx; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001330 r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001331 if (r.finishing) {
1332 continue;
1333 }
1334 ActivityOptions opts = r.takeOptionsLocked();
1335 if (opts != null) {
1336 ret.updateOptionsLocked(opts);
1337 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001338 if (mStack != null && mStack.finishActivityLocked(
Todd Kennedy539db512014-12-15 09:57:55 -08001339 r, Activity.RESULT_CANCELED, null, "clear-task-stack", false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001340 --activityNdx;
1341 --numActivities;
1342 }
1343 }
1344
1345 // Finally, if this is a normal launch mode (that is, not
1346 // expecting onNewIntent()), then we will finish the current
1347 // instance of the activity so a new fresh one can be started.
1348 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
Daichi Hirono15a02992016-04-27 18:47:01 +09001349 && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0
1350 && !ActivityStarter.isDocumentLaunchesIntoExisting(launchFlags)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001351 if (!ret.finishing) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001352 if (mStack != null) {
1353 mStack.finishActivityLocked(
Wale Ogunwale7d701172015-03-11 15:36:30 -07001354 ret, Activity.RESULT_CANCELED, null, "clear-task-top", false);
1355 }
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001356 return null;
1357 }
1358 }
1359
1360 return ret;
1361 }
1362 }
1363
1364 return null;
1365 }
1366
Winson Chung6954fc92017-03-24 16:22:12 -07001367 void removeTaskActivitiesLocked(boolean pauseImmediately) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001368 // Just remove the entire task.
Winson Chung6954fc92017-03-24 16:22:12 -07001369 performClearTaskAtIndexLocked(0, pauseImmediately);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07001370 }
1371
Craig Mautner432f64e2015-05-20 14:59:57 -07001372 String lockTaskAuthToString() {
1373 switch (mLockTaskAuth) {
1374 case LOCK_TASK_AUTH_DONT_LOCK: return "LOCK_TASK_AUTH_DONT_LOCK";
1375 case LOCK_TASK_AUTH_PINNABLE: return "LOCK_TASK_AUTH_PINNABLE";
1376 case LOCK_TASK_AUTH_LAUNCHABLE: return "LOCK_TASK_AUTH_LAUNCHABLE";
1377 case LOCK_TASK_AUTH_WHITELISTED: return "LOCK_TASK_AUTH_WHITELISTED";
Benjamin Franz469dd582015-06-09 14:24:36 +01001378 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV: return "LOCK_TASK_AUTH_LAUNCHABLE_PRIV";
Craig Mautner432f64e2015-05-20 14:59:57 -07001379 default: return "unknown=" + mLockTaskAuth;
1380 }
1381 }
1382
Craig Mautner15df08a2015-04-01 12:17:18 -07001383 void setLockTaskAuth() {
Benjamin Franz469dd582015-06-09 14:24:36 +01001384 if (!mPrivileged &&
1385 (mLockTaskMode == LOCK_TASK_LAUNCH_MODE_ALWAYS ||
1386 mLockTaskMode == LOCK_TASK_LAUNCH_MODE_NEVER)) {
1387 // Non-priv apps are not allowed to use always or never, fall back to default
1388 mLockTaskMode = LOCK_TASK_LAUNCH_MODE_DEFAULT;
1389 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001390 switch (mLockTaskMode) {
1391 case LOCK_TASK_LAUNCH_MODE_DEFAULT:
1392 mLockTaskAuth = isLockTaskWhitelistedLocked() ?
1393 LOCK_TASK_AUTH_WHITELISTED : LOCK_TASK_AUTH_PINNABLE;
1394 break;
1395
1396 case LOCK_TASK_LAUNCH_MODE_NEVER:
Benjamin Franz469dd582015-06-09 14:24:36 +01001397 mLockTaskAuth = LOCK_TASK_AUTH_DONT_LOCK;
Craig Mautner15df08a2015-04-01 12:17:18 -07001398 break;
1399
1400 case LOCK_TASK_LAUNCH_MODE_ALWAYS:
Benjamin Franz469dd582015-06-09 14:24:36 +01001401 mLockTaskAuth = LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -07001402 break;
1403
1404 case LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED:
1405 mLockTaskAuth = isLockTaskWhitelistedLocked() ?
1406 LOCK_TASK_AUTH_LAUNCHABLE : LOCK_TASK_AUTH_PINNABLE;
1407 break;
1408 }
Craig Mautner432f64e2015-05-20 14:59:57 -07001409 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "setLockTaskAuth: task=" + this +
1410 " mLockTaskAuth=" + lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07001411 }
1412
Benjamin Franza83859f2017-07-03 16:34:14 +01001413 private boolean isLockTaskWhitelistedLocked() {
Benjamin Franz6fd84cc2015-08-06 18:09:03 +01001414 String pkg = (realActivity != null) ? realActivity.getPackageName() : null;
1415 if (pkg == null) {
Craig Mautner15df08a2015-04-01 12:17:18 -07001416 return false;
1417 }
1418 String[] packages = mService.mLockTaskPackages.get(userId);
1419 if (packages == null) {
1420 return false;
1421 }
1422 for (int i = packages.length - 1; i >= 0; --i) {
Benjamin Franz6fd84cc2015-08-06 18:09:03 +01001423 if (pkg.equals(packages[i])) {
Craig Mautner15df08a2015-04-01 12:17:18 -07001424 return true;
1425 }
1426 }
1427 return false;
1428 }
Wale Ogunwale74e26592016-02-05 11:48:37 -08001429
Craig Mautnera82aa092013-09-13 15:34:08 -07001430 boolean isHomeTask() {
Craig Mautner84984fa2014-06-19 11:19:20 -07001431 return taskType == HOME_ACTIVITY_TYPE;
Craig Mautnera82aa092013-09-13 15:34:08 -07001432 }
1433
Wale Ogunwale74e26592016-02-05 11:48:37 -08001434 boolean isRecentsTask() {
1435 return taskType == RECENTS_ACTIVITY_TYPE;
1436 }
1437
Winson Chung83471632016-12-13 11:02:12 -08001438 boolean isAssistantTask() {
1439 return taskType == ASSISTANT_ACTIVITY_TYPE;
1440 }
1441
Craig Mautner86d67a42013-05-14 10:34:38 -07001442 boolean isApplicationTask() {
Craig Mautner84984fa2014-06-19 11:19:20 -07001443 return taskType == APPLICATION_ACTIVITY_TYPE;
1444 }
1445
1446 boolean isOverHomeStack() {
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001447 return mTaskToReturnTo == HOME_ACTIVITY_TYPE;
Craig Mautner1602ec22013-05-12 10:24:27 -07001448 }
1449
Winson Chung83471632016-12-13 11:02:12 -08001450 boolean isOverAssistantStack() {
1451 return mTaskToReturnTo == ASSISTANT_ACTIVITY_TYPE;
1452 }
1453
Winson Chungd3395382016-12-13 11:49:09 -08001454 private boolean isResizeable(boolean checkSupportsPip) {
1455 return (mService.mForceResizableActivities || ActivityInfo.isResizeableMode(mResizeMode)
1456 || (checkSupportsPip && mSupportsPictureInPicture)) && !mTemporarilyUnresizable;
1457 }
1458
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001459 boolean isResizeable() {
Winson Chungd3395382016-12-13 11:49:09 -08001460 return isResizeable(true /* checkSupportsPip */);
1461 }
1462
1463 boolean supportsSplitScreen() {
1464 // A task can not be docked even if it is considered resizeable because it only supports
1465 // picture-in-picture mode but has a non-resizeable resizeMode
1466 return mService.mSupportsSplitScreenMultiWindow
Bryce Leec857a5b2017-08-16 10:04:52 -07001467 && (mService.mForceResizableActivities
1468 || (isResizeable(false /* checkSupportsPip */)
1469 && !ActivityInfo.isPreserveOrientationMode(mResizeMode)));
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001470 }
1471
skuhne@google.com322347b2016-12-02 12:54:03 -08001472 /**
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001473 * Check whether this task can be launched on the specified display.
1474 * @param displayId Target display id.
1475 * @return {@code true} if either it is the default display or this activity is resizeable and
1476 * can be put a secondary screen.
1477 */
1478 boolean canBeLaunchedOnDisplay(int displayId) {
1479 return mService.mStackSupervisor.canPlaceEntityOnDisplay(displayId,
Andrii Kulian02689a72017-07-06 14:28:59 -07001480 isResizeable(false /* checkSupportsPip */), -1 /* don't check PID */,
1481 -1 /* don't check UID */, null /* activityInfo */);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001482 }
1483
1484 /**
skuhne@google.com322347b2016-12-02 12:54:03 -08001485 * Check that a given bounds matches the application requested orientation.
1486 *
1487 * @param bounds The bounds to be tested.
1488 * @return True if the requested bounds are okay for a resizing request.
1489 */
Wale Ogunwale069bbd32017-02-03 07:58:14 -08001490 private boolean canResizeToBounds(Rect bounds) {
skuhne@google.com322347b2016-12-02 12:54:03 -08001491 if (bounds == null || getStackId() != FREEFORM_WORKSPACE_STACK_ID) {
1492 // Note: If not on the freeform workspace, we ignore the bounds.
1493 return true;
1494 }
1495 final boolean landscape = bounds.width() > bounds.height();
1496 if (mResizeMode == RESIZE_MODE_FORCE_RESIZABLE_PRESERVE_ORIENTATION) {
1497 return mBounds == null || landscape == (mBounds.width() > mBounds.height());
1498 }
1499 return (mResizeMode != RESIZE_MODE_FORCE_RESIZABLE_PORTRAIT_ONLY || !landscape)
1500 && (mResizeMode != RESIZE_MODE_FORCE_RESIZABLE_LANDSCAPE_ONLY || landscape);
1501 }
1502
Craig Mautner525f3d92013-05-07 14:01:50 -07001503 /**
Yorke Leebdef5372017-04-10 16:38:51 -07001504 * @return {@code true} if the task is being cleared for the purposes of being reused.
1505 */
1506 boolean isClearingToReuseTask() {
1507 return mReuseTask;
1508 }
1509
1510 /**
Craig Mautner525f3d92013-05-07 14:01:50 -07001511 * Find the activity in the history stack within the given task. Returns
1512 * the index within the history at which it's found, or < 0 if not found.
1513 */
1514 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r) {
1515 final ComponentName realActivity = r.realActivity;
1516 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1517 ActivityRecord candidate = mActivities.get(activityNdx);
1518 if (candidate.finishing) {
1519 continue;
1520 }
1521 if (candidate.realActivity.equals(realActivity)) {
1522 return candidate;
1523 }
1524 }
1525 return null;
1526 }
1527
Winson Chunga449dc02014-05-16 11:15:04 -07001528 /** Updates the last task description values. */
1529 void updateTaskDescription() {
1530 // Traverse upwards looking for any break between main task activities and
1531 // utility activities.
1532 int activityNdx;
1533 final int numActivities = mActivities.size();
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001534 final boolean relinquish = numActivities != 0 &&
1535 (mActivities.get(0).info.flags & FLAG_RELINQUISH_TASK_IDENTITY) != 0;
Winson Chunga449dc02014-05-16 11:15:04 -07001536 for (activityNdx = Math.min(numActivities, 1); activityNdx < numActivities;
Craig Mautner21d24a22014-04-23 11:45:37 -07001537 ++activityNdx) {
Winson Chunga449dc02014-05-16 11:15:04 -07001538 final ActivityRecord r = mActivities.get(activityNdx);
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001539 if (relinquish && (r.info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001540 // This will be the top activity for determining taskDescription. Pre-inc to
1541 // overcome initial decrement below.
1542 ++activityNdx;
1543 break;
1544 }
Winson Chunga449dc02014-05-16 11:15:04 -07001545 if (r.intent != null &&
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001546 (r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Winson Chunga449dc02014-05-16 11:15:04 -07001547 break;
1548 }
1549 }
1550 if (activityNdx > 0) {
1551 // Traverse downwards starting below break looking for set label, icon.
1552 // Note that if there are activities in the task but none of them set the
1553 // recent activity values, then we do not fall back to the last set
1554 // values in the TaskRecord.
1555 String label = null;
Craig Mautner648f69b2014-09-18 14:16:26 -07001556 String iconFilename = null;
Winson Chunga449dc02014-05-16 11:15:04 -07001557 int colorPrimary = 0;
Winson Chung1af8eda2016-02-05 17:55:56 +00001558 int colorBackground = 0;
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001559 int statusBarColor = 0;
1560 int navigationBarColor = 0;
1561 boolean topActivity = true;
Winson Chunga449dc02014-05-16 11:15:04 -07001562 for (--activityNdx; activityNdx >= 0; --activityNdx) {
1563 final ActivityRecord r = mActivities.get(activityNdx);
1564 if (r.taskDescription != null) {
1565 if (label == null) {
1566 label = r.taskDescription.getLabel();
1567 }
Craig Mautner648f69b2014-09-18 14:16:26 -07001568 if (iconFilename == null) {
1569 iconFilename = r.taskDescription.getIconFilename();
Winson Chunga449dc02014-05-16 11:15:04 -07001570 }
1571 if (colorPrimary == 0) {
1572 colorPrimary = r.taskDescription.getPrimaryColor();
Winson Chunga449dc02014-05-16 11:15:04 -07001573 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001574 if (topActivity) {
Winson Chung1af8eda2016-02-05 17:55:56 +00001575 colorBackground = r.taskDescription.getBackgroundColor();
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001576 statusBarColor = r.taskDescription.getStatusBarColor();
1577 navigationBarColor = r.taskDescription.getNavigationBarColor();
Winson Chung1af8eda2016-02-05 17:55:56 +00001578 }
Winson Chunga449dc02014-05-16 11:15:04 -07001579 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001580 topActivity = false;
Winson Chunga449dc02014-05-16 11:15:04 -07001581 }
Winson Chung1af8eda2016-02-05 17:55:56 +00001582 lastTaskDescription = new TaskDescription(label, null, iconFilename, colorPrimary,
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001583 colorBackground, statusBarColor, navigationBarColor);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +01001584 if (mWindowContainerController != null) {
1585 mWindowContainerController.setTaskDescription(lastTaskDescription);
1586 }
Winson Chungec396d62014-08-06 17:08:00 -07001587 // Update the task affiliation color if we are the parent of the group
1588 if (taskId == mAffiliatedTaskId) {
1589 mAffiliatedTaskColor = lastTaskDescription.getPrimaryColor();
1590 }
Winson Chunga449dc02014-05-16 11:15:04 -07001591 }
1592 }
1593
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001594 int findEffectiveRootIndex() {
Craig Mautner4767f4b2014-09-18 15:38:33 -07001595 int effectiveNdx = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001596 final int topActivityNdx = mActivities.size() - 1;
Dianne Hackborn39569af2014-09-23 10:56:58 -07001597 for (int activityNdx = 0; activityNdx <= topActivityNdx; ++activityNdx) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001598 final ActivityRecord r = mActivities.get(activityNdx);
1599 if (r.finishing) {
1600 continue;
1601 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001602 effectiveNdx = activityNdx;
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001603 if ((r.info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001604 break;
1605 }
1606 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001607 return effectiveNdx;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001608 }
1609
1610 void updateEffectiveIntent() {
1611 final int effectiveRootIndex = findEffectiveRootIndex();
1612 final ActivityRecord r = mActivities.get(effectiveRootIndex);
Winson Chungfee26772014-08-05 12:21:52 -07001613 setIntent(r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001614 }
1615
Craig Mautner21d24a22014-04-23 11:45:37 -07001616 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001617 if (DEBUG_RECENTS) Slog.i(TAG_RECENTS, "Saving task=" + this);
Craig Mautner21d24a22014-04-23 11:45:37 -07001618
1619 out.attribute(null, ATTR_TASKID, String.valueOf(taskId));
1620 if (realActivity != null) {
1621 out.attribute(null, ATTR_REALACTIVITY, realActivity.flattenToShortString());
1622 }
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001623 out.attribute(null, ATTR_REALACTIVITY_SUSPENDED, String.valueOf(realActivitySuspended));
Craig Mautner21d24a22014-04-23 11:45:37 -07001624 if (origActivity != null) {
1625 out.attribute(null, ATTR_ORIGACTIVITY, origActivity.flattenToShortString());
1626 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001627 // Write affinity, and root affinity if it is different from affinity.
1628 // We use the special string "@" for a null root affinity, so we can identify
1629 // later whether we were given a root affinity or should just make it the
1630 // same as the affinity.
Craig Mautner21d24a22014-04-23 11:45:37 -07001631 if (affinity != null) {
1632 out.attribute(null, ATTR_AFFINITY, affinity);
Dianne Hackborn79228822014-09-16 11:11:23 -07001633 if (!affinity.equals(rootAffinity)) {
1634 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
1635 }
1636 } else if (rootAffinity != null) {
1637 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
Craig Mautner21d24a22014-04-23 11:45:37 -07001638 }
1639 out.attribute(null, ATTR_ROOTHASRESET, String.valueOf(rootWasReset));
Dianne Hackborn13420f22014-07-18 15:43:56 -07001640 out.attribute(null, ATTR_AUTOREMOVERECENTS, String.valueOf(autoRemoveRecents));
Craig Mautner21d24a22014-04-23 11:45:37 -07001641 out.attribute(null, ATTR_ASKEDCOMPATMODE, String.valueOf(askedCompatMode));
1642 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001643 out.attribute(null, ATTR_USER_SETUP_COMPLETE, String.valueOf(mUserSetupComplete));
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001644 out.attribute(null, ATTR_EFFECTIVE_UID, String.valueOf(effectiveUid));
Craig Mautner21d24a22014-04-23 11:45:37 -07001645 out.attribute(null, ATTR_TASKTYPE, String.valueOf(taskType));
Winson Chungffa2ec62014-07-03 15:54:42 -07001646 out.attribute(null, ATTR_FIRSTACTIVETIME, String.valueOf(firstActiveTime));
Winson Chungf1fbd772014-06-24 18:06:58 -07001647 out.attribute(null, ATTR_LASTACTIVETIME, String.valueOf(lastActiveTime));
Craig Mautner21d24a22014-04-23 11:45:37 -07001648 out.attribute(null, ATTR_LASTTIMEMOVED, String.valueOf(mLastTimeMoved));
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001649 out.attribute(null, ATTR_NEVERRELINQUISH, String.valueOf(mNeverRelinquishIdentity));
Craig Mautner21d24a22014-04-23 11:45:37 -07001650 if (lastDescription != null) {
1651 out.attribute(null, ATTR_LASTDESCRIPTION, lastDescription.toString());
1652 }
Winson Chung2cb86c72014-06-25 12:03:30 -07001653 if (lastTaskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07001654 lastTaskDescription.saveToXml(out);
Winson Chung2cb86c72014-06-25 12:03:30 -07001655 }
Winson Chungec396d62014-08-06 17:08:00 -07001656 out.attribute(null, ATTR_TASK_AFFILIATION_COLOR, String.valueOf(mAffiliatedTaskColor));
Craig Mautnera228ae92014-07-09 05:44:55 -07001657 out.attribute(null, ATTR_TASK_AFFILIATION, String.valueOf(mAffiliatedTaskId));
1658 out.attribute(null, ATTR_PREV_AFFILIATION, String.valueOf(mPrevAffiliateTaskId));
1659 out.attribute(null, ATTR_NEXT_AFFILIATION, String.valueOf(mNextAffiliateTaskId));
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001660 out.attribute(null, ATTR_CALLING_UID, String.valueOf(mCallingUid));
1661 out.attribute(null, ATTR_CALLING_PACKAGE, mCallingPackage == null ? "" : mCallingPackage);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001662 out.attribute(null, ATTR_RESIZE_MODE, String.valueOf(mResizeMode));
Winson Chungd3395382016-12-13 11:49:09 -08001663 out.attribute(null, ATTR_SUPPORTS_PICTURE_IN_PICTURE,
1664 String.valueOf(mSupportsPictureInPicture));
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001665 out.attribute(null, ATTR_PRIVILEGED, String.valueOf(mPrivileged));
Wale Ogunwale706ed792015-08-02 10:29:44 -07001666 if (mLastNonFullscreenBounds != null) {
1667 out.attribute(
1668 null, ATTR_NON_FULLSCREEN_BOUNDS, mLastNonFullscreenBounds.flattenToString());
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001669 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001670 out.attribute(null, ATTR_MIN_WIDTH, String.valueOf(mMinWidth));
1671 out.attribute(null, ATTR_MIN_HEIGHT, String.valueOf(mMinHeight));
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001672 out.attribute(null, ATTR_PERSIST_TASK_VERSION, String.valueOf(PERSIST_TASK_VERSION));
Winson Chung2cb86c72014-06-25 12:03:30 -07001673
Craig Mautner21d24a22014-04-23 11:45:37 -07001674 if (affinityIntent != null) {
1675 out.startTag(null, TAG_AFFINITYINTENT);
1676 affinityIntent.saveToXml(out);
1677 out.endTag(null, TAG_AFFINITYINTENT);
1678 }
1679
Winson Chung36f3f032016-09-08 23:29:43 +00001680 out.startTag(null, TAG_INTENT);
1681 intent.saveToXml(out);
1682 out.endTag(null, TAG_INTENT);
Craig Mautner21d24a22014-04-23 11:45:37 -07001683
1684 final ArrayList<ActivityRecord> activities = mActivities;
1685 final int numActivities = activities.size();
1686 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1687 final ActivityRecord r = activities.get(activityNdx);
Craig Mautner43e52ed2014-06-16 17:18:52 -07001688 if (r.info.persistableMode == ActivityInfo.PERSIST_ROOT_ONLY || !r.isPersistable() ||
Wale Ogunwale843bfb92015-03-27 11:06:48 -07001689 ((r.intent.getFlags() & FLAG_ACTIVITY_NEW_DOCUMENT
1690 | FLAG_ACTIVITY_RETAIN_IN_RECENTS) == FLAG_ACTIVITY_NEW_DOCUMENT) &&
Craig Mautner43e52ed2014-06-16 17:18:52 -07001691 activityNdx > 0) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001692 // Stop at first non-persistable or first break in task (CLEAR_WHEN_TASK_RESET).
Craig Mautner21d24a22014-04-23 11:45:37 -07001693 break;
1694 }
1695 out.startTag(null, TAG_ACTIVITY);
1696 r.saveToXml(out);
1697 out.endTag(null, TAG_ACTIVITY);
1698 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001699 }
1700
Winson Chung36f3f032016-09-08 23:29:43 +00001701 static TaskRecord restoreFromXml(XmlPullParser in, ActivityStackSupervisor stackSupervisor)
1702 throws IOException, XmlPullParserException {
Craig Mautner21d24a22014-04-23 11:45:37 -07001703 Intent intent = null;
1704 Intent affinityIntent = null;
Winsonc809cbb2015-11-02 12:06:15 -08001705 ArrayList<ActivityRecord> activities = new ArrayList<>();
Craig Mautner21d24a22014-04-23 11:45:37 -07001706 ComponentName realActivity = null;
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001707 boolean realActivitySuspended = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001708 ComponentName origActivity = null;
1709 String affinity = null;
Dianne Hackborn79228822014-09-16 11:11:23 -07001710 String rootAffinity = null;
1711 boolean hasRootAffinity = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001712 boolean rootHasReset = false;
Dianne Hackborn13420f22014-07-18 15:43:56 -07001713 boolean autoRemoveRecents = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001714 boolean askedCompatMode = false;
1715 int taskType = ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner21d24a22014-04-23 11:45:37 -07001716 int userId = 0;
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001717 boolean userSetupComplete = true;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001718 int effectiveUid = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -07001719 String lastDescription = null;
Winson Chungffa2ec62014-07-03 15:54:42 -07001720 long firstActiveTime = -1;
Winson Chung2cb86c72014-06-25 12:03:30 -07001721 long lastActiveTime = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -07001722 long lastTimeOnTop = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001723 boolean neverRelinquishIdentity = true;
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001724 int taskId = INVALID_TASK_ID;
Craig Mautner21d24a22014-04-23 11:45:37 -07001725 final int outerDepth = in.getDepth();
Craig Mautner648f69b2014-09-18 14:16:26 -07001726 TaskDescription taskDescription = new TaskDescription();
Wale Ogunwale18795a22014-12-03 11:38:33 -08001727 int taskAffiliation = INVALID_TASK_ID;
Winson Chungec396d62014-08-06 17:08:00 -07001728 int taskAffiliationColor = 0;
Wale Ogunwale18795a22014-12-03 11:38:33 -08001729 int prevTaskId = INVALID_TASK_ID;
1730 int nextTaskId = INVALID_TASK_ID;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001731 int callingUid = -1;
1732 String callingPackage = "";
Wale Ogunwaled829d362016-02-10 19:24:49 -08001733 int resizeMode = RESIZE_MODE_FORCE_RESIZEABLE;
Winson Chungd3395382016-12-13 11:49:09 -08001734 boolean supportsPictureInPicture = false;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001735 boolean privileged = 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);
Winson Chungffa2ec62014-07-03 15:54:42 -07001773 } else if (ATTR_FIRSTACTIVETIME.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001774 firstActiveTime = Long.parseLong(attrValue);
Winson Chungf1fbd772014-06-24 18:06:58 -07001775 } else if (ATTR_LASTACTIVETIME.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001776 lastActiveTime = Long.parseLong(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001777 } else if (ATTR_LASTDESCRIPTION.equals(attrName)) {
1778 lastDescription = attrValue;
1779 } else if (ATTR_LASTTIMEMOVED.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001780 lastTimeOnTop = Long.parseLong(attrValue);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001781 } else if (ATTR_NEVERRELINQUISH.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001782 neverRelinquishIdentity = Boolean.parseBoolean(attrValue);
Craig Mautner648f69b2014-09-18 14:16:26 -07001783 } else if (attrName.startsWith(TaskDescription.ATTR_TASKDESCRIPTION_PREFIX)) {
1784 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001785 } else if (ATTR_TASK_AFFILIATION.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001786 taskAffiliation = Integer.parseInt(attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001787 } else if (ATTR_PREV_AFFILIATION.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001788 prevTaskId = Integer.parseInt(attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001789 } else if (ATTR_NEXT_AFFILIATION.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001790 nextTaskId = Integer.parseInt(attrValue);
Winson Chungec396d62014-08-06 17:08:00 -07001791 } else if (ATTR_TASK_AFFILIATION_COLOR.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001792 taskAffiliationColor = Integer.parseInt(attrValue);
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001793 } else if (ATTR_CALLING_UID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001794 callingUid = Integer.parseInt(attrValue);
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001795 } else if (ATTR_CALLING_PACKAGE.equals(attrName)) {
1796 callingPackage = attrValue;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001797 } else if (ATTR_RESIZE_MODE.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001798 resizeMode = Integer.parseInt(attrValue);
Winson Chungd3395382016-12-13 11:49:09 -08001799 } else if (ATTR_SUPPORTS_PICTURE_IN_PICTURE.equals(attrName)) {
1800 supportsPictureInPicture = Boolean.parseBoolean(attrValue);
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001801 } else if (ATTR_PRIVILEGED.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001802 privileged = Boolean.parseBoolean(attrValue);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001803 } else if (ATTR_NON_FULLSCREEN_BOUNDS.equals(attrName)) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001804 bounds = Rect.unflattenFromString(attrValue);
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001805 } else if (ATTR_MIN_WIDTH.equals(attrName)) {
1806 minWidth = Integer.parseInt(attrValue);
1807 } else if (ATTR_MIN_HEIGHT.equals(attrName)) {
1808 minHeight = Integer.parseInt(attrValue);
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001809 } else if (ATTR_PERSIST_TASK_VERSION.equals(attrName)) {
1810 persistTaskVersion = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001811 } else {
1812 Slog.w(TAG, "TaskRecord: Unknown attribute=" + attrName);
1813 }
1814 }
1815
1816 int event;
1817 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
Ben Kwa8814cf42015-07-08 10:54:56 -07001818 (event != XmlPullParser.END_TAG || in.getDepth() >= outerDepth)) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001819 if (event == XmlPullParser.START_TAG) {
1820 final String name = in.getName();
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001821 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: START_TAG name=" +
1822 name);
Craig Mautner21d24a22014-04-23 11:45:37 -07001823 if (TAG_AFFINITYINTENT.equals(name)) {
1824 affinityIntent = Intent.restoreFromXml(in);
1825 } else if (TAG_INTENT.equals(name)) {
1826 intent = Intent.restoreFromXml(in);
1827 } else if (TAG_ACTIVITY.equals(name)) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001828 ActivityRecord activity = ActivityRecord.restoreFromXml(in, stackSupervisor);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001829 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: activity=" +
1830 activity);
Craig Mautner21d24a22014-04-23 11:45:37 -07001831 if (activity != null) {
1832 activities.add(activity);
1833 }
1834 } else {
1835 Slog.e(TAG, "restoreTask: Unexpected name=" + name);
1836 XmlUtils.skipCurrentTag(in);
1837 }
1838 }
1839 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001840 if (!hasRootAffinity) {
1841 rootAffinity = affinity;
1842 } else if ("@".equals(rootAffinity)) {
1843 rootAffinity = null;
1844 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001845 if (effectiveUid <= 0) {
1846 Intent checkIntent = intent != null ? intent : affinityIntent;
1847 effectiveUid = 0;
1848 if (checkIntent != null) {
1849 IPackageManager pm = AppGlobals.getPackageManager();
1850 try {
1851 ApplicationInfo ai = pm.getApplicationInfo(
1852 checkIntent.getComponent().getPackageName(),
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -07001853 PackageManager.MATCH_UNINSTALLED_PACKAGES
1854 | PackageManager.MATCH_DISABLED_COMPONENTS, userId);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001855 if (ai != null) {
1856 effectiveUid = ai.uid;
1857 }
1858 } catch (RemoteException e) {
1859 }
1860 }
1861 Slog.w(TAG, "Updating task #" + taskId + " for " + checkIntent
1862 + ": effectiveUid=" + effectiveUid);
1863 }
1864
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001865 if (persistTaskVersion < 1) {
1866 // We need to convert the resize mode of home activities saved before version one if
1867 // they are marked as RESIZE_MODE_RESIZEABLE to RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION
1868 // since we didn't have that differentiation before version 1 and the system didn't
1869 // resize home activities before then.
1870 if (taskType == HOME_ACTIVITY_TYPE && resizeMode == RESIZE_MODE_RESIZEABLE) {
1871 resizeMode = RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
1872 }
Winson Chungd3395382016-12-13 11:49:09 -08001873 } else {
1874 // This activity has previously marked itself explicitly as both resizeable and
1875 // supporting picture-in-picture. Since there is no longer a requirement for
1876 // picture-in-picture activities to be resizeable, we can mark this simply as
1877 // resizeable and supporting picture-in-picture separately.
1878 if (resizeMode == RESIZE_MODE_RESIZEABLE_AND_PIPABLE_DEPRECATED) {
1879 resizeMode = RESIZE_MODE_RESIZEABLE;
1880 supportsPictureInPicture = true;
1881 }
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001882 }
1883
Winson Chung36f3f032016-09-08 23:29:43 +00001884 final TaskRecord task = new TaskRecord(stackSupervisor.mService, taskId, intent,
Dianne Hackborn79228822014-09-16 11:11:23 -07001885 affinityIntent, affinity, rootAffinity, realActivity, origActivity, rootHasReset,
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001886 autoRemoveRecents, askedCompatMode, taskType, userId, effectiveUid, lastDescription,
1887 activities, firstActiveTime, lastActiveTime, lastTimeOnTop, neverRelinquishIdentity,
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001888 taskDescription, taskAffiliation, prevTaskId, nextTaskId, taskAffiliationColor,
1889 callingUid, callingPackage, resizeMode, supportsPictureInPicture, privileged,
1890 realActivitySuspended, userSetupComplete, minWidth, minHeight);
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07001891 task.updateOverrideConfiguration(bounds);
Craig Mautner21d24a22014-04-23 11:45:37 -07001892
1893 for (int activityNdx = activities.size() - 1; activityNdx >=0; --activityNdx) {
Bryce Leeaf691c02017-03-20 14:20:22 -07001894 activities.get(activityNdx).setTask(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001895 }
1896
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001897 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Restored task=" + task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001898 return task;
1899 }
1900
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001901 private void adjustForMinimalTaskDimensions(Rect bounds) {
1902 if (bounds == null) {
1903 return;
1904 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001905 int minWidth = mMinWidth;
1906 int minHeight = mMinHeight;
Robert Carr9c5867d2016-03-10 15:52:46 -08001907 // If the task has no requested minimal size, we'd like to enforce a minimal size
1908 // so that the user can not render the task too small to manipulate. We don't need
1909 // to do this for the pinned stack as the bounds are controlled by the system.
Andrii Kulian02b7a832016-10-06 23:11:56 -07001910 if (getStackId() != PINNED_STACK_ID) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001911 if (minWidth == INVALID_MIN_SIZE) {
1912 minWidth = mService.mStackSupervisor.mDefaultMinSizeOfResizeableTask;
Andrii Kulian2e751b82016-03-16 16:59:32 -07001913 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001914 if (minHeight == INVALID_MIN_SIZE) {
1915 minHeight = mService.mStackSupervisor.mDefaultMinSizeOfResizeableTask;
Andrii Kulian2e751b82016-03-16 16:59:32 -07001916 }
Robert Carr9c5867d2016-03-10 15:52:46 -08001917 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001918 final boolean adjustWidth = minWidth > bounds.width();
1919 final boolean adjustHeight = minHeight > bounds.height();
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001920 if (!(adjustWidth || adjustHeight)) {
1921 return;
1922 }
1923
1924 if (adjustWidth) {
1925 if (mBounds != null && bounds.right == mBounds.right) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001926 bounds.left = bounds.right - minWidth;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001927 } else {
1928 // Either left bounds match, or neither match, or the previous bounds were
1929 // fullscreen and we default to keeping left.
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001930 bounds.right = bounds.left + minWidth;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001931 }
1932 }
1933 if (adjustHeight) {
1934 if (mBounds != null && bounds.bottom == mBounds.bottom) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001935 bounds.top = bounds.bottom - minHeight;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001936 } else {
1937 // Either top bounds match, or neither match, or the previous bounds were
1938 // fullscreen and we default to keeping top.
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001939 bounds.bottom = bounds.top + minHeight;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001940 }
1941 }
1942 }
1943
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001944 /**
Winson Chung5af42fc2017-03-24 17:11:33 -07001945 * @return a new Configuration for this Task, given the provided {@param bounds} and
1946 * {@param insetBounds}.
1947 */
1948 Configuration computeNewOverrideConfigurationForBounds(Rect bounds, Rect insetBounds) {
1949 // Compute a new override configuration for the given bounds, if fullscreen bounds
1950 // (bounds == null), then leave the override config unset
1951 final Configuration newOverrideConfig = new Configuration();
1952 if (bounds != null) {
1953 newOverrideConfig.setTo(getOverrideConfiguration());
1954 mTmpRect.set(bounds);
1955 adjustForMinimalTaskDimensions(mTmpRect);
1956 computeOverrideConfiguration(newOverrideConfig, mTmpRect, insetBounds,
1957 mTmpRect.right != bounds.right, mTmpRect.bottom != bounds.bottom);
1958 }
1959
1960 return newOverrideConfig;
1961 }
1962
1963 /**
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001964 * Update task's override configuration based on the bounds.
Jorim Jaggi0a932142016-02-01 17:42:25 -08001965 * @param bounds The bounds of the task.
Andrii Kulian8072d112016-09-16 11:11:01 -07001966 * @return True if the override configuration was updated.
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001967 */
Andrii Kulian8072d112016-09-16 11:11:01 -07001968 boolean updateOverrideConfiguration(Rect bounds) {
Jorim Jaggi0a932142016-02-01 17:42:25 -08001969 return updateOverrideConfiguration(bounds, null /* insetBounds */);
1970 }
1971
1972 /**
1973 * Update task's override configuration based on the bounds.
1974 * @param bounds The bounds of the task.
1975 * @param insetBounds The bounds used to calculate the system insets, which is used here to
1976 * subtract the navigation bar/status bar size from the screen size reported
1977 * to the application. See {@link IActivityManager#resizeDockedStack}.
Andrii Kulian8072d112016-09-16 11:11:01 -07001978 * @return True if the override configuration was updated.
Jorim Jaggi0a932142016-02-01 17:42:25 -08001979 */
Andrii Kulian8072d112016-09-16 11:11:01 -07001980 boolean updateOverrideConfiguration(Rect bounds, @Nullable Rect insetBounds) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001981 if (Objects.equals(mBounds, bounds)) {
Andrii Kulian8072d112016-09-16 11:11:01 -07001982 return false;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001983 }
Andrii Kulian1779e612016-10-12 21:58:25 -07001984 mTmpConfig.setTo(getOverrideConfiguration());
Wale Ogunwale5f986092015-12-04 15:35:38 -08001985 final boolean oldFullscreen = mFullscreen;
Andrii Kulian1779e612016-10-12 21:58:25 -07001986 final Configuration newConfig = getOverrideConfiguration();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001987
1988 mFullscreen = bounds == null;
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001989 final boolean persistBounds = getWindowConfiguration().persistTaskBounds();
Wale Ogunwale706ed792015-08-02 10:29:44 -07001990 if (mFullscreen) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001991 if (mBounds != null && persistBounds) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001992 mLastNonFullscreenBounds = mBounds;
1993 }
1994 mBounds = null;
Andrii Kulian1779e612016-10-12 21:58:25 -07001995 newConfig.unset();
Wale Ogunwale706ed792015-08-02 10:29:44 -07001996 } else {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001997 mTmpRect.set(bounds);
1998 adjustForMinimalTaskDimensions(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -08001999 if (mBounds == null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002000 mBounds = new Rect(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -08002001 } else {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002002 mBounds.set(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -08002003 }
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002004 if (mStack == null || persistBounds) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002005 mLastNonFullscreenBounds = mBounds;
2006 }
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002007 computeOverrideConfiguration(newConfig, mTmpRect, insetBounds,
Andrii Kuliandaea3572016-04-08 13:20:51 -07002008 mTmpRect.right != bounds.right, mTmpRect.bottom != bounds.bottom);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002009 }
Andrii Kulian1779e612016-10-12 21:58:25 -07002010 onOverrideConfigurationChanged(newConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002011
2012 if (mFullscreen != oldFullscreen) {
Winson Chung5af42fc2017-03-24 17:11:33 -07002013 mService.mStackSupervisor.scheduleUpdateMultiWindowMode(this);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002014 }
2015
Andrii Kulian1779e612016-10-12 21:58:25 -07002016 return !mTmpConfig.equals(newConfig);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002017 }
2018
Andrii Kulian1779e612016-10-12 21:58:25 -07002019 /** Clears passed config and fills it with new override values. */
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002020 // TODO(b/36505427): TaskRecord.computeOverrideConfiguration() is a utility method that doesn't
2021 // depend on task or stacks, but uses those object to get the display to base the calculation
2022 // on. Probably best to centralize calculations like this in ConfigurationContainer.
2023 void computeOverrideConfiguration(Configuration config, Rect bounds, Rect insetBounds,
Andrii Kuliana8a9bc52016-10-14 11:00:13 -07002024 boolean overrideWidth, boolean overrideHeight) {
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002025 mTmpNonDecorBounds.set(bounds);
2026 mTmpStableBounds.set(bounds);
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002027
Andrii Kulian1779e612016-10-12 21:58:25 -07002028 config.unset();
Winson Chungbdc646f2017-02-13 12:12:22 -08002029 final Configuration parentConfig = getParent().getConfiguration();
Bryce Lee7566d762017-03-30 09:34:15 -07002030
Andrii Kulian1779e612016-10-12 21:58:25 -07002031 final float density = parentConfig.densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002032
Winson Chungbdc646f2017-02-13 12:12:22 -08002033 if (mStack != null) {
2034 final StackWindowController stackController = mStack.getWindowContainerController();
2035 stackController.adjustConfigurationForBounds(bounds, insetBounds,
2036 mTmpNonDecorBounds, mTmpStableBounds, overrideWidth, overrideHeight, density,
2037 config, parentConfig);
2038 } else {
Bryce Lee7566d762017-03-30 09:34:15 -07002039 throw new IllegalArgumentException("Expected stack when calculating override config");
Winson Chungbdc646f2017-02-13 12:12:22 -08002040 }
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002041
Winson Chung60c1aba2017-03-14 17:47:42 -07002042 config.orientation = (config.screenWidthDp <= config.screenHeightDp)
2043 ? Configuration.ORIENTATION_PORTRAIT
2044 : Configuration.ORIENTATION_LANDSCAPE;
2045
Jorim Jaggi85639432016-05-06 17:27:55 -07002046 // For calculating screen layout, we need to use the non-decor inset screen area for the
2047 // calculation for compatibility reasons, i.e. screen area without system bars that could
2048 // never go away in Honeycomb.
Winson Chungbdc646f2017-02-13 12:12:22 -08002049 final int compatScreenWidthDp = (int) (mTmpNonDecorBounds.width() / density);
2050 final int compatScreenHeightDp = (int) (mTmpNonDecorBounds.height() / density);
Andrii Kulian1779e612016-10-12 21:58:25 -07002051 // We're only overriding LONG, SIZE and COMPAT parts of screenLayout, so we start override
2052 // calculation with partial default.
2053 final int sl = Configuration.SCREENLAYOUT_LONG_YES | Configuration.SCREENLAYOUT_SIZE_XLARGE;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002054 final int longSize = Math.max(compatScreenHeightDp, compatScreenWidthDp);
Andrii Kulian1779e612016-10-12 21:58:25 -07002055 final int shortSize = Math.min(compatScreenHeightDp, compatScreenWidthDp);
Jorim Jaggi85639432016-05-06 17:27:55 -07002056 config.screenLayout = Configuration.reduceScreenLayout(sl, longSize, shortSize);
2057
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08002058 }
2059
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002060 Rect updateOverrideConfigurationFromLaunchBounds() {
2061 final Rect bounds = validateBounds(getLaunchBounds());
2062 updateOverrideConfiguration(bounds);
Andrii Kulian73336d812016-03-24 12:56:08 -07002063 if (bounds != null) {
2064 bounds.set(mBounds);
2065 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002066 return bounds;
2067 }
2068
2069 static Rect validateBounds(Rect bounds) {
2070 if (bounds != null && bounds.isEmpty()) {
2071 Slog.wtf(TAG, "Received strange task bounds: " + bounds, new Throwable());
2072 return null;
2073 }
2074 return bounds;
2075 }
2076
Wale Ogunwale935e5022015-11-10 12:36:10 -08002077 /** Updates the task's bounds and override configuration to match what is expected for the
2078 * input stack. */
2079 void updateOverrideConfigurationForStack(ActivityStack inStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002080 if (mStack != null && mStack == inStack) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08002081 return;
2082 }
2083
2084 if (inStack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002085 if (!isResizeable()) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08002086 throw new IllegalArgumentException("Can not position non-resizeable task="
2087 + this + " in stack=" + inStack);
2088 }
2089 if (mBounds != null) {
2090 return;
2091 }
2092 if (mLastNonFullscreenBounds != null) {
2093 updateOverrideConfiguration(mLastNonFullscreenBounds);
2094 } else {
2095 inStack.layoutTaskInStack(this, null);
2096 }
2097 } else {
2098 updateOverrideConfiguration(inStack.mBounds);
2099 }
2100 }
2101
Chong Zhang0fa656b2015-08-31 15:17:21 -07002102 /**
2103 * Returns the correct stack to use based on task type and currently set bounds,
2104 * regardless of the focused stack and current stack association of the task.
2105 * The task will be moved (and stack focus changed) later if necessary.
2106 */
2107 int getLaunchStackId() {
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002108 if (isRecentsTask()) {
2109 return RECENTS_STACK_ID;
2110 }
2111 if (isHomeTask()) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002112 return HOME_STACK_ID;
2113 }
Winson Chung83471632016-12-13 11:02:12 -08002114 if (isAssistantTask()) {
2115 return ASSISTANT_STACK_ID;
2116 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002117 if (mBounds != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002118 return FREEFORM_WORKSPACE_STACK_ID;
2119 }
2120 return FULLSCREEN_WORKSPACE_STACK_ID;
2121 }
2122
2123 /** Returns the bounds that should be used to launch this task. */
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002124 private Rect getLaunchBounds() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002125 if (mStack == null) {
Chong Zhang7d5f5102016-01-13 10:29:24 -08002126 return null;
2127 }
2128
Andrii Kulian02b7a832016-10-06 23:11:56 -07002129 final int stackId = mStack.mStackId;
Chong Zhang7d5f5102016-01-13 10:29:24 -08002130 if (stackId == HOME_STACK_ID
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002131 || stackId == RECENTS_STACK_ID
Winson Chung83471632016-12-13 11:02:12 -08002132 || stackId == ASSISTANT_STACK_ID
Chong Zhang7d5f5102016-01-13 10:29:24 -08002133 || stackId == FULLSCREEN_WORKSPACE_STACK_ID
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002134 || (stackId == DOCKED_STACK_ID && !isResizeable())) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002135 return isResizeable() ? mStack.mBounds : null;
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002136 } else if (!getWindowConfiguration().persistTaskBounds()) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002137 return mStack.mBounds;
Wale Ogunwale706ed792015-08-02 10:29:44 -07002138 }
2139 return mLastNonFullscreenBounds;
2140 }
2141
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01002142 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
2143 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2144 final ActivityRecord r = mActivities.get(activityNdx);
2145 if (r.visible) {
2146 r.showStartingWindow(null /* prev */, false /* newTask */, taskSwitch);
2147 }
2148 }
2149 }
2150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002152 pw.print(prefix); pw.print("userId="); pw.print(userId);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07002153 pw.print(" effectiveUid="); UserHandle.formatUid(pw, effectiveUid);
2154 pw.print(" mCallingUid="); UserHandle.formatUid(pw, mCallingUid);
Suprabh Shukla7745c142016-03-07 18:21:10 -08002155 pw.print(" mUserSetupComplete="); pw.print(mUserSetupComplete);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002156 pw.print(" mCallingPackage="); pw.println(mCallingPackage);
Dianne Hackborn79228822014-09-16 11:11:23 -07002157 if (affinity != null || rootAffinity != null) {
2158 pw.print(prefix); pw.print("affinity="); pw.print(affinity);
2159 if (affinity == null || !affinity.equals(rootAffinity)) {
2160 pw.print(" root="); pw.println(rootAffinity);
2161 } else {
2162 pw.println();
2163 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002164 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002165 if (voiceSession != null || voiceInteractor != null) {
2166 pw.print(prefix); pw.print("VOICE: session=0x");
2167 pw.print(Integer.toHexString(System.identityHashCode(voiceSession)));
2168 pw.print(" interactor=0x");
2169 pw.println(Integer.toHexString(System.identityHashCode(voiceInteractor)));
2170 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002171 if (intent != null) {
2172 StringBuilder sb = new StringBuilder(128);
2173 sb.append(prefix); sb.append("intent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08002174 intent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002175 sb.append('}');
2176 pw.println(sb.toString());
2177 }
2178 if (affinityIntent != null) {
2179 StringBuilder sb = new StringBuilder(128);
2180 sb.append(prefix); sb.append("affinityIntent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08002181 affinityIntent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002182 sb.append('}');
2183 pw.println(sb.toString());
2184 }
2185 if (origActivity != null) {
2186 pw.print(prefix); pw.print("origActivity=");
2187 pw.println(origActivity.flattenToShortString());
2188 }
2189 if (realActivity != null) {
2190 pw.print(prefix); pw.print("realActivity=");
2191 pw.println(realActivity.flattenToShortString());
2192 }
Dianne Hackborn852975d2014-08-22 17:42:43 -07002193 if (autoRemoveRecents || isPersistable || taskType != 0 || mTaskToReturnTo != 0
2194 || numFullscreen != 0) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002195 pw.print(prefix); pw.print("autoRemoveRecents="); pw.print(autoRemoveRecents);
Dianne Hackborn852975d2014-08-22 17:42:43 -07002196 pw.print(" isPersistable="); pw.print(isPersistable);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002197 pw.print(" numFullscreen="); pw.print(numFullscreen);
2198 pw.print(" taskType="); pw.print(taskType);
2199 pw.print(" mTaskToReturnTo="); pw.println(mTaskToReturnTo);
2200 }
Craig Mautner432f64e2015-05-20 14:59:57 -07002201 if (rootWasReset || mNeverRelinquishIdentity || mReuseTask
2202 || mLockTaskAuth != LOCK_TASK_AUTH_PINNABLE) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002203 pw.print(prefix); pw.print("rootWasReset="); pw.print(rootWasReset);
2204 pw.print(" mNeverRelinquishIdentity="); pw.print(mNeverRelinquishIdentity);
Craig Mautner432f64e2015-05-20 14:59:57 -07002205 pw.print(" mReuseTask="); pw.print(mReuseTask);
2206 pw.print(" mLockTaskAuth="); pw.println(lockTaskAuthToString());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002207 }
Wale Ogunwale18795a22014-12-03 11:38:33 -08002208 if (mAffiliatedTaskId != taskId || mPrevAffiliateTaskId != INVALID_TASK_ID
2209 || mPrevAffiliate != null || mNextAffiliateTaskId != INVALID_TASK_ID
2210 || mNextAffiliate != null) {
Dianne Hackborn852975d2014-08-22 17:42:43 -07002211 pw.print(prefix); pw.print("affiliation="); pw.print(mAffiliatedTaskId);
2212 pw.print(" prevAffiliation="); pw.print(mPrevAffiliateTaskId);
2213 pw.print(" (");
2214 if (mPrevAffiliate == null) {
2215 pw.print("null");
2216 } else {
2217 pw.print(Integer.toHexString(System.identityHashCode(mPrevAffiliate)));
2218 }
2219 pw.print(") nextAffiliation="); pw.print(mNextAffiliateTaskId);
2220 pw.print(" (");
2221 if (mNextAffiliate == null) {
2222 pw.print("null");
2223 } else {
2224 pw.print(Integer.toHexString(System.identityHashCode(mNextAffiliate)));
2225 }
2226 pw.println(")");
2227 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002228 pw.print(prefix); pw.print("Activities="); pw.println(mActivities);
Dianne Hackborn852975d2014-08-22 17:42:43 -07002229 if (!askedCompatMode || !inRecents || !isAvailable) {
2230 pw.print(prefix); pw.print("askedCompatMode="); pw.print(askedCompatMode);
2231 pw.print(" inRecents="); pw.print(inRecents);
2232 pw.print(" isAvailable="); pw.println(isAvailable);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07002233 }
Dianne Hackborn852975d2014-08-22 17:42:43 -07002234 if (lastDescription != null) {
2235 pw.print(prefix); pw.print("lastDescription="); pw.println(lastDescription);
2236 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002237 pw.print(prefix); pw.print("stackId="); pw.println(getStackId());
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002238 pw.print(prefix + "hasBeenVisible=" + hasBeenVisible);
2239 pw.print(" mResizeMode=" + ActivityInfo.resizeModeToString(mResizeMode));
Winson Chungd3395382016-12-13 11:49:09 -08002240 pw.print(" mSupportsPictureInPicture=" + mSupportsPictureInPicture);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002241 pw.print(" isResizeable=" + isResizeable());
daqi961e50e2017-05-30 15:37:23 +08002242 pw.print(" firstActiveTime=" + firstActiveTime);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002243 pw.print(" lastActiveTime=" + lastActiveTime);
2244 pw.println(" (inactive for " + (getInactiveDuration() / 1000) + "s)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 }
2246
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002247 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002249 StringBuilder sb = new StringBuilder(128);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002250 if (stringName != null) {
2251 sb.append(stringName);
2252 sb.append(" U=");
2253 sb.append(userId);
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002254 sb.append(" StackId=");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002255 sb.append(getStackId());
Craig Mautnerde4ef022013-04-07 19:01:33 -07002256 sb.append(" sz=");
2257 sb.append(mActivities.size());
2258 sb.append('}');
2259 return sb.toString();
2260 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002261 sb.append("TaskRecord{");
2262 sb.append(Integer.toHexString(System.identityHashCode(this)));
2263 sb.append(" #");
2264 sb.append(taskId);
2265 if (affinity != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002266 sb.append(" A=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002267 sb.append(affinity);
2268 } else if (intent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002269 sb.append(" I=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002270 sb.append(intent.getComponent().flattenToShortString());
2271 } else if (affinityIntent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002272 sb.append(" aI=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002273 sb.append(affinityIntent.getComponent().flattenToShortString());
2274 } else {
2275 sb.append(" ??");
2276 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002277 stringName = sb.toString();
2278 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 }
2280}