blob: 815b9d8ed2b6edf518992503975aaf3b50ede5af [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
Winson Chung74666102017-02-22 17:49:24 -080019import android.annotation.IntDef;
Jorim Jaggi0a932142016-02-01 17:42:25 -080020import android.annotation.Nullable;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -070021import android.app.Activity;
Craig Mautner9db9a0b2013-04-29 17:05:56 -070022import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070023import android.app.ActivityManager.StackId;
Craig Mautner648f69b2014-09-18 14:16:26 -070024import android.app.ActivityManager.TaskDescription;
Jorim Jaggie2c77f92016-12-29 14:57:22 +010025import android.app.ActivityManager.TaskSnapshot;
Wale Ogunwaleff3c66c2015-11-18 19:22:49 -080026import android.app.ActivityManager.TaskThumbnail;
Winsonc809cbb2015-11-02 12:06:15 -080027import android.app.ActivityManager.TaskThumbnailInfo;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -070028import android.app.ActivityOptions;
Dianne Hackborn885fbe52014-08-23 15:23:58 -070029import android.app.AppGlobals;
Jorim Jaggi0a932142016-02-01 17:42:25 -080030import android.app.IActivityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.content.ComponentName;
32import android.content.Intent;
33import android.content.pm.ActivityInfo;
Dianne Hackborn885fbe52014-08-23 15:23:58 -070034import android.content.pm.ApplicationInfo;
35import android.content.pm.IPackageManager;
36import android.content.pm.PackageManager;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070037import android.content.res.Configuration;
Craig Mautner9db9a0b2013-04-29 17:05:56 -070038import android.graphics.Bitmap;
Winsonc809cbb2015-11-02 12:06:15 -080039import android.graphics.Point;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070040import android.graphics.Rect;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070041import android.os.Debug;
Craig Mautnerc0ffce52014-07-01 12:38:52 -070042import android.os.ParcelFileDescriptor;
Dianne Hackborn885fbe52014-08-23 15:23:58 -070043import android.os.RemoteException;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080044import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070045import android.os.UserHandle;
Suprabh Shukla7745c142016-03-07 18:21:10 -080046import android.provider.Settings;
Dianne Hackborn91097de2014-04-04 18:02:06 -070047import android.service.voice.IVoiceInteractionSession;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070048import android.util.DisplayMetrics;
Dianne Hackborn7f96b792012-05-29 18:46:45 -070049import android.util.Slog;
Suprabh Shukla23593142015-11-03 17:31:15 -080050
Dianne Hackborn91097de2014-04-04 18:02:06 -070051import com.android.internal.app.IVoiceInteractor;
Craig Mautner21d24a22014-04-23 11:45:37 -070052import com.android.internal.util.XmlUtils;
Suprabh Shukla23593142015-11-03 17:31:15 -080053
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -080054import com.android.server.wm.AppWindowContainerController;
Winson Chungbdc646f2017-02-13 12:12:22 -080055import com.android.server.wm.StackWindowController;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080056import com.android.server.wm.TaskWindowContainerController;
Jorim Jaggifb9d78a2017-01-05 18:57:12 +010057import com.android.server.wm.TaskWindowContainerListener;
Winson Chung74666102017-02-22 17:49:24 -080058import com.android.server.wm.WindowManagerService;
Jorim Jaggifb9d78a2017-01-05 18:57:12 +010059
Craig Mautner21d24a22014-04-23 11:45:37 -070060import org.xmlpull.v1.XmlPullParser;
61import org.xmlpull.v1.XmlPullParserException;
62import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
Craig Mautnerc0ffce52014-07-01 12:38:52 -070064import java.io.File;
Craig Mautner21d24a22014-04-23 11:45:37 -070065import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import java.io.PrintWriter;
Winson Chung74666102017-02-22 17:49:24 -080067import java.lang.annotation.Retention;
68import java.lang.annotation.RetentionPolicy;
Craig Mautner5d9c7be2013-02-15 14:02:56 -080069import java.util.ArrayList;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070070import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080072import static android.app.ActivityManager.RESIZE_MODE_FORCED;
Winson Chung74666102017-02-22 17:49:24 -080073import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
Winson Chung83471632016-12-13 11:02:12 -080074import static android.app.ActivityManager.StackId.ASSISTANT_STACK_ID;
Jorim Jaggi0a932142016-02-01 17:42:25 -080075import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
76import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
77import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
78import static android.app.ActivityManager.StackId.HOME_STACK_ID;
79import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
80import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -080081import static android.app.ActivityManager.StackId.RECENTS_STACK_ID;
Jorim Jaggi0a932142016-02-01 17:42:25 -080082import static android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
83import static android.content.Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
Wale Ogunwale3eadad72016-10-13 09:16:59 -070084import static android.content.pm.ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY;
Jorim Jaggi0a932142016-02-01 17:42:25 -080085import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_ALWAYS;
86import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_DEFAULT;
87import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
88import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_NEVER;
skuhne@google.com322347b2016-12-02 12:54:03 -080089import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZABLE_LANDSCAPE_ONLY;
90import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZABLE_PORTRAIT_ONLY;
91import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZABLE_PRESERVE_ORIENTATION;
Wale Ogunwaled829d362016-02-10 19:24:49 -080092import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Wale Ogunwale625ed0c2016-10-18 08:50:31 -070093import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE;
Winson Chungd3395382016-12-13 11:49:09 -080094import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_AND_PIPABLE_DEPRECATED;
Wale Ogunwale625ed0c2016-10-18 08:50:31 -070095import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Jorim Jaggi0a932142016-02-01 17:42:25 -080096import static android.content.pm.ApplicationInfo.PRIVATE_FLAG_PRIVILEGED;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080097import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
Suprabh Shukla7745c142016-03-07 18:21:10 -080098import static android.provider.Settings.Secure.USER_SETUP_COMPLETE;
Jorim Jaggi0a932142016-02-01 17:42:25 -080099import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ADD_REMOVE;
100import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
101import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
102import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
103import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_ADD_REMOVE;
104import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
105import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
106import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
107import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
108import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Jorim Jaggi0a932142016-02-01 17:42:25 -0800109import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Winson Chung83471632016-12-13 11:02:12 -0800110import static com.android.server.am.ActivityRecord.ASSISTANT_ACTIVITY_TYPE;
Jorim Jaggi0a932142016-02-01 17:42:25 -0800111import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
112import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
Wale Ogunwale3b232392016-05-13 15:37:13 -0700113import static com.android.server.am.ActivityRecord.STARTING_WINDOW_SHOWN;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800114import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
Winson Chung6954fc92017-03-24 16:22:12 -0700115import static com.android.server.am.ActivityStackSupervisor.PAUSE_IMMEDIATELY;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800116import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Jorim Jaggi0a932142016-02-01 17:42:25 -0800117
Winson Chung74666102017-02-22 17:49:24 -0800118import static java.lang.Integer.MAX_VALUE;
119
Jorim Jaggifb9d78a2017-01-05 18:57:12 +0100120final class TaskRecord extends ConfigurationContainer implements TaskWindowContainerListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800121 private static final String TAG = TAG_WITH_CLASS_NAME ? "TaskRecord" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700122 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700123 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Craig Mautnere0570202015-05-13 13:06:11 -0700124 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700125 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800126
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700127 private static final String ATTR_TASKID = "task_id";
Craig Mautner21d24a22014-04-23 11:45:37 -0700128 private static final String TAG_INTENT = "intent";
129 private static final String TAG_AFFINITYINTENT = "affinity_intent";
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700130 private static final String ATTR_REALACTIVITY = "real_activity";
131 private static final String ATTR_REALACTIVITY_SUSPENDED = "real_activity_suspended";
Craig Mautner21d24a22014-04-23 11:45:37 -0700132 private static final String ATTR_ORIGACTIVITY = "orig_activity";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700133 private static final String TAG_ACTIVITY = "activity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700134 private static final String ATTR_AFFINITY = "affinity";
Dianne Hackborn79228822014-09-16 11:11:23 -0700135 private static final String ATTR_ROOT_AFFINITY = "root_affinity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700136 private static final String ATTR_ROOTHASRESET = "root_has_reset";
Dianne Hackborn13420f22014-07-18 15:43:56 -0700137 private static final String ATTR_AUTOREMOVERECENTS = "auto_remove_recents";
Craig Mautner21d24a22014-04-23 11:45:37 -0700138 private static final String ATTR_ASKEDCOMPATMODE = "asked_compat_mode";
139 private static final String ATTR_USERID = "user_id";
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800140 private static final String ATTR_USER_SETUP_COMPLETE = "user_setup_complete";
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700141 private static final String ATTR_EFFECTIVE_UID = "effective_uid";
Craig Mautner21d24a22014-04-23 11:45:37 -0700142 private static final String ATTR_TASKTYPE = "task_type";
Winson Chungffa2ec62014-07-03 15:54:42 -0700143 private static final String ATTR_FIRSTACTIVETIME = "first_active_time";
Winson Chungf1fbd772014-06-24 18:06:58 -0700144 private static final String ATTR_LASTACTIVETIME = "last_active_time";
Craig Mautner21d24a22014-04-23 11:45:37 -0700145 private static final String ATTR_LASTDESCRIPTION = "last_description";
146 private static final String ATTR_LASTTIMEMOVED = "last_time_moved";
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700147 private static final String ATTR_NEVERRELINQUISH = "never_relinquish_identity";
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700148 private static final String ATTR_TASK_AFFILIATION = "task_affiliation";
Craig Mautnera228ae92014-07-09 05:44:55 -0700149 private static final String ATTR_PREV_AFFILIATION = "prev_affiliation";
150 private static final String ATTR_NEXT_AFFILIATION = "next_affiliation";
Winson Chungec396d62014-08-06 17:08:00 -0700151 private static final String ATTR_TASK_AFFILIATION_COLOR = "task_affiliation_color";
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700152 private static final String ATTR_CALLING_UID = "calling_uid";
153 private static final String ATTR_CALLING_PACKAGE = "calling_package";
Winson Chungd3395382016-12-13 11:49:09 -0800154 private static final String ATTR_SUPPORTS_PICTURE_IN_PICTURE = "supports_picture_in_picture";
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800155 private static final String ATTR_RESIZE_MODE = "resize_mode";
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700156 private static final String ATTR_PRIVILEGED = "privileged";
Wale Ogunwale706ed792015-08-02 10:29:44 -0700157 private static final String ATTR_NON_FULLSCREEN_BOUNDS = "non_fullscreen_bounds";
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700158 private static final String ATTR_MIN_WIDTH = "min_width";
159 private static final String ATTR_MIN_HEIGHT = "min_height";
Wale Ogunwale625ed0c2016-10-18 08:50:31 -0700160 private static final String ATTR_PERSIST_TASK_VERSION = "persist_task_version";
Andrii Kulian18d75122016-03-27 20:20:28 -0700161
Wale Ogunwale625ed0c2016-10-18 08:50:31 -0700162 // Current version of the task record we persist. Used to check if we need to run any upgrade
163 // code.
164 private static final int PERSIST_TASK_VERSION = 1;
Craig Mautner21d24a22014-04-23 11:45:37 -0700165 private static final String TASK_THUMBNAIL_SUFFIX = "_task_thumbnail";
166
Wale Ogunwale18795a22014-12-03 11:38:33 -0800167 static final int INVALID_TASK_ID = -1;
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700168 private static final int INVALID_MIN_SIZE = -1;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800169
Winson Chung74666102017-02-22 17:49:24 -0800170 /**
171 * The modes to control how the stack is moved to the front when calling
172 * {@link TaskRecord#reparent}.
173 */
174 @Retention(RetentionPolicy.SOURCE)
175 @IntDef({
176 REPARENT_MOVE_STACK_TO_FRONT,
177 REPARENT_KEEP_STACK_AT_FRONT,
178 REPARENT_LEAVE_STACK_IN_PLACE
179 })
180 public @interface ReparentMoveStackMode {}
181 // Moves the stack to the front if it was not at the front
182 public static final int REPARENT_MOVE_STACK_TO_FRONT = 0;
183 // Only moves the stack to the front if it was focused or front most already
184 public static final int REPARENT_KEEP_STACK_AT_FRONT = 1;
185 // Do not move the stack as a part of reparenting
186 public static final int REPARENT_LEAVE_STACK_IN_PLACE = 2;
187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 final int taskId; // Unique identifier for this task.
Dianne Hackborn79228822014-09-16 11:11:23 -0700189 String affinity; // The affinity name for this task, or null; may change identity.
190 String rootAffinity; // Initial base affinity, or null; does not change from initial root.
Dianne Hackborn91097de2014-04-04 18:02:06 -0700191 final IVoiceInteractionSession voiceSession; // Voice interaction session driving task
192 final IVoiceInteractor voiceInteractor; // Associated interactor to provide to app
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 Intent intent; // The original intent that started the task.
194 Intent affinityIntent; // Intent of affinity-moved activity that started this task.
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700195 int effectiveUid; // The current effective uid of the identity of this task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 ComponentName origActivity; // The non-alias activity component of the intent.
197 ComponentName realActivity; // The actual activity component that started the task.
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +0000198 boolean realActivitySuspended; // True if the actual activity component that started the
199 // task is suspended.
Winson Chung36f3f032016-09-08 23:29:43 +0000200 long firstActiveTime; // First time this task was active.
201 long lastActiveTime; // Last time this task was active, including sleep.
Dianne Hackborn852975d2014-08-22 17:42:43 -0700202 boolean inRecents; // Actually in the recents list?
203 boolean isAvailable; // Is the activity available to be launched?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 boolean rootWasReset; // True if the intent at the root of the task had
205 // the FLAG_ACTIVITY_RESET_TASK_IF_NEEDED flag.
Dianne Hackborn13420f22014-07-18 15:43:56 -0700206 boolean autoRemoveRecents; // If true, we should automatically remove the task from
207 // recents when activity finishes
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700208 boolean askedCompatMode;// Have asked the user about compat mode for this task.
Dianne Hackbornd38aed82014-06-10 21:36:35 -0700209 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 -0800210
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700211 String stringName; // caching of toString() result.
Dianne Hackborn9da2d402012-03-15 13:43:08 -0700212 int userId; // user for which this task was created
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800213 boolean mUserSetupComplete; // The user set-up is complete as of the last time the task activity
214 // was changed.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800215
216 int numFullscreen; // Number of fullscreen activities.
217
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800218 int mResizeMode; // The resize mode of this task and its activities.
219 // Based on the {@link ActivityInfo#resizeMode} of the root activity.
Wale Ogunwale069bbd32017-02-03 07:58:14 -0800220 private boolean mSupportsPictureInPicture; // Whether or not this task and its activities
221 // support PiP. Based on the {@link ActivityInfo#FLAG_SUPPORTS_PICTURE_IN_PICTURE} flag
222 // of the root activity.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -0800223 boolean mTemporarilyUnresizable; // Separate flag from mResizeMode used to suppress resize
224 // changes on a temporary basis.
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700225 private int mLockTaskMode; // Which tasklock mode to launch this task in. One of
226 // ActivityManager.LOCK_TASK_LAUNCH_MODE_*
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700227 private boolean mPrivileged; // The root activity application of this task holds
228 // privileged permissions.
229
Craig Mautner15df08a2015-04-01 12:17:18 -0700230 /** Can't be put in lockTask mode. */
231 final static int LOCK_TASK_AUTH_DONT_LOCK = 0;
Benjamin Franz469dd582015-06-09 14:24:36 +0100232 /** Can enter app pinning with user approval. Can never start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700233 final static int LOCK_TASK_AUTH_PINNABLE = 1;
234 /** Starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing lockTask task. */
235 final static int LOCK_TASK_AUTH_LAUNCHABLE = 2;
Benjamin Franz469dd582015-06-09 14:24:36 +0100236 /** Can enter lockTask without user approval. Can start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700237 final static int LOCK_TASK_AUTH_WHITELISTED = 3;
Benjamin Franz469dd582015-06-09 14:24:36 +0100238 /** Priv-app that starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing
239 * lockTask task. */
240 final static int LOCK_TASK_AUTH_LAUNCHABLE_PRIV = 4;
Craig Mautner15df08a2015-04-01 12:17:18 -0700241 int mLockTaskAuth = LOCK_TASK_AUTH_PINNABLE;
242
243 int mLockTaskUid = -1; // The uid of the application that called startLockTask().
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800244
Winson Chung03a9bae2014-05-02 09:56:12 -0700245 // This represents the last resolved activity values for this task
246 // NOTE: This value needs to be persisted with each task
Craig Mautner648f69b2014-09-18 14:16:26 -0700247 TaskDescription lastTaskDescription = new TaskDescription();
Winson Chung03a9bae2014-05-02 09:56:12 -0700248
Craig Mautnerd2328952013-03-05 12:46:26 -0800249 /** List of all activities in the task arranged in history order */
Craig Mautner21d24a22014-04-23 11:45:37 -0700250 final ArrayList<ActivityRecord> mActivities;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800251
Andrii Kulian02b7a832016-10-06 23:11:56 -0700252 /** Current stack. Setter must always be used to update the value. */
253 private ActivityStack mStack;
Craig Mautnerd2328952013-03-05 12:46:26 -0800254
Craig Mautner2c1faed2013-07-23 12:56:02 -0700255 /** Takes on same set of values as ActivityRecord.mActivityType */
Craig Mautner21d24a22014-04-23 11:45:37 -0700256 int taskType;
Craig Mautner1602ec22013-05-12 10:24:27 -0700257
Craig Mautner21d24a22014-04-23 11:45:37 -0700258 /** Takes on same value as first root activity */
259 boolean isPersistable = false;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700260 int maxRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700261
262 /** Only used for persistable tasks, otherwise 0. The last time this task was moved. Used for
263 * determining the order when restoring. Sign indicates whether last task movement was to front
264 * (positive) or back (negative). Absolute value indicates time. */
265 long mLastTimeMoved = System.currentTimeMillis();
266
Craig Mautner84984fa2014-06-19 11:19:20 -0700267 /** Indication of what to run next when task exits. Use ActivityRecord types.
268 * ActivityRecord.APPLICATION_ACTIVITY_TYPE indicates to resume the task below this one in the
269 * task stack. */
270 private int mTaskToReturnTo = APPLICATION_ACTIVITY_TYPE;
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700271
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700272 /** If original intent did not allow relinquishing task identity, save that information */
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700273 private boolean mNeverRelinquishIdentity = true;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700274
Craig Mautner362449a2014-06-20 14:04:39 -0700275 // Used in the unique case where we are clearing the task in order to reuse it. In that case we
276 // do not want to delete the stack when the task goes empty.
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -0800277 private boolean mReuseTask = false;
Craig Mautner362449a2014-06-20 14:04:39 -0700278
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700279 private Bitmap mLastThumbnail; // Last thumbnail captured for this item.
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700280 private final File mLastThumbnailFile; // File containing last thumbnail.
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700281 private final String mFilename;
Winsonc809cbb2015-11-02 12:06:15 -0800282 private TaskThumbnailInfo mLastThumbnailInfo;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700283 CharSequence lastDescription; // Last description captured for this item.
284
Craig Mautnera228ae92014-07-09 05:44:55 -0700285 int mAffiliatedTaskId; // taskId of parent affiliation or self if no parent.
Winson Chungec396d62014-08-06 17:08:00 -0700286 int mAffiliatedTaskColor; // color of the parent task affiliation.
Craig Mautnera228ae92014-07-09 05:44:55 -0700287 TaskRecord mPrevAffiliate; // previous task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800288 int mPrevAffiliateTaskId = INVALID_TASK_ID; // previous id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700289 TaskRecord mNextAffiliate; // next task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800290 int mNextAffiliateTaskId = INVALID_TASK_ID; // next id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700291
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700292 // For relaunching the task from recents as though it was launched by the original launcher.
293 int mCallingUid;
294 String mCallingPackage;
295
Craig Mautner21d24a22014-04-23 11:45:37 -0700296 final ActivityManagerService mService;
297
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700298 // Whether or not this task covers the entire screen; by default tasks are fullscreen.
299 boolean mFullscreen = true;
300
301 // Bounds of the Task. null for fullscreen tasks.
302 Rect mBounds = null;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -0800303 private final Rect mTmpStableBounds = new Rect();
304 private final Rect mTmpNonDecorBounds = new Rect();
Wale Ogunwale9a08f822016-02-17 19:03:58 -0800305 private final Rect mTmpRect = new Rect();
Jorim Jaggi0a932142016-02-01 17:42:25 -0800306
Wale Ogunwale706ed792015-08-02 10:29:44 -0700307 // Last non-fullscreen bounds the task was launched in or resized to.
308 // The information is persisted and used to determine the appropriate stack to launch the
309 // task into on restore.
310 Rect mLastNonFullscreenBounds = null;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700311 // Minimal width and height of this task when it's resizeable. -1 means it should use the
312 // default minimal width/height.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700313 int mMinWidth;
314 int mMinHeight;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700315
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700316 // Ranking (from top) of this task among all visible tasks. (-1 means it's not visible)
317 // This number will be assigned when we evaluate OOM scores for all visible tasks.
318 int mLayerRank = -1;
319
Andrii Kulian1779e612016-10-12 21:58:25 -0700320 /** Helper object used for updating override configuration. */
321 private Configuration mTmpConfig = new Configuration();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700322
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800323 private TaskWindowContainerController mWindowContainerController;
324
Craig Mautner21d24a22014-04-23 11:45:37 -0700325 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Wale Ogunwale72919d22016-12-08 18:58:50 -0800326 IVoiceInteractionSession _voiceSession, IVoiceInteractor _voiceInteractor, int type) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700327 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700328 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
329 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800330 userId = UserHandle.getUserId(info.applicationInfo.uid);
331 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800332 mLastThumbnailInfo = new TaskThumbnailInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 taskId = _taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700334 mAffiliatedTaskId = _taskId;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700335 voiceSession = _voiceSession;
336 voiceInteractor = _voiceInteractor;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700337 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800338 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700339 mCallingUid = info.applicationInfo.uid;
340 mCallingPackage = info.packageName;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800341 taskType = type;
Martijn Coenend4a69702014-06-30 11:12:17 -0700342 setIntent(_intent, info);
Andrii Kulian2e751b82016-03-16 16:59:32 -0700343 setMinDimensions(info);
Winson730bf062016-03-31 18:04:56 -0700344 touchActiveTime();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700345 mService.mTaskChangeNotificationController.notifyTaskCreated(_taskId, realActivity);
Craig Mautner21d24a22014-04-23 11:45:37 -0700346 }
347
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700348 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Winsonc809cbb2015-11-02 12:06:15 -0800349 TaskDescription _taskDescription, TaskThumbnailInfo thumbnailInfo) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700350 mService = service;
351 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
352 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800353 userId = UserHandle.getUserId(info.applicationInfo.uid);
354 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800355 mLastThumbnailInfo = thumbnailInfo;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700356 taskId = _taskId;
357 mAffiliatedTaskId = _taskId;
358 voiceSession = null;
359 voiceInteractor = null;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700360 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800361 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700362 mCallingUid = info.applicationInfo.uid;
363 mCallingPackage = info.packageName;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700364 setIntent(_intent, info);
Andrii Kulian2e751b82016-03-16 16:59:32 -0700365 setMinDimensions(info);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700366
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700367 isPersistable = true;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700368 // Clamp to [1, max].
369 maxRecents = Math.min(Math.max(info.maxRecents, 1),
370 ActivityManager.getMaxAppRecentsLimitStatic());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700371
372 taskType = APPLICATION_ACTIVITY_TYPE;
373 mTaskToReturnTo = HOME_ACTIVITY_TYPE;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700374 lastTaskDescription = _taskDescription;
Winson730bf062016-03-31 18:04:56 -0700375 touchActiveTime();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700376 mService.mTaskChangeNotificationController.notifyTaskCreated(_taskId, realActivity);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700377 }
378
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800379 private TaskRecord(ActivityManagerService service, int _taskId, Intent _intent,
380 Intent _affinityIntent, String _affinity, String _rootAffinity,
381 ComponentName _realActivity, ComponentName _origActivity, boolean _rootWasReset,
382 boolean _autoRemoveRecents, boolean _askedCompatMode, int _taskType, int _userId,
383 int _effectiveUid, String _lastDescription, ArrayList<ActivityRecord> activities,
384 long _firstActiveTime, long _lastActiveTime, long lastTimeMoved,
385 boolean neverRelinquishIdentity, TaskDescription _lastTaskDescription,
Winsonc809cbb2015-11-02 12:06:15 -0800386 TaskThumbnailInfo lastThumbnailInfo, int taskAffiliation, int prevTaskId,
387 int nextTaskId, int taskAffiliationColor, int callingUid, String callingPackage,
Winson Chungd3395382016-12-13 11:49:09 -0800388 int resizeMode, boolean supportsPictureInPicture, boolean privileged,
389 boolean _realActivitySuspended, boolean userSetupComplete, int minWidth,
390 int minHeight) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700391 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700392 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
393 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800394 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(_userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800395 mLastThumbnailInfo = lastThumbnailInfo;
Craig Mautner21d24a22014-04-23 11:45:37 -0700396 taskId = _taskId;
397 intent = _intent;
398 affinityIntent = _affinityIntent;
399 affinity = _affinity;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800400 rootAffinity = _rootAffinity;
Craig Mautner21d24a22014-04-23 11:45:37 -0700401 voiceSession = null;
402 voiceInteractor = null;
403 realActivity = _realActivity;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800404 realActivitySuspended = _realActivitySuspended;
Craig Mautner21d24a22014-04-23 11:45:37 -0700405 origActivity = _origActivity;
406 rootWasReset = _rootWasReset;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700407 isAvailable = true;
Dianne Hackborn13420f22014-07-18 15:43:56 -0700408 autoRemoveRecents = _autoRemoveRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700409 askedCompatMode = _askedCompatMode;
410 taskType = _taskType;
Craig Mautner84984fa2014-06-19 11:19:20 -0700411 mTaskToReturnTo = HOME_ACTIVITY_TYPE;
Craig Mautner21d24a22014-04-23 11:45:37 -0700412 userId = _userId;
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800413 mUserSetupComplete = userSetupComplete;
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700414 effectiveUid = _effectiveUid;
Winson Chungffa2ec62014-07-03 15:54:42 -0700415 firstActiveTime = _firstActiveTime;
Winson Chungf1fbd772014-06-24 18:06:58 -0700416 lastActiveTime = _lastActiveTime;
Craig Mautner21d24a22014-04-23 11:45:37 -0700417 lastDescription = _lastDescription;
418 mActivities = activities;
419 mLastTimeMoved = lastTimeMoved;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700420 mNeverRelinquishIdentity = neverRelinquishIdentity;
Winson Chung2cb86c72014-06-25 12:03:30 -0700421 lastTaskDescription = _lastTaskDescription;
Craig Mautnera228ae92014-07-09 05:44:55 -0700422 mAffiliatedTaskId = taskAffiliation;
Winson Chungec396d62014-08-06 17:08:00 -0700423 mAffiliatedTaskColor = taskAffiliationColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700424 mPrevAffiliateTaskId = prevTaskId;
425 mNextAffiliateTaskId = nextTaskId;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700426 mCallingUid = callingUid;
427 mCallingPackage = callingPackage;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800428 mResizeMode = resizeMode;
Winson Chungd3395382016-12-13 11:49:09 -0800429 mSupportsPictureInPicture = supportsPictureInPicture;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700430 mPrivileged = privileged;
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700431 mMinWidth = minWidth;
432 mMinHeight = minHeight;
Yorke Leebd54c2a2016-10-25 13:49:23 -0700433 mService.mTaskChangeNotificationController.notifyTaskCreated(_taskId, realActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 }
435
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800436 TaskWindowContainerController getWindowContainerController() {
437 return mWindowContainerController;
438 }
439
440 void createWindowContainer(boolean onTop, boolean showForAllUsers) {
441 if (mWindowContainerController != null) {
442 throw new IllegalArgumentException("Window container=" + mWindowContainerController
443 + " already created for task=" + this);
444 }
445
446 final Rect bounds = updateOverrideConfigurationFromLaunchBounds();
447 final Configuration overrideConfig = getOverrideConfiguration();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800448 mWindowContainerController = new TaskWindowContainerController(taskId, this,
449 getStack().getWindowContainerController(), userId, bounds, overrideConfig,
Wale Ogunwale069bbd32017-02-03 07:58:14 -0800450 mResizeMode, mSupportsPictureInPicture, isHomeTask(), onTop, showForAllUsers,
451 lastTaskDescription);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800452 }
453
454 void removeWindowContainer() {
455 mService.mStackSupervisor.removeLockedTaskLocked(this);
456 mWindowContainerController.removeContainer();
457 if (!StackId.persistTaskBounds(getStackId())) {
458 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
459 // default configuration the next time it launches.
460 updateOverrideConfiguration(null);
461 }
462 mService.mTaskChangeNotificationController.notifyTaskRemoved(taskId);
463 mWindowContainerController = null;
464 }
465
Jorim Jaggifb9d78a2017-01-05 18:57:12 +0100466 @Override
467 public void onSnapshotChanged(TaskSnapshot snapshot) {
468 mService.mTaskChangeNotificationController.notifyTaskSnapshotChanged(taskId, snapshot);
469 }
470
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800471 void setResizeMode(int resizeMode) {
472 if (mResizeMode == resizeMode) {
473 return;
474 }
475 mResizeMode = resizeMode;
476 mWindowContainerController.setResizeable(resizeMode);
477 mService.mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
478 mService.mStackSupervisor.resumeFocusedStackTopActivityLocked();
479 }
480
481 void setTaskDockedResizing(boolean resizing) {
482 mWindowContainerController.setTaskDockedResizing(resizing);
483 }
484
Wale Ogunwale1666e312016-12-16 11:27:18 -0800485 // TODO: Consolidate this with the resize() method below.
486 @Override
487 public void requestResize(Rect bounds, int resizeMode) {
488 mService.resizeTask(taskId, bounds, resizeMode);
489 }
490
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800491 boolean resize(Rect bounds, int resizeMode, boolean preserveWindow, boolean deferResume) {
492 if (!isResizeable()) {
493 Slog.w(TAG, "resizeTask: task " + this + " not resizeable.");
494 return true;
495 }
496
497 // If this is a forced resize, let it go through even if the bounds is not changing,
498 // as we might need a relayout due to surface size change (to/from fullscreen).
499 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
500 if (Objects.equals(mBounds, bounds) && !forced) {
501 // Nothing to do here...
502 return true;
503 }
504 bounds = validateBounds(bounds);
505
506 if (mWindowContainerController == null) {
507 // Task doesn't exist in window manager yet (e.g. was restored from recents).
508 // All we can do for now is update the bounds so it can be used when the task is
509 // added to window manager.
510 updateOverrideConfiguration(bounds);
511 if (getStackId() != FREEFORM_WORKSPACE_STACK_ID) {
512 // re-restore the task so it can have the proper stack association.
513 mService.mStackSupervisor.restoreRecentTaskLocked(this,
514 FREEFORM_WORKSPACE_STACK_ID);
515 }
516 return true;
517 }
518
519 if (!canResizeToBounds(bounds)) {
520 throw new IllegalArgumentException("resizeTask: Can not resize task=" + this
521 + " to bounds=" + bounds + " resizeMode=" + mResizeMode);
522 }
523
524 // Do not move the task to another stack here.
525 // This method assumes that the task is already placed in the right stack.
526 // we do not mess with that decision and we only do the resize!
527
528 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + taskId);
529
530 final boolean updatedConfig = updateOverrideConfiguration(bounds);
531 // This variable holds information whether the configuration didn't change in a significant
532 // way and the activity was kept the way it was. If it's false, it means the activity had
533 // to be relaunched due to configuration change.
534 boolean kept = true;
535 if (updatedConfig) {
536 final ActivityRecord r = topRunningActivityLocked();
537 if (r != null) {
538 kept = r.ensureActivityConfigurationLocked(0 /* globalChanges */, preserveWindow);
539
540 if (!deferResume) {
541 // All other activities must be made visible with their correct configuration.
542 mService.mStackSupervisor.ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
543 if (!kept) {
544 mService.mStackSupervisor.resumeFocusedStackTopActivityLocked();
545 }
546 }
547 }
548 }
549 mWindowContainerController.resize(mBounds, getOverrideConfiguration(), kept, forced);
550
551 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
552 return kept;
553 }
554
555 // TODO: Investigate combining with the resize() method above.
556 void resizeWindowContainer() {
557 mWindowContainerController.resize(mBounds, getOverrideConfiguration(), false /* relayout */,
558 false /* forced */);
559 }
560
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800561 void getWindowContainerBounds(Rect bounds) {
562 mWindowContainerController.getBounds(bounds);
563 }
564
Winson Chung74666102017-02-22 17:49:24 -0800565 /**
566 * Convenience method to reparent a task to the top or bottom position of the stack.
567 */
568 boolean reparent(int preferredStackId, boolean toTop, @ReparentMoveStackMode int moveStackMode,
569 boolean animate, boolean deferResume, String reason) {
570 return reparent(preferredStackId, toTop ? MAX_VALUE : 0, moveStackMode, animate,
571 deferResume, reason);
572 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800573
Winson Chung74666102017-02-22 17:49:24 -0800574 /**
575 * Reparents the task into a preferred stack, creating it if necessary.
576 *
577 * @param preferredStackId the stack id of the target stack to move this task
578 * @param position the position to place this task in the new stack
579 * @param animate whether or not we should wait for the new window created as a part of the
580 * reparenting to be drawn and animated in
581 * @param moveStackMode whether or not to move the stack to the front always, only if it was
582 * previously focused & in front, or never
583 * @param deferResume whether or not to update the visibility of other tasks and stacks that may
584 * have changed as a result of this reparenting
585 * @param reason the caller of this reparenting
586 * @return
587 */
588 boolean reparent(int preferredStackId, int position, @ReparentMoveStackMode int moveStackMode,
589 boolean animate, boolean deferResume, String reason) {
590 final ActivityStackSupervisor supervisor = mService.mStackSupervisor;
591 final WindowManagerService windowManager = mService.mWindowManager;
592 final ActivityStack sourceStack = getStack();
593 final ActivityStack toStack = supervisor.getReparentTargetStack(this, preferredStackId,
594 position == MAX_VALUE);
595 if (toStack == sourceStack) {
596 return false;
597 }
598
599 final int sourceStackId = getStackId();
600 final int stackId = toStack.getStackId();
601 final ActivityRecord topActivity = getTopActivity();
602
603 final boolean mightReplaceWindow = StackId.replaceWindowsOnTaskMove(sourceStackId, stackId)
604 && topActivity != null;
605 if (mightReplaceWindow) {
606 // We are about to relaunch the activity because its configuration changed due to
607 // being maximized, i.e. size change. The activity will first remove the old window
608 // and then add a new one. This call will tell window manager about this, so it can
609 // preserve the old window until the new one is drawn. This prevents having a gap
610 // between the removal and addition, in which no window is visible. We also want the
611 // entrance of the new window to be properly animated.
612 // Note here we always set the replacing window first, as the flags might be needed
613 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
614 windowManager.setWillReplaceWindow(topActivity.appToken, animate);
615 }
616
617 windowManager.deferSurfaceLayout();
618 boolean kept = true;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800619 try {
Winson Chung74666102017-02-22 17:49:24 -0800620 final ActivityRecord r = topRunningActivityLocked();
621 final boolean wasFocused = supervisor.isFocusedStack(sourceStack)
622 && (topRunningActivityLocked() == r);
623 final boolean wasResumed = sourceStack.mResumedActivity == r;
624 final boolean wasPaused = sourceStack.mPausingActivity == r;
625
626 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
627 // Whenever we are moving the top activity from the front stack we want to make sure to
628 // move the stack to the front.
629 final boolean wasFront = supervisor.isFrontStackOnDisplay(sourceStack)
630 && (sourceStack.topRunningActivityLocked() == r);
631
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800632 // Adjust the position for the new parent stack as needed.
Winson Chung74666102017-02-22 17:49:24 -0800633 position = toStack.getAdjustedPositionForTask(this, position, null /* starting */);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800634
635 // Must reparent first in window manager to avoid a situation where AM can delete the
636 // we are coming from in WM before we reparent because it became empty.
Winson Chung74666102017-02-22 17:49:24 -0800637 mWindowContainerController.reparent(toStack.getWindowContainerController(), position);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800638
Winson Chung74666102017-02-22 17:49:24 -0800639 // Reset the resumed activity on the previous stack
640 if (wasResumed) {
641 sourceStack.mResumedActivity = null;
642 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800643
Winson Chung74666102017-02-22 17:49:24 -0800644 // Reset the paused activity on the previous stack
645 if (wasPaused) {
646 sourceStack.mPausingActivity = null;
647 sourceStack.removeTimeoutsForActivityLocked(r);
648 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800649
Winson Chung74666102017-02-22 17:49:24 -0800650 // Move the task
651 sourceStack.removeTask(this, reason, REMOVE_TASK_MODE_MOVING);
652 toStack.addTask(this, position, reason);
653
654 // TODO: Ensure that this is actually necessary here
655 // Notify of picture-in-picture mode changes
656 supervisor.scheduleReportPictureInPictureModeChangedIfNeeded(this, sourceStack);
657
658 // TODO: Ensure that this is actually necessary here
659 // Notify the voice session if required
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800660 if (voiceSession != null) {
661 try {
662 voiceSession.taskStarted(intent, taskId);
663 } catch (RemoteException e) {
664 }
665 }
Winson Chung74666102017-02-22 17:49:24 -0800666
667 // If the task had focus before (or we're requested to move focus), move focus to the
668 // new stack by moving the stack to the front.
669 final boolean moveStackToFront = moveStackMode == REPARENT_MOVE_STACK_TO_FRONT
670 || (moveStackMode == REPARENT_KEEP_STACK_AT_FRONT && (wasFocused || wasFront));
671 toStack.moveToFrontAndResumeStateIfNeeded(r, moveStackToFront, wasResumed, wasPaused,
672 reason);
673 if (!animate) {
674 toStack.mNoAnimActivities.add(topActivity);
675 }
676
677 // We might trigger a configuration change. Save the current task bounds for freezing.
678 // TODO: Should this call be moved inside the resize method in WM?
679 toStack.prepareFreezingTaskBounds();
680
681 // Make sure the task has the appropriate bounds/size for the stack it is in.
682 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID
683 && !Objects.equals(mBounds, toStack.mBounds)) {
684 kept = resize(toStack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow,
685 deferResume);
686 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
687 Rect bounds = getLaunchBounds();
688 if (bounds == null) {
689 toStack.layoutTaskInStack(this, null);
690 bounds = mBounds;
691 }
692 kept = resize(bounds, RESIZE_MODE_FORCED, !mightReplaceWindow, deferResume);
693 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Matthew Ng330757d2017-02-28 14:19:17 -0800694 if (stackId == DOCKED_STACK_ID && moveStackMode == REPARENT_KEEP_STACK_AT_FRONT) {
695 // Move recents to front so it is not behind home stack when going into docked
696 // mode
697 mService.mStackSupervisor.moveRecentsStackToFront(reason);
698 }
Winson Chung74666102017-02-22 17:49:24 -0800699 kept = resize(toStack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow,
700 deferResume);
701 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800702 } finally {
Winson Chung74666102017-02-22 17:49:24 -0800703 windowManager.continueSurfaceLayout();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800704 }
Winson Chung74666102017-02-22 17:49:24 -0800705
706 if (mightReplaceWindow) {
707 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
708 // window), we need to clear the replace window settings. Otherwise, we schedule a
709 // timeout to remove the old window if the replacing window is not coming in time.
710 windowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
711 }
712
713 if (!deferResume) {
714 // The task might have already been running and its visibility needs to be synchronized
715 // with the visibility of the stack / windows.
716 supervisor.ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
717 supervisor.resumeFocusedStackTopActivityLocked();
718 }
719
720 supervisor.handleNonResizableTaskIfNeeded(this, preferredStackId, stackId);
721
Matthew Ng330757d2017-02-28 14:19:17 -0800722 boolean successful = (preferredStackId == stackId);
723 if (successful && stackId == DOCKED_STACK_ID) {
724 // If task moved to docked stack - show recents if needed.
725 mService.mWindowManager.showRecentApps(false /* fromHome */);
726 }
727 return successful;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800728 }
729
730 void cancelWindowTransition() {
731 mWindowContainerController.cancelWindowTransition();
732 }
733
734 void cancelThumbnailTransition() {
735 mWindowContainerController.cancelThumbnailTransition();
736 }
737
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100738 /**
739 * DO NOT HOLD THE ACTIVITY MANAGER LOCK WHEN CALLING THIS METHOD!
740 */
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100741 TaskSnapshot getSnapshot(boolean reducedResolution) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100742
743 // TODO: Move this to {@link TaskWindowContainerController} once recent tasks are more
744 // synchronized between AM and WM.
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100745 return mService.mWindowManager.getTaskSnapshot(taskId, userId, reducedResolution);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800746 }
747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 void touchActiveTime() {
Winson Chung36f3f032016-09-08 23:29:43 +0000749 lastActiveTime = System.currentTimeMillis();
Winson Chungffa2ec62014-07-03 15:54:42 -0700750 if (firstActiveTime == 0) {
751 firstActiveTime = lastActiveTime;
752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 long getInactiveDuration() {
Winson Chung36f3f032016-09-08 23:29:43 +0000756 return System.currentTimeMillis() - lastActiveTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700758
Winson Chungfee26772014-08-05 12:21:52 -0700759 /** Sets the original intent, and the calling uid and package. */
760 void setIntent(ActivityRecord r) {
Winson Chungfee26772014-08-05 12:21:52 -0700761 mCallingUid = r.launchedFromUid;
762 mCallingPackage = r.launchedFromPackage;
Craig Mautner15df08a2015-04-01 12:17:18 -0700763 setIntent(r.intent, r.info);
Winson Chungfee26772014-08-05 12:21:52 -0700764 }
765
766 /** Sets the original intent, _without_ updating the calling uid or package. */
767 private void setIntent(Intent _intent, ActivityInfo info) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700768 if (intent == null) {
769 mNeverRelinquishIdentity =
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700770 (info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700771 } else if (mNeverRelinquishIdentity) {
772 return;
773 }
774
775 affinity = info.taskAffinity;
Dianne Hackborn79228822014-09-16 11:11:23 -0700776 if (intent == null) {
777 // If this task already has an intent associated with it, don't set the root
778 // affinity -- we don't want it changing after initially set, but the initially
779 // set value may be null.
780 rootAffinity = affinity;
781 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700782 effectiveUid = info.applicationInfo.uid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700783 stringName = null;
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 if (info.targetActivity == null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800786 if (_intent != null) {
787 // If this Intent has a selector, we want to clear it for the
788 // recent task since it is not relevant if the user later wants
789 // to re-launch the app.
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700790 if (_intent.getSelector() != null || _intent.getSourceBounds() != null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800791 _intent = new Intent(_intent);
792 _intent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700793 _intent.setSourceBounds(null);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800794 }
795 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700796 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Setting Intent of " + this + " to " + _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 intent = _intent;
798 realActivity = _intent != null ? _intent.getComponent() : null;
799 origActivity = null;
800 } else {
801 ComponentName targetComponent = new ComponentName(
802 info.packageName, info.targetActivity);
803 if (_intent != null) {
804 Intent targetIntent = new Intent(_intent);
805 targetIntent.setComponent(targetComponent);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800806 targetIntent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700807 targetIntent.setSourceBounds(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700808 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700809 "Setting Intent of " + this + " to target " + targetIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 intent = targetIntent;
811 realActivity = targetComponent;
812 origActivity = _intent.getComponent();
813 } else {
814 intent = null;
815 realActivity = targetComponent;
816 origActivity = new ComponentName(info.packageName, info.name);
817 }
818 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700819
Craig Mautner47b20ba2014-09-17 17:23:44 -0700820 final int intentFlags = intent == null ? 0 : intent.getFlags();
821 if ((intentFlags & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 // Once we are set to an Intent with this flag, we count this
823 // task as having a true root activity.
824 rootWasReset = true;
825 }
Dianne Hackborn09233282014-04-30 11:33:59 -0700826 userId = UserHandle.getUserId(info.applicationInfo.uid);
Winson Chung36f3f032016-09-08 23:29:43 +0000827 mUserSetupComplete = Settings.Secure.getIntForUser(mService.mContext.getContentResolver(),
828 USER_SETUP_COMPLETE, 0, userId) != 0;
Craig Mautner41db4a72014-05-07 17:20:56 -0700829 if ((info.flags & ActivityInfo.FLAG_AUTO_REMOVE_FROM_RECENTS) != 0) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700830 // If the activity itself has requested auto-remove, then just always do it.
831 autoRemoveRecents = true;
Wale Ogunwale843bfb92015-03-27 11:06:48 -0700832 } else if ((intentFlags & (FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_RETAIN_IN_RECENTS))
833 == FLAG_ACTIVITY_NEW_DOCUMENT) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700834 // If the caller has not asked for the document to be retained, then we may
835 // want to turn on auto-remove, depending on whether the target has set its
836 // own document launch mode.
837 if (info.documentLaunchMode != ActivityInfo.DOCUMENT_LAUNCH_NONE) {
838 autoRemoveRecents = false;
839 } else {
840 autoRemoveRecents = true;
841 }
842 } else {
843 autoRemoveRecents = false;
Craig Mautner41db4a72014-05-07 17:20:56 -0700844 }
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800845 mResizeMode = info.resizeMode;
Winson Chungd3395382016-12-13 11:49:09 -0800846 mSupportsPictureInPicture = info.supportsPictureInPicture();
Craig Mautner15df08a2015-04-01 12:17:18 -0700847 mLockTaskMode = info.lockTaskLaunchMode;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700848 mPrivileged = (info.applicationInfo.privateFlags & PRIVATE_FLAG_PRIVILEGED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -0700849 setLockTaskAuth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800851
Andrii Kulian2e751b82016-03-16 16:59:32 -0700852 /** Sets the original minimal width and height. */
853 private void setMinDimensions(ActivityInfo info) {
854 if (info != null && info.windowLayout != null) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700855 mMinWidth = info.windowLayout.minWidth;
856 mMinHeight = info.windowLayout.minHeight;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700857 } else {
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700858 mMinWidth = INVALID_MIN_SIZE;
859 mMinHeight = INVALID_MIN_SIZE;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700860 }
861 }
862
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800863 /**
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700864 * Return true if the input activity has the same intent filter as the intent this task
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800865 * record is based on (normally the root activity intent).
866 */
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700867 boolean isSameIntentFilter(ActivityRecord r) {
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800868 final Intent intent = new Intent(r.intent);
869 // Correct the activity intent for aliasing. The task record intent will always be based on
870 // the real activity that will be launched not the alias, so we need to use an intent with
871 // the component name pointing to the real activity not the alias in the activity record.
872 intent.setComponent(r.realActivity);
873 return this.intent.filterEquals(intent);
874 }
875
Craig Mautner84984fa2014-06-19 11:19:20 -0700876 void setTaskToReturnTo(int taskToReturnTo) {
Wale Ogunwale673cbd22016-01-30 18:30:55 -0800877 mTaskToReturnTo = (taskToReturnTo == RECENTS_ACTIVITY_TYPE)
878 ? HOME_ACTIVITY_TYPE : taskToReturnTo;
Craig Mautner84984fa2014-06-19 11:19:20 -0700879 }
880
Winson Chung83471632016-12-13 11:02:12 -0800881 void setTaskToReturnTo(ActivityRecord source) {
882 if (source.isRecentsActivity()) {
883 setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
884 } else if (source.isAssistantActivity()) {
885 setTaskToReturnTo(ASSISTANT_ACTIVITY_TYPE);
886 }
887 }
888
Craig Mautner84984fa2014-06-19 11:19:20 -0700889 int getTaskToReturnTo() {
890 return mTaskToReturnTo;
891 }
892
Craig Mautnera228ae92014-07-09 05:44:55 -0700893 void setPrevAffiliate(TaskRecord prevAffiliate) {
894 mPrevAffiliate = prevAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800895 mPrevAffiliateTaskId = prevAffiliate == null ? INVALID_TASK_ID : prevAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700896 }
897
898 void setNextAffiliate(TaskRecord nextAffiliate) {
899 mNextAffiliate = nextAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800900 mNextAffiliateTaskId = nextAffiliate == null ? INVALID_TASK_ID : nextAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700901 }
902
Andrii Kulian02b7a832016-10-06 23:11:56 -0700903 ActivityStack getStack() {
904 return mStack;
905 }
906
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800907 /**
908 * Must be used for setting parent stack because it performs configuration updates.
909 * Must be called after adding task as a child to the stack.
910 */
Andrii Kulian02b7a832016-10-06 23:11:56 -0700911 void setStack(ActivityStack stack) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800912 if (stack != null && !stack.isInStackLocked(this)) {
913 throw new IllegalStateException("Task must be added as a Stack child first.");
914 }
Andrii Kulian02b7a832016-10-06 23:11:56 -0700915 mStack = stack;
Andrii Kulian1779e612016-10-12 21:58:25 -0700916 onParentChanged();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700917 }
918
919 /**
920 * @return Id of current stack, {@link INVALID_STACK_ID} if no stack is set.
921 */
922 int getStackId() {
923 return mStack != null ? mStack.mStackId : INVALID_STACK_ID;
924 }
925
Andrii Kulian1779e612016-10-12 21:58:25 -0700926 @Override
927 protected int getChildCount() {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700928 return mActivities.size();
Andrii Kulian1779e612016-10-12 21:58:25 -0700929 }
930
931 @Override
932 protected ConfigurationContainer getChildAt(int index) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700933 return mActivities.get(index);
Andrii Kulian1779e612016-10-12 21:58:25 -0700934 }
935
936 @Override
937 protected ConfigurationContainer getParent() {
938 return mStack;
939 }
940
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800941 @Override
942 void onParentChanged() {
943 super.onParentChanged();
944 mService.mStackSupervisor.updateUIDsPresentOnDisplay();
945 }
946
Craig Mautnera228ae92014-07-09 05:44:55 -0700947 // Close up recents linked list.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700948 private void closeRecentsChain() {
Craig Mautnera228ae92014-07-09 05:44:55 -0700949 if (mPrevAffiliate != null) {
950 mPrevAffiliate.setNextAffiliate(mNextAffiliate);
951 }
952 if (mNextAffiliate != null) {
953 mNextAffiliate.setPrevAffiliate(mPrevAffiliate);
954 }
955 setPrevAffiliate(null);
956 setNextAffiliate(null);
957 }
958
Winson Chung740c3ac2014-11-12 16:14:38 -0800959 void removedFromRecents() {
Dianne Hackborn852975d2014-08-22 17:42:43 -0700960 disposeThumbnail();
961 closeRecentsChain();
962 if (inRecents) {
963 inRecents = false;
Winson Chung740c3ac2014-11-12 16:14:38 -0800964 mService.notifyTaskPersisterLocked(this, false);
Dianne Hackborn852975d2014-08-22 17:42:43 -0700965 }
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100966
967 // TODO: Use window container controller once tasks are better synced between AM and WM
968 mService.mWindowManager.notifyTaskRemovedFromRecents(taskId, userId);
Dianne Hackborn852975d2014-08-22 17:42:43 -0700969 }
970
Craig Mautnera228ae92014-07-09 05:44:55 -0700971 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
972 closeRecentsChain();
973 mAffiliatedTaskId = taskToAffiliateWith.mAffiliatedTaskId;
Winson Chungec396d62014-08-06 17:08:00 -0700974 mAffiliatedTaskColor = taskToAffiliateWith.mAffiliatedTaskColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700975 // Find the end
976 while (taskToAffiliateWith.mNextAffiliate != null) {
977 final TaskRecord nextRecents = taskToAffiliateWith.mNextAffiliate;
978 if (nextRecents.mAffiliatedTaskId != mAffiliatedTaskId) {
979 Slog.e(TAG, "setTaskToAffiliateWith: nextRecents=" + nextRecents + " affilTaskId="
980 + nextRecents.mAffiliatedTaskId + " should be " + mAffiliatedTaskId);
981 if (nextRecents.mPrevAffiliate == taskToAffiliateWith) {
982 nextRecents.setPrevAffiliate(null);
983 }
984 taskToAffiliateWith.setNextAffiliate(null);
985 break;
986 }
987 taskToAffiliateWith = nextRecents;
988 }
989 taskToAffiliateWith.setNextAffiliate(this);
990 setPrevAffiliate(taskToAffiliateWith);
991 setNextAffiliate(null);
992 }
993
Winson Chung096f36b2014-08-20 15:39:01 -0700994 /**
Winsonc809cbb2015-11-02 12:06:15 -0800995 * Sets the last thumbnail with the current task bounds and the system orientation.
Winson Chung096f36b2014-08-20 15:39:01 -0700996 * @return whether the thumbnail was set
997 */
Winsonc809cbb2015-11-02 12:06:15 -0800998 boolean setLastThumbnailLocked(Bitmap thumbnail) {
Winsonc809cbb2015-11-02 12:06:15 -0800999 int taskWidth = 0;
1000 int taskHeight = 0;
1001 if (mBounds != null) {
1002 // Non-fullscreen tasks
1003 taskWidth = mBounds.width();
1004 taskHeight = mBounds.height();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001005 } else if (mStack != null) {
Winsonc809cbb2015-11-02 12:06:15 -08001006 // Fullscreen tasks
1007 final Point displaySize = new Point();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001008 mStack.getDisplaySize(displaySize);
Winsonc809cbb2015-11-02 12:06:15 -08001009 taskWidth = displaySize.x;
1010 taskHeight = displaySize.y;
1011 } else {
1012 Slog.e(TAG, "setLastThumbnailLocked() called on Task without stack");
1013 }
Andrii Kulian1779e612016-10-12 21:58:25 -07001014 // We need to provide the current orientation of the display on which this task resides,
1015 // not the orientation of the task.
1016 final int orientation =
1017 getStack().mActivityContainer.mActivityDisplay.getConfiguration().orientation;
1018 return setLastThumbnailLocked(thumbnail, taskWidth, taskHeight, orientation);
Winsonc809cbb2015-11-02 12:06:15 -08001019 }
1020
1021 /**
1022 * Sets the last thumbnail with the current task bounds.
1023 * @return whether the thumbnail was set
1024 */
1025 private boolean setLastThumbnailLocked(Bitmap thumbnail, int taskWidth, int taskHeight,
1026 int screenOrientation) {
Winson Chung096f36b2014-08-20 15:39:01 -07001027 if (mLastThumbnail != thumbnail) {
1028 mLastThumbnail = thumbnail;
Winsonc809cbb2015-11-02 12:06:15 -08001029 mLastThumbnailInfo.taskWidth = taskWidth;
1030 mLastThumbnailInfo.taskHeight = taskHeight;
1031 mLastThumbnailInfo.screenOrientation = screenOrientation;
Winson Chung096f36b2014-08-20 15:39:01 -07001032 if (thumbnail == null) {
1033 if (mLastThumbnailFile != null) {
1034 mLastThumbnailFile.delete();
1035 }
1036 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08001037 mService.mRecentTasks.saveImage(thumbnail, mLastThumbnailFile.getAbsolutePath());
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001038 }
Winson Chung096f36b2014-08-20 15:39:01 -07001039 return true;
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001040 }
Winson Chung096f36b2014-08-20 15:39:01 -07001041 return false;
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001042 }
1043
1044 void getLastThumbnail(TaskThumbnail thumbs) {
1045 thumbs.mainThumbnail = mLastThumbnail;
Winsonc809cbb2015-11-02 12:06:15 -08001046 thumbs.thumbnailInfo = mLastThumbnailInfo;
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001047 thumbs.thumbnailFileDescriptor = null;
Craig Mautnerf4f8bb72014-07-29 10:41:40 -07001048 if (mLastThumbnail == null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08001049 thumbs.mainThumbnail = mService.mRecentTasks.getImageFromWriteQueue(
Suprabh Shukla23593142015-11-03 17:31:15 -08001050 mLastThumbnailFile.getAbsolutePath());
Craig Mautnerf4f8bb72014-07-29 10:41:40 -07001051 }
Winson Chung096f36b2014-08-20 15:39:01 -07001052 // Only load the thumbnail file if we don't have a thumbnail
1053 if (thumbs.mainThumbnail == null && mLastThumbnailFile.exists()) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001054 try {
1055 thumbs.thumbnailFileDescriptor = ParcelFileDescriptor.open(mLastThumbnailFile,
1056 ParcelFileDescriptor.MODE_READ_ONLY);
1057 } catch (IOException e) {
Dianne Hackborn9844d292013-10-04 16:44:22 -07001058 }
1059 }
1060 }
1061
Winsonc809cbb2015-11-02 12:06:15 -08001062 /**
1063 * Removes in-memory thumbnail data when the max number of in-memory task thumbnails is reached.
1064 */
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001065 void freeLastThumbnail() {
1066 mLastThumbnail = null;
1067 }
1068
Winsonc809cbb2015-11-02 12:06:15 -08001069 /**
1070 * Removes all associated thumbnail data when a task is removed or pruned from recents.
1071 */
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001072 void disposeThumbnail() {
Winsonc6a2da02015-11-11 18:11:59 -08001073 mLastThumbnailInfo.reset();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001074 mLastThumbnail = null;
1075 lastDescription = null;
1076 }
1077
Winson Chung1147c402014-05-14 11:05:00 -07001078 /** Returns the intent for the root activity for this task */
1079 Intent getBaseIntent() {
1080 return intent != null ? intent : affinityIntent;
1081 }
1082
Winson Chung3b3f4642014-04-22 10:08:18 -07001083 /** Returns the first non-finishing activity from the root. */
1084 ActivityRecord getRootActivity() {
1085 for (int i = 0; i < mActivities.size(); i++) {
1086 final ActivityRecord r = mActivities.get(i);
1087 if (r.finishing) {
1088 continue;
1089 }
1090 return r;
1091 }
1092 return null;
1093 }
1094
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001095 ActivityRecord getTopActivity() {
1096 for (int i = mActivities.size() - 1; i >= 0; --i) {
1097 final ActivityRecord r = mActivities.get(i);
1098 if (r.finishing) {
1099 continue;
1100 }
1101 return r;
1102 }
1103 return null;
1104 }
1105
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001106 ActivityRecord topRunningActivityLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001107 if (mStack != null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001108 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1109 ActivityRecord r = mActivities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -07001110 if (!r.finishing && r.okToShowLocked()) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001111 return r;
1112 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001113 }
1114 }
1115 return null;
1116 }
1117
Wale Ogunwale3b232392016-05-13 15:37:13 -07001118 ActivityRecord topRunningActivityWithStartingWindowLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001119 if (mStack != null) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07001120 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1121 ActivityRecord r = mActivities.get(activityNdx);
1122 if (r.mStartingWindowState != STARTING_WINDOW_SHOWN
Chong Zhang87761972016-08-22 13:53:24 -07001123 || r.finishing || !r.okToShowLocked()) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07001124 continue;
1125 }
1126 return r;
1127 }
1128 }
1129 return null;
1130 }
1131
Chong Zhang87761972016-08-22 13:53:24 -07001132 boolean okToShowLocked() {
1133 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
1134 // okay to show the activity when locked.
1135 return mService.mStackSupervisor.isCurrentProfileLocked(userId)
1136 || topRunningActivityLocked() != null;
1137 }
1138
Craig Mautner3b475fe2013-12-16 15:58:31 -08001139 /** Call after activity movement or finish to make sure that frontOfTask is set correctly */
Bryce Leed71317c2017-02-07 14:27:22 -08001140 final void setFrontOfTask() {
1141 boolean foundFront = false;
Craig Mautner3b475fe2013-12-16 15:58:31 -08001142 final int numActivities = mActivities.size();
Craig Mautner704e40b2013-12-18 16:43:51 -08001143 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
Craig Mautner3b475fe2013-12-16 15:58:31 -08001144 final ActivityRecord r = mActivities.get(activityNdx);
1145 if (foundFront || r.finishing) {
1146 r.frontOfTask = false;
1147 } else {
1148 r.frontOfTask = true;
1149 // Set frontOfTask false for every following activity.
1150 foundFront = true;
1151 }
1152 }
Craig Mautner9587ee02014-06-23 15:00:10 +00001153 if (!foundFront && numActivities > 0) {
1154 // All activities of this task are finishing. As we ought to have a frontOfTask
1155 // activity, make the bottom activity front.
1156 mActivities.get(0).frontOfTask = true;
1157 }
Craig Mautner3b475fe2013-12-16 15:58:31 -08001158 }
1159
Craig Mautnerde4ef022013-04-07 19:01:33 -07001160 /**
Craig Mautner3b475fe2013-12-16 15:58:31 -08001161 * Reorder the history stack so that the passed activity is brought to the front.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001162 */
1163 final void moveActivityToFrontLocked(ActivityRecord newTop) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001164 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
1165 "Removing and adding activity " + newTop
1166 + " to stack at top callers=" + Debug.getCallers(4));
Craig Mautnerde4ef022013-04-07 19:01:33 -07001167
Craig Mautnerde4ef022013-04-07 19:01:33 -07001168 mActivities.remove(newTop);
1169 mActivities.add(newTop);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001170 updateEffectiveIntent();
Craig Mautner3b475fe2013-12-16 15:58:31 -08001171
Bryce Leed71317c2017-02-07 14:27:22 -08001172 setFrontOfTask();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001173 }
1174
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001175 void addActivityAtBottom(ActivityRecord r) {
Craig Mautner77878772013-03-04 19:46:24 -08001176 addActivityAtIndex(0, r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001177 }
1178
1179 void addActivityToTop(ActivityRecord r) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001180 addActivityAtIndex(mActivities.size(), r);
1181 }
1182
Winson Chung30480042017-01-26 10:55:34 -08001183 /**
1184 * Adds an activity {@param r} at the given {@param index}. The activity {@param r} must either
1185 * be in the current task or unparented to any task.
1186 */
Craig Mautner1602ec22013-05-12 10:24:27 -07001187 void addActivityAtIndex(int index, ActivityRecord r) {
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001188 if (r.task != null && r.task != this) {
1189 throw new IllegalArgumentException("Can not add r=" + " to task=" + this
1190 + " current parent=" + r.task);
1191 }
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001192 // TODO(b/36505427): Maybe make task private to ActivityRecord so we can also do
1193 // onParentChanged() within the setter?
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001194 r.task = this;
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001195 r.onParentChanged();
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001196
Craig Mautner6170f732013-04-02 13:05:23 -07001197 // 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 -08001198 if (!mActivities.remove(r) && r.fullscreen) {
1199 // Was not previously in list.
1200 numFullscreen++;
1201 }
Craig Mautner2c1faed2013-07-23 12:56:02 -07001202 // Only set this based on the first activity
1203 if (mActivities.isEmpty()) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001204 taskType = r.mActivityType;
1205 isPersistable = r.isPersistable();
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001206 mCallingUid = r.launchedFromUid;
1207 mCallingPackage = r.launchedFromPackage;
Dianne Hackborn852975d2014-08-22 17:42:43 -07001208 // Clamp to [1, max].
1209 maxRecents = Math.min(Math.max(r.info.maxRecents, 1),
1210 ActivityManager.getMaxAppRecentsLimitStatic());
Craig Mautner2c1faed2013-07-23 12:56:02 -07001211 } else {
1212 // Otherwise make all added activities match this one.
Craig Mautner21d24a22014-04-23 11:45:37 -07001213 r.mActivityType = taskType;
Craig Mautner78733002013-06-10 13:54:49 -07001214 }
Wale Ogunwale3b232392016-05-13 15:37:13 -07001215
1216 final int size = mActivities.size();
1217
1218 if (index == size && size > 0) {
1219 final ActivityRecord top = mActivities.get(size - 1);
1220 if (top.mTaskOverlay) {
1221 // Place below the task overlay activity since the overlay activity should always
1222 // be on top.
1223 index--;
1224 }
1225 }
1226
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001227 index = Math.min(size, index);
Craig Mautner77878772013-03-04 19:46:24 -08001228 mActivities.add(index, r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001229 updateEffectiveIntent();
Craig Mautner21d24a22014-04-23 11:45:37 -07001230 if (r.isPersistable()) {
1231 mService.notifyTaskPersisterLocked(this, false);
1232 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001233
1234 // Sync. with window manager
1235 updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001236 final AppWindowContainerController appController = r.getWindowContainerController();
1237 if (appController != null) {
1238 // Only attempt to move in WM if the child has a controller. It is possible we haven't
1239 // created controller for the activity we are starting yet.
1240 mWindowContainerController.positionChildAt(appController, index);
1241 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001242 r.onOverrideConfigurationSent();
David Stevens82ea6cb2017-03-03 16:18:50 -08001243
1244 // Make sure the list of display UID whitelists is updated
1245 // now that this record is in a new task.
1246 mService.mStackSupervisor.updateUIDsPresentOnDisplay();
Craig Mautner77878772013-03-04 19:46:24 -08001247 }
1248
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001249 /**
1250 * @return true if this was the last activity in the task
1251 */
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001252 boolean removeActivity(ActivityRecord r) {
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001253 if (r.task != this) {
1254 throw new IllegalArgumentException(
1255 "Activity=" + r + " does not belong to task=" + this);
1256 }
1257
1258 r.task = null;
1259
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001260 if (mActivities.remove(r) && r.fullscreen) {
1261 // Was previously in list.
1262 numFullscreen--;
1263 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001264 if (r.isPersistable()) {
1265 mService.notifyTaskPersisterLocked(this, false);
1266 }
Wale Ogunwale89182d52016-03-11 10:38:36 -08001267
Andrii Kulian02b7a832016-10-06 23:11:56 -07001268 if (getStackId() == PINNED_STACK_ID) {
Wale Ogunwale89182d52016-03-11 10:38:36 -08001269 // We normally notify listeners of task stack changes on pause, however pinned stack
1270 // activities are normally in the paused state so no notification will be sent there
1271 // before the activity is removed. We send it here so instead.
Yorke Leebd54c2a2016-10-25 13:49:23 -07001272 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale89182d52016-03-11 10:38:36 -08001273 }
1274
Craig Mautner41326202014-06-20 14:38:21 -07001275 if (mActivities.isEmpty()) {
Craig Mautner5afcd4d2014-06-21 18:11:33 -07001276 return !mReuseTask;
Craig Mautner41326202014-06-20 14:38:21 -07001277 }
1278 updateEffectiveIntent();
1279 return false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001280 }
1281
Winson Chung6954fc92017-03-24 16:22:12 -07001282 /**
1283 * @return whether or not there are ONLY task overlay activities in the stack.
1284 * If {@param excludeFinishing} is set, then ignore finishing activities in the check.
1285 * If there are no task overlay activities, this call returns false.
1286 */
1287 boolean onlyHasTaskOverlayActivities(boolean excludeFinishing) {
1288 int count = 0;
1289 for (int i = mActivities.size() - 1; i >= 0; i--) {
1290 final ActivityRecord r = mActivities.get(i);
1291 if (excludeFinishing && r.finishing) {
1292 continue;
1293 }
1294 if (!r.mTaskOverlay) {
1295 return false;
1296 }
1297 count++;
1298 }
1299 return count > 0;
1300 }
1301
Craig Mautner41db4a72014-05-07 17:20:56 -07001302 boolean autoRemoveFromRecents() {
Dianne Hackbornd38aed82014-06-10 21:36:35 -07001303 // We will automatically remove the task either if it has explicitly asked for
1304 // this, or it is empty and has never contained an activity that got shown to
1305 // the user.
Dianne Hackborn13420f22014-07-18 15:43:56 -07001306 return autoRemoveRecents || (mActivities.isEmpty() && !hasBeenVisible);
Craig Mautner41db4a72014-05-07 17:20:56 -07001307 }
1308
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001309 /**
1310 * Completely remove all activities associated with an existing
1311 * task starting at a specified index.
1312 */
Winson Chung6954fc92017-03-24 16:22:12 -07001313 final void performClearTaskAtIndexLocked(int activityNdx, boolean pauseImmediately) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001314 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001315 for ( ; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001316 final ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001317 if (r.finishing) {
1318 continue;
1319 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001320 if (mStack == null) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001321 // Task was restored from persistent storage.
1322 r.takeFromHistory();
1323 mActivities.remove(activityNdx);
1324 --activityNdx;
1325 --numActivities;
Winson Chung6954fc92017-03-24 16:22:12 -07001326 } else if (mStack.finishActivityLocked(r, Activity.RESULT_CANCELED, null,
1327 "clear-task-index", false, pauseImmediately)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001328 --activityNdx;
1329 --numActivities;
1330 }
1331 }
1332 }
1333
1334 /**
1335 * Completely remove all activities associated with an existing task.
1336 */
1337 final void performClearTaskLocked() {
Craig Mautner362449a2014-06-20 14:04:39 -07001338 mReuseTask = true;
Winson Chung6954fc92017-03-24 16:22:12 -07001339 performClearTaskAtIndexLocked(0, !PAUSE_IMMEDIATELY);
Craig Mautner362449a2014-06-20 14:04:39 -07001340 mReuseTask = false;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001341 }
1342
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -08001343 ActivityRecord performClearTaskForReuseLocked(ActivityRecord newR, int launchFlags) {
1344 mReuseTask = true;
1345 final ActivityRecord result = performClearTaskLocked(newR, launchFlags);
1346 mReuseTask = false;
1347 return result;
1348 }
1349
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001350 /**
1351 * Perform clear operation as requested by
1352 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
1353 * stack to the given task, then look for
1354 * an instance of that activity in the stack and, if found, finish all
1355 * activities on top of it and return the instance.
1356 *
1357 * @param newR Description of the new activity being started.
1358 * @return Returns the old activity that should be continued to be used,
1359 * or null if none was found.
1360 */
1361 final ActivityRecord performClearTaskLocked(ActivityRecord newR, int launchFlags) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001362 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001363 for (int activityNdx = numActivities - 1; activityNdx >= 0; --activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001364 ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001365 if (r.finishing) {
1366 continue;
1367 }
1368 if (r.realActivity.equals(newR.realActivity)) {
1369 // Here it is! Now finish everything in front...
Craig Mautner1602ec22013-05-12 10:24:27 -07001370 final ActivityRecord ret = r;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001371
1372 for (++activityNdx; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001373 r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001374 if (r.finishing) {
1375 continue;
1376 }
1377 ActivityOptions opts = r.takeOptionsLocked();
1378 if (opts != null) {
1379 ret.updateOptionsLocked(opts);
1380 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001381 if (mStack != null && mStack.finishActivityLocked(
Todd Kennedy539db512014-12-15 09:57:55 -08001382 r, Activity.RESULT_CANCELED, null, "clear-task-stack", false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001383 --activityNdx;
1384 --numActivities;
1385 }
1386 }
1387
1388 // Finally, if this is a normal launch mode (that is, not
1389 // expecting onNewIntent()), then we will finish the current
1390 // instance of the activity so a new fresh one can be started.
1391 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
Daichi Hirono15a02992016-04-27 18:47:01 +09001392 && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0
1393 && !ActivityStarter.isDocumentLaunchesIntoExisting(launchFlags)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001394 if (!ret.finishing) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001395 if (mStack != null) {
1396 mStack.finishActivityLocked(
Wale Ogunwale7d701172015-03-11 15:36:30 -07001397 ret, Activity.RESULT_CANCELED, null, "clear-task-top", false);
1398 }
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001399 return null;
1400 }
1401 }
1402
1403 return ret;
1404 }
1405 }
1406
1407 return null;
1408 }
1409
Andrii Kulian21713ac2016-10-12 22:05:05 -07001410 TaskThumbnail getTaskThumbnailLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001411 if (mStack != null) {
1412 final ActivityRecord resumedActivity = mStack.mResumedActivity;
Craig Mautner21d24a22014-04-23 11:45:37 -07001413 if (resumedActivity != null && resumedActivity.task == this) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001414 final Bitmap thumbnail = resumedActivity.screenshotActivityLocked();
Winsonc809cbb2015-11-02 12:06:15 -08001415 setLastThumbnailLocked(thumbnail);
Craig Mautner21d24a22014-04-23 11:45:37 -07001416 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -07001417 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001418 final TaskThumbnail taskThumbnail = new TaskThumbnail();
1419 getLastThumbnail(taskThumbnail);
1420 return taskThumbnail;
Craig Mautner9db9a0b2013-04-29 17:05:56 -07001421 }
1422
Winson Chung6954fc92017-03-24 16:22:12 -07001423 void removeTaskActivitiesLocked(boolean pauseImmediately) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001424 // Just remove the entire task.
Winson Chung6954fc92017-03-24 16:22:12 -07001425 performClearTaskAtIndexLocked(0, pauseImmediately);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07001426 }
1427
Craig Mautner432f64e2015-05-20 14:59:57 -07001428 String lockTaskAuthToString() {
1429 switch (mLockTaskAuth) {
1430 case LOCK_TASK_AUTH_DONT_LOCK: return "LOCK_TASK_AUTH_DONT_LOCK";
1431 case LOCK_TASK_AUTH_PINNABLE: return "LOCK_TASK_AUTH_PINNABLE";
1432 case LOCK_TASK_AUTH_LAUNCHABLE: return "LOCK_TASK_AUTH_LAUNCHABLE";
1433 case LOCK_TASK_AUTH_WHITELISTED: return "LOCK_TASK_AUTH_WHITELISTED";
Benjamin Franz469dd582015-06-09 14:24:36 +01001434 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV: return "LOCK_TASK_AUTH_LAUNCHABLE_PRIV";
Craig Mautner432f64e2015-05-20 14:59:57 -07001435 default: return "unknown=" + mLockTaskAuth;
1436 }
1437 }
1438
Craig Mautner15df08a2015-04-01 12:17:18 -07001439 void setLockTaskAuth() {
Benjamin Franz469dd582015-06-09 14:24:36 +01001440 if (!mPrivileged &&
1441 (mLockTaskMode == LOCK_TASK_LAUNCH_MODE_ALWAYS ||
1442 mLockTaskMode == LOCK_TASK_LAUNCH_MODE_NEVER)) {
1443 // Non-priv apps are not allowed to use always or never, fall back to default
1444 mLockTaskMode = LOCK_TASK_LAUNCH_MODE_DEFAULT;
1445 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001446 switch (mLockTaskMode) {
1447 case LOCK_TASK_LAUNCH_MODE_DEFAULT:
1448 mLockTaskAuth = isLockTaskWhitelistedLocked() ?
1449 LOCK_TASK_AUTH_WHITELISTED : LOCK_TASK_AUTH_PINNABLE;
1450 break;
1451
1452 case LOCK_TASK_LAUNCH_MODE_NEVER:
Benjamin Franz469dd582015-06-09 14:24:36 +01001453 mLockTaskAuth = LOCK_TASK_AUTH_DONT_LOCK;
Craig Mautner15df08a2015-04-01 12:17:18 -07001454 break;
1455
1456 case LOCK_TASK_LAUNCH_MODE_ALWAYS:
Benjamin Franz469dd582015-06-09 14:24:36 +01001457 mLockTaskAuth = LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -07001458 break;
1459
1460 case LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED:
1461 mLockTaskAuth = isLockTaskWhitelistedLocked() ?
1462 LOCK_TASK_AUTH_LAUNCHABLE : LOCK_TASK_AUTH_PINNABLE;
1463 break;
1464 }
Craig Mautner432f64e2015-05-20 14:59:57 -07001465 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "setLockTaskAuth: task=" + this +
1466 " mLockTaskAuth=" + lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07001467 }
1468
1469 boolean isLockTaskWhitelistedLocked() {
Benjamin Franz6fd84cc2015-08-06 18:09:03 +01001470 String pkg = (realActivity != null) ? realActivity.getPackageName() : null;
1471 if (pkg == null) {
Craig Mautner15df08a2015-04-01 12:17:18 -07001472 return false;
1473 }
1474 String[] packages = mService.mLockTaskPackages.get(userId);
1475 if (packages == null) {
1476 return false;
1477 }
1478 for (int i = packages.length - 1; i >= 0; --i) {
Benjamin Franz6fd84cc2015-08-06 18:09:03 +01001479 if (pkg.equals(packages[i])) {
Craig Mautner15df08a2015-04-01 12:17:18 -07001480 return true;
1481 }
1482 }
1483 return false;
1484 }
Wale Ogunwale74e26592016-02-05 11:48:37 -08001485
Craig Mautnera82aa092013-09-13 15:34:08 -07001486 boolean isHomeTask() {
Craig Mautner84984fa2014-06-19 11:19:20 -07001487 return taskType == HOME_ACTIVITY_TYPE;
Craig Mautnera82aa092013-09-13 15:34:08 -07001488 }
1489
Wale Ogunwale74e26592016-02-05 11:48:37 -08001490 boolean isRecentsTask() {
1491 return taskType == RECENTS_ACTIVITY_TYPE;
1492 }
1493
Winson Chung83471632016-12-13 11:02:12 -08001494 boolean isAssistantTask() {
1495 return taskType == ASSISTANT_ACTIVITY_TYPE;
1496 }
1497
Craig Mautner86d67a42013-05-14 10:34:38 -07001498 boolean isApplicationTask() {
Craig Mautner84984fa2014-06-19 11:19:20 -07001499 return taskType == APPLICATION_ACTIVITY_TYPE;
1500 }
1501
1502 boolean isOverHomeStack() {
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001503 return mTaskToReturnTo == HOME_ACTIVITY_TYPE;
Craig Mautner1602ec22013-05-12 10:24:27 -07001504 }
1505
Winson Chung83471632016-12-13 11:02:12 -08001506 boolean isOverAssistantStack() {
1507 return mTaskToReturnTo == ASSISTANT_ACTIVITY_TYPE;
1508 }
1509
Winson Chungd3395382016-12-13 11:49:09 -08001510 private boolean isResizeable(boolean checkSupportsPip) {
1511 return (mService.mForceResizableActivities || ActivityInfo.isResizeableMode(mResizeMode)
1512 || (checkSupportsPip && mSupportsPictureInPicture)) && !mTemporarilyUnresizable;
1513 }
1514
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001515 boolean isResizeable() {
Winson Chungd3395382016-12-13 11:49:09 -08001516 return isResizeable(true /* checkSupportsPip */);
1517 }
1518
1519 boolean supportsSplitScreen() {
1520 // A task can not be docked even if it is considered resizeable because it only supports
1521 // picture-in-picture mode but has a non-resizeable resizeMode
1522 return mService.mSupportsSplitScreenMultiWindow
1523 && isResizeable(false /* checkSupportsPip */)
1524 && !ActivityInfo.isPreserveOrientationMode(mResizeMode);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001525 }
1526
skuhne@google.com322347b2016-12-02 12:54:03 -08001527 /**
1528 * Check that a given bounds matches the application requested orientation.
1529 *
1530 * @param bounds The bounds to be tested.
1531 * @return True if the requested bounds are okay for a resizing request.
1532 */
Wale Ogunwale069bbd32017-02-03 07:58:14 -08001533 private boolean canResizeToBounds(Rect bounds) {
skuhne@google.com322347b2016-12-02 12:54:03 -08001534 if (bounds == null || getStackId() != FREEFORM_WORKSPACE_STACK_ID) {
1535 // Note: If not on the freeform workspace, we ignore the bounds.
1536 return true;
1537 }
1538 final boolean landscape = bounds.width() > bounds.height();
1539 if (mResizeMode == RESIZE_MODE_FORCE_RESIZABLE_PRESERVE_ORIENTATION) {
1540 return mBounds == null || landscape == (mBounds.width() > mBounds.height());
1541 }
1542 return (mResizeMode != RESIZE_MODE_FORCE_RESIZABLE_PORTRAIT_ONLY || !landscape)
1543 && (mResizeMode != RESIZE_MODE_FORCE_RESIZABLE_LANDSCAPE_ONLY || landscape);
1544 }
1545
Craig Mautner525f3d92013-05-07 14:01:50 -07001546 /**
1547 * Find the activity in the history stack within the given task. Returns
1548 * the index within the history at which it's found, or < 0 if not found.
1549 */
1550 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r) {
1551 final ComponentName realActivity = r.realActivity;
1552 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1553 ActivityRecord candidate = mActivities.get(activityNdx);
1554 if (candidate.finishing) {
1555 continue;
1556 }
1557 if (candidate.realActivity.equals(realActivity)) {
1558 return candidate;
1559 }
1560 }
1561 return null;
1562 }
1563
Winson Chunga449dc02014-05-16 11:15:04 -07001564 /** Updates the last task description values. */
1565 void updateTaskDescription() {
1566 // Traverse upwards looking for any break between main task activities and
1567 // utility activities.
1568 int activityNdx;
1569 final int numActivities = mActivities.size();
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001570 final boolean relinquish = numActivities != 0 &&
1571 (mActivities.get(0).info.flags & FLAG_RELINQUISH_TASK_IDENTITY) != 0;
Winson Chunga449dc02014-05-16 11:15:04 -07001572 for (activityNdx = Math.min(numActivities, 1); activityNdx < numActivities;
Craig Mautner21d24a22014-04-23 11:45:37 -07001573 ++activityNdx) {
Winson Chunga449dc02014-05-16 11:15:04 -07001574 final ActivityRecord r = mActivities.get(activityNdx);
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001575 if (relinquish && (r.info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001576 // This will be the top activity for determining taskDescription. Pre-inc to
1577 // overcome initial decrement below.
1578 ++activityNdx;
1579 break;
1580 }
Winson Chunga449dc02014-05-16 11:15:04 -07001581 if (r.intent != null &&
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001582 (r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Winson Chunga449dc02014-05-16 11:15:04 -07001583 break;
1584 }
1585 }
1586 if (activityNdx > 0) {
1587 // Traverse downwards starting below break looking for set label, icon.
1588 // Note that if there are activities in the task but none of them set the
1589 // recent activity values, then we do not fall back to the last set
1590 // values in the TaskRecord.
1591 String label = null;
Craig Mautner648f69b2014-09-18 14:16:26 -07001592 String iconFilename = null;
Winson Chunga449dc02014-05-16 11:15:04 -07001593 int colorPrimary = 0;
Winson Chung1af8eda2016-02-05 17:55:56 +00001594 int colorBackground = 0;
Winson Chunga449dc02014-05-16 11:15:04 -07001595 for (--activityNdx; activityNdx >= 0; --activityNdx) {
1596 final ActivityRecord r = mActivities.get(activityNdx);
1597 if (r.taskDescription != null) {
1598 if (label == null) {
1599 label = r.taskDescription.getLabel();
1600 }
Craig Mautner648f69b2014-09-18 14:16:26 -07001601 if (iconFilename == null) {
1602 iconFilename = r.taskDescription.getIconFilename();
Winson Chunga449dc02014-05-16 11:15:04 -07001603 }
1604 if (colorPrimary == 0) {
1605 colorPrimary = r.taskDescription.getPrimaryColor();
Winson Chunga449dc02014-05-16 11:15:04 -07001606 }
Winson Chung1af8eda2016-02-05 17:55:56 +00001607 if (colorBackground == 0) {
1608 colorBackground = r.taskDescription.getBackgroundColor();
1609 }
Winson Chunga449dc02014-05-16 11:15:04 -07001610 }
1611 }
Winson Chung1af8eda2016-02-05 17:55:56 +00001612 lastTaskDescription = new TaskDescription(label, null, iconFilename, colorPrimary,
1613 colorBackground);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +01001614 if (mWindowContainerController != null) {
1615 mWindowContainerController.setTaskDescription(lastTaskDescription);
1616 }
Winson Chungec396d62014-08-06 17:08:00 -07001617 // Update the task affiliation color if we are the parent of the group
1618 if (taskId == mAffiliatedTaskId) {
1619 mAffiliatedTaskColor = lastTaskDescription.getPrimaryColor();
1620 }
Winson Chunga449dc02014-05-16 11:15:04 -07001621 }
1622 }
1623
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001624 int findEffectiveRootIndex() {
Craig Mautner4767f4b2014-09-18 15:38:33 -07001625 int effectiveNdx = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001626 final int topActivityNdx = mActivities.size() - 1;
Dianne Hackborn39569af2014-09-23 10:56:58 -07001627 for (int activityNdx = 0; activityNdx <= topActivityNdx; ++activityNdx) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001628 final ActivityRecord r = mActivities.get(activityNdx);
1629 if (r.finishing) {
1630 continue;
1631 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001632 effectiveNdx = activityNdx;
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001633 if ((r.info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001634 break;
1635 }
1636 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001637 return effectiveNdx;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001638 }
1639
1640 void updateEffectiveIntent() {
1641 final int effectiveRootIndex = findEffectiveRootIndex();
1642 final ActivityRecord r = mActivities.get(effectiveRootIndex);
Winson Chungfee26772014-08-05 12:21:52 -07001643 setIntent(r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001644 }
1645
Craig Mautner21d24a22014-04-23 11:45:37 -07001646 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001647 if (DEBUG_RECENTS) Slog.i(TAG_RECENTS, "Saving task=" + this);
Craig Mautner21d24a22014-04-23 11:45:37 -07001648
1649 out.attribute(null, ATTR_TASKID, String.valueOf(taskId));
1650 if (realActivity != null) {
1651 out.attribute(null, ATTR_REALACTIVITY, realActivity.flattenToShortString());
1652 }
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001653 out.attribute(null, ATTR_REALACTIVITY_SUSPENDED, String.valueOf(realActivitySuspended));
Craig Mautner21d24a22014-04-23 11:45:37 -07001654 if (origActivity != null) {
1655 out.attribute(null, ATTR_ORIGACTIVITY, origActivity.flattenToShortString());
1656 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001657 // Write affinity, and root affinity if it is different from affinity.
1658 // We use the special string "@" for a null root affinity, so we can identify
1659 // later whether we were given a root affinity or should just make it the
1660 // same as the affinity.
Craig Mautner21d24a22014-04-23 11:45:37 -07001661 if (affinity != null) {
1662 out.attribute(null, ATTR_AFFINITY, affinity);
Dianne Hackborn79228822014-09-16 11:11:23 -07001663 if (!affinity.equals(rootAffinity)) {
1664 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
1665 }
1666 } else if (rootAffinity != null) {
1667 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
Craig Mautner21d24a22014-04-23 11:45:37 -07001668 }
1669 out.attribute(null, ATTR_ROOTHASRESET, String.valueOf(rootWasReset));
Dianne Hackborn13420f22014-07-18 15:43:56 -07001670 out.attribute(null, ATTR_AUTOREMOVERECENTS, String.valueOf(autoRemoveRecents));
Craig Mautner21d24a22014-04-23 11:45:37 -07001671 out.attribute(null, ATTR_ASKEDCOMPATMODE, String.valueOf(askedCompatMode));
1672 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001673 out.attribute(null, ATTR_USER_SETUP_COMPLETE, String.valueOf(mUserSetupComplete));
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001674 out.attribute(null, ATTR_EFFECTIVE_UID, String.valueOf(effectiveUid));
Craig Mautner21d24a22014-04-23 11:45:37 -07001675 out.attribute(null, ATTR_TASKTYPE, String.valueOf(taskType));
Winson Chungffa2ec62014-07-03 15:54:42 -07001676 out.attribute(null, ATTR_FIRSTACTIVETIME, String.valueOf(firstActiveTime));
Winson Chungf1fbd772014-06-24 18:06:58 -07001677 out.attribute(null, ATTR_LASTACTIVETIME, String.valueOf(lastActiveTime));
Craig Mautner21d24a22014-04-23 11:45:37 -07001678 out.attribute(null, ATTR_LASTTIMEMOVED, String.valueOf(mLastTimeMoved));
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001679 out.attribute(null, ATTR_NEVERRELINQUISH, String.valueOf(mNeverRelinquishIdentity));
Craig Mautner21d24a22014-04-23 11:45:37 -07001680 if (lastDescription != null) {
1681 out.attribute(null, ATTR_LASTDESCRIPTION, lastDescription.toString());
1682 }
Winson Chung2cb86c72014-06-25 12:03:30 -07001683 if (lastTaskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07001684 lastTaskDescription.saveToXml(out);
Winson Chung2cb86c72014-06-25 12:03:30 -07001685 }
Winson Chung36f3f032016-09-08 23:29:43 +00001686 mLastThumbnailInfo.saveToXml(out);
Winson Chungec396d62014-08-06 17:08:00 -07001687 out.attribute(null, ATTR_TASK_AFFILIATION_COLOR, String.valueOf(mAffiliatedTaskColor));
Craig Mautnera228ae92014-07-09 05:44:55 -07001688 out.attribute(null, ATTR_TASK_AFFILIATION, String.valueOf(mAffiliatedTaskId));
1689 out.attribute(null, ATTR_PREV_AFFILIATION, String.valueOf(mPrevAffiliateTaskId));
1690 out.attribute(null, ATTR_NEXT_AFFILIATION, String.valueOf(mNextAffiliateTaskId));
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001691 out.attribute(null, ATTR_CALLING_UID, String.valueOf(mCallingUid));
1692 out.attribute(null, ATTR_CALLING_PACKAGE, mCallingPackage == null ? "" : mCallingPackage);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001693 out.attribute(null, ATTR_RESIZE_MODE, String.valueOf(mResizeMode));
Winson Chungd3395382016-12-13 11:49:09 -08001694 out.attribute(null, ATTR_SUPPORTS_PICTURE_IN_PICTURE,
1695 String.valueOf(mSupportsPictureInPicture));
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001696 out.attribute(null, ATTR_PRIVILEGED, String.valueOf(mPrivileged));
Wale Ogunwale706ed792015-08-02 10:29:44 -07001697 if (mLastNonFullscreenBounds != null) {
1698 out.attribute(
1699 null, ATTR_NON_FULLSCREEN_BOUNDS, mLastNonFullscreenBounds.flattenToString());
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001700 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001701 out.attribute(null, ATTR_MIN_WIDTH, String.valueOf(mMinWidth));
1702 out.attribute(null, ATTR_MIN_HEIGHT, String.valueOf(mMinHeight));
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001703 out.attribute(null, ATTR_PERSIST_TASK_VERSION, String.valueOf(PERSIST_TASK_VERSION));
Winson Chung2cb86c72014-06-25 12:03:30 -07001704
Craig Mautner21d24a22014-04-23 11:45:37 -07001705 if (affinityIntent != null) {
1706 out.startTag(null, TAG_AFFINITYINTENT);
1707 affinityIntent.saveToXml(out);
1708 out.endTag(null, TAG_AFFINITYINTENT);
1709 }
1710
Winson Chung36f3f032016-09-08 23:29:43 +00001711 out.startTag(null, TAG_INTENT);
1712 intent.saveToXml(out);
1713 out.endTag(null, TAG_INTENT);
Craig Mautner21d24a22014-04-23 11:45:37 -07001714
1715 final ArrayList<ActivityRecord> activities = mActivities;
1716 final int numActivities = activities.size();
1717 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1718 final ActivityRecord r = activities.get(activityNdx);
Craig Mautner43e52ed2014-06-16 17:18:52 -07001719 if (r.info.persistableMode == ActivityInfo.PERSIST_ROOT_ONLY || !r.isPersistable() ||
Wale Ogunwale843bfb92015-03-27 11:06:48 -07001720 ((r.intent.getFlags() & FLAG_ACTIVITY_NEW_DOCUMENT
1721 | FLAG_ACTIVITY_RETAIN_IN_RECENTS) == FLAG_ACTIVITY_NEW_DOCUMENT) &&
Craig Mautner43e52ed2014-06-16 17:18:52 -07001722 activityNdx > 0) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001723 // Stop at first non-persistable or first break in task (CLEAR_WHEN_TASK_RESET).
Craig Mautner21d24a22014-04-23 11:45:37 -07001724 break;
1725 }
1726 out.startTag(null, TAG_ACTIVITY);
1727 r.saveToXml(out);
1728 out.endTag(null, TAG_ACTIVITY);
1729 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001730 }
1731
Winson Chung36f3f032016-09-08 23:29:43 +00001732 static TaskRecord restoreFromXml(XmlPullParser in, ActivityStackSupervisor stackSupervisor)
1733 throws IOException, XmlPullParserException {
Craig Mautner21d24a22014-04-23 11:45:37 -07001734 Intent intent = null;
1735 Intent affinityIntent = null;
Winsonc809cbb2015-11-02 12:06:15 -08001736 ArrayList<ActivityRecord> activities = new ArrayList<>();
Craig Mautner21d24a22014-04-23 11:45:37 -07001737 ComponentName realActivity = null;
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001738 boolean realActivitySuspended = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001739 ComponentName origActivity = null;
1740 String affinity = null;
Dianne Hackborn79228822014-09-16 11:11:23 -07001741 String rootAffinity = null;
1742 boolean hasRootAffinity = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001743 boolean rootHasReset = false;
Dianne Hackborn13420f22014-07-18 15:43:56 -07001744 boolean autoRemoveRecents = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001745 boolean askedCompatMode = false;
1746 int taskType = ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner21d24a22014-04-23 11:45:37 -07001747 int userId = 0;
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001748 boolean userSetupComplete = true;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001749 int effectiveUid = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -07001750 String lastDescription = null;
Winson Chungffa2ec62014-07-03 15:54:42 -07001751 long firstActiveTime = -1;
Winson Chung2cb86c72014-06-25 12:03:30 -07001752 long lastActiveTime = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -07001753 long lastTimeOnTop = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001754 boolean neverRelinquishIdentity = true;
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001755 int taskId = INVALID_TASK_ID;
Craig Mautner21d24a22014-04-23 11:45:37 -07001756 final int outerDepth = in.getDepth();
Craig Mautner648f69b2014-09-18 14:16:26 -07001757 TaskDescription taskDescription = new TaskDescription();
Winsonc809cbb2015-11-02 12:06:15 -08001758 TaskThumbnailInfo thumbnailInfo = new TaskThumbnailInfo();
Wale Ogunwale18795a22014-12-03 11:38:33 -08001759 int taskAffiliation = INVALID_TASK_ID;
Winson Chungec396d62014-08-06 17:08:00 -07001760 int taskAffiliationColor = 0;
Wale Ogunwale18795a22014-12-03 11:38:33 -08001761 int prevTaskId = INVALID_TASK_ID;
1762 int nextTaskId = INVALID_TASK_ID;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001763 int callingUid = -1;
1764 String callingPackage = "";
Wale Ogunwaled829d362016-02-10 19:24:49 -08001765 int resizeMode = RESIZE_MODE_FORCE_RESIZEABLE;
Winson Chungd3395382016-12-13 11:49:09 -08001766 boolean supportsPictureInPicture = false;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001767 boolean privileged = false;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001768 Rect bounds = null;
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001769 int minWidth = INVALID_MIN_SIZE;
1770 int minHeight = INVALID_MIN_SIZE;
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001771 int persistTaskVersion = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -07001772
1773 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
1774 final String attrName = in.getAttributeName(attrNdx);
1775 final String attrValue = in.getAttributeValue(attrNdx);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001776 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: attribute name=" +
1777 attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001778 if (ATTR_TASKID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001779 if (taskId == INVALID_TASK_ID) taskId = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001780 } else if (ATTR_REALACTIVITY.equals(attrName)) {
1781 realActivity = ComponentName.unflattenFromString(attrValue);
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001782 } else if (ATTR_REALACTIVITY_SUSPENDED.equals(attrName)) {
1783 realActivitySuspended = Boolean.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001784 } else if (ATTR_ORIGACTIVITY.equals(attrName)) {
1785 origActivity = ComponentName.unflattenFromString(attrValue);
1786 } else if (ATTR_AFFINITY.equals(attrName)) {
1787 affinity = attrValue;
Dianne Hackborn79228822014-09-16 11:11:23 -07001788 } else if (ATTR_ROOT_AFFINITY.equals(attrName)) {
1789 rootAffinity = attrValue;
1790 hasRootAffinity = true;
Craig Mautner21d24a22014-04-23 11:45:37 -07001791 } else if (ATTR_ROOTHASRESET.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001792 rootHasReset = Boolean.parseBoolean(attrValue);
Dianne Hackborn13420f22014-07-18 15:43:56 -07001793 } else if (ATTR_AUTOREMOVERECENTS.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001794 autoRemoveRecents = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001795 } else if (ATTR_ASKEDCOMPATMODE.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001796 askedCompatMode = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001797 } else if (ATTR_USERID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001798 userId = Integer.parseInt(attrValue);
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001799 } else if (ATTR_USER_SETUP_COMPLETE.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001800 userSetupComplete = Boolean.parseBoolean(attrValue);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001801 } else if (ATTR_EFFECTIVE_UID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001802 effectiveUid = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001803 } else if (ATTR_TASKTYPE.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001804 taskType = Integer.parseInt(attrValue);
Winson Chungffa2ec62014-07-03 15:54:42 -07001805 } else if (ATTR_FIRSTACTIVETIME.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001806 firstActiveTime = Long.parseLong(attrValue);
Winson Chungf1fbd772014-06-24 18:06:58 -07001807 } else if (ATTR_LASTACTIVETIME.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001808 lastActiveTime = Long.parseLong(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001809 } else if (ATTR_LASTDESCRIPTION.equals(attrName)) {
1810 lastDescription = attrValue;
1811 } else if (ATTR_LASTTIMEMOVED.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001812 lastTimeOnTop = Long.parseLong(attrValue);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001813 } else if (ATTR_NEVERRELINQUISH.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001814 neverRelinquishIdentity = Boolean.parseBoolean(attrValue);
Winsonc809cbb2015-11-02 12:06:15 -08001815 } else if (attrName.startsWith(TaskThumbnailInfo.ATTR_TASK_THUMBNAILINFO_PREFIX)) {
1816 thumbnailInfo.restoreFromXml(attrName, attrValue);
Craig Mautner648f69b2014-09-18 14:16:26 -07001817 } else if (attrName.startsWith(TaskDescription.ATTR_TASKDESCRIPTION_PREFIX)) {
1818 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001819 } else if (ATTR_TASK_AFFILIATION.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001820 taskAffiliation = Integer.parseInt(attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001821 } else if (ATTR_PREV_AFFILIATION.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001822 prevTaskId = Integer.parseInt(attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001823 } else if (ATTR_NEXT_AFFILIATION.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001824 nextTaskId = Integer.parseInt(attrValue);
Winson Chungec396d62014-08-06 17:08:00 -07001825 } else if (ATTR_TASK_AFFILIATION_COLOR.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001826 taskAffiliationColor = Integer.parseInt(attrValue);
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001827 } else if (ATTR_CALLING_UID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001828 callingUid = Integer.parseInt(attrValue);
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001829 } else if (ATTR_CALLING_PACKAGE.equals(attrName)) {
1830 callingPackage = attrValue;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001831 } else if (ATTR_RESIZE_MODE.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001832 resizeMode = Integer.parseInt(attrValue);
Winson Chungd3395382016-12-13 11:49:09 -08001833 } else if (ATTR_SUPPORTS_PICTURE_IN_PICTURE.equals(attrName)) {
1834 supportsPictureInPicture = Boolean.parseBoolean(attrValue);
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001835 } else if (ATTR_PRIVILEGED.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001836 privileged = Boolean.parseBoolean(attrValue);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001837 } else if (ATTR_NON_FULLSCREEN_BOUNDS.equals(attrName)) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001838 bounds = Rect.unflattenFromString(attrValue);
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001839 } else if (ATTR_MIN_WIDTH.equals(attrName)) {
1840 minWidth = Integer.parseInt(attrValue);
1841 } else if (ATTR_MIN_HEIGHT.equals(attrName)) {
1842 minHeight = Integer.parseInt(attrValue);
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001843 } else if (ATTR_PERSIST_TASK_VERSION.equals(attrName)) {
1844 persistTaskVersion = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001845 } else {
1846 Slog.w(TAG, "TaskRecord: Unknown attribute=" + attrName);
1847 }
1848 }
1849
1850 int event;
1851 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
Ben Kwa8814cf42015-07-08 10:54:56 -07001852 (event != XmlPullParser.END_TAG || in.getDepth() >= outerDepth)) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001853 if (event == XmlPullParser.START_TAG) {
1854 final String name = in.getName();
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001855 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: START_TAG name=" +
1856 name);
Craig Mautner21d24a22014-04-23 11:45:37 -07001857 if (TAG_AFFINITYINTENT.equals(name)) {
1858 affinityIntent = Intent.restoreFromXml(in);
1859 } else if (TAG_INTENT.equals(name)) {
1860 intent = Intent.restoreFromXml(in);
1861 } else if (TAG_ACTIVITY.equals(name)) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001862 ActivityRecord activity = ActivityRecord.restoreFromXml(in, stackSupervisor);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001863 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: activity=" +
1864 activity);
Craig Mautner21d24a22014-04-23 11:45:37 -07001865 if (activity != null) {
1866 activities.add(activity);
1867 }
1868 } else {
1869 Slog.e(TAG, "restoreTask: Unexpected name=" + name);
1870 XmlUtils.skipCurrentTag(in);
1871 }
1872 }
1873 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001874 if (!hasRootAffinity) {
1875 rootAffinity = affinity;
1876 } else if ("@".equals(rootAffinity)) {
1877 rootAffinity = null;
1878 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001879 if (effectiveUid <= 0) {
1880 Intent checkIntent = intent != null ? intent : affinityIntent;
1881 effectiveUid = 0;
1882 if (checkIntent != null) {
1883 IPackageManager pm = AppGlobals.getPackageManager();
1884 try {
1885 ApplicationInfo ai = pm.getApplicationInfo(
1886 checkIntent.getComponent().getPackageName(),
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -07001887 PackageManager.MATCH_UNINSTALLED_PACKAGES
1888 | PackageManager.MATCH_DISABLED_COMPONENTS, userId);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001889 if (ai != null) {
1890 effectiveUid = ai.uid;
1891 }
1892 } catch (RemoteException e) {
1893 }
1894 }
1895 Slog.w(TAG, "Updating task #" + taskId + " for " + checkIntent
1896 + ": effectiveUid=" + effectiveUid);
1897 }
1898
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001899 if (persistTaskVersion < 1) {
1900 // We need to convert the resize mode of home activities saved before version one if
1901 // they are marked as RESIZE_MODE_RESIZEABLE to RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION
1902 // since we didn't have that differentiation before version 1 and the system didn't
1903 // resize home activities before then.
1904 if (taskType == HOME_ACTIVITY_TYPE && resizeMode == RESIZE_MODE_RESIZEABLE) {
1905 resizeMode = RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
1906 }
Winson Chungd3395382016-12-13 11:49:09 -08001907 } else {
1908 // This activity has previously marked itself explicitly as both resizeable and
1909 // supporting picture-in-picture. Since there is no longer a requirement for
1910 // picture-in-picture activities to be resizeable, we can mark this simply as
1911 // resizeable and supporting picture-in-picture separately.
1912 if (resizeMode == RESIZE_MODE_RESIZEABLE_AND_PIPABLE_DEPRECATED) {
1913 resizeMode = RESIZE_MODE_RESIZEABLE;
1914 supportsPictureInPicture = true;
1915 }
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001916 }
1917
Winson Chung36f3f032016-09-08 23:29:43 +00001918 final TaskRecord task = new TaskRecord(stackSupervisor.mService, taskId, intent,
Dianne Hackborn79228822014-09-16 11:11:23 -07001919 affinityIntent, affinity, rootAffinity, realActivity, origActivity, rootHasReset,
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001920 autoRemoveRecents, askedCompatMode, taskType, userId, effectiveUid, lastDescription,
1921 activities, firstActiveTime, lastActiveTime, lastTimeOnTop, neverRelinquishIdentity,
Winsonc809cbb2015-11-02 12:06:15 -08001922 taskDescription, thumbnailInfo, taskAffiliation, prevTaskId, nextTaskId,
Winson Chungd3395382016-12-13 11:49:09 -08001923 taskAffiliationColor, callingUid, callingPackage, resizeMode,
1924 supportsPictureInPicture, privileged, realActivitySuspended, userSetupComplete,
1925 minWidth, minHeight);
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07001926 task.updateOverrideConfiguration(bounds);
Craig Mautner21d24a22014-04-23 11:45:37 -07001927
1928 for (int activityNdx = activities.size() - 1; activityNdx >=0; --activityNdx) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001929 activities.get(activityNdx).task = task;
Craig Mautner21d24a22014-04-23 11:45:37 -07001930 }
1931
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001932 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Restored task=" + task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001933 return task;
1934 }
1935
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001936 private void adjustForMinimalTaskDimensions(Rect bounds) {
1937 if (bounds == null) {
1938 return;
1939 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001940 int minWidth = mMinWidth;
1941 int minHeight = mMinHeight;
Robert Carr9c5867d2016-03-10 15:52:46 -08001942 // If the task has no requested minimal size, we'd like to enforce a minimal size
1943 // so that the user can not render the task too small to manipulate. We don't need
1944 // to do this for the pinned stack as the bounds are controlled by the system.
Andrii Kulian02b7a832016-10-06 23:11:56 -07001945 if (getStackId() != PINNED_STACK_ID) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001946 if (minWidth == INVALID_MIN_SIZE) {
1947 minWidth = mService.mStackSupervisor.mDefaultMinSizeOfResizeableTask;
Andrii Kulian2e751b82016-03-16 16:59:32 -07001948 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001949 if (minHeight == INVALID_MIN_SIZE) {
1950 minHeight = mService.mStackSupervisor.mDefaultMinSizeOfResizeableTask;
Andrii Kulian2e751b82016-03-16 16:59:32 -07001951 }
Robert Carr9c5867d2016-03-10 15:52:46 -08001952 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001953 final boolean adjustWidth = minWidth > bounds.width();
1954 final boolean adjustHeight = minHeight > bounds.height();
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001955 if (!(adjustWidth || adjustHeight)) {
1956 return;
1957 }
1958
1959 if (adjustWidth) {
1960 if (mBounds != null && bounds.right == mBounds.right) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001961 bounds.left = bounds.right - minWidth;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001962 } else {
1963 // Either left bounds match, or neither match, or the previous bounds were
1964 // fullscreen and we default to keeping left.
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001965 bounds.right = bounds.left + minWidth;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001966 }
1967 }
1968 if (adjustHeight) {
1969 if (mBounds != null && bounds.bottom == mBounds.bottom) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001970 bounds.top = bounds.bottom - minHeight;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001971 } else {
1972 // Either top bounds match, or neither match, or the previous bounds were
1973 // fullscreen and we default to keeping top.
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001974 bounds.bottom = bounds.top + minHeight;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001975 }
1976 }
1977 }
1978
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001979 /**
1980 * Update task's override configuration based on the bounds.
Jorim Jaggi0a932142016-02-01 17:42:25 -08001981 * @param bounds The bounds of the task.
Andrii Kulian8072d112016-09-16 11:11:01 -07001982 * @return True if the override configuration was updated.
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001983 */
Andrii Kulian8072d112016-09-16 11:11:01 -07001984 boolean updateOverrideConfiguration(Rect bounds) {
Jorim Jaggi0a932142016-02-01 17:42:25 -08001985 return updateOverrideConfiguration(bounds, null /* insetBounds */);
1986 }
1987
1988 /**
1989 * Update task's override configuration based on the bounds.
1990 * @param bounds The bounds of the task.
1991 * @param insetBounds The bounds used to calculate the system insets, which is used here to
1992 * subtract the navigation bar/status bar size from the screen size reported
1993 * to the application. See {@link IActivityManager#resizeDockedStack}.
Andrii Kulian8072d112016-09-16 11:11:01 -07001994 * @return True if the override configuration was updated.
Jorim Jaggi0a932142016-02-01 17:42:25 -08001995 */
Andrii Kulian8072d112016-09-16 11:11:01 -07001996 boolean updateOverrideConfiguration(Rect bounds, @Nullable Rect insetBounds) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001997 if (Objects.equals(mBounds, bounds)) {
Andrii Kulian8072d112016-09-16 11:11:01 -07001998 return false;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001999 }
Andrii Kulian1779e612016-10-12 21:58:25 -07002000 mTmpConfig.setTo(getOverrideConfiguration());
Wale Ogunwale5f986092015-12-04 15:35:38 -08002001 final boolean oldFullscreen = mFullscreen;
Andrii Kulian1779e612016-10-12 21:58:25 -07002002 final Configuration newConfig = getOverrideConfiguration();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002003
2004 mFullscreen = bounds == null;
Wale Ogunwale706ed792015-08-02 10:29:44 -07002005 if (mFullscreen) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002006 if (mBounds != null && StackId.persistTaskBounds(mStack.mStackId)) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002007 mLastNonFullscreenBounds = mBounds;
2008 }
2009 mBounds = null;
Andrii Kulian1779e612016-10-12 21:58:25 -07002010 newConfig.unset();
Wale Ogunwale706ed792015-08-02 10:29:44 -07002011 } else {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002012 mTmpRect.set(bounds);
2013 adjustForMinimalTaskDimensions(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -08002014 if (mBounds == null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002015 mBounds = new Rect(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -08002016 } else {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08002017 mBounds.set(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -08002018 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002019 if (mStack == null || StackId.persistTaskBounds(mStack.mStackId)) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002020 mLastNonFullscreenBounds = mBounds;
2021 }
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002022 computeOverrideConfiguration(newConfig, mTmpRect, insetBounds,
Andrii Kuliandaea3572016-04-08 13:20:51 -07002023 mTmpRect.right != bounds.right, mTmpRect.bottom != bounds.bottom);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002024 }
Andrii Kulian1779e612016-10-12 21:58:25 -07002025 onOverrideConfigurationChanged(newConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002026
2027 if (mFullscreen != oldFullscreen) {
Andrii Kulian933076d2016-03-29 17:04:42 -07002028 mService.mStackSupervisor.scheduleReportMultiWindowModeChanged(this);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002029 }
2030
Andrii Kulian1779e612016-10-12 21:58:25 -07002031 return !mTmpConfig.equals(newConfig);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002032 }
2033
Andrii Kulian1779e612016-10-12 21:58:25 -07002034 /** Clears passed config and fills it with new override values. */
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002035 // TODO(b/36505427): TaskRecord.computeOverrideConfiguration() is a utility method that doesn't
2036 // depend on task or stacks, but uses those object to get the display to base the calculation
2037 // on. Probably best to centralize calculations like this in ConfigurationContainer.
2038 void computeOverrideConfiguration(Configuration config, Rect bounds, Rect insetBounds,
Andrii Kuliana8a9bc52016-10-14 11:00:13 -07002039 boolean overrideWidth, boolean overrideHeight) {
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002040 mTmpNonDecorBounds.set(bounds);
2041 mTmpStableBounds.set(bounds);
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002042
Andrii Kulian1779e612016-10-12 21:58:25 -07002043 config.unset();
Winson Chungbdc646f2017-02-13 12:12:22 -08002044 final Configuration parentConfig = getParent().getConfiguration();
Andrii Kulian1779e612016-10-12 21:58:25 -07002045 final float density = parentConfig.densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002046
Winson Chungbdc646f2017-02-13 12:12:22 -08002047 if (mStack != null) {
2048 final StackWindowController stackController = mStack.getWindowContainerController();
2049 stackController.adjustConfigurationForBounds(bounds, insetBounds,
2050 mTmpNonDecorBounds, mTmpStableBounds, overrideWidth, overrideHeight, density,
2051 config, parentConfig);
2052 } else {
2053 // No stack, give some default values
2054 config.smallestScreenWidthDp =
2055 mService.mStackSupervisor.mDefaultMinSizeOfResizeableTask;
2056 config.screenWidthDp = config.screenHeightDp = config.smallestScreenWidthDp;
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002057 Slog.wtf(TAG, "Expected stack when calculating override config");
Winson Chungbdc646f2017-02-13 12:12:22 -08002058 }
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002059
Winson Chung60c1aba2017-03-14 17:47:42 -07002060 config.orientation = (config.screenWidthDp <= config.screenHeightDp)
2061 ? Configuration.ORIENTATION_PORTRAIT
2062 : Configuration.ORIENTATION_LANDSCAPE;
2063
Jorim Jaggi85639432016-05-06 17:27:55 -07002064 // For calculating screen layout, we need to use the non-decor inset screen area for the
2065 // calculation for compatibility reasons, i.e. screen area without system bars that could
2066 // never go away in Honeycomb.
Winson Chungbdc646f2017-02-13 12:12:22 -08002067 final int compatScreenWidthDp = (int) (mTmpNonDecorBounds.width() / density);
2068 final int compatScreenHeightDp = (int) (mTmpNonDecorBounds.height() / density);
Andrii Kulian1779e612016-10-12 21:58:25 -07002069 // We're only overriding LONG, SIZE and COMPAT parts of screenLayout, so we start override
2070 // calculation with partial default.
2071 final int sl = Configuration.SCREENLAYOUT_LONG_YES | Configuration.SCREENLAYOUT_SIZE_XLARGE;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002072 final int longSize = Math.max(compatScreenHeightDp, compatScreenWidthDp);
Andrii Kulian1779e612016-10-12 21:58:25 -07002073 final int shortSize = Math.min(compatScreenHeightDp, compatScreenWidthDp);
Jorim Jaggi85639432016-05-06 17:27:55 -07002074 config.screenLayout = Configuration.reduceScreenLayout(sl, longSize, shortSize);
2075
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08002076 }
2077
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002078 Rect updateOverrideConfigurationFromLaunchBounds() {
2079 final Rect bounds = validateBounds(getLaunchBounds());
2080 updateOverrideConfiguration(bounds);
Andrii Kulian73336d812016-03-24 12:56:08 -07002081 if (bounds != null) {
2082 bounds.set(mBounds);
2083 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002084 return bounds;
2085 }
2086
2087 static Rect validateBounds(Rect bounds) {
2088 if (bounds != null && bounds.isEmpty()) {
2089 Slog.wtf(TAG, "Received strange task bounds: " + bounds, new Throwable());
2090 return null;
2091 }
2092 return bounds;
2093 }
2094
Wale Ogunwale935e5022015-11-10 12:36:10 -08002095 /** Updates the task's bounds and override configuration to match what is expected for the
2096 * input stack. */
2097 void updateOverrideConfigurationForStack(ActivityStack inStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002098 if (mStack != null && mStack == inStack) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08002099 return;
2100 }
2101
2102 if (inStack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002103 if (!isResizeable()) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08002104 throw new IllegalArgumentException("Can not position non-resizeable task="
2105 + this + " in stack=" + inStack);
2106 }
2107 if (mBounds != null) {
2108 return;
2109 }
2110 if (mLastNonFullscreenBounds != null) {
2111 updateOverrideConfiguration(mLastNonFullscreenBounds);
2112 } else {
2113 inStack.layoutTaskInStack(this, null);
2114 }
2115 } else {
2116 updateOverrideConfiguration(inStack.mBounds);
2117 }
2118 }
2119
Chong Zhang0fa656b2015-08-31 15:17:21 -07002120 /**
2121 * Returns the correct stack to use based on task type and currently set bounds,
2122 * regardless of the focused stack and current stack association of the task.
2123 * The task will be moved (and stack focus changed) later if necessary.
2124 */
2125 int getLaunchStackId() {
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002126 if (isRecentsTask()) {
2127 return RECENTS_STACK_ID;
2128 }
2129 if (isHomeTask()) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002130 return HOME_STACK_ID;
2131 }
Winson Chung83471632016-12-13 11:02:12 -08002132 if (isAssistantTask()) {
2133 return ASSISTANT_STACK_ID;
2134 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002135 if (mBounds != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002136 return FREEFORM_WORKSPACE_STACK_ID;
2137 }
2138 return FULLSCREEN_WORKSPACE_STACK_ID;
2139 }
2140
2141 /** Returns the bounds that should be used to launch this task. */
2142 Rect getLaunchBounds() {
Chong Zhang75b37202015-12-04 14:16:36 -08002143 // If we're over lockscreen, forget about stack bounds and use fullscreen.
Jorim Jaggife762342016-10-13 14:33:27 +02002144 if (mService.mStackSupervisor.mKeyguardController.isKeyguardShowing()) {
Chong Zhang75b37202015-12-04 14:16:36 -08002145 return null;
2146 }
2147
Andrii Kulian02b7a832016-10-06 23:11:56 -07002148 if (mStack == null) {
Chong Zhang7d5f5102016-01-13 10:29:24 -08002149 return null;
2150 }
2151
Andrii Kulian02b7a832016-10-06 23:11:56 -07002152 final int stackId = mStack.mStackId;
Chong Zhang7d5f5102016-01-13 10:29:24 -08002153 if (stackId == HOME_STACK_ID
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002154 || stackId == RECENTS_STACK_ID
Winson Chung83471632016-12-13 11:02:12 -08002155 || stackId == ASSISTANT_STACK_ID
Chong Zhang7d5f5102016-01-13 10:29:24 -08002156 || stackId == FULLSCREEN_WORKSPACE_STACK_ID
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002157 || (stackId == DOCKED_STACK_ID && !isResizeable())) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002158 return isResizeable() ? mStack.mBounds : null;
Wale Ogunwale3797c222015-10-27 14:21:58 -07002159 } else if (!StackId.persistTaskBounds(stackId)) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002160 return mStack.mBounds;
Wale Ogunwale706ed792015-08-02 10:29:44 -07002161 }
2162 return mLastNonFullscreenBounds;
2163 }
2164
Wale Ogunwale39381972015-12-17 17:15:29 -08002165 boolean canMatchRootAffinity() {
2166 // We don't allow root affinity matching on the pinned stack as no other task should
2167 // be launching in it based on affinity.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002168 return rootAffinity != null && getStackId() != PINNED_STACK_ID;
Wale Ogunwale39381972015-12-17 17:15:29 -08002169 }
2170
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01002171 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
2172 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2173 final ActivityRecord r = mActivities.get(activityNdx);
2174 if (r.visible) {
2175 r.showStartingWindow(null /* prev */, false /* newTask */, taskSwitch);
2176 }
2177 }
2178 }
2179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002181 pw.print(prefix); pw.print("userId="); pw.print(userId);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07002182 pw.print(" effectiveUid="); UserHandle.formatUid(pw, effectiveUid);
2183 pw.print(" mCallingUid="); UserHandle.formatUid(pw, mCallingUid);
Suprabh Shukla7745c142016-03-07 18:21:10 -08002184 pw.print(" mUserSetupComplete="); pw.print(mUserSetupComplete);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002185 pw.print(" mCallingPackage="); pw.println(mCallingPackage);
Dianne Hackborn79228822014-09-16 11:11:23 -07002186 if (affinity != null || rootAffinity != null) {
2187 pw.print(prefix); pw.print("affinity="); pw.print(affinity);
2188 if (affinity == null || !affinity.equals(rootAffinity)) {
2189 pw.print(" root="); pw.println(rootAffinity);
2190 } else {
2191 pw.println();
2192 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002193 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002194 if (voiceSession != null || voiceInteractor != null) {
2195 pw.print(prefix); pw.print("VOICE: session=0x");
2196 pw.print(Integer.toHexString(System.identityHashCode(voiceSession)));
2197 pw.print(" interactor=0x");
2198 pw.println(Integer.toHexString(System.identityHashCode(voiceInteractor)));
2199 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002200 if (intent != null) {
2201 StringBuilder sb = new StringBuilder(128);
2202 sb.append(prefix); sb.append("intent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08002203 intent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002204 sb.append('}');
2205 pw.println(sb.toString());
2206 }
2207 if (affinityIntent != null) {
2208 StringBuilder sb = new StringBuilder(128);
2209 sb.append(prefix); sb.append("affinityIntent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08002210 affinityIntent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002211 sb.append('}');
2212 pw.println(sb.toString());
2213 }
2214 if (origActivity != null) {
2215 pw.print(prefix); pw.print("origActivity=");
2216 pw.println(origActivity.flattenToShortString());
2217 }
2218 if (realActivity != null) {
2219 pw.print(prefix); pw.print("realActivity=");
2220 pw.println(realActivity.flattenToShortString());
2221 }
Dianne Hackborn852975d2014-08-22 17:42:43 -07002222 if (autoRemoveRecents || isPersistable || taskType != 0 || mTaskToReturnTo != 0
2223 || numFullscreen != 0) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002224 pw.print(prefix); pw.print("autoRemoveRecents="); pw.print(autoRemoveRecents);
Dianne Hackborn852975d2014-08-22 17:42:43 -07002225 pw.print(" isPersistable="); pw.print(isPersistable);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002226 pw.print(" numFullscreen="); pw.print(numFullscreen);
2227 pw.print(" taskType="); pw.print(taskType);
2228 pw.print(" mTaskToReturnTo="); pw.println(mTaskToReturnTo);
2229 }
Craig Mautner432f64e2015-05-20 14:59:57 -07002230 if (rootWasReset || mNeverRelinquishIdentity || mReuseTask
2231 || mLockTaskAuth != LOCK_TASK_AUTH_PINNABLE) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002232 pw.print(prefix); pw.print("rootWasReset="); pw.print(rootWasReset);
2233 pw.print(" mNeverRelinquishIdentity="); pw.print(mNeverRelinquishIdentity);
Craig Mautner432f64e2015-05-20 14:59:57 -07002234 pw.print(" mReuseTask="); pw.print(mReuseTask);
2235 pw.print(" mLockTaskAuth="); pw.println(lockTaskAuthToString());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002236 }
Wale Ogunwale18795a22014-12-03 11:38:33 -08002237 if (mAffiliatedTaskId != taskId || mPrevAffiliateTaskId != INVALID_TASK_ID
2238 || mPrevAffiliate != null || mNextAffiliateTaskId != INVALID_TASK_ID
2239 || mNextAffiliate != null) {
Dianne Hackborn852975d2014-08-22 17:42:43 -07002240 pw.print(prefix); pw.print("affiliation="); pw.print(mAffiliatedTaskId);
2241 pw.print(" prevAffiliation="); pw.print(mPrevAffiliateTaskId);
2242 pw.print(" (");
2243 if (mPrevAffiliate == null) {
2244 pw.print("null");
2245 } else {
2246 pw.print(Integer.toHexString(System.identityHashCode(mPrevAffiliate)));
2247 }
2248 pw.print(") nextAffiliation="); pw.print(mNextAffiliateTaskId);
2249 pw.print(" (");
2250 if (mNextAffiliate == null) {
2251 pw.print("null");
2252 } else {
2253 pw.print(Integer.toHexString(System.identityHashCode(mNextAffiliate)));
2254 }
2255 pw.println(")");
2256 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002257 pw.print(prefix); pw.print("Activities="); pw.println(mActivities);
Dianne Hackborn852975d2014-08-22 17:42:43 -07002258 if (!askedCompatMode || !inRecents || !isAvailable) {
2259 pw.print(prefix); pw.print("askedCompatMode="); pw.print(askedCompatMode);
2260 pw.print(" inRecents="); pw.print(inRecents);
2261 pw.print(" isAvailable="); pw.println(isAvailable);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07002262 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002263 pw.print(prefix); pw.print("lastThumbnail="); pw.print(mLastThumbnail);
Dianne Hackborn852975d2014-08-22 17:42:43 -07002264 pw.print(" lastThumbnailFile="); pw.println(mLastThumbnailFile);
2265 if (lastDescription != null) {
2266 pw.print(prefix); pw.print("lastDescription="); pw.println(lastDescription);
2267 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002268 pw.print(prefix); pw.print("stackId="); pw.println(getStackId());
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002269 pw.print(prefix + "hasBeenVisible=" + hasBeenVisible);
2270 pw.print(" mResizeMode=" + ActivityInfo.resizeModeToString(mResizeMode));
Winson Chungd3395382016-12-13 11:49:09 -08002271 pw.print(" mSupportsPictureInPicture=" + mSupportsPictureInPicture);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002272 pw.print(" isResizeable=" + isResizeable());
Winson Chung36f3f032016-09-08 23:29:43 +00002273 pw.print(" firstActiveTime=" + lastActiveTime);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002274 pw.print(" lastActiveTime=" + lastActiveTime);
2275 pw.println(" (inactive for " + (getInactiveDuration() / 1000) + "s)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 }
2277
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002278 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002280 StringBuilder sb = new StringBuilder(128);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002281 if (stringName != null) {
2282 sb.append(stringName);
2283 sb.append(" U=");
2284 sb.append(userId);
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002285 sb.append(" StackId=");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002286 sb.append(getStackId());
Craig Mautnerde4ef022013-04-07 19:01:33 -07002287 sb.append(" sz=");
2288 sb.append(mActivities.size());
2289 sb.append('}');
2290 return sb.toString();
2291 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002292 sb.append("TaskRecord{");
2293 sb.append(Integer.toHexString(System.identityHashCode(this)));
2294 sb.append(" #");
2295 sb.append(taskId);
2296 if (affinity != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002297 sb.append(" A=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002298 sb.append(affinity);
2299 } else if (intent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002300 sb.append(" I=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002301 sb.append(intent.getComponent().flattenToShortString());
2302 } else if (affinityIntent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002303 sb.append(" aI=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002304 sb.append(affinityIntent.getComponent().flattenToShortString());
2305 } else {
2306 sb.append(" ??");
2307 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002308 stringName = sb.toString();
2309 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 }
2311}