blob: c14cfefbccacdcadd543ecb27b176796b36726d4 [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
Chong Zhang7d5f5102016-01-13 10:29:24 -080019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070020import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
21import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
22import static android.app.ActivityManager.StackId.HOME_STACK_ID;
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -080023import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Wale Ogunwale5f986092015-12-04 15:35:38 -080024import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwale843bfb92015-03-27 11:06:48 -070025import static android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
26import static android.content.Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080027import static android.content.pm.ActivityInfo.RESIZE_MODE_CROP_WINDOWS;
Wale Ogunwaled26176f2016-01-25 20:04:04 -080028import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -080029import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Craig Mautner15df08a2015-04-01 12:17:18 -070030import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_ALWAYS;
31import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_DEFAULT;
32import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
33import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_NEVER;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -070034import static android.content.pm.ApplicationInfo.PRIVATE_FLAG_PRIVILEGED;
Suprabh Shukla23593142015-11-03 17:31:15 -080035import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ADD_REMOVE;
36import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
37import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
38import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
39import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_ADD_REMOVE;
40import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
41import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
42import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
43import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
44import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Chong Zhang75b37202015-12-04 14:16:36 -080045import static com.android.server.am.ActivityManagerService.LOCK_SCREEN_SHOWN;
Craig Mautner84984fa2014-06-19 11:19:20 -070046import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Suprabh Shukla23593142015-11-03 17:31:15 -080047import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070048import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
Craig Mautnerde4ef022013-04-07 19:01:33 -070049
Craig Mautnerb0f7dc72013-04-01 16:34:45 -070050import android.app.Activity;
Craig Mautner9db9a0b2013-04-29 17:05:56 -070051import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070052import android.app.ActivityManager.StackId;
Craig Mautner648f69b2014-09-18 14:16:26 -070053import android.app.ActivityManager.TaskDescription;
Wale Ogunwaleff3c66c2015-11-18 19:22:49 -080054import android.app.ActivityManager.TaskThumbnail;
Winsonc809cbb2015-11-02 12:06:15 -080055import android.app.ActivityManager.TaskThumbnailInfo;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -070056import android.app.ActivityOptions;
Dianne Hackborn885fbe52014-08-23 15:23:58 -070057import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.content.ComponentName;
59import android.content.Intent;
60import android.content.pm.ActivityInfo;
Dianne Hackborn885fbe52014-08-23 15:23:58 -070061import android.content.pm.ApplicationInfo;
62import android.content.pm.IPackageManager;
63import android.content.pm.PackageManager;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070064import android.content.res.Configuration;
Craig Mautner9db9a0b2013-04-29 17:05:56 -070065import android.graphics.Bitmap;
Winsonc809cbb2015-11-02 12:06:15 -080066import android.graphics.Point;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070067import android.graphics.Rect;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070068import android.os.Debug;
Craig Mautnerc0ffce52014-07-01 12:38:52 -070069import android.os.ParcelFileDescriptor;
Dianne Hackborn885fbe52014-08-23 15:23:58 -070070import android.os.RemoteException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070071import android.os.UserHandle;
Dianne Hackborn91097de2014-04-04 18:02:06 -070072import android.service.voice.IVoiceInteractionSession;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070073import android.util.DisplayMetrics;
Dianne Hackborn7f96b792012-05-29 18:46:45 -070074import android.util.Slog;
Suprabh Shukla23593142015-11-03 17:31:15 -080075
Dianne Hackborn91097de2014-04-04 18:02:06 -070076import com.android.internal.app.IVoiceInteractor;
Craig Mautner21d24a22014-04-23 11:45:37 -070077import com.android.internal.util.XmlUtils;
Suprabh Shukla23593142015-11-03 17:31:15 -080078
Craig Mautner21d24a22014-04-23 11:45:37 -070079import org.xmlpull.v1.XmlPullParser;
80import org.xmlpull.v1.XmlPullParserException;
81import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
Craig Mautnerc0ffce52014-07-01 12:38:52 -070083import java.io.File;
Craig Mautner21d24a22014-04-23 11:45:37 -070084import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import java.io.PrintWriter;
Craig Mautner5d9c7be2013-02-15 14:02:56 -080086import java.util.ArrayList;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070087import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
Craig Mautnerc0ffce52014-07-01 12:38:52 -070089final class TaskRecord {
Wale Ogunwalee23149f2015-03-06 15:39:44 -080090 private static final String TAG = TAG_WITH_CLASS_NAME ? "TaskRecord" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070091 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070092 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Craig Mautnere0570202015-05-13 13:06:11 -070093 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070094 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080095
Wale Ogunwale18795a22014-12-03 11:38:33 -080096 static final String ATTR_TASKID = "task_id";
Craig Mautner21d24a22014-04-23 11:45:37 -070097 private static final String TAG_INTENT = "intent";
98 private static final String TAG_AFFINITYINTENT = "affinity_intent";
Wale Ogunwale18795a22014-12-03 11:38:33 -080099 static final String ATTR_REALACTIVITY = "real_activity";
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +0000100 static final String ATTR_REALACTIVITY_SUSPENDED = "real_activity_suspended";
Craig Mautner21d24a22014-04-23 11:45:37 -0700101 private static final String ATTR_ORIGACTIVITY = "orig_activity";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700102 private static final String TAG_ACTIVITY = "activity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700103 private static final String ATTR_AFFINITY = "affinity";
Dianne Hackborn79228822014-09-16 11:11:23 -0700104 private static final String ATTR_ROOT_AFFINITY = "root_affinity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700105 private static final String ATTR_ROOTHASRESET = "root_has_reset";
Dianne Hackborn13420f22014-07-18 15:43:56 -0700106 private static final String ATTR_AUTOREMOVERECENTS = "auto_remove_recents";
Craig Mautner21d24a22014-04-23 11:45:37 -0700107 private static final String ATTR_ASKEDCOMPATMODE = "asked_compat_mode";
108 private static final String ATTR_USERID = "user_id";
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700109 private static final String ATTR_EFFECTIVE_UID = "effective_uid";
Craig Mautner21d24a22014-04-23 11:45:37 -0700110 private static final String ATTR_TASKTYPE = "task_type";
Winson Chungffa2ec62014-07-03 15:54:42 -0700111 private static final String ATTR_FIRSTACTIVETIME = "first_active_time";
Winson Chungf1fbd772014-06-24 18:06:58 -0700112 private static final String ATTR_LASTACTIVETIME = "last_active_time";
Craig Mautner21d24a22014-04-23 11:45:37 -0700113 private static final String ATTR_LASTDESCRIPTION = "last_description";
114 private static final String ATTR_LASTTIMEMOVED = "last_time_moved";
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700115 private static final String ATTR_NEVERRELINQUISH = "never_relinquish_identity";
Wale Ogunwale18795a22014-12-03 11:38:33 -0800116 static final String ATTR_TASK_AFFILIATION = "task_affiliation";
Craig Mautnera228ae92014-07-09 05:44:55 -0700117 private static final String ATTR_PREV_AFFILIATION = "prev_affiliation";
118 private static final String ATTR_NEXT_AFFILIATION = "next_affiliation";
Winson Chungec396d62014-08-06 17:08:00 -0700119 private static final String ATTR_TASK_AFFILIATION_COLOR = "task_affiliation_color";
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700120 private static final String ATTR_CALLING_UID = "calling_uid";
121 private static final String ATTR_CALLING_PACKAGE = "calling_package";
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800122 // TODO(b/26847884): Currently needed while migrating to resize_mode.
123 // Can be removed at some later point.
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800124 private static final String ATTR_RESIZEABLE = "resizeable";
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800125 private static final String ATTR_RESIZE_MODE = "resize_mode";
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700126 private static final String ATTR_PRIVILEGED = "privileged";
Wale Ogunwale706ed792015-08-02 10:29:44 -0700127 private static final String ATTR_NON_FULLSCREEN_BOUNDS = "non_fullscreen_bounds";
Craig Mautner21d24a22014-04-23 11:45:37 -0700128
129 private static final String TASK_THUMBNAIL_SUFFIX = "_task_thumbnail";
130
Craig Mautner0b633fc2014-07-23 10:42:18 -0700131 static final boolean IGNORE_RETURN_TO_RECENTS = true;
132
Wale Ogunwale18795a22014-12-03 11:38:33 -0800133 static final int INVALID_TASK_ID = -1;
134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 final int taskId; // Unique identifier for this task.
Dianne Hackborn79228822014-09-16 11:11:23 -0700136 String affinity; // The affinity name for this task, or null; may change identity.
137 String rootAffinity; // Initial base affinity, or null; does not change from initial root.
Dianne Hackborn91097de2014-04-04 18:02:06 -0700138 final IVoiceInteractionSession voiceSession; // Voice interaction session driving task
139 final IVoiceInteractor voiceInteractor; // Associated interactor to provide to app
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 Intent intent; // The original intent that started the task.
141 Intent affinityIntent; // Intent of affinity-moved activity that started this task.
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700142 int effectiveUid; // The current effective uid of the identity of this task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 ComponentName origActivity; // The non-alias activity component of the intent.
144 ComponentName realActivity; // The actual activity component that started the task.
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +0000145 boolean realActivitySuspended; // True if the actual activity component that started the
146 // task is suspended.
Winson Chungffa2ec62014-07-03 15:54:42 -0700147 long firstActiveTime; // First time this task was active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 long lastActiveTime; // Last time this task was active, including sleep.
Dianne Hackborn852975d2014-08-22 17:42:43 -0700149 boolean inRecents; // Actually in the recents list?
150 boolean isAvailable; // Is the activity available to be launched?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 boolean rootWasReset; // True if the intent at the root of the task had
152 // the FLAG_ACTIVITY_RESET_TASK_IF_NEEDED flag.
Dianne Hackborn13420f22014-07-18 15:43:56 -0700153 boolean autoRemoveRecents; // If true, we should automatically remove the task from
154 // recents when activity finishes
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700155 boolean askedCompatMode;// Have asked the user about compat mode for this task.
Dianne Hackbornd38aed82014-06-10 21:36:35 -0700156 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 -0800157
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700158 String stringName; // caching of toString() result.
Dianne Hackborn9da2d402012-03-15 13:43:08 -0700159 int userId; // user for which this task was created
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800160
161 int numFullscreen; // Number of fullscreen activities.
162
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800163 int mResizeMode; // The resize mode of this task and its activities.
164 // Based on the {@link ActivityInfo#resizeMode} of the root activity.
Craig Mautner15df08a2015-04-01 12:17:18 -0700165 int mLockTaskMode; // Which tasklock mode to launch this task in. One of
166 // ActivityManager.LOCK_TASK_LAUNCH_MODE_*
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700167 private boolean mPrivileged; // The root activity application of this task holds
168 // privileged permissions.
169
Craig Mautner15df08a2015-04-01 12:17:18 -0700170 /** Can't be put in lockTask mode. */
171 final static int LOCK_TASK_AUTH_DONT_LOCK = 0;
Benjamin Franz469dd582015-06-09 14:24:36 +0100172 /** Can enter app pinning with user approval. Can never start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700173 final static int LOCK_TASK_AUTH_PINNABLE = 1;
174 /** Starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing lockTask task. */
175 final static int LOCK_TASK_AUTH_LAUNCHABLE = 2;
Benjamin Franz469dd582015-06-09 14:24:36 +0100176 /** Can enter lockTask without user approval. Can start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700177 final static int LOCK_TASK_AUTH_WHITELISTED = 3;
Benjamin Franz469dd582015-06-09 14:24:36 +0100178 /** Priv-app that starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing
179 * lockTask task. */
180 final static int LOCK_TASK_AUTH_LAUNCHABLE_PRIV = 4;
Craig Mautner15df08a2015-04-01 12:17:18 -0700181 int mLockTaskAuth = LOCK_TASK_AUTH_PINNABLE;
182
183 int mLockTaskUid = -1; // The uid of the application that called startLockTask().
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800184
Winson Chung03a9bae2014-05-02 09:56:12 -0700185 // This represents the last resolved activity values for this task
186 // NOTE: This value needs to be persisted with each task
Craig Mautner648f69b2014-09-18 14:16:26 -0700187 TaskDescription lastTaskDescription = new TaskDescription();
Winson Chung03a9bae2014-05-02 09:56:12 -0700188
Craig Mautnerd2328952013-03-05 12:46:26 -0800189 /** List of all activities in the task arranged in history order */
Craig Mautner21d24a22014-04-23 11:45:37 -0700190 final ArrayList<ActivityRecord> mActivities;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800191
Craig Mautnerd2328952013-03-05 12:46:26 -0800192 /** Current stack */
193 ActivityStack stack;
194
Craig Mautner2c1faed2013-07-23 12:56:02 -0700195 /** Takes on same set of values as ActivityRecord.mActivityType */
Craig Mautner21d24a22014-04-23 11:45:37 -0700196 int taskType;
Craig Mautner1602ec22013-05-12 10:24:27 -0700197
Craig Mautner21d24a22014-04-23 11:45:37 -0700198 /** Takes on same value as first root activity */
199 boolean isPersistable = false;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700200 int maxRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700201
202 /** Only used for persistable tasks, otherwise 0. The last time this task was moved. Used for
203 * determining the order when restoring. Sign indicates whether last task movement was to front
204 * (positive) or back (negative). Absolute value indicates time. */
205 long mLastTimeMoved = System.currentTimeMillis();
206
Craig Mautner84984fa2014-06-19 11:19:20 -0700207 /** Indication of what to run next when task exits. Use ActivityRecord types.
208 * ActivityRecord.APPLICATION_ACTIVITY_TYPE indicates to resume the task below this one in the
209 * task stack. */
210 private int mTaskToReturnTo = APPLICATION_ACTIVITY_TYPE;
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700211
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700212 /** If original intent did not allow relinquishing task identity, save that information */
213 boolean mNeverRelinquishIdentity = true;
214
Craig Mautner362449a2014-06-20 14:04:39 -0700215 // Used in the unique case where we are clearing the task in order to reuse it. In that case we
216 // do not want to delete the stack when the task goes empty.
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -0800217 private boolean mReuseTask = false;
Craig Mautner362449a2014-06-20 14:04:39 -0700218
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700219 private Bitmap mLastThumbnail; // Last thumbnail captured for this item.
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700220 private final File mLastThumbnailFile; // File containing last thumbnail.
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700221 private final String mFilename;
Winsonc809cbb2015-11-02 12:06:15 -0800222 private TaskThumbnailInfo mLastThumbnailInfo;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700223 CharSequence lastDescription; // Last description captured for this item.
224
Craig Mautnera228ae92014-07-09 05:44:55 -0700225 int mAffiliatedTaskId; // taskId of parent affiliation or self if no parent.
Winson Chungec396d62014-08-06 17:08:00 -0700226 int mAffiliatedTaskColor; // color of the parent task affiliation.
Craig Mautnera228ae92014-07-09 05:44:55 -0700227 TaskRecord mPrevAffiliate; // previous task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800228 int mPrevAffiliateTaskId = INVALID_TASK_ID; // previous id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700229 TaskRecord mNextAffiliate; // next task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800230 int mNextAffiliateTaskId = INVALID_TASK_ID; // next id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700231
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700232 // For relaunching the task from recents as though it was launched by the original launcher.
233 int mCallingUid;
234 String mCallingPackage;
235
Craig Mautner21d24a22014-04-23 11:45:37 -0700236 final ActivityManagerService mService;
237
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700238 // Whether or not this task covers the entire screen; by default tasks are fullscreen.
239 boolean mFullscreen = true;
240
241 // Bounds of the Task. null for fullscreen tasks.
242 Rect mBounds = null;
Wale Ogunwale706ed792015-08-02 10:29:44 -0700243 // Last non-fullscreen bounds the task was launched in or resized to.
244 // The information is persisted and used to determine the appropriate stack to launch the
245 // task into on restore.
246 Rect mLastNonFullscreenBounds = null;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700247 // Minimal size for width/height of this task when it's resizeable. -1 means it should use the
248 // default minimal size.
249 final int mMinimalSize;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700250
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700251 // Ranking (from top) of this task among all visible tasks. (-1 means it's not visible)
252 // This number will be assigned when we evaluate OOM scores for all visible tasks.
253 int mLayerRank = -1;
254
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700255 Configuration mOverrideConfig = Configuration.EMPTY;
256
Craig Mautner21d24a22014-04-23 11:45:37 -0700257 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700258 IVoiceInteractionSession _voiceSession, IVoiceInteractor _voiceInteractor) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700259 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700260 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
261 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800262 userId = UserHandle.getUserId(info.applicationInfo.uid);
263 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800264 mLastThumbnailInfo = new TaskThumbnailInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 taskId = _taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700266 mAffiliatedTaskId = _taskId;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700267 voiceSession = _voiceSession;
268 voiceInteractor = _voiceInteractor;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700269 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800270 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700271 mCallingUid = info.applicationInfo.uid;
272 mCallingPackage = info.packageName;
Martijn Coenend4a69702014-06-30 11:12:17 -0700273 setIntent(_intent, info);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700274 mMinimalSize = info != null && info.layout != null ? info.layout.minimalSize : -1;
Craig Mautner21d24a22014-04-23 11:45:37 -0700275 }
276
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700277 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Winsonc809cbb2015-11-02 12:06:15 -0800278 TaskDescription _taskDescription, TaskThumbnailInfo thumbnailInfo) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700279 mService = service;
280 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 = thumbnailInfo;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700285 taskId = _taskId;
286 mAffiliatedTaskId = _taskId;
287 voiceSession = null;
288 voiceInteractor = null;
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;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700293 setIntent(_intent, info);
294
295 taskType = ActivityRecord.APPLICATION_ACTIVITY_TYPE;
296 isPersistable = true;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700297 // Clamp to [1, max].
298 maxRecents = Math.min(Math.max(info.maxRecents, 1),
299 ActivityManager.getMaxAppRecentsLimitStatic());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700300
301 taskType = APPLICATION_ACTIVITY_TYPE;
302 mTaskToReturnTo = HOME_ACTIVITY_TYPE;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700303 lastTaskDescription = _taskDescription;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700304 mMinimalSize = info != null && info.layout != null ? info.layout.minimalSize : -1;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700305 }
306
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800307 private TaskRecord(ActivityManagerService service, int _taskId, Intent _intent,
308 Intent _affinityIntent, String _affinity, String _rootAffinity,
309 ComponentName _realActivity, ComponentName _origActivity, boolean _rootWasReset,
310 boolean _autoRemoveRecents, boolean _askedCompatMode, int _taskType, int _userId,
311 int _effectiveUid, String _lastDescription, ArrayList<ActivityRecord> activities,
312 long _firstActiveTime, long _lastActiveTime, long lastTimeMoved,
313 boolean neverRelinquishIdentity, TaskDescription _lastTaskDescription,
Winsonc809cbb2015-11-02 12:06:15 -0800314 TaskThumbnailInfo lastThumbnailInfo, int taskAffiliation, int prevTaskId,
315 int nextTaskId, int taskAffiliationColor, int callingUid, String callingPackage,
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800316 int resizeMode, boolean privileged, boolean _realActivitySuspended) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700317 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700318 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
319 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800320 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(_userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800321 mLastThumbnailInfo = lastThumbnailInfo;
Craig Mautner21d24a22014-04-23 11:45:37 -0700322 taskId = _taskId;
323 intent = _intent;
324 affinityIntent = _affinityIntent;
325 affinity = _affinity;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800326 rootAffinity = _rootAffinity;
Craig Mautner21d24a22014-04-23 11:45:37 -0700327 voiceSession = null;
328 voiceInteractor = null;
329 realActivity = _realActivity;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800330 realActivitySuspended = _realActivitySuspended;
Craig Mautner21d24a22014-04-23 11:45:37 -0700331 origActivity = _origActivity;
332 rootWasReset = _rootWasReset;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700333 isAvailable = true;
Dianne Hackborn13420f22014-07-18 15:43:56 -0700334 autoRemoveRecents = _autoRemoveRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700335 askedCompatMode = _askedCompatMode;
336 taskType = _taskType;
Craig Mautner84984fa2014-06-19 11:19:20 -0700337 mTaskToReturnTo = HOME_ACTIVITY_TYPE;
Craig Mautner21d24a22014-04-23 11:45:37 -0700338 userId = _userId;
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700339 effectiveUid = _effectiveUid;
Winson Chungffa2ec62014-07-03 15:54:42 -0700340 firstActiveTime = _firstActiveTime;
Winson Chungf1fbd772014-06-24 18:06:58 -0700341 lastActiveTime = _lastActiveTime;
Craig Mautner21d24a22014-04-23 11:45:37 -0700342 lastDescription = _lastDescription;
343 mActivities = activities;
344 mLastTimeMoved = lastTimeMoved;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700345 mNeverRelinquishIdentity = neverRelinquishIdentity;
Winson Chung2cb86c72014-06-25 12:03:30 -0700346 lastTaskDescription = _lastTaskDescription;
Craig Mautnera228ae92014-07-09 05:44:55 -0700347 mAffiliatedTaskId = taskAffiliation;
Winson Chungec396d62014-08-06 17:08:00 -0700348 mAffiliatedTaskColor = taskAffiliationColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700349 mPrevAffiliateTaskId = prevTaskId;
350 mNextAffiliateTaskId = nextTaskId;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700351 mCallingUid = callingUid;
352 mCallingPackage = callingPackage;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800353 mResizeMode = resizeMode;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700354 mPrivileged = privileged;
Suprabh Shukla74f9f162015-11-06 15:39:42 -0800355 ActivityInfo info = (mActivities.size() > 0) ? mActivities.get(0).info : null;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700356 mMinimalSize = info != null && info.layout != null ? info.layout.minimalSize : -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 }
358
359 void touchActiveTime() {
Craig Mautner494f6bdd2014-07-21 11:17:46 -0700360 lastActiveTime = System.currentTimeMillis();
Winson Chungffa2ec62014-07-03 15:54:42 -0700361 if (firstActiveTime == 0) {
362 firstActiveTime = lastActiveTime;
363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 long getInactiveDuration() {
Craig Mautner494f6bdd2014-07-21 11:17:46 -0700367 return System.currentTimeMillis() - lastActiveTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700369
Winson Chungfee26772014-08-05 12:21:52 -0700370 /** Sets the original intent, and the calling uid and package. */
371 void setIntent(ActivityRecord r) {
Winson Chungfee26772014-08-05 12:21:52 -0700372 mCallingUid = r.launchedFromUid;
373 mCallingPackage = r.launchedFromPackage;
Craig Mautner15df08a2015-04-01 12:17:18 -0700374 setIntent(r.intent, r.info);
Winson Chungfee26772014-08-05 12:21:52 -0700375 }
376
377 /** Sets the original intent, _without_ updating the calling uid or package. */
378 private void setIntent(Intent _intent, ActivityInfo info) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700379 if (intent == null) {
380 mNeverRelinquishIdentity =
381 (info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) == 0;
382 } else if (mNeverRelinquishIdentity) {
383 return;
384 }
385
386 affinity = info.taskAffinity;
Dianne Hackborn79228822014-09-16 11:11:23 -0700387 if (intent == null) {
388 // If this task already has an intent associated with it, don't set the root
389 // affinity -- we don't want it changing after initially set, but the initially
390 // set value may be null.
391 rootAffinity = affinity;
392 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700393 effectiveUid = info.applicationInfo.uid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700394 stringName = null;
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 if (info.targetActivity == null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800397 if (_intent != null) {
398 // If this Intent has a selector, we want to clear it for the
399 // recent task since it is not relevant if the user later wants
400 // to re-launch the app.
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700401 if (_intent.getSelector() != null || _intent.getSourceBounds() != null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800402 _intent = new Intent(_intent);
403 _intent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700404 _intent.setSourceBounds(null);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800405 }
406 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700407 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Setting Intent of " + this + " to " + _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 intent = _intent;
409 realActivity = _intent != null ? _intent.getComponent() : null;
410 origActivity = null;
411 } else {
412 ComponentName targetComponent = new ComponentName(
413 info.packageName, info.targetActivity);
414 if (_intent != null) {
415 Intent targetIntent = new Intent(_intent);
416 targetIntent.setComponent(targetComponent);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800417 targetIntent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700418 targetIntent.setSourceBounds(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700419 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700420 "Setting Intent of " + this + " to target " + targetIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 intent = targetIntent;
422 realActivity = targetComponent;
423 origActivity = _intent.getComponent();
424 } else {
425 intent = null;
426 realActivity = targetComponent;
427 origActivity = new ComponentName(info.packageName, info.name);
428 }
429 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700430
Craig Mautner47b20ba2014-09-17 17:23:44 -0700431 final int intentFlags = intent == null ? 0 : intent.getFlags();
432 if ((intentFlags & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 // Once we are set to an Intent with this flag, we count this
434 // task as having a true root activity.
435 rootWasReset = true;
436 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700437
Dianne Hackborn09233282014-04-30 11:33:59 -0700438 userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner41db4a72014-05-07 17:20:56 -0700439 if ((info.flags & ActivityInfo.FLAG_AUTO_REMOVE_FROM_RECENTS) != 0) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700440 // If the activity itself has requested auto-remove, then just always do it.
441 autoRemoveRecents = true;
Wale Ogunwale843bfb92015-03-27 11:06:48 -0700442 } else if ((intentFlags & (FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_RETAIN_IN_RECENTS))
443 == FLAG_ACTIVITY_NEW_DOCUMENT) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700444 // If the caller has not asked for the document to be retained, then we may
445 // want to turn on auto-remove, depending on whether the target has set its
446 // own document launch mode.
447 if (info.documentLaunchMode != ActivityInfo.DOCUMENT_LAUNCH_NONE) {
448 autoRemoveRecents = false;
449 } else {
450 autoRemoveRecents = true;
451 }
452 } else {
453 autoRemoveRecents = false;
Craig Mautner41db4a72014-05-07 17:20:56 -0700454 }
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800455 mResizeMode = info.resizeMode;
Craig Mautner15df08a2015-04-01 12:17:18 -0700456 mLockTaskMode = info.lockTaskLaunchMode;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700457 mPrivileged = (info.applicationInfo.privateFlags & PRIVATE_FLAG_PRIVILEGED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -0700458 setLockTaskAuth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800460
Craig Mautner84984fa2014-06-19 11:19:20 -0700461 void setTaskToReturnTo(int taskToReturnTo) {
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700462 mTaskToReturnTo = (IGNORE_RETURN_TO_RECENTS && taskToReturnTo == RECENTS_ACTIVITY_TYPE)
463 ? HOME_ACTIVITY_TYPE : taskToReturnTo;
Craig Mautner84984fa2014-06-19 11:19:20 -0700464 }
465
466 int getTaskToReturnTo() {
467 return mTaskToReturnTo;
468 }
469
Craig Mautnera228ae92014-07-09 05:44:55 -0700470 void setPrevAffiliate(TaskRecord prevAffiliate) {
471 mPrevAffiliate = prevAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800472 mPrevAffiliateTaskId = prevAffiliate == null ? INVALID_TASK_ID : prevAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700473 }
474
475 void setNextAffiliate(TaskRecord nextAffiliate) {
476 mNextAffiliate = nextAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800477 mNextAffiliateTaskId = nextAffiliate == null ? INVALID_TASK_ID : nextAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700478 }
479
480 // Close up recents linked list.
481 void closeRecentsChain() {
482 if (mPrevAffiliate != null) {
483 mPrevAffiliate.setNextAffiliate(mNextAffiliate);
484 }
485 if (mNextAffiliate != null) {
486 mNextAffiliate.setPrevAffiliate(mPrevAffiliate);
487 }
488 setPrevAffiliate(null);
489 setNextAffiliate(null);
490 }
491
Winson Chung740c3ac2014-11-12 16:14:38 -0800492 void removedFromRecents() {
Dianne Hackborn852975d2014-08-22 17:42:43 -0700493 disposeThumbnail();
494 closeRecentsChain();
495 if (inRecents) {
496 inRecents = false;
Winson Chung740c3ac2014-11-12 16:14:38 -0800497 mService.notifyTaskPersisterLocked(this, false);
Dianne Hackborn852975d2014-08-22 17:42:43 -0700498 }
499 }
500
Craig Mautnera228ae92014-07-09 05:44:55 -0700501 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
502 closeRecentsChain();
503 mAffiliatedTaskId = taskToAffiliateWith.mAffiliatedTaskId;
Winson Chungec396d62014-08-06 17:08:00 -0700504 mAffiliatedTaskColor = taskToAffiliateWith.mAffiliatedTaskColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700505 // Find the end
506 while (taskToAffiliateWith.mNextAffiliate != null) {
507 final TaskRecord nextRecents = taskToAffiliateWith.mNextAffiliate;
508 if (nextRecents.mAffiliatedTaskId != mAffiliatedTaskId) {
509 Slog.e(TAG, "setTaskToAffiliateWith: nextRecents=" + nextRecents + " affilTaskId="
510 + nextRecents.mAffiliatedTaskId + " should be " + mAffiliatedTaskId);
511 if (nextRecents.mPrevAffiliate == taskToAffiliateWith) {
512 nextRecents.setPrevAffiliate(null);
513 }
514 taskToAffiliateWith.setNextAffiliate(null);
515 break;
516 }
517 taskToAffiliateWith = nextRecents;
518 }
519 taskToAffiliateWith.setNextAffiliate(this);
520 setPrevAffiliate(taskToAffiliateWith);
521 setNextAffiliate(null);
522 }
523
Winson Chung096f36b2014-08-20 15:39:01 -0700524 /**
Winsonc809cbb2015-11-02 12:06:15 -0800525 * Sets the last thumbnail with the current task bounds and the system orientation.
Winson Chung096f36b2014-08-20 15:39:01 -0700526 * @return whether the thumbnail was set
527 */
Winsonc809cbb2015-11-02 12:06:15 -0800528 boolean setLastThumbnailLocked(Bitmap thumbnail) {
529 final Configuration serviceConfig = mService.mConfiguration;
530 int taskWidth = 0;
531 int taskHeight = 0;
532 if (mBounds != null) {
533 // Non-fullscreen tasks
534 taskWidth = mBounds.width();
535 taskHeight = mBounds.height();
536 } else if (stack != null) {
537 // Fullscreen tasks
538 final Point displaySize = new Point();
539 stack.getDisplaySize(displaySize);
540 taskWidth = displaySize.x;
541 taskHeight = displaySize.y;
542 } else {
543 Slog.e(TAG, "setLastThumbnailLocked() called on Task without stack");
544 }
545 return setLastThumbnailLocked(thumbnail, taskWidth, taskHeight, serviceConfig.orientation);
546 }
547
548 /**
549 * Sets the last thumbnail with the current task bounds.
550 * @return whether the thumbnail was set
551 */
552 private boolean setLastThumbnailLocked(Bitmap thumbnail, int taskWidth, int taskHeight,
553 int screenOrientation) {
Winson Chung096f36b2014-08-20 15:39:01 -0700554 if (mLastThumbnail != thumbnail) {
555 mLastThumbnail = thumbnail;
Winsonc809cbb2015-11-02 12:06:15 -0800556 mLastThumbnailInfo.taskWidth = taskWidth;
557 mLastThumbnailInfo.taskHeight = taskHeight;
558 mLastThumbnailInfo.screenOrientation = screenOrientation;
Winson Chung096f36b2014-08-20 15:39:01 -0700559 if (thumbnail == null) {
560 if (mLastThumbnailFile != null) {
561 mLastThumbnailFile.delete();
562 }
563 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800564 mService.mRecentTasks.saveImage(thumbnail, mLastThumbnailFile.getAbsolutePath());
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700565 }
Winson Chung096f36b2014-08-20 15:39:01 -0700566 return true;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700567 }
Winson Chung096f36b2014-08-20 15:39:01 -0700568 return false;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700569 }
570
571 void getLastThumbnail(TaskThumbnail thumbs) {
572 thumbs.mainThumbnail = mLastThumbnail;
Winsonc809cbb2015-11-02 12:06:15 -0800573 thumbs.thumbnailInfo = mLastThumbnailInfo;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700574 thumbs.thumbnailFileDescriptor = null;
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700575 if (mLastThumbnail == null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800576 thumbs.mainThumbnail = mService.mRecentTasks.getImageFromWriteQueue(
Suprabh Shukla23593142015-11-03 17:31:15 -0800577 mLastThumbnailFile.getAbsolutePath());
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700578 }
Winson Chung096f36b2014-08-20 15:39:01 -0700579 // Only load the thumbnail file if we don't have a thumbnail
580 if (thumbs.mainThumbnail == null && mLastThumbnailFile.exists()) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700581 try {
582 thumbs.thumbnailFileDescriptor = ParcelFileDescriptor.open(mLastThumbnailFile,
583 ParcelFileDescriptor.MODE_READ_ONLY);
584 } catch (IOException e) {
Dianne Hackborn9844d292013-10-04 16:44:22 -0700585 }
586 }
587 }
588
Winsonc809cbb2015-11-02 12:06:15 -0800589 /**
590 * Removes in-memory thumbnail data when the max number of in-memory task thumbnails is reached.
591 */
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700592 void freeLastThumbnail() {
593 mLastThumbnail = null;
594 }
595
Winsonc809cbb2015-11-02 12:06:15 -0800596 /**
597 * Removes all associated thumbnail data when a task is removed or pruned from recents.
598 */
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700599 void disposeThumbnail() {
Winsonc6a2da02015-11-11 18:11:59 -0800600 mLastThumbnailInfo.reset();
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700601 mLastThumbnail = null;
602 lastDescription = null;
603 }
604
Winson Chung1147c402014-05-14 11:05:00 -0700605 /** Returns the intent for the root activity for this task */
606 Intent getBaseIntent() {
607 return intent != null ? intent : affinityIntent;
608 }
609
Winson Chung3b3f4642014-04-22 10:08:18 -0700610 /** Returns the first non-finishing activity from the root. */
611 ActivityRecord getRootActivity() {
612 for (int i = 0; i < mActivities.size(); i++) {
613 final ActivityRecord r = mActivities.get(i);
614 if (r.finishing) {
615 continue;
616 }
617 return r;
618 }
619 return null;
620 }
621
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800622 ActivityRecord getTopActivity() {
623 for (int i = mActivities.size() - 1; i >= 0; --i) {
624 final ActivityRecord r = mActivities.get(i);
625 if (r.finishing) {
626 continue;
627 }
628 return r;
629 }
630 return null;
631 }
632
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700633 ActivityRecord topRunningActivityLocked() {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700634 if (stack != null) {
635 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
636 ActivityRecord r = mActivities.get(activityNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700637 if (!r.finishing && stack.okToShowLocked(r)) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700638 return r;
639 }
Craig Mautner6b74cb52013-09-27 17:02:21 -0700640 }
641 }
642 return null;
643 }
644
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800645 void setFrontOfTask() {
646 setFrontOfTask(null);
647 }
648
Craig Mautner3b475fe2013-12-16 15:58:31 -0800649 /** Call after activity movement or finish to make sure that frontOfTask is set correctly */
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800650 void setFrontOfTask(ActivityRecord newTop) {
651 // If a top candidate is suggested by the caller, go ahead and use it and mark all others
652 // as not front. This is needed in situations where the current front activity in the
653 // task isn't finished yet and we want to set the front to the activity moved to the front
654 // of the task.
655 boolean foundFront = newTop != null ? true : false;
656
Craig Mautner3b475fe2013-12-16 15:58:31 -0800657 final int numActivities = mActivities.size();
Craig Mautner704e40b2013-12-18 16:43:51 -0800658 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
Craig Mautner3b475fe2013-12-16 15:58:31 -0800659 final ActivityRecord r = mActivities.get(activityNdx);
660 if (foundFront || r.finishing) {
661 r.frontOfTask = false;
662 } else {
663 r.frontOfTask = true;
664 // Set frontOfTask false for every following activity.
665 foundFront = true;
666 }
667 }
Craig Mautner9587ee02014-06-23 15:00:10 +0000668 if (!foundFront && numActivities > 0) {
669 // All activities of this task are finishing. As we ought to have a frontOfTask
670 // activity, make the bottom activity front.
671 mActivities.get(0).frontOfTask = true;
672 }
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800673 if (newTop != null) {
674 newTop.frontOfTask = true;
675 }
Craig Mautner3b475fe2013-12-16 15:58:31 -0800676 }
677
Craig Mautnerde4ef022013-04-07 19:01:33 -0700678 /**
Craig Mautner3b475fe2013-12-16 15:58:31 -0800679 * Reorder the history stack so that the passed activity is brought to the front.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700680 */
681 final void moveActivityToFrontLocked(ActivityRecord newTop) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700682 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
683 "Removing and adding activity " + newTop
684 + " to stack at top callers=" + Debug.getCallers(4));
Craig Mautnerde4ef022013-04-07 19:01:33 -0700685
Craig Mautnerde4ef022013-04-07 19:01:33 -0700686 mActivities.remove(newTop);
687 mActivities.add(newTop);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700688 updateEffectiveIntent();
Craig Mautner3b475fe2013-12-16 15:58:31 -0800689
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800690 setFrontOfTask(newTop);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700691 }
692
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800693 void addActivityAtBottom(ActivityRecord r) {
Craig Mautner77878772013-03-04 19:46:24 -0800694 addActivityAtIndex(0, r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800695 }
696
697 void addActivityToTop(ActivityRecord r) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700698 addActivityAtIndex(mActivities.size(), r);
699 }
700
701 void addActivityAtIndex(int index, ActivityRecord r) {
Craig Mautner6170f732013-04-02 13:05:23 -0700702 // 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 -0800703 if (!mActivities.remove(r) && r.fullscreen) {
704 // Was not previously in list.
705 numFullscreen++;
706 }
Craig Mautner2c1faed2013-07-23 12:56:02 -0700707 // Only set this based on the first activity
708 if (mActivities.isEmpty()) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700709 taskType = r.mActivityType;
710 isPersistable = r.isPersistable();
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700711 mCallingUid = r.launchedFromUid;
712 mCallingPackage = r.launchedFromPackage;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700713 // Clamp to [1, max].
714 maxRecents = Math.min(Math.max(r.info.maxRecents, 1),
715 ActivityManager.getMaxAppRecentsLimitStatic());
Craig Mautner2c1faed2013-07-23 12:56:02 -0700716 } else {
717 // Otherwise make all added activities match this one.
Craig Mautner21d24a22014-04-23 11:45:37 -0700718 r.mActivityType = taskType;
Craig Mautner78733002013-06-10 13:54:49 -0700719 }
Craig Mautner77878772013-03-04 19:46:24 -0800720 mActivities.add(index, r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700721 updateEffectiveIntent();
Craig Mautner21d24a22014-04-23 11:45:37 -0700722 if (r.isPersistable()) {
723 mService.notifyTaskPersisterLocked(this, false);
724 }
Craig Mautner77878772013-03-04 19:46:24 -0800725 }
726
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800727 /** @return true if this was the last activity in the task */
728 boolean removeActivity(ActivityRecord r) {
729 if (mActivities.remove(r) && r.fullscreen) {
730 // Was previously in list.
731 numFullscreen--;
732 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700733 if (r.isPersistable()) {
734 mService.notifyTaskPersisterLocked(this, false);
735 }
Craig Mautner41326202014-06-20 14:38:21 -0700736 if (mActivities.isEmpty()) {
Craig Mautner5afcd4d2014-06-21 18:11:33 -0700737 return !mReuseTask;
Craig Mautner41326202014-06-20 14:38:21 -0700738 }
739 updateEffectiveIntent();
740 return false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800741 }
742
Craig Mautner41db4a72014-05-07 17:20:56 -0700743 boolean autoRemoveFromRecents() {
Dianne Hackbornd38aed82014-06-10 21:36:35 -0700744 // We will automatically remove the task either if it has explicitly asked for
745 // this, or it is empty and has never contained an activity that got shown to
746 // the user.
Dianne Hackborn13420f22014-07-18 15:43:56 -0700747 return autoRemoveRecents || (mActivities.isEmpty() && !hasBeenVisible);
Craig Mautner41db4a72014-05-07 17:20:56 -0700748 }
749
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700750 /**
751 * Completely remove all activities associated with an existing
752 * task starting at a specified index.
753 */
754 final void performClearTaskAtIndexLocked(int activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700755 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700756 for ( ; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700757 final ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700758 if (r.finishing) {
759 continue;
760 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700761 if (stack == null) {
762 // Task was restored from persistent storage.
763 r.takeFromHistory();
764 mActivities.remove(activityNdx);
765 --activityNdx;
766 --numActivities;
Todd Kennedy539db512014-12-15 09:57:55 -0800767 } else if (stack.finishActivityLocked(
768 r, Activity.RESULT_CANCELED, null, "clear-task-index", false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700769 --activityNdx;
770 --numActivities;
771 }
772 }
773 }
774
775 /**
776 * Completely remove all activities associated with an existing task.
777 */
778 final void performClearTaskLocked() {
Craig Mautner362449a2014-06-20 14:04:39 -0700779 mReuseTask = true;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700780 performClearTaskAtIndexLocked(0);
Craig Mautner362449a2014-06-20 14:04:39 -0700781 mReuseTask = false;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700782 }
783
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -0800784 ActivityRecord performClearTaskForReuseLocked(ActivityRecord newR, int launchFlags) {
785 mReuseTask = true;
786 final ActivityRecord result = performClearTaskLocked(newR, launchFlags);
787 mReuseTask = false;
788 return result;
789 }
790
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700791 /**
792 * Perform clear operation as requested by
793 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
794 * stack to the given task, then look for
795 * an instance of that activity in the stack and, if found, finish all
796 * activities on top of it and return the instance.
797 *
798 * @param newR Description of the new activity being started.
799 * @return Returns the old activity that should be continued to be used,
800 * or null if none was found.
801 */
802 final ActivityRecord performClearTaskLocked(ActivityRecord newR, int launchFlags) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700803 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700804 for (int activityNdx = numActivities - 1; activityNdx >= 0; --activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700805 ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700806 if (r.finishing) {
807 continue;
808 }
809 if (r.realActivity.equals(newR.realActivity)) {
810 // Here it is! Now finish everything in front...
Craig Mautner1602ec22013-05-12 10:24:27 -0700811 final ActivityRecord ret = r;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700812
813 for (++activityNdx; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700814 r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700815 if (r.finishing) {
816 continue;
817 }
818 ActivityOptions opts = r.takeOptionsLocked();
819 if (opts != null) {
820 ret.updateOptionsLocked(opts);
821 }
Wale Ogunwale7d701172015-03-11 15:36:30 -0700822 if (stack != null && stack.finishActivityLocked(
Todd Kennedy539db512014-12-15 09:57:55 -0800823 r, Activity.RESULT_CANCELED, null, "clear-task-stack", false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700824 --activityNdx;
825 --numActivities;
826 }
827 }
828
829 // Finally, if this is a normal launch mode (that is, not
830 // expecting onNewIntent()), then we will finish the current
831 // instance of the activity so a new fresh one can be started.
832 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
833 && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0) {
834 if (!ret.finishing) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700835 if (stack != null) {
836 stack.finishActivityLocked(
837 ret, Activity.RESULT_CANCELED, null, "clear-task-top", false);
838 }
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700839 return null;
840 }
841 }
842
843 return ret;
844 }
845 }
846
847 return null;
848 }
849
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700850 public TaskThumbnail getTaskThumbnailLocked() {
Craig Mautner21d24a22014-04-23 11:45:37 -0700851 if (stack != null) {
852 final ActivityRecord resumedActivity = stack.mResumedActivity;
853 if (resumedActivity != null && resumedActivity.task == this) {
Winson8b1871d2015-11-20 09:56:20 -0800854 final Bitmap thumbnail = stack.screenshotActivitiesLocked(resumedActivity);
Winsonc809cbb2015-11-02 12:06:15 -0800855 setLastThumbnailLocked(thumbnail);
Craig Mautner21d24a22014-04-23 11:45:37 -0700856 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700857 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700858 final TaskThumbnail taskThumbnail = new TaskThumbnail();
859 getLastThumbnail(taskThumbnail);
860 return taskThumbnail;
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700861 }
862
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700863 public void removeTaskActivitiesLocked() {
864 // Just remove the entire task.
865 performClearTaskAtIndexLocked(0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700866 }
867
Craig Mautner432f64e2015-05-20 14:59:57 -0700868 String lockTaskAuthToString() {
869 switch (mLockTaskAuth) {
870 case LOCK_TASK_AUTH_DONT_LOCK: return "LOCK_TASK_AUTH_DONT_LOCK";
871 case LOCK_TASK_AUTH_PINNABLE: return "LOCK_TASK_AUTH_PINNABLE";
872 case LOCK_TASK_AUTH_LAUNCHABLE: return "LOCK_TASK_AUTH_LAUNCHABLE";
873 case LOCK_TASK_AUTH_WHITELISTED: return "LOCK_TASK_AUTH_WHITELISTED";
Benjamin Franz469dd582015-06-09 14:24:36 +0100874 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV: return "LOCK_TASK_AUTH_LAUNCHABLE_PRIV";
Craig Mautner432f64e2015-05-20 14:59:57 -0700875 default: return "unknown=" + mLockTaskAuth;
876 }
877 }
878
Craig Mautner15df08a2015-04-01 12:17:18 -0700879 void setLockTaskAuth() {
Benjamin Franz469dd582015-06-09 14:24:36 +0100880 if (!mPrivileged &&
881 (mLockTaskMode == LOCK_TASK_LAUNCH_MODE_ALWAYS ||
882 mLockTaskMode == LOCK_TASK_LAUNCH_MODE_NEVER)) {
883 // Non-priv apps are not allowed to use always or never, fall back to default
884 mLockTaskMode = LOCK_TASK_LAUNCH_MODE_DEFAULT;
885 }
Craig Mautner15df08a2015-04-01 12:17:18 -0700886 switch (mLockTaskMode) {
887 case LOCK_TASK_LAUNCH_MODE_DEFAULT:
888 mLockTaskAuth = isLockTaskWhitelistedLocked() ?
889 LOCK_TASK_AUTH_WHITELISTED : LOCK_TASK_AUTH_PINNABLE;
890 break;
891
892 case LOCK_TASK_LAUNCH_MODE_NEVER:
Benjamin Franz469dd582015-06-09 14:24:36 +0100893 mLockTaskAuth = LOCK_TASK_AUTH_DONT_LOCK;
Craig Mautner15df08a2015-04-01 12:17:18 -0700894 break;
895
896 case LOCK_TASK_LAUNCH_MODE_ALWAYS:
Benjamin Franz469dd582015-06-09 14:24:36 +0100897 mLockTaskAuth = LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -0700898 break;
899
900 case LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED:
901 mLockTaskAuth = isLockTaskWhitelistedLocked() ?
902 LOCK_TASK_AUTH_LAUNCHABLE : LOCK_TASK_AUTH_PINNABLE;
903 break;
904 }
Craig Mautner432f64e2015-05-20 14:59:57 -0700905 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "setLockTaskAuth: task=" + this +
906 " mLockTaskAuth=" + lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -0700907 }
908
909 boolean isLockTaskWhitelistedLocked() {
Benjamin Franz6fd84cc2015-08-06 18:09:03 +0100910 String pkg = (realActivity != null) ? realActivity.getPackageName() : null;
911 if (pkg == null) {
Craig Mautner15df08a2015-04-01 12:17:18 -0700912 return false;
913 }
914 String[] packages = mService.mLockTaskPackages.get(userId);
915 if (packages == null) {
916 return false;
917 }
918 for (int i = packages.length - 1; i >= 0; --i) {
Benjamin Franz6fd84cc2015-08-06 18:09:03 +0100919 if (pkg.equals(packages[i])) {
Craig Mautner15df08a2015-04-01 12:17:18 -0700920 return true;
921 }
922 }
923 return false;
924 }
Craig Mautnera82aa092013-09-13 15:34:08 -0700925 boolean isHomeTask() {
Craig Mautner84984fa2014-06-19 11:19:20 -0700926 return taskType == HOME_ACTIVITY_TYPE;
Craig Mautnera82aa092013-09-13 15:34:08 -0700927 }
928
Craig Mautner86d67a42013-05-14 10:34:38 -0700929 boolean isApplicationTask() {
Craig Mautner84984fa2014-06-19 11:19:20 -0700930 return taskType == APPLICATION_ACTIVITY_TYPE;
931 }
932
933 boolean isOverHomeStack() {
934 return mTaskToReturnTo == HOME_ACTIVITY_TYPE || mTaskToReturnTo == RECENTS_ACTIVITY_TYPE;
Craig Mautner1602ec22013-05-12 10:24:27 -0700935 }
936
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800937 boolean isResizeable() {
938 return !isHomeTask() && (mService.mForceResizableActivities
939 || ActivityInfo.isResizeableMode(mResizeMode));
940 }
941
942 boolean inCropWindowsResizeMode() {
943 return !isResizeable() && mResizeMode == RESIZE_MODE_CROP_WINDOWS;
944 }
945
Craig Mautner525f3d92013-05-07 14:01:50 -0700946 /**
947 * Find the activity in the history stack within the given task. Returns
948 * the index within the history at which it's found, or < 0 if not found.
949 */
950 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r) {
951 final ComponentName realActivity = r.realActivity;
952 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
953 ActivityRecord candidate = mActivities.get(activityNdx);
954 if (candidate.finishing) {
955 continue;
956 }
957 if (candidate.realActivity.equals(realActivity)) {
958 return candidate;
959 }
960 }
961 return null;
962 }
963
Winson Chunga449dc02014-05-16 11:15:04 -0700964 /** Updates the last task description values. */
965 void updateTaskDescription() {
966 // Traverse upwards looking for any break between main task activities and
967 // utility activities.
968 int activityNdx;
969 final int numActivities = mActivities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700970 final boolean relinquish = numActivities == 0 ? false :
971 (mActivities.get(0).info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) != 0;
Winson Chunga449dc02014-05-16 11:15:04 -0700972 for (activityNdx = Math.min(numActivities, 1); activityNdx < numActivities;
Craig Mautner21d24a22014-04-23 11:45:37 -0700973 ++activityNdx) {
Winson Chunga449dc02014-05-16 11:15:04 -0700974 final ActivityRecord r = mActivities.get(activityNdx);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700975 if (relinquish && (r.info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
976 // This will be the top activity for determining taskDescription. Pre-inc to
977 // overcome initial decrement below.
978 ++activityNdx;
979 break;
980 }
Winson Chunga449dc02014-05-16 11:15:04 -0700981 if (r.intent != null &&
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700982 (r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Winson Chunga449dc02014-05-16 11:15:04 -0700983 break;
984 }
985 }
986 if (activityNdx > 0) {
987 // Traverse downwards starting below break looking for set label, icon.
988 // Note that if there are activities in the task but none of them set the
989 // recent activity values, then we do not fall back to the last set
990 // values in the TaskRecord.
991 String label = null;
Craig Mautner648f69b2014-09-18 14:16:26 -0700992 String iconFilename = null;
Winson Chunga449dc02014-05-16 11:15:04 -0700993 int colorPrimary = 0;
994 for (--activityNdx; activityNdx >= 0; --activityNdx) {
995 final ActivityRecord r = mActivities.get(activityNdx);
996 if (r.taskDescription != null) {
997 if (label == null) {
998 label = r.taskDescription.getLabel();
999 }
Craig Mautner648f69b2014-09-18 14:16:26 -07001000 if (iconFilename == null) {
1001 iconFilename = r.taskDescription.getIconFilename();
Winson Chunga449dc02014-05-16 11:15:04 -07001002 }
1003 if (colorPrimary == 0) {
1004 colorPrimary = r.taskDescription.getPrimaryColor();
Winson Chunga449dc02014-05-16 11:15:04 -07001005 }
1006 }
1007 }
Craig Mautner648f69b2014-09-18 14:16:26 -07001008 lastTaskDescription = new TaskDescription(label, colorPrimary, iconFilename);
Winson Chungec396d62014-08-06 17:08:00 -07001009 // Update the task affiliation color if we are the parent of the group
1010 if (taskId == mAffiliatedTaskId) {
1011 mAffiliatedTaskColor = lastTaskDescription.getPrimaryColor();
1012 }
Winson Chunga449dc02014-05-16 11:15:04 -07001013 }
1014 }
1015
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001016 int findEffectiveRootIndex() {
Craig Mautner4767f4b2014-09-18 15:38:33 -07001017 int effectiveNdx = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001018 final int topActivityNdx = mActivities.size() - 1;
Dianne Hackborn39569af2014-09-23 10:56:58 -07001019 for (int activityNdx = 0; activityNdx <= topActivityNdx; ++activityNdx) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001020 final ActivityRecord r = mActivities.get(activityNdx);
1021 if (r.finishing) {
1022 continue;
1023 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001024 effectiveNdx = activityNdx;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001025 if ((r.info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
1026 break;
1027 }
1028 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001029 return effectiveNdx;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001030 }
1031
1032 void updateEffectiveIntent() {
1033 final int effectiveRootIndex = findEffectiveRootIndex();
1034 final ActivityRecord r = mActivities.get(effectiveRootIndex);
Winson Chungfee26772014-08-05 12:21:52 -07001035 setIntent(r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001036 }
1037
Craig Mautner21d24a22014-04-23 11:45:37 -07001038 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001039 if (DEBUG_RECENTS) Slog.i(TAG_RECENTS, "Saving task=" + this);
Craig Mautner21d24a22014-04-23 11:45:37 -07001040
1041 out.attribute(null, ATTR_TASKID, String.valueOf(taskId));
1042 if (realActivity != null) {
1043 out.attribute(null, ATTR_REALACTIVITY, realActivity.flattenToShortString());
1044 }
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001045 out.attribute(null, ATTR_REALACTIVITY_SUSPENDED, String.valueOf(realActivitySuspended));
Craig Mautner21d24a22014-04-23 11:45:37 -07001046 if (origActivity != null) {
1047 out.attribute(null, ATTR_ORIGACTIVITY, origActivity.flattenToShortString());
1048 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001049 // Write affinity, and root affinity if it is different from affinity.
1050 // We use the special string "@" for a null root affinity, so we can identify
1051 // later whether we were given a root affinity or should just make it the
1052 // same as the affinity.
Craig Mautner21d24a22014-04-23 11:45:37 -07001053 if (affinity != null) {
1054 out.attribute(null, ATTR_AFFINITY, affinity);
Dianne Hackborn79228822014-09-16 11:11:23 -07001055 if (!affinity.equals(rootAffinity)) {
1056 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
1057 }
1058 } else if (rootAffinity != null) {
1059 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
Craig Mautner21d24a22014-04-23 11:45:37 -07001060 }
1061 out.attribute(null, ATTR_ROOTHASRESET, String.valueOf(rootWasReset));
Dianne Hackborn13420f22014-07-18 15:43:56 -07001062 out.attribute(null, ATTR_AUTOREMOVERECENTS, String.valueOf(autoRemoveRecents));
Craig Mautner21d24a22014-04-23 11:45:37 -07001063 out.attribute(null, ATTR_ASKEDCOMPATMODE, String.valueOf(askedCompatMode));
1064 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001065 out.attribute(null, ATTR_EFFECTIVE_UID, String.valueOf(effectiveUid));
Craig Mautner21d24a22014-04-23 11:45:37 -07001066 out.attribute(null, ATTR_TASKTYPE, String.valueOf(taskType));
Winson Chungffa2ec62014-07-03 15:54:42 -07001067 out.attribute(null, ATTR_FIRSTACTIVETIME, String.valueOf(firstActiveTime));
Winson Chungf1fbd772014-06-24 18:06:58 -07001068 out.attribute(null, ATTR_LASTACTIVETIME, String.valueOf(lastActiveTime));
Craig Mautner21d24a22014-04-23 11:45:37 -07001069 out.attribute(null, ATTR_LASTTIMEMOVED, String.valueOf(mLastTimeMoved));
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001070 out.attribute(null, ATTR_NEVERRELINQUISH, String.valueOf(mNeverRelinquishIdentity));
Craig Mautner21d24a22014-04-23 11:45:37 -07001071 if (lastDescription != null) {
1072 out.attribute(null, ATTR_LASTDESCRIPTION, lastDescription.toString());
1073 }
Winson Chung2cb86c72014-06-25 12:03:30 -07001074 if (lastTaskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07001075 lastTaskDescription.saveToXml(out);
Winson Chung2cb86c72014-06-25 12:03:30 -07001076 }
Winsonc809cbb2015-11-02 12:06:15 -08001077 mLastThumbnailInfo.saveToXml(out);
Winson Chungec396d62014-08-06 17:08:00 -07001078 out.attribute(null, ATTR_TASK_AFFILIATION_COLOR, String.valueOf(mAffiliatedTaskColor));
Craig Mautnera228ae92014-07-09 05:44:55 -07001079 out.attribute(null, ATTR_TASK_AFFILIATION, String.valueOf(mAffiliatedTaskId));
1080 out.attribute(null, ATTR_PREV_AFFILIATION, String.valueOf(mPrevAffiliateTaskId));
1081 out.attribute(null, ATTR_NEXT_AFFILIATION, String.valueOf(mNextAffiliateTaskId));
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001082 out.attribute(null, ATTR_CALLING_UID, String.valueOf(mCallingUid));
1083 out.attribute(null, ATTR_CALLING_PACKAGE, mCallingPackage == null ? "" : mCallingPackage);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001084 out.attribute(null, ATTR_RESIZE_MODE, String.valueOf(mResizeMode));
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001085 out.attribute(null, ATTR_PRIVILEGED, String.valueOf(mPrivileged));
Wale Ogunwale706ed792015-08-02 10:29:44 -07001086 if (mLastNonFullscreenBounds != null) {
1087 out.attribute(
1088 null, ATTR_NON_FULLSCREEN_BOUNDS, mLastNonFullscreenBounds.flattenToString());
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001089 }
Winson Chung2cb86c72014-06-25 12:03:30 -07001090
Craig Mautner21d24a22014-04-23 11:45:37 -07001091 if (affinityIntent != null) {
1092 out.startTag(null, TAG_AFFINITYINTENT);
1093 affinityIntent.saveToXml(out);
1094 out.endTag(null, TAG_AFFINITYINTENT);
1095 }
1096
1097 out.startTag(null, TAG_INTENT);
1098 intent.saveToXml(out);
1099 out.endTag(null, TAG_INTENT);
1100
1101 final ArrayList<ActivityRecord> activities = mActivities;
1102 final int numActivities = activities.size();
1103 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1104 final ActivityRecord r = activities.get(activityNdx);
Craig Mautner43e52ed2014-06-16 17:18:52 -07001105 if (r.info.persistableMode == ActivityInfo.PERSIST_ROOT_ONLY || !r.isPersistable() ||
Wale Ogunwale843bfb92015-03-27 11:06:48 -07001106 ((r.intent.getFlags() & FLAG_ACTIVITY_NEW_DOCUMENT
1107 | FLAG_ACTIVITY_RETAIN_IN_RECENTS) == FLAG_ACTIVITY_NEW_DOCUMENT) &&
Craig Mautner43e52ed2014-06-16 17:18:52 -07001108 activityNdx > 0) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001109 // Stop at first non-persistable or first break in task (CLEAR_WHEN_TASK_RESET).
Craig Mautner21d24a22014-04-23 11:45:37 -07001110 break;
1111 }
1112 out.startTag(null, TAG_ACTIVITY);
1113 r.saveToXml(out);
1114 out.endTag(null, TAG_ACTIVITY);
1115 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001116 }
1117
1118 static TaskRecord restoreFromXml(XmlPullParser in, ActivityStackSupervisor stackSupervisor)
1119 throws IOException, XmlPullParserException {
1120 Intent intent = null;
1121 Intent affinityIntent = null;
Winsonc809cbb2015-11-02 12:06:15 -08001122 ArrayList<ActivityRecord> activities = new ArrayList<>();
Craig Mautner21d24a22014-04-23 11:45:37 -07001123 ComponentName realActivity = null;
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001124 boolean realActivitySuspended = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001125 ComponentName origActivity = null;
1126 String affinity = null;
Dianne Hackborn79228822014-09-16 11:11:23 -07001127 String rootAffinity = null;
1128 boolean hasRootAffinity = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001129 boolean rootHasReset = false;
Dianne Hackborn13420f22014-07-18 15:43:56 -07001130 boolean autoRemoveRecents = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001131 boolean askedCompatMode = false;
1132 int taskType = ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner21d24a22014-04-23 11:45:37 -07001133 int userId = 0;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001134 int effectiveUid = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -07001135 String lastDescription = null;
Winson Chungffa2ec62014-07-03 15:54:42 -07001136 long firstActiveTime = -1;
Winson Chung2cb86c72014-06-25 12:03:30 -07001137 long lastActiveTime = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -07001138 long lastTimeOnTop = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001139 boolean neverRelinquishIdentity = true;
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001140 int taskId = INVALID_TASK_ID;
Craig Mautner21d24a22014-04-23 11:45:37 -07001141 final int outerDepth = in.getDepth();
Craig Mautner648f69b2014-09-18 14:16:26 -07001142 TaskDescription taskDescription = new TaskDescription();
Winsonc809cbb2015-11-02 12:06:15 -08001143 TaskThumbnailInfo thumbnailInfo = new TaskThumbnailInfo();
Wale Ogunwale18795a22014-12-03 11:38:33 -08001144 int taskAffiliation = INVALID_TASK_ID;
Winson Chungec396d62014-08-06 17:08:00 -07001145 int taskAffiliationColor = 0;
Wale Ogunwale18795a22014-12-03 11:38:33 -08001146 int prevTaskId = INVALID_TASK_ID;
1147 int nextTaskId = INVALID_TASK_ID;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001148 int callingUid = -1;
1149 String callingPackage = "";
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001150 int resizeMode = RESIZE_MODE_UNRESIZEABLE;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001151 boolean privileged = false;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001152 Rect bounds = null;
Craig Mautner21d24a22014-04-23 11:45:37 -07001153
1154 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
1155 final String attrName = in.getAttributeName(attrNdx);
1156 final String attrValue = in.getAttributeValue(attrNdx);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001157 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: attribute name=" +
1158 attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001159 if (ATTR_TASKID.equals(attrName)) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001160 if (taskId == INVALID_TASK_ID) taskId = Integer.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001161 } else if (ATTR_REALACTIVITY.equals(attrName)) {
1162 realActivity = ComponentName.unflattenFromString(attrValue);
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001163 } else if (ATTR_REALACTIVITY_SUSPENDED.equals(attrName)) {
1164 realActivitySuspended = Boolean.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001165 } else if (ATTR_ORIGACTIVITY.equals(attrName)) {
1166 origActivity = ComponentName.unflattenFromString(attrValue);
1167 } else if (ATTR_AFFINITY.equals(attrName)) {
1168 affinity = attrValue;
Dianne Hackborn79228822014-09-16 11:11:23 -07001169 } else if (ATTR_ROOT_AFFINITY.equals(attrName)) {
1170 rootAffinity = attrValue;
1171 hasRootAffinity = true;
Craig Mautner21d24a22014-04-23 11:45:37 -07001172 } else if (ATTR_ROOTHASRESET.equals(attrName)) {
1173 rootHasReset = Boolean.valueOf(attrValue);
Dianne Hackborn13420f22014-07-18 15:43:56 -07001174 } else if (ATTR_AUTOREMOVERECENTS.equals(attrName)) {
1175 autoRemoveRecents = Boolean.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001176 } else if (ATTR_ASKEDCOMPATMODE.equals(attrName)) {
1177 askedCompatMode = Boolean.valueOf(attrValue);
1178 } else if (ATTR_USERID.equals(attrName)) {
1179 userId = Integer.valueOf(attrValue);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001180 } else if (ATTR_EFFECTIVE_UID.equals(attrName)) {
1181 effectiveUid = Integer.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001182 } else if (ATTR_TASKTYPE.equals(attrName)) {
1183 taskType = Integer.valueOf(attrValue);
Winson Chungffa2ec62014-07-03 15:54:42 -07001184 } else if (ATTR_FIRSTACTIVETIME.equals(attrName)) {
1185 firstActiveTime = Long.valueOf(attrValue);
Winson Chungf1fbd772014-06-24 18:06:58 -07001186 } else if (ATTR_LASTACTIVETIME.equals(attrName)) {
1187 lastActiveTime = Long.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001188 } else if (ATTR_LASTDESCRIPTION.equals(attrName)) {
1189 lastDescription = attrValue;
1190 } else if (ATTR_LASTTIMEMOVED.equals(attrName)) {
1191 lastTimeOnTop = Long.valueOf(attrValue);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001192 } else if (ATTR_NEVERRELINQUISH.equals(attrName)) {
1193 neverRelinquishIdentity = Boolean.valueOf(attrValue);
Winsonc809cbb2015-11-02 12:06:15 -08001194 } else if (attrName.startsWith(TaskThumbnailInfo.ATTR_TASK_THUMBNAILINFO_PREFIX)) {
1195 thumbnailInfo.restoreFromXml(attrName, attrValue);
Craig Mautner648f69b2014-09-18 14:16:26 -07001196 } else if (attrName.startsWith(TaskDescription.ATTR_TASKDESCRIPTION_PREFIX)) {
1197 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001198 } else if (ATTR_TASK_AFFILIATION.equals(attrName)) {
1199 taskAffiliation = Integer.valueOf(attrValue);
1200 } else if (ATTR_PREV_AFFILIATION.equals(attrName)) {
1201 prevTaskId = Integer.valueOf(attrValue);
1202 } else if (ATTR_NEXT_AFFILIATION.equals(attrName)) {
1203 nextTaskId = Integer.valueOf(attrValue);
Winson Chungec396d62014-08-06 17:08:00 -07001204 } else if (ATTR_TASK_AFFILIATION_COLOR.equals(attrName)) {
1205 taskAffiliationColor = Integer.valueOf(attrValue);
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001206 } else if (ATTR_CALLING_UID.equals(attrName)) {
1207 callingUid = Integer.valueOf(attrValue);
1208 } else if (ATTR_CALLING_PACKAGE.equals(attrName)) {
1209 callingPackage = attrValue;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001210 } else if (ATTR_RESIZEABLE.equals(attrName)) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001211 resizeMode = Boolean.valueOf(attrValue)
1212 ? RESIZE_MODE_RESIZEABLE : RESIZE_MODE_CROP_WINDOWS;
1213 } else if (ATTR_RESIZE_MODE.equals(attrName)) {
1214 resizeMode = Integer.valueOf(attrValue);
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001215 } else if (ATTR_PRIVILEGED.equals(attrName)) {
1216 privileged = Boolean.valueOf(attrValue);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001217 } else if (ATTR_NON_FULLSCREEN_BOUNDS.equals(attrName)) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001218 bounds = Rect.unflattenFromString(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001219 } else {
1220 Slog.w(TAG, "TaskRecord: Unknown attribute=" + attrName);
1221 }
1222 }
1223
1224 int event;
1225 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
Ben Kwa8814cf42015-07-08 10:54:56 -07001226 (event != XmlPullParser.END_TAG || in.getDepth() >= outerDepth)) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001227 if (event == XmlPullParser.START_TAG) {
1228 final String name = in.getName();
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001229 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: START_TAG name=" +
1230 name);
Craig Mautner21d24a22014-04-23 11:45:37 -07001231 if (TAG_AFFINITYINTENT.equals(name)) {
1232 affinityIntent = Intent.restoreFromXml(in);
1233 } else if (TAG_INTENT.equals(name)) {
1234 intent = Intent.restoreFromXml(in);
1235 } else if (TAG_ACTIVITY.equals(name)) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001236 ActivityRecord activity = ActivityRecord.restoreFromXml(in, stackSupervisor);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001237 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: activity=" +
1238 activity);
Craig Mautner21d24a22014-04-23 11:45:37 -07001239 if (activity != null) {
1240 activities.add(activity);
1241 }
1242 } else {
1243 Slog.e(TAG, "restoreTask: Unexpected name=" + name);
1244 XmlUtils.skipCurrentTag(in);
1245 }
1246 }
1247 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001248 if (!hasRootAffinity) {
1249 rootAffinity = affinity;
1250 } else if ("@".equals(rootAffinity)) {
1251 rootAffinity = null;
1252 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001253 if (effectiveUid <= 0) {
1254 Intent checkIntent = intent != null ? intent : affinityIntent;
1255 effectiveUid = 0;
1256 if (checkIntent != null) {
1257 IPackageManager pm = AppGlobals.getPackageManager();
1258 try {
1259 ApplicationInfo ai = pm.getApplicationInfo(
1260 checkIntent.getComponent().getPackageName(),
1261 PackageManager.GET_UNINSTALLED_PACKAGES
1262 | PackageManager.GET_DISABLED_COMPONENTS, userId);
1263 if (ai != null) {
1264 effectiveUid = ai.uid;
1265 }
1266 } catch (RemoteException e) {
1267 }
1268 }
1269 Slog.w(TAG, "Updating task #" + taskId + " for " + checkIntent
1270 + ": effectiveUid=" + effectiveUid);
1271 }
1272
Craig Mautner21d24a22014-04-23 11:45:37 -07001273 final TaskRecord task = new TaskRecord(stackSupervisor.mService, taskId, intent,
Dianne Hackborn79228822014-09-16 11:11:23 -07001274 affinityIntent, affinity, rootAffinity, realActivity, origActivity, rootHasReset,
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001275 autoRemoveRecents, askedCompatMode, taskType, userId, effectiveUid, lastDescription,
1276 activities, firstActiveTime, lastActiveTime, lastTimeOnTop, neverRelinquishIdentity,
Winsonc809cbb2015-11-02 12:06:15 -08001277 taskDescription, thumbnailInfo, taskAffiliation, prevTaskId, nextTaskId,
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001278 taskAffiliationColor, callingUid, callingPackage, resizeMode, privileged,
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001279 realActivitySuspended);
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07001280 task.updateOverrideConfiguration(bounds);
Craig Mautner21d24a22014-04-23 11:45:37 -07001281
1282 for (int activityNdx = activities.size() - 1; activityNdx >=0; --activityNdx) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001283 activities.get(activityNdx).task = task;
Craig Mautner21d24a22014-04-23 11:45:37 -07001284 }
1285
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001286 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Restored task=" + task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001287 return task;
1288 }
1289
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001290 /**
1291 * Update task's override configuration based on the bounds.
1292 * @return Update configuration or null if there is no change.
1293 */
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07001294 Configuration updateOverrideConfiguration(Rect bounds) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001295 if (Objects.equals(mBounds, bounds)) {
1296 return null;
1297 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001298 final Configuration oldConfig = mOverrideConfig;
1299 final boolean oldFullscreen = mFullscreen;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001300
1301 mFullscreen = bounds == null;
Wale Ogunwale706ed792015-08-02 10:29:44 -07001302 if (mFullscreen) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001303 if (mBounds != null && StackId.persistTaskBounds(stack.mStackId)) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001304 mLastNonFullscreenBounds = mBounds;
1305 }
1306 mBounds = null;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001307 mOverrideConfig = Configuration.EMPTY;
Wale Ogunwale706ed792015-08-02 10:29:44 -07001308 } else {
Chong Zhangb15758a2015-11-17 12:12:03 -08001309 if (mBounds == null) {
1310 mBounds = new Rect(bounds);
1311 } else {
1312 mBounds.set(bounds);
1313 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001314 if (stack == null || StackId.persistTaskBounds(stack.mStackId)) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001315 mLastNonFullscreenBounds = mBounds;
1316 }
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08001317 mOverrideConfig = calculateOverrideConfig(mBounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001318 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001319
1320 if (mFullscreen != oldFullscreen) {
1321 reportMultiWindowModeChange();
1322 }
1323
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001324 return !mOverrideConfig.equals(oldConfig) ? mOverrideConfig : null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001325 }
1326
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08001327 Configuration calculateOverrideConfig(Rect bounds) {
1328 final Configuration serviceConfig = mService.mConfiguration;
1329 final Configuration config = new Configuration(Configuration.EMPTY);
1330 // TODO(multidisplay): Update Dp to that of display stack is on.
1331 final float density = serviceConfig.densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE;
1332 config.screenWidthDp =
1333 Math.min((int)(bounds.width() / density), serviceConfig.screenWidthDp);
1334 config.screenHeightDp =
1335 Math.min((int)(bounds.height() / density), serviceConfig.screenHeightDp);
1336 config.smallestScreenWidthDp =
1337 Math.min(config.screenWidthDp, config.screenHeightDp);
1338 config.orientation = (config.screenWidthDp <= config.screenHeightDp)
1339 ? Configuration.ORIENTATION_PORTRAIT
1340 : Configuration.ORIENTATION_LANDSCAPE;
1341 final int sl = Configuration.resetScreenLayout(serviceConfig.screenLayout);
1342 config.screenLayout = Configuration.reduceScreenLayout(
1343 sl, config.screenWidthDp, config.screenHeightDp);
1344 return config;
1345 }
1346
1347 /**
1348 * Using the existing configuration {@param config}, creates a new task override config such
1349 * that all the fields that are usually set in an override config are set to the ones in
1350 * {@param config}.
1351 */
1352 Configuration extractOverrideConfig(Configuration config) {
1353 final Configuration extracted = new Configuration(Configuration.EMPTY);
1354 extracted.screenWidthDp = config.screenWidthDp;
1355 extracted.screenHeightDp = config.screenHeightDp;
1356 extracted.smallestScreenWidthDp = config.smallestScreenWidthDp;
1357 extracted.orientation = config.orientation;
1358 extracted.screenLayout = config.screenLayout;
1359 return extracted;
1360 }
1361
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001362 Rect updateOverrideConfigurationFromLaunchBounds() {
1363 final Rect bounds = validateBounds(getLaunchBounds());
1364 updateOverrideConfiguration(bounds);
1365 return bounds;
1366 }
1367
1368 static Rect validateBounds(Rect bounds) {
1369 if (bounds != null && bounds.isEmpty()) {
1370 Slog.wtf(TAG, "Received strange task bounds: " + bounds, new Throwable());
1371 return null;
1372 }
1373 return bounds;
1374 }
1375
Wale Ogunwale5f986092015-12-04 15:35:38 -08001376 private void reportMultiWindowModeChange() {
1377 for (int i = mActivities.size() - 1; i >= 0; i--) {
1378 final ActivityRecord r = mActivities.get(i);
1379 if (r.app != null && r.app.thread != null) {
1380 try {
1381 // An activity is consider to be in multi-window mode if its task isn't
1382 // fullscreen.
1383 r.app.thread.scheduleMultiWindowModeChanged(r.appToken, !mFullscreen);
1384 } catch (Exception e) {
1385 Slog.e(TAG, "TaskRecord.reportMultiWindowModeChange: ", e);
1386 }
1387 }
1388 }
1389 }
1390
Wale Ogunwaled046a012015-12-24 13:05:59 -08001391 void reportPictureInPictureModeChangeIfNeeded(ActivityStack prevStack) {
1392 if (prevStack == null || prevStack == stack
1393 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
1394 return;
1395 }
1396
Wale Ogunwale5f986092015-12-04 15:35:38 -08001397 for (int i = mActivities.size() - 1; i >= 0; i--) {
1398 final ActivityRecord r = mActivities.get(i);
1399 if (r.app != null && r.app.thread != null) {
1400 try {
1401 r.app.thread.schedulePictureInPictureModeChanged(
1402 r.appToken, stack.mStackId == PINNED_STACK_ID);
1403 } catch (Exception e) {
1404 Slog.e(TAG, "TaskRecord.reportMultiWindowModeChange: ", e);
1405 }
1406 }
1407 }
1408 }
1409
Wale Ogunwale935e5022015-11-10 12:36:10 -08001410 /** Updates the task's bounds and override configuration to match what is expected for the
1411 * input stack. */
1412 void updateOverrideConfigurationForStack(ActivityStack inStack) {
1413 if (stack != null && stack == inStack) {
1414 return;
1415 }
1416
1417 if (inStack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001418 if (!isResizeable()) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08001419 throw new IllegalArgumentException("Can not position non-resizeable task="
1420 + this + " in stack=" + inStack);
1421 }
1422 if (mBounds != null) {
1423 return;
1424 }
1425 if (mLastNonFullscreenBounds != null) {
1426 updateOverrideConfiguration(mLastNonFullscreenBounds);
1427 } else {
1428 inStack.layoutTaskInStack(this, null);
1429 }
1430 } else {
1431 updateOverrideConfiguration(inStack.mBounds);
1432 }
1433 }
1434
Chong Zhang0fa656b2015-08-31 15:17:21 -07001435 /**
1436 * Returns the correct stack to use based on task type and currently set bounds,
1437 * regardless of the focused stack and current stack association of the task.
1438 * The task will be moved (and stack focus changed) later if necessary.
1439 */
1440 int getLaunchStackId() {
1441 if (!isApplicationTask()) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001442 return HOME_STACK_ID;
1443 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001444 if (mBounds != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001445 return FREEFORM_WORKSPACE_STACK_ID;
1446 }
1447 return FULLSCREEN_WORKSPACE_STACK_ID;
1448 }
1449
1450 /** Returns the bounds that should be used to launch this task. */
1451 Rect getLaunchBounds() {
Chong Zhang75b37202015-12-04 14:16:36 -08001452 // If we're over lockscreen, forget about stack bounds and use fullscreen.
1453 if (mService.mLockScreenShown == LOCK_SCREEN_SHOWN) {
1454 return null;
1455 }
1456
Chong Zhang7d5f5102016-01-13 10:29:24 -08001457 if (stack == null) {
1458 return null;
1459 }
1460
1461 final int stackId = stack.mStackId;
1462 if (stackId == HOME_STACK_ID
1463 || stackId == FULLSCREEN_WORKSPACE_STACK_ID
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001464 || (stackId == DOCKED_STACK_ID && !isResizeable())) {
1465 return isResizeable() ? stack.mBounds : null;
Wale Ogunwale3797c222015-10-27 14:21:58 -07001466 } else if (!StackId.persistTaskBounds(stackId)) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001467 return stack.mBounds;
Wale Ogunwale706ed792015-08-02 10:29:44 -07001468 }
1469 return mLastNonFullscreenBounds;
1470 }
1471
Wale Ogunwale39381972015-12-17 17:15:29 -08001472 boolean canMatchRootAffinity() {
1473 // We don't allow root affinity matching on the pinned stack as no other task should
1474 // be launching in it based on affinity.
1475 return rootAffinity != null && (stack == null || stack.mStackId != PINNED_STACK_ID);
1476 }
1477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001479 pw.print(prefix); pw.print("userId="); pw.print(userId);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001480 pw.print(" effectiveUid="); UserHandle.formatUid(pw, effectiveUid);
1481 pw.print(" mCallingUid="); UserHandle.formatUid(pw, mCallingUid);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001482 pw.print(" mCallingPackage="); pw.println(mCallingPackage);
Dianne Hackborn79228822014-09-16 11:11:23 -07001483 if (affinity != null || rootAffinity != null) {
1484 pw.print(prefix); pw.print("affinity="); pw.print(affinity);
1485 if (affinity == null || !affinity.equals(rootAffinity)) {
1486 pw.print(" root="); pw.println(rootAffinity);
1487 } else {
1488 pw.println();
1489 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001490 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07001491 if (voiceSession != null || voiceInteractor != null) {
1492 pw.print(prefix); pw.print("VOICE: session=0x");
1493 pw.print(Integer.toHexString(System.identityHashCode(voiceSession)));
1494 pw.print(" interactor=0x");
1495 pw.println(Integer.toHexString(System.identityHashCode(voiceInteractor)));
1496 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001497 if (intent != null) {
1498 StringBuilder sb = new StringBuilder(128);
1499 sb.append(prefix); sb.append("intent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08001500 intent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001501 sb.append('}');
1502 pw.println(sb.toString());
1503 }
1504 if (affinityIntent != null) {
1505 StringBuilder sb = new StringBuilder(128);
1506 sb.append(prefix); sb.append("affinityIntent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08001507 affinityIntent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001508 sb.append('}');
1509 pw.println(sb.toString());
1510 }
1511 if (origActivity != null) {
1512 pw.print(prefix); pw.print("origActivity=");
1513 pw.println(origActivity.flattenToShortString());
1514 }
1515 if (realActivity != null) {
1516 pw.print(prefix); pw.print("realActivity=");
1517 pw.println(realActivity.flattenToShortString());
1518 }
Dianne Hackborn852975d2014-08-22 17:42:43 -07001519 if (autoRemoveRecents || isPersistable || taskType != 0 || mTaskToReturnTo != 0
1520 || numFullscreen != 0) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001521 pw.print(prefix); pw.print("autoRemoveRecents="); pw.print(autoRemoveRecents);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001522 pw.print(" isPersistable="); pw.print(isPersistable);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001523 pw.print(" numFullscreen="); pw.print(numFullscreen);
1524 pw.print(" taskType="); pw.print(taskType);
1525 pw.print(" mTaskToReturnTo="); pw.println(mTaskToReturnTo);
1526 }
Craig Mautner432f64e2015-05-20 14:59:57 -07001527 if (rootWasReset || mNeverRelinquishIdentity || mReuseTask
1528 || mLockTaskAuth != LOCK_TASK_AUTH_PINNABLE) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001529 pw.print(prefix); pw.print("rootWasReset="); pw.print(rootWasReset);
1530 pw.print(" mNeverRelinquishIdentity="); pw.print(mNeverRelinquishIdentity);
Craig Mautner432f64e2015-05-20 14:59:57 -07001531 pw.print(" mReuseTask="); pw.print(mReuseTask);
1532 pw.print(" mLockTaskAuth="); pw.println(lockTaskAuthToString());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001533 }
Wale Ogunwale18795a22014-12-03 11:38:33 -08001534 if (mAffiliatedTaskId != taskId || mPrevAffiliateTaskId != INVALID_TASK_ID
1535 || mPrevAffiliate != null || mNextAffiliateTaskId != INVALID_TASK_ID
1536 || mNextAffiliate != null) {
Dianne Hackborn852975d2014-08-22 17:42:43 -07001537 pw.print(prefix); pw.print("affiliation="); pw.print(mAffiliatedTaskId);
1538 pw.print(" prevAffiliation="); pw.print(mPrevAffiliateTaskId);
1539 pw.print(" (");
1540 if (mPrevAffiliate == null) {
1541 pw.print("null");
1542 } else {
1543 pw.print(Integer.toHexString(System.identityHashCode(mPrevAffiliate)));
1544 }
1545 pw.print(") nextAffiliation="); pw.print(mNextAffiliateTaskId);
1546 pw.print(" (");
1547 if (mNextAffiliate == null) {
1548 pw.print("null");
1549 } else {
1550 pw.print(Integer.toHexString(System.identityHashCode(mNextAffiliate)));
1551 }
1552 pw.println(")");
1553 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001554 pw.print(prefix); pw.print("Activities="); pw.println(mActivities);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001555 if (!askedCompatMode || !inRecents || !isAvailable) {
1556 pw.print(prefix); pw.print("askedCompatMode="); pw.print(askedCompatMode);
1557 pw.print(" inRecents="); pw.print(inRecents);
1558 pw.print(" isAvailable="); pw.println(isAvailable);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07001559 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001560 pw.print(prefix); pw.print("lastThumbnail="); pw.print(mLastThumbnail);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001561 pw.print(" lastThumbnailFile="); pw.println(mLastThumbnailFile);
1562 if (lastDescription != null) {
1563 pw.print(prefix); pw.print("lastDescription="); pw.println(lastDescription);
1564 }
Wale Ogunwale069fbe42015-07-29 11:38:01 -07001565 if (stack != null) {
1566 pw.print(prefix); pw.print("stackId="); pw.println(stack.mStackId);
1567 }
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001568 pw.print(prefix + "hasBeenVisible=" + hasBeenVisible);
1569 pw.print(" mResizeMode=" + ActivityInfo.resizeModeToString(mResizeMode));
1570 pw.print(" isResizeable=" + isResizeable());
1571 pw.print(" firstActiveTime=" + lastActiveTime);
1572 pw.print(" lastActiveTime=" + lastActiveTime);
1573 pw.println(" (inactive for " + (getInactiveDuration() / 1000) + "s)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 }
1575
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001576 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001578 StringBuilder sb = new StringBuilder(128);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001579 if (stringName != null) {
1580 sb.append(stringName);
1581 sb.append(" U=");
1582 sb.append(userId);
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08001583 sb.append(" StackId=");
1584 sb.append(stack != null ? stack.mStackId : INVALID_STACK_ID);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001585 sb.append(" sz=");
1586 sb.append(mActivities.size());
1587 sb.append('}');
1588 return sb.toString();
1589 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001590 sb.append("TaskRecord{");
1591 sb.append(Integer.toHexString(System.identityHashCode(this)));
1592 sb.append(" #");
1593 sb.append(taskId);
1594 if (affinity != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001595 sb.append(" A=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001596 sb.append(affinity);
1597 } else if (intent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001598 sb.append(" I=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001599 sb.append(intent.getComponent().flattenToShortString());
1600 } else if (affinityIntent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001601 sb.append(" aI=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001602 sb.append(affinityIntent.getComponent().flattenToShortString());
1603 } else {
1604 sb.append(" ??");
1605 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001606 stringName = sb.toString();
1607 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 }
1609}