blob: 4ce8b2f67eee0d0e4fe87f3bc56886618191f86e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Chong Zhang7d5f5102016-01-13 10:29:24 -080019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070020import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
21import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
22import static android.app.ActivityManager.StackId.HOME_STACK_ID;
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -080023import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Wale Ogunwale5f986092015-12-04 15:35:38 -080024import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwale843bfb92015-03-27 11:06:48 -070025import static android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
26import static android.content.Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
Wale Ogunwale6cae7652015-12-26 07:36:26 -080027import static android.content.pm.ActivityInfo.FLAG_RESIZEABLE;
Craig Mautner15df08a2015-04-01 12:17:18 -070028import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_ALWAYS;
29import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_DEFAULT;
30import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
31import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_NEVER;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -070032import static android.content.pm.ApplicationInfo.PRIVATE_FLAG_PRIVILEGED;
Suprabh Shukla23593142015-11-03 17:31:15 -080033import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ADD_REMOVE;
34import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_LOCKTASK;
35import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
36import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS;
37import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_ADD_REMOVE;
38import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_LOCKTASK;
39import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS;
40import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS;
41import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
42import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Chong Zhang75b37202015-12-04 14:16:36 -080043import static com.android.server.am.ActivityManagerService.LOCK_SCREEN_SHOWN;
Craig Mautner84984fa2014-06-19 11:19:20 -070044import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Suprabh Shukla23593142015-11-03 17:31:15 -080045import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070046import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
Craig Mautnerde4ef022013-04-07 19:01:33 -070047
Craig Mautnerb0f7dc72013-04-01 16:34:45 -070048import android.app.Activity;
Craig Mautner9db9a0b2013-04-29 17:05:56 -070049import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070050import android.app.ActivityManager.StackId;
Craig Mautner648f69b2014-09-18 14:16:26 -070051import android.app.ActivityManager.TaskDescription;
Wale Ogunwaleff3c66c2015-11-18 19:22:49 -080052import android.app.ActivityManager.TaskThumbnail;
Winsonc809cbb2015-11-02 12:06:15 -080053import android.app.ActivityManager.TaskThumbnailInfo;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -070054import android.app.ActivityOptions;
Dianne Hackborn885fbe52014-08-23 15:23:58 -070055import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.content.ComponentName;
57import android.content.Intent;
58import android.content.pm.ActivityInfo;
Dianne Hackborn885fbe52014-08-23 15:23:58 -070059import android.content.pm.ApplicationInfo;
60import android.content.pm.IPackageManager;
61import android.content.pm.PackageManager;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070062import android.content.res.Configuration;
Craig Mautner9db9a0b2013-04-29 17:05:56 -070063import android.graphics.Bitmap;
Winsonc809cbb2015-11-02 12:06:15 -080064import android.graphics.Point;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -070065import android.graphics.Rect;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070066import android.os.Debug;
Craig Mautnerc0ffce52014-07-01 12:38:52 -070067import android.os.ParcelFileDescriptor;
Dianne Hackborn885fbe52014-08-23 15:23:58 -070068import android.os.RemoteException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070069import android.os.UserHandle;
Dianne Hackborn91097de2014-04-04 18:02:06 -070070import android.service.voice.IVoiceInteractionSession;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070071import android.util.DisplayMetrics;
Dianne Hackborn7f96b792012-05-29 18:46:45 -070072import android.util.Slog;
Suprabh Shukla23593142015-11-03 17:31:15 -080073
Dianne Hackborn91097de2014-04-04 18:02:06 -070074import com.android.internal.app.IVoiceInteractor;
Craig Mautner21d24a22014-04-23 11:45:37 -070075import com.android.internal.util.XmlUtils;
Suprabh Shukla23593142015-11-03 17:31:15 -080076
Craig Mautner21d24a22014-04-23 11:45:37 -070077import org.xmlpull.v1.XmlPullParser;
78import org.xmlpull.v1.XmlPullParserException;
79import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
Craig Mautnerc0ffce52014-07-01 12:38:52 -070081import java.io.File;
Craig Mautner21d24a22014-04-23 11:45:37 -070082import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import java.io.PrintWriter;
Craig Mautner5d9c7be2013-02-15 14:02:56 -080084import java.util.ArrayList;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -070085import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
Craig Mautnerc0ffce52014-07-01 12:38:52 -070087final class TaskRecord {
Wale Ogunwalee23149f2015-03-06 15:39:44 -080088 private static final String TAG = TAG_WITH_CLASS_NAME ? "TaskRecord" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070089 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070090 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Craig Mautnere0570202015-05-13 13:06:11 -070091 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070092 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080093
Wale Ogunwale18795a22014-12-03 11:38:33 -080094 static final String ATTR_TASKID = "task_id";
Craig Mautner21d24a22014-04-23 11:45:37 -070095 private static final String TAG_INTENT = "intent";
96 private static final String TAG_AFFINITYINTENT = "affinity_intent";
Wale Ogunwale18795a22014-12-03 11:38:33 -080097 static final String ATTR_REALACTIVITY = "real_activity";
Craig Mautner21d24a22014-04-23 11:45:37 -070098 private static final String ATTR_ORIGACTIVITY = "orig_activity";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -070099 private static final String TAG_ACTIVITY = "activity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700100 private static final String ATTR_AFFINITY = "affinity";
Dianne Hackborn79228822014-09-16 11:11:23 -0700101 private static final String ATTR_ROOT_AFFINITY = "root_affinity";
Craig Mautner21d24a22014-04-23 11:45:37 -0700102 private static final String ATTR_ROOTHASRESET = "root_has_reset";
Dianne Hackborn13420f22014-07-18 15:43:56 -0700103 private static final String ATTR_AUTOREMOVERECENTS = "auto_remove_recents";
Craig Mautner21d24a22014-04-23 11:45:37 -0700104 private static final String ATTR_ASKEDCOMPATMODE = "asked_compat_mode";
105 private static final String ATTR_USERID = "user_id";
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700106 private static final String ATTR_EFFECTIVE_UID = "effective_uid";
Craig Mautner21d24a22014-04-23 11:45:37 -0700107 private static final String ATTR_TASKTYPE = "task_type";
Winson Chungffa2ec62014-07-03 15:54:42 -0700108 private static final String ATTR_FIRSTACTIVETIME = "first_active_time";
Winson Chungf1fbd772014-06-24 18:06:58 -0700109 private static final String ATTR_LASTACTIVETIME = "last_active_time";
Craig Mautner21d24a22014-04-23 11:45:37 -0700110 private static final String ATTR_LASTDESCRIPTION = "last_description";
111 private static final String ATTR_LASTTIMEMOVED = "last_time_moved";
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700112 private static final String ATTR_NEVERRELINQUISH = "never_relinquish_identity";
Wale Ogunwale18795a22014-12-03 11:38:33 -0800113 static final String ATTR_TASK_AFFILIATION = "task_affiliation";
Craig Mautnera228ae92014-07-09 05:44:55 -0700114 private static final String ATTR_PREV_AFFILIATION = "prev_affiliation";
115 private static final String ATTR_NEXT_AFFILIATION = "next_affiliation";
Winson Chungec396d62014-08-06 17:08:00 -0700116 private static final String ATTR_TASK_AFFILIATION_COLOR = "task_affiliation_color";
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700117 private static final String ATTR_CALLING_UID = "calling_uid";
118 private static final String ATTR_CALLING_PACKAGE = "calling_package";
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800119 private static final String ATTR_RESIZEABLE = "resizeable";
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700120 private static final String ATTR_PRIVILEGED = "privileged";
Wale Ogunwale706ed792015-08-02 10:29:44 -0700121 private static final String ATTR_NON_FULLSCREEN_BOUNDS = "non_fullscreen_bounds";
Craig Mautner21d24a22014-04-23 11:45:37 -0700122
123 private static final String TASK_THUMBNAIL_SUFFIX = "_task_thumbnail";
124
Craig Mautner0b633fc2014-07-23 10:42:18 -0700125 static final boolean IGNORE_RETURN_TO_RECENTS = true;
126
Wale Ogunwale18795a22014-12-03 11:38:33 -0800127 static final int INVALID_TASK_ID = -1;
128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 final int taskId; // Unique identifier for this task.
Dianne Hackborn79228822014-09-16 11:11:23 -0700130 String affinity; // The affinity name for this task, or null; may change identity.
131 String rootAffinity; // Initial base affinity, or null; does not change from initial root.
Dianne Hackborn91097de2014-04-04 18:02:06 -0700132 final IVoiceInteractionSession voiceSession; // Voice interaction session driving task
133 final IVoiceInteractor voiceInteractor; // Associated interactor to provide to app
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 Intent intent; // The original intent that started the task.
135 Intent affinityIntent; // Intent of affinity-moved activity that started this task.
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700136 int effectiveUid; // The current effective uid of the identity of this task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 ComponentName origActivity; // The non-alias activity component of the intent.
138 ComponentName realActivity; // The actual activity component that started the task.
Winson Chungffa2ec62014-07-03 15:54:42 -0700139 long firstActiveTime; // First time this task was active.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 long lastActiveTime; // Last time this task was active, including sleep.
Dianne Hackborn852975d2014-08-22 17:42:43 -0700141 boolean inRecents; // Actually in the recents list?
142 boolean isAvailable; // Is the activity available to be launched?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 boolean rootWasReset; // True if the intent at the root of the task had
144 // the FLAG_ACTIVITY_RESET_TASK_IF_NEEDED flag.
Dianne Hackborn13420f22014-07-18 15:43:56 -0700145 boolean autoRemoveRecents; // If true, we should automatically remove the task from
146 // recents when activity finishes
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700147 boolean askedCompatMode;// Have asked the user about compat mode for this task.
Dianne Hackbornd38aed82014-06-10 21:36:35 -0700148 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 -0800149
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700150 String stringName; // caching of toString() result.
Dianne Hackborn9da2d402012-03-15 13:43:08 -0700151 int userId; // user for which this task was created
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800152
153 int numFullscreen; // Number of fullscreen activities.
154
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800155 boolean mResizeable; // Activities in the task resizeable. Based on the resizable setting of
156 // the root activity.
Craig Mautner15df08a2015-04-01 12:17:18 -0700157 int mLockTaskMode; // Which tasklock mode to launch this task in. One of
158 // ActivityManager.LOCK_TASK_LAUNCH_MODE_*
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700159 private boolean mPrivileged; // The root activity application of this task holds
160 // privileged permissions.
161
Craig Mautner15df08a2015-04-01 12:17:18 -0700162 /** Can't be put in lockTask mode. */
163 final static int LOCK_TASK_AUTH_DONT_LOCK = 0;
Benjamin Franz469dd582015-06-09 14:24:36 +0100164 /** Can enter app pinning with user approval. Can never start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700165 final static int LOCK_TASK_AUTH_PINNABLE = 1;
166 /** Starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing lockTask task. */
167 final static int LOCK_TASK_AUTH_LAUNCHABLE = 2;
Benjamin Franz469dd582015-06-09 14:24:36 +0100168 /** Can enter lockTask without user approval. Can start over existing lockTask task. */
Craig Mautner15df08a2015-04-01 12:17:18 -0700169 final static int LOCK_TASK_AUTH_WHITELISTED = 3;
Benjamin Franz469dd582015-06-09 14:24:36 +0100170 /** Priv-app that starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing
171 * lockTask task. */
172 final static int LOCK_TASK_AUTH_LAUNCHABLE_PRIV = 4;
Craig Mautner15df08a2015-04-01 12:17:18 -0700173 int mLockTaskAuth = LOCK_TASK_AUTH_PINNABLE;
174
175 int mLockTaskUid = -1; // The uid of the application that called startLockTask().
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800176
Winson Chung03a9bae2014-05-02 09:56:12 -0700177 // This represents the last resolved activity values for this task
178 // NOTE: This value needs to be persisted with each task
Craig Mautner648f69b2014-09-18 14:16:26 -0700179 TaskDescription lastTaskDescription = new TaskDescription();
Winson Chung03a9bae2014-05-02 09:56:12 -0700180
Craig Mautnerd2328952013-03-05 12:46:26 -0800181 /** List of all activities in the task arranged in history order */
Craig Mautner21d24a22014-04-23 11:45:37 -0700182 final ArrayList<ActivityRecord> mActivities;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800183
Craig Mautnerd2328952013-03-05 12:46:26 -0800184 /** Current stack */
185 ActivityStack stack;
186
Craig Mautner2c1faed2013-07-23 12:56:02 -0700187 /** Takes on same set of values as ActivityRecord.mActivityType */
Craig Mautner21d24a22014-04-23 11:45:37 -0700188 int taskType;
Craig Mautner1602ec22013-05-12 10:24:27 -0700189
Craig Mautner21d24a22014-04-23 11:45:37 -0700190 /** Takes on same value as first root activity */
191 boolean isPersistable = false;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700192 int maxRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700193
194 /** Only used for persistable tasks, otherwise 0. The last time this task was moved. Used for
195 * determining the order when restoring. Sign indicates whether last task movement was to front
196 * (positive) or back (negative). Absolute value indicates time. */
197 long mLastTimeMoved = System.currentTimeMillis();
198
Craig Mautner84984fa2014-06-19 11:19:20 -0700199 /** Indication of what to run next when task exits. Use ActivityRecord types.
200 * ActivityRecord.APPLICATION_ACTIVITY_TYPE indicates to resume the task below this one in the
201 * task stack. */
202 private int mTaskToReturnTo = APPLICATION_ACTIVITY_TYPE;
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700203
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700204 /** If original intent did not allow relinquishing task identity, save that information */
205 boolean mNeverRelinquishIdentity = true;
206
Craig Mautner362449a2014-06-20 14:04:39 -0700207 // Used in the unique case where we are clearing the task in order to reuse it. In that case we
208 // do not want to delete the stack when the task goes empty.
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -0800209 private boolean mReuseTask = false;
Craig Mautner362449a2014-06-20 14:04:39 -0700210
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700211 private Bitmap mLastThumbnail; // Last thumbnail captured for this item.
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700212 private final File mLastThumbnailFile; // File containing last thumbnail.
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700213 private final String mFilename;
Winsonc809cbb2015-11-02 12:06:15 -0800214 private TaskThumbnailInfo mLastThumbnailInfo;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700215 CharSequence lastDescription; // Last description captured for this item.
216
Craig Mautnera228ae92014-07-09 05:44:55 -0700217 int mAffiliatedTaskId; // taskId of parent affiliation or self if no parent.
Winson Chungec396d62014-08-06 17:08:00 -0700218 int mAffiliatedTaskColor; // color of the parent task affiliation.
Craig Mautnera228ae92014-07-09 05:44:55 -0700219 TaskRecord mPrevAffiliate; // previous task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800220 int mPrevAffiliateTaskId = INVALID_TASK_ID; // previous id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700221 TaskRecord mNextAffiliate; // next task in affiliated chain.
Wale Ogunwale18795a22014-12-03 11:38:33 -0800222 int mNextAffiliateTaskId = INVALID_TASK_ID; // next id for persistence.
Craig Mautnera228ae92014-07-09 05:44:55 -0700223
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700224 // For relaunching the task from recents as though it was launched by the original launcher.
225 int mCallingUid;
226 String mCallingPackage;
227
Craig Mautner21d24a22014-04-23 11:45:37 -0700228 final ActivityManagerService mService;
229
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700230 // Whether or not this task covers the entire screen; by default tasks are fullscreen.
231 boolean mFullscreen = true;
232
233 // Bounds of the Task. null for fullscreen tasks.
234 Rect mBounds = null;
Wale Ogunwale706ed792015-08-02 10:29:44 -0700235 // Last non-fullscreen bounds the task was launched in or resized to.
236 // The information is persisted and used to determine the appropriate stack to launch the
237 // task into on restore.
238 Rect mLastNonFullscreenBounds = null;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700239 // Minimal size for width/height of this task when it's resizeable. -1 means it should use the
240 // default minimal size.
241 final int mMinimalSize;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700242
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700243 // Ranking (from top) of this task among all visible tasks. (-1 means it's not visible)
244 // This number will be assigned when we evaluate OOM scores for all visible tasks.
245 int mLayerRank = -1;
246
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700247 Configuration mOverrideConfig = Configuration.EMPTY;
248
Craig Mautner21d24a22014-04-23 11:45:37 -0700249 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700250 IVoiceInteractionSession _voiceSession, IVoiceInteractor _voiceInteractor) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700251 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700252 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
253 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800254 userId = UserHandle.getUserId(info.applicationInfo.uid);
255 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800256 mLastThumbnailInfo = new TaskThumbnailInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 taskId = _taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700258 mAffiliatedTaskId = _taskId;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700259 voiceSession = _voiceSession;
260 voiceInteractor = _voiceInteractor;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700261 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800262 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700263 mCallingUid = info.applicationInfo.uid;
264 mCallingPackage = info.packageName;
Martijn Coenend4a69702014-06-30 11:12:17 -0700265 setIntent(_intent, info);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700266 mMinimalSize = info != null && info.layout != null ? info.layout.minimalSize : -1;
Craig Mautner21d24a22014-04-23 11:45:37 -0700267 }
268
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700269 TaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info, Intent _intent,
Winsonc809cbb2015-11-02 12:06:15 -0800270 TaskDescription _taskDescription, TaskThumbnailInfo thumbnailInfo) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700271 mService = service;
272 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
273 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800274 userId = UserHandle.getUserId(info.applicationInfo.uid);
275 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800276 mLastThumbnailInfo = thumbnailInfo;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700277 taskId = _taskId;
278 mAffiliatedTaskId = _taskId;
279 voiceSession = null;
280 voiceInteractor = null;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700281 isAvailable = true;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800282 mActivities = new ArrayList<>();
Craig Mautner15df08a2015-04-01 12:17:18 -0700283 mCallingUid = info.applicationInfo.uid;
284 mCallingPackage = info.packageName;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700285 setIntent(_intent, info);
286
287 taskType = ActivityRecord.APPLICATION_ACTIVITY_TYPE;
288 isPersistable = true;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700289 // Clamp to [1, max].
290 maxRecents = Math.min(Math.max(info.maxRecents, 1),
291 ActivityManager.getMaxAppRecentsLimitStatic());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700292
293 taskType = APPLICATION_ACTIVITY_TYPE;
294 mTaskToReturnTo = HOME_ACTIVITY_TYPE;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700295 lastTaskDescription = _taskDescription;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700296 mMinimalSize = info != null && info.layout != null ? info.layout.minimalSize : -1;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700297 }
298
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800299 private TaskRecord(ActivityManagerService service, int _taskId, Intent _intent,
300 Intent _affinityIntent, String _affinity, String _rootAffinity,
301 ComponentName _realActivity, ComponentName _origActivity, boolean _rootWasReset,
302 boolean _autoRemoveRecents, boolean _askedCompatMode, int _taskType, int _userId,
303 int _effectiveUid, String _lastDescription, ArrayList<ActivityRecord> activities,
304 long _firstActiveTime, long _lastActiveTime, long lastTimeMoved,
305 boolean neverRelinquishIdentity, TaskDescription _lastTaskDescription,
Winsonc809cbb2015-11-02 12:06:15 -0800306 TaskThumbnailInfo lastThumbnailInfo, int taskAffiliation, int prevTaskId,
307 int nextTaskId, int taskAffiliationColor, int callingUid, String callingPackage,
308 boolean resizeable, boolean privileged) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700309 mService = service;
Craig Mautnera228ae92014-07-09 05:44:55 -0700310 mFilename = String.valueOf(_taskId) + TASK_THUMBNAIL_SUFFIX +
311 TaskPersister.IMAGE_EXTENSION;
Suprabh Shukla23593142015-11-03 17:31:15 -0800312 mLastThumbnailFile = new File(TaskPersister.getUserImagesDir(_userId), mFilename);
Winsonc809cbb2015-11-02 12:06:15 -0800313 mLastThumbnailInfo = lastThumbnailInfo;
Craig Mautner21d24a22014-04-23 11:45:37 -0700314 taskId = _taskId;
315 intent = _intent;
316 affinityIntent = _affinityIntent;
317 affinity = _affinity;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -0800318 rootAffinity = _rootAffinity;
Craig Mautner21d24a22014-04-23 11:45:37 -0700319 voiceSession = null;
320 voiceInteractor = null;
321 realActivity = _realActivity;
322 origActivity = _origActivity;
323 rootWasReset = _rootWasReset;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700324 isAvailable = true;
Dianne Hackborn13420f22014-07-18 15:43:56 -0700325 autoRemoveRecents = _autoRemoveRecents;
Craig Mautner21d24a22014-04-23 11:45:37 -0700326 askedCompatMode = _askedCompatMode;
327 taskType = _taskType;
Craig Mautner84984fa2014-06-19 11:19:20 -0700328 mTaskToReturnTo = HOME_ACTIVITY_TYPE;
Craig Mautner21d24a22014-04-23 11:45:37 -0700329 userId = _userId;
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700330 effectiveUid = _effectiveUid;
Winson Chungffa2ec62014-07-03 15:54:42 -0700331 firstActiveTime = _firstActiveTime;
Winson Chungf1fbd772014-06-24 18:06:58 -0700332 lastActiveTime = _lastActiveTime;
Craig Mautner21d24a22014-04-23 11:45:37 -0700333 lastDescription = _lastDescription;
334 mActivities = activities;
335 mLastTimeMoved = lastTimeMoved;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700336 mNeverRelinquishIdentity = neverRelinquishIdentity;
Winson Chung2cb86c72014-06-25 12:03:30 -0700337 lastTaskDescription = _lastTaskDescription;
Craig Mautnera228ae92014-07-09 05:44:55 -0700338 mAffiliatedTaskId = taskAffiliation;
Winson Chungec396d62014-08-06 17:08:00 -0700339 mAffiliatedTaskColor = taskAffiliationColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700340 mPrevAffiliateTaskId = prevTaskId;
341 mNextAffiliateTaskId = nextTaskId;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700342 mCallingUid = callingUid;
343 mCallingPackage = callingPackage;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -0800344 mResizeable = resizeable || mService.mForceResizableActivities;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700345 mPrivileged = privileged;
Suprabh Shukla74f9f162015-11-06 15:39:42 -0800346 ActivityInfo info = (mActivities.size() > 0) ? mActivities.get(0).info : null;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700347 mMinimalSize = info != null && info.layout != null ? info.layout.minimalSize : -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 }
349
350 void touchActiveTime() {
Craig Mautner494f6bdd2014-07-21 11:17:46 -0700351 lastActiveTime = System.currentTimeMillis();
Winson Chungffa2ec62014-07-03 15:54:42 -0700352 if (firstActiveTime == 0) {
353 firstActiveTime = lastActiveTime;
354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 long getInactiveDuration() {
Craig Mautner494f6bdd2014-07-21 11:17:46 -0700358 return System.currentTimeMillis() - lastActiveTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700360
Winson Chungfee26772014-08-05 12:21:52 -0700361 /** Sets the original intent, and the calling uid and package. */
362 void setIntent(ActivityRecord r) {
Winson Chungfee26772014-08-05 12:21:52 -0700363 mCallingUid = r.launchedFromUid;
364 mCallingPackage = r.launchedFromPackage;
Craig Mautner15df08a2015-04-01 12:17:18 -0700365 setIntent(r.intent, r.info);
Winson Chungfee26772014-08-05 12:21:52 -0700366 }
367
368 /** Sets the original intent, _without_ updating the calling uid or package. */
369 private void setIntent(Intent _intent, ActivityInfo info) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700370 if (intent == null) {
371 mNeverRelinquishIdentity =
372 (info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) == 0;
373 } else if (mNeverRelinquishIdentity) {
374 return;
375 }
376
377 affinity = info.taskAffinity;
Dianne Hackborn79228822014-09-16 11:11:23 -0700378 if (intent == null) {
379 // If this task already has an intent associated with it, don't set the root
380 // affinity -- we don't want it changing after initially set, but the initially
381 // set value may be null.
382 rootAffinity = affinity;
383 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -0700384 effectiveUid = info.applicationInfo.uid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700385 stringName = null;
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 if (info.targetActivity == null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800388 if (_intent != null) {
389 // If this Intent has a selector, we want to clear it for the
390 // recent task since it is not relevant if the user later wants
391 // to re-launch the app.
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700392 if (_intent.getSelector() != null || _intent.getSourceBounds() != null) {
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800393 _intent = new Intent(_intent);
394 _intent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700395 _intent.setSourceBounds(null);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800396 }
397 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700398 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Setting Intent of " + this + " to " + _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 intent = _intent;
400 realActivity = _intent != null ? _intent.getComponent() : null;
401 origActivity = null;
402 } else {
403 ComponentName targetComponent = new ComponentName(
404 info.packageName, info.targetActivity);
405 if (_intent != null) {
406 Intent targetIntent = new Intent(_intent);
407 targetIntent.setComponent(targetComponent);
Dianne Hackbornf5b86712011-12-05 17:42:41 -0800408 targetIntent.setSelector(null);
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700409 targetIntent.setSourceBounds(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700410 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
Dianne Hackborn7f96b792012-05-29 18:46:45 -0700411 "Setting Intent of " + this + " to target " + targetIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 intent = targetIntent;
413 realActivity = targetComponent;
414 origActivity = _intent.getComponent();
415 } else {
416 intent = null;
417 realActivity = targetComponent;
418 origActivity = new ComponentName(info.packageName, info.name);
419 }
420 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700421
Craig Mautner47b20ba2014-09-17 17:23:44 -0700422 final int intentFlags = intent == null ? 0 : intent.getFlags();
423 if ((intentFlags & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 // Once we are set to an Intent with this flag, we count this
425 // task as having a true root activity.
426 rootWasReset = true;
427 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700428
Dianne Hackborn09233282014-04-30 11:33:59 -0700429 userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautner41db4a72014-05-07 17:20:56 -0700430 if ((info.flags & ActivityInfo.FLAG_AUTO_REMOVE_FROM_RECENTS) != 0) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700431 // If the activity itself has requested auto-remove, then just always do it.
432 autoRemoveRecents = true;
Wale Ogunwale843bfb92015-03-27 11:06:48 -0700433 } else if ((intentFlags & (FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_RETAIN_IN_RECENTS))
434 == FLAG_ACTIVITY_NEW_DOCUMENT) {
Dianne Hackborn13420f22014-07-18 15:43:56 -0700435 // If the caller has not asked for the document to be retained, then we may
436 // want to turn on auto-remove, depending on whether the target has set its
437 // own document launch mode.
438 if (info.documentLaunchMode != ActivityInfo.DOCUMENT_LAUNCH_NONE) {
439 autoRemoveRecents = false;
440 } else {
441 autoRemoveRecents = true;
442 }
443 } else {
444 autoRemoveRecents = false;
Craig Mautner41db4a72014-05-07 17:20:56 -0700445 }
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800446 mResizeable = (info.flags & FLAG_RESIZEABLE) != 0 || mService.mForceResizableActivities;
Craig Mautner15df08a2015-04-01 12:17:18 -0700447 mLockTaskMode = info.lockTaskLaunchMode;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -0700448 mPrivileged = (info.applicationInfo.privateFlags & PRIVATE_FLAG_PRIVILEGED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -0700449 setLockTaskAuth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800451
Craig Mautner84984fa2014-06-19 11:19:20 -0700452 void setTaskToReturnTo(int taskToReturnTo) {
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700453 mTaskToReturnTo = (IGNORE_RETURN_TO_RECENTS && taskToReturnTo == RECENTS_ACTIVITY_TYPE)
454 ? HOME_ACTIVITY_TYPE : taskToReturnTo;
Craig Mautner84984fa2014-06-19 11:19:20 -0700455 }
456
457 int getTaskToReturnTo() {
458 return mTaskToReturnTo;
459 }
460
Craig Mautnera228ae92014-07-09 05:44:55 -0700461 void setPrevAffiliate(TaskRecord prevAffiliate) {
462 mPrevAffiliate = prevAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800463 mPrevAffiliateTaskId = prevAffiliate == null ? INVALID_TASK_ID : prevAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700464 }
465
466 void setNextAffiliate(TaskRecord nextAffiliate) {
467 mNextAffiliate = nextAffiliate;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800468 mNextAffiliateTaskId = nextAffiliate == null ? INVALID_TASK_ID : nextAffiliate.taskId;
Craig Mautnera228ae92014-07-09 05:44:55 -0700469 }
470
471 // Close up recents linked list.
472 void closeRecentsChain() {
473 if (mPrevAffiliate != null) {
474 mPrevAffiliate.setNextAffiliate(mNextAffiliate);
475 }
476 if (mNextAffiliate != null) {
477 mNextAffiliate.setPrevAffiliate(mPrevAffiliate);
478 }
479 setPrevAffiliate(null);
480 setNextAffiliate(null);
481 }
482
Winson Chung740c3ac2014-11-12 16:14:38 -0800483 void removedFromRecents() {
Dianne Hackborn852975d2014-08-22 17:42:43 -0700484 disposeThumbnail();
485 closeRecentsChain();
486 if (inRecents) {
487 inRecents = false;
Winson Chung740c3ac2014-11-12 16:14:38 -0800488 mService.notifyTaskPersisterLocked(this, false);
Dianne Hackborn852975d2014-08-22 17:42:43 -0700489 }
490 }
491
Craig Mautnera228ae92014-07-09 05:44:55 -0700492 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
493 closeRecentsChain();
494 mAffiliatedTaskId = taskToAffiliateWith.mAffiliatedTaskId;
Winson Chungec396d62014-08-06 17:08:00 -0700495 mAffiliatedTaskColor = taskToAffiliateWith.mAffiliatedTaskColor;
Craig Mautnera228ae92014-07-09 05:44:55 -0700496 // Find the end
497 while (taskToAffiliateWith.mNextAffiliate != null) {
498 final TaskRecord nextRecents = taskToAffiliateWith.mNextAffiliate;
499 if (nextRecents.mAffiliatedTaskId != mAffiliatedTaskId) {
500 Slog.e(TAG, "setTaskToAffiliateWith: nextRecents=" + nextRecents + " affilTaskId="
501 + nextRecents.mAffiliatedTaskId + " should be " + mAffiliatedTaskId);
502 if (nextRecents.mPrevAffiliate == taskToAffiliateWith) {
503 nextRecents.setPrevAffiliate(null);
504 }
505 taskToAffiliateWith.setNextAffiliate(null);
506 break;
507 }
508 taskToAffiliateWith = nextRecents;
509 }
510 taskToAffiliateWith.setNextAffiliate(this);
511 setPrevAffiliate(taskToAffiliateWith);
512 setNextAffiliate(null);
513 }
514
Winson Chung096f36b2014-08-20 15:39:01 -0700515 /**
Winsonc809cbb2015-11-02 12:06:15 -0800516 * Sets the last thumbnail with the current task bounds and the system orientation.
Winson Chung096f36b2014-08-20 15:39:01 -0700517 * @return whether the thumbnail was set
518 */
Winsonc809cbb2015-11-02 12:06:15 -0800519 boolean setLastThumbnailLocked(Bitmap thumbnail) {
520 final Configuration serviceConfig = mService.mConfiguration;
521 int taskWidth = 0;
522 int taskHeight = 0;
523 if (mBounds != null) {
524 // Non-fullscreen tasks
525 taskWidth = mBounds.width();
526 taskHeight = mBounds.height();
527 } else if (stack != null) {
528 // Fullscreen tasks
529 final Point displaySize = new Point();
530 stack.getDisplaySize(displaySize);
531 taskWidth = displaySize.x;
532 taskHeight = displaySize.y;
533 } else {
534 Slog.e(TAG, "setLastThumbnailLocked() called on Task without stack");
535 }
536 return setLastThumbnailLocked(thumbnail, taskWidth, taskHeight, serviceConfig.orientation);
537 }
538
539 /**
540 * Sets the last thumbnail with the current task bounds.
541 * @return whether the thumbnail was set
542 */
543 private boolean setLastThumbnailLocked(Bitmap thumbnail, int taskWidth, int taskHeight,
544 int screenOrientation) {
Winson Chung096f36b2014-08-20 15:39:01 -0700545 if (mLastThumbnail != thumbnail) {
546 mLastThumbnail = thumbnail;
Winsonc809cbb2015-11-02 12:06:15 -0800547 mLastThumbnailInfo.taskWidth = taskWidth;
548 mLastThumbnailInfo.taskHeight = taskHeight;
549 mLastThumbnailInfo.screenOrientation = screenOrientation;
Winson Chung096f36b2014-08-20 15:39:01 -0700550 if (thumbnail == null) {
551 if (mLastThumbnailFile != null) {
552 mLastThumbnailFile.delete();
553 }
554 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800555 mService.mRecentTasks.saveImage(thumbnail, mLastThumbnailFile.getAbsolutePath());
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700556 }
Winson Chung096f36b2014-08-20 15:39:01 -0700557 return true;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700558 }
Winson Chung096f36b2014-08-20 15:39:01 -0700559 return false;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700560 }
561
562 void getLastThumbnail(TaskThumbnail thumbs) {
563 thumbs.mainThumbnail = mLastThumbnail;
Winsonc809cbb2015-11-02 12:06:15 -0800564 thumbs.thumbnailInfo = mLastThumbnailInfo;
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700565 thumbs.thumbnailFileDescriptor = null;
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700566 if (mLastThumbnail == null) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800567 thumbs.mainThumbnail = mService.mRecentTasks.getImageFromWriteQueue(
Suprabh Shukla23593142015-11-03 17:31:15 -0800568 mLastThumbnailFile.getAbsolutePath());
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700569 }
Winson Chung096f36b2014-08-20 15:39:01 -0700570 // Only load the thumbnail file if we don't have a thumbnail
571 if (thumbs.mainThumbnail == null && mLastThumbnailFile.exists()) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700572 try {
573 thumbs.thumbnailFileDescriptor = ParcelFileDescriptor.open(mLastThumbnailFile,
574 ParcelFileDescriptor.MODE_READ_ONLY);
575 } catch (IOException e) {
Dianne Hackborn9844d292013-10-04 16:44:22 -0700576 }
577 }
578 }
579
Winsonc809cbb2015-11-02 12:06:15 -0800580 /**
581 * Removes in-memory thumbnail data when the max number of in-memory task thumbnails is reached.
582 */
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700583 void freeLastThumbnail() {
584 mLastThumbnail = null;
585 }
586
Winsonc809cbb2015-11-02 12:06:15 -0800587 /**
588 * Removes all associated thumbnail data when a task is removed or pruned from recents.
589 */
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700590 void disposeThumbnail() {
Winsonc6a2da02015-11-11 18:11:59 -0800591 mLastThumbnailInfo.reset();
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700592 mLastThumbnail = null;
593 lastDescription = null;
594 }
595
Winson Chung1147c402014-05-14 11:05:00 -0700596 /** Returns the intent for the root activity for this task */
597 Intent getBaseIntent() {
598 return intent != null ? intent : affinityIntent;
599 }
600
Winson Chung3b3f4642014-04-22 10:08:18 -0700601 /** Returns the first non-finishing activity from the root. */
602 ActivityRecord getRootActivity() {
603 for (int i = 0; i < mActivities.size(); i++) {
604 final ActivityRecord r = mActivities.get(i);
605 if (r.finishing) {
606 continue;
607 }
608 return r;
609 }
610 return null;
611 }
612
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800613 ActivityRecord getTopActivity() {
614 for (int i = mActivities.size() - 1; i >= 0; --i) {
615 final ActivityRecord r = mActivities.get(i);
616 if (r.finishing) {
617 continue;
618 }
619 return r;
620 }
621 return null;
622 }
623
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700624 ActivityRecord topRunningActivityLocked() {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700625 if (stack != null) {
626 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
627 ActivityRecord r = mActivities.get(activityNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700628 if (!r.finishing && stack.okToShowLocked(r)) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700629 return r;
630 }
Craig Mautner6b74cb52013-09-27 17:02:21 -0700631 }
632 }
633 return null;
634 }
635
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800636 void setFrontOfTask() {
637 setFrontOfTask(null);
638 }
639
Craig Mautner3b475fe2013-12-16 15:58:31 -0800640 /** Call after activity movement or finish to make sure that frontOfTask is set correctly */
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800641 void setFrontOfTask(ActivityRecord newTop) {
642 // If a top candidate is suggested by the caller, go ahead and use it and mark all others
643 // as not front. This is needed in situations where the current front activity in the
644 // task isn't finished yet and we want to set the front to the activity moved to the front
645 // of the task.
646 boolean foundFront = newTop != null ? true : false;
647
Craig Mautner3b475fe2013-12-16 15:58:31 -0800648 final int numActivities = mActivities.size();
Craig Mautner704e40b2013-12-18 16:43:51 -0800649 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
Craig Mautner3b475fe2013-12-16 15:58:31 -0800650 final ActivityRecord r = mActivities.get(activityNdx);
651 if (foundFront || r.finishing) {
652 r.frontOfTask = false;
653 } else {
654 r.frontOfTask = true;
655 // Set frontOfTask false for every following activity.
656 foundFront = true;
657 }
658 }
Craig Mautner9587ee02014-06-23 15:00:10 +0000659 if (!foundFront && numActivities > 0) {
660 // All activities of this task are finishing. As we ought to have a frontOfTask
661 // activity, make the bottom activity front.
662 mActivities.get(0).frontOfTask = true;
663 }
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800664 if (newTop != null) {
665 newTop.frontOfTask = true;
666 }
Craig Mautner3b475fe2013-12-16 15:58:31 -0800667 }
668
Craig Mautnerde4ef022013-04-07 19:01:33 -0700669 /**
Craig Mautner3b475fe2013-12-16 15:58:31 -0800670 * Reorder the history stack so that the passed activity is brought to the front.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700671 */
672 final void moveActivityToFrontLocked(ActivityRecord newTop) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700673 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
674 "Removing and adding activity " + newTop
675 + " to stack at top callers=" + Debug.getCallers(4));
Craig Mautnerde4ef022013-04-07 19:01:33 -0700676
Craig Mautnerde4ef022013-04-07 19:01:33 -0700677 mActivities.remove(newTop);
678 mActivities.add(newTop);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700679 updateEffectiveIntent();
Craig Mautner3b475fe2013-12-16 15:58:31 -0800680
Wale Ogunwale6d42df62015-11-05 09:50:55 -0800681 setFrontOfTask(newTop);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700682 }
683
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800684 void addActivityAtBottom(ActivityRecord r) {
Craig Mautner77878772013-03-04 19:46:24 -0800685 addActivityAtIndex(0, r);
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800686 }
687
688 void addActivityToTop(ActivityRecord r) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700689 addActivityAtIndex(mActivities.size(), r);
690 }
691
692 void addActivityAtIndex(int index, ActivityRecord r) {
Craig Mautner6170f732013-04-02 13:05:23 -0700693 // 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 -0800694 if (!mActivities.remove(r) && r.fullscreen) {
695 // Was not previously in list.
696 numFullscreen++;
697 }
Craig Mautner2c1faed2013-07-23 12:56:02 -0700698 // Only set this based on the first activity
699 if (mActivities.isEmpty()) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700700 taskType = r.mActivityType;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -0800701 if (taskType == HOME_ACTIVITY_TYPE && mService.mForceResizableActivities) {
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800702 mResizeable = r.isResizeable();
Jorim Jaggi8ccdf3a2015-10-22 12:50:31 +0200703 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700704 isPersistable = r.isPersistable();
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700705 mCallingUid = r.launchedFromUid;
706 mCallingPackage = r.launchedFromPackage;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700707 // Clamp to [1, max].
708 maxRecents = Math.min(Math.max(r.info.maxRecents, 1),
709 ActivityManager.getMaxAppRecentsLimitStatic());
Craig Mautner2c1faed2013-07-23 12:56:02 -0700710 } else {
711 // Otherwise make all added activities match this one.
Craig Mautner21d24a22014-04-23 11:45:37 -0700712 r.mActivityType = taskType;
Craig Mautner78733002013-06-10 13:54:49 -0700713 }
Craig Mautner77878772013-03-04 19:46:24 -0800714 mActivities.add(index, r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700715 updateEffectiveIntent();
Craig Mautner21d24a22014-04-23 11:45:37 -0700716 if (r.isPersistable()) {
717 mService.notifyTaskPersisterLocked(this, false);
718 }
Craig Mautner77878772013-03-04 19:46:24 -0800719 }
720
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800721 /** @return true if this was the last activity in the task */
722 boolean removeActivity(ActivityRecord r) {
723 if (mActivities.remove(r) && r.fullscreen) {
724 // Was previously in list.
725 numFullscreen--;
726 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700727 if (r.isPersistable()) {
728 mService.notifyTaskPersisterLocked(this, false);
729 }
Craig Mautner41326202014-06-20 14:38:21 -0700730 if (mActivities.isEmpty()) {
Craig Mautner5afcd4d2014-06-21 18:11:33 -0700731 return !mReuseTask;
Craig Mautner41326202014-06-20 14:38:21 -0700732 }
733 updateEffectiveIntent();
734 return false;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800735 }
736
Craig Mautner41db4a72014-05-07 17:20:56 -0700737 boolean autoRemoveFromRecents() {
Dianne Hackbornd38aed82014-06-10 21:36:35 -0700738 // We will automatically remove the task either if it has explicitly asked for
739 // this, or it is empty and has never contained an activity that got shown to
740 // the user.
Dianne Hackborn13420f22014-07-18 15:43:56 -0700741 return autoRemoveRecents || (mActivities.isEmpty() && !hasBeenVisible);
Craig Mautner41db4a72014-05-07 17:20:56 -0700742 }
743
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700744 /**
745 * Completely remove all activities associated with an existing
746 * task starting at a specified index.
747 */
748 final void performClearTaskAtIndexLocked(int activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700749 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700750 for ( ; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700751 final ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700752 if (r.finishing) {
753 continue;
754 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700755 if (stack == null) {
756 // Task was restored from persistent storage.
757 r.takeFromHistory();
758 mActivities.remove(activityNdx);
759 --activityNdx;
760 --numActivities;
Todd Kennedy539db512014-12-15 09:57:55 -0800761 } else if (stack.finishActivityLocked(
762 r, Activity.RESULT_CANCELED, null, "clear-task-index", false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700763 --activityNdx;
764 --numActivities;
765 }
766 }
767 }
768
769 /**
770 * Completely remove all activities associated with an existing task.
771 */
772 final void performClearTaskLocked() {
Craig Mautner362449a2014-06-20 14:04:39 -0700773 mReuseTask = true;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700774 performClearTaskAtIndexLocked(0);
Craig Mautner362449a2014-06-20 14:04:39 -0700775 mReuseTask = false;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700776 }
777
Filip Gruszczynskibe9dabd2016-01-19 12:23:10 -0800778 ActivityRecord performClearTaskForReuseLocked(ActivityRecord newR, int launchFlags) {
779 mReuseTask = true;
780 final ActivityRecord result = performClearTaskLocked(newR, launchFlags);
781 mReuseTask = false;
782 return result;
783 }
784
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700785 /**
786 * Perform clear operation as requested by
787 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
788 * stack to the given task, then look for
789 * an instance of that activity in the stack and, if found, finish all
790 * activities on top of it and return the instance.
791 *
792 * @param newR Description of the new activity being started.
793 * @return Returns the old activity that should be continued to be used,
794 * or null if none was found.
795 */
796 final ActivityRecord performClearTaskLocked(ActivityRecord newR, int launchFlags) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700797 int numActivities = mActivities.size();
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700798 for (int activityNdx = numActivities - 1; activityNdx >= 0; --activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700799 ActivityRecord r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700800 if (r.finishing) {
801 continue;
802 }
803 if (r.realActivity.equals(newR.realActivity)) {
804 // Here it is! Now finish everything in front...
Craig Mautner1602ec22013-05-12 10:24:27 -0700805 final ActivityRecord ret = r;
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700806
807 for (++activityNdx; activityNdx < numActivities; ++activityNdx) {
Craig Mautner1602ec22013-05-12 10:24:27 -0700808 r = mActivities.get(activityNdx);
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700809 if (r.finishing) {
810 continue;
811 }
812 ActivityOptions opts = r.takeOptionsLocked();
813 if (opts != null) {
814 ret.updateOptionsLocked(opts);
815 }
Wale Ogunwale7d701172015-03-11 15:36:30 -0700816 if (stack != null && stack.finishActivityLocked(
Todd Kennedy539db512014-12-15 09:57:55 -0800817 r, Activity.RESULT_CANCELED, null, "clear-task-stack", false)) {
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700818 --activityNdx;
819 --numActivities;
820 }
821 }
822
823 // Finally, if this is a normal launch mode (that is, not
824 // expecting onNewIntent()), then we will finish the current
825 // instance of the activity so a new fresh one can be started.
826 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
827 && (launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0) {
828 if (!ret.finishing) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700829 if (stack != null) {
830 stack.finishActivityLocked(
831 ret, Activity.RESULT_CANCELED, null, "clear-task-top", false);
832 }
Craig Mautnerb0f7dc72013-04-01 16:34:45 -0700833 return null;
834 }
835 }
836
837 return ret;
838 }
839 }
840
841 return null;
842 }
843
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700844 public TaskThumbnail getTaskThumbnailLocked() {
Craig Mautner21d24a22014-04-23 11:45:37 -0700845 if (stack != null) {
846 final ActivityRecord resumedActivity = stack.mResumedActivity;
847 if (resumedActivity != null && resumedActivity.task == this) {
Winson8b1871d2015-11-20 09:56:20 -0800848 final Bitmap thumbnail = stack.screenshotActivitiesLocked(resumedActivity);
Winsonc809cbb2015-11-02 12:06:15 -0800849 setLastThumbnailLocked(thumbnail);
Craig Mautner21d24a22014-04-23 11:45:37 -0700850 }
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700851 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700852 final TaskThumbnail taskThumbnail = new TaskThumbnail();
853 getLastThumbnail(taskThumbnail);
854 return taskThumbnail;
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700855 }
856
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700857 public void removeTaskActivitiesLocked() {
858 // Just remove the entire task.
859 performClearTaskAtIndexLocked(0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -0700860 }
861
Craig Mautner432f64e2015-05-20 14:59:57 -0700862 String lockTaskAuthToString() {
863 switch (mLockTaskAuth) {
864 case LOCK_TASK_AUTH_DONT_LOCK: return "LOCK_TASK_AUTH_DONT_LOCK";
865 case LOCK_TASK_AUTH_PINNABLE: return "LOCK_TASK_AUTH_PINNABLE";
866 case LOCK_TASK_AUTH_LAUNCHABLE: return "LOCK_TASK_AUTH_LAUNCHABLE";
867 case LOCK_TASK_AUTH_WHITELISTED: return "LOCK_TASK_AUTH_WHITELISTED";
Benjamin Franz469dd582015-06-09 14:24:36 +0100868 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV: return "LOCK_TASK_AUTH_LAUNCHABLE_PRIV";
Craig Mautner432f64e2015-05-20 14:59:57 -0700869 default: return "unknown=" + mLockTaskAuth;
870 }
871 }
872
Craig Mautner15df08a2015-04-01 12:17:18 -0700873 void setLockTaskAuth() {
Benjamin Franz469dd582015-06-09 14:24:36 +0100874 if (!mPrivileged &&
875 (mLockTaskMode == LOCK_TASK_LAUNCH_MODE_ALWAYS ||
876 mLockTaskMode == LOCK_TASK_LAUNCH_MODE_NEVER)) {
877 // Non-priv apps are not allowed to use always or never, fall back to default
878 mLockTaskMode = LOCK_TASK_LAUNCH_MODE_DEFAULT;
879 }
Craig Mautner15df08a2015-04-01 12:17:18 -0700880 switch (mLockTaskMode) {
881 case LOCK_TASK_LAUNCH_MODE_DEFAULT:
882 mLockTaskAuth = isLockTaskWhitelistedLocked() ?
883 LOCK_TASK_AUTH_WHITELISTED : LOCK_TASK_AUTH_PINNABLE;
884 break;
885
886 case LOCK_TASK_LAUNCH_MODE_NEVER:
Benjamin Franz469dd582015-06-09 14:24:36 +0100887 mLockTaskAuth = LOCK_TASK_AUTH_DONT_LOCK;
Craig Mautner15df08a2015-04-01 12:17:18 -0700888 break;
889
890 case LOCK_TASK_LAUNCH_MODE_ALWAYS:
Benjamin Franz469dd582015-06-09 14:24:36 +0100891 mLockTaskAuth = LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -0700892 break;
893
894 case LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED:
895 mLockTaskAuth = isLockTaskWhitelistedLocked() ?
896 LOCK_TASK_AUTH_LAUNCHABLE : LOCK_TASK_AUTH_PINNABLE;
897 break;
898 }
Craig Mautner432f64e2015-05-20 14:59:57 -0700899 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "setLockTaskAuth: task=" + this +
900 " mLockTaskAuth=" + lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -0700901 }
902
903 boolean isLockTaskWhitelistedLocked() {
Benjamin Franz6fd84cc2015-08-06 18:09:03 +0100904 String pkg = (realActivity != null) ? realActivity.getPackageName() : null;
905 if (pkg == null) {
Craig Mautner15df08a2015-04-01 12:17:18 -0700906 return false;
907 }
908 String[] packages = mService.mLockTaskPackages.get(userId);
909 if (packages == null) {
910 return false;
911 }
912 for (int i = packages.length - 1; i >= 0; --i) {
Benjamin Franz6fd84cc2015-08-06 18:09:03 +0100913 if (pkg.equals(packages[i])) {
Craig Mautner15df08a2015-04-01 12:17:18 -0700914 return true;
915 }
916 }
917 return false;
918 }
Craig Mautnera82aa092013-09-13 15:34:08 -0700919 boolean isHomeTask() {
Craig Mautner84984fa2014-06-19 11:19:20 -0700920 return taskType == HOME_ACTIVITY_TYPE;
Craig Mautnera82aa092013-09-13 15:34:08 -0700921 }
922
Craig Mautner86d67a42013-05-14 10:34:38 -0700923 boolean isApplicationTask() {
Craig Mautner84984fa2014-06-19 11:19:20 -0700924 return taskType == APPLICATION_ACTIVITY_TYPE;
925 }
926
927 boolean isOverHomeStack() {
928 return mTaskToReturnTo == HOME_ACTIVITY_TYPE || mTaskToReturnTo == RECENTS_ACTIVITY_TYPE;
Craig Mautner1602ec22013-05-12 10:24:27 -0700929 }
930
Craig Mautner525f3d92013-05-07 14:01:50 -0700931 /**
932 * Find the activity in the history stack within the given task. Returns
933 * the index within the history at which it's found, or < 0 if not found.
934 */
935 final ActivityRecord findActivityInHistoryLocked(ActivityRecord r) {
936 final ComponentName realActivity = r.realActivity;
937 for (int activityNdx = mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
938 ActivityRecord candidate = mActivities.get(activityNdx);
939 if (candidate.finishing) {
940 continue;
941 }
942 if (candidate.realActivity.equals(realActivity)) {
943 return candidate;
944 }
945 }
946 return null;
947 }
948
Winson Chunga449dc02014-05-16 11:15:04 -0700949 /** Updates the last task description values. */
950 void updateTaskDescription() {
951 // Traverse upwards looking for any break between main task activities and
952 // utility activities.
953 int activityNdx;
954 final int numActivities = mActivities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700955 final boolean relinquish = numActivities == 0 ? false :
956 (mActivities.get(0).info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) != 0;
Winson Chunga449dc02014-05-16 11:15:04 -0700957 for (activityNdx = Math.min(numActivities, 1); activityNdx < numActivities;
Craig Mautner21d24a22014-04-23 11:45:37 -0700958 ++activityNdx) {
Winson Chunga449dc02014-05-16 11:15:04 -0700959 final ActivityRecord r = mActivities.get(activityNdx);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700960 if (relinquish && (r.info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
961 // This will be the top activity for determining taskDescription. Pre-inc to
962 // overcome initial decrement below.
963 ++activityNdx;
964 break;
965 }
Winson Chunga449dc02014-05-16 11:15:04 -0700966 if (r.intent != null &&
Craig Mautner9d4e9bc2014-06-18 18:34:56 -0700967 (r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Winson Chunga449dc02014-05-16 11:15:04 -0700968 break;
969 }
970 }
971 if (activityNdx > 0) {
972 // Traverse downwards starting below break looking for set label, icon.
973 // Note that if there are activities in the task but none of them set the
974 // recent activity values, then we do not fall back to the last set
975 // values in the TaskRecord.
976 String label = null;
Craig Mautner648f69b2014-09-18 14:16:26 -0700977 String iconFilename = null;
Winson Chunga449dc02014-05-16 11:15:04 -0700978 int colorPrimary = 0;
979 for (--activityNdx; activityNdx >= 0; --activityNdx) {
980 final ActivityRecord r = mActivities.get(activityNdx);
981 if (r.taskDescription != null) {
982 if (label == null) {
983 label = r.taskDescription.getLabel();
984 }
Craig Mautner648f69b2014-09-18 14:16:26 -0700985 if (iconFilename == null) {
986 iconFilename = r.taskDescription.getIconFilename();
Winson Chunga449dc02014-05-16 11:15:04 -0700987 }
988 if (colorPrimary == 0) {
989 colorPrimary = r.taskDescription.getPrimaryColor();
Winson Chunga449dc02014-05-16 11:15:04 -0700990 }
991 }
992 }
Craig Mautner648f69b2014-09-18 14:16:26 -0700993 lastTaskDescription = new TaskDescription(label, colorPrimary, iconFilename);
Winson Chungec396d62014-08-06 17:08:00 -0700994 // Update the task affiliation color if we are the parent of the group
995 if (taskId == mAffiliatedTaskId) {
996 mAffiliatedTaskColor = lastTaskDescription.getPrimaryColor();
997 }
Winson Chunga449dc02014-05-16 11:15:04 -0700998 }
999 }
1000
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001001 int findEffectiveRootIndex() {
Craig Mautner4767f4b2014-09-18 15:38:33 -07001002 int effectiveNdx = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001003 final int topActivityNdx = mActivities.size() - 1;
Dianne Hackborn39569af2014-09-23 10:56:58 -07001004 for (int activityNdx = 0; activityNdx <= topActivityNdx; ++activityNdx) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001005 final ActivityRecord r = mActivities.get(activityNdx);
1006 if (r.finishing) {
1007 continue;
1008 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001009 effectiveNdx = activityNdx;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001010 if ((r.info.flags & ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY) == 0) {
1011 break;
1012 }
1013 }
Craig Mautner4767f4b2014-09-18 15:38:33 -07001014 return effectiveNdx;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001015 }
1016
1017 void updateEffectiveIntent() {
1018 final int effectiveRootIndex = findEffectiveRootIndex();
1019 final ActivityRecord r = mActivities.get(effectiveRootIndex);
Winson Chungfee26772014-08-05 12:21:52 -07001020 setIntent(r);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001021 }
1022
Craig Mautner21d24a22014-04-23 11:45:37 -07001023 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001024 if (DEBUG_RECENTS) Slog.i(TAG_RECENTS, "Saving task=" + this);
Craig Mautner21d24a22014-04-23 11:45:37 -07001025
1026 out.attribute(null, ATTR_TASKID, String.valueOf(taskId));
1027 if (realActivity != null) {
1028 out.attribute(null, ATTR_REALACTIVITY, realActivity.flattenToShortString());
1029 }
1030 if (origActivity != null) {
1031 out.attribute(null, ATTR_ORIGACTIVITY, origActivity.flattenToShortString());
1032 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001033 // Write affinity, and root affinity if it is different from affinity.
1034 // We use the special string "@" for a null root affinity, so we can identify
1035 // later whether we were given a root affinity or should just make it the
1036 // same as the affinity.
Craig Mautner21d24a22014-04-23 11:45:37 -07001037 if (affinity != null) {
1038 out.attribute(null, ATTR_AFFINITY, affinity);
Dianne Hackborn79228822014-09-16 11:11:23 -07001039 if (!affinity.equals(rootAffinity)) {
1040 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
1041 }
1042 } else if (rootAffinity != null) {
1043 out.attribute(null, ATTR_ROOT_AFFINITY, rootAffinity != null ? rootAffinity : "@");
Craig Mautner21d24a22014-04-23 11:45:37 -07001044 }
1045 out.attribute(null, ATTR_ROOTHASRESET, String.valueOf(rootWasReset));
Dianne Hackborn13420f22014-07-18 15:43:56 -07001046 out.attribute(null, ATTR_AUTOREMOVERECENTS, String.valueOf(autoRemoveRecents));
Craig Mautner21d24a22014-04-23 11:45:37 -07001047 out.attribute(null, ATTR_ASKEDCOMPATMODE, String.valueOf(askedCompatMode));
1048 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001049 out.attribute(null, ATTR_EFFECTIVE_UID, String.valueOf(effectiveUid));
Craig Mautner21d24a22014-04-23 11:45:37 -07001050 out.attribute(null, ATTR_TASKTYPE, String.valueOf(taskType));
Winson Chungffa2ec62014-07-03 15:54:42 -07001051 out.attribute(null, ATTR_FIRSTACTIVETIME, String.valueOf(firstActiveTime));
Winson Chungf1fbd772014-06-24 18:06:58 -07001052 out.attribute(null, ATTR_LASTACTIVETIME, String.valueOf(lastActiveTime));
Craig Mautner21d24a22014-04-23 11:45:37 -07001053 out.attribute(null, ATTR_LASTTIMEMOVED, String.valueOf(mLastTimeMoved));
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001054 out.attribute(null, ATTR_NEVERRELINQUISH, String.valueOf(mNeverRelinquishIdentity));
Craig Mautner21d24a22014-04-23 11:45:37 -07001055 if (lastDescription != null) {
1056 out.attribute(null, ATTR_LASTDESCRIPTION, lastDescription.toString());
1057 }
Winson Chung2cb86c72014-06-25 12:03:30 -07001058 if (lastTaskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07001059 lastTaskDescription.saveToXml(out);
Winson Chung2cb86c72014-06-25 12:03:30 -07001060 }
Winsonc809cbb2015-11-02 12:06:15 -08001061 mLastThumbnailInfo.saveToXml(out);
Winson Chungec396d62014-08-06 17:08:00 -07001062 out.attribute(null, ATTR_TASK_AFFILIATION_COLOR, String.valueOf(mAffiliatedTaskColor));
Craig Mautnera228ae92014-07-09 05:44:55 -07001063 out.attribute(null, ATTR_TASK_AFFILIATION, String.valueOf(mAffiliatedTaskId));
1064 out.attribute(null, ATTR_PREV_AFFILIATION, String.valueOf(mPrevAffiliateTaskId));
1065 out.attribute(null, ATTR_NEXT_AFFILIATION, String.valueOf(mNextAffiliateTaskId));
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001066 out.attribute(null, ATTR_CALLING_UID, String.valueOf(mCallingUid));
1067 out.attribute(null, ATTR_CALLING_PACKAGE, mCallingPackage == null ? "" : mCallingPackage);
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001068 out.attribute(null, ATTR_RESIZEABLE, String.valueOf(mResizeable));
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001069 out.attribute(null, ATTR_PRIVILEGED, String.valueOf(mPrivileged));
Wale Ogunwale706ed792015-08-02 10:29:44 -07001070 if (mLastNonFullscreenBounds != null) {
1071 out.attribute(
1072 null, ATTR_NON_FULLSCREEN_BOUNDS, mLastNonFullscreenBounds.flattenToString());
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001073 }
Winson Chung2cb86c72014-06-25 12:03:30 -07001074
Craig Mautner21d24a22014-04-23 11:45:37 -07001075 if (affinityIntent != null) {
1076 out.startTag(null, TAG_AFFINITYINTENT);
1077 affinityIntent.saveToXml(out);
1078 out.endTag(null, TAG_AFFINITYINTENT);
1079 }
1080
1081 out.startTag(null, TAG_INTENT);
1082 intent.saveToXml(out);
1083 out.endTag(null, TAG_INTENT);
1084
1085 final ArrayList<ActivityRecord> activities = mActivities;
1086 final int numActivities = activities.size();
1087 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
1088 final ActivityRecord r = activities.get(activityNdx);
Craig Mautner43e52ed2014-06-16 17:18:52 -07001089 if (r.info.persistableMode == ActivityInfo.PERSIST_ROOT_ONLY || !r.isPersistable() ||
Wale Ogunwale843bfb92015-03-27 11:06:48 -07001090 ((r.intent.getFlags() & FLAG_ACTIVITY_NEW_DOCUMENT
1091 | FLAG_ACTIVITY_RETAIN_IN_RECENTS) == FLAG_ACTIVITY_NEW_DOCUMENT) &&
Craig Mautner43e52ed2014-06-16 17:18:52 -07001092 activityNdx > 0) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001093 // Stop at first non-persistable or first break in task (CLEAR_WHEN_TASK_RESET).
Craig Mautner21d24a22014-04-23 11:45:37 -07001094 break;
1095 }
1096 out.startTag(null, TAG_ACTIVITY);
1097 r.saveToXml(out);
1098 out.endTag(null, TAG_ACTIVITY);
1099 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001100 }
1101
1102 static TaskRecord restoreFromXml(XmlPullParser in, ActivityStackSupervisor stackSupervisor)
1103 throws IOException, XmlPullParserException {
1104 Intent intent = null;
1105 Intent affinityIntent = null;
Winsonc809cbb2015-11-02 12:06:15 -08001106 ArrayList<ActivityRecord> activities = new ArrayList<>();
Craig Mautner21d24a22014-04-23 11:45:37 -07001107 ComponentName realActivity = null;
1108 ComponentName origActivity = null;
1109 String affinity = null;
Dianne Hackborn79228822014-09-16 11:11:23 -07001110 String rootAffinity = null;
1111 boolean hasRootAffinity = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001112 boolean rootHasReset = false;
Dianne Hackborn13420f22014-07-18 15:43:56 -07001113 boolean autoRemoveRecents = false;
Craig Mautner21d24a22014-04-23 11:45:37 -07001114 boolean askedCompatMode = false;
1115 int taskType = ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner21d24a22014-04-23 11:45:37 -07001116 int userId = 0;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001117 int effectiveUid = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -07001118 String lastDescription = null;
Winson Chungffa2ec62014-07-03 15:54:42 -07001119 long firstActiveTime = -1;
Winson Chung2cb86c72014-06-25 12:03:30 -07001120 long lastActiveTime = -1;
Craig Mautner21d24a22014-04-23 11:45:37 -07001121 long lastTimeOnTop = 0;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001122 boolean neverRelinquishIdentity = true;
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001123 int taskId = INVALID_TASK_ID;
Craig Mautner21d24a22014-04-23 11:45:37 -07001124 final int outerDepth = in.getDepth();
Craig Mautner648f69b2014-09-18 14:16:26 -07001125 TaskDescription taskDescription = new TaskDescription();
Winsonc809cbb2015-11-02 12:06:15 -08001126 TaskThumbnailInfo thumbnailInfo = new TaskThumbnailInfo();
Wale Ogunwale18795a22014-12-03 11:38:33 -08001127 int taskAffiliation = INVALID_TASK_ID;
Winson Chungec396d62014-08-06 17:08:00 -07001128 int taskAffiliationColor = 0;
Wale Ogunwale18795a22014-12-03 11:38:33 -08001129 int prevTaskId = INVALID_TASK_ID;
1130 int nextTaskId = INVALID_TASK_ID;
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001131 int callingUid = -1;
1132 String callingPackage = "";
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001133 boolean resizeable = false;
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001134 boolean privileged = false;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001135 Rect bounds = null;
Craig Mautner21d24a22014-04-23 11:45:37 -07001136
1137 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
1138 final String attrName = in.getAttributeName(attrNdx);
1139 final String attrValue = in.getAttributeValue(attrNdx);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001140 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: attribute name=" +
1141 attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001142 if (ATTR_TASKID.equals(attrName)) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001143 if (taskId == INVALID_TASK_ID) taskId = Integer.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001144 } else if (ATTR_REALACTIVITY.equals(attrName)) {
1145 realActivity = ComponentName.unflattenFromString(attrValue);
1146 } else if (ATTR_ORIGACTIVITY.equals(attrName)) {
1147 origActivity = ComponentName.unflattenFromString(attrValue);
1148 } else if (ATTR_AFFINITY.equals(attrName)) {
1149 affinity = attrValue;
Dianne Hackborn79228822014-09-16 11:11:23 -07001150 } else if (ATTR_ROOT_AFFINITY.equals(attrName)) {
1151 rootAffinity = attrValue;
1152 hasRootAffinity = true;
Craig Mautner21d24a22014-04-23 11:45:37 -07001153 } else if (ATTR_ROOTHASRESET.equals(attrName)) {
1154 rootHasReset = Boolean.valueOf(attrValue);
Dianne Hackborn13420f22014-07-18 15:43:56 -07001155 } else if (ATTR_AUTOREMOVERECENTS.equals(attrName)) {
1156 autoRemoveRecents = Boolean.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001157 } else if (ATTR_ASKEDCOMPATMODE.equals(attrName)) {
1158 askedCompatMode = Boolean.valueOf(attrValue);
1159 } else if (ATTR_USERID.equals(attrName)) {
1160 userId = Integer.valueOf(attrValue);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001161 } else if (ATTR_EFFECTIVE_UID.equals(attrName)) {
1162 effectiveUid = Integer.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001163 } else if (ATTR_TASKTYPE.equals(attrName)) {
1164 taskType = Integer.valueOf(attrValue);
Winson Chungffa2ec62014-07-03 15:54:42 -07001165 } else if (ATTR_FIRSTACTIVETIME.equals(attrName)) {
1166 firstActiveTime = Long.valueOf(attrValue);
Winson Chungf1fbd772014-06-24 18:06:58 -07001167 } else if (ATTR_LASTACTIVETIME.equals(attrName)) {
1168 lastActiveTime = Long.valueOf(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001169 } else if (ATTR_LASTDESCRIPTION.equals(attrName)) {
1170 lastDescription = attrValue;
1171 } else if (ATTR_LASTTIMEMOVED.equals(attrName)) {
1172 lastTimeOnTop = Long.valueOf(attrValue);
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001173 } else if (ATTR_NEVERRELINQUISH.equals(attrName)) {
1174 neverRelinquishIdentity = Boolean.valueOf(attrValue);
Winsonc809cbb2015-11-02 12:06:15 -08001175 } else if (attrName.startsWith(TaskThumbnailInfo.ATTR_TASK_THUMBNAILINFO_PREFIX)) {
1176 thumbnailInfo.restoreFromXml(attrName, attrValue);
Craig Mautner648f69b2014-09-18 14:16:26 -07001177 } else if (attrName.startsWith(TaskDescription.ATTR_TASKDESCRIPTION_PREFIX)) {
1178 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautnera228ae92014-07-09 05:44:55 -07001179 } else if (ATTR_TASK_AFFILIATION.equals(attrName)) {
1180 taskAffiliation = Integer.valueOf(attrValue);
1181 } else if (ATTR_PREV_AFFILIATION.equals(attrName)) {
1182 prevTaskId = Integer.valueOf(attrValue);
1183 } else if (ATTR_NEXT_AFFILIATION.equals(attrName)) {
1184 nextTaskId = Integer.valueOf(attrValue);
Winson Chungec396d62014-08-06 17:08:00 -07001185 } else if (ATTR_TASK_AFFILIATION_COLOR.equals(attrName)) {
1186 taskAffiliationColor = Integer.valueOf(attrValue);
Craig Mautnerdc00cbe2014-07-20 17:48:47 -07001187 } else if (ATTR_CALLING_UID.equals(attrName)) {
1188 callingUid = Integer.valueOf(attrValue);
1189 } else if (ATTR_CALLING_PACKAGE.equals(attrName)) {
1190 callingPackage = attrValue;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001191 } else if (ATTR_RESIZEABLE.equals(attrName)) {
1192 resizeable = Boolean.valueOf(attrValue);
Wale Ogunwalea7afaa22015-05-01 20:39:18 -07001193 } else if (ATTR_PRIVILEGED.equals(attrName)) {
1194 privileged = Boolean.valueOf(attrValue);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001195 } else if (ATTR_NON_FULLSCREEN_BOUNDS.equals(attrName)) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001196 bounds = Rect.unflattenFromString(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07001197 } else {
1198 Slog.w(TAG, "TaskRecord: Unknown attribute=" + attrName);
1199 }
1200 }
1201
1202 int event;
1203 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
Ben Kwa8814cf42015-07-08 10:54:56 -07001204 (event != XmlPullParser.END_TAG || in.getDepth() >= outerDepth)) {
Craig Mautner21d24a22014-04-23 11:45:37 -07001205 if (event == XmlPullParser.START_TAG) {
1206 final String name = in.getName();
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001207 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: START_TAG name=" +
1208 name);
Craig Mautner21d24a22014-04-23 11:45:37 -07001209 if (TAG_AFFINITYINTENT.equals(name)) {
1210 affinityIntent = Intent.restoreFromXml(in);
1211 } else if (TAG_INTENT.equals(name)) {
1212 intent = Intent.restoreFromXml(in);
1213 } else if (TAG_ACTIVITY.equals(name)) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001214 ActivityRecord activity = ActivityRecord.restoreFromXml(in, stackSupervisor);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07001215 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG, "TaskRecord: activity=" +
1216 activity);
Craig Mautner21d24a22014-04-23 11:45:37 -07001217 if (activity != null) {
1218 activities.add(activity);
1219 }
1220 } else {
1221 Slog.e(TAG, "restoreTask: Unexpected name=" + name);
1222 XmlUtils.skipCurrentTag(in);
1223 }
1224 }
1225 }
Dianne Hackborn79228822014-09-16 11:11:23 -07001226 if (!hasRootAffinity) {
1227 rootAffinity = affinity;
1228 } else if ("@".equals(rootAffinity)) {
1229 rootAffinity = null;
1230 }
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001231 if (effectiveUid <= 0) {
1232 Intent checkIntent = intent != null ? intent : affinityIntent;
1233 effectiveUid = 0;
1234 if (checkIntent != null) {
1235 IPackageManager pm = AppGlobals.getPackageManager();
1236 try {
1237 ApplicationInfo ai = pm.getApplicationInfo(
1238 checkIntent.getComponent().getPackageName(),
1239 PackageManager.GET_UNINSTALLED_PACKAGES
1240 | PackageManager.GET_DISABLED_COMPONENTS, userId);
1241 if (ai != null) {
1242 effectiveUid = ai.uid;
1243 }
1244 } catch (RemoteException e) {
1245 }
1246 }
1247 Slog.w(TAG, "Updating task #" + taskId + " for " + checkIntent
1248 + ": effectiveUid=" + effectiveUid);
1249 }
1250
Craig Mautner21d24a22014-04-23 11:45:37 -07001251 final TaskRecord task = new TaskRecord(stackSupervisor.mService, taskId, intent,
Dianne Hackborn79228822014-09-16 11:11:23 -07001252 affinityIntent, affinity, rootAffinity, realActivity, origActivity, rootHasReset,
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001253 autoRemoveRecents, askedCompatMode, taskType, userId, effectiveUid, lastDescription,
1254 activities, firstActiveTime, lastActiveTime, lastTimeOnTop, neverRelinquishIdentity,
Winsonc809cbb2015-11-02 12:06:15 -08001255 taskDescription, thumbnailInfo, taskAffiliation, prevTaskId, nextTaskId,
1256 taskAffiliationColor, callingUid, callingPackage, resizeable, privileged);
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07001257 task.updateOverrideConfiguration(bounds);
Craig Mautner21d24a22014-04-23 11:45:37 -07001258
1259 for (int activityNdx = activities.size() - 1; activityNdx >=0; --activityNdx) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001260 activities.get(activityNdx).task = task;
Craig Mautner21d24a22014-04-23 11:45:37 -07001261 }
1262
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001263 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Restored task=" + task);
Craig Mautner21d24a22014-04-23 11:45:37 -07001264 return task;
1265 }
1266
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001267 /**
1268 * Update task's override configuration based on the bounds.
1269 * @return Update configuration or null if there is no change.
1270 */
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07001271 Configuration updateOverrideConfiguration(Rect bounds) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001272 if (Objects.equals(mBounds, bounds)) {
1273 return null;
1274 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001275 final Configuration oldConfig = mOverrideConfig;
1276 final boolean oldFullscreen = mFullscreen;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001277
1278 mFullscreen = bounds == null;
Wale Ogunwale706ed792015-08-02 10:29:44 -07001279 if (mFullscreen) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07001280 if (mBounds != null && StackId.persistTaskBounds(stack.mStackId)) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001281 mLastNonFullscreenBounds = mBounds;
1282 }
1283 mBounds = null;
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001284 mOverrideConfig = Configuration.EMPTY;
Wale Ogunwale706ed792015-08-02 10:29:44 -07001285 } else {
Chong Zhangb15758a2015-11-17 12:12:03 -08001286 if (mBounds == null) {
1287 mBounds = new Rect(bounds);
1288 } else {
1289 mBounds.set(bounds);
1290 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07001291 if (stack == null || StackId.persistTaskBounds(stack.mStackId)) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001292 mLastNonFullscreenBounds = mBounds;
1293 }
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08001294 mOverrideConfig = calculateOverrideConfig(mBounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001295 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001296
1297 if (mFullscreen != oldFullscreen) {
1298 reportMultiWindowModeChange();
1299 }
1300
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07001301 return !mOverrideConfig.equals(oldConfig) ? mOverrideConfig : null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001302 }
1303
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08001304 Configuration calculateOverrideConfig(Rect bounds) {
1305 final Configuration serviceConfig = mService.mConfiguration;
1306 final Configuration config = new Configuration(Configuration.EMPTY);
1307 // TODO(multidisplay): Update Dp to that of display stack is on.
1308 final float density = serviceConfig.densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE;
1309 config.screenWidthDp =
1310 Math.min((int)(bounds.width() / density), serviceConfig.screenWidthDp);
1311 config.screenHeightDp =
1312 Math.min((int)(bounds.height() / density), serviceConfig.screenHeightDp);
1313 config.smallestScreenWidthDp =
1314 Math.min(config.screenWidthDp, config.screenHeightDp);
1315 config.orientation = (config.screenWidthDp <= config.screenHeightDp)
1316 ? Configuration.ORIENTATION_PORTRAIT
1317 : Configuration.ORIENTATION_LANDSCAPE;
1318 final int sl = Configuration.resetScreenLayout(serviceConfig.screenLayout);
1319 config.screenLayout = Configuration.reduceScreenLayout(
1320 sl, config.screenWidthDp, config.screenHeightDp);
1321 return config;
1322 }
1323
1324 /**
1325 * Using the existing configuration {@param config}, creates a new task override config such
1326 * that all the fields that are usually set in an override config are set to the ones in
1327 * {@param config}.
1328 */
1329 Configuration extractOverrideConfig(Configuration config) {
1330 final Configuration extracted = new Configuration(Configuration.EMPTY);
1331 extracted.screenWidthDp = config.screenWidthDp;
1332 extracted.screenHeightDp = config.screenHeightDp;
1333 extracted.smallestScreenWidthDp = config.smallestScreenWidthDp;
1334 extracted.orientation = config.orientation;
1335 extracted.screenLayout = config.screenLayout;
1336 return extracted;
1337 }
1338
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08001339 Rect updateOverrideConfigurationFromLaunchBounds() {
1340 final Rect bounds = validateBounds(getLaunchBounds());
1341 updateOverrideConfiguration(bounds);
1342 return bounds;
1343 }
1344
1345 static Rect validateBounds(Rect bounds) {
1346 if (bounds != null && bounds.isEmpty()) {
1347 Slog.wtf(TAG, "Received strange task bounds: " + bounds, new Throwable());
1348 return null;
1349 }
1350 return bounds;
1351 }
1352
Wale Ogunwale5f986092015-12-04 15:35:38 -08001353 private void reportMultiWindowModeChange() {
1354 for (int i = mActivities.size() - 1; i >= 0; i--) {
1355 final ActivityRecord r = mActivities.get(i);
1356 if (r.app != null && r.app.thread != null) {
1357 try {
1358 // An activity is consider to be in multi-window mode if its task isn't
1359 // fullscreen.
1360 r.app.thread.scheduleMultiWindowModeChanged(r.appToken, !mFullscreen);
1361 } catch (Exception e) {
1362 Slog.e(TAG, "TaskRecord.reportMultiWindowModeChange: ", e);
1363 }
1364 }
1365 }
1366 }
1367
Wale Ogunwaled046a012015-12-24 13:05:59 -08001368 void reportPictureInPictureModeChangeIfNeeded(ActivityStack prevStack) {
1369 if (prevStack == null || prevStack == stack
1370 || (prevStack.mStackId != PINNED_STACK_ID && stack.mStackId != PINNED_STACK_ID)) {
1371 return;
1372 }
1373
Wale Ogunwale5f986092015-12-04 15:35:38 -08001374 for (int i = mActivities.size() - 1; i >= 0; i--) {
1375 final ActivityRecord r = mActivities.get(i);
1376 if (r.app != null && r.app.thread != null) {
1377 try {
1378 r.app.thread.schedulePictureInPictureModeChanged(
1379 r.appToken, stack.mStackId == PINNED_STACK_ID);
1380 } catch (Exception e) {
1381 Slog.e(TAG, "TaskRecord.reportMultiWindowModeChange: ", e);
1382 }
1383 }
1384 }
1385 }
1386
Wale Ogunwale935e5022015-11-10 12:36:10 -08001387 /** Updates the task's bounds and override configuration to match what is expected for the
1388 * input stack. */
1389 void updateOverrideConfigurationForStack(ActivityStack inStack) {
1390 if (stack != null && stack == inStack) {
1391 return;
1392 }
1393
1394 if (inStack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1395 if (!mResizeable) {
1396 throw new IllegalArgumentException("Can not position non-resizeable task="
1397 + this + " in stack=" + inStack);
1398 }
1399 if (mBounds != null) {
1400 return;
1401 }
1402 if (mLastNonFullscreenBounds != null) {
1403 updateOverrideConfiguration(mLastNonFullscreenBounds);
1404 } else {
1405 inStack.layoutTaskInStack(this, null);
1406 }
1407 } else {
1408 updateOverrideConfiguration(inStack.mBounds);
1409 }
1410 }
1411
Chong Zhang0fa656b2015-08-31 15:17:21 -07001412 /**
1413 * Returns the correct stack to use based on task type and currently set bounds,
1414 * regardless of the focused stack and current stack association of the task.
1415 * The task will be moved (and stack focus changed) later if necessary.
1416 */
1417 int getLaunchStackId() {
1418 if (!isApplicationTask()) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001419 return HOME_STACK_ID;
1420 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07001421 if (mBounds != null) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07001422 return FREEFORM_WORKSPACE_STACK_ID;
1423 }
1424 return FULLSCREEN_WORKSPACE_STACK_ID;
1425 }
1426
1427 /** Returns the bounds that should be used to launch this task. */
1428 Rect getLaunchBounds() {
Chong Zhang75b37202015-12-04 14:16:36 -08001429 // If we're over lockscreen, forget about stack bounds and use fullscreen.
1430 if (mService.mLockScreenShown == LOCK_SCREEN_SHOWN) {
1431 return null;
1432 }
1433
Chong Zhang7d5f5102016-01-13 10:29:24 -08001434 if (stack == null) {
1435 return null;
1436 }
1437
1438 final int stackId = stack.mStackId;
1439 if (stackId == HOME_STACK_ID
1440 || stackId == FULLSCREEN_WORKSPACE_STACK_ID
1441 || (stackId == DOCKED_STACK_ID && !mResizeable)) {
1442 return mResizeable ? stack.mBounds : null;
Wale Ogunwale3797c222015-10-27 14:21:58 -07001443 } else if (!StackId.persistTaskBounds(stackId)) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001444 return stack.mBounds;
Wale Ogunwale706ed792015-08-02 10:29:44 -07001445 }
1446 return mLastNonFullscreenBounds;
1447 }
1448
Wale Ogunwale39381972015-12-17 17:15:29 -08001449 boolean canMatchRootAffinity() {
1450 // We don't allow root affinity matching on the pinned stack as no other task should
1451 // be launching in it based on affinity.
1452 return rootAffinity != null && (stack == null || stack.mStackId != PINNED_STACK_ID);
1453 }
1454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001456 pw.print(prefix); pw.print("userId="); pw.print(userId);
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001457 pw.print(" effectiveUid="); UserHandle.formatUid(pw, effectiveUid);
1458 pw.print(" mCallingUid="); UserHandle.formatUid(pw, mCallingUid);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001459 pw.print(" mCallingPackage="); pw.println(mCallingPackage);
Dianne Hackborn79228822014-09-16 11:11:23 -07001460 if (affinity != null || rootAffinity != null) {
1461 pw.print(prefix); pw.print("affinity="); pw.print(affinity);
1462 if (affinity == null || !affinity.equals(rootAffinity)) {
1463 pw.print(" root="); pw.println(rootAffinity);
1464 } else {
1465 pw.println();
1466 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001467 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07001468 if (voiceSession != null || voiceInteractor != null) {
1469 pw.print(prefix); pw.print("VOICE: session=0x");
1470 pw.print(Integer.toHexString(System.identityHashCode(voiceSession)));
1471 pw.print(" interactor=0x");
1472 pw.println(Integer.toHexString(System.identityHashCode(voiceInteractor)));
1473 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001474 if (intent != null) {
1475 StringBuilder sb = new StringBuilder(128);
1476 sb.append(prefix); sb.append("intent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08001477 intent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001478 sb.append('}');
1479 pw.println(sb.toString());
1480 }
1481 if (affinityIntent != null) {
1482 StringBuilder sb = new StringBuilder(128);
1483 sb.append(prefix); sb.append("affinityIntent={");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08001484 affinityIntent.toShortString(sb, false, true, false, true);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001485 sb.append('}');
1486 pw.println(sb.toString());
1487 }
1488 if (origActivity != null) {
1489 pw.print(prefix); pw.print("origActivity=");
1490 pw.println(origActivity.flattenToShortString());
1491 }
1492 if (realActivity != null) {
1493 pw.print(prefix); pw.print("realActivity=");
1494 pw.println(realActivity.flattenToShortString());
1495 }
Dianne Hackborn852975d2014-08-22 17:42:43 -07001496 if (autoRemoveRecents || isPersistable || taskType != 0 || mTaskToReturnTo != 0
1497 || numFullscreen != 0) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001498 pw.print(prefix); pw.print("autoRemoveRecents="); pw.print(autoRemoveRecents);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001499 pw.print(" isPersistable="); pw.print(isPersistable);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001500 pw.print(" numFullscreen="); pw.print(numFullscreen);
1501 pw.print(" taskType="); pw.print(taskType);
1502 pw.print(" mTaskToReturnTo="); pw.println(mTaskToReturnTo);
1503 }
Craig Mautner432f64e2015-05-20 14:59:57 -07001504 if (rootWasReset || mNeverRelinquishIdentity || mReuseTask
1505 || mLockTaskAuth != LOCK_TASK_AUTH_PINNABLE) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001506 pw.print(prefix); pw.print("rootWasReset="); pw.print(rootWasReset);
1507 pw.print(" mNeverRelinquishIdentity="); pw.print(mNeverRelinquishIdentity);
Craig Mautner432f64e2015-05-20 14:59:57 -07001508 pw.print(" mReuseTask="); pw.print(mReuseTask);
1509 pw.print(" mLockTaskAuth="); pw.println(lockTaskAuthToString());
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001510 }
Wale Ogunwale18795a22014-12-03 11:38:33 -08001511 if (mAffiliatedTaskId != taskId || mPrevAffiliateTaskId != INVALID_TASK_ID
1512 || mPrevAffiliate != null || mNextAffiliateTaskId != INVALID_TASK_ID
1513 || mNextAffiliate != null) {
Dianne Hackborn852975d2014-08-22 17:42:43 -07001514 pw.print(prefix); pw.print("affiliation="); pw.print(mAffiliatedTaskId);
1515 pw.print(" prevAffiliation="); pw.print(mPrevAffiliateTaskId);
1516 pw.print(" (");
1517 if (mPrevAffiliate == null) {
1518 pw.print("null");
1519 } else {
1520 pw.print(Integer.toHexString(System.identityHashCode(mPrevAffiliate)));
1521 }
1522 pw.print(") nextAffiliation="); pw.print(mNextAffiliateTaskId);
1523 pw.print(" (");
1524 if (mNextAffiliate == null) {
1525 pw.print("null");
1526 } else {
1527 pw.print(Integer.toHexString(System.identityHashCode(mNextAffiliate)));
1528 }
1529 pw.println(")");
1530 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001531 pw.print(prefix); pw.print("Activities="); pw.println(mActivities);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001532 if (!askedCompatMode || !inRecents || !isAvailable) {
1533 pw.print(prefix); pw.print("askedCompatMode="); pw.print(askedCompatMode);
1534 pw.print(" inRecents="); pw.print(inRecents);
1535 pw.print(" isAvailable="); pw.println(isAvailable);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07001536 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001537 pw.print(prefix); pw.print("lastThumbnail="); pw.print(mLastThumbnail);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001538 pw.print(" lastThumbnailFile="); pw.println(mLastThumbnailFile);
1539 if (lastDescription != null) {
1540 pw.print(prefix); pw.print("lastDescription="); pw.println(lastDescription);
1541 }
Wale Ogunwale069fbe42015-07-29 11:38:01 -07001542 if (stack != null) {
1543 pw.print(prefix); pw.print("stackId="); pw.println(stack.mStackId);
1544 }
Dianne Hackbornd38aed82014-06-10 21:36:35 -07001545 pw.print(prefix); pw.print("hasBeenVisible="); pw.print(hasBeenVisible);
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08001546 pw.print(" mResizeable="); pw.print(mResizeable);
Winson Chungffa2ec62014-07-03 15:54:42 -07001547 pw.print(" firstActiveTime="); pw.print(lastActiveTime);
Dianne Hackbornd38aed82014-06-10 21:36:35 -07001548 pw.print(" lastActiveTime="); pw.print(lastActiveTime);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001549 pw.print(" (inactive for ");
1550 pw.print((getInactiveDuration()/1000)); pw.println("s)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 }
1552
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001553 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001555 StringBuilder sb = new StringBuilder(128);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001556 if (stringName != null) {
1557 sb.append(stringName);
1558 sb.append(" U=");
1559 sb.append(userId);
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08001560 sb.append(" StackId=");
1561 sb.append(stack != null ? stack.mStackId : INVALID_STACK_ID);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001562 sb.append(" sz=");
1563 sb.append(mActivities.size());
1564 sb.append('}');
1565 return sb.toString();
1566 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001567 sb.append("TaskRecord{");
1568 sb.append(Integer.toHexString(System.identityHashCode(this)));
1569 sb.append(" #");
1570 sb.append(taskId);
1571 if (affinity != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001572 sb.append(" A=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001573 sb.append(affinity);
1574 } else if (intent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001575 sb.append(" I=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001576 sb.append(intent.getComponent().flattenToShortString());
1577 } else if (affinityIntent != null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001578 sb.append(" aI=");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001579 sb.append(affinityIntent.getComponent().flattenToShortString());
1580 } else {
1581 sb.append(" ??");
1582 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001583 stringName = sb.toString();
1584 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 }
1586}