blob: 65b835487b29012e9961f431b386d883c5bcbe8b [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
Jorim Jaggi0a932142016-02-01 17:42:25 -080019import android.annotation.Nullable;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -070020import android.app.Activity;
Craig Mautner9db9a0b2013-04-29 17:05:56 -070021import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070022import android.app.ActivityManager.StackId;
Craig Mautner648f69b2014-09-18 14:16:26 -070023import android.app.ActivityManager.TaskDescription;
Wale Ogunwaleff3c66c2015-11-18 19:22:49 -080024import android.app.ActivityManager.TaskThumbnail;
Winsonc809cbb2015-11-02 12:06:15 -080025import android.app.ActivityManager.TaskThumbnailInfo;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -070026import android.app.ActivityOptions;
Dianne Hackborn885fbe52014-08-23 15:23:58 -070027import android.app.AppGlobals;
Jorim Jaggi0a932142016-02-01 17:42:25 -080028import android.app.IActivityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.ComponentName;
30import android.content.Intent;
31import android.content.pm.ActivityInfo;
Dianne Hackborn885fbe52014-08-23 15:23:58 -070032import android.content.pm.ApplicationInfo;
33import android.content.pm.IPackageManager;
34import android.content.pm.PackageManager;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070035import android.content.res.Configuration;
Craig Mautner9db9a0b2013-04-29 17:05:56 -070036import android.graphics.Bitmap;
Winsonc809cbb2015-11-02 12:06:15 -080037import android.graphics.Point;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070038import android.graphics.Rect;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070039import android.os.Debug;
Craig Mautnerc0ffce52014-07-01 12:38:52 -070040import android.os.ParcelFileDescriptor;
Dianne Hackborn885fbe52014-08-23 15:23:58 -070041import android.os.RemoteException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070042import android.os.UserHandle;
Suprabh Shukla7745c142016-03-07 18:21:10 -080043import android.provider.Settings;
Dianne Hackborn91097de2014-04-04 18:02:06 -070044import android.service.voice.IVoiceInteractionSession;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070045import android.util.DisplayMetrics;
Dianne Hackborn7f96b792012-05-29 18:46:45 -070046import android.util.Slog;
Suprabh Shukla23593142015-11-03 17:31:15 -080047
Dianne Hackborn91097de2014-04-04 18:02:06 -070048import com.android.internal.app.IVoiceInteractor;
Craig Mautner21d24a22014-04-23 11:45:37 -070049import com.android.internal.util.XmlUtils;
Suprabh Shukla23593142015-11-03 17:31:15 -080050
Craig Mautner21d24a22014-04-23 11:45:37 -070051import org.xmlpull.v1.XmlPullParser;
52import org.xmlpull.v1.XmlPullParserException;
53import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
Craig Mautnerc0ffce52014-07-01 12:38:52 -070055import java.io.File;
Craig Mautner21d24a22014-04-23 11:45:37 -070056import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import java.io.PrintWriter;
Craig Mautner5d9c7be2013-02-15 14:02:56 -080058import java.util.ArrayList;
Andrii Kulian1779e612016-10-12 21:58:25 -070059import java.util.Collections;
60import java.util.List;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070061import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
Jorim Jaggi0a932142016-02-01 17:42:25 -080063import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
64import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
65import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
66import static android.app.ActivityManager.StackId.HOME_STACK_ID;
67import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
68import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
69import static android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
70import static android.content.Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
Jiaquan Hedd1e66f2016-06-15 15:15:12 -070071import static android.content.pm.ActivityInfo.FLAG_ON_TOP_LAUNCHER;
Wale Ogunwale3eadad72016-10-13 09:16:59 -070072import static android.content.pm.ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY;
Jorim Jaggi0a932142016-02-01 17:42:25 -080073import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_ALWAYS;
74import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_DEFAULT;
75import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
76import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_NEVER;
Wale Ogunwaled829d362016-02-10 19:24:49 -080077import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Jorim Jaggi0a932142016-02-01 17:42:25 -080078import static android.content.pm.ApplicationInfo.PRIVATE_FLAG_PRIVILEGED;
Suprabh Shukla7745c142016-03-07 18:21:10 -080079import static android.provider.Settings.Secure.USER_SETUP_COMPLETE;
Jorim Jaggi0a932142016-02-01 17:42:25 -080080import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ADD_REMOVE;
81import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
82import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
83import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
84import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_ADD_REMOVE;
85import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
86import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
87import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
88import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
89import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
90import static com.android.server.am.ActivityManagerService.LOCK_SCREEN_SHOWN;
91import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
92import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
93import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
Wale Ogunwale3b232392016-05-13 15:37:13 -070094import static com.android.server.am.ActivityRecord.STARTING_WINDOW_SHOWN;
Jorim Jaggi0a932142016-02-01 17:42:25 -080095
Andrii Kulian1779e612016-10-12 21:58:25 -070096final class TaskRecord extends ConfigurationContainer {
Wale Ogunwalee23149f2015-03-06 15:39:44 -080097 private static final String TAG = TAG_WITH_CLASS_NAME ? "TaskRecord" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070098 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070099 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Craig Mautnere0570202015-05-13 13:06:11 -0700100 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700101 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800102
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700103 private static final String ATTR_TASKID = "task_id";
Craig Mautner21d24a22014-04-23 11:45:37 -0700104 private static final String TAG_INTENT = "intent";
105 private static final String TAG_AFFINITYINTENT = "affinity_intent";
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700106 private static final String ATTR_REALACTIVITY = "real_activity";
107 private static final String ATTR_REALACTIVITY_SUSPENDED = "real_activity_suspended";
Craig Mautner21d24a22014-04-23 11:45:37 -0700108 private static final String ATTR_ORIGACTIVITY = "orig_activity";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700109 private static final String TAG_ACTIVITY = "activity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700110 private static final String ATTR_AFFINITY = "affinity";
Dianne Hackborn79228822014-09-16 11:11:23 -0700111 private static final String ATTR_ROOT_AFFINITY = "root_affinity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700112 private static final String ATTR_ROOTHASRESET = "root_has_reset";
Dianne Hackborn13420f22014-07-18 15:43:56 -0700113 private static final String ATTR_AUTOREMOVERECENTS = "auto_remove_recents";
Craig Mautner21d24a22014-04-23 11:45:37 -0700114 private static final String ATTR_ASKEDCOMPATMODE = "asked_compat_mode";
115 private static final String ATTR_USERID = "user_id";
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800116 private static final String ATTR_USER_SETUP_COMPLETE = "user_setup_complete";
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700117 private static final String ATTR_EFFECTIVE_UID = "effective_uid";
Craig Mautner21d24a22014-04-23 11:45:37 -0700118 private static final String ATTR_TASKTYPE = "task_type";
Winson Chungffa2ec62014-07-03 15:54:42 -0700119 private static final String ATTR_FIRSTACTIVETIME = "first_active_time";
Winson Chungf1fbd772014-06-24 18:06:58 -0700120 private static final String ATTR_LASTACTIVETIME = "last_active_time";
Craig Mautner21d24a22014-04-23 11:45:37 -0700121 private static final String ATTR_LASTDESCRIPTION = "last_description";
122 private static final String ATTR_LASTTIMEMOVED = "last_time_moved";
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700123 private static final String ATTR_NEVERRELINQUISH = "never_relinquish_identity";
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700124 private static final String ATTR_TASK_AFFILIATION = "task_affiliation";
Craig Mautnera228ae92014-07-09 05:44:55 -0700125 private static final String ATTR_PREV_AFFILIATION = "prev_affiliation";
126 private static final String ATTR_NEXT_AFFILIATION = "next_affiliation";
Winson Chungec396d62014-08-06 17:08:00 -0700127 private static final String ATTR_TASK_AFFILIATION_COLOR = "task_affiliation_color";
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700128 private static final String ATTR_CALLING_UID = "calling_uid";
129 private static final String ATTR_CALLING_PACKAGE = "calling_package";
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800130 private static final String ATTR_RESIZE_MODE = "resize_mode";
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700131 private static final String ATTR_PRIVILEGED = "privileged";
Wale Ogunwale706ed792015-08-02 10:29:44 -0700132 private static final String ATTR_NON_FULLSCREEN_BOUNDS = "non_fullscreen_bounds";
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700133 private static final String ATTR_MIN_WIDTH = "min_width";
134 private static final String ATTR_MIN_HEIGHT = "min_height";
Andrii Kulian18d75122016-03-27 20:20:28 -0700135
Craig Mautner21d24a22014-04-23 11:45:37 -0700136
137 private static final String TASK_THUMBNAIL_SUFFIX = "_task_thumbnail";
138
Wale Ogunwale18795a22014-12-03 11:38:33 -0800139 static final int INVALID_TASK_ID = -1;
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700140 private static final int INVALID_MIN_SIZE = -1;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 final int taskId; // Unique identifier for this task.
Dianne Hackborn79228822014-09-16 11:11:23 -0700143 String affinity; // The affinity name for this task, or null; may change identity.
144 String rootAffinity; // Initial base affinity, or null; does not change from initial root.
Dianne Hackborn91097de2014-04-04 18:02:06 -0700145 final IVoiceInteractionSession voiceSession; // Voice interaction session driving task
146 final IVoiceInteractor voiceInteractor; // Associated interactor to provide to app
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 Intent intent; // The original intent that started the task.
148 Intent affinityIntent; // Intent of affinity-moved activity that started this task.
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700149 int effectiveUid; // The current effective uid of the identity of this task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 ComponentName origActivity; // The non-alias activity component of the intent.
151 ComponentName realActivity; // The actual activity component that started the task.
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +0000152 boolean realActivitySuspended; // True if the actual activity component that started the
153 // task is suspended.
Winson Chung36f3f032016-09-08 23:29:43 +0000154 long firstActiveTime; // First time this task was active.
155 long lastActiveTime; // Last time this task was active, including sleep.
Dianne Hackborn852975d2014-08-22 17:42:43 -0700156 boolean inRecents; // Actually in the recents list?
157 boolean isAvailable; // Is the activity available to be launched?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 boolean rootWasReset; // True if the intent at the root of the task had
159 // the FLAG_ACTIVITY_RESET_TASK_IF_NEEDED flag.
Dianne Hackborn13420f22014-07-18 15:43:56 -0700160 boolean autoRemoveRecents; // If true, we should automatically remove the task from
161 // recents when activity finishes
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700162 boolean askedCompatMode;// Have asked the user about compat mode for this task.
Dianne Hackbornd38aed82014-06-10 21:36:35 -0700163 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 -0800164
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700165 String stringName; // caching of toString() result.
Dianne Hackborn9da2d402012-03-15 13:43:08 -0700166 int userId; // user for which this task was created
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800167 boolean mUserSetupComplete; // The user set-up is complete as of the last time the task activity
168 // was changed.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800169
170 int numFullscreen; // Number of fullscreen activities.
171
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800172 int mResizeMode; // The resize mode of this task and its activities.
173 // Based on the {@link ActivityInfo#resizeMode} of the root activity.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -0800174 boolean mTemporarilyUnresizable; // Separate flag from mResizeMode used to suppress resize
175 // changes on a temporary basis.
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700176 private int mLockTaskMode; // Which tasklock mode to launch this task in. One of
177 // ActivityManager.LOCK_TASK_LAUNCH_MODE_*
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700178 private boolean mPrivileged; // The root activity application of this task holds
179 // privileged permissions.
Jiaquan Hedd1e66f2016-06-15 15:15:12 -0700180 private boolean mIsOnTopLauncher; // Whether this task is an on-top launcher. See
181 // android.R.attr#onTopLauncher.
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700182
Craig Mautner15df08a2015-04-01 12:17:18 -0700183 /** Can't be put in lockTask mode. */
184 final static int LOCK_TASK_AUTH_DONT_LOCK = 0;
Benjamin Franz469dd582015-06-09 14:24:36 +0100185 /** Can enter app pinning with user approval. Can never start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700186 final static int LOCK_TASK_AUTH_PINNABLE = 1;
187 /** Starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing lockTask task. */
188 final static int LOCK_TASK_AUTH_LAUNCHABLE = 2;
Benjamin Franz469dd582015-06-09 14:24:36 +0100189 /** Can enter lockTask without user approval. Can start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700190 final static int LOCK_TASK_AUTH_WHITELISTED = 3;
Benjamin Franz469dd582015-06-09 14:24:36 +0100191 /** Priv-app that starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing
192 * lockTask task. */
193 final static int LOCK_TASK_AUTH_LAUNCHABLE_PRIV = 4;
Craig Mautner15df08a2015-04-01 12:17:18 -0700194 int mLockTaskAuth = LOCK_TASK_AUTH_PINNABLE;
195
196 int mLockTaskUid = -1; // The uid of the application that called startLockTask().
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800197
Winson Chung03a9bae2014-05-02 09:56:12 -0700198 // This represents the last resolved activity values for this task
199 // NOTE: This value needs to be persisted with each task
Craig Mautner648f69b2014-09-18 14:16:26 -0700200 TaskDescription lastTaskDescription = new TaskDescription();
Winson Chung03a9bae2014-05-02 09:56:12 -0700201
Craig Mautnerd2328952013-03-05 12:46:26 -0800202 /** List of all activities in the task arranged in history order */
Craig Mautner21d24a22014-04-23 11:45:37 -0700203 final ArrayList<ActivityRecord> mActivities;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800204
Andrii Kulian02b7a832016-10-06 23:11:56 -0700205 /** Current stack. Setter must always be used to update the value. */
206 private ActivityStack mStack;
Craig Mautnerd2328952013-03-05 12:46:26 -0800207
Craig Mautner2c1faed2013-07-23 12:56:02 -0700208 /** Takes on same set of values as ActivityRecord.mActivityType */
Craig Mautner21d24a22014-04-23 11:45:37 -0700209 int taskType;
Craig Mautner1602ec22013-05-12 10:24:27 -0700210
Craig Mautner21d24a22014-04-23 11:45:37 -0700211 /** Takes on same value as first root activity */
212 boolean isPersistable = false;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700213 int maxRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700214
215 /** Only used for persistable tasks, otherwise 0. The last time this task was moved. Used for
216 * determining the order when restoring. Sign indicates whether last task movement was to front
217 * (positive) or back (negative). Absolute value indicates time. */
218 long mLastTimeMoved = System.currentTimeMillis();
219
Craig Mautner84984fa2014-06-19 11:19:20 -0700220 /** Indication of what to run next when task exits. Use ActivityRecord types.
221 * ActivityRecord.APPLICATION_ACTIVITY_TYPE indicates to resume the task below this one in the
222 * task stack. */
223 private int mTaskToReturnTo = APPLICATION_ACTIVITY_TYPE;
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700224
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700225 /** If original intent did not allow relinquishing task identity, save that information */
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700226 private boolean mNeverRelinquishIdentity = true;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700227
Craig Mautner362449a2014-06-20 14:04:39 -0700228 // Used in the unique case where we are clearing the task in order to reuse it. In that case we
229 // do not want to delete the stack when the task goes empty.
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -0800230 private boolean mReuseTask = false;
Craig Mautner362449a2014-06-20 14:04:39 -0700231
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700232 private Bitmap mLastThumbnail; // Last thumbnail captured for this item.
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700233 private final File mLastThumbnailFile; // File containing last thumbnail.
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700234 private final String mFilename;
Winsonc809cbb2015-11-02 12:06:15 -0800235 private TaskThumbnailInfo mLastThumbnailInfo;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700236 CharSequence lastDescription; // Last description captured for this item.
237
Craig Mautnera228ae92014-07-09 05:44:55 -0700238 int mAffiliatedTaskId; // taskId of parent affiliation or self if no parent.
Winson Chungec396d62014-08-06 17:08:00 -0700239 int mAffiliatedTaskColor; // color of the parent task affiliation.
Craig Mautnera228ae92014-07-09 05:44:55 -0700240 TaskRecord mPrevAffiliate; // previous task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800241 int mPrevAffiliateTaskId = INVALID_TASK_ID; // previous id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700242 TaskRecord mNextAffiliate; // next task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800243 int mNextAffiliateTaskId = INVALID_TASK_ID; // next id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700244
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700245 // For relaunching the task from recents as though it was launched by the original launcher.
246 int mCallingUid;
247 String mCallingPackage;
248
Craig Mautner21d24a22014-04-23 11:45:37 -0700249 final ActivityManagerService mService;
250
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700251 // Whether or not this task covers the entire screen; by default tasks are fullscreen.
252 boolean mFullscreen = true;
253
254 // Bounds of the Task. null for fullscreen tasks.
255 Rect mBounds = null;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -0800256 private final Rect mTmpStableBounds = new Rect();
257 private final Rect mTmpNonDecorBounds = new Rect();
Wale Ogunwale9a08f822016-02-17 19:03:58 -0800258 private final Rect mTmpRect = new Rect();
Jorim Jaggi0a932142016-02-01 17:42:25 -0800259 private final Rect mTmpRect2 = new Rect();
260
Wale Ogunwale706ed792015-08-02 10:29:44 -0700261 // Last non-fullscreen bounds the task was launched in or resized to.
262 // The information is persisted and used to determine the appropriate stack to launch the
263 // task into on restore.
264 Rect mLastNonFullscreenBounds = null;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700265 // Minimal width and height of this task when it's resizeable. -1 means it should use the
266 // default minimal width/height.
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700267 int mMinWidth;
268 int mMinHeight;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700269
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700270 // Ranking (from top) of this task among all visible tasks. (-1 means it's not visible)
271 // This number will be assigned when we evaluate OOM scores for all visible tasks.
272 int mLayerRank = -1;
273
Andrii Kulian1779e612016-10-12 21:58:25 -0700274 /** Helper object used for updating override configuration. */
275 private Configuration mTmpConfig = new Configuration();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700276
Craig Mautner21d24a22014-04-23 11:45:37 -0700277 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700278 IVoiceInteractionSession _voiceSession, IVoiceInteractor _voiceInteractor) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700279 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700280 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
281 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800282 userId = UserHandle.getUserId(info.applicationInfo.uid);
283 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800284 mLastThumbnailInfo = new TaskThumbnailInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 taskId = _taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700286 mAffiliatedTaskId = _taskId;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700287 voiceSession = _voiceSession;
288 voiceInteractor = _voiceInteractor;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700289 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800290 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700291 mCallingUid = info.applicationInfo.uid;
292 mCallingPackage = info.packageName;
Martijn Coenend4a69702014-06-30 11:12:17 -0700293 setIntent(_intent, info);
Andrii Kulian2e751b82016-03-16 16:59:32 -0700294 setMinDimensions(info);
Winson730bf062016-03-31 18:04:56 -0700295 touchActiveTime();
Craig Mautner21d24a22014-04-23 11:45:37 -0700296 }
297
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700298 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Winsonc809cbb2015-11-02 12:06:15 -0800299 TaskDescription _taskDescription, TaskThumbnailInfo thumbnailInfo) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700300 mService = service;
301 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
302 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800303 userId = UserHandle.getUserId(info.applicationInfo.uid);
304 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800305 mLastThumbnailInfo = thumbnailInfo;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700306 taskId = _taskId;
307 mAffiliatedTaskId = _taskId;
308 voiceSession = null;
309 voiceInteractor = null;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700310 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800311 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700312 mCallingUid = info.applicationInfo.uid;
313 mCallingPackage = info.packageName;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700314 setIntent(_intent, info);
Andrii Kulian2e751b82016-03-16 16:59:32 -0700315 setMinDimensions(info);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700316
317 taskType = ActivityRecord.APPLICATION_ACTIVITY_TYPE;
318 isPersistable = true;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700319 // Clamp to [1, max].
320 maxRecents = Math.min(Math.max(info.maxRecents, 1),
321 ActivityManager.getMaxAppRecentsLimitStatic());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700322
323 taskType = APPLICATION_ACTIVITY_TYPE;
324 mTaskToReturnTo = HOME_ACTIVITY_TYPE;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700325 lastTaskDescription = _taskDescription;
Winson730bf062016-03-31 18:04:56 -0700326 touchActiveTime();
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700327 }
328
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800329 private TaskRecord(ActivityManagerService service, int _taskId, Intent _intent,
330 Intent _affinityIntent, String _affinity, String _rootAffinity,
331 ComponentName _realActivity, ComponentName _origActivity, boolean _rootWasReset,
332 boolean _autoRemoveRecents, boolean _askedCompatMode, int _taskType, int _userId,
333 int _effectiveUid, String _lastDescription, ArrayList<ActivityRecord> activities,
334 long _firstActiveTime, long _lastActiveTime, long lastTimeMoved,
335 boolean neverRelinquishIdentity, TaskDescription _lastTaskDescription,
Winsonc809cbb2015-11-02 12:06:15 -0800336 TaskThumbnailInfo lastThumbnailInfo, int taskAffiliation, int prevTaskId,
337 int nextTaskId, int taskAffiliationColor, int callingUid, String callingPackage,
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800338 int resizeMode, boolean privileged, boolean _realActivitySuspended,
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700339 boolean userSetupComplete, int minWidth, int minHeight) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700340 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700341 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
342 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800343 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(_userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800344 mLastThumbnailInfo = lastThumbnailInfo;
Craig Mautner21d24a22014-04-23 11:45:37 -0700345 taskId = _taskId;
346 intent = _intent;
347 affinityIntent = _affinityIntent;
348 affinity = _affinity;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800349 rootAffinity = _rootAffinity;
Craig Mautner21d24a22014-04-23 11:45:37 -0700350 voiceSession = null;
351 voiceInteractor = null;
352 realActivity = _realActivity;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800353 realActivitySuspended = _realActivitySuspended;
Craig Mautner21d24a22014-04-23 11:45:37 -0700354 origActivity = _origActivity;
355 rootWasReset = _rootWasReset;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700356 isAvailable = true;
Dianne Hackborn13420f22014-07-18 15:43:56 -0700357 autoRemoveRecents = _autoRemoveRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700358 askedCompatMode = _askedCompatMode;
359 taskType = _taskType;
Craig Mautner84984fa2014-06-19 11:19:20 -0700360 mTaskToReturnTo = HOME_ACTIVITY_TYPE;
Craig Mautner21d24a22014-04-23 11:45:37 -0700361 userId = _userId;
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800362 mUserSetupComplete = userSetupComplete;
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700363 effectiveUid = _effectiveUid;
Winson Chungffa2ec62014-07-03 15:54:42 -0700364 firstActiveTime = _firstActiveTime;
Winson Chungf1fbd772014-06-24 18:06:58 -0700365 lastActiveTime = _lastActiveTime;
Craig Mautner21d24a22014-04-23 11:45:37 -0700366 lastDescription = _lastDescription;
367 mActivities = activities;
368 mLastTimeMoved = lastTimeMoved;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700369 mNeverRelinquishIdentity = neverRelinquishIdentity;
Winson Chung2cb86c72014-06-25 12:03:30 -0700370 lastTaskDescription = _lastTaskDescription;
Craig Mautnera228ae92014-07-09 05:44:55 -0700371 mAffiliatedTaskId = taskAffiliation;
Winson Chungec396d62014-08-06 17:08:00 -0700372 mAffiliatedTaskColor = taskAffiliationColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700373 mPrevAffiliateTaskId = prevTaskId;
374 mNextAffiliateTaskId = nextTaskId;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700375 mCallingUid = callingUid;
376 mCallingPackage = callingPackage;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800377 mResizeMode = resizeMode;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700378 mPrivileged = privileged;
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700379 mMinWidth = minWidth;
380 mMinHeight = minHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 }
382
383 void touchActiveTime() {
Winson Chung36f3f032016-09-08 23:29:43 +0000384 lastActiveTime = System.currentTimeMillis();
Winson Chungffa2ec62014-07-03 15:54:42 -0700385 if (firstActiveTime == 0) {
386 firstActiveTime = lastActiveTime;
387 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 long getInactiveDuration() {
Winson Chung36f3f032016-09-08 23:29:43 +0000391 return System.currentTimeMillis() - lastActiveTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700393
Winson Chungfee26772014-08-05 12:21:52 -0700394 /** Sets the original intent, and the calling uid and package. */
395 void setIntent(ActivityRecord r) {
Winson Chungfee26772014-08-05 12:21:52 -0700396 mCallingUid = r.launchedFromUid;
397 mCallingPackage = r.launchedFromPackage;
Craig Mautner15df08a2015-04-01 12:17:18 -0700398 setIntent(r.intent, r.info);
Winson Chungfee26772014-08-05 12:21:52 -0700399 }
400
401 /** Sets the original intent, _without_ updating the calling uid or package. */
402 private void setIntent(Intent _intent, ActivityInfo info) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700403 if (intent == null) {
404 mNeverRelinquishIdentity =
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700405 (info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700406 } else if (mNeverRelinquishIdentity) {
407 return;
408 }
409
410 affinity = info.taskAffinity;
Dianne Hackborn79228822014-09-16 11:11:23 -0700411 if (intent == null) {
412 // If this task already has an intent associated with it, don't set the root
413 // affinity -- we don't want it changing after initially set, but the initially
414 // set value may be null.
415 rootAffinity = affinity;
416 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700417 effectiveUid = info.applicationInfo.uid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700418 stringName = null;
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 if (info.targetActivity == null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800421 if (_intent != null) {
422 // If this Intent has a selector, we want to clear it for the
423 // recent task since it is not relevant if the user later wants
424 // to re-launch the app.
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700425 if (_intent.getSelector() != null || _intent.getSourceBounds() != null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800426 _intent = new Intent(_intent);
427 _intent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700428 _intent.setSourceBounds(null);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800429 }
430 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700431 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Setting Intent of " + this + " to " + _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 intent = _intent;
433 realActivity = _intent != null ? _intent.getComponent() : null;
434 origActivity = null;
435 } else {
436 ComponentName targetComponent = new ComponentName(
437 info.packageName, info.targetActivity);
438 if (_intent != null) {
439 Intent targetIntent = new Intent(_intent);
440 targetIntent.setComponent(targetComponent);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800441 targetIntent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700442 targetIntent.setSourceBounds(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700443 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700444 "Setting Intent of " + this + " to target " + targetIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 intent = targetIntent;
446 realActivity = targetComponent;
447 origActivity = _intent.getComponent();
448 } else {
449 intent = null;
450 realActivity = targetComponent;
451 origActivity = new ComponentName(info.packageName, info.name);
452 }
453 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700454
Craig Mautner47b20ba2014-09-17 17:23:44 -0700455 final int intentFlags = intent == null ? 0 : intent.getFlags();
456 if ((intentFlags & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 // Once we are set to an Intent with this flag, we count this
458 // task as having a true root activity.
459 rootWasReset = true;
460 }
Dianne Hackborn09233282014-04-30 11:33:59 -0700461 userId = UserHandle.getUserId(info.applicationInfo.uid);
Winson Chung36f3f032016-09-08 23:29:43 +0000462 mUserSetupComplete = Settings.Secure.getIntForUser(mService.mContext.getContentResolver(),
463 USER_SETUP_COMPLETE, 0, userId) != 0;
Craig Mautner41db4a72014-05-07 17:20:56 -0700464 if ((info.flags & ActivityInfo.FLAG_AUTO_REMOVE_FROM_RECENTS) != 0) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700465 // If the activity itself has requested auto-remove, then just always do it.
466 autoRemoveRecents = true;
Wale Ogunwale843bfb92015-03-27 11:06:48 -0700467 } else if ((intentFlags & (FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_RETAIN_IN_RECENTS))
468 == FLAG_ACTIVITY_NEW_DOCUMENT) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700469 // If the caller has not asked for the document to be retained, then we may
470 // want to turn on auto-remove, depending on whether the target has set its
471 // own document launch mode.
472 if (info.documentLaunchMode != ActivityInfo.DOCUMENT_LAUNCH_NONE) {
473 autoRemoveRecents = false;
474 } else {
475 autoRemoveRecents = true;
476 }
477 } else {
478 autoRemoveRecents = false;
Craig Mautner41db4a72014-05-07 17:20:56 -0700479 }
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800480 mResizeMode = info.resizeMode;
Jiaquan Hedd1e66f2016-06-15 15:15:12 -0700481 mIsOnTopLauncher = (info.flags & FLAG_ON_TOP_LAUNCHER) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -0700482 mLockTaskMode = info.lockTaskLaunchMode;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700483 mPrivileged = (info.applicationInfo.privateFlags & PRIVATE_FLAG_PRIVILEGED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -0700484 setLockTaskAuth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800486
Andrii Kulian2e751b82016-03-16 16:59:32 -0700487 /** Sets the original minimal width and height. */
488 private void setMinDimensions(ActivityInfo info) {
489 if (info != null && info.windowLayout != null) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700490 mMinWidth = info.windowLayout.minWidth;
491 mMinHeight = info.windowLayout.minHeight;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700492 } else {
Andrii Kulianf66a83d2016-05-17 12:17:44 -0700493 mMinWidth = INVALID_MIN_SIZE;
494 mMinHeight = INVALID_MIN_SIZE;
Andrii Kulian2e751b82016-03-16 16:59:32 -0700495 }
496 }
497
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800498 /**
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700499 * Return true if the input activity has the same intent filter as the intent this task
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800500 * record is based on (normally the root activity intent).
501 */
Andrii Kulian206b9fa2016-06-02 13:18:01 -0700502 boolean isSameIntentFilter(ActivityRecord r) {
Wale Ogunwale715a1dc2016-02-29 14:27:32 -0800503 final Intent intent = new Intent(r.intent);
504 // Correct the activity intent for aliasing. The task record intent will always be based on
505 // the real activity that will be launched not the alias, so we need to use an intent with
506 // the component name pointing to the real activity not the alias in the activity record.
507 intent.setComponent(r.realActivity);
508 return this.intent.filterEquals(intent);
509 }
510
Craig Mautner84984fa2014-06-19 11:19:20 -0700511 void setTaskToReturnTo(int taskToReturnTo) {
Wale Ogunwale673cbd22016-01-30 18:30:55 -0800512 mTaskToReturnTo = (taskToReturnTo == RECENTS_ACTIVITY_TYPE)
513 ? HOME_ACTIVITY_TYPE : taskToReturnTo;
Craig Mautner84984fa2014-06-19 11:19:20 -0700514 }
515
516 int getTaskToReturnTo() {
517 return mTaskToReturnTo;
518 }
519
Craig Mautnera228ae92014-07-09 05:44:55 -0700520 void setPrevAffiliate(TaskRecord prevAffiliate) {
521 mPrevAffiliate = prevAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800522 mPrevAffiliateTaskId = prevAffiliate == null ? INVALID_TASK_ID : prevAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700523 }
524
525 void setNextAffiliate(TaskRecord nextAffiliate) {
526 mNextAffiliate = nextAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800527 mNextAffiliateTaskId = nextAffiliate == null ? INVALID_TASK_ID : nextAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700528 }
529
Andrii Kulian02b7a832016-10-06 23:11:56 -0700530 ActivityStack getStack() {
531 return mStack;
532 }
533
Andrii Kulian1779e612016-10-12 21:58:25 -0700534 /** Must be used for setting parent stack because it performs configuration updates. */
Andrii Kulian02b7a832016-10-06 23:11:56 -0700535 void setStack(ActivityStack stack) {
536 mStack = stack;
Andrii Kulian1779e612016-10-12 21:58:25 -0700537 onParentChanged();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700538 }
539
540 /**
541 * @return Id of current stack, {@link INVALID_STACK_ID} if no stack is set.
542 */
543 int getStackId() {
544 return mStack != null ? mStack.mStackId : INVALID_STACK_ID;
545 }
546
Andrii Kulian1779e612016-10-12 21:58:25 -0700547 @Override
548 protected int getChildCount() {
549 return 0;
550 }
551
552 @Override
553 protected ConfigurationContainer getChildAt(int index) {
554 return null;
555 }
556
557 @Override
558 protected ConfigurationContainer getParent() {
559 return mStack;
560 }
561
Craig Mautnera228ae92014-07-09 05:44:55 -0700562 // Close up recents linked list.
563 void closeRecentsChain() {
564 if (mPrevAffiliate != null) {
565 mPrevAffiliate.setNextAffiliate(mNextAffiliate);
566 }
567 if (mNextAffiliate != null) {
568 mNextAffiliate.setPrevAffiliate(mPrevAffiliate);
569 }
570 setPrevAffiliate(null);
571 setNextAffiliate(null);
572 }
573
Winson Chung740c3ac2014-11-12 16:14:38 -0800574 void removedFromRecents() {
Dianne Hackborn852975d2014-08-22 17:42:43 -0700575 disposeThumbnail();
576 closeRecentsChain();
577 if (inRecents) {
578 inRecents = false;
Winson Chung740c3ac2014-11-12 16:14:38 -0800579 mService.notifyTaskPersisterLocked(this, false);
Dianne Hackborn852975d2014-08-22 17:42:43 -0700580 }
581 }
582
Craig Mautnera228ae92014-07-09 05:44:55 -0700583 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
584 closeRecentsChain();
585 mAffiliatedTaskId = taskToAffiliateWith.mAffiliatedTaskId;
Winson Chungec396d62014-08-06 17:08:00 -0700586 mAffiliatedTaskColor = taskToAffiliateWith.mAffiliatedTaskColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700587 // Find the end
588 while (taskToAffiliateWith.mNextAffiliate != null) {
589 final TaskRecord nextRecents = taskToAffiliateWith.mNextAffiliate;
590 if (nextRecents.mAffiliatedTaskId != mAffiliatedTaskId) {
591 Slog.e(TAG, "setTaskToAffiliateWith: nextRecents=" + nextRecents + " affilTaskId="
592 + nextRecents.mAffiliatedTaskId + " should be " + mAffiliatedTaskId);
593 if (nextRecents.mPrevAffiliate == taskToAffiliateWith) {
594 nextRecents.setPrevAffiliate(null);
595 }
596 taskToAffiliateWith.setNextAffiliate(null);
597 break;
598 }
599 taskToAffiliateWith = nextRecents;
600 }
601 taskToAffiliateWith.setNextAffiliate(this);
602 setPrevAffiliate(taskToAffiliateWith);
603 setNextAffiliate(null);
604 }
605
Winson Chung096f36b2014-08-20 15:39:01 -0700606 /**
Winsonc809cbb2015-11-02 12:06:15 -0800607 * Sets the last thumbnail with the current task bounds and the system orientation.
Winson Chung096f36b2014-08-20 15:39:01 -0700608 * @return whether the thumbnail was set
609 */
Winsonc809cbb2015-11-02 12:06:15 -0800610 boolean setLastThumbnailLocked(Bitmap thumbnail) {
Winsonc809cbb2015-11-02 12:06:15 -0800611 int taskWidth = 0;
612 int taskHeight = 0;
613 if (mBounds != null) {
614 // Non-fullscreen tasks
615 taskWidth = mBounds.width();
616 taskHeight = mBounds.height();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700617 } else if (mStack != null) {
Winsonc809cbb2015-11-02 12:06:15 -0800618 // Fullscreen tasks
619 final Point displaySize = new Point();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700620 mStack.getDisplaySize(displaySize);
Winsonc809cbb2015-11-02 12:06:15 -0800621 taskWidth = displaySize.x;
622 taskHeight = displaySize.y;
623 } else {
624 Slog.e(TAG, "setLastThumbnailLocked() called on Task without stack");
625 }
Andrii Kulian1779e612016-10-12 21:58:25 -0700626 // We need to provide the current orientation of the display on which this task resides,
627 // not the orientation of the task.
628 final int orientation =
629 getStack().mActivityContainer.mActivityDisplay.getConfiguration().orientation;
630 return setLastThumbnailLocked(thumbnail, taskWidth, taskHeight, orientation);
Winsonc809cbb2015-11-02 12:06:15 -0800631 }
632
633 /**
634 * Sets the last thumbnail with the current task bounds.
635 * @return whether the thumbnail was set
636 */
637 private boolean setLastThumbnailLocked(Bitmap thumbnail, int taskWidth, int taskHeight,
638 int screenOrientation) {
Winson Chung096f36b2014-08-20 15:39:01 -0700639 if (mLastThumbnail != thumbnail) {
640 mLastThumbnail = thumbnail;
Winsonc809cbb2015-11-02 12:06:15 -0800641 mLastThumbnailInfo.taskWidth = taskWidth;
642 mLastThumbnailInfo.taskHeight = taskHeight;
643 mLastThumbnailInfo.screenOrientation = screenOrientation;
Winson Chung096f36b2014-08-20 15:39:01 -0700644 if (thumbnail == null) {
645 if (mLastThumbnailFile != null) {
646 mLastThumbnailFile.delete();
647 }
648 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800649 mService.mRecentTasks.saveImage(thumbnail, mLastThumbnailFile.getAbsolutePath());
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700650 }
Winson Chung096f36b2014-08-20 15:39:01 -0700651 return true;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700652 }
Winson Chung096f36b2014-08-20 15:39:01 -0700653 return false;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700654 }
655
656 void getLastThumbnail(TaskThumbnail thumbs) {
657 thumbs.mainThumbnail = mLastThumbnail;
Winsonc809cbb2015-11-02 12:06:15 -0800658 thumbs.thumbnailInfo = mLastThumbnailInfo;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700659 thumbs.thumbnailFileDescriptor = null;
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700660 if (mLastThumbnail == null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800661 thumbs.mainThumbnail = mService.mRecentTasks.getImageFromWriteQueue(
Suprabh Shukla23593142015-11-03 17:31:15 -0800662 mLastThumbnailFile.getAbsolutePath());
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700663 }
Winson Chung096f36b2014-08-20 15:39:01 -0700664 // Only load the thumbnail file if we don't have a thumbnail
665 if (thumbs.mainThumbnail == null && mLastThumbnailFile.exists()) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700666 try {
667 thumbs.thumbnailFileDescriptor = ParcelFileDescriptor.open(mLastThumbnailFile,
668 ParcelFileDescriptor.MODE_READ_ONLY);
669 } catch (IOException e) {
Dianne Hackborn9844d292013-10-04 16:44:22 -0700670 }
671 }
672 }
673
Winsonc809cbb2015-11-02 12:06:15 -0800674 /**
675 * Removes in-memory thumbnail data when the max number of in-memory task thumbnails is reached.
676 */
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700677 void freeLastThumbnail() {
678 mLastThumbnail = null;
679 }
680
Winsonc809cbb2015-11-02 12:06:15 -0800681 /**
682 * Removes all associated thumbnail data when a task is removed or pruned from recents.
683 */
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700684 void disposeThumbnail() {
Winsonc6a2da02015-11-11 18:11:59 -0800685 mLastThumbnailInfo.reset();
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700686 mLastThumbnail = null;
687 lastDescription = null;
688 }
689
Winson Chung1147c402014-05-14 11:05:00 -0700690 /** Returns the intent for the root activity for this task */
691 Intent getBaseIntent() {
692 return intent != null ? intent : affinityIntent;
693 }
694
Winson Chung3b3f4642014-04-22 10:08:18 -0700695 /** Returns the first non-finishing activity from the root. */
696 ActivityRecord getRootActivity() {
697 for (int i = 0; i < mActivities.size(); i++) {
698 final ActivityRecord r = mActivities.get(i);
699 if (r.finishing) {
700 continue;
701 }
702 return r;
703 }
704 return null;
705 }
706
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800707 ActivityRecord getTopActivity() {
708 for (int i = mActivities.size() - 1; i >= 0; --i) {
709 final ActivityRecord r = mActivities.get(i);
710 if (r.finishing) {
711 continue;
712 }
713 return r;
714 }
715 return null;
716 }
717
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700718 ActivityRecord topRunningActivityLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700719 if (mStack != null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700720 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
721 ActivityRecord r = mActivities.get(activityNdx);
Chong Zhang87761972016-08-22 13:53:24 -0700722 if (!r.finishing && r.okToShowLocked()) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700723 return r;
724 }
Craig Mautner6b74cb52013-09-27 17:02:21 -0700725 }
726 }
727 return null;
728 }
729
Wale Ogunwale3b232392016-05-13 15:37:13 -0700730 ActivityRecord topRunningActivityWithStartingWindowLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700731 if (mStack != null) {
Wale Ogunwale3b232392016-05-13 15:37:13 -0700732 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
733 ActivityRecord r = mActivities.get(activityNdx);
734 if (r.mStartingWindowState != STARTING_WINDOW_SHOWN
Chong Zhang87761972016-08-22 13:53:24 -0700735 || r.finishing || !r.okToShowLocked()) {
Wale Ogunwale3b232392016-05-13 15:37:13 -0700736 continue;
737 }
738 return r;
739 }
740 }
741 return null;
742 }
743
Chong Zhang87761972016-08-22 13:53:24 -0700744 boolean okToShowLocked() {
745 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
746 // okay to show the activity when locked.
747 return mService.mStackSupervisor.isCurrentProfileLocked(userId)
748 || topRunningActivityLocked() != null;
749 }
750
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800751 void setFrontOfTask() {
752 setFrontOfTask(null);
753 }
754
Craig Mautner3b475fe2013-12-16 15:58:31 -0800755 /** Call after activity movement or finish to make sure that frontOfTask is set correctly */
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800756 void setFrontOfTask(ActivityRecord newTop) {
757 // If a top candidate is suggested by the caller, go ahead and use it and mark all others
758 // as not front. This is needed in situations where the current front activity in the
759 // task isn't finished yet and we want to set the front to the activity moved to the front
760 // of the task.
761 boolean foundFront = newTop != null ? true : false;
762
Craig Mautner3b475fe2013-12-16 15:58:31 -0800763 final int numActivities = mActivities.size();
Craig Mautner704e40b2013-12-18 16:43:51 -0800764 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
Craig Mautner3b475fe2013-12-16 15:58:31 -0800765 final ActivityRecord r = mActivities.get(activityNdx);
766 if (foundFront || r.finishing) {
767 r.frontOfTask = false;
768 } else {
769 r.frontOfTask = true;
770 // Set frontOfTask false for every following activity.
771 foundFront = true;
772 }
773 }
Craig Mautner9587ee02014-06-23 15:00:10 +0000774 if (!foundFront && numActivities > 0) {
775 // All activities of this task are finishing. As we ought to have a frontOfTask
776 // activity, make the bottom activity front.
777 mActivities.get(0).frontOfTask = true;
778 }
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800779 if (newTop != null) {
780 newTop.frontOfTask = true;
781 }
Craig Mautner3b475fe2013-12-16 15:58:31 -0800782 }
783
Craig Mautnerde4ef022013-04-07 19:01:33 -0700784 /**
Craig Mautner3b475fe2013-12-16 15:58:31 -0800785 * Reorder the history stack so that the passed activity is brought to the front.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700786 */
787 final void moveActivityToFrontLocked(ActivityRecord newTop) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700788 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
789 "Removing and adding activity " + newTop
790 + " to stack at top callers=" + Debug.getCallers(4));
Craig Mautnerde4ef022013-04-07 19:01:33 -0700791
Craig Mautnerde4ef022013-04-07 19:01:33 -0700792 mActivities.remove(newTop);
793 mActivities.add(newTop);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700794 updateEffectiveIntent();
Craig Mautner3b475fe2013-12-16 15:58:31 -0800795
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800796 setFrontOfTask(newTop);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700797 }
798
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800799 void addActivityAtBottom(ActivityRecord r) {
Craig Mautner77878772013-03-04 19:46:24 -0800800 addActivityAtIndex(0, r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800801 }
802
803 void addActivityToTop(ActivityRecord r) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700804 addActivityAtIndex(mActivities.size(), r);
805 }
806
807 void addActivityAtIndex(int index, ActivityRecord r) {
Craig Mautner6170f732013-04-02 13:05:23 -0700808 // 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 -0800809 if (!mActivities.remove(r) && r.fullscreen) {
810 // Was not previously in list.
811 numFullscreen++;
812 }
Craig Mautner2c1faed2013-07-23 12:56:02 -0700813 // Only set this based on the first activity
814 if (mActivities.isEmpty()) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700815 taskType = r.mActivityType;
816 isPersistable = r.isPersistable();
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700817 mCallingUid = r.launchedFromUid;
818 mCallingPackage = r.launchedFromPackage;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700819 // Clamp to [1, max].
820 maxRecents = Math.min(Math.max(r.info.maxRecents, 1),
821 ActivityManager.getMaxAppRecentsLimitStatic());
Craig Mautner2c1faed2013-07-23 12:56:02 -0700822 } else {
823 // Otherwise make all added activities match this one.
Craig Mautner21d24a22014-04-23 11:45:37 -0700824 r.mActivityType = taskType;
Craig Mautner78733002013-06-10 13:54:49 -0700825 }
Wale Ogunwale3b232392016-05-13 15:37:13 -0700826
827 final int size = mActivities.size();
828
829 if (index == size && size > 0) {
830 final ActivityRecord top = mActivities.get(size - 1);
831 if (top.mTaskOverlay) {
832 // Place below the task overlay activity since the overlay activity should always
833 // be on top.
834 index--;
835 }
836 }
837
Craig Mautner77878772013-03-04 19:46:24 -0800838 mActivities.add(index, r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700839 updateEffectiveIntent();
Craig Mautner21d24a22014-04-23 11:45:37 -0700840 if (r.isPersistable()) {
841 mService.notifyTaskPersisterLocked(this, false);
842 }
Craig Mautner77878772013-03-04 19:46:24 -0800843 }
844
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800845 /** @return true if this was the last activity in the task */
846 boolean removeActivity(ActivityRecord r) {
847 if (mActivities.remove(r) && r.fullscreen) {
848 // Was previously in list.
849 numFullscreen--;
850 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700851 if (r.isPersistable()) {
852 mService.notifyTaskPersisterLocked(this, false);
853 }
Wale Ogunwale89182d52016-03-11 10:38:36 -0800854
Andrii Kulian02b7a832016-10-06 23:11:56 -0700855 if (getStackId() == PINNED_STACK_ID) {
Wale Ogunwale89182d52016-03-11 10:38:36 -0800856 // We normally notify listeners of task stack changes on pause, however pinned stack
857 // activities are normally in the paused state so no notification will be sent there
858 // before the activity is removed. We send it here so instead.
859 mService.notifyTaskStackChangedLocked();
860 }
861
Craig Mautner41326202014-06-20 14:38:21 -0700862 if (mActivities.isEmpty()) {
Craig Mautner5afcd4d2014-06-21 18:11:33 -0700863 return !mReuseTask;
Craig Mautner41326202014-06-20 14:38:21 -0700864 }
865 updateEffectiveIntent();
866 return false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800867 }
868
Craig Mautner41db4a72014-05-07 17:20:56 -0700869 boolean autoRemoveFromRecents() {
Dianne Hackbornd38aed82014-06-10 21:36:35 -0700870 // We will automatically remove the task either if it has explicitly asked for
871 // this, or it is empty and has never contained an activity that got shown to
872 // the user.
Dianne Hackborn13420f22014-07-18 15:43:56 -0700873 return autoRemoveRecents || (mActivities.isEmpty() && !hasBeenVisible);
Craig Mautner41db4a72014-05-07 17:20:56 -0700874 }
875
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700876 /**
877 * Completely remove all activities associated with an existing
878 * task starting at a specified index.
879 */
880 final void performClearTaskAtIndexLocked(int activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700881 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700882 for ( ; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700883 final ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700884 if (r.finishing) {
885 continue;
886 }
Andrii Kulian02b7a832016-10-06 23:11:56 -0700887 if (mStack == null) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700888 // Task was restored from persistent storage.
889 r.takeFromHistory();
890 mActivities.remove(activityNdx);
891 --activityNdx;
892 --numActivities;
Andrii Kulian02b7a832016-10-06 23:11:56 -0700893 } else if (mStack.finishActivityLocked(
Todd Kennedy539db512014-12-15 09:57:55 -0800894 r, Activity.RESULT_CANCELED, null, "clear-task-index", false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700895 --activityNdx;
896 --numActivities;
897 }
898 }
899 }
900
901 /**
902 * Completely remove all activities associated with an existing task.
903 */
904 final void performClearTaskLocked() {
Craig Mautner362449a2014-06-20 14:04:39 -0700905 mReuseTask = true;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700906 performClearTaskAtIndexLocked(0);
Craig Mautner362449a2014-06-20 14:04:39 -0700907 mReuseTask = false;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700908 }
909
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -0800910 ActivityRecord performClearTaskForReuseLocked(ActivityRecord newR, int launchFlags) {
911 mReuseTask = true;
912 final ActivityRecord result = performClearTaskLocked(newR, launchFlags);
913 mReuseTask = false;
914 return result;
915 }
916
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700917 /**
918 * Perform clear operation as requested by
919 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
920 * stack to the given task, then look for
921 * an instance of that activity in the stack and, if found, finish all
922 * activities on top of it and return the instance.
923 *
924 * @param newR Description of the new activity being started.
925 * @return Returns the old activity that should be continued to be used,
926 * or null if none was found.
927 */
928 final ActivityRecord performClearTaskLocked(ActivityRecord newR, int launchFlags) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700929 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700930 for (int activityNdx = numActivities - 1; activityNdx >= 0; --activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700931 ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700932 if (r.finishing) {
933 continue;
934 }
935 if (r.realActivity.equals(newR.realActivity)) {
936 // Here it is! Now finish everything in front...
Craig Mautner1602ec22013-05-12 10:24:27 -0700937 final ActivityRecord ret = r;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700938
939 for (++activityNdx; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700940 r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700941 if (r.finishing) {
942 continue;
943 }
944 ActivityOptions opts = r.takeOptionsLocked();
945 if (opts != null) {
946 ret.updateOptionsLocked(opts);
947 }
Andrii Kulian02b7a832016-10-06 23:11:56 -0700948 if (mStack != null && mStack.finishActivityLocked(
Todd Kennedy539db512014-12-15 09:57:55 -0800949 r, Activity.RESULT_CANCELED, null, "clear-task-stack", false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700950 --activityNdx;
951 --numActivities;
952 }
953 }
954
955 // Finally, if this is a normal launch mode (that is, not
956 // expecting onNewIntent()), then we will finish the current
957 // instance of the activity so a new fresh one can be started.
958 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
Daichi Hirono15a02992016-04-27 18:47:01 +0900959 && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0
960 && !ActivityStarter.isDocumentLaunchesIntoExisting(launchFlags)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700961 if (!ret.finishing) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700962 if (mStack != null) {
963 mStack.finishActivityLocked(
Wale Ogunwale7d701172015-03-11 15:36:30 -0700964 ret, Activity.RESULT_CANCELED, null, "clear-task-top", false);
965 }
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700966 return null;
967 }
968 }
969
970 return ret;
971 }
972 }
973
974 return null;
975 }
976
Andrii Kulian21713ac2016-10-12 22:05:05 -0700977 TaskThumbnail getTaskThumbnailLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700978 if (mStack != null) {
979 final ActivityRecord resumedActivity = mStack.mResumedActivity;
Craig Mautner21d24a22014-04-23 11:45:37 -0700980 if (resumedActivity != null && resumedActivity.task == this) {
Andrii Kulian21713ac2016-10-12 22:05:05 -0700981 final Bitmap thumbnail = resumedActivity.screenshotActivityLocked();
Winsonc809cbb2015-11-02 12:06:15 -0800982 setLastThumbnailLocked(thumbnail);
Craig Mautner21d24a22014-04-23 11:45:37 -0700983 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700984 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700985 final TaskThumbnail taskThumbnail = new TaskThumbnail();
986 getLastThumbnail(taskThumbnail);
987 return taskThumbnail;
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700988 }
989
Andrii Kulian21713ac2016-10-12 22:05:05 -0700990 void removeTaskActivitiesLocked() {
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700991 // Just remove the entire task.
992 performClearTaskAtIndexLocked(0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700993 }
994
Craig Mautner432f64e2015-05-20 14:59:57 -0700995 String lockTaskAuthToString() {
996 switch (mLockTaskAuth) {
997 case LOCK_TASK_AUTH_DONT_LOCK: return "LOCK_TASK_AUTH_DONT_LOCK";
998 case LOCK_TASK_AUTH_PINNABLE: return "LOCK_TASK_AUTH_PINNABLE";
999 case LOCK_TASK_AUTH_LAUNCHABLE: return "LOCK_TASK_AUTH_LAUNCHABLE";
1000 case LOCK_TASK_AUTH_WHITELISTED: return "LOCK_TASK_AUTH_WHITELISTED";
Benjamin Franz469dd582015-06-09 14:24:36 +01001001 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV: return "LOCK_TASK_AUTH_LAUNCHABLE_PRIV";
Craig Mautner432f64e2015-05-20 14:59:57 -07001002 default: return "unknown=" + mLockTaskAuth;
1003 }
1004 }
1005
Craig Mautner15df08a2015-04-01 12:17:18 -07001006 void setLockTaskAuth() {
Benjamin Franz469dd582015-06-09 14:24:36 +01001007 if (!mPrivileged &&
1008 (mLockTaskMode == LOCK_TASK_LAUNCH_MODE_ALWAYS ||
1009 mLockTaskMode == LOCK_TASK_LAUNCH_MODE_NEVER)) {
1010 // Non-priv apps are not allowed to use always or never, fall back to default
1011 mLockTaskMode = LOCK_TASK_LAUNCH_MODE_DEFAULT;
1012 }
Craig Mautner15df08a2015-04-01 12:17:18 -07001013 switch (mLockTaskMode) {
1014 case LOCK_TASK_LAUNCH_MODE_DEFAULT:
1015 mLockTaskAuth = isLockTaskWhitelistedLocked() ?
1016 LOCK_TASK_AUTH_WHITELISTED : LOCK_TASK_AUTH_PINNABLE;
1017 break;
1018
1019 case LOCK_TASK_LAUNCH_MODE_NEVER:
Benjamin Franz469dd582015-06-09 14:24:36 +01001020 mLockTaskAuth = LOCK_TASK_AUTH_DONT_LOCK;
Craig Mautner15df08a2015-04-01 12:17:18 -07001021 break;
1022
1023 case LOCK_TASK_LAUNCH_MODE_ALWAYS:
Benjamin Franz469dd582015-06-09 14:24:36 +01001024 mLockTaskAuth = LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -07001025 break;
1026
1027 case LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED:
1028 mLockTaskAuth = isLockTaskWhitelistedLocked() ?
1029 LOCK_TASK_AUTH_LAUNCHABLE : LOCK_TASK_AUTH_PINNABLE;
1030 break;
1031 }
Craig Mautner432f64e2015-05-20 14:59:57 -07001032 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "setLockTaskAuth: task=" + this +
1033 " mLockTaskAuth=" + lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07001034 }
1035
1036 boolean isLockTaskWhitelistedLocked() {
Benjamin Franz6fd84cc2015-08-06 18:09:03 +01001037 String pkg = (realActivity != null) ? realActivity.getPackageName() : null;
1038 if (pkg == null) {
Craig Mautner15df08a2015-04-01 12:17:18 -07001039 return false;
1040 }
1041 String[] packages = mService.mLockTaskPackages.get(userId);
1042 if (packages == null) {
1043 return false;
1044 }
1045 for (int i = packages.length - 1; i >= 0; --i) {
Benjamin Franz6fd84cc2015-08-06 18:09:03 +01001046 if (pkg.equals(packages[i])) {
Craig Mautner15df08a2015-04-01 12:17:18 -07001047 return true;
1048 }
1049 }
1050 return false;
1051 }
Wale Ogunwale74e26592016-02-05 11:48:37 -08001052
Craig Mautnera82aa092013-09-13 15:34:08 -07001053 boolean isHomeTask() {
Craig Mautner84984fa2014-06-19 11:19:20 -07001054 return taskType == HOME_ACTIVITY_TYPE;
Craig Mautnera82aa092013-09-13 15:34:08 -07001055 }
1056
Wale Ogunwale74e26592016-02-05 11:48:37 -08001057 boolean isRecentsTask() {
1058 return taskType == RECENTS_ACTIVITY_TYPE;
1059 }
1060
Craig Mautner86d67a42013-05-14 10:34:38 -07001061 boolean isApplicationTask() {
Craig Mautner84984fa2014-06-19 11:19:20 -07001062 return taskType == APPLICATION_ACTIVITY_TYPE;
1063 }
1064
1065 boolean isOverHomeStack() {
1066 return mTaskToReturnTo == HOME_ACTIVITY_TYPE || mTaskToReturnTo == RECENTS_ACTIVITY_TYPE;
Craig Mautner1602ec22013-05-12 10:24:27 -07001067 }
1068
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001069 boolean isResizeable() {
1070 return !isHomeTask() && (mService.mForceResizableActivities
Jorim Jaggi8202b2a2016-02-03 19:24:31 -08001071 || ActivityInfo.isResizeableMode(mResizeMode)) && !mTemporarilyUnresizable;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001072 }
1073
Jiaquan Hedd1e66f2016-06-15 15:15:12 -07001074 boolean isOnTopLauncher() {
1075 return isHomeTask() && mIsOnTopLauncher;
1076 }
1077
Wale Ogunwale513346d2016-01-27 10:55:01 -08001078 boolean canGoInDockedStack() {
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001079 return isResizeable();
Wale Ogunwale513346d2016-01-27 10:55:01 -08001080 }
1081
Craig Mautner525f3d92013-05-07 14:01:50 -07001082 /**
1083 * Find the activity in the history stack within the given task. Returns
1084 * the index within the history at which it's found, or < 0 if not found.
1085 */
1086 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r) {
1087 final ComponentName realActivity = r.realActivity;
1088 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
1089 ActivityRecord candidate = mActivities.get(activityNdx);
1090 if (candidate.finishing) {
1091 continue;
1092 }
1093 if (candidate.realActivity.equals(realActivity)) {
1094 return candidate;
1095 }
1096 }
1097 return null;
1098 }
1099
Winson Chunga449dc02014-05-16 11:15:04 -07001100 /** Updates the last task description values. */
1101 void updateTaskDescription() {
1102 // Traverse upwards looking for any break between main task activities and
1103 // utility activities.
1104 int activityNdx;
1105 final int numActivities = mActivities.size();
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001106 final boolean relinquish = numActivities != 0 &&
1107 (mActivities.get(0).info.flags & FLAG_RELINQUISH_TASK_IDENTITY) != 0;
Winson Chunga449dc02014-05-16 11:15:04 -07001108 for (activityNdx = Math.min(numActivities, 1); activityNdx < numActivities;
Craig Mautner21d24a22014-04-23 11:45:37 -07001109 ++activityNdx) {
Winson Chunga449dc02014-05-16 11:15:04 -07001110 final ActivityRecord r = mActivities.get(activityNdx);
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001111 if (relinquish && (r.info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001112 // This will be the top activity for determining taskDescription. Pre-inc to
1113 // overcome initial decrement below.
1114 ++activityNdx;
1115 break;
1116 }
Winson Chunga449dc02014-05-16 11:15:04 -07001117 if (r.intent != null &&
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001118 (r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Winson Chunga449dc02014-05-16 11:15:04 -07001119 break;
1120 }
1121 }
1122 if (activityNdx > 0) {
1123 // Traverse downwards starting below break looking for set label, icon.
1124 // Note that if there are activities in the task but none of them set the
1125 // recent activity values, then we do not fall back to the last set
1126 // values in the TaskRecord.
1127 String label = null;
Craig Mautner648f69b2014-09-18 14:16:26 -07001128 String iconFilename = null;
Winson Chunga449dc02014-05-16 11:15:04 -07001129 int colorPrimary = 0;
Winson Chung1af8eda2016-02-05 17:55:56 +00001130 int colorBackground = 0;
Winson Chunga449dc02014-05-16 11:15:04 -07001131 for (--activityNdx; activityNdx >= 0; --activityNdx) {
1132 final ActivityRecord r = mActivities.get(activityNdx);
1133 if (r.taskDescription != null) {
1134 if (label == null) {
1135 label = r.taskDescription.getLabel();
1136 }
Craig Mautner648f69b2014-09-18 14:16:26 -07001137 if (iconFilename == null) {
1138 iconFilename = r.taskDescription.getIconFilename();
Winson Chunga449dc02014-05-16 11:15:04 -07001139 }
1140 if (colorPrimary == 0) {
1141 colorPrimary = r.taskDescription.getPrimaryColor();
Winson Chunga449dc02014-05-16 11:15:04 -07001142 }
Winson Chung1af8eda2016-02-05 17:55:56 +00001143 if (colorBackground == 0) {
1144 colorBackground = r.taskDescription.getBackgroundColor();
1145 }
Winson Chunga449dc02014-05-16 11:15:04 -07001146 }
1147 }
Winson Chung1af8eda2016-02-05 17:55:56 +00001148 lastTaskDescription = new TaskDescription(label, null, iconFilename, colorPrimary,
1149 colorBackground);
Winson Chungec396d62014-08-06 17:08:00 -07001150 // Update the task affiliation color if we are the parent of the group
1151 if (taskId == mAffiliatedTaskId) {
1152 mAffiliatedTaskColor = lastTaskDescription.getPrimaryColor();
1153 }
Winson Chunga449dc02014-05-16 11:15:04 -07001154 }
1155 }
1156
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001157 int findEffectiveRootIndex() {
Craig Mautner4767f4b2014-09-18 15:38:33 -07001158 int effectiveNdx = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001159 final int topActivityNdx = mActivities.size() - 1;
Dianne Hackborn39569af2014-09-23 10:56:58 -07001160 for (int activityNdx = 0; activityNdx <= topActivityNdx; ++activityNdx) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001161 final ActivityRecord r = mActivities.get(activityNdx);
1162 if (r.finishing) {
1163 continue;
1164 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001165 effectiveNdx = activityNdx;
Wale Ogunwale3eadad72016-10-13 09:16:59 -07001166 if ((r.info.flags & FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001167 break;
1168 }
1169 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001170 return effectiveNdx;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001171 }
1172
1173 void updateEffectiveIntent() {
1174 final int effectiveRootIndex = findEffectiveRootIndex();
1175 final ActivityRecord r = mActivities.get(effectiveRootIndex);
Winson Chungfee26772014-08-05 12:21:52 -07001176 setIntent(r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001177 }
1178
Craig Mautner21d24a22014-04-23 11:45:37 -07001179 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001180 if (DEBUG_RECENTS) Slog.i(TAG_RECENTS, "Saving task=" + this);
Craig Mautner21d24a22014-04-23 11:45:37 -07001181
1182 out.attribute(null, ATTR_TASKID, String.valueOf(taskId));
1183 if (realActivity != null) {
1184 out.attribute(null, ATTR_REALACTIVITY, realActivity.flattenToShortString());
1185 }
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001186 out.attribute(null, ATTR_REALACTIVITY_SUSPENDED, String.valueOf(realActivitySuspended));
Craig Mautner21d24a22014-04-23 11:45:37 -07001187 if (origActivity != null) {
1188 out.attribute(null, ATTR_ORIGACTIVITY, origActivity.flattenToShortString());
1189 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001190 // Write affinity, and root affinity if it is different from affinity.
1191 // We use the special string "@" for a null root affinity, so we can identify
1192 // later whether we were given a root affinity or should just make it the
1193 // same as the affinity.
Craig Mautner21d24a22014-04-23 11:45:37 -07001194 if (affinity != null) {
1195 out.attribute(null, ATTR_AFFINITY, affinity);
Dianne Hackborn79228822014-09-16 11:11:23 -07001196 if (!affinity.equals(rootAffinity)) {
1197 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
1198 }
1199 } else if (rootAffinity != null) {
1200 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
Craig Mautner21d24a22014-04-23 11:45:37 -07001201 }
1202 out.attribute(null, ATTR_ROOTHASRESET, String.valueOf(rootWasReset));
Dianne Hackborn13420f22014-07-18 15:43:56 -07001203 out.attribute(null, ATTR_AUTOREMOVERECENTS, String.valueOf(autoRemoveRecents));
Craig Mautner21d24a22014-04-23 11:45:37 -07001204 out.attribute(null, ATTR_ASKEDCOMPATMODE, String.valueOf(askedCompatMode));
1205 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001206 out.attribute(null, ATTR_USER_SETUP_COMPLETE, String.valueOf(mUserSetupComplete));
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001207 out.attribute(null, ATTR_EFFECTIVE_UID, String.valueOf(effectiveUid));
Craig Mautner21d24a22014-04-23 11:45:37 -07001208 out.attribute(null, ATTR_TASKTYPE, String.valueOf(taskType));
Winson Chungffa2ec62014-07-03 15:54:42 -07001209 out.attribute(null, ATTR_FIRSTACTIVETIME, String.valueOf(firstActiveTime));
Winson Chungf1fbd772014-06-24 18:06:58 -07001210 out.attribute(null, ATTR_LASTACTIVETIME, String.valueOf(lastActiveTime));
Craig Mautner21d24a22014-04-23 11:45:37 -07001211 out.attribute(null, ATTR_LASTTIMEMOVED, String.valueOf(mLastTimeMoved));
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001212 out.attribute(null, ATTR_NEVERRELINQUISH, String.valueOf(mNeverRelinquishIdentity));
Craig Mautner21d24a22014-04-23 11:45:37 -07001213 if (lastDescription != null) {
1214 out.attribute(null, ATTR_LASTDESCRIPTION, lastDescription.toString());
1215 }
Winson Chung2cb86c72014-06-25 12:03:30 -07001216 if (lastTaskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07001217 lastTaskDescription.saveToXml(out);
Winson Chung2cb86c72014-06-25 12:03:30 -07001218 }
Winson Chung36f3f032016-09-08 23:29:43 +00001219 mLastThumbnailInfo.saveToXml(out);
Winson Chungec396d62014-08-06 17:08:00 -07001220 out.attribute(null, ATTR_TASK_AFFILIATION_COLOR, String.valueOf(mAffiliatedTaskColor));
Craig Mautnera228ae92014-07-09 05:44:55 -07001221 out.attribute(null, ATTR_TASK_AFFILIATION, String.valueOf(mAffiliatedTaskId));
1222 out.attribute(null, ATTR_PREV_AFFILIATION, String.valueOf(mPrevAffiliateTaskId));
1223 out.attribute(null, ATTR_NEXT_AFFILIATION, String.valueOf(mNextAffiliateTaskId));
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001224 out.attribute(null, ATTR_CALLING_UID, String.valueOf(mCallingUid));
1225 out.attribute(null, ATTR_CALLING_PACKAGE, mCallingPackage == null ? "" : mCallingPackage);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001226 out.attribute(null, ATTR_RESIZE_MODE, String.valueOf(mResizeMode));
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001227 out.attribute(null, ATTR_PRIVILEGED, String.valueOf(mPrivileged));
Wale Ogunwale706ed792015-08-02 10:29:44 -07001228 if (mLastNonFullscreenBounds != null) {
1229 out.attribute(
1230 null, ATTR_NON_FULLSCREEN_BOUNDS, mLastNonFullscreenBounds.flattenToString());
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001231 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001232 out.attribute(null, ATTR_MIN_WIDTH, String.valueOf(mMinWidth));
1233 out.attribute(null, ATTR_MIN_HEIGHT, String.valueOf(mMinHeight));
Winson Chung2cb86c72014-06-25 12:03:30 -07001234
Craig Mautner21d24a22014-04-23 11:45:37 -07001235 if (affinityIntent != null) {
1236 out.startTag(null, TAG_AFFINITYINTENT);
1237 affinityIntent.saveToXml(out);
1238 out.endTag(null, TAG_AFFINITYINTENT);
1239 }
1240
Winson Chung36f3f032016-09-08 23:29:43 +00001241 out.startTag(null, TAG_INTENT);
1242 intent.saveToXml(out);
1243 out.endTag(null, TAG_INTENT);
Craig Mautner21d24a22014-04-23 11:45:37 -07001244
1245 final ArrayList<ActivityRecord> activities = mActivities;
1246 final int numActivities = activities.size();
1247 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1248 final ActivityRecord r = activities.get(activityNdx);
Craig Mautner43e52ed2014-06-16 17:18:52 -07001249 if (r.info.persistableMode == ActivityInfo.PERSIST_ROOT_ONLY || !r.isPersistable() ||
Wale Ogunwale843bfb92015-03-27 11:06:48 -07001250 ((r.intent.getFlags() & FLAG_ACTIVITY_NEW_DOCUMENT
1251 | FLAG_ACTIVITY_RETAIN_IN_RECENTS) == FLAG_ACTIVITY_NEW_DOCUMENT) &&
Craig Mautner43e52ed2014-06-16 17:18:52 -07001252 activityNdx > 0) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001253 // Stop at first non-persistable or first break in task (CLEAR_WHEN_TASK_RESET).
Craig Mautner21d24a22014-04-23 11:45:37 -07001254 break;
1255 }
1256 out.startTag(null, TAG_ACTIVITY);
1257 r.saveToXml(out);
1258 out.endTag(null, TAG_ACTIVITY);
1259 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001260 }
1261
Winson Chung36f3f032016-09-08 23:29:43 +00001262 static TaskRecord restoreFromXml(XmlPullParser in, ActivityStackSupervisor stackSupervisor)
1263 throws IOException, XmlPullParserException {
Craig Mautner21d24a22014-04-23 11:45:37 -07001264 Intent intent = null;
1265 Intent affinityIntent = null;
Winsonc809cbb2015-11-02 12:06:15 -08001266 ArrayList<ActivityRecord> activities = new ArrayList<>();
Craig Mautner21d24a22014-04-23 11:45:37 -07001267 ComponentName realActivity = null;
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001268 boolean realActivitySuspended = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001269 ComponentName origActivity = null;
1270 String affinity = null;
Dianne Hackborn79228822014-09-16 11:11:23 -07001271 String rootAffinity = null;
1272 boolean hasRootAffinity = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001273 boolean rootHasReset = false;
Dianne Hackborn13420f22014-07-18 15:43:56 -07001274 boolean autoRemoveRecents = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001275 boolean askedCompatMode = false;
1276 int taskType = ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner21d24a22014-04-23 11:45:37 -07001277 int userId = 0;
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001278 boolean userSetupComplete = true;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001279 int effectiveUid = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -07001280 String lastDescription = null;
Winson Chungffa2ec62014-07-03 15:54:42 -07001281 long firstActiveTime = -1;
Winson Chung2cb86c72014-06-25 12:03:30 -07001282 long lastActiveTime = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -07001283 long lastTimeOnTop = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001284 boolean neverRelinquishIdentity = true;
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001285 int taskId = INVALID_TASK_ID;
Craig Mautner21d24a22014-04-23 11:45:37 -07001286 final int outerDepth = in.getDepth();
Craig Mautner648f69b2014-09-18 14:16:26 -07001287 TaskDescription taskDescription = new TaskDescription();
Winsonc809cbb2015-11-02 12:06:15 -08001288 TaskThumbnailInfo thumbnailInfo = new TaskThumbnailInfo();
Wale Ogunwale18795a22014-12-03 11:38:33 -08001289 int taskAffiliation = INVALID_TASK_ID;
Winson Chungec396d62014-08-06 17:08:00 -07001290 int taskAffiliationColor = 0;
Wale Ogunwale18795a22014-12-03 11:38:33 -08001291 int prevTaskId = INVALID_TASK_ID;
1292 int nextTaskId = INVALID_TASK_ID;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001293 int callingUid = -1;
1294 String callingPackage = "";
Wale Ogunwaled829d362016-02-10 19:24:49 -08001295 int resizeMode = RESIZE_MODE_FORCE_RESIZEABLE;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001296 boolean privileged = false;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001297 Rect bounds = null;
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001298 int minWidth = INVALID_MIN_SIZE;
1299 int minHeight = INVALID_MIN_SIZE;
Craig Mautner21d24a22014-04-23 11:45:37 -07001300
1301 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
1302 final String attrName = in.getAttributeName(attrNdx);
1303 final String attrValue = in.getAttributeValue(attrNdx);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001304 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: attribute name=" +
1305 attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001306 if (ATTR_TASKID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001307 if (taskId == INVALID_TASK_ID) taskId = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001308 } else if (ATTR_REALACTIVITY.equals(attrName)) {
1309 realActivity = ComponentName.unflattenFromString(attrValue);
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001310 } else if (ATTR_REALACTIVITY_SUSPENDED.equals(attrName)) {
1311 realActivitySuspended = Boolean.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001312 } else if (ATTR_ORIGACTIVITY.equals(attrName)) {
1313 origActivity = ComponentName.unflattenFromString(attrValue);
1314 } else if (ATTR_AFFINITY.equals(attrName)) {
1315 affinity = attrValue;
Dianne Hackborn79228822014-09-16 11:11:23 -07001316 } else if (ATTR_ROOT_AFFINITY.equals(attrName)) {
1317 rootAffinity = attrValue;
1318 hasRootAffinity = true;
Craig Mautner21d24a22014-04-23 11:45:37 -07001319 } else if (ATTR_ROOTHASRESET.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001320 rootHasReset = Boolean.parseBoolean(attrValue);
Dianne Hackborn13420f22014-07-18 15:43:56 -07001321 } else if (ATTR_AUTOREMOVERECENTS.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001322 autoRemoveRecents = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001323 } else if (ATTR_ASKEDCOMPATMODE.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001324 askedCompatMode = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001325 } else if (ATTR_USERID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001326 userId = Integer.parseInt(attrValue);
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001327 } else if (ATTR_USER_SETUP_COMPLETE.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001328 userSetupComplete = Boolean.parseBoolean(attrValue);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001329 } else if (ATTR_EFFECTIVE_UID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001330 effectiveUid = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001331 } else if (ATTR_TASKTYPE.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001332 taskType = Integer.parseInt(attrValue);
Winson Chungffa2ec62014-07-03 15:54:42 -07001333 } else if (ATTR_FIRSTACTIVETIME.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001334 firstActiveTime = Long.parseLong(attrValue);
Winson Chungf1fbd772014-06-24 18:06:58 -07001335 } else if (ATTR_LASTACTIVETIME.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001336 lastActiveTime = Long.parseLong(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001337 } else if (ATTR_LASTDESCRIPTION.equals(attrName)) {
1338 lastDescription = attrValue;
1339 } else if (ATTR_LASTTIMEMOVED.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001340 lastTimeOnTop = Long.parseLong(attrValue);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001341 } else if (ATTR_NEVERRELINQUISH.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001342 neverRelinquishIdentity = Boolean.parseBoolean(attrValue);
Winsonc809cbb2015-11-02 12:06:15 -08001343 } else if (attrName.startsWith(TaskThumbnailInfo.ATTR_TASK_THUMBNAILINFO_PREFIX)) {
1344 thumbnailInfo.restoreFromXml(attrName, attrValue);
Craig Mautner648f69b2014-09-18 14:16:26 -07001345 } else if (attrName.startsWith(TaskDescription.ATTR_TASKDESCRIPTION_PREFIX)) {
1346 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001347 } else if (ATTR_TASK_AFFILIATION.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001348 taskAffiliation = Integer.parseInt(attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001349 } else if (ATTR_PREV_AFFILIATION.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001350 prevTaskId = Integer.parseInt(attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001351 } else if (ATTR_NEXT_AFFILIATION.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001352 nextTaskId = Integer.parseInt(attrValue);
Winson Chungec396d62014-08-06 17:08:00 -07001353 } else if (ATTR_TASK_AFFILIATION_COLOR.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001354 taskAffiliationColor = Integer.parseInt(attrValue);
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001355 } else if (ATTR_CALLING_UID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001356 callingUid = Integer.parseInt(attrValue);
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001357 } else if (ATTR_CALLING_PACKAGE.equals(attrName)) {
1358 callingPackage = attrValue;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001359 } else if (ATTR_RESIZE_MODE.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01001360 resizeMode = Integer.parseInt(attrValue);
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001361 } else if (ATTR_PRIVILEGED.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01001362 privileged = Boolean.parseBoolean(attrValue);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001363 } else if (ATTR_NON_FULLSCREEN_BOUNDS.equals(attrName)) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001364 bounds = Rect.unflattenFromString(attrValue);
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001365 } else if (ATTR_MIN_WIDTH.equals(attrName)) {
1366 minWidth = Integer.parseInt(attrValue);
1367 } else if (ATTR_MIN_HEIGHT.equals(attrName)) {
1368 minHeight = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001369 } else {
1370 Slog.w(TAG, "TaskRecord: Unknown attribute=" + attrName);
1371 }
1372 }
1373
1374 int event;
1375 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
Ben Kwa8814cf42015-07-08 10:54:56 -07001376 (event != XmlPullParser.END_TAG || in.getDepth() >= outerDepth)) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001377 if (event == XmlPullParser.START_TAG) {
1378 final String name = in.getName();
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001379 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: START_TAG name=" +
1380 name);
Craig Mautner21d24a22014-04-23 11:45:37 -07001381 if (TAG_AFFINITYINTENT.equals(name)) {
1382 affinityIntent = Intent.restoreFromXml(in);
1383 } else if (TAG_INTENT.equals(name)) {
1384 intent = Intent.restoreFromXml(in);
1385 } else if (TAG_ACTIVITY.equals(name)) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001386 ActivityRecord activity = ActivityRecord.restoreFromXml(in, stackSupervisor);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001387 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: activity=" +
1388 activity);
Craig Mautner21d24a22014-04-23 11:45:37 -07001389 if (activity != null) {
1390 activities.add(activity);
1391 }
1392 } else {
1393 Slog.e(TAG, "restoreTask: Unexpected name=" + name);
1394 XmlUtils.skipCurrentTag(in);
1395 }
1396 }
1397 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001398 if (!hasRootAffinity) {
1399 rootAffinity = affinity;
1400 } else if ("@".equals(rootAffinity)) {
1401 rootAffinity = null;
1402 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001403 if (effectiveUid <= 0) {
1404 Intent checkIntent = intent != null ? intent : affinityIntent;
1405 effectiveUid = 0;
1406 if (checkIntent != null) {
1407 IPackageManager pm = AppGlobals.getPackageManager();
1408 try {
1409 ApplicationInfo ai = pm.getApplicationInfo(
1410 checkIntent.getComponent().getPackageName(),
1411 PackageManager.GET_UNINSTALLED_PACKAGES
1412 | PackageManager.GET_DISABLED_COMPONENTS, userId);
1413 if (ai != null) {
1414 effectiveUid = ai.uid;
1415 }
1416 } catch (RemoteException e) {
1417 }
1418 }
1419 Slog.w(TAG, "Updating task #" + taskId + " for " + checkIntent
1420 + ": effectiveUid=" + effectiveUid);
1421 }
1422
Winson Chung36f3f032016-09-08 23:29:43 +00001423 final TaskRecord task = new TaskRecord(stackSupervisor.mService, taskId, intent,
Dianne Hackborn79228822014-09-16 11:11:23 -07001424 affinityIntent, affinity, rootAffinity, realActivity, origActivity, rootHasReset,
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001425 autoRemoveRecents, askedCompatMode, taskType, userId, effectiveUid, lastDescription,
1426 activities, firstActiveTime, lastActiveTime, lastTimeOnTop, neverRelinquishIdentity,
Winsonc809cbb2015-11-02 12:06:15 -08001427 taskDescription, thumbnailInfo, taskAffiliation, prevTaskId, nextTaskId,
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001428 taskAffiliationColor, callingUid, callingPackage, resizeMode, privileged,
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001429 realActivitySuspended, userSetupComplete, minWidth, minHeight);
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07001430 task.updateOverrideConfiguration(bounds);
Craig Mautner21d24a22014-04-23 11:45:37 -07001431
1432 for (int activityNdx = activities.size() - 1; activityNdx >=0; --activityNdx) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001433 activities.get(activityNdx).task = task;
Craig Mautner21d24a22014-04-23 11:45:37 -07001434 }
1435
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001436 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Restored task=" + task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001437 return task;
1438 }
1439
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001440 private void adjustForMinimalTaskDimensions(Rect bounds) {
1441 if (bounds == null) {
1442 return;
1443 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001444 int minWidth = mMinWidth;
1445 int minHeight = mMinHeight;
Robert Carr9c5867d2016-03-10 15:52:46 -08001446 // If the task has no requested minimal size, we'd like to enforce a minimal size
1447 // so that the user can not render the task too small to manipulate. We don't need
1448 // to do this for the pinned stack as the bounds are controlled by the system.
Andrii Kulian02b7a832016-10-06 23:11:56 -07001449 if (getStackId() != PINNED_STACK_ID) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001450 if (minWidth == INVALID_MIN_SIZE) {
1451 minWidth = mService.mStackSupervisor.mDefaultMinSizeOfResizeableTask;
Andrii Kulian2e751b82016-03-16 16:59:32 -07001452 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001453 if (minHeight == INVALID_MIN_SIZE) {
1454 minHeight = mService.mStackSupervisor.mDefaultMinSizeOfResizeableTask;
Andrii Kulian2e751b82016-03-16 16:59:32 -07001455 }
Robert Carr9c5867d2016-03-10 15:52:46 -08001456 }
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001457 final boolean adjustWidth = minWidth > bounds.width();
1458 final boolean adjustHeight = minHeight > bounds.height();
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001459 if (!(adjustWidth || adjustHeight)) {
1460 return;
1461 }
1462
1463 if (adjustWidth) {
1464 if (mBounds != null && bounds.right == mBounds.right) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001465 bounds.left = bounds.right - minWidth;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001466 } else {
1467 // Either left bounds match, or neither match, or the previous bounds were
1468 // fullscreen and we default to keeping left.
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001469 bounds.right = bounds.left + minWidth;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001470 }
1471 }
1472 if (adjustHeight) {
1473 if (mBounds != null && bounds.bottom == mBounds.bottom) {
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001474 bounds.top = bounds.bottom - minHeight;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001475 } else {
1476 // Either top bounds match, or neither match, or the previous bounds were
1477 // fullscreen and we default to keeping top.
Andrii Kulianf66a83d2016-05-17 12:17:44 -07001478 bounds.bottom = bounds.top + minHeight;
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001479 }
1480 }
1481 }
1482
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001483 /**
1484 * Update task's override configuration based on the bounds.
Jorim Jaggi0a932142016-02-01 17:42:25 -08001485 * @param bounds The bounds of the task.
Andrii Kulian8072d112016-09-16 11:11:01 -07001486 * @return True if the override configuration was updated.
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001487 */
Andrii Kulian8072d112016-09-16 11:11:01 -07001488 boolean updateOverrideConfiguration(Rect bounds) {
Jorim Jaggi0a932142016-02-01 17:42:25 -08001489 return updateOverrideConfiguration(bounds, null /* insetBounds */);
1490 }
1491
1492 /**
1493 * Update task's override configuration based on the bounds.
1494 * @param bounds The bounds of the task.
1495 * @param insetBounds The bounds used to calculate the system insets, which is used here to
1496 * subtract the navigation bar/status bar size from the screen size reported
1497 * to the application. See {@link IActivityManager#resizeDockedStack}.
Andrii Kulian8072d112016-09-16 11:11:01 -07001498 * @return True if the override configuration was updated.
Jorim Jaggi0a932142016-02-01 17:42:25 -08001499 */
Andrii Kulian8072d112016-09-16 11:11:01 -07001500 boolean updateOverrideConfiguration(Rect bounds, @Nullable Rect insetBounds) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001501 if (Objects.equals(mBounds, bounds)) {
Andrii Kulian8072d112016-09-16 11:11:01 -07001502 return false;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001503 }
Andrii Kulian1779e612016-10-12 21:58:25 -07001504 mTmpConfig.setTo(getOverrideConfiguration());
Wale Ogunwale5f986092015-12-04 15:35:38 -08001505 final boolean oldFullscreen = mFullscreen;
Andrii Kulian1779e612016-10-12 21:58:25 -07001506 final Configuration newConfig = getOverrideConfiguration();
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001507
1508 mFullscreen = bounds == null;
Wale Ogunwale706ed792015-08-02 10:29:44 -07001509 if (mFullscreen) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001510 if (mBounds != null && StackId.persistTaskBounds(mStack.mStackId)) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001511 mLastNonFullscreenBounds = mBounds;
1512 }
1513 mBounds = null;
Andrii Kulian1779e612016-10-12 21:58:25 -07001514 newConfig.unset();
Wale Ogunwale706ed792015-08-02 10:29:44 -07001515 } else {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001516 mTmpRect.set(bounds);
1517 adjustForMinimalTaskDimensions(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -08001518 if (mBounds == null) {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001519 mBounds = new Rect(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -08001520 } else {
Wale Ogunwale9a08f822016-02-17 19:03:58 -08001521 mBounds.set(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -08001522 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001523 if (mStack == null || StackId.persistTaskBounds(mStack.mStackId)) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001524 mLastNonFullscreenBounds = mBounds;
1525 }
Andrii Kulian1779e612016-10-12 21:58:25 -07001526 calculateOverrideConfig(newConfig, mTmpRect, insetBounds,
Andrii Kuliandaea3572016-04-08 13:20:51 -07001527 mTmpRect.right != bounds.right, mTmpRect.bottom != bounds.bottom);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001528 }
Andrii Kulian1779e612016-10-12 21:58:25 -07001529 onOverrideConfigurationChanged(newConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001530
1531 if (mFullscreen != oldFullscreen) {
Andrii Kulian933076d2016-03-29 17:04:42 -07001532 mService.mStackSupervisor.scheduleReportMultiWindowModeChanged(this);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001533 }
1534
Andrii Kulian1779e612016-10-12 21:58:25 -07001535 return !mTmpConfig.equals(newConfig);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001536 }
1537
Andrii Kuliandaea3572016-04-08 13:20:51 -07001538 private void subtractNonDecorInsets(Rect inOutBounds, Rect inInsetBounds,
1539 boolean overrideWidth, boolean overrideHeight) {
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001540 mTmpRect2.set(inInsetBounds);
1541 mService.mWindowManager.subtractNonDecorInsets(mTmpRect2);
1542 int leftInset = mTmpRect2.left - inInsetBounds.left;
1543 int topInset = mTmpRect2.top - inInsetBounds.top;
Andrii Kuliandaea3572016-04-08 13:20:51 -07001544 int rightInset = overrideWidth ? 0 : inInsetBounds.right - mTmpRect2.right;
1545 int bottomInset = overrideHeight ? 0 : inInsetBounds.bottom - mTmpRect2.bottom;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001546 inOutBounds.inset(leftInset, topInset, rightInset, bottomInset);
1547 }
1548
Andrii Kuliandaea3572016-04-08 13:20:51 -07001549 private void subtractStableInsets(Rect inOutBounds, Rect inInsetBounds,
1550 boolean overrideWidth, boolean overrideHeight) {
Jorim Jaggi0a932142016-02-01 17:42:25 -08001551 mTmpRect2.set(inInsetBounds);
1552 mService.mWindowManager.subtractStableInsets(mTmpRect2);
1553 int leftInset = mTmpRect2.left - inInsetBounds.left;
1554 int topInset = mTmpRect2.top - inInsetBounds.top;
Andrii Kuliandaea3572016-04-08 13:20:51 -07001555 int rightInset = overrideWidth ? 0 : inInsetBounds.right - mTmpRect2.right;
1556 int bottomInset = overrideHeight ? 0 : inInsetBounds.bottom - mTmpRect2.bottom;
Jorim Jaggi0a932142016-02-01 17:42:25 -08001557 inOutBounds.inset(leftInset, topInset, rightInset, bottomInset);
1558 }
1559
Andrii Kulian1779e612016-10-12 21:58:25 -07001560 /** Clears passed config and fills it with new override values. */
1561 private Configuration calculateOverrideConfig(Configuration config, Rect bounds,
1562 Rect insetBounds, boolean overrideWidth, boolean overrideHeight) {
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001563 mTmpNonDecorBounds.set(bounds);
1564 mTmpStableBounds.set(bounds);
1565 subtractNonDecorInsets(
Andrii Kuliandaea3572016-04-08 13:20:51 -07001566 mTmpNonDecorBounds, insetBounds != null ? insetBounds : bounds,
1567 overrideWidth, overrideHeight);
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001568 subtractStableInsets(
Andrii Kuliandaea3572016-04-08 13:20:51 -07001569 mTmpStableBounds, insetBounds != null ? insetBounds : bounds,
1570 overrideWidth, overrideHeight);
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001571
Andrii Kulian1779e612016-10-12 21:58:25 -07001572 // For calculating screenWidthDp, screenHeightDp, we use the stable inset screen area,
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001573 // i.e. the screen area without the system bars.
Andrii Kulian1779e612016-10-12 21:58:25 -07001574 // Additionally task dimensions should not be bigger than its parents dimensions.
1575 final Configuration parentConfig = getParent().getConfiguration();
1576 config.unset();
1577 final float density = parentConfig.densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE;
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08001578 config.screenWidthDp =
Andrii Kulian1779e612016-10-12 21:58:25 -07001579 Math.min((int)(mTmpStableBounds.width() / density), parentConfig.screenWidthDp);
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08001580 config.screenHeightDp =
Andrii Kulian1779e612016-10-12 21:58:25 -07001581 Math.min((int)(mTmpStableBounds.height() / density), parentConfig.screenHeightDp);
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001582
1583 // TODO: Orientation?
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08001584 config.orientation = (config.screenWidthDp <= config.screenHeightDp)
1585 ? Configuration.ORIENTATION_PORTRAIT
1586 : Configuration.ORIENTATION_LANDSCAPE;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001587
Jorim Jaggi85639432016-05-06 17:27:55 -07001588 // For calculating screen layout, we need to use the non-decor inset screen area for the
1589 // calculation for compatibility reasons, i.e. screen area without system bars that could
1590 // never go away in Honeycomb.
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001591 final int compatScreenWidthDp = (int)(mTmpNonDecorBounds.width() / density);
1592 final int compatScreenHeightDp = (int)(mTmpNonDecorBounds.height() / density);
Andrii Kulian1779e612016-10-12 21:58:25 -07001593 // We're only overriding LONG, SIZE and COMPAT parts of screenLayout, so we start override
1594 // calculation with partial default.
1595 final int sl = Configuration.SCREENLAYOUT_LONG_YES | Configuration.SCREENLAYOUT_SIZE_XLARGE;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001596 final int longSize = Math.max(compatScreenHeightDp, compatScreenWidthDp);
Andrii Kulian1779e612016-10-12 21:58:25 -07001597 final int shortSize = Math.min(compatScreenHeightDp, compatScreenWidthDp);
Jorim Jaggi85639432016-05-06 17:27:55 -07001598 config.screenLayout = Configuration.reduceScreenLayout(sl, longSize, shortSize);
1599
1600 config.smallestScreenWidthDp = mService.mWindowManager.getSmallestWidthForTaskBounds(
1601 insetBounds != null ? insetBounds : bounds);
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08001602 return config;
1603 }
1604
1605 /**
1606 * Using the existing configuration {@param config}, creates a new task override config such
1607 * that all the fields that are usually set in an override config are set to the ones in
1608 * {@param config}.
1609 */
1610 Configuration extractOverrideConfig(Configuration config) {
Andrii Kulian1779e612016-10-12 21:58:25 -07001611 final Configuration extracted = new Configuration();
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08001612 extracted.screenWidthDp = config.screenWidthDp;
1613 extracted.screenHeightDp = config.screenHeightDp;
1614 extracted.smallestScreenWidthDp = config.smallestScreenWidthDp;
1615 extracted.orientation = config.orientation;
Andrii Kulian1779e612016-10-12 21:58:25 -07001616 // We're only overriding LONG, SIZE and COMPAT parts of screenLayout.
1617 extracted.screenLayout = config.screenLayout & (Configuration.SCREENLAYOUT_LONG_MASK
1618 | Configuration.SCREENLAYOUT_SIZE_MASK | Configuration.SCREENLAYOUT_COMPAT_NEEDED);
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08001619 return extracted;
1620 }
1621
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001622 Rect updateOverrideConfigurationFromLaunchBounds() {
1623 final Rect bounds = validateBounds(getLaunchBounds());
1624 updateOverrideConfiguration(bounds);
Andrii Kulian73336d812016-03-24 12:56:08 -07001625 if (bounds != null) {
1626 bounds.set(mBounds);
1627 }
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001628 return bounds;
1629 }
1630
1631 static Rect validateBounds(Rect bounds) {
1632 if (bounds != null && bounds.isEmpty()) {
1633 Slog.wtf(TAG, "Received strange task bounds: " + bounds, new Throwable());
1634 return null;
1635 }
1636 return bounds;
1637 }
1638
Wale Ogunwale935e5022015-11-10 12:36:10 -08001639 /** Updates the task's bounds and override configuration to match what is expected for the
1640 * input stack. */
1641 void updateOverrideConfigurationForStack(ActivityStack inStack) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001642 if (mStack != null && mStack == inStack) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08001643 return;
1644 }
1645
1646 if (inStack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001647 if (!isResizeable()) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08001648 throw new IllegalArgumentException("Can not position non-resizeable task="
1649 + this + " in stack=" + inStack);
1650 }
1651 if (mBounds != null) {
1652 return;
1653 }
1654 if (mLastNonFullscreenBounds != null) {
1655 updateOverrideConfiguration(mLastNonFullscreenBounds);
1656 } else {
1657 inStack.layoutTaskInStack(this, null);
1658 }
1659 } else {
1660 updateOverrideConfiguration(inStack.mBounds);
1661 }
1662 }
1663
Chong Zhang0fa656b2015-08-31 15:17:21 -07001664 /**
1665 * Returns the correct stack to use based on task type and currently set bounds,
1666 * regardless of the focused stack and current stack association of the task.
1667 * The task will be moved (and stack focus changed) later if necessary.
1668 */
1669 int getLaunchStackId() {
1670 if (!isApplicationTask()) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001671 return HOME_STACK_ID;
1672 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001673 if (mBounds != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001674 return FREEFORM_WORKSPACE_STACK_ID;
1675 }
1676 return FULLSCREEN_WORKSPACE_STACK_ID;
1677 }
1678
1679 /** Returns the bounds that should be used to launch this task. */
1680 Rect getLaunchBounds() {
Chong Zhang75b37202015-12-04 14:16:36 -08001681 // If we're over lockscreen, forget about stack bounds and use fullscreen.
1682 if (mService.mLockScreenShown == LOCK_SCREEN_SHOWN) {
1683 return null;
1684 }
1685
Andrii Kulian02b7a832016-10-06 23:11:56 -07001686 if (mStack == null) {
Chong Zhang7d5f5102016-01-13 10:29:24 -08001687 return null;
1688 }
1689
Andrii Kulian02b7a832016-10-06 23:11:56 -07001690 final int stackId = mStack.mStackId;
Chong Zhang7d5f5102016-01-13 10:29:24 -08001691 if (stackId == HOME_STACK_ID
1692 || stackId == FULLSCREEN_WORKSPACE_STACK_ID
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001693 || (stackId == DOCKED_STACK_ID && !isResizeable())) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001694 return isResizeable() ? mStack.mBounds : null;
Wale Ogunwale3797c222015-10-27 14:21:58 -07001695 } else if (!StackId.persistTaskBounds(stackId)) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001696 return mStack.mBounds;
Wale Ogunwale706ed792015-08-02 10:29:44 -07001697 }
1698 return mLastNonFullscreenBounds;
1699 }
1700
Wale Ogunwale39381972015-12-17 17:15:29 -08001701 boolean canMatchRootAffinity() {
1702 // We don't allow root affinity matching on the pinned stack as no other task should
1703 // be launching in it based on affinity.
Andrii Kulian02b7a832016-10-06 23:11:56 -07001704 return rootAffinity != null && getStackId() != PINNED_STACK_ID;
Wale Ogunwale39381972015-12-17 17:15:29 -08001705 }
1706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001708 pw.print(prefix); pw.print("userId="); pw.print(userId);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001709 pw.print(" effectiveUid="); UserHandle.formatUid(pw, effectiveUid);
1710 pw.print(" mCallingUid="); UserHandle.formatUid(pw, mCallingUid);
Suprabh Shukla7745c142016-03-07 18:21:10 -08001711 pw.print(" mUserSetupComplete="); pw.print(mUserSetupComplete);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001712 pw.print(" mCallingPackage="); pw.println(mCallingPackage);
Dianne Hackborn79228822014-09-16 11:11:23 -07001713 if (affinity != null || rootAffinity != null) {
1714 pw.print(prefix); pw.print("affinity="); pw.print(affinity);
1715 if (affinity == null || !affinity.equals(rootAffinity)) {
1716 pw.print(" root="); pw.println(rootAffinity);
1717 } else {
1718 pw.println();
1719 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001720 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07001721 if (voiceSession != null || voiceInteractor != null) {
1722 pw.print(prefix); pw.print("VOICE: session=0x");
1723 pw.print(Integer.toHexString(System.identityHashCode(voiceSession)));
1724 pw.print(" interactor=0x");
1725 pw.println(Integer.toHexString(System.identityHashCode(voiceInteractor)));
1726 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001727 if (intent != null) {
1728 StringBuilder sb = new StringBuilder(128);
1729 sb.append(prefix); sb.append("intent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08001730 intent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001731 sb.append('}');
1732 pw.println(sb.toString());
1733 }
1734 if (affinityIntent != null) {
1735 StringBuilder sb = new StringBuilder(128);
1736 sb.append(prefix); sb.append("affinityIntent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08001737 affinityIntent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001738 sb.append('}');
1739 pw.println(sb.toString());
1740 }
1741 if (origActivity != null) {
1742 pw.print(prefix); pw.print("origActivity=");
1743 pw.println(origActivity.flattenToShortString());
1744 }
1745 if (realActivity != null) {
1746 pw.print(prefix); pw.print("realActivity=");
1747 pw.println(realActivity.flattenToShortString());
1748 }
Dianne Hackborn852975d2014-08-22 17:42:43 -07001749 if (autoRemoveRecents || isPersistable || taskType != 0 || mTaskToReturnTo != 0
1750 || numFullscreen != 0) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001751 pw.print(prefix); pw.print("autoRemoveRecents="); pw.print(autoRemoveRecents);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001752 pw.print(" isPersistable="); pw.print(isPersistable);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001753 pw.print(" numFullscreen="); pw.print(numFullscreen);
1754 pw.print(" taskType="); pw.print(taskType);
1755 pw.print(" mTaskToReturnTo="); pw.println(mTaskToReturnTo);
1756 }
Craig Mautner432f64e2015-05-20 14:59:57 -07001757 if (rootWasReset || mNeverRelinquishIdentity || mReuseTask
1758 || mLockTaskAuth != LOCK_TASK_AUTH_PINNABLE) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001759 pw.print(prefix); pw.print("rootWasReset="); pw.print(rootWasReset);
1760 pw.print(" mNeverRelinquishIdentity="); pw.print(mNeverRelinquishIdentity);
Craig Mautner432f64e2015-05-20 14:59:57 -07001761 pw.print(" mReuseTask="); pw.print(mReuseTask);
1762 pw.print(" mLockTaskAuth="); pw.println(lockTaskAuthToString());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001763 }
Wale Ogunwale18795a22014-12-03 11:38:33 -08001764 if (mAffiliatedTaskId != taskId || mPrevAffiliateTaskId != INVALID_TASK_ID
1765 || mPrevAffiliate != null || mNextAffiliateTaskId != INVALID_TASK_ID
1766 || mNextAffiliate != null) {
Dianne Hackborn852975d2014-08-22 17:42:43 -07001767 pw.print(prefix); pw.print("affiliation="); pw.print(mAffiliatedTaskId);
1768 pw.print(" prevAffiliation="); pw.print(mPrevAffiliateTaskId);
1769 pw.print(" (");
1770 if (mPrevAffiliate == null) {
1771 pw.print("null");
1772 } else {
1773 pw.print(Integer.toHexString(System.identityHashCode(mPrevAffiliate)));
1774 }
1775 pw.print(") nextAffiliation="); pw.print(mNextAffiliateTaskId);
1776 pw.print(" (");
1777 if (mNextAffiliate == null) {
1778 pw.print("null");
1779 } else {
1780 pw.print(Integer.toHexString(System.identityHashCode(mNextAffiliate)));
1781 }
1782 pw.println(")");
1783 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001784 pw.print(prefix); pw.print("Activities="); pw.println(mActivities);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001785 if (!askedCompatMode || !inRecents || !isAvailable) {
1786 pw.print(prefix); pw.print("askedCompatMode="); pw.print(askedCompatMode);
1787 pw.print(" inRecents="); pw.print(inRecents);
1788 pw.print(" isAvailable="); pw.println(isAvailable);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07001789 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001790 pw.print(prefix); pw.print("lastThumbnail="); pw.print(mLastThumbnail);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001791 pw.print(" lastThumbnailFile="); pw.println(mLastThumbnailFile);
1792 if (lastDescription != null) {
1793 pw.print(prefix); pw.print("lastDescription="); pw.println(lastDescription);
1794 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001795 pw.print(prefix); pw.print("stackId="); pw.println(getStackId());
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001796 pw.print(prefix + "hasBeenVisible=" + hasBeenVisible);
1797 pw.print(" mResizeMode=" + ActivityInfo.resizeModeToString(mResizeMode));
1798 pw.print(" isResizeable=" + isResizeable());
Winson Chung36f3f032016-09-08 23:29:43 +00001799 pw.print(" firstActiveTime=" + lastActiveTime);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001800 pw.print(" lastActiveTime=" + lastActiveTime);
1801 pw.println(" (inactive for " + (getInactiveDuration() / 1000) + "s)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 }
1803
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001804 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001806 StringBuilder sb = new StringBuilder(128);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001807 if (stringName != null) {
1808 sb.append(stringName);
1809 sb.append(" U=");
1810 sb.append(userId);
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08001811 sb.append(" StackId=");
Andrii Kulian02b7a832016-10-06 23:11:56 -07001812 sb.append(getStackId());
Craig Mautnerde4ef022013-04-07 19:01:33 -07001813 sb.append(" sz=");
1814 sb.append(mActivities.size());
1815 sb.append('}');
1816 return sb.toString();
1817 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001818 sb.append("TaskRecord{");
1819 sb.append(Integer.toHexString(System.identityHashCode(this)));
1820 sb.append(" #");
1821 sb.append(taskId);
1822 if (affinity != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001823 sb.append(" A=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001824 sb.append(affinity);
1825 } else if (intent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001826 sb.append(" I=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001827 sb.append(intent.getComponent().flattenToShortString());
1828 } else if (affinityIntent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001829 sb.append(" aI=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001830 sb.append(affinityIntent.getComponent().flattenToShortString());
1831 } else {
1832 sb.append(" ??");
1833 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001834 stringName = sb.toString();
1835 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 }
1837}