blob: 99fe418e66e12b3cab0444738eee724a1bbfc9d4 [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;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800115import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Jorim Jaggi0a932142016-02-01 17:42:25 -0800116
Winson Chung74666102017-02-22 17:49:24 -0800117import static java.lang.Integer.MAX_VALUE;
118
Jorim Jaggifb9d78a2017-01-05 18:57:12 +0100119final class TaskRecord extends ConfigurationContainer implements TaskWindowContainerListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800120 private static final String TAG = TAG_WITH_CLASS_NAME ? "TaskRecord" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700121 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700122 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Craig Mautnere0570202015-05-13 13:06:11 -0700123 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700124 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800125
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700126 private static final String ATTR_TASKID = "task_id";
Craig Mautner21d24a22014-04-23 11:45:37 -0700127 private static final String TAG_INTENT = "intent";
128 private static final String TAG_AFFINITYINTENT = "affinity_intent";
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700129 private static final String ATTR_REALACTIVITY = "real_activity";
130 private static final String ATTR_REALACTIVITY_SUSPENDED = "real_activity_suspended";
Craig Mautner21d24a22014-04-23 11:45:37 -0700131 private static final String ATTR_ORIGACTIVITY = "orig_activity";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700132 private static final String TAG_ACTIVITY = "activity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700133 private static final String ATTR_AFFINITY = "affinity";
Dianne Hackborn79228822014-09-16 11:11:23 -0700134 private static final String ATTR_ROOT_AFFINITY = "root_affinity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700135 private static final String ATTR_ROOTHASRESET = "root_has_reset";
Dianne Hackborn13420f22014-07-18 15:43:56 -0700136 private static final String ATTR_AUTOREMOVERECENTS = "auto_remove_recents";
Craig Mautner21d24a22014-04-23 11:45:37 -0700137 private static final String ATTR_ASKEDCOMPATMODE = "asked_compat_mode";
138 private static final String ATTR_USERID = "user_id";
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800139 private static final String ATTR_USER_SETUP_COMPLETE = "user_setup_complete";
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700140 private static final String ATTR_EFFECTIVE_UID = "effective_uid";
Craig Mautner21d24a22014-04-23 11:45:37 -0700141 private static final String ATTR_TASKTYPE = "task_type";
Winson Chungffa2ec62014-07-03 15:54:42 -0700142 private static final String ATTR_FIRSTACTIVETIME = "first_active_time";
Winson Chungf1fbd772014-06-24 18:06:58 -0700143 private static final String ATTR_LASTACTIVETIME = "last_active_time";
Craig Mautner21d24a22014-04-23 11:45:37 -0700144 private static final String ATTR_LASTDESCRIPTION = "last_description";
145 private static final String ATTR_LASTTIMEMOVED = "last_time_moved";
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700146 private static final String ATTR_NEVERRELINQUISH = "never_relinquish_identity";
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700147 private static final String ATTR_TASK_AFFILIATION = "task_affiliation";
Craig Mautnera228ae92014-07-09 05:44:55 -0700148 private static final String ATTR_PREV_AFFILIATION = "prev_affiliation";
149 private static final String ATTR_NEXT_AFFILIATION = "next_affiliation";
Winson Chungec396d62014-08-06 17:08:00 -0700150 private static final String ATTR_TASK_AFFILIATION_COLOR = "task_affiliation_color";
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700151 private static final String ATTR_CALLING_UID = "calling_uid";
152 private static final String ATTR_CALLING_PACKAGE = "calling_package";
Winson Chungd3395382016-12-13 11:49:09 -0800153 private static final String ATTR_SUPPORTS_PICTURE_IN_PICTURE = "supports_picture_in_picture";
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800154 private static final String ATTR_RESIZE_MODE = "resize_mode";
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700155 private static final String ATTR_PRIVILEGED = "privileged";
Wale Ogunwale706ed792015-08-02 10:29:44 -0700156 private static final String ATTR_NON_FULLSCREEN_BOUNDS = "non_fullscreen_bounds";
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700157 private static final String ATTR_MIN_WIDTH = "min_width";
158 private static final String ATTR_MIN_HEIGHT = "min_height";
Wale Ogunwale625ed0c2016-10-18 08:50:31 -0700159 private static final String ATTR_PERSIST_TASK_VERSION = "persist_task_version";
Andrii Kulian18d75122016-03-27 20:20:28 -0700160
Wale Ogunwale625ed0c2016-10-18 08:50:31 -0700161 // Current version of the task record we persist. Used to check if we need to run any upgrade
162 // code.
163 private static final int PERSIST_TASK_VERSION = 1;
Craig Mautner21d24a22014-04-23 11:45:37 -0700164 private static final String TASK_THUMBNAIL_SUFFIX = "_task_thumbnail";
165
Wale Ogunwale18795a22014-12-03 11:38:33 -0800166 static final int INVALID_TASK_ID = -1;
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700167 private static final int INVALID_MIN_SIZE = -1;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800168
Winson Chung74666102017-02-22 17:49:24 -0800169 /**
170 * The modes to control how the stack is moved to the front when calling
171 * {@link TaskRecord#reparent}.
172 */
173 @Retention(RetentionPolicy.SOURCE)
174 @IntDef({
175 REPARENT_MOVE_STACK_TO_FRONT,
176 REPARENT_KEEP_STACK_AT_FRONT,
177 REPARENT_LEAVE_STACK_IN_PLACE
178 })
179 public @interface ReparentMoveStackMode {}
180 // Moves the stack to the front if it was not at the front
181 public static final int REPARENT_MOVE_STACK_TO_FRONT = 0;
182 // Only moves the stack to the front if it was focused or front most already
183 public static final int REPARENT_KEEP_STACK_AT_FRONT = 1;
184 // Do not move the stack as a part of reparenting
185 public static final int REPARENT_LEAVE_STACK_IN_PLACE = 2;
186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 final int taskId; // Unique identifier for this task.
Dianne Hackborn79228822014-09-16 11:11:23 -0700188 String affinity; // The affinity name for this task, or null; may change identity.
189 String rootAffinity; // Initial base affinity, or null; does not change from initial root.
Dianne Hackborn91097de2014-04-04 18:02:06 -0700190 final IVoiceInteractionSession voiceSession; // Voice interaction session driving task
191 final IVoiceInteractor voiceInteractor; // Associated interactor to provide to app
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 Intent intent; // The original intent that started the task.
193 Intent affinityIntent; // Intent of affinity-moved activity that started this task.
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700194 int effectiveUid; // The current effective uid of the identity of this task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 ComponentName origActivity; // The non-alias activity component of the intent.
196 ComponentName realActivity; // The actual activity component that started the task.
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +0000197 boolean realActivitySuspended; // True if the actual activity component that started the
198 // task is suspended.
Winson Chung36f3f032016-09-08 23:29:43 +0000199 long firstActiveTime; // First time this task was active.
200 long lastActiveTime; // Last time this task was active, including sleep.
Dianne Hackborn852975d2014-08-22 17:42:43 -0700201 boolean inRecents; // Actually in the recents list?
202 boolean isAvailable; // Is the activity available to be launched?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 boolean rootWasReset; // True if the intent at the root of the task had
204 // the FLAG_ACTIVITY_RESET_TASK_IF_NEEDED flag.
Dianne Hackborn13420f22014-07-18 15:43:56 -0700205 boolean autoRemoveRecents; // If true, we should automatically remove the task from
206 // recents when activity finishes
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700207 boolean askedCompatMode;// Have asked the user about compat mode for this task.
Dianne Hackbornd38aed82014-06-10 21:36:35 -0700208 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 -0800209
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700210 String stringName; // caching of toString() result.
Dianne Hackborn9da2d402012-03-15 13:43:08 -0700211 int userId; // user for which this task was created
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800212 boolean mUserSetupComplete; // The user set-up is complete as of the last time the task activity
213 // was changed.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800214
215 int numFullscreen; // Number of fullscreen activities.
216
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800217 int mResizeMode; // The resize mode of this task and its activities.
218 // Based on the {@link ActivityInfo#resizeMode} of the root activity.
Wale Ogunwale069bbd32017-02-03 07:58:14 -0800219 private boolean mSupportsPictureInPicture; // Whether or not this task and its activities
220 // support PiP. Based on the {@link ActivityInfo#FLAG_SUPPORTS_PICTURE_IN_PICTURE} flag
221 // of the root activity.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -0800222 boolean mTemporarilyUnresizable; // Separate flag from mResizeMode used to suppress resize
223 // changes on a temporary basis.
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700224 private int mLockTaskMode; // Which tasklock mode to launch this task in. One of
225 // ActivityManager.LOCK_TASK_LAUNCH_MODE_*
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700226 private boolean mPrivileged; // The root activity application of this task holds
227 // privileged permissions.
228
Craig Mautner15df08a2015-04-01 12:17:18 -0700229 /** Can't be put in lockTask mode. */
230 final static int LOCK_TASK_AUTH_DONT_LOCK = 0;
Benjamin Franz469dd582015-06-09 14:24:36 +0100231 /** Can enter app pinning with user approval. Can never start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700232 final static int LOCK_TASK_AUTH_PINNABLE = 1;
233 /** Starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing lockTask task. */
234 final static int LOCK_TASK_AUTH_LAUNCHABLE = 2;
Benjamin Franz469dd582015-06-09 14:24:36 +0100235 /** Can enter lockTask without user approval. Can start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700236 final static int LOCK_TASK_AUTH_WHITELISTED = 3;
Benjamin Franz469dd582015-06-09 14:24:36 +0100237 /** Priv-app that starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing
238 * lockTask task. */
239 final static int LOCK_TASK_AUTH_LAUNCHABLE_PRIV = 4;
Craig Mautner15df08a2015-04-01 12:17:18 -0700240 int mLockTaskAuth = LOCK_TASK_AUTH_PINNABLE;
241
242 int mLockTaskUid = -1; // The uid of the application that called startLockTask().
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800243
Winson Chung03a9bae2014-05-02 09:56:12 -0700244 // This represents the last resolved activity values for this task
245 // NOTE: This value needs to be persisted with each task
Craig Mautner648f69b2014-09-18 14:16:26 -0700246 TaskDescription lastTaskDescription = new TaskDescription();
Winson Chung03a9bae2014-05-02 09:56:12 -0700247
Craig Mautnerd2328952013-03-05 12:46:26 -0800248 /** List of all activities in the task arranged in history order */
Craig Mautner21d24a22014-04-23 11:45:37 -0700249 final ArrayList<ActivityRecord> mActivities;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800250
Andrii Kulian02b7a832016-10-06 23:11:56 -0700251 /** Current stack. Setter must always be used to update the value. */
252 private ActivityStack mStack;
Craig Mautnerd2328952013-03-05 12:46:26 -0800253
Craig Mautner2c1faed2013-07-23 12:56:02 -0700254 /** Takes on same set of values as ActivityRecord.mActivityType */
Craig Mautner21d24a22014-04-23 11:45:37 -0700255 int taskType;
Craig Mautner1602ec22013-05-12 10:24:27 -0700256
Craig Mautner21d24a22014-04-23 11:45:37 -0700257 /** Takes on same value as first root activity */
258 boolean isPersistable = false;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700259 int maxRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700260
261 /** Only used for persistable tasks, otherwise 0. The last time this task was moved. Used for
262 * determining the order when restoring. Sign indicates whether last task movement was to front
263 * (positive) or back (negative). Absolute value indicates time. */
264 long mLastTimeMoved = System.currentTimeMillis();
265
Craig Mautner84984fa2014-06-19 11:19:20 -0700266 /** Indication of what to run next when task exits. Use ActivityRecord types.
267 * ActivityRecord.APPLICATION_ACTIVITY_TYPE indicates to resume the task below this one in the
268 * task stack. */
269 private int mTaskToReturnTo = APPLICATION_ACTIVITY_TYPE;
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700270
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700271 /** If original intent did not allow relinquishing task identity, save that information */
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700272 private boolean mNeverRelinquishIdentity = true;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700273
Craig Mautner362449a2014-06-20 14:04:39 -0700274 // Used in the unique case where we are clearing the task in order to reuse it. In that case we
275 // do not want to delete the stack when the task goes empty.
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -0800276 private boolean mReuseTask = false;
Craig Mautner362449a2014-06-20 14:04:39 -0700277
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700278 private Bitmap mLastThumbnail; // Last thumbnail captured for this item.
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700279 private final File mLastThumbnailFile; // File containing last thumbnail.
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700280 private final String mFilename;
Winsonc809cbb2015-11-02 12:06:15 -0800281 private TaskThumbnailInfo mLastThumbnailInfo;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700282 CharSequence lastDescription; // Last description captured for this item.
283
Craig Mautnera228ae92014-07-09 05:44:55 -0700284 int mAffiliatedTaskId; // taskId of parent affiliation or self if no parent.
Winson Chungec396d62014-08-06 17:08:00 -0700285 int mAffiliatedTaskColor; // color of the parent task affiliation.
Craig Mautnera228ae92014-07-09 05:44:55 -0700286 TaskRecord mPrevAffiliate; // previous task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800287 int mPrevAffiliateTaskId = INVALID_TASK_ID; // previous id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700288 TaskRecord mNextAffiliate; // next task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800289 int mNextAffiliateTaskId = INVALID_TASK_ID; // next id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700290
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700291 // For relaunching the task from recents as though it was launched by the original launcher.
292 int mCallingUid;
293 String mCallingPackage;
294
Craig Mautner21d24a22014-04-23 11:45:37 -0700295 final ActivityManagerService mService;
296
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700297 // Whether or not this task covers the entire screen; by default tasks are fullscreen.
298 boolean mFullscreen = true;
299
300 // Bounds of the Task. null for fullscreen tasks.
301 Rect mBounds = null;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -0800302 private final Rect mTmpStableBounds = new Rect();
303 private final Rect mTmpNonDecorBounds = new Rect();
Wale Ogunwale9a08f822016-02-17 19:03:58 -0800304 private final Rect mTmpRect = new Rect();
Jorim Jaggi0a932142016-02-01 17:42:25 -0800305
Wale Ogunwale706ed792015-08-02 10:29:44 -0700306 // Last non-fullscreen bounds the task was launched in or resized to.
307 // The information is persisted and used to determine the appropriate stack to launch the
308 // task into on restore.
309 Rect mLastNonFullscreenBounds = null;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700310 // Minimal width and height of this task when it's resizeable. -1 means it should use the
311 // default minimal width/height.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700312 int mMinWidth;
313 int mMinHeight;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700314
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700315 // Ranking (from top) of this task among all visible tasks. (-1 means it's not visible)
316 // This number will be assigned when we evaluate OOM scores for all visible tasks.
317 int mLayerRank = -1;
318
Andrii Kulian1779e612016-10-12 21:58:25 -0700319 /** Helper object used for updating override configuration. */
320 private Configuration mTmpConfig = new Configuration();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700321
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800322 private TaskWindowContainerController mWindowContainerController;
323
Craig Mautner21d24a22014-04-23 11:45:37 -0700324 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Wale Ogunwale72919d22016-12-08 18:58:50 -0800325 IVoiceInteractionSession _voiceSession, IVoiceInteractor _voiceInteractor, int type) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700326 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700327 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
328 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800329 userId = UserHandle.getUserId(info.applicationInfo.uid);
330 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800331 mLastThumbnailInfo = new TaskThumbnailInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 taskId = _taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700333 mAffiliatedTaskId = _taskId;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700334 voiceSession = _voiceSession;
335 voiceInteractor = _voiceInteractor;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700336 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800337 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700338 mCallingUid = info.applicationInfo.uid;
339 mCallingPackage = info.packageName;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800340 taskType = type;
Martijn Coenend4a69702014-06-30 11:12:17 -0700341 setIntent(_intent, info);
Andrii Kulian2e751b82016-03-16 16:59:32 -0700342 setMinDimensions(info);
Winson730bf062016-03-31 18:04:56 -0700343 touchActiveTime();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700344 mService.mTaskChangeNotificationController.notifyTaskCreated(_taskId, realActivity);
Craig Mautner21d24a22014-04-23 11:45:37 -0700345 }
346
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700347 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Winsonc809cbb2015-11-02 12:06:15 -0800348 TaskDescription _taskDescription, TaskThumbnailInfo thumbnailInfo) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700349 mService = service;
350 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
351 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800352 userId = UserHandle.getUserId(info.applicationInfo.uid);
353 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800354 mLastThumbnailInfo = thumbnailInfo;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700355 taskId = _taskId;
356 mAffiliatedTaskId = _taskId;
357 voiceSession = null;
358 voiceInteractor = null;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700359 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800360 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700361 mCallingUid = info.applicationInfo.uid;
362 mCallingPackage = info.packageName;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700363 setIntent(_intent, info);
Andrii Kulian2e751b82016-03-16 16:59:32 -0700364 setMinDimensions(info);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700365
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700366 isPersistable = true;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700367 // Clamp to [1, max].
368 maxRecents = Math.min(Math.max(info.maxRecents, 1),
369 ActivityManager.getMaxAppRecentsLimitStatic());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700370
371 taskType = APPLICATION_ACTIVITY_TYPE;
372 mTaskToReturnTo = HOME_ACTIVITY_TYPE;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700373 lastTaskDescription = _taskDescription;
Winson730bf062016-03-31 18:04:56 -0700374 touchActiveTime();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700375 mService.mTaskChangeNotificationController.notifyTaskCreated(_taskId, realActivity);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700376 }
377
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800378 private TaskRecord(ActivityManagerService service, int _taskId, Intent _intent,
379 Intent _affinityIntent, String _affinity, String _rootAffinity,
380 ComponentName _realActivity, ComponentName _origActivity, boolean _rootWasReset,
381 boolean _autoRemoveRecents, boolean _askedCompatMode, int _taskType, int _userId,
382 int _effectiveUid, String _lastDescription, ArrayList<ActivityRecord> activities,
383 long _firstActiveTime, long _lastActiveTime, long lastTimeMoved,
384 boolean neverRelinquishIdentity, TaskDescription _lastTaskDescription,
Winsonc809cbb2015-11-02 12:06:15 -0800385 TaskThumbnailInfo lastThumbnailInfo, int taskAffiliation, int prevTaskId,
386 int nextTaskId, int taskAffiliationColor, int callingUid, String callingPackage,
Winson Chungd3395382016-12-13 11:49:09 -0800387 int resizeMode, boolean supportsPictureInPicture, boolean privileged,
388 boolean _realActivitySuspended, boolean userSetupComplete, int minWidth,
389 int minHeight) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700390 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700391 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
392 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800393 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(_userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800394 mLastThumbnailInfo = lastThumbnailInfo;
Craig Mautner21d24a22014-04-23 11:45:37 -0700395 taskId = _taskId;
396 intent = _intent;
397 affinityIntent = _affinityIntent;
398 affinity = _affinity;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800399 rootAffinity = _rootAffinity;
Craig Mautner21d24a22014-04-23 11:45:37 -0700400 voiceSession = null;
401 voiceInteractor = null;
402 realActivity = _realActivity;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800403 realActivitySuspended = _realActivitySuspended;
Craig Mautner21d24a22014-04-23 11:45:37 -0700404 origActivity = _origActivity;
405 rootWasReset = _rootWasReset;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700406 isAvailable = true;
Dianne Hackborn13420f22014-07-18 15:43:56 -0700407 autoRemoveRecents = _autoRemoveRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700408 askedCompatMode = _askedCompatMode;
409 taskType = _taskType;
Craig Mautner84984fa2014-06-19 11:19:20 -0700410 mTaskToReturnTo = HOME_ACTIVITY_TYPE;
Craig Mautner21d24a22014-04-23 11:45:37 -0700411 userId = _userId;
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800412 mUserSetupComplete = userSetupComplete;
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700413 effectiveUid = _effectiveUid;
Winson Chungffa2ec62014-07-03 15:54:42 -0700414 firstActiveTime = _firstActiveTime;
Winson Chungf1fbd772014-06-24 18:06:58 -0700415 lastActiveTime = _lastActiveTime;
Craig Mautner21d24a22014-04-23 11:45:37 -0700416 lastDescription = _lastDescription;
417 mActivities = activities;
418 mLastTimeMoved = lastTimeMoved;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700419 mNeverRelinquishIdentity = neverRelinquishIdentity;
Winson Chung2cb86c72014-06-25 12:03:30 -0700420 lastTaskDescription = _lastTaskDescription;
Craig Mautnera228ae92014-07-09 05:44:55 -0700421 mAffiliatedTaskId = taskAffiliation;
Winson Chungec396d62014-08-06 17:08:00 -0700422 mAffiliatedTaskColor = taskAffiliationColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700423 mPrevAffiliateTaskId = prevTaskId;
424 mNextAffiliateTaskId = nextTaskId;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700425 mCallingUid = callingUid;
426 mCallingPackage = callingPackage;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800427 mResizeMode = resizeMode;
Winson Chungd3395382016-12-13 11:49:09 -0800428 mSupportsPictureInPicture = supportsPictureInPicture;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700429 mPrivileged = privileged;
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700430 mMinWidth = minWidth;
431 mMinHeight = minHeight;
Yorke Leebd54c2a2016-10-25 13:49:23 -0700432 mService.mTaskChangeNotificationController.notifyTaskCreated(_taskId, realActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 }
434
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800435 TaskWindowContainerController getWindowContainerController() {
436 return mWindowContainerController;
437 }
438
439 void createWindowContainer(boolean onTop, boolean showForAllUsers) {
440 if (mWindowContainerController != null) {
441 throw new IllegalArgumentException("Window container=" + mWindowContainerController
442 + " already created for task=" + this);
443 }
444
445 final Rect bounds = updateOverrideConfigurationFromLaunchBounds();
446 final Configuration overrideConfig = getOverrideConfiguration();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800447 mWindowContainerController = new TaskWindowContainerController(taskId, this,
448 getStack().getWindowContainerController(), userId, bounds, overrideConfig,
Wale Ogunwale069bbd32017-02-03 07:58:14 -0800449 mResizeMode, mSupportsPictureInPicture, isHomeTask(), onTop, showForAllUsers,
450 lastTaskDescription);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800451 }
452
453 void removeWindowContainer() {
454 mService.mStackSupervisor.removeLockedTaskLocked(this);
455 mWindowContainerController.removeContainer();
456 if (!StackId.persistTaskBounds(getStackId())) {
457 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
458 // default configuration the next time it launches.
459 updateOverrideConfiguration(null);
460 }
461 mService.mTaskChangeNotificationController.notifyTaskRemoved(taskId);
462 mWindowContainerController = null;
463 }
464
Jorim Jaggifb9d78a2017-01-05 18:57:12 +0100465 @Override
466 public void onSnapshotChanged(TaskSnapshot snapshot) {
467 mService.mTaskChangeNotificationController.notifyTaskSnapshotChanged(taskId, snapshot);
468 }
469
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800470 void setResizeMode(int resizeMode) {
471 if (mResizeMode == resizeMode) {
472 return;
473 }
474 mResizeMode = resizeMode;
475 mWindowContainerController.setResizeable(resizeMode);
476 mService.mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
477 mService.mStackSupervisor.resumeFocusedStackTopActivityLocked();
478 }
479
480 void setTaskDockedResizing(boolean resizing) {
481 mWindowContainerController.setTaskDockedResizing(resizing);
482 }
483
Wale Ogunwale1666e312016-12-16 11:27:18 -0800484 // TODO: Consolidate this with the resize() method below.
485 @Override
486 public void requestResize(Rect bounds, int resizeMode) {
487 mService.resizeTask(taskId, bounds, resizeMode);
488 }
489
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800490 boolean resize(Rect bounds, int resizeMode, boolean preserveWindow, boolean deferResume) {
491 if (!isResizeable()) {
492 Slog.w(TAG, "resizeTask: task " + this + " not resizeable.");
493 return true;
494 }
495
496 // If this is a forced resize, let it go through even if the bounds is not changing,
497 // as we might need a relayout due to surface size change (to/from fullscreen).
498 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
499 if (Objects.equals(mBounds, bounds) && !forced) {
500 // Nothing to do here...
501 return true;
502 }
503 bounds = validateBounds(bounds);
504
505 if (mWindowContainerController == null) {
506 // Task doesn't exist in window manager yet (e.g. was restored from recents).
507 // All we can do for now is update the bounds so it can be used when the task is
508 // added to window manager.
509 updateOverrideConfiguration(bounds);
510 if (getStackId() != FREEFORM_WORKSPACE_STACK_ID) {
511 // re-restore the task so it can have the proper stack association.
512 mService.mStackSupervisor.restoreRecentTaskLocked(this,
513 FREEFORM_WORKSPACE_STACK_ID);
514 }
515 return true;
516 }
517
518 if (!canResizeToBounds(bounds)) {
519 throw new IllegalArgumentException("resizeTask: Can not resize task=" + this
520 + " to bounds=" + bounds + " resizeMode=" + mResizeMode);
521 }
522
523 // Do not move the task to another stack here.
524 // This method assumes that the task is already placed in the right stack.
525 // we do not mess with that decision and we only do the resize!
526
527 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + taskId);
528
529 final boolean updatedConfig = updateOverrideConfiguration(bounds);
530 // This variable holds information whether the configuration didn't change in a significant
531 // way and the activity was kept the way it was. If it's false, it means the activity had
532 // to be relaunched due to configuration change.
533 boolean kept = true;
534 if (updatedConfig) {
535 final ActivityRecord r = topRunningActivityLocked();
536 if (r != null) {
537 kept = r.ensureActivityConfigurationLocked(0 /* globalChanges */, preserveWindow);
538
539 if (!deferResume) {
540 // All other activities must be made visible with their correct configuration.
541 mService.mStackSupervisor.ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
542 if (!kept) {
543 mService.mStackSupervisor.resumeFocusedStackTopActivityLocked();
544 }
545 }
546 }
547 }
548 mWindowContainerController.resize(mBounds, getOverrideConfiguration(), kept, forced);
549
550 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
551 return kept;
552 }
553
554 // TODO: Investigate combining with the resize() method above.
555 void resizeWindowContainer() {
556 mWindowContainerController.resize(mBounds, getOverrideConfiguration(), false /* relayout */,
557 false /* forced */);
558 }
559
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800560 void getWindowContainerBounds(Rect bounds) {
561 mWindowContainerController.getBounds(bounds);
562 }
563
Winson Chung74666102017-02-22 17:49:24 -0800564 /**
565 * Convenience method to reparent a task to the top or bottom position of the stack.
566 */
567 boolean reparent(int preferredStackId, boolean toTop, @ReparentMoveStackMode int moveStackMode,
568 boolean animate, boolean deferResume, String reason) {
569 return reparent(preferredStackId, toTop ? MAX_VALUE : 0, moveStackMode, animate,
570 deferResume, reason);
571 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800572
Winson Chung74666102017-02-22 17:49:24 -0800573 /**
574 * Reparents the task into a preferred stack, creating it if necessary.
575 *
576 * @param preferredStackId the stack id of the target stack to move this task
577 * @param position the position to place this task in the new stack
578 * @param animate whether or not we should wait for the new window created as a part of the
579 * reparenting to be drawn and animated in
580 * @param moveStackMode whether or not to move the stack to the front always, only if it was
581 * previously focused & in front, or never
582 * @param deferResume whether or not to update the visibility of other tasks and stacks that may
583 * have changed as a result of this reparenting
584 * @param reason the caller of this reparenting
585 * @return
586 */
587 boolean reparent(int preferredStackId, int position, @ReparentMoveStackMode int moveStackMode,
588 boolean animate, boolean deferResume, String reason) {
589 final ActivityStackSupervisor supervisor = mService.mStackSupervisor;
590 final WindowManagerService windowManager = mService.mWindowManager;
591 final ActivityStack sourceStack = getStack();
592 final ActivityStack toStack = supervisor.getReparentTargetStack(this, preferredStackId,
593 position == MAX_VALUE);
594 if (toStack == sourceStack) {
595 return false;
596 }
597
598 final int sourceStackId = getStackId();
599 final int stackId = toStack.getStackId();
600 final ActivityRecord topActivity = getTopActivity();
601
602 final boolean mightReplaceWindow = StackId.replaceWindowsOnTaskMove(sourceStackId, stackId)
603 && topActivity != null;
604 if (mightReplaceWindow) {
605 // We are about to relaunch the activity because its configuration changed due to
606 // being maximized, i.e. size change. The activity will first remove the old window
607 // and then add a new one. This call will tell window manager about this, so it can
608 // preserve the old window until the new one is drawn. This prevents having a gap
609 // between the removal and addition, in which no window is visible. We also want the
610 // entrance of the new window to be properly animated.
611 // Note here we always set the replacing window first, as the flags might be needed
612 // during the relaunch. If we end up not doing any relaunch, we clear the flags later.
613 windowManager.setWillReplaceWindow(topActivity.appToken, animate);
614 }
615
616 windowManager.deferSurfaceLayout();
617 boolean kept = true;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800618 try {
Winson Chung74666102017-02-22 17:49:24 -0800619 final ActivityRecord r = topRunningActivityLocked();
620 final boolean wasFocused = supervisor.isFocusedStack(sourceStack)
621 && (topRunningActivityLocked() == r);
622 final boolean wasResumed = sourceStack.mResumedActivity == r;
623 final boolean wasPaused = sourceStack.mPausingActivity == r;
624
625 // In some cases the focused stack isn't the front stack. E.g. pinned stack.
626 // Whenever we are moving the top activity from the front stack we want to make sure to
627 // move the stack to the front.
628 final boolean wasFront = supervisor.isFrontStackOnDisplay(sourceStack)
629 && (sourceStack.topRunningActivityLocked() == r);
630
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800631 // Adjust the position for the new parent stack as needed.
Winson Chung74666102017-02-22 17:49:24 -0800632 position = toStack.getAdjustedPositionForTask(this, position, null /* starting */);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800633
634 // Must reparent first in window manager to avoid a situation where AM can delete the
635 // we are coming from in WM before we reparent because it became empty.
Winson Chung74666102017-02-22 17:49:24 -0800636 mWindowContainerController.reparent(toStack.getWindowContainerController(), position);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800637
Winson Chung74666102017-02-22 17:49:24 -0800638 // Reset the resumed activity on the previous stack
639 if (wasResumed) {
640 sourceStack.mResumedActivity = null;
641 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800642
Winson Chung74666102017-02-22 17:49:24 -0800643 // Reset the paused activity on the previous stack
644 if (wasPaused) {
645 sourceStack.mPausingActivity = null;
646 sourceStack.removeTimeoutsForActivityLocked(r);
647 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800648
Winson Chung74666102017-02-22 17:49:24 -0800649 // Move the task
650 sourceStack.removeTask(this, reason, REMOVE_TASK_MODE_MOVING);
651 toStack.addTask(this, position, reason);
652
653 // TODO: Ensure that this is actually necessary here
654 // Notify of picture-in-picture mode changes
655 supervisor.scheduleReportPictureInPictureModeChangedIfNeeded(this, sourceStack);
656
657 // TODO: Ensure that this is actually necessary here
658 // Notify the voice session if required
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800659 if (voiceSession != null) {
660 try {
661 voiceSession.taskStarted(intent, taskId);
662 } catch (RemoteException e) {
663 }
664 }
Winson Chung74666102017-02-22 17:49:24 -0800665
666 // If the task had focus before (or we're requested to move focus), move focus to the
667 // new stack by moving the stack to the front.
668 final boolean moveStackToFront = moveStackMode == REPARENT_MOVE_STACK_TO_FRONT
669 || (moveStackMode == REPARENT_KEEP_STACK_AT_FRONT && (wasFocused || wasFront));
670 toStack.moveToFrontAndResumeStateIfNeeded(r, moveStackToFront, wasResumed, wasPaused,
671 reason);
672 if (!animate) {
673 toStack.mNoAnimActivities.add(topActivity);
674 }
675
676 // We might trigger a configuration change. Save the current task bounds for freezing.
677 // TODO: Should this call be moved inside the resize method in WM?
678 toStack.prepareFreezingTaskBounds();
679
680 // Make sure the task has the appropriate bounds/size for the stack it is in.
681 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID
682 && !Objects.equals(mBounds, toStack.mBounds)) {
683 kept = resize(toStack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow,
684 deferResume);
685 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID) {
686 Rect bounds = getLaunchBounds();
687 if (bounds == null) {
688 toStack.layoutTaskInStack(this, null);
689 bounds = mBounds;
690 }
691 kept = resize(bounds, RESIZE_MODE_FORCED, !mightReplaceWindow, deferResume);
692 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Matthew Ng330757d2017-02-28 14:19:17 -0800693 if (stackId == DOCKED_STACK_ID && moveStackMode == REPARENT_KEEP_STACK_AT_FRONT) {
694 // Move recents to front so it is not behind home stack when going into docked
695 // mode
696 mService.mStackSupervisor.moveRecentsStackToFront(reason);
697 }
Winson Chung74666102017-02-22 17:49:24 -0800698 kept = resize(toStack.mBounds, RESIZE_MODE_SYSTEM, !mightReplaceWindow,
699 deferResume);
700 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800701 } finally {
Winson Chung74666102017-02-22 17:49:24 -0800702 windowManager.continueSurfaceLayout();
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800703 }
Winson Chung74666102017-02-22 17:49:24 -0800704
705 if (mightReplaceWindow) {
706 // If we didn't actual do a relaunch (indicated by kept==true meaning we kept the old
707 // window), we need to clear the replace window settings. Otherwise, we schedule a
708 // timeout to remove the old window if the replacing window is not coming in time.
709 windowManager.scheduleClearWillReplaceWindows(topActivity.appToken, !kept);
710 }
711
712 if (!deferResume) {
713 // The task might have already been running and its visibility needs to be synchronized
714 // with the visibility of the stack / windows.
715 supervisor.ensureActivitiesVisibleLocked(null, 0, !mightReplaceWindow);
716 supervisor.resumeFocusedStackTopActivityLocked();
717 }
718
719 supervisor.handleNonResizableTaskIfNeeded(this, preferredStackId, stackId);
720
Matthew Ng330757d2017-02-28 14:19:17 -0800721 boolean successful = (preferredStackId == stackId);
722 if (successful && stackId == DOCKED_STACK_ID) {
723 // If task moved to docked stack - show recents if needed.
724 mService.mWindowManager.showRecentApps(false /* fromHome */);
725 }
726 return successful;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800727 }
728
729 void cancelWindowTransition() {
730 mWindowContainerController.cancelWindowTransition();
731 }
732
733 void cancelThumbnailTransition() {
734 mWindowContainerController.cancelThumbnailTransition();
735 }
736
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100737 /**
738 * DO NOT HOLD THE ACTIVITY MANAGER LOCK WHEN CALLING THIS METHOD!
739 */
740 TaskSnapshot getSnapshot() {
741
742 // TODO: Move this to {@link TaskWindowContainerController} once recent tasks are more
743 // synchronized between AM and WM.
744 return mService.mWindowManager.getTaskSnapshot(taskId, userId);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800745 }
746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 void touchActiveTime() {
Winson Chung36f3f032016-09-08 23:29:43 +0000748 lastActiveTime = System.currentTimeMillis();
Winson Chungffa2ec62014-07-03 15:54:42 -0700749 if (firstActiveTime == 0) {
750 firstActiveTime = lastActiveTime;
751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 long getInactiveDuration() {
Winson Chung36f3f032016-09-08 23:29:43 +0000755 return System.currentTimeMillis() - lastActiveTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700757
Winson Chungfee26772014-08-05 12:21:52 -0700758 /** Sets the original intent, and the calling uid and package. */
759 void setIntent(ActivityRecord r) {
Winson Chungfee26772014-08-05 12:21:52 -0700760 mCallingUid = r.launchedFromUid;
761 mCallingPackage = r.launchedFromPackage;
Craig Mautner15df08a2015-04-01 12:17:18 -0700762 setIntent(r.intent, r.info);
Winson Chungfee26772014-08-05 12:21:52 -0700763 }
764
765 /** Sets the original intent, _without_ updating the calling uid or package. */
766 private void setIntent(Intent _intent, ActivityInfo info) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700767 if (intent == null) {
768 mNeverRelinquishIdentity =
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700769 (info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700770 } else if (mNeverRelinquishIdentity) {
771 return;
772 }
773
774 affinity = info.taskAffinity;
Dianne Hackborn79228822014-09-16 11:11:23 -0700775 if (intent == null) {
776 // If this task already has an intent associated with it, don't set the root
777 // affinity -- we don't want it changing after initially set, but the initially
778 // set value may be null.
779 rootAffinity = affinity;
780 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700781 effectiveUid = info.applicationInfo.uid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700782 stringName = null;
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 if (info.targetActivity == null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800785 if (_intent != null) {
786 // If this Intent has a selector, we want to clear it for the
787 // recent task since it is not relevant if the user later wants
788 // to re-launch the app.
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700789 if (_intent.getSelector() != null || _intent.getSourceBounds() != null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800790 _intent = new Intent(_intent);
791 _intent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700792 _intent.setSourceBounds(null);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800793 }
794 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700795 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Setting Intent of " + this + " to " + _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 intent = _intent;
797 realActivity = _intent != null ? _intent.getComponent() : null;
798 origActivity = null;
799 } else {
800 ComponentName targetComponent = new ComponentName(
801 info.packageName, info.targetActivity);
802 if (_intent != null) {
803 Intent targetIntent = new Intent(_intent);
804 targetIntent.setComponent(targetComponent);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800805 targetIntent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700806 targetIntent.setSourceBounds(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700807 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700808 "Setting Intent of " + this + " to target " + targetIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 intent = targetIntent;
810 realActivity = targetComponent;
811 origActivity = _intent.getComponent();
812 } else {
813 intent = null;
814 realActivity = targetComponent;
815 origActivity = new ComponentName(info.packageName, info.name);
816 }
817 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700818
Craig Mautner47b20ba2014-09-17 17:23:44 -0700819 final int intentFlags = intent == null ? 0 : intent.getFlags();
820 if ((intentFlags & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 // Once we are set to an Intent with this flag, we count this
822 // task as having a true root activity.
823 rootWasReset = true;
824 }
Dianne Hackborn09233282014-04-30 11:33:59 -0700825 userId = UserHandle.getUserId(info.applicationInfo.uid);
Winson Chung36f3f032016-09-08 23:29:43 +0000826 mUserSetupComplete = Settings.Secure.getIntForUser(mService.mContext.getContentResolver(),
827 USER_SETUP_COMPLETE, 0, userId) != 0;
Craig Mautner41db4a72014-05-07 17:20:56 -0700828 if ((info.flags & ActivityInfo.FLAG_AUTO_REMOVE_FROM_RECENTS) != 0) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700829 // If the activity itself has requested auto-remove, then just always do it.
830 autoRemoveRecents = true;
Wale Ogunwale843bfb92015-03-27 11:06:48 -0700831 } else if ((intentFlags & (FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_RETAIN_IN_RECENTS))
832 == FLAG_ACTIVITY_NEW_DOCUMENT) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700833 // If the caller has not asked for the document to be retained, then we may
834 // want to turn on auto-remove, depending on whether the target has set its
835 // own document launch mode.
836 if (info.documentLaunchMode != ActivityInfo.DOCUMENT_LAUNCH_NONE) {
837 autoRemoveRecents = false;
838 } else {
839 autoRemoveRecents = true;
840 }
841 } else {
842 autoRemoveRecents = false;
Craig Mautner41db4a72014-05-07 17:20:56 -0700843 }
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800844 mResizeMode = info.resizeMode;
Winson Chungd3395382016-12-13 11:49:09 -0800845 mSupportsPictureInPicture = info.supportsPictureInPicture();
Craig Mautner15df08a2015-04-01 12:17:18 -0700846 mLockTaskMode = info.lockTaskLaunchMode;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700847 mPrivileged = (info.applicationInfo.privateFlags & PRIVATE_FLAG_PRIVILEGED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -0700848 setLockTaskAuth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800850
Andrii Kulian2e751b82016-03-16 16:59:32 -0700851 /** Sets the original minimal width and height. */
852 private void setMinDimensions(ActivityInfo info) {
853 if (info != null && info.windowLayout != null) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700854 mMinWidth = info.windowLayout.minWidth;
855 mMinHeight = info.windowLayout.minHeight;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700856 } else {
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700857 mMinWidth = INVALID_MIN_SIZE;
858 mMinHeight = INVALID_MIN_SIZE;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700859 }
860 }
861
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800862 /**
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700863 * Return true if the input activity has the same intent filter as the intent this task
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800864 * record is based on (normally the root activity intent).
865 */
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700866 boolean isSameIntentFilter(ActivityRecord r) {
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800867 final Intent intent = new Intent(r.intent);
868 // Correct the activity intent for aliasing. The task record intent will always be based on
869 // the real activity that will be launched not the alias, so we need to use an intent with
870 // the component name pointing to the real activity not the alias in the activity record.
871 intent.setComponent(r.realActivity);
872 return this.intent.filterEquals(intent);
873 }
874
Craig Mautner84984fa2014-06-19 11:19:20 -0700875 void setTaskToReturnTo(int taskToReturnTo) {
Wale Ogunwale673cbd22016-01-30 18:30:55 -0800876 mTaskToReturnTo = (taskToReturnTo == RECENTS_ACTIVITY_TYPE)
877 ? HOME_ACTIVITY_TYPE : taskToReturnTo;
Craig Mautner84984fa2014-06-19 11:19:20 -0700878 }
879
Winson Chung83471632016-12-13 11:02:12 -0800880 void setTaskToReturnTo(ActivityRecord source) {
881 if (source.isRecentsActivity()) {
882 setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
883 } else if (source.isAssistantActivity()) {
884 setTaskToReturnTo(ASSISTANT_ACTIVITY_TYPE);
885 }
886 }
887
Craig Mautner84984fa2014-06-19 11:19:20 -0700888 int getTaskToReturnTo() {
889 return mTaskToReturnTo;
890 }
891
Craig Mautnera228ae92014-07-09 05:44:55 -0700892 void setPrevAffiliate(TaskRecord prevAffiliate) {
893 mPrevAffiliate = prevAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800894 mPrevAffiliateTaskId = prevAffiliate == null ? INVALID_TASK_ID : prevAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700895 }
896
897 void setNextAffiliate(TaskRecord nextAffiliate) {
898 mNextAffiliate = nextAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800899 mNextAffiliateTaskId = nextAffiliate == null ? INVALID_TASK_ID : nextAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700900 }
901
Andrii Kulian02b7a832016-10-06 23:11:56 -0700902 ActivityStack getStack() {
903 return mStack;
904 }
905
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800906 /**
907 * Must be used for setting parent stack because it performs configuration updates.
908 * Must be called after adding task as a child to the stack.
909 */
Andrii Kulian02b7a832016-10-06 23:11:56 -0700910 void setStack(ActivityStack stack) {
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800911 if (stack != null && !stack.isInStackLocked(this)) {
912 throw new IllegalStateException("Task must be added as a Stack child first.");
913 }
Andrii Kulian02b7a832016-10-06 23:11:56 -0700914 mStack = stack;
Andrii Kulian1779e612016-10-12 21:58:25 -0700915 onParentChanged();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700916 }
917
918 /**
919 * @return Id of current stack, {@link INVALID_STACK_ID} if no stack is set.
920 */
921 int getStackId() {
922 return mStack != null ? mStack.mStackId : INVALID_STACK_ID;
923 }
924
Andrii Kulian1779e612016-10-12 21:58:25 -0700925 @Override
926 protected int getChildCount() {
927 return 0;
928 }
929
930 @Override
931 protected ConfigurationContainer getChildAt(int index) {
932 return null;
933 }
934
935 @Override
936 protected ConfigurationContainer getParent() {
937 return mStack;
938 }
939
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800940 @Override
941 void onParentChanged() {
942 super.onParentChanged();
943 mService.mStackSupervisor.updateUIDsPresentOnDisplay();
944 }
945
Craig Mautnera228ae92014-07-09 05:44:55 -0700946 // Close up recents linked list.
947 void closeRecentsChain() {
948 if (mPrevAffiliate != null) {
949 mPrevAffiliate.setNextAffiliate(mNextAffiliate);
950 }
951 if (mNextAffiliate != null) {
952 mNextAffiliate.setPrevAffiliate(mPrevAffiliate);
953 }
954 setPrevAffiliate(null);
955 setNextAffiliate(null);
956 }
957
Winson Chung740c3ac2014-11-12 16:14:38 -0800958 void removedFromRecents() {
Dianne Hackborn852975d2014-08-22 17:42:43 -0700959 disposeThumbnail();
960 closeRecentsChain();
961 if (inRecents) {
962 inRecents = false;
Winson Chung740c3ac2014-11-12 16:14:38 -0800963 mService.notifyTaskPersisterLocked(this, false);
Dianne Hackborn852975d2014-08-22 17:42:43 -0700964 }
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100965
966 // TODO: Use window container controller once tasks are better synced between AM and WM
967 mService.mWindowManager.notifyTaskRemovedFromRecents(taskId, userId);
Dianne Hackborn852975d2014-08-22 17:42:43 -0700968 }
969
Craig Mautnera228ae92014-07-09 05:44:55 -0700970 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
971 closeRecentsChain();
972 mAffiliatedTaskId = taskToAffiliateWith.mAffiliatedTaskId;
Winson Chungec396d62014-08-06 17:08:00 -0700973 mAffiliatedTaskColor = taskToAffiliateWith.mAffiliatedTaskColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700974 // Find the end
975 while (taskToAffiliateWith.mNextAffiliate != null) {
976 final TaskRecord nextRecents = taskToAffiliateWith.mNextAffiliate;
977 if (nextRecents.mAffiliatedTaskId != mAffiliatedTaskId) {
978 Slog.e(TAG, "setTaskToAffiliateWith: nextRecents=" + nextRecents + " affilTaskId="
979 + nextRecents.mAffiliatedTaskId + " should be " + mAffiliatedTaskId);
980 if (nextRecents.mPrevAffiliate == taskToAffiliateWith) {
981 nextRecents.setPrevAffiliate(null);
982 }
983 taskToAffiliateWith.setNextAffiliate(null);
984 break;
985 }
986 taskToAffiliateWith = nextRecents;
987 }
988 taskToAffiliateWith.setNextAffiliate(this);
989 setPrevAffiliate(taskToAffiliateWith);
990 setNextAffiliate(null);
991 }
992
Winson Chung096f36b2014-08-20 15:39:01 -0700993 /**
Winsonc809cbb2015-11-02 12:06:15 -0800994 * Sets the last thumbnail with the current task bounds and the system orientation.
Winson Chung096f36b2014-08-20 15:39:01 -0700995 * @return whether the thumbnail was set
996 */
Winsonc809cbb2015-11-02 12:06:15 -0800997 boolean setLastThumbnailLocked(Bitmap thumbnail) {
Winsonc809cbb2015-11-02 12:06:15 -0800998 int taskWidth = 0;
999 int taskHeight = 0;
1000 if (mBounds != null) {
1001 // Non-fullscreen tasks
1002 taskWidth = mBounds.width();
1003 taskHeight = mBounds.height();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001004 } else if (mStack != null) {
Winsonc809cbb2015-11-02 12:06:15 -08001005 // Fullscreen tasks
1006 final Point displaySize = new Point();
Andrii Kulian02b7a832016-10-06 23:11:56 -07001007 mStack.getDisplaySize(displaySize);
Winsonc809cbb2015-11-02 12:06:15 -08001008 taskWidth = displaySize.x;
1009 taskHeight = displaySize.y;
1010 } else {
1011 Slog.e(TAG, "setLastThumbnailLocked() called on Task without stack");
1012 }
Andrii Kulian1779e612016-10-12 21:58:25 -07001013 // We need to provide the current orientation of the display on which this task resides,
1014 // not the orientation of the task.
1015 final int orientation =
1016 getStack().mActivityContainer.mActivityDisplay.getConfiguration().orientation;
1017 return setLastThumbnailLocked(thumbnail, taskWidth, taskHeight, orientation);
Winsonc809cbb2015-11-02 12:06:15 -08001018 }
1019
1020 /**
1021 * Sets the last thumbnail with the current task bounds.
1022 * @return whether the thumbnail was set
1023 */
1024 private boolean setLastThumbnailLocked(Bitmap thumbnail, int taskWidth, int taskHeight,
1025 int screenOrientation) {
Winson Chung096f36b2014-08-20 15:39:01 -07001026 if (mLastThumbnail != thumbnail) {
1027 mLastThumbnail = thumbnail;
Winsonc809cbb2015-11-02 12:06:15 -08001028 mLastThumbnailInfo.taskWidth = taskWidth;
1029 mLastThumbnailInfo.taskHeight = taskHeight;
1030 mLastThumbnailInfo.screenOrientation = screenOrientation;
Winson Chung096f36b2014-08-20 15:39:01 -07001031 if (thumbnail == null) {
1032 if (mLastThumbnailFile != null) {
1033 mLastThumbnailFile.delete();
1034 }
1035 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08001036 mService.mRecentTasks.saveImage(thumbnail, mLastThumbnailFile.getAbsolutePath());
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001037 }
Winson Chung096f36b2014-08-20 15:39:01 -07001038 return true;
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001039 }
Winson Chung096f36b2014-08-20 15:39:01 -07001040 return false;
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001041 }
1042
1043 void getLastThumbnail(TaskThumbnail thumbs) {
1044 thumbs.mainThumbnail = mLastThumbnail;
Winsonc809cbb2015-11-02 12:06:15 -08001045 thumbs.thumbnailInfo = mLastThumbnailInfo;
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001046 thumbs.thumbnailFileDescriptor = null;
Craig Mautnerf4f8bb72014-07-29 10:41:40 -07001047 if (mLastThumbnail == null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08001048 thumbs.mainThumbnail = mService.mRecentTasks.getImageFromWriteQueue(
Suprabh Shukla23593142015-11-03 17:31:15 -08001049 mLastThumbnailFile.getAbsolutePath());
Craig Mautnerf4f8bb72014-07-29 10:41:40 -07001050 }
Winson Chung096f36b2014-08-20 15:39:01 -07001051 // Only load the thumbnail file if we don't have a thumbnail
1052 if (thumbs.mainThumbnail == null && mLastThumbnailFile.exists()) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001053 try {
1054 thumbs.thumbnailFileDescriptor = ParcelFileDescriptor.open(mLastThumbnailFile,
1055 ParcelFileDescriptor.MODE_READ_ONLY);
1056 } catch (IOException e) {
Dianne Hackborn9844d292013-10-04 16:44:22 -07001057 }
1058 }
1059 }
1060
Winsonc809cbb2015-11-02 12:06:15 -08001061 /**
1062 * Removes in-memory thumbnail data when the max number of in-memory task thumbnails is reached.
1063 */
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001064 void freeLastThumbnail() {
1065 mLastThumbnail = null;
1066 }
1067
Winsonc809cbb2015-11-02 12:06:15 -08001068 /**
1069 * Removes all associated thumbnail data when a task is removed or pruned from recents.
1070 */
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001071 void disposeThumbnail() {
Winsonc6a2da02015-11-11 18:11:59 -08001072 mLastThumbnailInfo.reset();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001073 mLastThumbnail = null;
1074 lastDescription = null;
1075 }
1076
Winson Chung1147c402014-05-14 11:05:00 -07001077 /** Returns the intent for the root activity for this task */
1078 Intent getBaseIntent() {
1079 return intent != null ? intent : affinityIntent;
1080 }
1081
Winson Chung3b3f4642014-04-22 10:08:18 -07001082 /** Returns the first non-finishing activity from the root. */
1083 ActivityRecord getRootActivity() {
1084 for (int i = 0; i < mActivities.size(); i++) {
1085 final ActivityRecord r = mActivities.get(i);
1086 if (r.finishing) {
1087 continue;
1088 }
1089 return r;
1090 }
1091 return null;
1092 }
1093
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001094 ActivityRecord getTopActivity() {
1095 for (int i = mActivities.size() - 1; i >= 0; --i) {
1096 final ActivityRecord r = mActivities.get(i);
1097 if (r.finishing) {
1098 continue;
1099 }
1100 return r;
1101 }
1102 return null;
1103 }
1104
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001105 ActivityRecord topRunningActivityLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001106 if (mStack != null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001107 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1108 ActivityRecord r = mActivities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -07001109 if (!r.finishing && r.okToShowLocked()) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001110 return r;
1111 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07001112 }
1113 }
1114 return null;
1115 }
1116
Wale Ogunwale3b232392016-05-13 15:37:13 -07001117 ActivityRecord topRunningActivityWithStartingWindowLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001118 if (mStack != null) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07001119 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1120 ActivityRecord r = mActivities.get(activityNdx);
1121 if (r.mStartingWindowState != STARTING_WINDOW_SHOWN
Chong Zhang87761972016-08-22 13:53:24 -07001122 || r.finishing || !r.okToShowLocked()) {
Wale Ogunwale3b232392016-05-13 15:37:13 -07001123 continue;
1124 }
1125 return r;
1126 }
1127 }
1128 return null;
1129 }
1130
Chong Zhang87761972016-08-22 13:53:24 -07001131 boolean okToShowLocked() {
1132 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
1133 // okay to show the activity when locked.
1134 return mService.mStackSupervisor.isCurrentProfileLocked(userId)
1135 || topRunningActivityLocked() != null;
1136 }
1137
Craig Mautner3b475fe2013-12-16 15:58:31 -08001138 /** Call after activity movement or finish to make sure that frontOfTask is set correctly */
Bryce Leed71317c2017-02-07 14:27:22 -08001139 final void setFrontOfTask() {
1140 boolean foundFront = false;
Craig Mautner3b475fe2013-12-16 15:58:31 -08001141 final int numActivities = mActivities.size();
Craig Mautner704e40b2013-12-18 16:43:51 -08001142 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
Craig Mautner3b475fe2013-12-16 15:58:31 -08001143 final ActivityRecord r = mActivities.get(activityNdx);
1144 if (foundFront || r.finishing) {
1145 r.frontOfTask = false;
1146 } else {
1147 r.frontOfTask = true;
1148 // Set frontOfTask false for every following activity.
1149 foundFront = true;
1150 }
1151 }
Craig Mautner9587ee02014-06-23 15:00:10 +00001152 if (!foundFront && numActivities > 0) {
1153 // All activities of this task are finishing. As we ought to have a frontOfTask
1154 // activity, make the bottom activity front.
1155 mActivities.get(0).frontOfTask = true;
1156 }
Craig Mautner3b475fe2013-12-16 15:58:31 -08001157 }
1158
Craig Mautnerde4ef022013-04-07 19:01:33 -07001159 /**
Craig Mautner3b475fe2013-12-16 15:58:31 -08001160 * Reorder the history stack so that the passed activity is brought to the front.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001161 */
1162 final void moveActivityToFrontLocked(ActivityRecord newTop) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001163 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
1164 "Removing and adding activity " + newTop
1165 + " to stack at top callers=" + Debug.getCallers(4));
Craig Mautnerde4ef022013-04-07 19:01:33 -07001166
Craig Mautnerde4ef022013-04-07 19:01:33 -07001167 mActivities.remove(newTop);
1168 mActivities.add(newTop);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001169 updateEffectiveIntent();
Craig Mautner3b475fe2013-12-16 15:58:31 -08001170
Bryce Leed71317c2017-02-07 14:27:22 -08001171 setFrontOfTask();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001172 }
1173
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001174 void addActivityAtBottom(ActivityRecord r) {
Craig Mautner77878772013-03-04 19:46:24 -08001175 addActivityAtIndex(0, r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001176 }
1177
1178 void addActivityToTop(ActivityRecord r) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001179 addActivityAtIndex(mActivities.size(), r);
1180 }
1181
Winson Chung30480042017-01-26 10:55:34 -08001182 /**
1183 * Adds an activity {@param r} at the given {@param index}. The activity {@param r} must either
1184 * be in the current task or unparented to any task.
1185 */
Craig Mautner1602ec22013-05-12 10:24:27 -07001186 void addActivityAtIndex(int index, ActivityRecord r) {
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001187 if (r.task != null && r.task != this) {
1188 throw new IllegalArgumentException("Can not add r=" + " to task=" + this
1189 + " current parent=" + r.task);
1190 }
1191 r.task = this;
1192
Craig Mautner6170f732013-04-02 13:05:23 -07001193 // 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 -08001194 if (!mActivities.remove(r) && r.fullscreen) {
1195 // Was not previously in list.
1196 numFullscreen++;
1197 }
Craig Mautner2c1faed2013-07-23 12:56:02 -07001198 // Only set this based on the first activity
1199 if (mActivities.isEmpty()) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001200 taskType = r.mActivityType;
1201 isPersistable = r.isPersistable();
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001202 mCallingUid = r.launchedFromUid;
1203 mCallingPackage = r.launchedFromPackage;
Dianne Hackborn852975d2014-08-22 17:42:43 -07001204 // Clamp to [1, max].
1205 maxRecents = Math.min(Math.max(r.info.maxRecents, 1),
1206 ActivityManager.getMaxAppRecentsLimitStatic());
Craig Mautner2c1faed2013-07-23 12:56:02 -07001207 } else {
1208 // Otherwise make all added activities match this one.
Craig Mautner21d24a22014-04-23 11:45:37 -07001209 r.mActivityType = taskType;
Craig Mautner78733002013-06-10 13:54:49 -07001210 }
Wale Ogunwale3b232392016-05-13 15:37:13 -07001211
1212 final int size = mActivities.size();
1213
1214 if (index == size && size > 0) {
1215 final ActivityRecord top = mActivities.get(size - 1);
1216 if (top.mTaskOverlay) {
1217 // Place below the task overlay activity since the overlay activity should always
1218 // be on top.
1219 index--;
1220 }
1221 }
1222
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001223 index = Math.min(size, index);
Craig Mautner77878772013-03-04 19:46:24 -08001224 mActivities.add(index, r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001225 updateEffectiveIntent();
Craig Mautner21d24a22014-04-23 11:45:37 -07001226 if (r.isPersistable()) {
1227 mService.notifyTaskPersisterLocked(this, false);
1228 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001229
1230 // Sync. with window manager
1231 updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001232 final AppWindowContainerController appController = r.getWindowContainerController();
1233 if (appController != null) {
1234 // Only attempt to move in WM if the child has a controller. It is possible we haven't
1235 // created controller for the activity we are starting yet.
1236 mWindowContainerController.positionChildAt(appController, index);
1237 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001238 r.onOverrideConfigurationSent();
Craig Mautner77878772013-03-04 19:46:24 -08001239 }
1240
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001241 /**
1242 * @return true if this was the last activity in the task
1243 */
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001244 boolean removeActivity(ActivityRecord r) {
Bryce Leeaa5e8c32017-03-01 16:01:06 -08001245 if (r.task != this) {
1246 throw new IllegalArgumentException(
1247 "Activity=" + r + " does not belong to task=" + this);
1248 }
1249
1250 r.task = null;
1251
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001252 if (mActivities.remove(r) && r.fullscreen) {
1253 // Was previously in list.
1254 numFullscreen--;
1255 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001256 if (r.isPersistable()) {
1257 mService.notifyTaskPersisterLocked(this, false);
1258 }
Wale Ogunwale89182d52016-03-11 10:38:36 -08001259
Andrii Kulian02b7a832016-10-06 23:11:56 -07001260 if (getStackId() == PINNED_STACK_ID) {
Wale Ogunwale89182d52016-03-11 10:38:36 -08001261 // We normally notify listeners of task stack changes on pause, however pinned stack
1262 // activities are normally in the paused state so no notification will be sent there
1263 // before the activity is removed. We send it here so instead.
Yorke Leebd54c2a2016-10-25 13:49:23 -07001264 mService.mTaskChangeNotificationController.notifyTaskStackChanged();
Wale Ogunwale89182d52016-03-11 10:38:36 -08001265 }
1266
Craig Mautner41326202014-06-20 14:38:21 -07001267 if (mActivities.isEmpty()) {
Craig Mautner5afcd4d2014-06-21 18:11:33 -07001268 return !mReuseTask;
Craig Mautner41326202014-06-20 14:38:21 -07001269 }
1270 updateEffectiveIntent();
1271 return false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001272 }
1273
Craig Mautner41db4a72014-05-07 17:20:56 -07001274 boolean autoRemoveFromRecents() {
Dianne Hackbornd38aed82014-06-10 21:36:35 -07001275 // We will automatically remove the task either if it has explicitly asked for
1276 // this, or it is empty and has never contained an activity that got shown to
1277 // the user.
Dianne Hackborn13420f22014-07-18 15:43:56 -07001278 return autoRemoveRecents || (mActivities.isEmpty() && !hasBeenVisible);
Craig Mautner41db4a72014-05-07 17:20:56 -07001279 }
1280
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001281 /**
1282 * Completely remove all activities associated with an existing
1283 * task starting at a specified index.
1284 */
1285 final void performClearTaskAtIndexLocked(int activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001286 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001287 for ( ; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001288 final ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001289 if (r.finishing) {
1290 continue;
1291 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001292 if (mStack == null) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001293 // Task was restored from persistent storage.
1294 r.takeFromHistory();
1295 mActivities.remove(activityNdx);
1296 --activityNdx;
1297 --numActivities;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001298 } else if (mStack.finishActivityLocked(
Todd Kennedy539db512014-12-15 09:57:55 -08001299 r, Activity.RESULT_CANCELED, null, "clear-task-index", false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001300 --activityNdx;
1301 --numActivities;
1302 }
1303 }
1304 }
1305
1306 /**
1307 * Completely remove all activities associated with an existing task.
1308 */
1309 final void performClearTaskLocked() {
Craig Mautner362449a2014-06-20 14:04:39 -07001310 mReuseTask = true;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001311 performClearTaskAtIndexLocked(0);
Craig Mautner362449a2014-06-20 14:04:39 -07001312 mReuseTask = false;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001313 }
1314
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -08001315 ActivityRecord performClearTaskForReuseLocked(ActivityRecord newR, int launchFlags) {
1316 mReuseTask = true;
1317 final ActivityRecord result = performClearTaskLocked(newR, launchFlags);
1318 mReuseTask = false;
1319 return result;
1320 }
1321
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001322 /**
1323 * Perform clear operation as requested by
1324 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
1325 * stack to the given task, then look for
1326 * an instance of that activity in the stack and, if found, finish all
1327 * activities on top of it and return the instance.
1328 *
1329 * @param newR Description of the new activity being started.
1330 * @return Returns the old activity that should be continued to be used,
1331 * or null if none was found.
1332 */
1333 final ActivityRecord performClearTaskLocked(ActivityRecord newR, int launchFlags) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001334 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001335 for (int activityNdx = numActivities - 1; activityNdx >= 0; --activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001336 ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001337 if (r.finishing) {
1338 continue;
1339 }
1340 if (r.realActivity.equals(newR.realActivity)) {
1341 // Here it is! Now finish everything in front...
Craig Mautner1602ec22013-05-12 10:24:27 -07001342 final ActivityRecord ret = r;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001343
1344 for (++activityNdx; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -07001345 r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001346 if (r.finishing) {
1347 continue;
1348 }
1349 ActivityOptions opts = r.takeOptionsLocked();
1350 if (opts != null) {
1351 ret.updateOptionsLocked(opts);
1352 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001353 if (mStack != null && mStack.finishActivityLocked(
Todd Kennedy539db512014-12-15 09:57:55 -08001354 r, Activity.RESULT_CANCELED, null, "clear-task-stack", false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001355 --activityNdx;
1356 --numActivities;
1357 }
1358 }
1359
1360 // Finally, if this is a normal launch mode (that is, not
1361 // expecting onNewIntent()), then we will finish the current
1362 // instance of the activity so a new fresh one can be started.
1363 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
Daichi Hirono15a02992016-04-27 18:47:01 +09001364 && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0
1365 && !ActivityStarter.isDocumentLaunchesIntoExisting(launchFlags)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001366 if (!ret.finishing) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001367 if (mStack != null) {
1368 mStack.finishActivityLocked(
Wale Ogunwale7d701172015-03-11 15:36:30 -07001369 ret, Activity.RESULT_CANCELED, null, "clear-task-top", false);
1370 }
Craig Mautnerb0f7dc72013-04-01 16:34:45 -07001371 return null;
1372 }
1373 }
1374
1375 return ret;
1376 }
1377 }
1378
1379 return null;
1380 }
1381
Andrii Kulian21713ac2016-10-12 22:05:05 -07001382 TaskThumbnail getTaskThumbnailLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001383 if (mStack != null) {
1384 final ActivityRecord resumedActivity = mStack.mResumedActivity;
Craig Mautner21d24a22014-04-23 11:45:37 -07001385 if (resumedActivity != null && resumedActivity.task == this) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001386 final Bitmap thumbnail = resumedActivity.screenshotActivityLocked();
Winsonc809cbb2015-11-02 12:06:15 -08001387 setLastThumbnailLocked(thumbnail);
Craig Mautner21d24a22014-04-23 11:45:37 -07001388 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -07001389 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001390 final TaskThumbnail taskThumbnail = new TaskThumbnail();
1391 getLastThumbnail(taskThumbnail);
1392 return taskThumbnail;
Craig Mautner9db9a0b2013-04-29 17:05:56 -07001393 }
1394
Andrii Kulian21713ac2016-10-12 22:05:05 -07001395 void removeTaskActivitiesLocked() {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001396 // Just remove the entire task.
1397 performClearTaskAtIndexLocked(0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07001398 }
1399
Craig Mautner432f64e2015-05-20 14:59:57 -07001400 String lockTaskAuthToString() {
1401 switch (mLockTaskAuth) {
1402 case LOCK_TASK_AUTH_DONT_LOCK: return "LOCK_TASK_AUTH_DONT_LOCK";
1403 case LOCK_TASK_AUTH_PINNABLE: return "LOCK_TASK_AUTH_PINNABLE";
1404 case LOCK_TASK_AUTH_LAUNCHABLE: return "LOCK_TASK_AUTH_LAUNCHABLE";
1405 case LOCK_TASK_AUTH_WHITELISTED: return "LOCK_TASK_AUTH_WHITELISTED";
Benjamin Franz469dd582015-06-09 14:24:36 +01001406 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV: return "LOCK_TASK_AUTH_LAUNCHABLE_PRIV";
Craig Mautner432f64e2015-05-20 14:59:57 -07001407 default: return "unknown=" + mLockTaskAuth;
1408 }
1409 }
1410
Craig Mautner15df08a2015-04-01 12:17:18 -07001411 void setLockTaskAuth() {
Benjamin Franz469dd582015-06-09 14:24:36 +01001412 if (!mPrivileged &&
1413 (mLockTaskMode == LOCK_TASK_LAUNCH_MODE_ALWAYS ||
1414 mLockTaskMode == LOCK_TASK_LAUNCH_MODE_NEVER)) {
1415 // Non-priv apps are not allowed to use always or never, fall back to default
1416 mLockTaskMode = LOCK_TASK_LAUNCH_MODE_DEFAULT;
1417 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001418 switch (mLockTaskMode) {
1419 case LOCK_TASK_LAUNCH_MODE_DEFAULT:
1420 mLockTaskAuth = isLockTaskWhitelistedLocked() ?
1421 LOCK_TASK_AUTH_WHITELISTED : LOCK_TASK_AUTH_PINNABLE;
1422 break;
1423
1424 case LOCK_TASK_LAUNCH_MODE_NEVER:
Benjamin Franz469dd582015-06-09 14:24:36 +01001425 mLockTaskAuth = LOCK_TASK_AUTH_DONT_LOCK;
Craig Mautner15df08a2015-04-01 12:17:18 -07001426 break;
1427
1428 case LOCK_TASK_LAUNCH_MODE_ALWAYS:
Benjamin Franz469dd582015-06-09 14:24:36 +01001429 mLockTaskAuth = LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -07001430 break;
1431
1432 case LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED:
1433 mLockTaskAuth = isLockTaskWhitelistedLocked() ?
1434 LOCK_TASK_AUTH_LAUNCHABLE : LOCK_TASK_AUTH_PINNABLE;
1435 break;
1436 }
Craig Mautner432f64e2015-05-20 14:59:57 -07001437 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "setLockTaskAuth: task=" + this +
1438 " mLockTaskAuth=" + lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07001439 }
1440
1441 boolean isLockTaskWhitelistedLocked() {
Benjamin Franz6fd84cc2015-08-06 18:09:03 +01001442 String pkg = (realActivity != null) ? realActivity.getPackageName() : null;
1443 if (pkg == null) {
Craig Mautner15df08a2015-04-01 12:17:18 -07001444 return false;
1445 }
1446 String[] packages = mService.mLockTaskPackages.get(userId);
1447 if (packages == null) {
1448 return false;
1449 }
1450 for (int i = packages.length - 1; i >= 0; --i) {
Benjamin Franz6fd84cc2015-08-06 18:09:03 +01001451 if (pkg.equals(packages[i])) {
Craig Mautner15df08a2015-04-01 12:17:18 -07001452 return true;
1453 }
1454 }
1455 return false;
1456 }
Wale Ogunwale74e26592016-02-05 11:48:37 -08001457
Craig Mautnera82aa092013-09-13 15:34:08 -07001458 boolean isHomeTask() {
Craig Mautner84984fa2014-06-19 11:19:20 -07001459 return taskType == HOME_ACTIVITY_TYPE;
Craig Mautnera82aa092013-09-13 15:34:08 -07001460 }
1461
Wale Ogunwale74e26592016-02-05 11:48:37 -08001462 boolean isRecentsTask() {
1463 return taskType == RECENTS_ACTIVITY_TYPE;
1464 }
1465
Winson Chung83471632016-12-13 11:02:12 -08001466 boolean isAssistantTask() {
1467 return taskType == ASSISTANT_ACTIVITY_TYPE;
1468 }
1469
Craig Mautner86d67a42013-05-14 10:34:38 -07001470 boolean isApplicationTask() {
Craig Mautner84984fa2014-06-19 11:19:20 -07001471 return taskType == APPLICATION_ACTIVITY_TYPE;
1472 }
1473
1474 boolean isOverHomeStack() {
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001475 return mTaskToReturnTo == HOME_ACTIVITY_TYPE;
Craig Mautner1602ec22013-05-12 10:24:27 -07001476 }
1477
Winson Chung83471632016-12-13 11:02:12 -08001478 boolean isOverAssistantStack() {
1479 return mTaskToReturnTo == ASSISTANT_ACTIVITY_TYPE;
1480 }
1481
Winson Chungd3395382016-12-13 11:49:09 -08001482 private boolean isResizeable(boolean checkSupportsPip) {
1483 return (mService.mForceResizableActivities || ActivityInfo.isResizeableMode(mResizeMode)
1484 || (checkSupportsPip && mSupportsPictureInPicture)) && !mTemporarilyUnresizable;
1485 }
1486
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001487 boolean isResizeable() {
Winson Chungd3395382016-12-13 11:49:09 -08001488 return isResizeable(true /* checkSupportsPip */);
1489 }
1490
1491 boolean supportsSplitScreen() {
1492 // A task can not be docked even if it is considered resizeable because it only supports
1493 // picture-in-picture mode but has a non-resizeable resizeMode
1494 return mService.mSupportsSplitScreenMultiWindow
1495 && isResizeable(false /* checkSupportsPip */)
1496 && !ActivityInfo.isPreserveOrientationMode(mResizeMode);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001497 }
1498
skuhne@google.com322347b2016-12-02 12:54:03 -08001499 /**
1500 * Check that a given bounds matches the application requested orientation.
1501 *
1502 * @param bounds The bounds to be tested.
1503 * @return True if the requested bounds are okay for a resizing request.
1504 */
Wale Ogunwale069bbd32017-02-03 07:58:14 -08001505 private boolean canResizeToBounds(Rect bounds) {
skuhne@google.com322347b2016-12-02 12:54:03 -08001506 if (bounds == null || getStackId() != FREEFORM_WORKSPACE_STACK_ID) {
1507 // Note: If not on the freeform workspace, we ignore the bounds.
1508 return true;
1509 }
1510 final boolean landscape = bounds.width() > bounds.height();
1511 if (mResizeMode == RESIZE_MODE_FORCE_RESIZABLE_PRESERVE_ORIENTATION) {
1512 return mBounds == null || landscape == (mBounds.width() > mBounds.height());
1513 }
1514 return (mResizeMode != RESIZE_MODE_FORCE_RESIZABLE_PORTRAIT_ONLY || !landscape)
1515 && (mResizeMode != RESIZE_MODE_FORCE_RESIZABLE_LANDSCAPE_ONLY || landscape);
1516 }
1517
Craig Mautner525f3d92013-05-07 14:01:50 -07001518 /**
1519 * Find the activity in the history stack within the given task. Returns
1520 * the index within the history at which it's found, or < 0 if not found.
1521 */
1522 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r) {
1523 final ComponentName realActivity = r.realActivity;
1524 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1525 ActivityRecord candidate = mActivities.get(activityNdx);
1526 if (candidate.finishing) {
1527 continue;
1528 }
1529 if (candidate.realActivity.equals(realActivity)) {
1530 return candidate;
1531 }
1532 }
1533 return null;
1534 }
1535
Winson Chunga449dc02014-05-16 11:15:04 -07001536 /** Updates the last task description values. */
1537 void updateTaskDescription() {
1538 // Traverse upwards looking for any break between main task activities and
1539 // utility activities.
1540 int activityNdx;
1541 final int numActivities = mActivities.size();
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001542 final boolean relinquish = numActivities != 0 &&
1543 (mActivities.get(0).info.flags & FLAG_RELINQUISH_TASK_IDENTITY) != 0;
Winson Chunga449dc02014-05-16 11:15:04 -07001544 for (activityNdx = Math.min(numActivities, 1); activityNdx < numActivities;
Craig Mautner21d24a22014-04-23 11:45:37 -07001545 ++activityNdx) {
Winson Chunga449dc02014-05-16 11:15:04 -07001546 final ActivityRecord r = mActivities.get(activityNdx);
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001547 if (relinquish && (r.info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001548 // This will be the top activity for determining taskDescription. Pre-inc to
1549 // overcome initial decrement below.
1550 ++activityNdx;
1551 break;
1552 }
Winson Chunga449dc02014-05-16 11:15:04 -07001553 if (r.intent != null &&
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001554 (r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Winson Chunga449dc02014-05-16 11:15:04 -07001555 break;
1556 }
1557 }
1558 if (activityNdx > 0) {
1559 // Traverse downwards starting below break looking for set label, icon.
1560 // Note that if there are activities in the task but none of them set the
1561 // recent activity values, then we do not fall back to the last set
1562 // values in the TaskRecord.
1563 String label = null;
Craig Mautner648f69b2014-09-18 14:16:26 -07001564 String iconFilename = null;
Winson Chunga449dc02014-05-16 11:15:04 -07001565 int colorPrimary = 0;
Winson Chung1af8eda2016-02-05 17:55:56 +00001566 int colorBackground = 0;
Winson Chunga449dc02014-05-16 11:15:04 -07001567 for (--activityNdx; activityNdx >= 0; --activityNdx) {
1568 final ActivityRecord r = mActivities.get(activityNdx);
1569 if (r.taskDescription != null) {
1570 if (label == null) {
1571 label = r.taskDescription.getLabel();
1572 }
Craig Mautner648f69b2014-09-18 14:16:26 -07001573 if (iconFilename == null) {
1574 iconFilename = r.taskDescription.getIconFilename();
Winson Chunga449dc02014-05-16 11:15:04 -07001575 }
1576 if (colorPrimary == 0) {
1577 colorPrimary = r.taskDescription.getPrimaryColor();
Winson Chunga449dc02014-05-16 11:15:04 -07001578 }
Winson Chung1af8eda2016-02-05 17:55:56 +00001579 if (colorBackground == 0) {
1580 colorBackground = r.taskDescription.getBackgroundColor();
1581 }
Winson Chunga449dc02014-05-16 11:15:04 -07001582 }
1583 }
Winson Chung1af8eda2016-02-05 17:55:56 +00001584 lastTaskDescription = new TaskDescription(label, null, iconFilename, colorPrimary,
1585 colorBackground);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +01001586 if (mWindowContainerController != null) {
1587 mWindowContainerController.setTaskDescription(lastTaskDescription);
1588 }
Winson Chungec396d62014-08-06 17:08:00 -07001589 // Update the task affiliation color if we are the parent of the group
1590 if (taskId == mAffiliatedTaskId) {
1591 mAffiliatedTaskColor = lastTaskDescription.getPrimaryColor();
1592 }
Winson Chunga449dc02014-05-16 11:15:04 -07001593 }
1594 }
1595
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001596 int findEffectiveRootIndex() {
Craig Mautner4767f4b2014-09-18 15:38:33 -07001597 int effectiveNdx = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001598 final int topActivityNdx = mActivities.size() - 1;
Dianne Hackborn39569af2014-09-23 10:56:58 -07001599 for (int activityNdx = 0; activityNdx <= topActivityNdx; ++activityNdx) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001600 final ActivityRecord r = mActivities.get(activityNdx);
1601 if (r.finishing) {
1602 continue;
1603 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001604 effectiveNdx = activityNdx;
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001605 if ((r.info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001606 break;
1607 }
1608 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001609 return effectiveNdx;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001610 }
1611
1612 void updateEffectiveIntent() {
1613 final int effectiveRootIndex = findEffectiveRootIndex();
1614 final ActivityRecord r = mActivities.get(effectiveRootIndex);
Winson Chungfee26772014-08-05 12:21:52 -07001615 setIntent(r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001616 }
1617
Craig Mautner21d24a22014-04-23 11:45:37 -07001618 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001619 if (DEBUG_RECENTS) Slog.i(TAG_RECENTS, "Saving task=" + this);
Craig Mautner21d24a22014-04-23 11:45:37 -07001620
1621 out.attribute(null, ATTR_TASKID, String.valueOf(taskId));
1622 if (realActivity != null) {
1623 out.attribute(null, ATTR_REALACTIVITY, realActivity.flattenToShortString());
1624 }
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001625 out.attribute(null, ATTR_REALACTIVITY_SUSPENDED, String.valueOf(realActivitySuspended));
Craig Mautner21d24a22014-04-23 11:45:37 -07001626 if (origActivity != null) {
1627 out.attribute(null, ATTR_ORIGACTIVITY, origActivity.flattenToShortString());
1628 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001629 // Write affinity, and root affinity if it is different from affinity.
1630 // We use the special string "@" for a null root affinity, so we can identify
1631 // later whether we were given a root affinity or should just make it the
1632 // same as the affinity.
Craig Mautner21d24a22014-04-23 11:45:37 -07001633 if (affinity != null) {
1634 out.attribute(null, ATTR_AFFINITY, affinity);
Dianne Hackborn79228822014-09-16 11:11:23 -07001635 if (!affinity.equals(rootAffinity)) {
1636 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
1637 }
1638 } else if (rootAffinity != null) {
1639 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
Craig Mautner21d24a22014-04-23 11:45:37 -07001640 }
1641 out.attribute(null, ATTR_ROOTHASRESET, String.valueOf(rootWasReset));
Dianne Hackborn13420f22014-07-18 15:43:56 -07001642 out.attribute(null, ATTR_AUTOREMOVERECENTS, String.valueOf(autoRemoveRecents));
Craig Mautner21d24a22014-04-23 11:45:37 -07001643 out.attribute(null, ATTR_ASKEDCOMPATMODE, String.valueOf(askedCompatMode));
1644 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001645 out.attribute(null, ATTR_USER_SETUP_COMPLETE, String.valueOf(mUserSetupComplete));
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001646 out.attribute(null, ATTR_EFFECTIVE_UID, String.valueOf(effectiveUid));
Craig Mautner21d24a22014-04-23 11:45:37 -07001647 out.attribute(null, ATTR_TASKTYPE, String.valueOf(taskType));
Winson Chungffa2ec62014-07-03 15:54:42 -07001648 out.attribute(null, ATTR_FIRSTACTIVETIME, String.valueOf(firstActiveTime));
Winson Chungf1fbd772014-06-24 18:06:58 -07001649 out.attribute(null, ATTR_LASTACTIVETIME, String.valueOf(lastActiveTime));
Craig Mautner21d24a22014-04-23 11:45:37 -07001650 out.attribute(null, ATTR_LASTTIMEMOVED, String.valueOf(mLastTimeMoved));
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001651 out.attribute(null, ATTR_NEVERRELINQUISH, String.valueOf(mNeverRelinquishIdentity));
Craig Mautner21d24a22014-04-23 11:45:37 -07001652 if (lastDescription != null) {
1653 out.attribute(null, ATTR_LASTDESCRIPTION, lastDescription.toString());
1654 }
Winson Chung2cb86c72014-06-25 12:03:30 -07001655 if (lastTaskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07001656 lastTaskDescription.saveToXml(out);
Winson Chung2cb86c72014-06-25 12:03:30 -07001657 }
Winson Chung36f3f032016-09-08 23:29:43 +00001658 mLastThumbnailInfo.saveToXml(out);
Winson Chungec396d62014-08-06 17:08:00 -07001659 out.attribute(null, ATTR_TASK_AFFILIATION_COLOR, String.valueOf(mAffiliatedTaskColor));
Craig Mautnera228ae92014-07-09 05:44:55 -07001660 out.attribute(null, ATTR_TASK_AFFILIATION, String.valueOf(mAffiliatedTaskId));
1661 out.attribute(null, ATTR_PREV_AFFILIATION, String.valueOf(mPrevAffiliateTaskId));
1662 out.attribute(null, ATTR_NEXT_AFFILIATION, String.valueOf(mNextAffiliateTaskId));
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001663 out.attribute(null, ATTR_CALLING_UID, String.valueOf(mCallingUid));
1664 out.attribute(null, ATTR_CALLING_PACKAGE, mCallingPackage == null ? "" : mCallingPackage);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001665 out.attribute(null, ATTR_RESIZE_MODE, String.valueOf(mResizeMode));
Winson Chungd3395382016-12-13 11:49:09 -08001666 out.attribute(null, ATTR_SUPPORTS_PICTURE_IN_PICTURE,
1667 String.valueOf(mSupportsPictureInPicture));
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001668 out.attribute(null, ATTR_PRIVILEGED, String.valueOf(mPrivileged));
Wale Ogunwale706ed792015-08-02 10:29:44 -07001669 if (mLastNonFullscreenBounds != null) {
1670 out.attribute(
1671 null, ATTR_NON_FULLSCREEN_BOUNDS, mLastNonFullscreenBounds.flattenToString());
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001672 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001673 out.attribute(null, ATTR_MIN_WIDTH, String.valueOf(mMinWidth));
1674 out.attribute(null, ATTR_MIN_HEIGHT, String.valueOf(mMinHeight));
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001675 out.attribute(null, ATTR_PERSIST_TASK_VERSION, String.valueOf(PERSIST_TASK_VERSION));
Winson Chung2cb86c72014-06-25 12:03:30 -07001676
Craig Mautner21d24a22014-04-23 11:45:37 -07001677 if (affinityIntent != null) {
1678 out.startTag(null, TAG_AFFINITYINTENT);
1679 affinityIntent.saveToXml(out);
1680 out.endTag(null, TAG_AFFINITYINTENT);
1681 }
1682
Winson Chung36f3f032016-09-08 23:29:43 +00001683 out.startTag(null, TAG_INTENT);
1684 intent.saveToXml(out);
1685 out.endTag(null, TAG_INTENT);
Craig Mautner21d24a22014-04-23 11:45:37 -07001686
1687 final ArrayList<ActivityRecord> activities = mActivities;
1688 final int numActivities = activities.size();
1689 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1690 final ActivityRecord r = activities.get(activityNdx);
Craig Mautner43e52ed2014-06-16 17:18:52 -07001691 if (r.info.persistableMode == ActivityInfo.PERSIST_ROOT_ONLY || !r.isPersistable() ||
Wale Ogunwale843bfb92015-03-27 11:06:48 -07001692 ((r.intent.getFlags() & FLAG_ACTIVITY_NEW_DOCUMENT
1693 | FLAG_ACTIVITY_RETAIN_IN_RECENTS) == FLAG_ACTIVITY_NEW_DOCUMENT) &&
Craig Mautner43e52ed2014-06-16 17:18:52 -07001694 activityNdx > 0) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001695 // Stop at first non-persistable or first break in task (CLEAR_WHEN_TASK_RESET).
Craig Mautner21d24a22014-04-23 11:45:37 -07001696 break;
1697 }
1698 out.startTag(null, TAG_ACTIVITY);
1699 r.saveToXml(out);
1700 out.endTag(null, TAG_ACTIVITY);
1701 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001702 }
1703
Winson Chung36f3f032016-09-08 23:29:43 +00001704 static TaskRecord restoreFromXml(XmlPullParser in, ActivityStackSupervisor stackSupervisor)
1705 throws IOException, XmlPullParserException {
Craig Mautner21d24a22014-04-23 11:45:37 -07001706 Intent intent = null;
1707 Intent affinityIntent = null;
Winsonc809cbb2015-11-02 12:06:15 -08001708 ArrayList<ActivityRecord> activities = new ArrayList<>();
Craig Mautner21d24a22014-04-23 11:45:37 -07001709 ComponentName realActivity = null;
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001710 boolean realActivitySuspended = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001711 ComponentName origActivity = null;
1712 String affinity = null;
Dianne Hackborn79228822014-09-16 11:11:23 -07001713 String rootAffinity = null;
1714 boolean hasRootAffinity = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001715 boolean rootHasReset = false;
Dianne Hackborn13420f22014-07-18 15:43:56 -07001716 boolean autoRemoveRecents = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001717 boolean askedCompatMode = false;
1718 int taskType = ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner21d24a22014-04-23 11:45:37 -07001719 int userId = 0;
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001720 boolean userSetupComplete = true;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001721 int effectiveUid = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -07001722 String lastDescription = null;
Winson Chungffa2ec62014-07-03 15:54:42 -07001723 long firstActiveTime = -1;
Winson Chung2cb86c72014-06-25 12:03:30 -07001724 long lastActiveTime = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -07001725 long lastTimeOnTop = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001726 boolean neverRelinquishIdentity = true;
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001727 int taskId = INVALID_TASK_ID;
Craig Mautner21d24a22014-04-23 11:45:37 -07001728 final int outerDepth = in.getDepth();
Craig Mautner648f69b2014-09-18 14:16:26 -07001729 TaskDescription taskDescription = new TaskDescription();
Winsonc809cbb2015-11-02 12:06:15 -08001730 TaskThumbnailInfo thumbnailInfo = new TaskThumbnailInfo();
Wale Ogunwale18795a22014-12-03 11:38:33 -08001731 int taskAffiliation = INVALID_TASK_ID;
Winson Chungec396d62014-08-06 17:08:00 -07001732 int taskAffiliationColor = 0;
Wale Ogunwale18795a22014-12-03 11:38:33 -08001733 int prevTaskId = INVALID_TASK_ID;
1734 int nextTaskId = INVALID_TASK_ID;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001735 int callingUid = -1;
1736 String callingPackage = "";
Wale Ogunwaled829d362016-02-10 19:24:49 -08001737 int resizeMode = RESIZE_MODE_FORCE_RESIZEABLE;
Winson Chungd3395382016-12-13 11:49:09 -08001738 boolean supportsPictureInPicture = false;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001739 boolean privileged = false;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001740 Rect bounds = null;
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001741 int minWidth = INVALID_MIN_SIZE;
1742 int minHeight = INVALID_MIN_SIZE;
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001743 int persistTaskVersion = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -07001744
1745 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
1746 final String attrName = in.getAttributeName(attrNdx);
1747 final String attrValue = in.getAttributeValue(attrNdx);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001748 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: attribute name=" +
1749 attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001750 if (ATTR_TASKID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001751 if (taskId == INVALID_TASK_ID) taskId = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001752 } else if (ATTR_REALACTIVITY.equals(attrName)) {
1753 realActivity = ComponentName.unflattenFromString(attrValue);
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001754 } else if (ATTR_REALACTIVITY_SUSPENDED.equals(attrName)) {
1755 realActivitySuspended = Boolean.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001756 } else if (ATTR_ORIGACTIVITY.equals(attrName)) {
1757 origActivity = ComponentName.unflattenFromString(attrValue);
1758 } else if (ATTR_AFFINITY.equals(attrName)) {
1759 affinity = attrValue;
Dianne Hackborn79228822014-09-16 11:11:23 -07001760 } else if (ATTR_ROOT_AFFINITY.equals(attrName)) {
1761 rootAffinity = attrValue;
1762 hasRootAffinity = true;
Craig Mautner21d24a22014-04-23 11:45:37 -07001763 } else if (ATTR_ROOTHASRESET.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001764 rootHasReset = Boolean.parseBoolean(attrValue);
Dianne Hackborn13420f22014-07-18 15:43:56 -07001765 } else if (ATTR_AUTOREMOVERECENTS.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001766 autoRemoveRecents = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001767 } else if (ATTR_ASKEDCOMPATMODE.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001768 askedCompatMode = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001769 } else if (ATTR_USERID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001770 userId = Integer.parseInt(attrValue);
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001771 } else if (ATTR_USER_SETUP_COMPLETE.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001772 userSetupComplete = Boolean.parseBoolean(attrValue);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001773 } else if (ATTR_EFFECTIVE_UID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001774 effectiveUid = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001775 } else if (ATTR_TASKTYPE.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001776 taskType = Integer.parseInt(attrValue);
Winson Chungffa2ec62014-07-03 15:54:42 -07001777 } else if (ATTR_FIRSTACTIVETIME.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001778 firstActiveTime = Long.parseLong(attrValue);
Winson Chungf1fbd772014-06-24 18:06:58 -07001779 } else if (ATTR_LASTACTIVETIME.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001780 lastActiveTime = Long.parseLong(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001781 } else if (ATTR_LASTDESCRIPTION.equals(attrName)) {
1782 lastDescription = attrValue;
1783 } else if (ATTR_LASTTIMEMOVED.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001784 lastTimeOnTop = Long.parseLong(attrValue);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001785 } else if (ATTR_NEVERRELINQUISH.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001786 neverRelinquishIdentity = Boolean.parseBoolean(attrValue);
Winsonc809cbb2015-11-02 12:06:15 -08001787 } else if (attrName.startsWith(TaskThumbnailInfo.ATTR_TASK_THUMBNAILINFO_PREFIX)) {
1788 thumbnailInfo.restoreFromXml(attrName, attrValue);
Craig Mautner648f69b2014-09-18 14:16:26 -07001789 } else if (attrName.startsWith(TaskDescription.ATTR_TASKDESCRIPTION_PREFIX)) {
1790 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001791 } else if (ATTR_TASK_AFFILIATION.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001792 taskAffiliation = Integer.parseInt(attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001793 } else if (ATTR_PREV_AFFILIATION.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001794 prevTaskId = Integer.parseInt(attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001795 } else if (ATTR_NEXT_AFFILIATION.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001796 nextTaskId = Integer.parseInt(attrValue);
Winson Chungec396d62014-08-06 17:08:00 -07001797 } else if (ATTR_TASK_AFFILIATION_COLOR.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001798 taskAffiliationColor = Integer.parseInt(attrValue);
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001799 } else if (ATTR_CALLING_UID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001800 callingUid = Integer.parseInt(attrValue);
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001801 } else if (ATTR_CALLING_PACKAGE.equals(attrName)) {
1802 callingPackage = attrValue;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001803 } else if (ATTR_RESIZE_MODE.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001804 resizeMode = Integer.parseInt(attrValue);
Winson Chungd3395382016-12-13 11:49:09 -08001805 } else if (ATTR_SUPPORTS_PICTURE_IN_PICTURE.equals(attrName)) {
1806 supportsPictureInPicture = Boolean.parseBoolean(attrValue);
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001807 } else if (ATTR_PRIVILEGED.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001808 privileged = Boolean.parseBoolean(attrValue);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001809 } else if (ATTR_NON_FULLSCREEN_BOUNDS.equals(attrName)) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001810 bounds = Rect.unflattenFromString(attrValue);
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001811 } else if (ATTR_MIN_WIDTH.equals(attrName)) {
1812 minWidth = Integer.parseInt(attrValue);
1813 } else if (ATTR_MIN_HEIGHT.equals(attrName)) {
1814 minHeight = Integer.parseInt(attrValue);
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001815 } else if (ATTR_PERSIST_TASK_VERSION.equals(attrName)) {
1816 persistTaskVersion = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001817 } else {
1818 Slog.w(TAG, "TaskRecord: Unknown attribute=" + attrName);
1819 }
1820 }
1821
1822 int event;
1823 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
Ben Kwa8814cf42015-07-08 10:54:56 -07001824 (event != XmlPullParser.END_TAG || in.getDepth() >= outerDepth)) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001825 if (event == XmlPullParser.START_TAG) {
1826 final String name = in.getName();
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001827 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: START_TAG name=" +
1828 name);
Craig Mautner21d24a22014-04-23 11:45:37 -07001829 if (TAG_AFFINITYINTENT.equals(name)) {
1830 affinityIntent = Intent.restoreFromXml(in);
1831 } else if (TAG_INTENT.equals(name)) {
1832 intent = Intent.restoreFromXml(in);
1833 } else if (TAG_ACTIVITY.equals(name)) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001834 ActivityRecord activity = ActivityRecord.restoreFromXml(in, stackSupervisor);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001835 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: activity=" +
1836 activity);
Craig Mautner21d24a22014-04-23 11:45:37 -07001837 if (activity != null) {
1838 activities.add(activity);
1839 }
1840 } else {
1841 Slog.e(TAG, "restoreTask: Unexpected name=" + name);
1842 XmlUtils.skipCurrentTag(in);
1843 }
1844 }
1845 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001846 if (!hasRootAffinity) {
1847 rootAffinity = affinity;
1848 } else if ("@".equals(rootAffinity)) {
1849 rootAffinity = null;
1850 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001851 if (effectiveUid <= 0) {
1852 Intent checkIntent = intent != null ? intent : affinityIntent;
1853 effectiveUid = 0;
1854 if (checkIntent != null) {
1855 IPackageManager pm = AppGlobals.getPackageManager();
1856 try {
1857 ApplicationInfo ai = pm.getApplicationInfo(
1858 checkIntent.getComponent().getPackageName(),
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -07001859 PackageManager.MATCH_UNINSTALLED_PACKAGES
1860 | PackageManager.MATCH_DISABLED_COMPONENTS, userId);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001861 if (ai != null) {
1862 effectiveUid = ai.uid;
1863 }
1864 } catch (RemoteException e) {
1865 }
1866 }
1867 Slog.w(TAG, "Updating task #" + taskId + " for " + checkIntent
1868 + ": effectiveUid=" + effectiveUid);
1869 }
1870
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001871 if (persistTaskVersion < 1) {
1872 // We need to convert the resize mode of home activities saved before version one if
1873 // they are marked as RESIZE_MODE_RESIZEABLE to RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION
1874 // since we didn't have that differentiation before version 1 and the system didn't
1875 // resize home activities before then.
1876 if (taskType == HOME_ACTIVITY_TYPE && resizeMode == RESIZE_MODE_RESIZEABLE) {
1877 resizeMode = RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
1878 }
Winson Chungd3395382016-12-13 11:49:09 -08001879 } else {
1880 // This activity has previously marked itself explicitly as both resizeable and
1881 // supporting picture-in-picture. Since there is no longer a requirement for
1882 // picture-in-picture activities to be resizeable, we can mark this simply as
1883 // resizeable and supporting picture-in-picture separately.
1884 if (resizeMode == RESIZE_MODE_RESIZEABLE_AND_PIPABLE_DEPRECATED) {
1885 resizeMode = RESIZE_MODE_RESIZEABLE;
1886 supportsPictureInPicture = true;
1887 }
Wale Ogunwale625ed0c2016-10-18 08:50:31 -07001888 }
1889
Winson Chung36f3f032016-09-08 23:29:43 +00001890 final TaskRecord task = new TaskRecord(stackSupervisor.mService, taskId, intent,
Dianne Hackborn79228822014-09-16 11:11:23 -07001891 affinityIntent, affinity, rootAffinity, realActivity, origActivity, rootHasReset,
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001892 autoRemoveRecents, askedCompatMode, taskType, userId, effectiveUid, lastDescription,
1893 activities, firstActiveTime, lastActiveTime, lastTimeOnTop, neverRelinquishIdentity,
Winsonc809cbb2015-11-02 12:06:15 -08001894 taskDescription, thumbnailInfo, taskAffiliation, prevTaskId, nextTaskId,
Winson Chungd3395382016-12-13 11:49:09 -08001895 taskAffiliationColor, callingUid, callingPackage, resizeMode,
1896 supportsPictureInPicture, privileged, realActivitySuspended, userSetupComplete,
1897 minWidth, minHeight);
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07001898 task.updateOverrideConfiguration(bounds);
Craig Mautner21d24a22014-04-23 11:45:37 -07001899
1900 for (int activityNdx = activities.size() - 1; activityNdx >=0; --activityNdx) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001901 activities.get(activityNdx).task = task;
Craig Mautner21d24a22014-04-23 11:45:37 -07001902 }
1903
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001904 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Restored task=" + task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001905 return task;
1906 }
1907
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001908 private void adjustForMinimalTaskDimensions(Rect bounds) {
1909 if (bounds == null) {
1910 return;
1911 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001912 int minWidth = mMinWidth;
1913 int minHeight = mMinHeight;
Robert Carr9c5867d2016-03-10 15:52:46 -08001914 // If the task has no requested minimal size, we'd like to enforce a minimal size
1915 // so that the user can not render the task too small to manipulate. We don't need
1916 // to do this for the pinned stack as the bounds are controlled by the system.
Andrii Kulian02b7a832016-10-06 23:11:56 -07001917 if (getStackId() != PINNED_STACK_ID) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001918 if (minWidth == INVALID_MIN_SIZE) {
1919 minWidth = mService.mStackSupervisor.mDefaultMinSizeOfResizeableTask;
Andrii Kulian2e751b82016-03-16 16:59:32 -07001920 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001921 if (minHeight == INVALID_MIN_SIZE) {
1922 minHeight = mService.mStackSupervisor.mDefaultMinSizeOfResizeableTask;
Andrii Kulian2e751b82016-03-16 16:59:32 -07001923 }
Robert Carr9c5867d2016-03-10 15:52:46 -08001924 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001925 final boolean adjustWidth = minWidth > bounds.width();
1926 final boolean adjustHeight = minHeight > bounds.height();
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001927 if (!(adjustWidth || adjustHeight)) {
1928 return;
1929 }
1930
1931 if (adjustWidth) {
1932 if (mBounds != null && bounds.right == mBounds.right) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001933 bounds.left = bounds.right - minWidth;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001934 } else {
1935 // Either left bounds match, or neither match, or the previous bounds were
1936 // fullscreen and we default to keeping left.
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001937 bounds.right = bounds.left + minWidth;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001938 }
1939 }
1940 if (adjustHeight) {
1941 if (mBounds != null && bounds.bottom == mBounds.bottom) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001942 bounds.top = bounds.bottom - minHeight;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001943 } else {
1944 // Either top bounds match, or neither match, or the previous bounds were
1945 // fullscreen and we default to keeping top.
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001946 bounds.bottom = bounds.top + minHeight;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001947 }
1948 }
1949 }
1950
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001951 /**
1952 * Update task's override configuration based on the bounds.
Jorim Jaggi0a932142016-02-01 17:42:25 -08001953 * @param bounds The bounds of the task.
Andrii Kulian8072d112016-09-16 11:11:01 -07001954 * @return True if the override configuration was updated.
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001955 */
Andrii Kulian8072d112016-09-16 11:11:01 -07001956 boolean updateOverrideConfiguration(Rect bounds) {
Jorim Jaggi0a932142016-02-01 17:42:25 -08001957 return updateOverrideConfiguration(bounds, null /* insetBounds */);
1958 }
1959
1960 /**
1961 * Update task's override configuration based on the bounds.
1962 * @param bounds The bounds of the task.
1963 * @param insetBounds The bounds used to calculate the system insets, which is used here to
1964 * subtract the navigation bar/status bar size from the screen size reported
1965 * to the application. See {@link IActivityManager#resizeDockedStack}.
Andrii Kulian8072d112016-09-16 11:11:01 -07001966 * @return True if the override configuration was updated.
Jorim Jaggi0a932142016-02-01 17:42:25 -08001967 */
Andrii Kulian8072d112016-09-16 11:11:01 -07001968 boolean updateOverrideConfiguration(Rect bounds, @Nullable Rect insetBounds) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001969 if (Objects.equals(mBounds, bounds)) {
Andrii Kulian8072d112016-09-16 11:11:01 -07001970 return false;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001971 }
Andrii Kulian1779e612016-10-12 21:58:25 -07001972 mTmpConfig.setTo(getOverrideConfiguration());
Wale Ogunwale5f986092015-12-04 15:35:38 -08001973 final boolean oldFullscreen = mFullscreen;
Andrii Kulian1779e612016-10-12 21:58:25 -07001974 final Configuration newConfig = getOverrideConfiguration();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001975
1976 mFullscreen = bounds == null;
Wale Ogunwale706ed792015-08-02 10:29:44 -07001977 if (mFullscreen) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001978 if (mBounds != null && StackId.persistTaskBounds(mStack.mStackId)) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001979 mLastNonFullscreenBounds = mBounds;
1980 }
1981 mBounds = null;
Andrii Kulian1779e612016-10-12 21:58:25 -07001982 newConfig.unset();
Wale Ogunwale706ed792015-08-02 10:29:44 -07001983 } else {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001984 mTmpRect.set(bounds);
1985 adjustForMinimalTaskDimensions(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -08001986 if (mBounds == null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001987 mBounds = new Rect(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -08001988 } else {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001989 mBounds.set(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -08001990 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001991 if (mStack == null || StackId.persistTaskBounds(mStack.mStackId)) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001992 mLastNonFullscreenBounds = mBounds;
1993 }
Andrii Kulian1779e612016-10-12 21:58:25 -07001994 calculateOverrideConfig(newConfig, mTmpRect, insetBounds,
Andrii Kuliandaea3572016-04-08 13:20:51 -07001995 mTmpRect.right != bounds.right, mTmpRect.bottom != bounds.bottom);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001996 }
Andrii Kulian1779e612016-10-12 21:58:25 -07001997 onOverrideConfigurationChanged(newConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001998
1999 if (mFullscreen != oldFullscreen) {
Andrii Kulian933076d2016-03-29 17:04:42 -07002000 mService.mStackSupervisor.scheduleReportMultiWindowModeChanged(this);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002001 }
2002
Andrii Kulian1779e612016-10-12 21:58:25 -07002003 return !mTmpConfig.equals(newConfig);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002004 }
2005
Andrii Kulian1779e612016-10-12 21:58:25 -07002006 /** Clears passed config and fills it with new override values. */
Andrii Kuliana8a9bc52016-10-14 11:00:13 -07002007 private void calculateOverrideConfig(Configuration config, Rect bounds, Rect insetBounds,
2008 boolean overrideWidth, boolean overrideHeight) {
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002009 mTmpNonDecorBounds.set(bounds);
2010 mTmpStableBounds.set(bounds);
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002011
Andrii Kulian1779e612016-10-12 21:58:25 -07002012 config.unset();
Winson Chungbdc646f2017-02-13 12:12:22 -08002013 final Configuration parentConfig = getParent().getConfiguration();
Andrii Kulian1779e612016-10-12 21:58:25 -07002014 final float density = parentConfig.densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002015
Winson Chungbdc646f2017-02-13 12:12:22 -08002016 if (mStack != null) {
2017 final StackWindowController stackController = mStack.getWindowContainerController();
2018 stackController.adjustConfigurationForBounds(bounds, insetBounds,
2019 mTmpNonDecorBounds, mTmpStableBounds, overrideWidth, overrideHeight, density,
2020 config, parentConfig);
2021 } else {
2022 // No stack, give some default values
2023 config.smallestScreenWidthDp =
2024 mService.mStackSupervisor.mDefaultMinSizeOfResizeableTask;
2025 config.screenWidthDp = config.screenHeightDp = config.smallestScreenWidthDp;
2026 Slog.wtf(TAG, "Expected stack when caclulating override config");
2027 }
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002028
Winson Chung60c1aba2017-03-14 17:47:42 -07002029 config.orientation = (config.screenWidthDp <= config.screenHeightDp)
2030 ? Configuration.ORIENTATION_PORTRAIT
2031 : Configuration.ORIENTATION_LANDSCAPE;
2032
Jorim Jaggi85639432016-05-06 17:27:55 -07002033 // For calculating screen layout, we need to use the non-decor inset screen area for the
2034 // calculation for compatibility reasons, i.e. screen area without system bars that could
2035 // never go away in Honeycomb.
Winson Chungbdc646f2017-02-13 12:12:22 -08002036 final int compatScreenWidthDp = (int) (mTmpNonDecorBounds.width() / density);
2037 final int compatScreenHeightDp = (int) (mTmpNonDecorBounds.height() / density);
Andrii Kulian1779e612016-10-12 21:58:25 -07002038 // We're only overriding LONG, SIZE and COMPAT parts of screenLayout, so we start override
2039 // calculation with partial default.
2040 final int sl = Configuration.SCREENLAYOUT_LONG_YES | Configuration.SCREENLAYOUT_SIZE_XLARGE;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08002041 final int longSize = Math.max(compatScreenHeightDp, compatScreenWidthDp);
Andrii Kulian1779e612016-10-12 21:58:25 -07002042 final int shortSize = Math.min(compatScreenHeightDp, compatScreenWidthDp);
Jorim Jaggi85639432016-05-06 17:27:55 -07002043 config.screenLayout = Configuration.reduceScreenLayout(sl, longSize, shortSize);
2044
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08002045 }
2046
2047 /**
2048 * Using the existing configuration {@param config}, creates a new task override config such
2049 * that all the fields that are usually set in an override config are set to the ones in
2050 * {@param config}.
2051 */
2052 Configuration extractOverrideConfig(Configuration config) {
Andrii Kulian1779e612016-10-12 21:58:25 -07002053 final Configuration extracted = new Configuration();
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08002054 extracted.screenWidthDp = config.screenWidthDp;
2055 extracted.screenHeightDp = config.screenHeightDp;
2056 extracted.smallestScreenWidthDp = config.smallestScreenWidthDp;
2057 extracted.orientation = config.orientation;
Andrii Kulian1779e612016-10-12 21:58:25 -07002058 // We're only overriding LONG, SIZE and COMPAT parts of screenLayout.
2059 extracted.screenLayout = config.screenLayout & (Configuration.SCREENLAYOUT_LONG_MASK
2060 | Configuration.SCREENLAYOUT_SIZE_MASK | Configuration.SCREENLAYOUT_COMPAT_NEEDED);
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08002061 return extracted;
2062 }
2063
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002064 Rect updateOverrideConfigurationFromLaunchBounds() {
2065 final Rect bounds = validateBounds(getLaunchBounds());
2066 updateOverrideConfiguration(bounds);
Andrii Kulian73336d812016-03-24 12:56:08 -07002067 if (bounds != null) {
2068 bounds.set(mBounds);
2069 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08002070 return bounds;
2071 }
2072
2073 static Rect validateBounds(Rect bounds) {
2074 if (bounds != null && bounds.isEmpty()) {
2075 Slog.wtf(TAG, "Received strange task bounds: " + bounds, new Throwable());
2076 return null;
2077 }
2078 return bounds;
2079 }
2080
Wale Ogunwale935e5022015-11-10 12:36:10 -08002081 /** Updates the task's bounds and override configuration to match what is expected for the
2082 * input stack. */
2083 void updateOverrideConfigurationForStack(ActivityStack inStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002084 if (mStack != null && mStack == inStack) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08002085 return;
2086 }
2087
2088 if (inStack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002089 if (!isResizeable()) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08002090 throw new IllegalArgumentException("Can not position non-resizeable task="
2091 + this + " in stack=" + inStack);
2092 }
2093 if (mBounds != null) {
2094 return;
2095 }
2096 if (mLastNonFullscreenBounds != null) {
2097 updateOverrideConfiguration(mLastNonFullscreenBounds);
2098 } else {
2099 inStack.layoutTaskInStack(this, null);
2100 }
2101 } else {
2102 updateOverrideConfiguration(inStack.mBounds);
2103 }
2104 }
2105
Chong Zhang0fa656b2015-08-31 15:17:21 -07002106 /**
2107 * Returns the correct stack to use based on task type and currently set bounds,
2108 * regardless of the focused stack and current stack association of the task.
2109 * The task will be moved (and stack focus changed) later if necessary.
2110 */
2111 int getLaunchStackId() {
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002112 if (isRecentsTask()) {
2113 return RECENTS_STACK_ID;
2114 }
2115 if (isHomeTask()) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002116 return HOME_STACK_ID;
2117 }
Winson Chung83471632016-12-13 11:02:12 -08002118 if (isAssistantTask()) {
2119 return ASSISTANT_STACK_ID;
2120 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002121 if (mBounds != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002122 return FREEFORM_WORKSPACE_STACK_ID;
2123 }
2124 return FULLSCREEN_WORKSPACE_STACK_ID;
2125 }
2126
2127 /** Returns the bounds that should be used to launch this task. */
2128 Rect getLaunchBounds() {
Chong Zhang75b37202015-12-04 14:16:36 -08002129 // If we're over lockscreen, forget about stack bounds and use fullscreen.
Jorim Jaggife762342016-10-13 14:33:27 +02002130 if (mService.mStackSupervisor.mKeyguardController.isKeyguardShowing()) {
Chong Zhang75b37202015-12-04 14:16:36 -08002131 return null;
2132 }
2133
Andrii Kulian02b7a832016-10-06 23:11:56 -07002134 if (mStack == null) {
Chong Zhang7d5f5102016-01-13 10:29:24 -08002135 return null;
2136 }
2137
Andrii Kulian02b7a832016-10-06 23:11:56 -07002138 final int stackId = mStack.mStackId;
Chong Zhang7d5f5102016-01-13 10:29:24 -08002139 if (stackId == HOME_STACK_ID
Matthew Ngae1ff4f2016-11-10 15:49:14 -08002140 || stackId == RECENTS_STACK_ID
Winson Chung83471632016-12-13 11:02:12 -08002141 || stackId == ASSISTANT_STACK_ID
Chong Zhang7d5f5102016-01-13 10:29:24 -08002142 || stackId == FULLSCREEN_WORKSPACE_STACK_ID
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002143 || (stackId == DOCKED_STACK_ID && !isResizeable())) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002144 return isResizeable() ? mStack.mBounds : null;
Wale Ogunwale3797c222015-10-27 14:21:58 -07002145 } else if (!StackId.persistTaskBounds(stackId)) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002146 return mStack.mBounds;
Wale Ogunwale706ed792015-08-02 10:29:44 -07002147 }
2148 return mLastNonFullscreenBounds;
2149 }
2150
Wale Ogunwale39381972015-12-17 17:15:29 -08002151 boolean canMatchRootAffinity() {
2152 // We don't allow root affinity matching on the pinned stack as no other task should
2153 // be launching in it based on affinity.
Andrii Kulian02b7a832016-10-06 23:11:56 -07002154 return rootAffinity != null && getStackId() != PINNED_STACK_ID;
Wale Ogunwale39381972015-12-17 17:15:29 -08002155 }
2156
Jorim Jaggi8b702ed2017-01-20 16:59:03 +01002157 void addStartingWindowsForVisibleActivities(boolean taskSwitch) {
2158 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2159 final ActivityRecord r = mActivities.get(activityNdx);
2160 if (r.visible) {
2161 r.showStartingWindow(null /* prev */, false /* newTask */, taskSwitch);
2162 }
2163 }
2164 }
2165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002167 pw.print(prefix); pw.print("userId="); pw.print(userId);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07002168 pw.print(" effectiveUid="); UserHandle.formatUid(pw, effectiveUid);
2169 pw.print(" mCallingUid="); UserHandle.formatUid(pw, mCallingUid);
Suprabh Shukla7745c142016-03-07 18:21:10 -08002170 pw.print(" mUserSetupComplete="); pw.print(mUserSetupComplete);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002171 pw.print(" mCallingPackage="); pw.println(mCallingPackage);
Dianne Hackborn79228822014-09-16 11:11:23 -07002172 if (affinity != null || rootAffinity != null) {
2173 pw.print(prefix); pw.print("affinity="); pw.print(affinity);
2174 if (affinity == null || !affinity.equals(rootAffinity)) {
2175 pw.print(" root="); pw.println(rootAffinity);
2176 } else {
2177 pw.println();
2178 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002179 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07002180 if (voiceSession != null || voiceInteractor != null) {
2181 pw.print(prefix); pw.print("VOICE: session=0x");
2182 pw.print(Integer.toHexString(System.identityHashCode(voiceSession)));
2183 pw.print(" interactor=0x");
2184 pw.println(Integer.toHexString(System.identityHashCode(voiceInteractor)));
2185 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002186 if (intent != null) {
2187 StringBuilder sb = new StringBuilder(128);
2188 sb.append(prefix); sb.append("intent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08002189 intent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002190 sb.append('}');
2191 pw.println(sb.toString());
2192 }
2193 if (affinityIntent != null) {
2194 StringBuilder sb = new StringBuilder(128);
2195 sb.append(prefix); sb.append("affinityIntent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08002196 affinityIntent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002197 sb.append('}');
2198 pw.println(sb.toString());
2199 }
2200 if (origActivity != null) {
2201 pw.print(prefix); pw.print("origActivity=");
2202 pw.println(origActivity.flattenToShortString());
2203 }
2204 if (realActivity != null) {
2205 pw.print(prefix); pw.print("realActivity=");
2206 pw.println(realActivity.flattenToShortString());
2207 }
Dianne Hackborn852975d2014-08-22 17:42:43 -07002208 if (autoRemoveRecents || isPersistable || taskType != 0 || mTaskToReturnTo != 0
2209 || numFullscreen != 0) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002210 pw.print(prefix); pw.print("autoRemoveRecents="); pw.print(autoRemoveRecents);
Dianne Hackborn852975d2014-08-22 17:42:43 -07002211 pw.print(" isPersistable="); pw.print(isPersistable);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002212 pw.print(" numFullscreen="); pw.print(numFullscreen);
2213 pw.print(" taskType="); pw.print(taskType);
2214 pw.print(" mTaskToReturnTo="); pw.println(mTaskToReturnTo);
2215 }
Craig Mautner432f64e2015-05-20 14:59:57 -07002216 if (rootWasReset || mNeverRelinquishIdentity || mReuseTask
2217 || mLockTaskAuth != LOCK_TASK_AUTH_PINNABLE) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002218 pw.print(prefix); pw.print("rootWasReset="); pw.print(rootWasReset);
2219 pw.print(" mNeverRelinquishIdentity="); pw.print(mNeverRelinquishIdentity);
Craig Mautner432f64e2015-05-20 14:59:57 -07002220 pw.print(" mReuseTask="); pw.print(mReuseTask);
2221 pw.print(" mLockTaskAuth="); pw.println(lockTaskAuthToString());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002222 }
Wale Ogunwale18795a22014-12-03 11:38:33 -08002223 if (mAffiliatedTaskId != taskId || mPrevAffiliateTaskId != INVALID_TASK_ID
2224 || mPrevAffiliate != null || mNextAffiliateTaskId != INVALID_TASK_ID
2225 || mNextAffiliate != null) {
Dianne Hackborn852975d2014-08-22 17:42:43 -07002226 pw.print(prefix); pw.print("affiliation="); pw.print(mAffiliatedTaskId);
2227 pw.print(" prevAffiliation="); pw.print(mPrevAffiliateTaskId);
2228 pw.print(" (");
2229 if (mPrevAffiliate == null) {
2230 pw.print("null");
2231 } else {
2232 pw.print(Integer.toHexString(System.identityHashCode(mPrevAffiliate)));
2233 }
2234 pw.print(") nextAffiliation="); pw.print(mNextAffiliateTaskId);
2235 pw.print(" (");
2236 if (mNextAffiliate == null) {
2237 pw.print("null");
2238 } else {
2239 pw.print(Integer.toHexString(System.identityHashCode(mNextAffiliate)));
2240 }
2241 pw.println(")");
2242 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002243 pw.print(prefix); pw.print("Activities="); pw.println(mActivities);
Dianne Hackborn852975d2014-08-22 17:42:43 -07002244 if (!askedCompatMode || !inRecents || !isAvailable) {
2245 pw.print(prefix); pw.print("askedCompatMode="); pw.print(askedCompatMode);
2246 pw.print(" inRecents="); pw.print(inRecents);
2247 pw.print(" isAvailable="); pw.println(isAvailable);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07002248 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002249 pw.print(prefix); pw.print("lastThumbnail="); pw.print(mLastThumbnail);
Dianne Hackborn852975d2014-08-22 17:42:43 -07002250 pw.print(" lastThumbnailFile="); pw.println(mLastThumbnailFile);
2251 if (lastDescription != null) {
2252 pw.print(prefix); pw.print("lastDescription="); pw.println(lastDescription);
2253 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002254 pw.print(prefix); pw.print("stackId="); pw.println(getStackId());
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002255 pw.print(prefix + "hasBeenVisible=" + hasBeenVisible);
2256 pw.print(" mResizeMode=" + ActivityInfo.resizeModeToString(mResizeMode));
Winson Chungd3395382016-12-13 11:49:09 -08002257 pw.print(" mSupportsPictureInPicture=" + mSupportsPictureInPicture);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002258 pw.print(" isResizeable=" + isResizeable());
Winson Chung36f3f032016-09-08 23:29:43 +00002259 pw.print(" firstActiveTime=" + lastActiveTime);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08002260 pw.print(" lastActiveTime=" + lastActiveTime);
2261 pw.println(" (inactive for " + (getInactiveDuration() / 1000) + "s)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 }
2263
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002264 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002266 StringBuilder sb = new StringBuilder(128);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002267 if (stringName != null) {
2268 sb.append(stringName);
2269 sb.append(" U=");
2270 sb.append(userId);
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08002271 sb.append(" StackId=");
Andrii Kulian02b7a832016-10-06 23:11:56 -07002272 sb.append(getStackId());
Craig Mautnerde4ef022013-04-07 19:01:33 -07002273 sb.append(" sz=");
2274 sb.append(mActivities.size());
2275 sb.append('}');
2276 return sb.toString();
2277 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002278 sb.append("TaskRecord{");
2279 sb.append(Integer.toHexString(System.identityHashCode(this)));
2280 sb.append(" #");
2281 sb.append(taskId);
2282 if (affinity != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002283 sb.append(" A=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002284 sb.append(affinity);
2285 } else if (intent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002286 sb.append(" I=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002287 sb.append(intent.getComponent().flattenToShortString());
2288 } else if (affinityIntent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08002289 sb.append(" aI=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002290 sb.append(affinityIntent.getComponent().flattenToShortString());
2291 } else {
2292 sb.append(" ??");
2293 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002294 stringName = sb.toString();
2295 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 }
2297}