blob: add1b9642affdd1d67ec365a55fd2ab3ff4f6309 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Jorim Jaggi0a932142016-02-01 17:42:25 -080019import android.annotation.Nullable;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -070020import android.app.Activity;
Craig Mautner9db9a0b2013-04-29 17:05:56 -070021import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070022import android.app.ActivityManager.StackId;
Craig Mautner648f69b2014-09-18 14:16:26 -070023import android.app.ActivityManager.TaskDescription;
Wale Ogunwaleff3c66c2015-11-18 19:22:49 -080024import android.app.ActivityManager.TaskThumbnail;
Winsonc809cbb2015-11-02 12:06:15 -080025import android.app.ActivityManager.TaskThumbnailInfo;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -070026import android.app.ActivityOptions;
Dianne Hackborn885fbe52014-08-23 15:23:58 -070027import android.app.AppGlobals;
Jorim Jaggi0a932142016-02-01 17:42:25 -080028import android.app.IActivityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.ComponentName;
30import android.content.Intent;
31import android.content.pm.ActivityInfo;
Dianne Hackborn885fbe52014-08-23 15:23:58 -070032import android.content.pm.ApplicationInfo;
33import android.content.pm.IPackageManager;
34import android.content.pm.PackageManager;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070035import android.content.res.Configuration;
Craig Mautner9db9a0b2013-04-29 17:05:56 -070036import android.graphics.Bitmap;
Winsonc809cbb2015-11-02 12:06:15 -080037import android.graphics.Point;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070038import android.graphics.Rect;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070039import android.os.Debug;
Craig Mautnerc0ffce52014-07-01 12:38:52 -070040import android.os.ParcelFileDescriptor;
Dianne Hackborn885fbe52014-08-23 15:23:58 -070041import android.os.RemoteException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070042import android.os.UserHandle;
Dianne Hackborn91097de2014-04-04 18:02:06 -070043import android.service.voice.IVoiceInteractionSession;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070044import android.util.DisplayMetrics;
Dianne Hackborn7f96b792012-05-29 18:46:45 -070045import android.util.Slog;
Suprabh Shukla23593142015-11-03 17:31:15 -080046
Dianne Hackborn91097de2014-04-04 18:02:06 -070047import com.android.internal.app.IVoiceInteractor;
Craig Mautner21d24a22014-04-23 11:45:37 -070048import com.android.internal.util.XmlUtils;
Suprabh Shukla23593142015-11-03 17:31:15 -080049
Craig Mautner21d24a22014-04-23 11:45:37 -070050import org.xmlpull.v1.XmlPullParser;
51import org.xmlpull.v1.XmlPullParserException;
52import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
Craig Mautnerc0ffce52014-07-01 12:38:52 -070054import java.io.File;
Craig Mautner21d24a22014-04-23 11:45:37 -070055import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import java.io.PrintWriter;
Craig Mautner5d9c7be2013-02-15 14:02:56 -080057import java.util.ArrayList;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070058import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
Jorim Jaggi0a932142016-02-01 17:42:25 -080060import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
61import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
62import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
63import static android.app.ActivityManager.StackId.HOME_STACK_ID;
64import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
65import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
66import static android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
67import static android.content.Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
68import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_ALWAYS;
69import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_DEFAULT;
70import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
71import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_NEVER;
72import static android.content.pm.ActivityInfo.RESIZE_MODE_CROP_WINDOWS;
Wale Ogunwaled829d362016-02-10 19:24:49 -080073import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Jorim Jaggi0a932142016-02-01 17:42:25 -080074import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE;
75import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
76import static android.content.pm.ApplicationInfo.PRIVATE_FLAG_PRIVILEGED;
77import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ADD_REMOVE;
78import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
79import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
80import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
81import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_ADD_REMOVE;
82import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
83import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
84import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
85import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
86import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
87import static com.android.server.am.ActivityManagerService.LOCK_SCREEN_SHOWN;
88import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
89import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
90import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
91
Craig Mautnerc0ffce52014-07-01 12:38:52 -070092final class TaskRecord {
Wale Ogunwalee23149f2015-03-06 15:39:44 -080093 private static final String TAG = TAG_WITH_CLASS_NAME ? "TaskRecord" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070094 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070095 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Craig Mautnere0570202015-05-13 13:06:11 -070096 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070097 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080098
Wale Ogunwale18795a22014-12-03 11:38:33 -080099 static final String ATTR_TASKID = "task_id";
Craig Mautner21d24a22014-04-23 11:45:37 -0700100 private static final String TAG_INTENT = "intent";
101 private static final String TAG_AFFINITYINTENT = "affinity_intent";
Wale Ogunwale18795a22014-12-03 11:38:33 -0800102 static final String ATTR_REALACTIVITY = "real_activity";
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +0000103 static final String ATTR_REALACTIVITY_SUSPENDED = "real_activity_suspended";
Craig Mautner21d24a22014-04-23 11:45:37 -0700104 private static final String ATTR_ORIGACTIVITY = "orig_activity";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700105 private static final String TAG_ACTIVITY = "activity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700106 private static final String ATTR_AFFINITY = "affinity";
Dianne Hackborn79228822014-09-16 11:11:23 -0700107 private static final String ATTR_ROOT_AFFINITY = "root_affinity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700108 private static final String ATTR_ROOTHASRESET = "root_has_reset";
Dianne Hackborn13420f22014-07-18 15:43:56 -0700109 private static final String ATTR_AUTOREMOVERECENTS = "auto_remove_recents";
Craig Mautner21d24a22014-04-23 11:45:37 -0700110 private static final String ATTR_ASKEDCOMPATMODE = "asked_compat_mode";
111 private static final String ATTR_USERID = "user_id";
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800112 private static final String ATTR_USER_SETUP_COMPLETE = "user_setup_complete";
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700113 private static final String ATTR_EFFECTIVE_UID = "effective_uid";
Craig Mautner21d24a22014-04-23 11:45:37 -0700114 private static final String ATTR_TASKTYPE = "task_type";
Winson Chungffa2ec62014-07-03 15:54:42 -0700115 private static final String ATTR_FIRSTACTIVETIME = "first_active_time";
Winson Chungf1fbd772014-06-24 18:06:58 -0700116 private static final String ATTR_LASTACTIVETIME = "last_active_time";
Craig Mautner21d24a22014-04-23 11:45:37 -0700117 private static final String ATTR_LASTDESCRIPTION = "last_description";
118 private static final String ATTR_LASTTIMEMOVED = "last_time_moved";
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700119 private static final String ATTR_NEVERRELINQUISH = "never_relinquish_identity";
Wale Ogunwale18795a22014-12-03 11:38:33 -0800120 static final String ATTR_TASK_AFFILIATION = "task_affiliation";
Craig Mautnera228ae92014-07-09 05:44:55 -0700121 private static final String ATTR_PREV_AFFILIATION = "prev_affiliation";
122 private static final String ATTR_NEXT_AFFILIATION = "next_affiliation";
Winson Chungec396d62014-08-06 17:08:00 -0700123 private static final String ATTR_TASK_AFFILIATION_COLOR = "task_affiliation_color";
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700124 private static final String ATTR_CALLING_UID = "calling_uid";
125 private static final String ATTR_CALLING_PACKAGE = "calling_package";
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800126 private static final String ATTR_RESIZE_MODE = "resize_mode";
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700127 private static final String ATTR_PRIVILEGED = "privileged";
Wale Ogunwale706ed792015-08-02 10:29:44 -0700128 private static final String ATTR_NON_FULLSCREEN_BOUNDS = "non_fullscreen_bounds";
Craig Mautner21d24a22014-04-23 11:45:37 -0700129
130 private static final String TASK_THUMBNAIL_SUFFIX = "_task_thumbnail";
131
Wale Ogunwale18795a22014-12-03 11:38:33 -0800132 static final int INVALID_TASK_ID = -1;
133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 final int taskId; // Unique identifier for this task.
Dianne Hackborn79228822014-09-16 11:11:23 -0700135 String affinity; // The affinity name for this task, or null; may change identity.
136 String rootAffinity; // Initial base affinity, or null; does not change from initial root.
Dianne Hackborn91097de2014-04-04 18:02:06 -0700137 final IVoiceInteractionSession voiceSession; // Voice interaction session driving task
138 final IVoiceInteractor voiceInteractor; // Associated interactor to provide to app
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 Intent intent; // The original intent that started the task.
140 Intent affinityIntent; // Intent of affinity-moved activity that started this task.
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700141 int effectiveUid; // The current effective uid of the identity of this task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 ComponentName origActivity; // The non-alias activity component of the intent.
143 ComponentName realActivity; // The actual activity component that started the task.
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +0000144 boolean realActivitySuspended; // True if the actual activity component that started the
145 // task is suspended.
Winson Chungffa2ec62014-07-03 15:54:42 -0700146 long firstActiveTime; // First time this task was active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 long lastActiveTime; // Last time this task was active, including sleep.
Dianne Hackborn852975d2014-08-22 17:42:43 -0700148 boolean inRecents; // Actually in the recents list?
149 boolean isAvailable; // Is the activity available to be launched?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 boolean rootWasReset; // True if the intent at the root of the task had
151 // the FLAG_ACTIVITY_RESET_TASK_IF_NEEDED flag.
Dianne Hackborn13420f22014-07-18 15:43:56 -0700152 boolean autoRemoveRecents; // If true, we should automatically remove the task from
153 // recents when activity finishes
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700154 boolean askedCompatMode;// Have asked the user about compat mode for this task.
Dianne Hackbornd38aed82014-06-10 21:36:35 -0700155 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 -0800156
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700157 String stringName; // caching of toString() result.
Dianne Hackborn9da2d402012-03-15 13:43:08 -0700158 int userId; // user for which this task was created
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800159 boolean mUserSetupComplete; // The user set-up is complete as of the last time the task activity
160 // was changed.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800161
162 int numFullscreen; // Number of fullscreen activities.
163
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800164 int mResizeMode; // The resize mode of this task and its activities.
165 // Based on the {@link ActivityInfo#resizeMode} of the root activity.
Jorim Jaggi8202b2a2016-02-03 19:24:31 -0800166 boolean mTemporarilyUnresizable; // Separate flag from mResizeMode used to suppress resize
167 // changes on a temporary basis.
Craig Mautner15df08a2015-04-01 12:17:18 -0700168 int mLockTaskMode; // Which tasklock mode to launch this task in. One of
169 // ActivityManager.LOCK_TASK_LAUNCH_MODE_*
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700170 private boolean mPrivileged; // The root activity application of this task holds
171 // privileged permissions.
172
Craig Mautner15df08a2015-04-01 12:17:18 -0700173 /** Can't be put in lockTask mode. */
174 final static int LOCK_TASK_AUTH_DONT_LOCK = 0;
Benjamin Franz469dd582015-06-09 14:24:36 +0100175 /** Can enter app pinning with user approval. Can never start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700176 final static int LOCK_TASK_AUTH_PINNABLE = 1;
177 /** Starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing lockTask task. */
178 final static int LOCK_TASK_AUTH_LAUNCHABLE = 2;
Benjamin Franz469dd582015-06-09 14:24:36 +0100179 /** Can enter lockTask without user approval. Can start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700180 final static int LOCK_TASK_AUTH_WHITELISTED = 3;
Benjamin Franz469dd582015-06-09 14:24:36 +0100181 /** Priv-app that starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing
182 * lockTask task. */
183 final static int LOCK_TASK_AUTH_LAUNCHABLE_PRIV = 4;
Craig Mautner15df08a2015-04-01 12:17:18 -0700184 int mLockTaskAuth = LOCK_TASK_AUTH_PINNABLE;
185
186 int mLockTaskUid = -1; // The uid of the application that called startLockTask().
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800187
Winson Chung03a9bae2014-05-02 09:56:12 -0700188 // This represents the last resolved activity values for this task
189 // NOTE: This value needs to be persisted with each task
Craig Mautner648f69b2014-09-18 14:16:26 -0700190 TaskDescription lastTaskDescription = new TaskDescription();
Winson Chung03a9bae2014-05-02 09:56:12 -0700191
Craig Mautnerd2328952013-03-05 12:46:26 -0800192 /** List of all activities in the task arranged in history order */
Craig Mautner21d24a22014-04-23 11:45:37 -0700193 final ArrayList<ActivityRecord> mActivities;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800194
Craig Mautnerd2328952013-03-05 12:46:26 -0800195 /** Current stack */
196 ActivityStack stack;
197
Craig Mautner2c1faed2013-07-23 12:56:02 -0700198 /** Takes on same set of values as ActivityRecord.mActivityType */
Craig Mautner21d24a22014-04-23 11:45:37 -0700199 int taskType;
Craig Mautner1602ec22013-05-12 10:24:27 -0700200
Craig Mautner21d24a22014-04-23 11:45:37 -0700201 /** Takes on same value as first root activity */
202 boolean isPersistable = false;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700203 int maxRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700204
205 /** Only used for persistable tasks, otherwise 0. The last time this task was moved. Used for
206 * determining the order when restoring. Sign indicates whether last task movement was to front
207 * (positive) or back (negative). Absolute value indicates time. */
208 long mLastTimeMoved = System.currentTimeMillis();
209
Craig Mautner84984fa2014-06-19 11:19:20 -0700210 /** Indication of what to run next when task exits. Use ActivityRecord types.
211 * ActivityRecord.APPLICATION_ACTIVITY_TYPE indicates to resume the task below this one in the
212 * task stack. */
213 private int mTaskToReturnTo = APPLICATION_ACTIVITY_TYPE;
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700214
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700215 /** If original intent did not allow relinquishing task identity, save that information */
216 boolean mNeverRelinquishIdentity = true;
217
Craig Mautner362449a2014-06-20 14:04:39 -0700218 // Used in the unique case where we are clearing the task in order to reuse it. In that case we
219 // do not want to delete the stack when the task goes empty.
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -0800220 private boolean mReuseTask = false;
Craig Mautner362449a2014-06-20 14:04:39 -0700221
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700222 private Bitmap mLastThumbnail; // Last thumbnail captured for this item.
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700223 private final File mLastThumbnailFile; // File containing last thumbnail.
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700224 private final String mFilename;
Winsonc809cbb2015-11-02 12:06:15 -0800225 private TaskThumbnailInfo mLastThumbnailInfo;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700226 CharSequence lastDescription; // Last description captured for this item.
227
Craig Mautnera228ae92014-07-09 05:44:55 -0700228 int mAffiliatedTaskId; // taskId of parent affiliation or self if no parent.
Winson Chungec396d62014-08-06 17:08:00 -0700229 int mAffiliatedTaskColor; // color of the parent task affiliation.
Craig Mautnera228ae92014-07-09 05:44:55 -0700230 TaskRecord mPrevAffiliate; // previous task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800231 int mPrevAffiliateTaskId = INVALID_TASK_ID; // previous id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700232 TaskRecord mNextAffiliate; // next task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800233 int mNextAffiliateTaskId = INVALID_TASK_ID; // next id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700234
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700235 // For relaunching the task from recents as though it was launched by the original launcher.
236 int mCallingUid;
237 String mCallingPackage;
238
Craig Mautner21d24a22014-04-23 11:45:37 -0700239 final ActivityManagerService mService;
240
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700241 // Whether or not this task covers the entire screen; by default tasks are fullscreen.
242 boolean mFullscreen = true;
243
244 // Bounds of the Task. null for fullscreen tasks.
245 Rect mBounds = null;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -0800246 private final Rect mTmpStableBounds = new Rect();
247 private final Rect mTmpNonDecorBounds = new Rect();
Jorim Jaggi0a932142016-02-01 17:42:25 -0800248 private final Rect mTmpRect2 = new Rect();
249
Wale Ogunwale706ed792015-08-02 10:29:44 -0700250 // Last non-fullscreen bounds the task was launched in or resized to.
251 // The information is persisted and used to determine the appropriate stack to launch the
252 // task into on restore.
253 Rect mLastNonFullscreenBounds = null;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700254 // Minimal size for width/height of this task when it's resizeable. -1 means it should use the
255 // default minimal size.
256 final int mMinimalSize;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700257
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700258 // Ranking (from top) of this task among all visible tasks. (-1 means it's not visible)
259 // This number will be assigned when we evaluate OOM scores for all visible tasks.
260 int mLayerRank = -1;
261
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700262 Configuration mOverrideConfig = Configuration.EMPTY;
263
Craig Mautner21d24a22014-04-23 11:45:37 -0700264 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700265 IVoiceInteractionSession _voiceSession, IVoiceInteractor _voiceInteractor) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700266 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700267 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
268 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800269 userId = UserHandle.getUserId(info.applicationInfo.uid);
270 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800271 mLastThumbnailInfo = new TaskThumbnailInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 taskId = _taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700273 mAffiliatedTaskId = _taskId;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700274 voiceSession = _voiceSession;
275 voiceInteractor = _voiceInteractor;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700276 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800277 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700278 mCallingUid = info.applicationInfo.uid;
279 mCallingPackage = info.packageName;
Martijn Coenend4a69702014-06-30 11:12:17 -0700280 setIntent(_intent, info);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700281 mMinimalSize = info != null && info.layout != null ? info.layout.minimalSize : -1;
Craig Mautner21d24a22014-04-23 11:45:37 -0700282 }
283
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700284 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Winsonc809cbb2015-11-02 12:06:15 -0800285 TaskDescription _taskDescription, TaskThumbnailInfo thumbnailInfo) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700286 mService = service;
287 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
288 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800289 userId = UserHandle.getUserId(info.applicationInfo.uid);
290 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800291 mLastThumbnailInfo = thumbnailInfo;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700292 taskId = _taskId;
293 mAffiliatedTaskId = _taskId;
294 voiceSession = null;
295 voiceInteractor = null;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700296 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800297 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700298 mCallingUid = info.applicationInfo.uid;
299 mCallingPackage = info.packageName;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700300 setIntent(_intent, info);
301
302 taskType = ActivityRecord.APPLICATION_ACTIVITY_TYPE;
303 isPersistable = true;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700304 // Clamp to [1, max].
305 maxRecents = Math.min(Math.max(info.maxRecents, 1),
306 ActivityManager.getMaxAppRecentsLimitStatic());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700307
308 taskType = APPLICATION_ACTIVITY_TYPE;
309 mTaskToReturnTo = HOME_ACTIVITY_TYPE;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700310 lastTaskDescription = _taskDescription;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700311 mMinimalSize = info != null && info.layout != null ? info.layout.minimalSize : -1;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700312 }
313
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800314 private TaskRecord(ActivityManagerService service, int _taskId, Intent _intent,
315 Intent _affinityIntent, String _affinity, String _rootAffinity,
316 ComponentName _realActivity, ComponentName _origActivity, boolean _rootWasReset,
317 boolean _autoRemoveRecents, boolean _askedCompatMode, int _taskType, int _userId,
318 int _effectiveUid, String _lastDescription, ArrayList<ActivityRecord> activities,
319 long _firstActiveTime, long _lastActiveTime, long lastTimeMoved,
320 boolean neverRelinquishIdentity, TaskDescription _lastTaskDescription,
Winsonc809cbb2015-11-02 12:06:15 -0800321 TaskThumbnailInfo lastThumbnailInfo, int taskAffiliation, int prevTaskId,
322 int nextTaskId, int taskAffiliationColor, int callingUid, String callingPackage,
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800323 int resizeMode, boolean privileged, boolean _realActivitySuspended,
324 boolean userSetupComplete) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700325 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700326 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
327 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800328 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(_userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800329 mLastThumbnailInfo = lastThumbnailInfo;
Craig Mautner21d24a22014-04-23 11:45:37 -0700330 taskId = _taskId;
331 intent = _intent;
332 affinityIntent = _affinityIntent;
333 affinity = _affinity;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800334 rootAffinity = _rootAffinity;
Craig Mautner21d24a22014-04-23 11:45:37 -0700335 voiceSession = null;
336 voiceInteractor = null;
337 realActivity = _realActivity;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800338 realActivitySuspended = _realActivitySuspended;
Craig Mautner21d24a22014-04-23 11:45:37 -0700339 origActivity = _origActivity;
340 rootWasReset = _rootWasReset;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700341 isAvailable = true;
Dianne Hackborn13420f22014-07-18 15:43:56 -0700342 autoRemoveRecents = _autoRemoveRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700343 askedCompatMode = _askedCompatMode;
344 taskType = _taskType;
Craig Mautner84984fa2014-06-19 11:19:20 -0700345 mTaskToReturnTo = HOME_ACTIVITY_TYPE;
Craig Mautner21d24a22014-04-23 11:45:37 -0700346 userId = _userId;
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800347 mUserSetupComplete = userSetupComplete;
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700348 effectiveUid = _effectiveUid;
Winson Chungffa2ec62014-07-03 15:54:42 -0700349 firstActiveTime = _firstActiveTime;
Winson Chungf1fbd772014-06-24 18:06:58 -0700350 lastActiveTime = _lastActiveTime;
Craig Mautner21d24a22014-04-23 11:45:37 -0700351 lastDescription = _lastDescription;
352 mActivities = activities;
353 mLastTimeMoved = lastTimeMoved;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700354 mNeverRelinquishIdentity = neverRelinquishIdentity;
Winson Chung2cb86c72014-06-25 12:03:30 -0700355 lastTaskDescription = _lastTaskDescription;
Craig Mautnera228ae92014-07-09 05:44:55 -0700356 mAffiliatedTaskId = taskAffiliation;
Winson Chungec396d62014-08-06 17:08:00 -0700357 mAffiliatedTaskColor = taskAffiliationColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700358 mPrevAffiliateTaskId = prevTaskId;
359 mNextAffiliateTaskId = nextTaskId;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700360 mCallingUid = callingUid;
361 mCallingPackage = callingPackage;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800362 mResizeMode = resizeMode;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700363 mPrivileged = privileged;
Suprabh Shukla74f9f162015-11-06 15:39:42 -0800364 ActivityInfo info = (mActivities.size() > 0) ? mActivities.get(0).info : null;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700365 mMinimalSize = info != null && info.layout != null ? info.layout.minimalSize : -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 }
367
368 void touchActiveTime() {
Craig Mautner494f6bdd2014-07-21 11:17:46 -0700369 lastActiveTime = System.currentTimeMillis();
Winson Chungffa2ec62014-07-03 15:54:42 -0700370 if (firstActiveTime == 0) {
371 firstActiveTime = lastActiveTime;
372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 long getInactiveDuration() {
Craig Mautner494f6bdd2014-07-21 11:17:46 -0700376 return System.currentTimeMillis() - lastActiveTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700378
Winson Chungfee26772014-08-05 12:21:52 -0700379 /** Sets the original intent, and the calling uid and package. */
380 void setIntent(ActivityRecord r) {
Winson Chungfee26772014-08-05 12:21:52 -0700381 mCallingUid = r.launchedFromUid;
382 mCallingPackage = r.launchedFromPackage;
Craig Mautner15df08a2015-04-01 12:17:18 -0700383 setIntent(r.intent, r.info);
Winson Chungfee26772014-08-05 12:21:52 -0700384 }
385
386 /** Sets the original intent, _without_ updating the calling uid or package. */
387 private void setIntent(Intent _intent, ActivityInfo info) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700388 if (intent == null) {
389 mNeverRelinquishIdentity =
390 (info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) == 0;
391 } else if (mNeverRelinquishIdentity) {
392 return;
393 }
394
395 affinity = info.taskAffinity;
Dianne Hackborn79228822014-09-16 11:11:23 -0700396 if (intent == null) {
397 // If this task already has an intent associated with it, don't set the root
398 // affinity -- we don't want it changing after initially set, but the initially
399 // set value may be null.
400 rootAffinity = affinity;
401 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700402 effectiveUid = info.applicationInfo.uid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700403 stringName = null;
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 if (info.targetActivity == null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800406 if (_intent != null) {
407 // If this Intent has a selector, we want to clear it for the
408 // recent task since it is not relevant if the user later wants
409 // to re-launch the app.
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700410 if (_intent.getSelector() != null || _intent.getSourceBounds() != null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800411 _intent = new Intent(_intent);
412 _intent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700413 _intent.setSourceBounds(null);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800414 }
415 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700416 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Setting Intent of " + this + " to " + _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 intent = _intent;
418 realActivity = _intent != null ? _intent.getComponent() : null;
419 origActivity = null;
420 } else {
421 ComponentName targetComponent = new ComponentName(
422 info.packageName, info.targetActivity);
423 if (_intent != null) {
424 Intent targetIntent = new Intent(_intent);
425 targetIntent.setComponent(targetComponent);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800426 targetIntent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700427 targetIntent.setSourceBounds(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700428 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700429 "Setting Intent of " + this + " to target " + targetIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 intent = targetIntent;
431 realActivity = targetComponent;
432 origActivity = _intent.getComponent();
433 } else {
434 intent = null;
435 realActivity = targetComponent;
436 origActivity = new ComponentName(info.packageName, info.name);
437 }
438 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700439
Craig Mautner47b20ba2014-09-17 17:23:44 -0700440 final int intentFlags = intent == null ? 0 : intent.getFlags();
441 if ((intentFlags & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 // Once we are set to an Intent with this flag, we count this
443 // task as having a true root activity.
444 rootWasReset = true;
445 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700446
Dianne Hackborn09233282014-04-30 11:33:59 -0700447 userId = UserHandle.getUserId(info.applicationInfo.uid);
Wale Ogunwalef80170f2016-02-04 15:12:29 -0800448 mUserSetupComplete = mService.mUserController.isUserSetupCompleteLocked(userId);
Craig Mautner41db4a72014-05-07 17:20:56 -0700449 if ((info.flags & ActivityInfo.FLAG_AUTO_REMOVE_FROM_RECENTS) != 0) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700450 // If the activity itself has requested auto-remove, then just always do it.
451 autoRemoveRecents = true;
Wale Ogunwale843bfb92015-03-27 11:06:48 -0700452 } else if ((intentFlags & (FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_RETAIN_IN_RECENTS))
453 == FLAG_ACTIVITY_NEW_DOCUMENT) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700454 // If the caller has not asked for the document to be retained, then we may
455 // want to turn on auto-remove, depending on whether the target has set its
456 // own document launch mode.
457 if (info.documentLaunchMode != ActivityInfo.DOCUMENT_LAUNCH_NONE) {
458 autoRemoveRecents = false;
459 } else {
460 autoRemoveRecents = true;
461 }
462 } else {
463 autoRemoveRecents = false;
Craig Mautner41db4a72014-05-07 17:20:56 -0700464 }
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800465 mResizeMode = info.resizeMode;
Craig Mautner15df08a2015-04-01 12:17:18 -0700466 mLockTaskMode = info.lockTaskLaunchMode;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700467 mPrivileged = (info.applicationInfo.privateFlags & PRIVATE_FLAG_PRIVILEGED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -0700468 setLockTaskAuth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800470
Craig Mautner84984fa2014-06-19 11:19:20 -0700471 void setTaskToReturnTo(int taskToReturnTo) {
Wale Ogunwale673cbd22016-01-30 18:30:55 -0800472 mTaskToReturnTo = (taskToReturnTo == RECENTS_ACTIVITY_TYPE)
473 ? HOME_ACTIVITY_TYPE : taskToReturnTo;
Craig Mautner84984fa2014-06-19 11:19:20 -0700474 }
475
476 int getTaskToReturnTo() {
477 return mTaskToReturnTo;
478 }
479
Craig Mautnera228ae92014-07-09 05:44:55 -0700480 void setPrevAffiliate(TaskRecord prevAffiliate) {
481 mPrevAffiliate = prevAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800482 mPrevAffiliateTaskId = prevAffiliate == null ? INVALID_TASK_ID : prevAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700483 }
484
485 void setNextAffiliate(TaskRecord nextAffiliate) {
486 mNextAffiliate = nextAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800487 mNextAffiliateTaskId = nextAffiliate == null ? INVALID_TASK_ID : nextAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700488 }
489
490 // Close up recents linked list.
491 void closeRecentsChain() {
492 if (mPrevAffiliate != null) {
493 mPrevAffiliate.setNextAffiliate(mNextAffiliate);
494 }
495 if (mNextAffiliate != null) {
496 mNextAffiliate.setPrevAffiliate(mPrevAffiliate);
497 }
498 setPrevAffiliate(null);
499 setNextAffiliate(null);
500 }
501
Winson Chung740c3ac2014-11-12 16:14:38 -0800502 void removedFromRecents() {
Dianne Hackborn852975d2014-08-22 17:42:43 -0700503 disposeThumbnail();
504 closeRecentsChain();
505 if (inRecents) {
506 inRecents = false;
Winson Chung740c3ac2014-11-12 16:14:38 -0800507 mService.notifyTaskPersisterLocked(this, false);
Dianne Hackborn852975d2014-08-22 17:42:43 -0700508 }
509 }
510
Craig Mautnera228ae92014-07-09 05:44:55 -0700511 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
512 closeRecentsChain();
513 mAffiliatedTaskId = taskToAffiliateWith.mAffiliatedTaskId;
Winson Chungec396d62014-08-06 17:08:00 -0700514 mAffiliatedTaskColor = taskToAffiliateWith.mAffiliatedTaskColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700515 // Find the end
516 while (taskToAffiliateWith.mNextAffiliate != null) {
517 final TaskRecord nextRecents = taskToAffiliateWith.mNextAffiliate;
518 if (nextRecents.mAffiliatedTaskId != mAffiliatedTaskId) {
519 Slog.e(TAG, "setTaskToAffiliateWith: nextRecents=" + nextRecents + " affilTaskId="
520 + nextRecents.mAffiliatedTaskId + " should be " + mAffiliatedTaskId);
521 if (nextRecents.mPrevAffiliate == taskToAffiliateWith) {
522 nextRecents.setPrevAffiliate(null);
523 }
524 taskToAffiliateWith.setNextAffiliate(null);
525 break;
526 }
527 taskToAffiliateWith = nextRecents;
528 }
529 taskToAffiliateWith.setNextAffiliate(this);
530 setPrevAffiliate(taskToAffiliateWith);
531 setNextAffiliate(null);
532 }
533
Winson Chung096f36b2014-08-20 15:39:01 -0700534 /**
Winsonc809cbb2015-11-02 12:06:15 -0800535 * Sets the last thumbnail with the current task bounds and the system orientation.
Winson Chung096f36b2014-08-20 15:39:01 -0700536 * @return whether the thumbnail was set
537 */
Winsonc809cbb2015-11-02 12:06:15 -0800538 boolean setLastThumbnailLocked(Bitmap thumbnail) {
539 final Configuration serviceConfig = mService.mConfiguration;
540 int taskWidth = 0;
541 int taskHeight = 0;
542 if (mBounds != null) {
543 // Non-fullscreen tasks
544 taskWidth = mBounds.width();
545 taskHeight = mBounds.height();
546 } else if (stack != null) {
547 // Fullscreen tasks
548 final Point displaySize = new Point();
549 stack.getDisplaySize(displaySize);
550 taskWidth = displaySize.x;
551 taskHeight = displaySize.y;
552 } else {
553 Slog.e(TAG, "setLastThumbnailLocked() called on Task without stack");
554 }
555 return setLastThumbnailLocked(thumbnail, taskWidth, taskHeight, serviceConfig.orientation);
556 }
557
558 /**
559 * Sets the last thumbnail with the current task bounds.
560 * @return whether the thumbnail was set
561 */
562 private boolean setLastThumbnailLocked(Bitmap thumbnail, int taskWidth, int taskHeight,
563 int screenOrientation) {
Winson Chung096f36b2014-08-20 15:39:01 -0700564 if (mLastThumbnail != thumbnail) {
565 mLastThumbnail = thumbnail;
Winsonc809cbb2015-11-02 12:06:15 -0800566 mLastThumbnailInfo.taskWidth = taskWidth;
567 mLastThumbnailInfo.taskHeight = taskHeight;
568 mLastThumbnailInfo.screenOrientation = screenOrientation;
Winson Chung096f36b2014-08-20 15:39:01 -0700569 if (thumbnail == null) {
570 if (mLastThumbnailFile != null) {
571 mLastThumbnailFile.delete();
572 }
573 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800574 mService.mRecentTasks.saveImage(thumbnail, mLastThumbnailFile.getAbsolutePath());
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700575 }
Winson Chung096f36b2014-08-20 15:39:01 -0700576 return true;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700577 }
Winson Chung096f36b2014-08-20 15:39:01 -0700578 return false;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700579 }
580
581 void getLastThumbnail(TaskThumbnail thumbs) {
582 thumbs.mainThumbnail = mLastThumbnail;
Winsonc809cbb2015-11-02 12:06:15 -0800583 thumbs.thumbnailInfo = mLastThumbnailInfo;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700584 thumbs.thumbnailFileDescriptor = null;
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700585 if (mLastThumbnail == null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800586 thumbs.mainThumbnail = mService.mRecentTasks.getImageFromWriteQueue(
Suprabh Shukla23593142015-11-03 17:31:15 -0800587 mLastThumbnailFile.getAbsolutePath());
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700588 }
Winson Chung096f36b2014-08-20 15:39:01 -0700589 // Only load the thumbnail file if we don't have a thumbnail
590 if (thumbs.mainThumbnail == null && mLastThumbnailFile.exists()) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700591 try {
592 thumbs.thumbnailFileDescriptor = ParcelFileDescriptor.open(mLastThumbnailFile,
593 ParcelFileDescriptor.MODE_READ_ONLY);
594 } catch (IOException e) {
Dianne Hackborn9844d292013-10-04 16:44:22 -0700595 }
596 }
597 }
598
Winsonc809cbb2015-11-02 12:06:15 -0800599 /**
600 * Removes in-memory thumbnail data when the max number of in-memory task thumbnails is reached.
601 */
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700602 void freeLastThumbnail() {
603 mLastThumbnail = null;
604 }
605
Winsonc809cbb2015-11-02 12:06:15 -0800606 /**
607 * Removes all associated thumbnail data when a task is removed or pruned from recents.
608 */
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700609 void disposeThumbnail() {
Winsonc6a2da02015-11-11 18:11:59 -0800610 mLastThumbnailInfo.reset();
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700611 mLastThumbnail = null;
612 lastDescription = null;
613 }
614
Winson Chung1147c402014-05-14 11:05:00 -0700615 /** Returns the intent for the root activity for this task */
616 Intent getBaseIntent() {
617 return intent != null ? intent : affinityIntent;
618 }
619
Winson Chung3b3f4642014-04-22 10:08:18 -0700620 /** Returns the first non-finishing activity from the root. */
621 ActivityRecord getRootActivity() {
622 for (int i = 0; i < mActivities.size(); i++) {
623 final ActivityRecord r = mActivities.get(i);
624 if (r.finishing) {
625 continue;
626 }
627 return r;
628 }
629 return null;
630 }
631
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800632 ActivityRecord getTopActivity() {
633 for (int i = mActivities.size() - 1; i >= 0; --i) {
634 final ActivityRecord r = mActivities.get(i);
635 if (r.finishing) {
636 continue;
637 }
638 return r;
639 }
640 return null;
641 }
642
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700643 ActivityRecord topRunningActivityLocked() {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700644 if (stack != null) {
645 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
646 ActivityRecord r = mActivities.get(activityNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700647 if (!r.finishing && stack.okToShowLocked(r)) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700648 return r;
649 }
Craig Mautner6b74cb52013-09-27 17:02:21 -0700650 }
651 }
652 return null;
653 }
654
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800655 void setFrontOfTask() {
656 setFrontOfTask(null);
657 }
658
Craig Mautner3b475fe2013-12-16 15:58:31 -0800659 /** Call after activity movement or finish to make sure that frontOfTask is set correctly */
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800660 void setFrontOfTask(ActivityRecord newTop) {
661 // If a top candidate is suggested by the caller, go ahead and use it and mark all others
662 // as not front. This is needed in situations where the current front activity in the
663 // task isn't finished yet and we want to set the front to the activity moved to the front
664 // of the task.
665 boolean foundFront = newTop != null ? true : false;
666
Craig Mautner3b475fe2013-12-16 15:58:31 -0800667 final int numActivities = mActivities.size();
Craig Mautner704e40b2013-12-18 16:43:51 -0800668 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
Craig Mautner3b475fe2013-12-16 15:58:31 -0800669 final ActivityRecord r = mActivities.get(activityNdx);
670 if (foundFront || r.finishing) {
671 r.frontOfTask = false;
672 } else {
673 r.frontOfTask = true;
674 // Set frontOfTask false for every following activity.
675 foundFront = true;
676 }
677 }
Craig Mautner9587ee02014-06-23 15:00:10 +0000678 if (!foundFront && numActivities > 0) {
679 // All activities of this task are finishing. As we ought to have a frontOfTask
680 // activity, make the bottom activity front.
681 mActivities.get(0).frontOfTask = true;
682 }
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800683 if (newTop != null) {
684 newTop.frontOfTask = true;
685 }
Craig Mautner3b475fe2013-12-16 15:58:31 -0800686 }
687
Craig Mautnerde4ef022013-04-07 19:01:33 -0700688 /**
Craig Mautner3b475fe2013-12-16 15:58:31 -0800689 * Reorder the history stack so that the passed activity is brought to the front.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700690 */
691 final void moveActivityToFrontLocked(ActivityRecord newTop) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700692 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
693 "Removing and adding activity " + newTop
694 + " to stack at top callers=" + Debug.getCallers(4));
Craig Mautnerde4ef022013-04-07 19:01:33 -0700695
Craig Mautnerde4ef022013-04-07 19:01:33 -0700696 mActivities.remove(newTop);
697 mActivities.add(newTop);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700698 updateEffectiveIntent();
Craig Mautner3b475fe2013-12-16 15:58:31 -0800699
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800700 setFrontOfTask(newTop);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700701 }
702
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800703 void addActivityAtBottom(ActivityRecord r) {
Craig Mautner77878772013-03-04 19:46:24 -0800704 addActivityAtIndex(0, r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800705 }
706
707 void addActivityToTop(ActivityRecord r) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700708 addActivityAtIndex(mActivities.size(), r);
709 }
710
711 void addActivityAtIndex(int index, ActivityRecord r) {
Craig Mautner6170f732013-04-02 13:05:23 -0700712 // 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 -0800713 if (!mActivities.remove(r) && r.fullscreen) {
714 // Was not previously in list.
715 numFullscreen++;
716 }
Craig Mautner2c1faed2013-07-23 12:56:02 -0700717 // Only set this based on the first activity
718 if (mActivities.isEmpty()) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700719 taskType = r.mActivityType;
720 isPersistable = r.isPersistable();
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700721 mCallingUid = r.launchedFromUid;
722 mCallingPackage = r.launchedFromPackage;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700723 // Clamp to [1, max].
724 maxRecents = Math.min(Math.max(r.info.maxRecents, 1),
725 ActivityManager.getMaxAppRecentsLimitStatic());
Craig Mautner2c1faed2013-07-23 12:56:02 -0700726 } else {
727 // Otherwise make all added activities match this one.
Craig Mautner21d24a22014-04-23 11:45:37 -0700728 r.mActivityType = taskType;
Craig Mautner78733002013-06-10 13:54:49 -0700729 }
Craig Mautner77878772013-03-04 19:46:24 -0800730 mActivities.add(index, r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700731 updateEffectiveIntent();
Craig Mautner21d24a22014-04-23 11:45:37 -0700732 if (r.isPersistable()) {
733 mService.notifyTaskPersisterLocked(this, false);
734 }
Craig Mautner77878772013-03-04 19:46:24 -0800735 }
736
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800737 /** @return true if this was the last activity in the task */
738 boolean removeActivity(ActivityRecord r) {
739 if (mActivities.remove(r) && r.fullscreen) {
740 // Was previously in list.
741 numFullscreen--;
742 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700743 if (r.isPersistable()) {
744 mService.notifyTaskPersisterLocked(this, false);
745 }
Craig Mautner41326202014-06-20 14:38:21 -0700746 if (mActivities.isEmpty()) {
Craig Mautner5afcd4d2014-06-21 18:11:33 -0700747 return !mReuseTask;
Craig Mautner41326202014-06-20 14:38:21 -0700748 }
749 updateEffectiveIntent();
750 return false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800751 }
752
Craig Mautner41db4a72014-05-07 17:20:56 -0700753 boolean autoRemoveFromRecents() {
Dianne Hackbornd38aed82014-06-10 21:36:35 -0700754 // We will automatically remove the task either if it has explicitly asked for
755 // this, or it is empty and has never contained an activity that got shown to
756 // the user.
Dianne Hackborn13420f22014-07-18 15:43:56 -0700757 return autoRemoveRecents || (mActivities.isEmpty() && !hasBeenVisible);
Craig Mautner41db4a72014-05-07 17:20:56 -0700758 }
759
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700760 /**
761 * Completely remove all activities associated with an existing
762 * task starting at a specified index.
763 */
764 final void performClearTaskAtIndexLocked(int activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700765 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700766 for ( ; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700767 final ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700768 if (r.finishing) {
769 continue;
770 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700771 if (stack == null) {
772 // Task was restored from persistent storage.
773 r.takeFromHistory();
774 mActivities.remove(activityNdx);
775 --activityNdx;
776 --numActivities;
Todd Kennedy539db512014-12-15 09:57:55 -0800777 } else if (stack.finishActivityLocked(
778 r, Activity.RESULT_CANCELED, null, "clear-task-index", false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700779 --activityNdx;
780 --numActivities;
781 }
782 }
783 }
784
785 /**
786 * Completely remove all activities associated with an existing task.
787 */
788 final void performClearTaskLocked() {
Craig Mautner362449a2014-06-20 14:04:39 -0700789 mReuseTask = true;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700790 performClearTaskAtIndexLocked(0);
Craig Mautner362449a2014-06-20 14:04:39 -0700791 mReuseTask = false;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700792 }
793
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -0800794 ActivityRecord performClearTaskForReuseLocked(ActivityRecord newR, int launchFlags) {
795 mReuseTask = true;
796 final ActivityRecord result = performClearTaskLocked(newR, launchFlags);
797 mReuseTask = false;
798 return result;
799 }
800
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700801 /**
802 * Perform clear operation as requested by
803 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
804 * stack to the given task, then look for
805 * an instance of that activity in the stack and, if found, finish all
806 * activities on top of it and return the instance.
807 *
808 * @param newR Description of the new activity being started.
809 * @return Returns the old activity that should be continued to be used,
810 * or null if none was found.
811 */
812 final ActivityRecord performClearTaskLocked(ActivityRecord newR, int launchFlags) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700813 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700814 for (int activityNdx = numActivities - 1; activityNdx >= 0; --activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700815 ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700816 if (r.finishing) {
817 continue;
818 }
819 if (r.realActivity.equals(newR.realActivity)) {
820 // Here it is! Now finish everything in front...
Craig Mautner1602ec22013-05-12 10:24:27 -0700821 final ActivityRecord ret = r;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700822
823 for (++activityNdx; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700824 r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700825 if (r.finishing) {
826 continue;
827 }
828 ActivityOptions opts = r.takeOptionsLocked();
829 if (opts != null) {
830 ret.updateOptionsLocked(opts);
831 }
Wale Ogunwale7d701172015-03-11 15:36:30 -0700832 if (stack != null && stack.finishActivityLocked(
Todd Kennedy539db512014-12-15 09:57:55 -0800833 r, Activity.RESULT_CANCELED, null, "clear-task-stack", false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700834 --activityNdx;
835 --numActivities;
836 }
837 }
838
839 // Finally, if this is a normal launch mode (that is, not
840 // expecting onNewIntent()), then we will finish the current
841 // instance of the activity so a new fresh one can be started.
842 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
843 && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0) {
844 if (!ret.finishing) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700845 if (stack != null) {
846 stack.finishActivityLocked(
847 ret, Activity.RESULT_CANCELED, null, "clear-task-top", false);
848 }
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700849 return null;
850 }
851 }
852
853 return ret;
854 }
855 }
856
857 return null;
858 }
859
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700860 public TaskThumbnail getTaskThumbnailLocked() {
Craig Mautner21d24a22014-04-23 11:45:37 -0700861 if (stack != null) {
862 final ActivityRecord resumedActivity = stack.mResumedActivity;
863 if (resumedActivity != null && resumedActivity.task == this) {
Winson8b1871d2015-11-20 09:56:20 -0800864 final Bitmap thumbnail = stack.screenshotActivitiesLocked(resumedActivity);
Winsonc809cbb2015-11-02 12:06:15 -0800865 setLastThumbnailLocked(thumbnail);
Craig Mautner21d24a22014-04-23 11:45:37 -0700866 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700867 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700868 final TaskThumbnail taskThumbnail = new TaskThumbnail();
869 getLastThumbnail(taskThumbnail);
870 return taskThumbnail;
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700871 }
872
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700873 public void removeTaskActivitiesLocked() {
874 // Just remove the entire task.
875 performClearTaskAtIndexLocked(0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700876 }
877
Craig Mautner432f64e2015-05-20 14:59:57 -0700878 String lockTaskAuthToString() {
879 switch (mLockTaskAuth) {
880 case LOCK_TASK_AUTH_DONT_LOCK: return "LOCK_TASK_AUTH_DONT_LOCK";
881 case LOCK_TASK_AUTH_PINNABLE: return "LOCK_TASK_AUTH_PINNABLE";
882 case LOCK_TASK_AUTH_LAUNCHABLE: return "LOCK_TASK_AUTH_LAUNCHABLE";
883 case LOCK_TASK_AUTH_WHITELISTED: return "LOCK_TASK_AUTH_WHITELISTED";
Benjamin Franz469dd582015-06-09 14:24:36 +0100884 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV: return "LOCK_TASK_AUTH_LAUNCHABLE_PRIV";
Craig Mautner432f64e2015-05-20 14:59:57 -0700885 default: return "unknown=" + mLockTaskAuth;
886 }
887 }
888
Craig Mautner15df08a2015-04-01 12:17:18 -0700889 void setLockTaskAuth() {
Benjamin Franz469dd582015-06-09 14:24:36 +0100890 if (!mPrivileged &&
891 (mLockTaskMode == LOCK_TASK_LAUNCH_MODE_ALWAYS ||
892 mLockTaskMode == LOCK_TASK_LAUNCH_MODE_NEVER)) {
893 // Non-priv apps are not allowed to use always or never, fall back to default
894 mLockTaskMode = LOCK_TASK_LAUNCH_MODE_DEFAULT;
895 }
Craig Mautner15df08a2015-04-01 12:17:18 -0700896 switch (mLockTaskMode) {
897 case LOCK_TASK_LAUNCH_MODE_DEFAULT:
898 mLockTaskAuth = isLockTaskWhitelistedLocked() ?
899 LOCK_TASK_AUTH_WHITELISTED : LOCK_TASK_AUTH_PINNABLE;
900 break;
901
902 case LOCK_TASK_LAUNCH_MODE_NEVER:
Benjamin Franz469dd582015-06-09 14:24:36 +0100903 mLockTaskAuth = LOCK_TASK_AUTH_DONT_LOCK;
Craig Mautner15df08a2015-04-01 12:17:18 -0700904 break;
905
906 case LOCK_TASK_LAUNCH_MODE_ALWAYS:
Benjamin Franz469dd582015-06-09 14:24:36 +0100907 mLockTaskAuth = LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -0700908 break;
909
910 case LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED:
911 mLockTaskAuth = isLockTaskWhitelistedLocked() ?
912 LOCK_TASK_AUTH_LAUNCHABLE : LOCK_TASK_AUTH_PINNABLE;
913 break;
914 }
Craig Mautner432f64e2015-05-20 14:59:57 -0700915 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "setLockTaskAuth: task=" + this +
916 " mLockTaskAuth=" + lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -0700917 }
918
919 boolean isLockTaskWhitelistedLocked() {
Benjamin Franz6fd84cc2015-08-06 18:09:03 +0100920 String pkg = (realActivity != null) ? realActivity.getPackageName() : null;
921 if (pkg == null) {
Craig Mautner15df08a2015-04-01 12:17:18 -0700922 return false;
923 }
924 String[] packages = mService.mLockTaskPackages.get(userId);
925 if (packages == null) {
926 return false;
927 }
928 for (int i = packages.length - 1; i >= 0; --i) {
Benjamin Franz6fd84cc2015-08-06 18:09:03 +0100929 if (pkg.equals(packages[i])) {
Craig Mautner15df08a2015-04-01 12:17:18 -0700930 return true;
931 }
932 }
933 return false;
934 }
Wale Ogunwale74e26592016-02-05 11:48:37 -0800935
Craig Mautnera82aa092013-09-13 15:34:08 -0700936 boolean isHomeTask() {
Craig Mautner84984fa2014-06-19 11:19:20 -0700937 return taskType == HOME_ACTIVITY_TYPE;
Craig Mautnera82aa092013-09-13 15:34:08 -0700938 }
939
Wale Ogunwale74e26592016-02-05 11:48:37 -0800940 boolean isRecentsTask() {
941 return taskType == RECENTS_ACTIVITY_TYPE;
942 }
943
Craig Mautner86d67a42013-05-14 10:34:38 -0700944 boolean isApplicationTask() {
Craig Mautner84984fa2014-06-19 11:19:20 -0700945 return taskType == APPLICATION_ACTIVITY_TYPE;
946 }
947
948 boolean isOverHomeStack() {
949 return mTaskToReturnTo == HOME_ACTIVITY_TYPE || mTaskToReturnTo == RECENTS_ACTIVITY_TYPE;
Craig Mautner1602ec22013-05-12 10:24:27 -0700950 }
951
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800952 boolean isResizeable() {
953 return !isHomeTask() && (mService.mForceResizableActivities
Jorim Jaggi8202b2a2016-02-03 19:24:31 -0800954 || ActivityInfo.isResizeableMode(mResizeMode)) && !mTemporarilyUnresizable;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -0800955 }
956
957 boolean inCropWindowsResizeMode() {
958 return !isResizeable() && mResizeMode == RESIZE_MODE_CROP_WINDOWS;
959 }
960
Wale Ogunwale513346d2016-01-27 10:55:01 -0800961 boolean canGoInDockedStack() {
962 return isResizeable() || inCropWindowsResizeMode();
963 }
964
Craig Mautner525f3d92013-05-07 14:01:50 -0700965 /**
966 * Find the activity in the history stack within the given task. Returns
967 * the index within the history at which it's found, or < 0 if not found.
968 */
969 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r) {
970 final ComponentName realActivity = r.realActivity;
971 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
972 ActivityRecord candidate = mActivities.get(activityNdx);
973 if (candidate.finishing) {
974 continue;
975 }
976 if (candidate.realActivity.equals(realActivity)) {
977 return candidate;
978 }
979 }
980 return null;
981 }
982
Winson Chunga449dc02014-05-16 11:15:04 -0700983 /** Updates the last task description values. */
984 void updateTaskDescription() {
985 // Traverse upwards looking for any break between main task activities and
986 // utility activities.
987 int activityNdx;
988 final int numActivities = mActivities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700989 final boolean relinquish = numActivities == 0 ? false :
990 (mActivities.get(0).info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) != 0;
Winson Chunga449dc02014-05-16 11:15:04 -0700991 for (activityNdx = Math.min(numActivities, 1); activityNdx < numActivities;
Craig Mautner21d24a22014-04-23 11:45:37 -0700992 ++activityNdx) {
Winson Chunga449dc02014-05-16 11:15:04 -0700993 final ActivityRecord r = mActivities.get(activityNdx);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700994 if (relinquish && (r.info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
995 // This will be the top activity for determining taskDescription. Pre-inc to
996 // overcome initial decrement below.
997 ++activityNdx;
998 break;
999 }
Winson Chunga449dc02014-05-16 11:15:04 -07001000 if (r.intent != null &&
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001001 (r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Winson Chunga449dc02014-05-16 11:15:04 -07001002 break;
1003 }
1004 }
1005 if (activityNdx > 0) {
1006 // Traverse downwards starting below break looking for set label, icon.
1007 // Note that if there are activities in the task but none of them set the
1008 // recent activity values, then we do not fall back to the last set
1009 // values in the TaskRecord.
1010 String label = null;
Craig Mautner648f69b2014-09-18 14:16:26 -07001011 String iconFilename = null;
Winson Chunga449dc02014-05-16 11:15:04 -07001012 int colorPrimary = 0;
Winson Chung1af8eda2016-02-05 17:55:56 +00001013 int colorBackground = 0;
Winson Chunga449dc02014-05-16 11:15:04 -07001014 for (--activityNdx; activityNdx >= 0; --activityNdx) {
1015 final ActivityRecord r = mActivities.get(activityNdx);
1016 if (r.taskDescription != null) {
1017 if (label == null) {
1018 label = r.taskDescription.getLabel();
1019 }
Craig Mautner648f69b2014-09-18 14:16:26 -07001020 if (iconFilename == null) {
1021 iconFilename = r.taskDescription.getIconFilename();
Winson Chunga449dc02014-05-16 11:15:04 -07001022 }
1023 if (colorPrimary == 0) {
1024 colorPrimary = r.taskDescription.getPrimaryColor();
Winson Chunga449dc02014-05-16 11:15:04 -07001025 }
Winson Chung1af8eda2016-02-05 17:55:56 +00001026 if (colorBackground == 0) {
1027 colorBackground = r.taskDescription.getBackgroundColor();
1028 }
Winson Chunga449dc02014-05-16 11:15:04 -07001029 }
1030 }
Winson Chung1af8eda2016-02-05 17:55:56 +00001031 lastTaskDescription = new TaskDescription(label, null, iconFilename, colorPrimary,
1032 colorBackground);
Winson Chungec396d62014-08-06 17:08:00 -07001033 // Update the task affiliation color if we are the parent of the group
1034 if (taskId == mAffiliatedTaskId) {
1035 mAffiliatedTaskColor = lastTaskDescription.getPrimaryColor();
1036 }
Winson Chunga449dc02014-05-16 11:15:04 -07001037 }
1038 }
1039
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001040 int findEffectiveRootIndex() {
Craig Mautner4767f4b2014-09-18 15:38:33 -07001041 int effectiveNdx = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001042 final int topActivityNdx = mActivities.size() - 1;
Dianne Hackborn39569af2014-09-23 10:56:58 -07001043 for (int activityNdx = 0; activityNdx <= topActivityNdx; ++activityNdx) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001044 final ActivityRecord r = mActivities.get(activityNdx);
1045 if (r.finishing) {
1046 continue;
1047 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001048 effectiveNdx = activityNdx;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001049 if ((r.info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
1050 break;
1051 }
1052 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001053 return effectiveNdx;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001054 }
1055
1056 void updateEffectiveIntent() {
1057 final int effectiveRootIndex = findEffectiveRootIndex();
1058 final ActivityRecord r = mActivities.get(effectiveRootIndex);
Winson Chungfee26772014-08-05 12:21:52 -07001059 setIntent(r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001060 }
1061
Craig Mautner21d24a22014-04-23 11:45:37 -07001062 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001063 if (DEBUG_RECENTS) Slog.i(TAG_RECENTS, "Saving task=" + this);
Craig Mautner21d24a22014-04-23 11:45:37 -07001064
1065 out.attribute(null, ATTR_TASKID, String.valueOf(taskId));
1066 if (realActivity != null) {
1067 out.attribute(null, ATTR_REALACTIVITY, realActivity.flattenToShortString());
1068 }
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001069 out.attribute(null, ATTR_REALACTIVITY_SUSPENDED, String.valueOf(realActivitySuspended));
Craig Mautner21d24a22014-04-23 11:45:37 -07001070 if (origActivity != null) {
1071 out.attribute(null, ATTR_ORIGACTIVITY, origActivity.flattenToShortString());
1072 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001073 // Write affinity, and root affinity if it is different from affinity.
1074 // We use the special string "@" for a null root affinity, so we can identify
1075 // later whether we were given a root affinity or should just make it the
1076 // same as the affinity.
Craig Mautner21d24a22014-04-23 11:45:37 -07001077 if (affinity != null) {
1078 out.attribute(null, ATTR_AFFINITY, affinity);
Dianne Hackborn79228822014-09-16 11:11:23 -07001079 if (!affinity.equals(rootAffinity)) {
1080 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
1081 }
1082 } else if (rootAffinity != null) {
1083 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
Craig Mautner21d24a22014-04-23 11:45:37 -07001084 }
1085 out.attribute(null, ATTR_ROOTHASRESET, String.valueOf(rootWasReset));
Dianne Hackborn13420f22014-07-18 15:43:56 -07001086 out.attribute(null, ATTR_AUTOREMOVERECENTS, String.valueOf(autoRemoveRecents));
Craig Mautner21d24a22014-04-23 11:45:37 -07001087 out.attribute(null, ATTR_ASKEDCOMPATMODE, String.valueOf(askedCompatMode));
1088 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001089 out.attribute(null, ATTR_USER_SETUP_COMPLETE, String.valueOf(mUserSetupComplete));
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001090 out.attribute(null, ATTR_EFFECTIVE_UID, String.valueOf(effectiveUid));
Craig Mautner21d24a22014-04-23 11:45:37 -07001091 out.attribute(null, ATTR_TASKTYPE, String.valueOf(taskType));
Winson Chungffa2ec62014-07-03 15:54:42 -07001092 out.attribute(null, ATTR_FIRSTACTIVETIME, String.valueOf(firstActiveTime));
Winson Chungf1fbd772014-06-24 18:06:58 -07001093 out.attribute(null, ATTR_LASTACTIVETIME, String.valueOf(lastActiveTime));
Craig Mautner21d24a22014-04-23 11:45:37 -07001094 out.attribute(null, ATTR_LASTTIMEMOVED, String.valueOf(mLastTimeMoved));
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001095 out.attribute(null, ATTR_NEVERRELINQUISH, String.valueOf(mNeverRelinquishIdentity));
Craig Mautner21d24a22014-04-23 11:45:37 -07001096 if (lastDescription != null) {
1097 out.attribute(null, ATTR_LASTDESCRIPTION, lastDescription.toString());
1098 }
Winson Chung2cb86c72014-06-25 12:03:30 -07001099 if (lastTaskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07001100 lastTaskDescription.saveToXml(out);
Winson Chung2cb86c72014-06-25 12:03:30 -07001101 }
Winsonc809cbb2015-11-02 12:06:15 -08001102 mLastThumbnailInfo.saveToXml(out);
Winson Chungec396d62014-08-06 17:08:00 -07001103 out.attribute(null, ATTR_TASK_AFFILIATION_COLOR, String.valueOf(mAffiliatedTaskColor));
Craig Mautnera228ae92014-07-09 05:44:55 -07001104 out.attribute(null, ATTR_TASK_AFFILIATION, String.valueOf(mAffiliatedTaskId));
1105 out.attribute(null, ATTR_PREV_AFFILIATION, String.valueOf(mPrevAffiliateTaskId));
1106 out.attribute(null, ATTR_NEXT_AFFILIATION, String.valueOf(mNextAffiliateTaskId));
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001107 out.attribute(null, ATTR_CALLING_UID, String.valueOf(mCallingUid));
1108 out.attribute(null, ATTR_CALLING_PACKAGE, mCallingPackage == null ? "" : mCallingPackage);
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001109 out.attribute(null, ATTR_RESIZE_MODE, String.valueOf(mResizeMode));
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001110 out.attribute(null, ATTR_PRIVILEGED, String.valueOf(mPrivileged));
Wale Ogunwale706ed792015-08-02 10:29:44 -07001111 if (mLastNonFullscreenBounds != null) {
1112 out.attribute(
1113 null, ATTR_NON_FULLSCREEN_BOUNDS, mLastNonFullscreenBounds.flattenToString());
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001114 }
Winson Chung2cb86c72014-06-25 12:03:30 -07001115
Craig Mautner21d24a22014-04-23 11:45:37 -07001116 if (affinityIntent != null) {
1117 out.startTag(null, TAG_AFFINITYINTENT);
1118 affinityIntent.saveToXml(out);
1119 out.endTag(null, TAG_AFFINITYINTENT);
1120 }
1121
1122 out.startTag(null, TAG_INTENT);
1123 intent.saveToXml(out);
1124 out.endTag(null, TAG_INTENT);
1125
1126 final ArrayList<ActivityRecord> activities = mActivities;
1127 final int numActivities = activities.size();
1128 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1129 final ActivityRecord r = activities.get(activityNdx);
Craig Mautner43e52ed2014-06-16 17:18:52 -07001130 if (r.info.persistableMode == ActivityInfo.PERSIST_ROOT_ONLY || !r.isPersistable() ||
Wale Ogunwale843bfb92015-03-27 11:06:48 -07001131 ((r.intent.getFlags() & FLAG_ACTIVITY_NEW_DOCUMENT
1132 | FLAG_ACTIVITY_RETAIN_IN_RECENTS) == FLAG_ACTIVITY_NEW_DOCUMENT) &&
Craig Mautner43e52ed2014-06-16 17:18:52 -07001133 activityNdx > 0) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001134 // Stop at first non-persistable or first break in task (CLEAR_WHEN_TASK_RESET).
Craig Mautner21d24a22014-04-23 11:45:37 -07001135 break;
1136 }
1137 out.startTag(null, TAG_ACTIVITY);
1138 r.saveToXml(out);
1139 out.endTag(null, TAG_ACTIVITY);
1140 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001141 }
1142
1143 static TaskRecord restoreFromXml(XmlPullParser in, ActivityStackSupervisor stackSupervisor)
1144 throws IOException, XmlPullParserException {
1145 Intent intent = null;
1146 Intent affinityIntent = null;
Winsonc809cbb2015-11-02 12:06:15 -08001147 ArrayList<ActivityRecord> activities = new ArrayList<>();
Craig Mautner21d24a22014-04-23 11:45:37 -07001148 ComponentName realActivity = null;
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001149 boolean realActivitySuspended = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001150 ComponentName origActivity = null;
1151 String affinity = null;
Dianne Hackborn79228822014-09-16 11:11:23 -07001152 String rootAffinity = null;
1153 boolean hasRootAffinity = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001154 boolean rootHasReset = false;
Dianne Hackborn13420f22014-07-18 15:43:56 -07001155 boolean autoRemoveRecents = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001156 boolean askedCompatMode = false;
1157 int taskType = ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner21d24a22014-04-23 11:45:37 -07001158 int userId = 0;
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001159 boolean userSetupComplete = true;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001160 int effectiveUid = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -07001161 String lastDescription = null;
Winson Chungffa2ec62014-07-03 15:54:42 -07001162 long firstActiveTime = -1;
Winson Chung2cb86c72014-06-25 12:03:30 -07001163 long lastActiveTime = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -07001164 long lastTimeOnTop = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001165 boolean neverRelinquishIdentity = true;
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001166 int taskId = INVALID_TASK_ID;
Craig Mautner21d24a22014-04-23 11:45:37 -07001167 final int outerDepth = in.getDepth();
Craig Mautner648f69b2014-09-18 14:16:26 -07001168 TaskDescription taskDescription = new TaskDescription();
Winsonc809cbb2015-11-02 12:06:15 -08001169 TaskThumbnailInfo thumbnailInfo = new TaskThumbnailInfo();
Wale Ogunwale18795a22014-12-03 11:38:33 -08001170 int taskAffiliation = INVALID_TASK_ID;
Winson Chungec396d62014-08-06 17:08:00 -07001171 int taskAffiliationColor = 0;
Wale Ogunwale18795a22014-12-03 11:38:33 -08001172 int prevTaskId = INVALID_TASK_ID;
1173 int nextTaskId = INVALID_TASK_ID;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001174 int callingUid = -1;
1175 String callingPackage = "";
Wale Ogunwaled829d362016-02-10 19:24:49 -08001176 int resizeMode = RESIZE_MODE_FORCE_RESIZEABLE;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001177 boolean privileged = false;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001178 Rect bounds = null;
Craig Mautner21d24a22014-04-23 11:45:37 -07001179
1180 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
1181 final String attrName = in.getAttributeName(attrNdx);
1182 final String attrValue = in.getAttributeValue(attrNdx);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001183 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: attribute name=" +
1184 attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001185 if (ATTR_TASKID.equals(attrName)) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001186 if (taskId == INVALID_TASK_ID) taskId = Integer.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001187 } else if (ATTR_REALACTIVITY.equals(attrName)) {
1188 realActivity = ComponentName.unflattenFromString(attrValue);
Andrei Stingaceanu4ccec532016-01-13 12:10:21 +00001189 } else if (ATTR_REALACTIVITY_SUSPENDED.equals(attrName)) {
1190 realActivitySuspended = Boolean.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001191 } else if (ATTR_ORIGACTIVITY.equals(attrName)) {
1192 origActivity = ComponentName.unflattenFromString(attrValue);
1193 } else if (ATTR_AFFINITY.equals(attrName)) {
1194 affinity = attrValue;
Dianne Hackborn79228822014-09-16 11:11:23 -07001195 } else if (ATTR_ROOT_AFFINITY.equals(attrName)) {
1196 rootAffinity = attrValue;
1197 hasRootAffinity = true;
Craig Mautner21d24a22014-04-23 11:45:37 -07001198 } else if (ATTR_ROOTHASRESET.equals(attrName)) {
1199 rootHasReset = Boolean.valueOf(attrValue);
Dianne Hackborn13420f22014-07-18 15:43:56 -07001200 } else if (ATTR_AUTOREMOVERECENTS.equals(attrName)) {
1201 autoRemoveRecents = Boolean.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001202 } else if (ATTR_ASKEDCOMPATMODE.equals(attrName)) {
1203 askedCompatMode = Boolean.valueOf(attrValue);
1204 } else if (ATTR_USERID.equals(attrName)) {
1205 userId = Integer.valueOf(attrValue);
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001206 } else if (ATTR_USER_SETUP_COMPLETE.equals(attrName)) {
1207 userSetupComplete = Boolean.valueOf(attrValue);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001208 } else if (ATTR_EFFECTIVE_UID.equals(attrName)) {
1209 effectiveUid = Integer.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001210 } else if (ATTR_TASKTYPE.equals(attrName)) {
1211 taskType = Integer.valueOf(attrValue);
Winson Chungffa2ec62014-07-03 15:54:42 -07001212 } else if (ATTR_FIRSTACTIVETIME.equals(attrName)) {
1213 firstActiveTime = Long.valueOf(attrValue);
Winson Chungf1fbd772014-06-24 18:06:58 -07001214 } else if (ATTR_LASTACTIVETIME.equals(attrName)) {
1215 lastActiveTime = Long.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001216 } else if (ATTR_LASTDESCRIPTION.equals(attrName)) {
1217 lastDescription = attrValue;
1218 } else if (ATTR_LASTTIMEMOVED.equals(attrName)) {
1219 lastTimeOnTop = Long.valueOf(attrValue);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001220 } else if (ATTR_NEVERRELINQUISH.equals(attrName)) {
1221 neverRelinquishIdentity = Boolean.valueOf(attrValue);
Winsonc809cbb2015-11-02 12:06:15 -08001222 } else if (attrName.startsWith(TaskThumbnailInfo.ATTR_TASK_THUMBNAILINFO_PREFIX)) {
1223 thumbnailInfo.restoreFromXml(attrName, attrValue);
Craig Mautner648f69b2014-09-18 14:16:26 -07001224 } else if (attrName.startsWith(TaskDescription.ATTR_TASKDESCRIPTION_PREFIX)) {
1225 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001226 } else if (ATTR_TASK_AFFILIATION.equals(attrName)) {
1227 taskAffiliation = Integer.valueOf(attrValue);
1228 } else if (ATTR_PREV_AFFILIATION.equals(attrName)) {
1229 prevTaskId = Integer.valueOf(attrValue);
1230 } else if (ATTR_NEXT_AFFILIATION.equals(attrName)) {
1231 nextTaskId = Integer.valueOf(attrValue);
Winson Chungec396d62014-08-06 17:08:00 -07001232 } else if (ATTR_TASK_AFFILIATION_COLOR.equals(attrName)) {
1233 taskAffiliationColor = Integer.valueOf(attrValue);
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001234 } else if (ATTR_CALLING_UID.equals(attrName)) {
1235 callingUid = Integer.valueOf(attrValue);
1236 } else if (ATTR_CALLING_PACKAGE.equals(attrName)) {
1237 callingPackage = attrValue;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001238 } else if (ATTR_RESIZE_MODE.equals(attrName)) {
1239 resizeMode = Integer.valueOf(attrValue);
Wale Ogunwaled829d362016-02-10 19:24:49 -08001240 resizeMode = (resizeMode == RESIZE_MODE_CROP_WINDOWS)
1241 ? RESIZE_MODE_FORCE_RESIZEABLE : resizeMode;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001242 } else if (ATTR_PRIVILEGED.equals(attrName)) {
1243 privileged = Boolean.valueOf(attrValue);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001244 } else if (ATTR_NON_FULLSCREEN_BOUNDS.equals(attrName)) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001245 bounds = Rect.unflattenFromString(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001246 } else {
1247 Slog.w(TAG, "TaskRecord: Unknown attribute=" + attrName);
1248 }
1249 }
1250
1251 int event;
1252 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
Ben Kwa8814cf42015-07-08 10:54:56 -07001253 (event != XmlPullParser.END_TAG || in.getDepth() >= outerDepth)) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001254 if (event == XmlPullParser.START_TAG) {
1255 final String name = in.getName();
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001256 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: START_TAG name=" +
1257 name);
Craig Mautner21d24a22014-04-23 11:45:37 -07001258 if (TAG_AFFINITYINTENT.equals(name)) {
1259 affinityIntent = Intent.restoreFromXml(in);
1260 } else if (TAG_INTENT.equals(name)) {
1261 intent = Intent.restoreFromXml(in);
1262 } else if (TAG_ACTIVITY.equals(name)) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001263 ActivityRecord activity = ActivityRecord.restoreFromXml(in, stackSupervisor);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001264 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: activity=" +
1265 activity);
Craig Mautner21d24a22014-04-23 11:45:37 -07001266 if (activity != null) {
1267 activities.add(activity);
1268 }
1269 } else {
1270 Slog.e(TAG, "restoreTask: Unexpected name=" + name);
1271 XmlUtils.skipCurrentTag(in);
1272 }
1273 }
1274 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001275 if (!hasRootAffinity) {
1276 rootAffinity = affinity;
1277 } else if ("@".equals(rootAffinity)) {
1278 rootAffinity = null;
1279 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001280 if (effectiveUid <= 0) {
1281 Intent checkIntent = intent != null ? intent : affinityIntent;
1282 effectiveUid = 0;
1283 if (checkIntent != null) {
1284 IPackageManager pm = AppGlobals.getPackageManager();
1285 try {
1286 ApplicationInfo ai = pm.getApplicationInfo(
1287 checkIntent.getComponent().getPackageName(),
1288 PackageManager.GET_UNINSTALLED_PACKAGES
1289 | PackageManager.GET_DISABLED_COMPONENTS, userId);
1290 if (ai != null) {
1291 effectiveUid = ai.uid;
1292 }
1293 } catch (RemoteException e) {
1294 }
1295 }
1296 Slog.w(TAG, "Updating task #" + taskId + " for " + checkIntent
1297 + ": effectiveUid=" + effectiveUid);
1298 }
1299
Craig Mautner21d24a22014-04-23 11:45:37 -07001300 final TaskRecord task = new TaskRecord(stackSupervisor.mService, taskId, intent,
Dianne Hackborn79228822014-09-16 11:11:23 -07001301 affinityIntent, affinity, rootAffinity, realActivity, origActivity, rootHasReset,
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001302 autoRemoveRecents, askedCompatMode, taskType, userId, effectiveUid, lastDescription,
1303 activities, firstActiveTime, lastActiveTime, lastTimeOnTop, neverRelinquishIdentity,
Winsonc809cbb2015-11-02 12:06:15 -08001304 taskDescription, thumbnailInfo, taskAffiliation, prevTaskId, nextTaskId,
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001305 taskAffiliationColor, callingUid, callingPackage, resizeMode, privileged,
Wale Ogunwalef80170f2016-02-04 15:12:29 -08001306 realActivitySuspended, userSetupComplete);
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07001307 task.updateOverrideConfiguration(bounds);
Craig Mautner21d24a22014-04-23 11:45:37 -07001308
1309 for (int activityNdx = activities.size() - 1; activityNdx >=0; --activityNdx) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001310 activities.get(activityNdx).task = task;
Craig Mautner21d24a22014-04-23 11:45:37 -07001311 }
1312
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001313 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Restored task=" + task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001314 return task;
1315 }
1316
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001317 /**
1318 * Update task's override configuration based on the bounds.
Jorim Jaggi0a932142016-02-01 17:42:25 -08001319 * @param bounds The bounds of the task.
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001320 * @return Update configuration or null if there is no change.
1321 */
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07001322 Configuration updateOverrideConfiguration(Rect bounds) {
Jorim Jaggi0a932142016-02-01 17:42:25 -08001323 return updateOverrideConfiguration(bounds, null /* insetBounds */);
1324 }
1325
1326 /**
1327 * Update task's override configuration based on the bounds.
1328 * @param bounds The bounds of the task.
1329 * @param insetBounds The bounds used to calculate the system insets, which is used here to
1330 * subtract the navigation bar/status bar size from the screen size reported
1331 * to the application. See {@link IActivityManager#resizeDockedStack}.
1332 * @return Update configuration or null if there is no change.
1333 */
1334 Configuration updateOverrideConfiguration(Rect bounds, @Nullable Rect insetBounds) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001335 if (Objects.equals(mBounds, bounds)) {
1336 return null;
1337 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001338 final Configuration oldConfig = mOverrideConfig;
1339 final boolean oldFullscreen = mFullscreen;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001340
1341 mFullscreen = bounds == null;
Wale Ogunwale706ed792015-08-02 10:29:44 -07001342 if (mFullscreen) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001343 if (mBounds != null && StackId.persistTaskBounds(stack.mStackId)) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001344 mLastNonFullscreenBounds = mBounds;
1345 }
1346 mBounds = null;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001347 mOverrideConfig = Configuration.EMPTY;
Wale Ogunwale706ed792015-08-02 10:29:44 -07001348 } else {
Chong Zhangb15758a2015-11-17 12:12:03 -08001349 if (mBounds == null) {
1350 mBounds = new Rect(bounds);
1351 } else {
1352 mBounds.set(bounds);
1353 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001354 if (stack == null || StackId.persistTaskBounds(stack.mStackId)) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001355 mLastNonFullscreenBounds = mBounds;
1356 }
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001357 mOverrideConfig = calculateOverrideConfig(bounds, insetBounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001358 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001359
1360 if (mFullscreen != oldFullscreen) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08001361 mService.mStackSupervisor.scheduleReportMultiWindowChanged(this);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001362 }
1363
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001364 return !mOverrideConfig.equals(oldConfig) ? mOverrideConfig : null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001365 }
1366
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001367 private void subtractNonDecorInsets(Rect inOutBounds, Rect inInsetBounds) {
1368 mTmpRect2.set(inInsetBounds);
1369 mService.mWindowManager.subtractNonDecorInsets(mTmpRect2);
1370 int leftInset = mTmpRect2.left - inInsetBounds.left;
1371 int topInset = mTmpRect2.top - inInsetBounds.top;
1372 int rightInset = inInsetBounds.right - mTmpRect2.right;
1373 int bottomInset = inInsetBounds.bottom - mTmpRect2.bottom;
1374 inOutBounds.inset(leftInset, topInset, rightInset, bottomInset);
1375 }
1376
Jorim Jaggi0a932142016-02-01 17:42:25 -08001377 private void subtractStableInsets(Rect inOutBounds, Rect inInsetBounds) {
1378 mTmpRect2.set(inInsetBounds);
1379 mService.mWindowManager.subtractStableInsets(mTmpRect2);
1380 int leftInset = mTmpRect2.left - inInsetBounds.left;
1381 int topInset = mTmpRect2.top - inInsetBounds.top;
1382 int rightInset = inInsetBounds.right - mTmpRect2.right;
1383 int bottomInset = inInsetBounds.bottom - mTmpRect2.bottom;
1384 inOutBounds.inset(leftInset, topInset, rightInset, bottomInset);
1385 }
1386
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001387 private Configuration calculateOverrideConfig(Rect bounds, Rect insetBounds) {
1388 mTmpNonDecorBounds.set(bounds);
1389 mTmpStableBounds.set(bounds);
1390 subtractNonDecorInsets(
1391 mTmpNonDecorBounds, insetBounds != null ? insetBounds : bounds);
1392 subtractStableInsets(
1393 mTmpStableBounds, insetBounds != null ? insetBounds : bounds);
1394
1395 // For calculating screenWidthDp, screenWidthDp, we use the stable inset screen area,
1396 // i.e. the screen area without the system bars.
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08001397 final Configuration serviceConfig = mService.mConfiguration;
1398 final Configuration config = new Configuration(Configuration.EMPTY);
1399 // TODO(multidisplay): Update Dp to that of display stack is on.
1400 final float density = serviceConfig.densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE;
1401 config.screenWidthDp =
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001402 Math.min((int)(mTmpStableBounds.width() / density), serviceConfig.screenWidthDp);
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08001403 config.screenHeightDp =
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001404 Math.min((int)(mTmpStableBounds.height() / density), serviceConfig.screenHeightDp);
1405 config.smallestScreenWidthDp = Math.min(config.screenWidthDp, config.screenHeightDp);
1406
1407 // TODO: Orientation?
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08001408 config.orientation = (config.screenWidthDp <= config.screenHeightDp)
1409 ? Configuration.ORIENTATION_PORTRAIT
1410 : Configuration.ORIENTATION_LANDSCAPE;
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001411
1412 // For calculating screen layout, we need to use the non-decor inset screen area for the
1413 // calculation for compatibility reasons, i.e. screen area without system bars that could
1414 // never go away in Honeycomb.
1415 final int compatScreenWidthDp = (int)(mTmpNonDecorBounds.width() / density);
1416 final int compatScreenHeightDp = (int)(mTmpNonDecorBounds.height() / density);
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08001417 final int sl = Configuration.resetScreenLayout(serviceConfig.screenLayout);
Jorim Jaggi82c9dc92016-02-05 15:10:33 -08001418 final int longSize = Math.max(compatScreenHeightDp, compatScreenWidthDp);
1419 final int shortSize = Math.min(compatScreenHeightDp, compatScreenWidthDp);
Jorim Jaggi0a932142016-02-01 17:42:25 -08001420 config.screenLayout = Configuration.reduceScreenLayout(sl, longSize, shortSize);
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08001421 return config;
1422 }
1423
1424 /**
1425 * Using the existing configuration {@param config}, creates a new task override config such
1426 * that all the fields that are usually set in an override config are set to the ones in
1427 * {@param config}.
1428 */
1429 Configuration extractOverrideConfig(Configuration config) {
1430 final Configuration extracted = new Configuration(Configuration.EMPTY);
1431 extracted.screenWidthDp = config.screenWidthDp;
1432 extracted.screenHeightDp = config.screenHeightDp;
1433 extracted.smallestScreenWidthDp = config.smallestScreenWidthDp;
1434 extracted.orientation = config.orientation;
1435 extracted.screenLayout = config.screenLayout;
1436 return extracted;
1437 }
1438
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001439 Rect updateOverrideConfigurationFromLaunchBounds() {
1440 final Rect bounds = validateBounds(getLaunchBounds());
1441 updateOverrideConfiguration(bounds);
1442 return bounds;
1443 }
1444
1445 static Rect validateBounds(Rect bounds) {
1446 if (bounds != null && bounds.isEmpty()) {
1447 Slog.wtf(TAG, "Received strange task bounds: " + bounds, new Throwable());
1448 return null;
1449 }
1450 return bounds;
1451 }
1452
Wale Ogunwale935e5022015-11-10 12:36:10 -08001453 /** Updates the task's bounds and override configuration to match what is expected for the
1454 * input stack. */
1455 void updateOverrideConfigurationForStack(ActivityStack inStack) {
1456 if (stack != null && stack == inStack) {
1457 return;
1458 }
1459
1460 if (inStack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001461 if (!isResizeable()) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08001462 throw new IllegalArgumentException("Can not position non-resizeable task="
1463 + this + " in stack=" + inStack);
1464 }
1465 if (mBounds != null) {
1466 return;
1467 }
1468 if (mLastNonFullscreenBounds != null) {
1469 updateOverrideConfiguration(mLastNonFullscreenBounds);
1470 } else {
1471 inStack.layoutTaskInStack(this, null);
1472 }
1473 } else {
1474 updateOverrideConfiguration(inStack.mBounds);
1475 }
1476 }
1477
Chong Zhang0fa656b2015-08-31 15:17:21 -07001478 /**
1479 * Returns the correct stack to use based on task type and currently set bounds,
1480 * regardless of the focused stack and current stack association of the task.
1481 * The task will be moved (and stack focus changed) later if necessary.
1482 */
1483 int getLaunchStackId() {
1484 if (!isApplicationTask()) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001485 return HOME_STACK_ID;
1486 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001487 if (mBounds != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001488 return FREEFORM_WORKSPACE_STACK_ID;
1489 }
1490 return FULLSCREEN_WORKSPACE_STACK_ID;
1491 }
1492
1493 /** Returns the bounds that should be used to launch this task. */
1494 Rect getLaunchBounds() {
Chong Zhang75b37202015-12-04 14:16:36 -08001495 // If we're over lockscreen, forget about stack bounds and use fullscreen.
1496 if (mService.mLockScreenShown == LOCK_SCREEN_SHOWN) {
1497 return null;
1498 }
1499
Chong Zhang7d5f5102016-01-13 10:29:24 -08001500 if (stack == null) {
1501 return null;
1502 }
1503
1504 final int stackId = stack.mStackId;
1505 if (stackId == HOME_STACK_ID
1506 || stackId == FULLSCREEN_WORKSPACE_STACK_ID
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001507 || (stackId == DOCKED_STACK_ID && !isResizeable())) {
1508 return isResizeable() ? stack.mBounds : null;
Wale Ogunwale3797c222015-10-27 14:21:58 -07001509 } else if (!StackId.persistTaskBounds(stackId)) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001510 return stack.mBounds;
Wale Ogunwale706ed792015-08-02 10:29:44 -07001511 }
1512 return mLastNonFullscreenBounds;
1513 }
1514
Wale Ogunwale39381972015-12-17 17:15:29 -08001515 boolean canMatchRootAffinity() {
1516 // We don't allow root affinity matching on the pinned stack as no other task should
1517 // be launching in it based on affinity.
1518 return rootAffinity != null && (stack == null || stack.mStackId != PINNED_STACK_ID);
1519 }
1520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001522 pw.print(prefix); pw.print("userId="); pw.print(userId);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001523 pw.print(" effectiveUid="); UserHandle.formatUid(pw, effectiveUid);
1524 pw.print(" mCallingUid="); UserHandle.formatUid(pw, mCallingUid);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001525 pw.print(" mCallingPackage="); pw.println(mCallingPackage);
Dianne Hackborn79228822014-09-16 11:11:23 -07001526 if (affinity != null || rootAffinity != null) {
1527 pw.print(prefix); pw.print("affinity="); pw.print(affinity);
1528 if (affinity == null || !affinity.equals(rootAffinity)) {
1529 pw.print(" root="); pw.println(rootAffinity);
1530 } else {
1531 pw.println();
1532 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001533 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07001534 if (voiceSession != null || voiceInteractor != null) {
1535 pw.print(prefix); pw.print("VOICE: session=0x");
1536 pw.print(Integer.toHexString(System.identityHashCode(voiceSession)));
1537 pw.print(" interactor=0x");
1538 pw.println(Integer.toHexString(System.identityHashCode(voiceInteractor)));
1539 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001540 if (intent != null) {
1541 StringBuilder sb = new StringBuilder(128);
1542 sb.append(prefix); sb.append("intent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08001543 intent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001544 sb.append('}');
1545 pw.println(sb.toString());
1546 }
1547 if (affinityIntent != null) {
1548 StringBuilder sb = new StringBuilder(128);
1549 sb.append(prefix); sb.append("affinityIntent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08001550 affinityIntent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001551 sb.append('}');
1552 pw.println(sb.toString());
1553 }
1554 if (origActivity != null) {
1555 pw.print(prefix); pw.print("origActivity=");
1556 pw.println(origActivity.flattenToShortString());
1557 }
1558 if (realActivity != null) {
1559 pw.print(prefix); pw.print("realActivity=");
1560 pw.println(realActivity.flattenToShortString());
1561 }
Dianne Hackborn852975d2014-08-22 17:42:43 -07001562 if (autoRemoveRecents || isPersistable || taskType != 0 || mTaskToReturnTo != 0
1563 || numFullscreen != 0) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001564 pw.print(prefix); pw.print("autoRemoveRecents="); pw.print(autoRemoveRecents);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001565 pw.print(" isPersistable="); pw.print(isPersistable);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001566 pw.print(" numFullscreen="); pw.print(numFullscreen);
1567 pw.print(" taskType="); pw.print(taskType);
1568 pw.print(" mTaskToReturnTo="); pw.println(mTaskToReturnTo);
1569 }
Craig Mautner432f64e2015-05-20 14:59:57 -07001570 if (rootWasReset || mNeverRelinquishIdentity || mReuseTask
1571 || mLockTaskAuth != LOCK_TASK_AUTH_PINNABLE) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001572 pw.print(prefix); pw.print("rootWasReset="); pw.print(rootWasReset);
1573 pw.print(" mNeverRelinquishIdentity="); pw.print(mNeverRelinquishIdentity);
Craig Mautner432f64e2015-05-20 14:59:57 -07001574 pw.print(" mReuseTask="); pw.print(mReuseTask);
1575 pw.print(" mLockTaskAuth="); pw.println(lockTaskAuthToString());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001576 }
Wale Ogunwale18795a22014-12-03 11:38:33 -08001577 if (mAffiliatedTaskId != taskId || mPrevAffiliateTaskId != INVALID_TASK_ID
1578 || mPrevAffiliate != null || mNextAffiliateTaskId != INVALID_TASK_ID
1579 || mNextAffiliate != null) {
Dianne Hackborn852975d2014-08-22 17:42:43 -07001580 pw.print(prefix); pw.print("affiliation="); pw.print(mAffiliatedTaskId);
1581 pw.print(" prevAffiliation="); pw.print(mPrevAffiliateTaskId);
1582 pw.print(" (");
1583 if (mPrevAffiliate == null) {
1584 pw.print("null");
1585 } else {
1586 pw.print(Integer.toHexString(System.identityHashCode(mPrevAffiliate)));
1587 }
1588 pw.print(") nextAffiliation="); pw.print(mNextAffiliateTaskId);
1589 pw.print(" (");
1590 if (mNextAffiliate == null) {
1591 pw.print("null");
1592 } else {
1593 pw.print(Integer.toHexString(System.identityHashCode(mNextAffiliate)));
1594 }
1595 pw.println(")");
1596 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001597 pw.print(prefix); pw.print("Activities="); pw.println(mActivities);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001598 if (!askedCompatMode || !inRecents || !isAvailable) {
1599 pw.print(prefix); pw.print("askedCompatMode="); pw.print(askedCompatMode);
1600 pw.print(" inRecents="); pw.print(inRecents);
1601 pw.print(" isAvailable="); pw.println(isAvailable);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07001602 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001603 pw.print(prefix); pw.print("lastThumbnail="); pw.print(mLastThumbnail);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001604 pw.print(" lastThumbnailFile="); pw.println(mLastThumbnailFile);
1605 if (lastDescription != null) {
1606 pw.print(prefix); pw.print("lastDescription="); pw.println(lastDescription);
1607 }
Wale Ogunwale069fbe42015-07-29 11:38:01 -07001608 if (stack != null) {
1609 pw.print(prefix); pw.print("stackId="); pw.println(stack.mStackId);
1610 }
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001611 pw.print(prefix + "hasBeenVisible=" + hasBeenVisible);
1612 pw.print(" mResizeMode=" + ActivityInfo.resizeModeToString(mResizeMode));
1613 pw.print(" isResizeable=" + isResizeable());
1614 pw.print(" firstActiveTime=" + lastActiveTime);
1615 pw.print(" lastActiveTime=" + lastActiveTime);
1616 pw.println(" (inactive for " + (getInactiveDuration() / 1000) + "s)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 }
1618
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001619 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001621 StringBuilder sb = new StringBuilder(128);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001622 if (stringName != null) {
1623 sb.append(stringName);
1624 sb.append(" U=");
1625 sb.append(userId);
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08001626 sb.append(" StackId=");
1627 sb.append(stack != null ? stack.mStackId : INVALID_STACK_ID);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001628 sb.append(" sz=");
1629 sb.append(mActivities.size());
1630 sb.append('}');
1631 return sb.toString();
1632 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001633 sb.append("TaskRecord{");
1634 sb.append(Integer.toHexString(System.identityHashCode(this)));
1635 sb.append(" #");
1636 sb.append(taskId);
1637 if (affinity != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001638 sb.append(" A=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001639 sb.append(affinity);
1640 } else if (intent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001641 sb.append(" I=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001642 sb.append(intent.getComponent().flattenToShortString());
1643 } else if (affinityIntent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001644 sb.append(" aI=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001645 sb.append(affinityIntent.getComponent().flattenToShortString());
1646 } else {
1647 sb.append(" ??");
1648 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001649 stringName = sb.toString();
1650 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 }
1652}